Skip to content

Commit 532ee81

Browse files
committed
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.
1 parent afc534d commit 532ee81

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/dbwrapper.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <logging.h>
88
#include <random.h>
9+
#include <span.h>
10+
#include <streams.h>
911
#include <tinyformat.h>
1012
#include <util/fs.h>
1113
#include <util/fs_helpers.h>
@@ -23,7 +25,9 @@
2325
#include <leveldb/helpers/memenv/memenv.h>
2426
#include <leveldb/iterator.h>
2527
#include <leveldb/options.h>
28+
#include <leveldb/slice.h>
2629
#include <leveldb/status.h>
30+
#include <leveldb/write_batch.h>
2731
#include <memory>
2832
#include <optional>
2933

@@ -132,6 +136,22 @@ static leveldb::Options GetOptions(size_t nCacheSize)
132136
return options;
133137
}
134138

139+
void CDBBatch::WriteImpl(Span<const std::byte> ssKey, CDataStream& ssValue)
140+
{
141+
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
142+
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
143+
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
144+
batch.Put(slKey, slValue);
145+
// LevelDB serializes writes as:
146+
// - byte: header
147+
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
148+
// - byte[]: key
149+
// - varint: value length
150+
// - byte[]: value
151+
// The formula below assumes the key and value are both less than 16k.
152+
size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
153+
}
154+
135155
CDBWrapper::CDBWrapper(const DBParams& params)
136156
: m_name{fs::PathToString(params.path.stem())}, m_path{params.path}, m_is_memory{params.memory_only}
137157
{

src/dbwrapper.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class CDBBatch
9797

9898
size_t size_estimate{0};
9999

100+
void WriteImpl(Span<const std::byte> ssKey, CDataStream& ssValue);
101+
100102
public:
101103
/**
102104
* @param[in] _parent CDBWrapper that this batch is to be submitted to
@@ -113,23 +115,10 @@ class CDBBatch
113115
void Write(const K& key, const V& value)
114116
{
115117
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
116-
ssKey << key;
117-
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
118-
119118
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
119+
ssKey << key;
120120
ssValue << value;
121-
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
122-
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
123-
124-
batch.Put(slKey, slValue);
125-
// LevelDB serializes writes as:
126-
// - byte: header
127-
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
128-
// - byte[]: key
129-
// - varint: value length
130-
// - byte[]: value
131-
// The formula below assumes the key and value are both less than 16k.
132-
size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
121+
WriteImpl(ssKey, ssValue);
133122
ssKey.clear();
134123
ssValue.clear();
135124
}

0 commit comments

Comments
 (0)