LCOV - code coverage report
Current view: top level - src/leveldb/db - builder.cc (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 94.3 % 35 33
Test Date: 2026-02-04 04:43:42 Functions: 100.0 % 1 1
Branches: 47.8 % 92 44

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
       2                 :             : // Use of this source code is governed by a BSD-style license that can be
       3                 :             : // found in the LICENSE file. See the AUTHORS file for names of contributors.
       4                 :             : 
       5                 :             : #include "db/builder.h"
       6                 :             : 
       7                 :             : #include "db/dbformat.h"
       8                 :             : #include "db/filename.h"
       9                 :             : #include "db/table_cache.h"
      10                 :             : #include "db/version_edit.h"
      11                 :             : #include "leveldb/db.h"
      12                 :             : #include "leveldb/env.h"
      13                 :             : #include "leveldb/iterator.h"
      14                 :             : 
      15                 :             : namespace leveldb {
      16                 :             : 
      17                 :          62 : Status BuildTable(const std::string& dbname, Env* env, const Options& options,
      18                 :             :                   TableCache* table_cache, Iterator* iter, FileMetaData* meta) {
      19         [ +  - ]:          62 :   Status s;
      20                 :          62 :   meta->file_size = 0;
      21         [ +  - ]:          62 :   iter->SeekToFirst();
      22                 :             : 
      23         [ +  - ]:          62 :   std::string fname = TableFileName(dbname, meta->number);
      24   [ +  -  +  - ]:          62 :   if (iter->Valid()) {
      25                 :          62 :     WritableFile* file;
      26   [ +  -  -  + ]:          62 :     s = env->NewWritableFile(fname, &file);
      27         [ +  - ]:          62 :     if (!s.ok()) {
      28                 :             :       return s;
      29                 :             :     }
      30                 :             : 
      31   [ +  -  +  - ]:          62 :     TableBuilder* builder = new TableBuilder(options, file);
      32   [ +  -  +  - ]:          62 :     meta->smallest.DecodeFrom(iter->key());
      33   [ +  -  +  -  :       54034 :     for (; iter->Valid(); iter->Next()) {
                   +  + ]
      34         [ +  - ]:       26986 :       Slice key = iter->key();
      35         [ +  - ]:       26986 :       meta->largest.DecodeFrom(key);
      36   [ +  -  +  - ]:       26986 :       builder->Add(key, iter->value());
      37                 :             :     }
      38                 :             : 
      39                 :             :     // Finish and check for builder errors
      40   [ +  -  -  + ]:          62 :     s = builder->Finish();
      41         [ +  - ]:          62 :     if (s.ok()) {
      42         [ +  - ]:          62 :       meta->file_size = builder->FileSize();
      43         [ -  + ]:          62 :       assert(meta->file_size > 0);
      44                 :             :     }
      45         [ +  - ]:          62 :     delete builder;
      46                 :             : 
      47                 :             :     // Finish and check for file errors
      48         [ +  - ]:          62 :     if (s.ok()) {
      49   [ +  -  -  + ]:          62 :       s = file->Sync();
      50                 :             :     }
      51         [ +  - ]:          62 :     if (s.ok()) {
      52   [ +  -  -  + ]:          62 :       s = file->Close();
      53                 :             :     }
      54         [ +  - ]:          62 :     delete file;
      55                 :          62 :     file = nullptr;
      56                 :             : 
      57   [ +  -  +  - ]:         124 :     if (s.ok()) {
      58                 :             :       // Verify that the table is usable
      59         [ +  - ]:          62 :       Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number,
      60                 :             :                                               meta->file_size);
      61   [ +  -  -  + ]:          62 :       s = it->status();
      62         [ +  - ]:          62 :       delete it;
      63                 :             :     }
      64                 :             :   }
      65                 :             : 
      66                 :             :   // Check for input iterator errors
      67   [ +  -  -  +  :          62 :   if (!iter->status().ok()) {
                   -  + ]
      68   [ #  #  #  # ]:           0 :     s = iter->status();
      69                 :             :   }
      70                 :             : 
      71   [ +  -  -  + ]:          62 :   if (s.ok() && meta->file_size > 0) {
      72                 :             :     // Keep it
      73                 :             :   } else {
      74         [ #  # ]:           0 :     env->DeleteFile(fname);
      75                 :             :   }
      76                 :             :   return s;
      77                 :          62 : }
      78                 :             : 
      79                 :             : }  // namespace leveldb
        

Generated by: LCOV version 2.0-1