dbwrapper: have Read and Exists reuse ReadRaw

`ExistsImpl` was removed since it duplicates `CDBWrapper::ReadImpl`.
The only difference is that `ExistsImpl` discards the value while `ReadImpl` returns it as `std::string`.
In this series, `HaveCoin` database lookups go through `GetCoin`/`Read`, and successful reads are cached in `CCoinsViewCache`, so this copy is not a separate recurring cost.
`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:
Lőrinc
2026-01-14 23:48:48 +01:00
parent 277c57f0c5
commit c0c94ec986
2 changed files with 15 additions and 28 deletions

View File

@@ -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());

View File

@@ -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)
{