From c534a615e93452a5f509aaf5f68c600391a98d6a Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Fri, 14 Jul 2023 12:04:41 +0200 Subject: 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. --- src/dbwrapper.cpp | 12 ++++++++++++ src/dbwrapper.h | 14 +++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index fe0a2e66f2..58fd47ce1c 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -33,6 +33,8 @@ #include #include +static auto CharCast(const std::byte* data) { return reinterpret_cast(data); } + bool DestroyDB(const std::string& path_str) { return leveldb::DestroyDB(path_str, {}).ok(); @@ -340,6 +342,16 @@ bool CDBWrapper::ExistsImpl(Span ssKey) const return true; } +size_t CDBWrapper::EstimateSizeImpl(Span ssKey1, Span ssKey2) const +{ + leveldb::Slice slKey1(CharCast(ssKey1.data()), ssKey1.size()); + leveldb::Slice slKey2(CharCast(ssKey2.data()), ssKey2.size()); + uint64_t size = 0; + leveldb::Range range(slKey1, slKey2); + pdb->GetApproximateSizes(&range, 1, &size); + return size; +} + bool CDBWrapper::IsEmpty() { std::unique_ptr it(NewIterator()); diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 9756207aec..fa7b6397cb 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -12,17 +12,15 @@ #include #include -#include #include -#include #include -#include #include #include #include #include #include namespace leveldb { +class DB; class Env; } @@ -52,8 +50,6 @@ struct DBParams { DBOptions options{}; }; -inline auto CharCast(const std::byte* data) { return reinterpret_cast(data); } - class dbwrapper_error : public std::runtime_error { public: @@ -231,6 +227,7 @@ private: std::optional ReadImpl(Span ssKey) const; bool ExistsImpl(Span ssKey) const; + size_t EstimateSizeImpl(Span ssKey1, Span ssKey2) const; public: CDBWrapper(const DBParams& params); @@ -312,12 +309,7 @@ public: ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey1 << key_begin; ssKey2 << key_end; - leveldb::Slice slKey1(CharCast(ssKey1.data()), ssKey1.size()); - leveldb::Slice slKey2(CharCast(ssKey2.data()), ssKey2.size()); - uint64_t size = 0; - leveldb::Range range(slKey1, slKey2); - pdb->GetApproximateSizes(&range, 1, &size); - return size; + return EstimateSizeImpl(ssKey1, ssKey2); } }; -- cgit v1.2.3