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.
This commit is contained in:
TheCharlatan 2023-07-14 12:04:41 +02:00
parent 586448888b
commit c534a615e9
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
2 changed files with 15 additions and 11 deletions

View file

@ -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());

View file

@ -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);
}
};