From b9870c920dc475ec759eaf7339ea42aecba92138 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Thu, 13 Jul 2023 21:46:25 +0200 Subject: [PATCH] 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. --- src/dbwrapper.cpp | 12 ++++++++++++ src/dbwrapper.h | 11 ++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index b909ce75b5a..b74c645b8f9 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -152,6 +152,18 @@ void CDBBatch::WriteImpl(Span ssKey, CDataStream& ssValue) size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size(); } +void CDBBatch::EraseImpl(Span 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} { diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 81c75f651ec..61f5003db74 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -98,6 +98,7 @@ private: size_t size_estimate{0}; void WriteImpl(Span ssKey, CDataStream& ssValue); + void EraseImpl(Span 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(); }