diff options
author | TheCharlatan <seb.kung@gmail.com> | 2023-07-29 12:03:35 +0200 |
---|---|---|
committer | TheCharlatan <seb.kung@gmail.com> | 2023-08-05 10:42:53 +0200 |
commit | e4af2408f2ac59788567b6fc8cb3a68fc43da9fe (patch) | |
tree | cde480915e567295a700b55c191febc7ea4bdc42 /src | |
parent | ef941ff1281e76308c3e746e592375bec023e9e4 (diff) | |
download | bitcoin-e4af2408f2ac59788567b6fc8cb3a68fc43da9fe.tar.xz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/dbwrapper.cpp | 28 | ||||
-rw-r--r-- | src/dbwrapper.h | 14 |
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. |