refactor: Pimpl leveldb::batch for CDBBatch

Hide the leveldb::WriteBatch member variable with a pimpl in order not
to expose it directly in the header.

Also move CDBBatch::Clear to the dbwrapper implementation to use the new
impl_batch.

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:55:07 +02:00
parent b9870c920d
commit ea8135de7e
2 changed files with 28 additions and 12 deletions

View File

@@ -4,8 +4,10 @@
#include <dbwrapper.h> #include <dbwrapper.h>
#include <clientversion.h>
#include <logging.h> #include <logging.h>
#include <random.h> #include <random.h>
#include <serialize.h>
#include <span.h> #include <span.h>
#include <streams.h> #include <streams.h>
#include <tinyformat.h> #include <tinyformat.h>
@@ -136,12 +138,28 @@ static leveldb::Options GetOptions(size_t nCacheSize)
return options; return options;
} }
struct CDBBatch::WriteBatchImpl {
leveldb::WriteBatch batch;
};
CDBBatch::CDBBatch(const CDBWrapper& _parent) : parent(_parent),
m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()},
ssValue(SER_DISK, CLIENT_VERSION){};
CDBBatch::~CDBBatch() = default;
void CDBBatch::Clear()
{
m_impl_batch->batch.Clear();
size_estimate = 0;
}
void CDBBatch::WriteImpl(Span<const std::byte> ssKey, CDataStream& ssValue) void CDBBatch::WriteImpl(Span<const std::byte> ssKey, CDataStream& ssValue)
{ {
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size()); leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
batch.Put(slKey, slValue); m_impl_batch->batch.Put(slKey, slValue);
// LevelDB serializes writes as: // LevelDB serializes writes as:
// - byte: header // - byte: header
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...) // - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
@@ -155,7 +173,7 @@ void CDBBatch::WriteImpl(Span<const std::byte> ssKey, CDataStream& ssValue)
void CDBBatch::EraseImpl(Span<const std::byte> ssKey) void CDBBatch::EraseImpl(Span<const std::byte> ssKey)
{ {
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
batch.Delete(slKey); m_impl_batch->batch.Delete(slKey);
// LevelDB serializes erases as: // LevelDB serializes erases as:
// - byte: header // - byte: header
// - varint: key length // - varint: key length
@@ -241,7 +259,7 @@ bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
if (log_memory) { if (log_memory) {
mem_before = DynamicMemoryUsage() / 1024.0 / 1024; mem_before = DynamicMemoryUsage() / 1024.0 / 1024;
} }
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch); leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.m_impl_batch->batch);
dbwrapper_private::HandleError(status); dbwrapper_private::HandleError(status);
if (log_memory) { if (log_memory) {
double mem_after = DynamicMemoryUsage() / 1024.0 / 1024; double mem_after = DynamicMemoryUsage() / 1024.0 / 1024;

View File

@@ -20,7 +20,7 @@
#include <leveldb/options.h> #include <leveldb/options.h>
#include <leveldb/slice.h> #include <leveldb/slice.h>
#include <leveldb/status.h> #include <leveldb/status.h>
#include <leveldb/write_batch.h> #include <memory>
#include <optional> #include <optional>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
@@ -90,7 +90,9 @@ class CDBBatch
private: private:
const CDBWrapper &parent; const CDBWrapper &parent;
leveldb::WriteBatch batch;
struct WriteBatchImpl;
const std::unique_ptr<WriteBatchImpl> m_impl_batch;
DataStream ssKey{}; DataStream ssKey{};
CDataStream ssValue; CDataStream ssValue;
@@ -104,13 +106,9 @@ public:
/** /**
* @param[in] _parent CDBWrapper that this batch is to be submitted to * @param[in] _parent CDBWrapper that this batch is to be submitted to
*/ */
explicit CDBBatch(const CDBWrapper& _parent) : parent(_parent), ssValue(SER_DISK, CLIENT_VERSION){}; explicit CDBBatch(const CDBWrapper& _parent);
~CDBBatch();
void Clear() void Clear();
{
batch.Clear();
size_estimate = 0;
}
template <typename K, typename V> template <typename K, typename V>
void Write(const K& key, const V& value) void Write(const K& key, const V& value)