aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dbwrapper.cpp28
-rw-r--r--src/dbwrapper.h14
2 files changed, 27 insertions, 15 deletions
diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp
index 2f0b9a5ff3..c0a2895a58 100644
--- a/src/dbwrapper.cpp
+++ b/src/dbwrapper.cpp
@@ -32,6 +32,7 @@
#include <leveldb/write_batch.h>
#include <memory>
#include <optional>
+#include <utility>
bool DestroyDB(const std::string& path_str)
{
@@ -306,26 +307,39 @@ bool CDBWrapper::IsEmpty()
return !(it->Valid());
}
+struct CDBIterator::IteratorImpl {
+ const std::unique_ptr<leveldb::Iterator> iter;
+
+ explicit IteratorImpl(leveldb::Iterator* _iter) : iter{_iter} {}
+};
+
+CDBIterator::CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter) : parent(_parent), m_impl_iter(std::move(_piter)) {}
+
+CDBIterator* CDBWrapper::NewIterator()
+{
+ return new CDBIterator{*this, std::make_unique<CDBIterator::IteratorImpl>(pdb->NewIterator(iteroptions))};
+}
+
void CDBIterator::SeekImpl(Span<const std::byte> ssKey)
{
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
- piter->Seek(slKey);
+ m_impl_iter->iter->Seek(slKey);
}
Span<const std::byte> CDBIterator::GetKeyImpl() const
{
- return MakeByteSpan(piter->key());
+ return MakeByteSpan(m_impl_iter->iter->key());
}
Span<const std::byte> CDBIterator::GetValueImpl() const
{
- return MakeByteSpan(piter->value());
+ return MakeByteSpan(m_impl_iter->iter->value());
}
-CDBIterator::~CDBIterator() { delete piter; }
-bool CDBIterator::Valid() const { return piter->Valid(); }
-void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
-void CDBIterator::Next() { piter->Next(); }
+CDBIterator::~CDBIterator() = default;
+bool CDBIterator::Valid() const { return m_impl_iter->iter->Valid(); }
+void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
+void CDBIterator::Next() { m_impl_iter->iter->Next(); }
namespace dbwrapper_private {
diff --git a/src/dbwrapper.h b/src/dbwrapper.h
index 2e6cc4d81e..9bcb79c169 100644
--- a/src/dbwrapper.h
+++ b/src/dbwrapper.h
@@ -26,7 +26,6 @@
#include <vector>
namespace leveldb {
class Env;
-class Iterator;
}
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
@@ -136,9 +135,12 @@ public:
class CDBIterator
{
+public:
+ struct IteratorImpl;
+
private:
const CDBWrapper &parent;
- leveldb::Iterator *piter;
+ const std::unique_ptr<IteratorImpl> m_impl_iter;
void SeekImpl(Span<const std::byte> ssKey);
Span<const std::byte> GetKeyImpl() const;
@@ -150,8 +152,7 @@ public:
* @param[in] _parent Parent CDBWrapper instance.
* @param[in] _piter The original leveldb iterator.
*/
- CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter) :
- parent(_parent), piter(_piter) { };
+ CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter);
~CDBIterator();
bool Valid() const;
@@ -315,10 +316,7 @@ public:
// Get an estimate of LevelDB memory usage (in bytes).
size_t DynamicMemoryUsage() const;
- CDBIterator *NewIterator()
- {
- return new CDBIterator(*this, pdb->NewIterator(iteroptions));
- }
+ CDBIterator* NewIterator();
/**
* Return true if the database managed by this class contains no entries.