Skip to content

Commit ea8135d

Browse files
committed
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.
1 parent b9870c9 commit ea8135d

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/dbwrapper.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
#include <dbwrapper.h>
66

7+
#include <clientversion.h>
78
#include <logging.h>
89
#include <random.h>
10+
#include <serialize.h>
911
#include <span.h>
1012
#include <streams.h>
1113
#include <tinyformat.h>
@@ -136,12 +138,28 @@ static leveldb::Options GetOptions(size_t nCacheSize)
136138
return options;
137139
}
138140

141+
struct CDBBatch::WriteBatchImpl {
142+
leveldb::WriteBatch batch;
143+
};
144+
145+
CDBBatch::CDBBatch(const CDBWrapper& _parent) : parent(_parent),
146+
m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()},
147+
ssValue(SER_DISK, CLIENT_VERSION){};
148+
149+
CDBBatch::~CDBBatch() = default;
150+
151+
void CDBBatch::Clear()
152+
{
153+
m_impl_batch->batch.Clear();
154+
size_estimate = 0;
155+
}
156+
139157
void CDBBatch::WriteImpl(Span<const std::byte> ssKey, CDataStream& ssValue)
140158
{
141159
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
142160
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
143161
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
144-
batch.Put(slKey, slValue);
162+
m_impl_batch->batch.Put(slKey, slValue);
145163
// LevelDB serializes writes as:
146164
// - byte: header
147165
// - 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)
155173
void CDBBatch::EraseImpl(Span<const std::byte> ssKey)
156174
{
157175
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
158-
batch.Delete(slKey);
176+
m_impl_batch->batch.Delete(slKey);
159177
// LevelDB serializes erases as:
160178
// - byte: header
161179
// - varint: key length
@@ -241,7 +259,7 @@ bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
241259
if (log_memory) {
242260
mem_before = DynamicMemoryUsage() / 1024.0 / 1024;
243261
}
244-
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
262+
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.m_impl_batch->batch);
245263
dbwrapper_private::HandleError(status);
246264
if (log_memory) {
247265
double mem_after = DynamicMemoryUsage() / 1024.0 / 1024;

src/dbwrapper.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <leveldb/options.h>
2121
#include <leveldb/slice.h>
2222
#include <leveldb/status.h>
23-
#include <leveldb/write_batch.h>
23+
#include <memory>
2424
#include <optional>
2525
#include <stdexcept>
2626
#include <string>
@@ -90,7 +90,9 @@ class CDBBatch
9090

9191
private:
9292
const CDBWrapper &parent;
93-
leveldb::WriteBatch batch;
93+
94+
struct WriteBatchImpl;
95+
const std::unique_ptr<WriteBatchImpl> m_impl_batch;
9496

9597
DataStream ssKey{};
9698
CDataStream ssValue;
@@ -104,13 +106,9 @@ class CDBBatch
104106
/**
105107
* @param[in] _parent CDBWrapper that this batch is to be submitted to
106108
*/
107-
explicit CDBBatch(const CDBWrapper& _parent) : parent(_parent), ssValue(SER_DISK, CLIENT_VERSION){};
108-
109-
void Clear()
110-
{
111-
batch.Clear();
112-
size_estimate = 0;
113-
}
109+
explicit CDBBatch(const CDBWrapper& _parent);
110+
~CDBBatch();
111+
void Clear();
114112

115113
template <typename K, typename V>
116114
void Write(const K& key, const V& value)

0 commit comments

Comments
 (0)