Skip to content

Commit 84058e0

Browse files
committed
refactor: Split dbwrapper CDBWrapper::Read 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 e4af240 commit 84058e0

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/dbwrapper.cpp

Lines changed: 14 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>
@@ -300,6 +299,20 @@ std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
300299
return ret;
301300
}
302301

302+
std::optional<std::string> CDBWrapper::ReadImpl(Span<const std::byte> ssKey) const
303+
{
304+
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
305+
std::string strValue;
306+
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
307+
if (!status.ok()) {
308+
if (status.IsNotFound())
309+
return std::nullopt;
310+
LogPrintf("LevelDB read failure: %s\n", status.ToString());
311+
dbwrapper_private::HandleError(status);
312+
}
313+
return strValue;
314+
}
315+
303316
bool CDBWrapper::IsEmpty()
304317
{
305318
std::unique_ptr<CDBIterator> it(NewIterator());

src/dbwrapper.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ class CDBWrapper
235235
//! whether or not the database resides in memory
236236
bool m_is_memory;
237237

238+
std::optional<std::string> ReadImpl(Span<const std::byte> ssKey) const;
239+
238240
public:
239241
CDBWrapper(const DBParams& params);
240242
~CDBWrapper();
@@ -248,18 +250,12 @@ class CDBWrapper
248250
DataStream ssKey{};
249251
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
250252
ssKey << key;
251-
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
252-
253-
std::string strValue;
254-
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
255-
if (!status.ok()) {
256-
if (status.IsNotFound())
257-
return false;
258-
LogPrintf("LevelDB read failure: %s\n", status.ToString());
259-
dbwrapper_private::HandleError(status);
253+
std::optional<std::string> strValue{ReadImpl(ssKey)};
254+
if (!strValue) {
255+
return false;
260256
}
261257
try {
262-
CDataStream ssValue{MakeByteSpan(strValue), SER_DISK, CLIENT_VERSION};
258+
CDataStream ssValue{MakeByteSpan(*strValue), SER_DISK, CLIENT_VERSION};
263259
ssValue.Xor(obfuscate_key);
264260
ssValue >> value;
265261
} catch (const std::exception&) {

0 commit comments

Comments
 (0)