diff options
author | TheCharlatan <seb.kung@gmail.com> | 2023-07-14 12:04:41 +0200 |
---|---|---|
committer | TheCharlatan <seb.kung@gmail.com> | 2023-08-05 10:43:01 +0200 |
commit | c534a615e93452a5f509aaf5f68c600391a98d6a (patch) | |
tree | 5ba2aa8df4dd8e0bfa087bfcc458e134ea6dbe1a /src | |
parent | 586448888b72f7c87db4dcd30fc4e4044afae13b (diff) | |
download | bitcoin-c534a615e93452a5f509aaf5f68c600391a98d6a.tar.xz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/dbwrapper.cpp | 12 | ||||
-rw-r--r-- | 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 <optional> #include <utility> +static auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); } + bool DestroyDB(const std::string& path_str) { return leveldb::DestroyDB(path_str, {}).ok(); @@ -340,6 +342,16 @@ bool CDBWrapper::ExistsImpl(Span<const std::byte> ssKey) const return true; } +size_t CDBWrapper::EstimateSizeImpl(Span<const std::byte> ssKey1, Span<const std::byte> 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<CDBIterator> 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 <util/fs.h> #include <cstddef> -#include <cstdint> #include <exception> -#include <leveldb/db.h> #include <leveldb/options.h> -#include <leveldb/slice.h> #include <memory> #include <optional> #include <stdexcept> #include <string> #include <vector> namespace leveldb { +class DB; class Env; } @@ -52,8 +50,6 @@ struct DBParams { DBOptions options{}; }; -inline auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); } - class dbwrapper_error : public std::runtime_error { public: @@ -231,6 +227,7 @@ private: std::optional<std::string> ReadImpl(Span<const std::byte> ssKey) const; bool ExistsImpl(Span<const std::byte> ssKey) const; + size_t EstimateSizeImpl(Span<const std::byte> ssKey1, Span<const std::byte> 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); } }; |