Skip to content

Commit c534a61

Browse files
committed
refactor: Split dbwrapper CDBWrapper::EstimateSize implementation
Keep the generic serialization in the header, while moving leveldb-specifics to the implementation file. Since CharCast is no longer needed in the header, move it to the implementation file. The context of this commit is an effort to decouple the dbwrapper header file from leveldb includes. To this end, the includes are moved to the dbwrapper implementation file. This is done as part of the kernel project to reduce the number of required includes for users of the kernel.
1 parent 5864488 commit c534a61

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/dbwrapper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <optional>
3434
#include <utility>
3535

36+
static auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
37+
3638
bool DestroyDB(const std::string& path_str)
3739
{
3840
return leveldb::DestroyDB(path_str, {}).ok();
@@ -340,6 +342,16 @@ bool CDBWrapper::ExistsImpl(Span<const std::byte> ssKey) const
340342
return true;
341343
}
342344

345+
size_t CDBWrapper::EstimateSizeImpl(Span<const std::byte> ssKey1, Span<const std::byte> ssKey2) const
346+
{
347+
leveldb::Slice slKey1(CharCast(ssKey1.data()), ssKey1.size());
348+
leveldb::Slice slKey2(CharCast(ssKey2.data()), ssKey2.size());
349+
uint64_t size = 0;
350+
leveldb::Range range(slKey1, slKey2);
351+
pdb->GetApproximateSizes(&range, 1, &size);
352+
return size;
353+
}
354+
343355
bool CDBWrapper::IsEmpty()
344356
{
345357
std::unique_ptr<CDBIterator> it(NewIterator());

src/dbwrapper.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@
1212
#include <util/fs.h>
1313

1414
#include <cstddef>
15-
#include <cstdint>
1615
#include <exception>
17-
#include <leveldb/db.h>
1816
#include <leveldb/options.h>
19-
#include <leveldb/slice.h>
2017
#include <memory>
2118
#include <optional>
2219
#include <stdexcept>
2320
#include <string>
2421
#include <vector>
2522
namespace leveldb {
23+
class DB;
2624
class Env;
2725
}
2826

@@ -52,8 +50,6 @@ struct DBParams {
5250
DBOptions options{};
5351
};
5452

55-
inline auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
56-
5753
class dbwrapper_error : public std::runtime_error
5854
{
5955
public:
@@ -231,6 +227,7 @@ class CDBWrapper
231227

232228
std::optional<std::string> ReadImpl(Span<const std::byte> ssKey) const;
233229
bool ExistsImpl(Span<const std::byte> ssKey) const;
230+
size_t EstimateSizeImpl(Span<const std::byte> ssKey1, Span<const std::byte> ssKey2) const;
234231

235232
public:
236233
CDBWrapper(const DBParams& params);
@@ -312,12 +309,7 @@ class CDBWrapper
312309
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
313310
ssKey1 << key_begin;
314311
ssKey2 << key_end;
315-
leveldb::Slice slKey1(CharCast(ssKey1.data()), ssKey1.size());
316-
leveldb::Slice slKey2(CharCast(ssKey2.data()), ssKey2.size());
317-
uint64_t size = 0;
318-
leveldb::Range range(slKey1, slKey2);
319-
pdb->GetApproximateSizes(&range, 1, &size);
320-
return size;
312+
return EstimateSizeImpl(ssKey1, ssKey2);
321313
}
322314
};
323315

0 commit comments

Comments
 (0)