LCOV - code coverage report
Current view: top level - src - dbwrapper.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 71.2 % 226 161
Test Date: 2026-08-23 07:04:24 Functions: 93.9 % 33 31
Branches: 36.8 % 326 120

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2012-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <dbwrapper.h>
       6                 :             : 
       7                 :             : #include <leveldb/cache.h>
       8                 :             : #include <leveldb/db.h>
       9                 :             : #include <leveldb/env.h>
      10                 :             : #include <leveldb/filter_policy.h>
      11                 :             : #include <leveldb/helpers/memenv/memenv.h>
      12                 :             : #include <leveldb/iterator.h>
      13                 :             : #include <leveldb/options.h>
      14                 :             : #include <leveldb/slice.h>
      15                 :             : #include <leveldb/status.h>
      16                 :             : #include <leveldb/write_batch.h>
      17                 :             : #include <random.h>
      18                 :             : #include <serialize.h>
      19                 :             : #include <span.h>
      20                 :             : #include <streams.h>
      21                 :             : #include <util/byte_units.h>
      22                 :             : #include <util/fs.h>
      23                 :             : #include <util/fs_helpers.h>
      24                 :             : #include <util/log.h>
      25                 :             : #include <util/obfuscation.h>
      26                 :             : #include <util/strencodings.h>
      27                 :             : 
      28                 :             : #include <algorithm>
      29                 :             : #include <cassert>
      30                 :             : #include <cstdarg>
      31                 :             : #include <cstdint>
      32                 :             : #include <cstdio>
      33                 :             : #include <memory>
      34                 :             : #include <optional>
      35                 :             : #include <utility>
      36                 :             : 
      37                 :    23693577 : static auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
      38                 :             : 
      39                 :           0 : bool DestroyDB(const std::string& path_str)
      40                 :             : {
      41         [ #  # ]:           0 :     return leveldb::DestroyDB(path_str, {}).ok();
      42                 :             : }
      43                 :             : 
      44                 :             : /** Handle database error by throwing dbwrapper_error exception.
      45                 :             :  */
      46                 :     1909589 : static void HandleError(const leveldb::Status& status)
      47                 :             : {
      48         [ +  - ]:     1909589 :     if (status.ok())
      49                 :     1909589 :         return;
      50         [ #  # ]:           0 :     const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
      51         [ #  # ]:           0 :     LogError("%s", errmsg);
      52         [ #  # ]:           0 :     LogInfo("You can use -debug=leveldb to get more complete diagnostic messages");
      53         [ #  # ]:           0 :     throw dbwrapper_error(errmsg);
      54                 :           0 : }
      55                 :             : 
      56                 :           0 : class CBitcoinLevelDBLogger : public leveldb::Logger {
      57                 :             : public:
      58                 :             :     // This code is adapted from posix_logger.h, which is why it is using vsprintf.
      59                 :             :     // Please do not do this in normal code
      60                 :      871711 :     void Logv(const char * format, va_list ap) override {
      61         [ -  + ]:      871711 :             if (!util::log::ShouldDebugLog(BCLog::LEVELDB)) {
      62                 :             :                 return;
      63                 :             :             }
      64                 :             :             char buffer[500];
      65         [ #  # ]:           0 :             for (int iter = 0; iter < 2; iter++) {
      66                 :           0 :                 char* base;
      67                 :           0 :                 int bufsize;
      68         [ #  # ]:           0 :                 if (iter == 0) {
      69                 :             :                     bufsize = sizeof(buffer);
      70                 :             :                     base = buffer;
      71                 :             :                 }
      72                 :             :                 else {
      73                 :           0 :                     bufsize = 30000;
      74                 :           0 :                     base = new char[bufsize];
      75                 :             :                 }
      76                 :           0 :                 char* p = base;
      77                 :           0 :                 char* limit = base + bufsize;
      78                 :             : 
      79                 :             :                 // Print the message
      80         [ #  # ]:           0 :                 if (p < limit) {
      81                 :           0 :                     va_list backup_ap;
      82                 :           0 :                     va_copy(backup_ap, ap);
      83                 :             :                     // Do not use vsnprintf elsewhere in bitcoin source code, see above.
      84                 :           0 :                     p += vsnprintf(p, limit - p, format, backup_ap);
      85                 :           0 :                     va_end(backup_ap);
      86                 :             :                 }
      87                 :             : 
      88                 :             :                 // Truncate to available space if necessary
      89         [ #  # ]:           0 :                 if (p >= limit) {
      90         [ #  # ]:           0 :                     if (iter == 0) {
      91                 :           0 :                         continue;       // Try again with larger buffer
      92                 :             :                     }
      93                 :             :                     else {
      94                 :           0 :                         p = limit - 1;
      95                 :             :                     }
      96                 :             :                 }
      97                 :             : 
      98                 :             :                 // Add newline if necessary
      99   [ #  #  #  # ]:           0 :                 if (p == base || p[-1] != '\n') {
     100                 :           0 :                     *p++ = '\n';
     101                 :             :                 }
     102                 :             : 
     103         [ #  # ]:           0 :                 assert(p <= limit);
     104         [ #  # ]:           0 :                 base[std::min(bufsize - 1, (int)(p - base))] = '\0';
     105         [ #  # ]:           0 :                 LogDebug(BCLog::LEVELDB, "%s\n", util::RemoveSuffixView(base, "\n"));
     106         [ #  # ]:           0 :                 if (base != buffer) {
     107         [ #  # ]:           0 :                     delete[] base;
     108                 :             :                 }
     109                 :             :                 break;
     110                 :             :             }
     111                 :             :     }
     112                 :             : };
     113                 :             : 
     114                 :       95728 : static void SetMaxOpenFiles(leveldb::Options *options) {
     115                 :             :     // On most platforms the default setting of max_open_files (which is 1000)
     116                 :             :     // is optimal. On Windows using a large file count is OK because the handles
     117                 :             :     // do not interfere with select() loops. On 64-bit Unix hosts this value is
     118                 :             :     // also OK, because up to that amount LevelDB will use an mmap
     119                 :             :     // implementation that does not use extra file descriptors (the fds are
     120                 :             :     // closed after being mmap'ed).
     121                 :             :     //
     122                 :             :     // Increasing the value beyond the default is dangerous because LevelDB will
     123                 :             :     // fall back to a non-mmap implementation when the file count is too large.
     124                 :             :     // On 32-bit Unix host we should decrease the value because the handles use
     125                 :             :     // up real fds, and we want to avoid fd exhaustion issues.
     126                 :             :     //
     127                 :             :     // See PR #12495 for further discussion.
     128                 :             : 
     129                 :       95728 :     int default_open_files = options->max_open_files;
     130                 :             : #ifndef WIN32
     131                 :       95728 :     if (sizeof(void*) < 8) {
     132                 :             :         options->max_open_files = 64;
     133                 :             :     }
     134                 :             : #endif
     135         [ -  + ]:       95728 :     LogDebug(BCLog::LEVELDB, "LevelDB using max_open_files=%d (default=%d)\n",
     136                 :             :              options->max_open_files, default_open_files);
     137                 :       95728 : }
     138                 :             : 
     139                 :       95728 : static leveldb::Options GetOptions(size_t nCacheSize, bool bloom_filter)
     140                 :             : {
     141                 :       95728 :     leveldb::Options options;
     142                 :       95728 :     options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
     143                 :       95728 :     options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
     144         [ +  + ]:       95728 :     options.filter_policy = bloom_filter ? leveldb::NewBloomFilterPolicy(10) : nullptr;
     145                 :       95728 :     options.compression = leveldb::kNoCompression;
     146                 :       95728 :     options.info_log = new CBitcoinLevelDBLogger();
     147                 :       95728 :     if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
     148                 :             :         // LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
     149                 :             :         // on corruption in later versions.
     150                 :       95728 :         options.paranoid_checks = true;
     151                 :             :     }
     152                 :       95728 :     SetMaxOpenFiles(&options);
     153                 :       95728 :     return options;
     154                 :             : }
     155                 :             : 
     156                 :           0 : bool CDBWrapper::HasKeyStartingWith(const fs::path& path, uint8_t prefix)
     157                 :             : {
     158   [ #  #  #  #  :           0 :     if (!fs::exists(path / "CURRENT")) return false;
                   #  # ]
     159                 :             : 
     160                 :           0 :     CBitcoinLevelDBLogger logger;
     161         [ #  # ]:           0 :     leveldb::Options options;
     162                 :           0 :     options.paranoid_checks = true;
     163                 :             :     // Avoid creating or rotating LevelDB's LOG files during this probe.
     164                 :           0 :     options.info_log = &logger;
     165                 :             : 
     166                 :           0 :     leveldb::DB* raw_db;
     167   [ #  #  #  #  :           0 :     HandleError(leveldb::DB::Open(options, fs::PathToString(path), &raw_db));
                   #  # ]
     168         [ #  # ]:           0 :     const std::unique_ptr<leveldb::DB> db{raw_db};
     169                 :             : 
     170                 :           0 :     leveldb::ReadOptions iteroptions;
     171                 :           0 :     iteroptions.verify_checksums = true;
     172                 :           0 :     iteroptions.fill_cache = false;
     173   [ #  #  #  # ]:           0 :     const std::unique_ptr<leveldb::Iterator> it{db->NewIterator(iteroptions)};
     174         [ #  # ]:           0 :     const leveldb::Slice prefix_slice{reinterpret_cast<const char*>(&prefix), sizeof(prefix)};
     175         [ #  # ]:           0 :     it->Seek(prefix_slice);
     176   [ #  #  #  # ]:           0 :     HandleError(it->status());
     177   [ #  #  #  #  :           0 :     return it->Valid() && it->key().starts_with(prefix_slice);
             #  #  #  # ]
     178                 :           0 : }
     179                 :             : 
     180         [ +  - ]:     3627722 : struct CDBBatch::WriteBatchImpl {
     181                 :             :     leveldb::WriteBatch batch;
     182                 :             : };
     183                 :             : 
     184                 :     1813861 : CDBBatch::CDBBatch(const CDBWrapper& _parent)
     185                 :     1813861 :     : parent{_parent},
     186                 :     1813861 :       m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()}
     187                 :             : {
     188         [ +  - ]:     1813861 :     m_key_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
     189         [ +  - ]:     1813861 :     m_value_scratch.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
     190         [ +  - ]:     1813861 :     Clear();
     191                 :     1813861 : };
     192                 :             : 
     193                 :     1813861 : CDBBatch::~CDBBatch() = default;
     194                 :             : 
     195                 :     1820541 : void CDBBatch::Clear()
     196                 :             : {
     197                 :     1820541 :     m_impl_batch->batch.Clear();
     198   [ -  +  -  + ]:     1820541 :     assert(m_key_scratch.empty());
     199   [ -  +  -  + ]:     3641082 :     assert(m_value_scratch.empty());
     200                 :     1820541 : }
     201                 :             : 
     202                 :     6943041 : void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& value)
     203                 :             : {
     204                 :     6943041 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     205         [ -  + ]:     6943041 :     dbwrapper_private::GetObfuscation(parent)(value);
     206         [ -  + ]:     6943041 :     leveldb::Slice slValue(CharCast(value.data()), value.size());
     207                 :     6943041 :     m_impl_batch->batch.Put(slKey, slValue);
     208                 :     6943041 : }
     209                 :             : 
     210                 :     3225895 : void CDBBatch::EraseImpl(std::span<const std::byte> key)
     211                 :             : {
     212                 :     3225895 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     213                 :     3225895 :     m_impl_batch->batch.Delete(slKey);
     214                 :     3225895 : }
     215                 :             : 
     216                 :      422342 : size_t CDBBatch::ApproximateSize() const
     217                 :             : {
     218                 :      422342 :     return m_impl_batch->batch.ApproximateSize();
     219                 :             : }
     220                 :             : 
     221                 :             : struct LevelDBContext {
     222                 :             :     //! custom environment this database is using (may be nullptr in case of default environment)
     223                 :             :     leveldb::Env* penv;
     224                 :             : 
     225                 :             :     //! database options used
     226                 :             :     leveldb::Options options;
     227                 :             : 
     228                 :             :     //! options used when reading from the database
     229                 :             :     leveldb::ReadOptions readoptions;
     230                 :             : 
     231                 :             :     //! options used when iterating over values of the database
     232                 :             :     leveldb::ReadOptions iteroptions;
     233                 :             : 
     234                 :             :     //! options used when writing to the database
     235                 :             :     leveldb::WriteOptions writeoptions;
     236                 :             : 
     237                 :             :     //! options used when sync writing to the database
     238                 :             :     leveldb::WriteOptions syncoptions;
     239                 :             : 
     240                 :             :     //! the database itself
     241                 :             :     leveldb::DB* pdb;
     242                 :             : };
     243                 :             : 
     244                 :       95728 : CDBWrapper::CDBWrapper(const DBParams& params)
     245   [ +  -  -  + ]:      478640 :     : m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}
     246                 :             : {
     247         [ +  - ]:       95728 :     DBContext().penv = nullptr;
     248         [ +  - ]:       95728 :     DBContext().readoptions.verify_checksums = true;
     249         [ +  - ]:       95728 :     DBContext().iteroptions.verify_checksums = true;
     250         [ +  - ]:       95728 :     DBContext().iteroptions.fill_cache = false;
     251         [ +  - ]:       95728 :     DBContext().syncoptions.sync = true;
     252   [ +  -  +  - ]:       95728 :     DBContext().options = GetOptions(params.cache_bytes, params.bloom_filter);
     253         [ +  - ]:       95728 :     DBContext().options.create_if_missing = true;
     254         [ +  - ]:       95728 :     DBContext().options.max_file_size = params.max_file_size;
     255   [ +  +  -  + ]:       95728 :     assert(!(params.testing_env && params.memory_only));
     256         [ +  + ]:       95728 :     if (params.testing_env) {
     257         [ +  - ]:       75458 :         DBContext().options.env = params.testing_env;
     258         [ +  - ]:       20270 :     } else if (params.memory_only) {
     259   [ +  -  +  -  :       20270 :         DBContext().penv = leveldb::NewMemEnv(leveldb::Env::Default());
                   +  - ]
     260   [ +  -  +  - ]:       20270 :         DBContext().options.env = DBContext().penv;
     261                 :             :     }
     262         [ +  + ]:       95728 :     if (!params.memory_only) {
     263         [ -  + ]:       75458 :         if (params.wipe_data) {
     264   [ #  #  #  # ]:           0 :             LogInfo("Wiping LevelDB in %s", fs::PathToString(params.path));
     265   [ #  #  #  #  :           0 :             leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), DBContext().options);
                   #  # ]
     266         [ #  # ]:           0 :             HandleError(result);
     267                 :           0 :         }
     268         [ -  + ]:       75458 :         if (!params.testing_env) {
     269         [ #  # ]:           0 :             TryCreateDirectories(params.path);
     270                 :             :         }
     271   [ -  +  +  - ]:      150916 :         LogInfo("Opening LevelDB in %s", fs::PathToString(params.path));
     272                 :             :     }
     273                 :             :     // PathToString() return value is safe to pass to leveldb open function,
     274                 :             :     // because on POSIX leveldb passes the byte string directly to ::open(), and
     275                 :             :     // on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
     276                 :             :     // (see env_posix.cc and env_windows.cc).
     277   [ +  -  -  +  :      191456 :     leveldb::Status status = leveldb::DB::Open(DBContext().options, fs::PathToString(params.path), &DBContext().pdb);
             +  -  +  - ]
     278         [ +  - ]:       95728 :     HandleError(status);
     279         [ +  - ]:       95728 :     LogInfo("Opened LevelDB successfully");
     280                 :             : 
     281         [ +  + ]:       95728 :     if (params.options.force_compact) {
     282   [ -  +  +  - ]:       52956 :         LogInfo("Starting database compaction of %s", fs::PathToString(params.path));
     283         [ +  - ]:       26478 :         CompactFull();
     284   [ -  +  +  - ]:       52956 :         LogInfo("Finished database compaction of %s", fs::PathToString(params.path));
     285                 :             :     }
     286                 :             : 
     287   [ +  -  +  +  :       95728 :     if (!Read(OBFUSCATION_KEY, m_obfuscation) && params.obfuscate && IsEmpty()) {
          +  +  +  -  +  
                      - ]
     288                 :             :         // Generate and write the new obfuscation key.
     289                 :        8511 :         const Obfuscation obfuscation{FastRandomContext{}.randbytes<Obfuscation::KEY_SIZE>()};
     290         [ -  + ]:        8511 :         assert(!m_obfuscation); // Make sure the key is written without obfuscation.
     291         [ +  - ]:        8511 :         Write(OBFUSCATION_KEY, obfuscation);
     292                 :        8511 :         m_obfuscation = obfuscation;
     293   [ +  -  -  +  :       17022 :         LogInfo("Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
                   +  - ]
     294                 :             :     }
     295   [ +  -  -  +  :      191456 :     LogInfo("Using obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
             +  -  -  + ]
     296                 :       95728 : }
     297                 :             : 
     298                 :       95728 : CDBWrapper::~CDBWrapper()
     299                 :             : {
     300         [ +  - ]:       95728 :     delete DBContext().pdb;
     301                 :       95728 :     DBContext().pdb = nullptr;
     302         [ +  + ]:       95728 :     delete DBContext().options.filter_policy;
     303                 :       95728 :     DBContext().options.filter_policy = nullptr;
     304         [ +  - ]:       95728 :     delete DBContext().options.info_log;
     305                 :       95728 :     DBContext().options.info_log = nullptr;
     306         [ +  - ]:       95728 :     delete DBContext().options.block_cache;
     307                 :       95728 :     DBContext().options.block_cache = nullptr;
     308         [ +  + ]:       95728 :     delete DBContext().penv;
     309                 :       95728 :     DBContext().options.env = nullptr;
     310                 :       95728 : }
     311                 :             : 
     312                 :     1813861 : void CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
     313                 :             : {
     314                 :     1813861 :     const bool log_memory = util::log::ShouldDebugLog(BCLog::LEVELDB);
     315                 :     1813861 :     double mem_before = 0;
     316         [ -  + ]:     1813861 :     if (log_memory) {
     317                 :           0 :         mem_before = DynamicMemoryUsage() / double(1_MiB);
     318                 :             :     }
     319         [ +  + ]:     1813861 :     leveldb::Status status = DBContext().pdb->Write(fSync ? DBContext().syncoptions : DBContext().writeoptions, &batch.m_impl_batch->batch);
     320         [ +  - ]:     1813861 :     HandleError(status);
     321         [ -  + ]:     1813861 :     if (log_memory) {
     322         [ #  # ]:           0 :         double mem_after{DynamicMemoryUsage() / double(1_MiB)};
     323   [ -  -  -  -  :     1813861 :         LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
             -  -  -  + ]
     324                 :             :                  m_name, mem_before, mem_after);
     325                 :             :     }
     326                 :     1813861 : }
     327                 :             : 
     328                 :        8786 : std::optional<std::string> CDBWrapper::GetProperty(const std::string& property) const
     329                 :             : {
     330   [ +  -  -  +  :        8786 :     if (std::string value; DBContext().pdb->GetProperty(property, &value)) return value;
             +  -  +  - ]
     331                 :           0 :     return std::nullopt;
     332                 :             : }
     333                 :             : 
     334                 :       54746 : void CDBWrapper::CompactFull() { DBContext().pdb->CompactRange(nullptr, nullptr); }
     335                 :             : 
     336                 :        8786 : size_t CDBWrapper::DynamicMemoryUsage() const
     337                 :             : {
     338                 :        8786 :     std::optional<size_t> parsed;
     339   [ +  -  +  -  :       17572 :     if (auto memory{GetProperty("leveldb.approximate-memory-usage")}; !memory || !(parsed = ToIntegral<size_t>(*memory))) {
             -  +  -  + ]
     340   [ #  #  #  #  :           0 :         LogDebug(BCLog::LEVELDB, "Failed to get approximate-memory-usage property\n");
                   #  # ]
     341                 :           0 :         return 0;
     342                 :           0 :     }
     343                 :        8786 :     return parsed.value();
     344                 :             : }
     345                 :             : 
     346                 :     5966939 : std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key) const
     347                 :             : {
     348         [ +  - ]:     5966939 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     349         [ +  - ]:     5966939 :     std::string strValue;
     350   [ +  -  +  -  :     5966939 :     leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
                   +  - ]
     351         [ +  + ]:     5966939 :     if (!status.ok()) {
     352         [ +  - ]:     3777666 :         if (status.IsNotFound())
     353                 :     3777666 :             return std::nullopt;
     354   [ #  #  #  # ]:           0 :         LogError("LevelDB read failure: %s", status.ToString());
     355         [ #  # ]:           0 :         HandleError(status);
     356                 :             :     }
     357                 :     2189273 :     return strValue;
     358                 :     5966939 : }
     359                 :             : 
     360                 :       29273 : bool CDBWrapper::ExistsImpl(std::span<const std::byte> key) const
     361                 :             : {
     362         [ +  - ]:       29273 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     363                 :             : 
     364         [ +  - ]:       29273 :     std::string strValue;
     365   [ +  -  +  -  :       29273 :     leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
                   +  - ]
     366         [ +  + ]:       29273 :     if (!status.ok()) {
     367         [ -  + ]:       23141 :         if (status.IsNotFound())
     368                 :             :             return false;
     369   [ #  #  #  # ]:           0 :         LogError("LevelDB read failure: %s", status.ToString());
     370         [ #  # ]:           0 :         HandleError(status);
     371                 :             :     }
     372                 :             :     return true;
     373                 :       29273 : }
     374                 :             : 
     375                 :      192892 : size_t CDBWrapper::EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const
     376                 :             : {
     377                 :      192892 :     leveldb::Slice slKey1(CharCast(key1.data()), key1.size());
     378                 :      192892 :     leveldb::Slice slKey2(CharCast(key2.data()), key2.size());
     379                 :      192892 :     uint64_t size = 0;
     380                 :      192892 :     leveldb::Range range(slKey1, slKey2);
     381                 :      192892 :     DBContext().pdb->GetApproximateSizes(&range, 1, &size);
     382                 :      192892 :     return size;
     383                 :             : }
     384                 :             : 
     385                 :       10569 : bool CDBWrapper::IsEmpty()
     386                 :             : {
     387         [ +  - ]:       10569 :     std::unique_ptr<CDBIterator> it(NewIterator());
     388         [ +  - ]:       10569 :     it->SeekToFirst();
     389         [ +  - ]:       21138 :     return !(it->Valid());
     390                 :       10569 : }
     391                 :             : 
     392                 :      286851 : struct CDBIterator::IteratorImpl {
     393                 :             :     const std::unique_ptr<leveldb::Iterator> iter;
     394                 :             : 
     395                 :      286851 :     explicit IteratorImpl(leveldb::Iterator* _iter) : iter{_iter} {}
     396                 :             : };
     397                 :             : 
     398                 :      286851 : CDBIterator::CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter) : parent(_parent),
     399         [ +  - ]:      286851 :                                                                                             m_impl_iter(std::move(_piter))
     400                 :             : {
     401         [ +  - ]:      286851 :     m_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
     402                 :      286851 : }
     403                 :             : 
     404                 :      286851 : CDBIterator* CDBWrapper::NewIterator()
     405                 :             : {
     406   [ +  -  +  -  :      286851 :     return new CDBIterator{*this, std::make_unique<CDBIterator::IteratorImpl>(DBContext().pdb->NewIterator(DBContext().iteroptions))};
          +  -  +  -  +  
                      - ]
     407                 :             : }
     408                 :             : 
     409                 :      199604 : void CDBIterator::SeekImpl(std::span<const std::byte> key)
     410                 :             : {
     411                 :      199604 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     412                 :      199604 :     m_impl_iter->iter->Seek(slKey);
     413                 :      199604 : }
     414                 :             : 
     415                 :     8934190 : std::span<const std::byte> CDBIterator::GetKeyImpl() const
     416                 :             : {
     417                 :             :     // The returned span borrows from the current iterator entry and is only
     418                 :             :     // valid until the iterator is advanced.
     419                 :     8934190 :     return MakeByteSpan(m_impl_iter->iter->key());
     420                 :             : }
     421                 :             : 
     422                 :     8813853 : std::span<const std::byte> CDBIterator::GetValueImpl() const
     423                 :             : {
     424                 :     8813853 :     return MakeByteSpan(m_impl_iter->iter->value());
     425                 :             : }
     426                 :             : 
     427                 :      286851 : CDBIterator::~CDBIterator() = default;
     428                 :     9157633 : bool CDBIterator::Valid() const { return m_impl_iter->iter->Valid(); }
     429                 :       86991 : void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
     430                 :     8868612 : void CDBIterator::Next() { m_impl_iter->iter->Next(); }
     431                 :             : 
     432                 :             : namespace dbwrapper_private {
     433                 :             : 
     434                 :    15756894 : const Obfuscation& GetObfuscation(const CDBWrapper& w)
     435                 :             : {
     436                 :    15756894 :     return w.m_obfuscation;
     437                 :             : }
     438                 :             : 
     439                 :             : } // namespace dbwrapper_private
        

Generated by: LCOV version 2.5.0-full