Skip to content

Commit 6005165

Browse files
committed
refactor: Add Reader class for CDBWrapper::Read
This wraps manipulating the data streams for reading and writing the db keys and values in a class such that these can be used without specifying template arguments by the dbwrapper implementation. 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 029ac57 commit 6005165

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

src/dbwrapper.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <serialize.h>
1111
#include <span.h>
1212
#include <streams.h>
13-
#include <tinyformat.h>
1413
#include <util/fs.h>
1514
#include <util/fs_helpers.h>
1615
#include <util/strencodings.h>
@@ -301,6 +300,31 @@ std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
301300
return ret;
302301
}
303302

303+
bool CDBWrapper::ReadImpl(CDBWrapper::ReaderBase& reader) const
304+
{
305+
DataStream ssKey{};
306+
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
307+
reader.WriteKeyToStream(ssKey);
308+
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
309+
310+
std::string strValue;
311+
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
312+
if (!status.ok()) {
313+
if (status.IsNotFound())
314+
return false;
315+
LogPrintf("LevelDB read failure: %s\n", status.ToString());
316+
dbwrapper_private::HandleError(status);
317+
}
318+
try {
319+
CDataStream ssValue{MakeByteSpan(strValue), SER_DISK, CLIENT_VERSION};
320+
ssValue.Xor(obfuscate_key);
321+
reader.ReadValueFromStream(ssValue);
322+
} catch (const std::exception&) {
323+
return false;
324+
}
325+
return true;
326+
}
327+
304328
bool CDBWrapper::IsEmpty()
305329
{
306330
std::unique_ptr<CDBIterator> it(NewIterator());

src/dbwrapper.h

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,36 @@ class CDBWrapper
232232
//! whether or not the database resides in memory
233233
bool m_is_memory;
234234

235+
class ReaderBase
236+
{
237+
public:
238+
virtual void ReadValueFromStream(CDataStream& ssValue) = 0;
239+
virtual void WriteKeyToStream(DataStream& ssKey) = 0;
240+
};
241+
242+
template <typename K, typename V>
243+
class Reader : public ReaderBase
244+
{
245+
private:
246+
const K& key;
247+
V& value;
248+
249+
public:
250+
Reader(const K& key, V& value) : key{key}, value{value} {}
251+
252+
void ReadValueFromStream(CDataStream& ssValue) override
253+
{
254+
ssValue >> value;
255+
}
256+
257+
void WriteKeyToStream(DataStream& ssKey) override
258+
{
259+
ssKey << key;
260+
}
261+
};
262+
263+
bool ReadImpl(ReaderBase& reader) const;
264+
235265
public:
236266
CDBWrapper(const DBParams& params);
237267
~CDBWrapper();
@@ -242,27 +272,8 @@ class CDBWrapper
242272
template <typename K, typename V>
243273
bool Read(const K& key, V& value) const
244274
{
245-
DataStream ssKey{};
246-
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
247-
ssKey << key;
248-
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
249-
250-
std::string strValue;
251-
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
252-
if (!status.ok()) {
253-
if (status.IsNotFound())
254-
return false;
255-
LogPrintf("LevelDB read failure: %s\n", status.ToString());
256-
dbwrapper_private::HandleError(status);
257-
}
258-
try {
259-
CDataStream ssValue{MakeByteSpan(strValue), SER_DISK, CLIENT_VERSION};
260-
ssValue.Xor(obfuscate_key);
261-
ssValue >> value;
262-
} catch (const std::exception&) {
263-
return false;
264-
}
265-
return true;
275+
Reader reader{key, value};
276+
return ReadImpl(reader);
266277
}
267278

268279
template <typename K, typename V>

0 commit comments

Comments
 (0)