mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-15 15:50:09 +02:00
refactor: Pimpl leveldb::Iterator for CDBIterator
Hide the leveldb::Iterator member variable with a pimpl in order not to expose it directly in the header. Also, move CDBWrapper::NewIterator to the dbwrapper implementation to use the pimpl for CDBIterator initialziation. 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:
parent
ef941ff128
commit
e4af2408f2
@ -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 {
|
||||
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user