refactor: Split dbwrapper CDBatch::Erase implementation

Keep the generic serialization in the header, while moving
leveldb-specifics 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-13 21:46:25 +02:00
parent 532ee812a4
commit b9870c920d
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
2 changed files with 14 additions and 9 deletions

View file

@ -152,6 +152,18 @@ void CDBBatch::WriteImpl(Span<const std::byte> ssKey, CDataStream& ssValue)
size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
}
void CDBBatch::EraseImpl(Span<const std::byte> ssKey)
{
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
batch.Delete(slKey);
// LevelDB serializes erases as:
// - byte: header
// - varint: key length
// - byte[]: key
// The formula below assumes the key is less than 16kB.
size_estimate += 2 + (slKey.size() > 127) + slKey.size();
}
CDBWrapper::CDBWrapper(const DBParams& params)
: m_name{fs::PathToString(params.path.stem())}, m_path{params.path}, m_is_memory{params.memory_only}
{

View file

@ -98,6 +98,7 @@ private:
size_t size_estimate{0};
void WriteImpl(Span<const std::byte> ssKey, CDataStream& ssValue);
void EraseImpl(Span<const std::byte> ssKey);
public:
/**
@ -128,15 +129,7 @@ public:
{
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
batch.Delete(slKey);
// LevelDB serializes erases as:
// - byte: header
// - varint: key length
// - byte[]: key
// The formula below assumes the key is less than 16kB.
size_estimate += 2 + (slKey.size() > 127) + slKey.size();
EraseImpl(ssKey);
ssKey.clear();
}