From 532ee812a499e13b123af6b8415d8de1f3804f0f Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Thu, 13 Jul 2023 21:38:46 +0200 Subject: [PATCH] refactor: Split dbwrapper CDBBatch::Write 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. --- src/dbwrapper.cpp | 20 ++++++++++++++++++++ src/dbwrapper.h | 19 ++++--------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 05d24f7507..b909ce75b5 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include #include #include #include @@ -23,7 +25,9 @@ #include #include #include +#include #include +#include #include #include @@ -132,6 +136,22 @@ static leveldb::Options GetOptions(size_t nCacheSize) return options; } +void CDBBatch::WriteImpl(Span ssKey, CDataStream& ssValue) +{ + leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); + ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); + leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size()); + batch.Put(slKey, slValue); + // LevelDB serializes writes as: + // - byte: header + // - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...) + // - byte[]: key + // - varint: value length + // - byte[]: value + // The formula below assumes the key and value are both less than 16k. + size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size(); +} + CDBWrapper::CDBWrapper(const DBParams& params) : m_name{fs::PathToString(params.path.stem())}, m_path{params.path}, m_is_memory{params.memory_only} { diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 478b73d56f..81c75f651e 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -97,6 +97,8 @@ private: size_t size_estimate{0}; + void WriteImpl(Span ssKey, CDataStream& ssValue); + public: /** * @param[in] _parent CDBWrapper that this batch is to be submitted to @@ -113,23 +115,10 @@ public: void Write(const K& key, const V& value) { ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); - ssKey << key; - leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); - ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE); + ssKey << key; ssValue << value; - ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); - leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size()); - - batch.Put(slKey, slValue); - // LevelDB serializes writes as: - // - byte: header - // - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...) - // - byte[]: key - // - varint: value length - // - byte[]: value - // The formula below assumes the key and value are both less than 16k. - size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size(); + WriteImpl(ssKey, ssValue); ssKey.clear(); ssValue.clear(); }