LCOV - code coverage report
Current view: top level - src - dbwrapper.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 85.0 % 226 192
Test Date: 2026-08-25 06:30:02 Functions: 100.0 % 33 33
Branches: 49.1 % 326 160

             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                 :     7862942 : static auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
      38                 :             : 
      39                 :          36 : bool DestroyDB(const std::string& path_str)
      40                 :             : {
      41         [ -  + ]:          36 :     return leveldb::DestroyDB(path_str, {}).ok();
      42                 :             : }
      43                 :             : 
      44                 :             : /** Handle database error by throwing dbwrapper_error exception.
      45                 :             :  */
      46                 :       47850 : static void HandleError(const leveldb::Status& status)
      47                 :             : {
      48         [ +  + ]:       47850 :     if (status.ok())
      49                 :       47840 :         return;
      50         [ +  - ]:          10 :     const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
      51         [ +  - ]:          10 :     LogError("%s", errmsg);
      52         [ +  - ]:          10 :     LogInfo("You can use -debug=leveldb to get more complete diagnostic messages");
      53         [ +  - ]:          10 :     throw dbwrapper_error(errmsg);
      54                 :          10 : }
      55                 :             : 
      56                 :           3 : 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                 :       12653 :     void Logv(const char * format, va_list ap) override {
      61         [ -  + ]:       12653 :             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                 :        3041 : 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                 :        3041 :     int default_open_files = options->max_open_files;
     130                 :             : #ifndef WIN32
     131                 :        3041 :     if (sizeof(void*) < 8) {
     132                 :             :         options->max_open_files = 64;
     133                 :             :     }
     134                 :             : #endif
     135         [ -  + ]:        3041 :     LogDebug(BCLog::LEVELDB, "LevelDB using max_open_files=%d (default=%d)\n",
     136                 :             :              options->max_open_files, default_open_files);
     137                 :        3041 : }
     138                 :             : 
     139                 :        3041 : static leveldb::Options GetOptions(size_t nCacheSize, bool bloom_filter)
     140                 :             : {
     141                 :        3041 :     leveldb::Options options;
     142                 :        3041 :     options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
     143                 :        3041 :     options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
     144         [ +  + ]:        3041 :     options.filter_policy = bloom_filter ? leveldb::NewBloomFilterPolicy(10) : nullptr;
     145                 :        3041 :     options.compression = leveldb::kNoCompression;
     146                 :        3041 :     options.info_log = new CBitcoinLevelDBLogger();
     147                 :        3041 :     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                 :        3041 :         options.paranoid_checks = true;
     151                 :             :     }
     152                 :        3041 :     SetMaxOpenFiles(&options);
     153                 :        3041 :     return options;
     154                 :             : }
     155                 :             : 
     156                 :          44 : bool CDBWrapper::HasKeyStartingWith(const fs::path& path, uint8_t prefix)
     157                 :             : {
     158   [ +  -  +  -  :         176 :     if (!fs::exists(path / "CURRENT")) return false;
                   +  + ]
     159                 :             : 
     160                 :          30 :     CBitcoinLevelDBLogger logger;
     161         [ +  - ]:          30 :     leveldb::Options options;
     162                 :          30 :     options.paranoid_checks = true;
     163                 :             :     // Avoid creating or rotating LevelDB's LOG files during this probe.
     164                 :          30 :     options.info_log = &logger;
     165                 :             : 
     166                 :          30 :     leveldb::DB* raw_db;
     167   [ -  +  +  -  :          93 :     HandleError(leveldb::DB::Open(options, fs::PathToString(path), &raw_db));
                   +  + ]
     168         [ +  - ]:          27 :     const std::unique_ptr<leveldb::DB> db{raw_db};
     169                 :             : 
     170                 :          27 :     leveldb::ReadOptions iteroptions;
     171                 :          27 :     iteroptions.verify_checksums = true;
     172                 :          27 :     iteroptions.fill_cache = false;
     173   [ +  -  +  - ]:          27 :     const std::unique_ptr<leveldb::Iterator> it{db->NewIterator(iteroptions)};
     174         [ +  - ]:          27 :     const leveldb::Slice prefix_slice{reinterpret_cast<const char*>(&prefix), sizeof(prefix)};
     175         [ +  - ]:          27 :     it->Seek(prefix_slice);
     176   [ +  -  +  - ]:          27 :     HandleError(it->status());
     177   [ +  -  +  +  :          27 :     return it->Valid() && it->key().starts_with(prefix_slice);
             +  -  +  + ]
     178                 :          27 : }
     179                 :             : 
     180         [ +  - ]:       89348 : struct CDBBatch::WriteBatchImpl {
     181                 :             :     leveldb::WriteBatch batch;
     182                 :             : };
     183                 :             : 
     184                 :       44674 : CDBBatch::CDBBatch(const CDBWrapper& _parent)
     185                 :       44674 :     : parent{_parent},
     186                 :       44674 :       m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()}
     187                 :             : {
     188         [ +  - ]:       44674 :     m_key_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
     189         [ +  - ]:       44674 :     m_value_scratch.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
     190         [ +  - ]:       44674 :     Clear();
     191                 :       44674 : };
     192                 :             : 
     193                 :       44674 : CDBBatch::~CDBBatch() = default;
     194                 :             : 
     195                 :       44676 : void CDBBatch::Clear()
     196                 :             : {
     197                 :       44676 :     m_impl_batch->batch.Clear();
     198   [ -  +  -  + ]:       44676 :     assert(m_key_scratch.empty());
     199   [ -  +  -  + ]:       89352 :     assert(m_value_scratch.empty());
     200                 :       44676 : }
     201                 :             : 
     202                 :      542049 : void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& value)
     203                 :             : {
     204                 :      542049 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     205         [ -  + ]:      542049 :     dbwrapper_private::GetObfuscation(parent)(value);
     206         [ -  + ]:      542049 :     leveldb::Slice slValue(CharCast(value.data()), value.size());
     207                 :      542049 :     m_impl_batch->batch.Put(slKey, slValue);
     208                 :      542049 : }
     209                 :             : 
     210                 :       43180 : void CDBBatch::EraseImpl(std::span<const std::byte> key)
     211                 :             : {
     212                 :       43180 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     213                 :       43180 :     m_impl_batch->batch.Delete(slKey);
     214                 :       43180 : }
     215                 :             : 
     216                 :      353678 : size_t CDBBatch::ApproximateSize() const
     217                 :             : {
     218                 :      353678 :     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                 :        3041 : CDBWrapper::CDBWrapper(const DBParams& params)
     245   [ +  -  -  + ]:       15205 :     : m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}
     246                 :             : {
     247         [ +  - ]:        3041 :     DBContext().penv = nullptr;
     248         [ +  - ]:        3041 :     DBContext().readoptions.verify_checksums = true;
     249         [ +  - ]:        3041 :     DBContext().iteroptions.verify_checksums = true;
     250         [ +  - ]:        3041 :     DBContext().iteroptions.fill_cache = false;
     251         [ +  - ]:        3041 :     DBContext().syncoptions.sync = true;
     252   [ +  -  +  - ]:        3041 :     DBContext().options = GetOptions(params.cache_bytes, params.bloom_filter);
     253         [ +  - ]:        3041 :     DBContext().options.create_if_missing = true;
     254         [ +  - ]:        3041 :     DBContext().options.max_file_size = params.max_file_size;
     255   [ -  +  -  - ]:        3041 :     assert(!(params.testing_env && params.memory_only));
     256         [ -  + ]:        3041 :     if (params.testing_env) {
     257         [ #  # ]:           0 :         DBContext().options.env = params.testing_env;
     258         [ +  + ]:        3041 :     } else if (params.memory_only) {
     259   [ +  -  +  -  :         369 :         DBContext().penv = leveldb::NewMemEnv(leveldb::Env::Default());
                   +  - ]
     260   [ +  -  +  - ]:         369 :         DBContext().options.env = DBContext().penv;
     261                 :             :     }
     262         [ +  + ]:        3041 :     if (!params.memory_only) {
     263         [ +  + ]:        2672 :         if (params.wipe_data) {
     264   [ -  +  +  - ]:         150 :             LogInfo("Wiping LevelDB in %s", fs::PathToString(params.path));
     265   [ +  -  -  +  :         150 :             leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), DBContext().options);
                   +  - ]
     266         [ +  - ]:          75 :             HandleError(result);
     267                 :          75 :         }
     268         [ +  - ]:        2672 :         if (!params.testing_env) {
     269         [ +  - ]:        2672 :             TryCreateDirectories(params.path);
     270                 :             :         }
     271   [ -  +  +  - ]:        5344 :         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   [ +  -  -  +  :        6082 :     leveldb::Status status = leveldb::DB::Open(DBContext().options, fs::PathToString(params.path), &DBContext().pdb);
             +  -  +  - ]
     278         [ +  + ]:        3041 :     HandleError(status);
     279         [ +  - ]:        3035 :     LogInfo("Opened LevelDB successfully");
     280                 :             : 
     281         [ -  + ]:        3035 :     if (params.options.force_compact) {
     282   [ #  #  #  # ]:           0 :         LogInfo("Starting database compaction of %s", fs::PathToString(params.path));
     283         [ #  # ]:           0 :         CompactFull();
     284   [ #  #  #  # ]:           0 :         LogInfo("Finished database compaction of %s", fs::PathToString(params.path));
     285                 :             :     }
     286                 :             : 
     287   [ +  +  +  +  :        3035 :     if (!Read(OBFUSCATION_KEY, m_obfuscation) && params.obfuscate && IsEmpty()) {
          +  +  +  -  +  
                      + ]
     288                 :             :         // Generate and write the new obfuscation key.
     289                 :         554 :         const Obfuscation obfuscation{FastRandomContext{}.randbytes<Obfuscation::KEY_SIZE>()};
     290         [ -  + ]:         554 :         assert(!m_obfuscation); // Make sure the key is written without obfuscation.
     291         [ +  - ]:         554 :         Write(OBFUSCATION_KEY, obfuscation);
     292                 :         554 :         m_obfuscation = obfuscation;
     293   [ +  -  -  +  :        1108 :         LogInfo("Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
                   +  - ]
     294                 :             :     }
     295   [ +  -  -  +  :        6068 :     LogInfo("Using obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
             +  -  -  + ]
     296                 :        3041 : }
     297                 :             : 
     298                 :        3034 : CDBWrapper::~CDBWrapper()
     299                 :             : {
     300         [ +  - ]:        3034 :     delete DBContext().pdb;
     301                 :        3034 :     DBContext().pdb = nullptr;
     302         [ +  + ]:        3034 :     delete DBContext().options.filter_policy;
     303                 :        3034 :     DBContext().options.filter_policy = nullptr;
     304         [ +  - ]:        3034 :     delete DBContext().options.info_log;
     305                 :        3034 :     DBContext().options.info_log = nullptr;
     306         [ +  - ]:        3034 :     delete DBContext().options.block_cache;
     307                 :        3034 :     DBContext().options.block_cache = nullptr;
     308         [ +  + ]:        3034 :     delete DBContext().penv;
     309                 :        3034 :     DBContext().options.env = nullptr;
     310                 :        3034 : }
     311                 :             : 
     312                 :       44676 : void CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
     313                 :             : {
     314                 :       44676 :     const bool log_memory = util::log::ShouldDebugLog(BCLog::LEVELDB);
     315                 :       44676 :     double mem_before = 0;
     316         [ +  + ]:       44676 :     if (log_memory) {
     317                 :           8 :         mem_before = DynamicMemoryUsage() / double(1_MiB);
     318                 :             :     }
     319         [ +  + ]:       44676 :     leveldb::Status status = DBContext().pdb->Write(fSync ? DBContext().syncoptions : DBContext().writeoptions, &batch.m_impl_batch->batch);
     320         [ +  - ]:       44676 :     HandleError(status);
     321         [ +  + ]:       44676 :     if (log_memory) {
     322         [ +  - ]:           8 :         double mem_after{DynamicMemoryUsage() / double(1_MiB)};
     323   [ +  -  +  -  :       44676 :         LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
             +  -  -  + ]
     324                 :             :                  m_name, mem_before, mem_after);
     325                 :             :     }
     326                 :       44676 : }
     327                 :             : 
     328                 :          18 : std::optional<std::string> CDBWrapper::GetProperty(const std::string& property) const
     329                 :             : {
     330   [ +  -  -  +  :          18 :     if (std::string value; DBContext().pdb->GetProperty(property, &value)) return value;
             +  -  +  - ]
     331                 :           0 :     return std::nullopt;
     332                 :             : }
     333                 :             : 
     334                 :           5 : void CDBWrapper::CompactFull() { DBContext().pdb->CompactRange(nullptr, nullptr); }
     335                 :             : 
     336                 :          16 : size_t CDBWrapper::DynamicMemoryUsage() const
     337                 :             : {
     338                 :          16 :     std::optional<size_t> parsed;
     339   [ +  -  +  -  :          32 :     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                 :          16 :     return parsed.value();
     344                 :             : }
     345                 :             : 
     346                 :     6724362 : std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key) const
     347                 :             : {
     348         [ +  - ]:     6724362 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     349         [ +  - ]:     6724362 :     std::string strValue;
     350   [ +  -  +  -  :     6724362 :     leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
                   +  - ]
     351         [ +  + ]:     6724362 :     if (!status.ok()) {
     352         [ +  + ]:     6632872 :         if (status.IsNotFound())
     353                 :     6632871 :             return std::nullopt;
     354   [ +  -  +  - ]:           1 :         LogError("LevelDB read failure: %s", status.ToString());
     355         [ -  + ]:           1 :         HandleError(status);
     356                 :             :     }
     357                 :       91490 :     return strValue;
     358                 :     6724362 : }
     359                 :             : 
     360                 :        6150 : bool CDBWrapper::ExistsImpl(std::span<const std::byte> key) const
     361                 :             : {
     362         [ +  - ]:        6150 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     363                 :             : 
     364         [ +  - ]:        6150 :     std::string strValue;
     365   [ +  -  +  -  :        6150 :     leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
                   +  - ]
     366         [ +  + ]:        6150 :     if (!status.ok()) {
     367         [ -  + ]:        5632 :         if (status.IsNotFound())
     368                 :             :             return false;
     369   [ #  #  #  # ]:           0 :         LogError("LevelDB read failure: %s", status.ToString());
     370         [ #  # ]:           0 :         HandleError(status);
     371                 :             :     }
     372                 :             :     return true;
     373                 :        6150 : }
     374                 :             : 
     375                 :         104 : size_t CDBWrapper::EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const
     376                 :             : {
     377                 :         104 :     leveldb::Slice slKey1(CharCast(key1.data()), key1.size());
     378                 :         104 :     leveldb::Slice slKey2(CharCast(key2.data()), key2.size());
     379                 :         104 :     uint64_t size = 0;
     380                 :         104 :     leveldb::Range range(slKey1, slKey2);
     381                 :         104 :     DBContext().pdb->GetApproximateSizes(&range, 1, &size);
     382                 :         104 :     return size;
     383                 :             : }
     384                 :             : 
     385                 :         558 : bool CDBWrapper::IsEmpty()
     386                 :             : {
     387         [ +  - ]:         558 :     std::unique_ptr<CDBIterator> it(NewIterator());
     388         [ +  - ]:         558 :     it->SeekToFirst();
     389         [ +  - ]:        1116 :     return !(it->Valid());
     390                 :         558 : }
     391                 :             : 
     392                 :        5496 : struct CDBIterator::IteratorImpl {
     393                 :             :     const std::unique_ptr<leveldb::Iterator> iter;
     394                 :             : 
     395                 :        5496 :     explicit IteratorImpl(leveldb::Iterator* _iter) : iter{_iter} {}
     396                 :             : };
     397                 :             : 
     398                 :        5496 : CDBIterator::CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter) : parent(_parent),
     399         [ +  - ]:        5496 :                                                                                             m_impl_iter(std::move(_piter))
     400                 :             : {
     401         [ +  - ]:        5496 :     m_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
     402                 :        5496 : }
     403                 :             : 
     404                 :        5496 : CDBIterator* CDBWrapper::NewIterator()
     405                 :             : {
     406   [ +  -  +  -  :        5496 :     return new CDBIterator{*this, std::make_unique<CDBIterator::IteratorImpl>(DBContext().pdb->NewIterator(DBContext().iteroptions))};
          +  -  +  -  +  
                      - ]
     407                 :             : }
     408                 :             : 
     409                 :        4944 : void CDBIterator::SeekImpl(std::span<const std::byte> key)
     410                 :             : {
     411                 :        4944 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     412                 :        4944 :     m_impl_iter->iter->Seek(slKey);
     413                 :        4944 : }
     414                 :             : 
     415                 :      465624 : 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                 :      465624 :     return MakeByteSpan(m_impl_iter->iter->key());
     420                 :             : }
     421                 :             : 
     422                 :      464271 : std::span<const std::byte> CDBIterator::GetValueImpl() const
     423                 :             : {
     424                 :      464271 :     return MakeByteSpan(m_impl_iter->iter->value());
     425                 :             : }
     426                 :             : 
     427                 :        5496 : CDBIterator::~CDBIterator() = default;
     428                 :      469759 : bool CDBIterator::Valid() const { return m_impl_iter->iter->Valid(); }
     429                 :         558 : void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
     430                 :      464109 : void CDBIterator::Next() { m_impl_iter->iter->Next(); }
     431                 :             : 
     432                 :             : namespace dbwrapper_private {
     433                 :             : 
     434                 :     1006334 : const Obfuscation& GetObfuscation(const CDBWrapper& w)
     435                 :             : {
     436                 :     1006334 :     return w.m_obfuscation;
     437                 :             : }
     438                 :             : 
     439                 :             : } // namespace dbwrapper_private
        

Generated by: LCOV version 2.0-1