fuzz coverage

Coverage Report

Created: 2025-06-01 19:34

/Users/eugenesiegel/btc/bitcoin/src/dbwrapper.cpp
Line
Count
Source (jump to first uncovered line)
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 <logging.h>
8
#include <random.h>
9
#include <serialize.h>
10
#include <span.h>
11
#include <streams.h>
12
#include <util/fs.h>
13
#include <util/fs_helpers.h>
14
#include <util/strencodings.h>
15
16
#include <algorithm>
17
#include <cassert>
18
#include <cstdarg>
19
#include <cstdint>
20
#include <cstdio>
21
#include <leveldb/cache.h>
22
#include <leveldb/db.h>
23
#include <leveldb/env.h>
24
#include <leveldb/filter_policy.h>
25
#include <leveldb/helpers/memenv/memenv.h>
26
#include <leveldb/iterator.h>
27
#include <leveldb/options.h>
28
#include <leveldb/slice.h>
29
#include <leveldb/status.h>
30
#include <leveldb/write_batch.h>
31
#include <memory>
32
#include <optional>
33
#include <utility>
34
35
41.1M
static auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
36
37
bool DestroyDB(const std::string& path_str)
38
0
{
39
0
    return leveldb::DestroyDB(path_str, {}).ok();
40
0
}
41
42
/** Handle database error by throwing dbwrapper_error exception.
43
 */
44
static void HandleError(const leveldb::Status& status)
45
150k
{
46
150k
    if (status.ok())
47
150k
        return;
48
0
    const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
49
0
    LogPrintf("%s\n", errmsg);
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
50
0
    LogPrintf("You can use -debug=leveldb to get more complete diagnostic messages\n");
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
51
0
    throw dbwrapper_error(errmsg);
52
150k
}
53
54
class CBitcoinLevelDBLogger : public leveldb::Logger {
55
public:
56
    // This code is adapted from posix_logger.h, which is why it is using vsprintf.
57
    // Please do not do this in normal code
58
99.9k
    void Logv(const char * format, va_list ap) override {
59
99.9k
            if (!LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug)) {
60
99.9k
                return;
61
99.9k
            }
62
0
            char buffer[500];
63
0
            for (int iter = 0; iter < 2; iter++) {
64
0
                char* base;
65
0
                int bufsize;
66
0
                if (iter == 0) {
67
0
                    bufsize = sizeof(buffer);
68
0
                    base = buffer;
69
0
                }
70
0
                else {
71
0
                    bufsize = 30000;
72
0
                    base = new char[bufsize];
73
0
                }
74
0
                char* p = base;
75
0
                char* limit = base + bufsize;
76
77
                // Print the message
78
0
                if (p < limit) {
79
0
                    va_list backup_ap;
80
0
                    va_copy(backup_ap, ap);
81
                    // Do not use vsnprintf elsewhere in bitcoin source code, see above.
82
0
                    p += vsnprintf(p, limit - p, format, backup_ap);
83
0
                    va_end(backup_ap);
84
0
                }
85
86
                // Truncate to available space if necessary
87
0
                if (p >= limit) {
88
0
                    if (iter == 0) {
89
0
                        continue;       // Try again with larger buffer
90
0
                    }
91
0
                    else {
92
0
                        p = limit - 1;
93
0
                    }
94
0
                }
95
96
                // Add newline if necessary
97
0
                if (p == base || p[-1] != '\n') {
98
0
                    *p++ = '\n';
99
0
                }
100
101
0
                assert(p <= limit);
102
0
                base[std::min(bufsize - 1, (int)(p - base))] = '\0';
103
0
                LogDebug(BCLog::LEVELDB, "%s\n", util::RemoveSuffixView(base, "\n"));
Line
Count
Source
280
0
#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
273
0
    do {                                                  \
274
0
        if (LogAcceptCategory((category), (level))) {     \
275
0
            LogPrintLevel_(category, level, __VA_ARGS__); \
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
276
0
        }                                                 \
277
0
    } while (0)
104
0
                if (base != buffer) {
105
0
                    delete[] base;
106
0
                }
107
0
                break;
108
0
            }
109
0
    }
110
};
111
112
99.9k
static void SetMaxOpenFiles(leveldb::Options *options) {
113
    // On most platforms the default setting of max_open_files (which is 1000)
114
    // is optimal. On Windows using a large file count is OK because the handles
115
    // do not interfere with select() loops. On 64-bit Unix hosts this value is
116
    // also OK, because up to that amount LevelDB will use an mmap
117
    // implementation that does not use extra file descriptors (the fds are
118
    // closed after being mmap'ed).
119
    //
120
    // Increasing the value beyond the default is dangerous because LevelDB will
121
    // fall back to a non-mmap implementation when the file count is too large.
122
    // On 32-bit Unix host we should decrease the value because the handles use
123
    // up real fds, and we want to avoid fd exhaustion issues.
124
    //
125
    // See PR #12495 for further discussion.
126
127
99.9k
    int default_open_files = options->max_open_files;
128
99.9k
#ifndef WIN32
129
99.9k
    if (sizeof(void*) < 8) {
130
0
        options->max_open_files = 64;
131
0
    }
132
99.9k
#endif
133
99.9k
    LogDebug(BCLog::LEVELDB, "LevelDB using max_open_files=%d (default=%d)\n",
Line
Count
Source
280
99.9k
#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
273
99.9k
    do {                                                  \
274
99.9k
        if (LogAcceptCategory((category), (level))) {     \
275
0
            LogPrintLevel_(category, level, __VA_ARGS__); \
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
276
0
        }                                                 \
277
99.9k
    } while (0)
134
99.9k
             options->max_open_files, default_open_files);
135
99.9k
}
136
137
static leveldb::Options GetOptions(size_t nCacheSize)
138
99.9k
{
139
99.9k
    leveldb::Options options;
140
99.9k
    options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
141
99.9k
    options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
142
99.9k
    options.filter_policy = leveldb::NewBloomFilterPolicy(10);
143
99.9k
    options.compression = leveldb::kNoCompression;
144
99.9k
    options.info_log = new CBitcoinLevelDBLogger();
145
99.9k
    if (leveldb::kMajorVersion > 1 || 
(0
leveldb::kMajorVersion == 10
&&
leveldb::kMinorVersion >= 160
)) {
146
        // LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
147
        // on corruption in later versions.
148
99.9k
        options.paranoid_checks = true;
149
99.9k
    }
150
99.9k
    options.max_file_size = std::max(options.max_file_size, DBWRAPPER_MAX_FILE_SIZE);
151
99.9k
    SetMaxOpenFiles(&options);
152
99.9k
    return options;
153
99.9k
}
154
155
struct CDBBatch::WriteBatchImpl {
156
    leveldb::WriteBatch batch;
157
};
158
159
CDBBatch::CDBBatch(const CDBWrapper& _parent)
160
50.9k
    : parent{_parent},
161
50.9k
      m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()}
162
50.9k
{
163
50.9k
    Clear();
164
50.9k
};
165
166
50.9k
CDBBatch::~CDBBatch() = default;
167
168
void CDBBatch::Clear()
169
50.9k
{
170
50.9k
    m_impl_batch->batch.Clear();
171
50.9k
}
172
173
void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& ssValue)
174
261k
{
175
261k
    leveldb::Slice slKey(CharCast(key.data()), key.size());
176
261k
    ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
177
261k
    leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
178
261k
    m_impl_batch->batch.Put(slKey, slValue);
179
261k
}
180
181
void CDBBatch::EraseImpl(std::span<const std::byte> key)
182
1.03k
{
183
1.03k
    leveldb::Slice slKey(CharCast(key.data()), key.size());
184
1.03k
    m_impl_batch->batch.Delete(slKey);
185
1.03k
}
186
187
size_t CDBBatch::ApproximateSize() const
188
103k
{
189
103k
    return m_impl_batch->batch.ApproximateSize();
190
103k
}
191
192
struct LevelDBContext {
193
    //! custom environment this database is using (may be nullptr in case of default environment)
194
    leveldb::Env* penv;
195
196
    //! database options used
197
    leveldb::Options options;
198
199
    //! options used when reading from the database
200
    leveldb::ReadOptions readoptions;
201
202
    //! options used when iterating over values of the database
203
    leveldb::ReadOptions iteroptions;
204
205
    //! options used when writing to the database
206
    leveldb::WriteOptions writeoptions;
207
208
    //! options used when sync writing to the database
209
    leveldb::WriteOptions syncoptions;
210
211
    //! the database itself
212
    leveldb::DB* pdb;
213
};
214
215
CDBWrapper::CDBWrapper(const DBParams& params)
216
99.9k
    : m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}, m_path{params.path}, m_is_memory{params.memory_only}
217
99.9k
{
218
99.9k
    DBContext().penv = nullptr;
219
99.9k
    DBContext().readoptions.verify_checksums = true;
220
99.9k
    DBContext().iteroptions.verify_checksums = true;
221
99.9k
    DBContext().iteroptions.fill_cache = false;
222
99.9k
    DBContext().syncoptions.sync = true;
223
99.9k
    DBContext().options = GetOptions(params.cache_bytes);
224
99.9k
    DBContext().options.create_if_missing = true;
225
99.9k
    if (params.memory_only) {
226
99.9k
        DBContext().penv = leveldb::NewMemEnv(leveldb::Env::Default());
227
99.9k
        DBContext().options.env = DBContext().penv;
228
99.9k
    } else {
229
0
        if (params.wipe_data) {
230
0
            LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(params.path));
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
231
0
            leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), DBContext().options);
232
0
            HandleError(result);
233
0
        }
234
0
        TryCreateDirectories(params.path);
235
0
        LogPrintf("Opening LevelDB in %s\n", fs::PathToString(params.path));
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
236
0
    }
237
    // PathToString() return value is safe to pass to leveldb open function,
238
    // because on POSIX leveldb passes the byte string directly to ::open(), and
239
    // on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
240
    // (see env_posix.cc and env_windows.cc).
241
99.9k
    leveldb::Status status = leveldb::DB::Open(DBContext().options, fs::PathToString(params.path), &DBContext().pdb);
242
99.9k
    HandleError(status);
243
99.9k
    LogPrintf("Opened LevelDB successfully\n");
Line
Count
Source
266
99.9k
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
99.9k
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
99.9k
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
244
245
99.9k
    if (params.options.force_compact) {
246
0
        LogPrintf("Starting database compaction of %s\n", fs::PathToString(params.path));
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
247
0
        DBContext().pdb->CompactRange(nullptr, nullptr);
248
0
        LogPrintf("Finished database compaction of %s\n", fs::PathToString(params.path));
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
249
0
    }
250
251
    // The base-case obfuscation key, which is a noop.
252
99.9k
    obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000');
253
254
99.9k
    bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
255
256
99.9k
    if (!key_exists && params.obfuscate && 
IsEmpty()49.9k
) {
257
        // Initialize non-degenerate obfuscation if it won't upset
258
        // existing, non-obfuscated data.
259
49.9k
        std::vector<unsigned char> new_key = CreateObfuscateKey();
260
261
        // Write `new_key` so we don't obfuscate the key with itself
262
49.9k
        Write(OBFUSCATE_KEY_KEY, new_key);
263
49.9k
        obfuscate_key = new_key;
264
265
49.9k
        LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
Line
Count
Source
266
49.9k
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
49.9k
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
49.9k
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
266
49.9k
    }
267
268
99.9k
    LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
Line
Count
Source
266
99.9k
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
99.9k
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
99.9k
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
269
99.9k
}
270
271
CDBWrapper::~CDBWrapper()
272
99.9k
{
273
99.9k
    delete DBContext().pdb;
274
99.9k
    DBContext().pdb = nullptr;
275
99.9k
    delete DBContext().options.filter_policy;
276
99.9k
    DBContext().options.filter_policy = nullptr;
277
99.9k
    delete DBContext().options.info_log;
278
99.9k
    DBContext().options.info_log = nullptr;
279
99.9k
    delete DBContext().options.block_cache;
280
99.9k
    DBContext().options.block_cache = nullptr;
281
99.9k
    delete DBContext().penv;
282
99.9k
    DBContext().options.env = nullptr;
283
99.9k
}
284
285
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
286
50.9k
{
287
50.9k
    const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug);
288
50.9k
    double mem_before = 0;
289
50.9k
    if (log_memory) {
290
0
        mem_before = DynamicMemoryUsage() / 1024.0 / 1024;
291
0
    }
292
50.9k
    leveldb::Status status = DBContext().pdb->Write(fSync ? 
DBContext().syncoptions517
:
DBContext().writeoptions50.4k
, &batch.m_impl_batch->batch);
293
50.9k
    HandleError(status);
294
50.9k
    if (log_memory) {
295
0
        double mem_after = DynamicMemoryUsage() / 1024.0 / 1024;
296
0
        LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
Line
Count
Source
280
0
#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
273
0
    do {                                                  \
274
0
        if (LogAcceptCategory((category), (level))) {     \
275
0
            LogPrintLevel_(category, level, __VA_ARGS__); \
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
276
0
        }                                                 \
277
0
    } while (0)
297
0
                 m_name, mem_before, mem_after);
298
0
    }
299
50.9k
    return true;
300
50.9k
}
301
302
size_t CDBWrapper::DynamicMemoryUsage() const
303
0
{
304
0
    std::string memory;
305
0
    std::optional<size_t> parsed;
306
0
    if (!DBContext().pdb->GetProperty("leveldb.approximate-memory-usage", &memory) || !(parsed = ToIntegral<size_t>(memory))) {
307
0
        LogDebug(BCLog::LEVELDB, "Failed to get approximate-memory-usage property\n");
Line
Count
Source
280
0
#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
273
0
    do {                                                  \
274
0
        if (LogAcceptCategory((category), (level))) {     \
275
0
            LogPrintLevel_(category, level, __VA_ARGS__); \
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
276
0
        }                                                 \
277
0
    } while (0)
308
0
        return 0;
309
0
    }
310
0
    return parsed.value();
311
0
}
312
313
// Prefixed with null character to avoid collisions with other keys
314
//
315
// We must use a string constructor which specifies length so that we copy
316
// past the null-terminator.
317
const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
318
319
const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
320
321
/**
322
 * Returns a string (consisting of 8 random bytes) suitable for use as an
323
 * obfuscating XOR key.
324
 */
325
std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
326
49.9k
{
327
49.9k
    std::vector<uint8_t> ret(OBFUSCATE_KEY_NUM_BYTES);
328
49.9k
    GetRandBytes(ret);
329
49.9k
    return ret;
330
49.9k
}
331
332
std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key) const
333
40.4M
{
334
40.4M
    leveldb::Slice slKey(CharCast(key.data()), key.size());
335
40.4M
    std::string strValue;
336
40.4M
    leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
337
40.4M
    if (!status.ok()) {
338
40.4M
        if (status.IsNotFound())
339
40.4M
            return std::nullopt;
340
0
        LogPrintf("LevelDB read failure: %s\n", status.ToString());
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
341
0
        HandleError(status);
342
0
    }
343
0
    return strValue;
344
40.4M
}
345
346
bool CDBWrapper::ExistsImpl(std::span<const std::byte> key) const
347
49.9k
{
348
49.9k
    leveldb::Slice slKey(CharCast(key.data()), key.size());
349
350
49.9k
    std::string strValue;
351
49.9k
    leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
352
49.9k
    if (!status.ok()) {
353
49.9k
        if (status.IsNotFound())
354
49.9k
            return false;
355
0
        LogPrintf("LevelDB read failure: %s\n", status.ToString());
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
356
0
        HandleError(status);
357
0
    }
358
0
    return true;
359
49.9k
}
360
361
size_t CDBWrapper::EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const
362
0
{
363
0
    leveldb::Slice slKey1(CharCast(key1.data()), key1.size());
364
0
    leveldb::Slice slKey2(CharCast(key2.data()), key2.size());
365
0
    uint64_t size = 0;
366
0
    leveldb::Range range(slKey1, slKey2);
367
0
    DBContext().pdb->GetApproximateSizes(&range, 1, &size);
368
0
    return size;
369
0
}
370
371
bool CDBWrapper::IsEmpty()
372
49.9k
{
373
49.9k
    std::unique_ptr<CDBIterator> it(NewIterator());
374
49.9k
    it->SeekToFirst();
375
49.9k
    return !(it->Valid());
376
49.9k
}
377
378
struct CDBIterator::IteratorImpl {
379
    const std::unique_ptr<leveldb::Iterator> iter;
380
381
149k
    explicit IteratorImpl(leveldb::Iterator* _iter) : iter{_iter} {}
382
};
383
384
149k
CDBIterator::CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter) : parent(_parent),
385
149k
                                                                                            m_impl_iter(std::move(_piter)) {}
386
387
CDBIterator* CDBWrapper::NewIterator()
388
149k
{
389
149k
    return new CDBIterator{*this, std::make_unique<CDBIterator::IteratorImpl>(DBContext().pdb->NewIterator(DBContext().iteroptions))};
390
149k
}
391
392
void CDBIterator::SeekImpl(std::span<const std::byte> key)
393
99.9k
{
394
99.9k
    leveldb::Slice slKey(CharCast(key.data()), key.size());
395
99.9k
    m_impl_iter->iter->Seek(slKey);
396
99.9k
}
397
398
std::span<const std::byte> CDBIterator::GetKeyImpl() const
399
0
{
400
0
    return MakeByteSpan(m_impl_iter->iter->key());
401
0
}
402
403
std::span<const std::byte> CDBIterator::GetValueImpl() const
404
0
{
405
0
    return MakeByteSpan(m_impl_iter->iter->value());
406
0
}
407
408
149k
CDBIterator::~CDBIterator() = default;
409
149k
bool CDBIterator::Valid() const { return m_impl_iter->iter->Valid(); }
410
49.9k
void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
411
0
void CDBIterator::Next() { m_impl_iter->iter->Next(); }
412
413
namespace dbwrapper_private {
414
415
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
416
261k
{
417
261k
    return w.obfuscate_key;
418
261k
}
419
420
} // namespace dbwrapper_private