mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-03 11:39:17 +01:00
dbwrapper: have Read and Exists reuse ReadRaw
`ExistsImpl` was removed since it duplicates `CDBWrapper::ReadImpl` (except that it copies the resulting string on success, but that will be needed for caching anyway). `CDBWrapper::Exists()` and `::ReadRaw()` both issue a LevelDB `::Get`. Introduce a small helper returning the raw value bytes (as `std::string`) for a given key. Move `Exists` closer to the read methods.
This commit is contained in:
@@ -316,21 +316,6 @@ std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key)
|
||||
return strValue;
|
||||
}
|
||||
|
||||
bool CDBWrapper::ExistsImpl(std::span<const std::byte> key) const
|
||||
{
|
||||
leveldb::Slice slKey(CharCast(key.data()), key.size());
|
||||
|
||||
std::string strValue;
|
||||
leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
|
||||
if (!status.ok()) {
|
||||
if (status.IsNotFound())
|
||||
return false;
|
||||
LogError("LevelDB read failure: %s", status.ToString());
|
||||
HandleError(status);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t CDBWrapper::EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const
|
||||
{
|
||||
leveldb::Slice slKey1(CharCast(key1.data()), key1.size());
|
||||
|
||||
@@ -192,7 +192,6 @@ private:
|
||||
inline static const std::string OBFUSCATION_KEY{"\000obfuscate_key", 14}; // explicit size to avoid truncation at leading \0
|
||||
|
||||
std::optional<std::string> ReadImpl(std::span<const std::byte> key) const;
|
||||
bool ExistsImpl(std::span<const std::byte> key) const;
|
||||
size_t EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const;
|
||||
auto& DBContext() const LIFETIMEBOUND { return *Assert(m_db_context); }
|
||||
|
||||
@@ -203,13 +202,19 @@ public:
|
||||
CDBWrapper(const CDBWrapper&) = delete;
|
||||
CDBWrapper& operator=(const CDBWrapper&) = delete;
|
||||
|
||||
template <typename K, typename V>
|
||||
bool Read(const K& key, V& value) const
|
||||
template <typename K>
|
||||
std::optional<std::string> ReadRaw(const K& key) const
|
||||
{
|
||||
DataStream ssKey{};
|
||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey << key;
|
||||
std::optional<std::string> strValue{ReadImpl(ssKey)};
|
||||
return ReadImpl(ssKey);
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
bool Read(const K& key, V& value) const
|
||||
{
|
||||
auto strValue{ReadRaw(key)};
|
||||
if (!strValue) {
|
||||
return false;
|
||||
}
|
||||
@@ -223,6 +228,12 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
bool Exists(const K& key) const
|
||||
{
|
||||
return !!ReadRaw(key);
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void Write(const K& key, const V& value, bool fSync = false)
|
||||
{
|
||||
@@ -231,15 +242,6 @@ public:
|
||||
WriteBatch(batch, fSync);
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
bool Exists(const K& key) const
|
||||
{
|
||||
DataStream ssKey{};
|
||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey << key;
|
||||
return ExistsImpl(ssKey);
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
void Erase(const K& key, bool fSync = false)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user