mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
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.
This commit is contained in:
@@ -10,7 +10,6 @@
|
|||||||
#include <serialize.h>
|
#include <serialize.h>
|
||||||
#include <span.h>
|
#include <span.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <tinyformat.h>
|
|
||||||
#include <util/fs.h>
|
#include <util/fs.h>
|
||||||
#include <util/fs_helpers.h>
|
#include <util/fs_helpers.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
@@ -300,6 +299,20 @@ std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<std::string> CDBWrapper::ReadImpl(Span<const std::byte> ssKey) const
|
||||||
|
{
|
||||||
|
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
|
||||||
|
std::string strValue;
|
||||||
|
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
||||||
|
if (!status.ok()) {
|
||||||
|
if (status.IsNotFound())
|
||||||
|
return std::nullopt;
|
||||||
|
LogPrintf("LevelDB read failure: %s\n", status.ToString());
|
||||||
|
dbwrapper_private::HandleError(status);
|
||||||
|
}
|
||||||
|
return strValue;
|
||||||
|
}
|
||||||
|
|
||||||
bool CDBWrapper::IsEmpty()
|
bool CDBWrapper::IsEmpty()
|
||||||
{
|
{
|
||||||
std::unique_ptr<CDBIterator> it(NewIterator());
|
std::unique_ptr<CDBIterator> it(NewIterator());
|
||||||
|
|||||||
@@ -235,6 +235,8 @@ private:
|
|||||||
//! whether or not the database resides in memory
|
//! whether or not the database resides in memory
|
||||||
bool m_is_memory;
|
bool m_is_memory;
|
||||||
|
|
||||||
|
std::optional<std::string> ReadImpl(Span<const std::byte> ssKey) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CDBWrapper(const DBParams& params);
|
CDBWrapper(const DBParams& params);
|
||||||
~CDBWrapper();
|
~CDBWrapper();
|
||||||
@@ -248,18 +250,12 @@ public:
|
|||||||
DataStream ssKey{};
|
DataStream ssKey{};
|
||||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||||
ssKey << key;
|
ssKey << key;
|
||||||
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
|
std::optional<std::string> strValue{ReadImpl(ssKey)};
|
||||||
|
if (!strValue) {
|
||||||
std::string strValue;
|
|
||||||
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
|
||||||
if (!status.ok()) {
|
|
||||||
if (status.IsNotFound())
|
|
||||||
return false;
|
return false;
|
||||||
LogPrintf("LevelDB read failure: %s\n", status.ToString());
|
|
||||||
dbwrapper_private::HandleError(status);
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
CDataStream ssValue{MakeByteSpan(strValue), SER_DISK, CLIENT_VERSION};
|
CDataStream ssValue{MakeByteSpan(*strValue), SER_DISK, CLIENT_VERSION};
|
||||||
ssValue.Xor(obfuscate_key);
|
ssValue.Xor(obfuscate_key);
|
||||||
ssValue >> value;
|
ssValue >> value;
|
||||||
} catch (const std::exception&) {
|
} catch (const std::exception&) {
|
||||||
|
|||||||
Reference in New Issue
Block a user