Skip to content

Commit e4af240

Browse files
committed
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.
1 parent ef941ff commit e4af240

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/dbwrapper.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <leveldb/write_batch.h>
3333
#include <memory>
3434
#include <optional>
35+
#include <utility>
3536

3637
bool DestroyDB(const std::string& path_str)
3738
{
@@ -306,26 +307,39 @@ bool CDBWrapper::IsEmpty()
306307
return !(it->Valid());
307308
}
308309

310+
struct CDBIterator::IteratorImpl {
311+
const std::unique_ptr<leveldb::Iterator> iter;
312+
313+
explicit IteratorImpl(leveldb::Iterator* _iter) : iter{_iter} {}
314+
};
315+
316+
CDBIterator::CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter) : parent(_parent), m_impl_iter(std::move(_piter)) {}
317+
318+
CDBIterator* CDBWrapper::NewIterator()
319+
{
320+
return new CDBIterator{*this, std::make_unique<CDBIterator::IteratorImpl>(pdb->NewIterator(iteroptions))};
321+
}
322+
309323
void CDBIterator::SeekImpl(Span<const std::byte> ssKey)
310324
{
311325
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
312-
piter->Seek(slKey);
326+
m_impl_iter->iter->Seek(slKey);
313327
}
314328

315329
Span<const std::byte> CDBIterator::GetKeyImpl() const
316330
{
317-
return MakeByteSpan(piter->key());
331+
return MakeByteSpan(m_impl_iter->iter->key());
318332
}
319333

320334
Span<const std::byte> CDBIterator::GetValueImpl() const
321335
{
322-
return MakeByteSpan(piter->value());
336+
return MakeByteSpan(m_impl_iter->iter->value());
323337
}
324338

325-
CDBIterator::~CDBIterator() { delete piter; }
326-
bool CDBIterator::Valid() const { return piter->Valid(); }
327-
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
328-
void CDBIterator::Next() { piter->Next(); }
339+
CDBIterator::~CDBIterator() = default;
340+
bool CDBIterator::Valid() const { return m_impl_iter->iter->Valid(); }
341+
void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
342+
void CDBIterator::Next() { m_impl_iter->iter->Next(); }
329343

330344
namespace dbwrapper_private {
331345

src/dbwrapper.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <vector>
2727
namespace leveldb {
2828
class Env;
29-
class Iterator;
3029
}
3130

3231
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
@@ -136,9 +135,12 @@ class CDBBatch
136135

137136
class CDBIterator
138137
{
138+
public:
139+
struct IteratorImpl;
140+
139141
private:
140142
const CDBWrapper &parent;
141-
leveldb::Iterator *piter;
143+
const std::unique_ptr<IteratorImpl> m_impl_iter;
142144

143145
void SeekImpl(Span<const std::byte> ssKey);
144146
Span<const std::byte> GetKeyImpl() const;
@@ -150,8 +152,7 @@ class CDBIterator
150152
* @param[in] _parent Parent CDBWrapper instance.
151153
* @param[in] _piter The original leveldb iterator.
152154
*/
153-
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter) :
154-
parent(_parent), piter(_piter) { };
155+
CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter);
155156
~CDBIterator();
156157

157158
bool Valid() const;
@@ -315,10 +316,7 @@ class CDBWrapper
315316
// Get an estimate of LevelDB memory usage (in bytes).
316317
size_t DynamicMemoryUsage() const;
317318

318-
CDBIterator *NewIterator()
319-
{
320-
return new CDBIterator(*this, pdb->NewIterator(iteroptions));
321-
}
319+
CDBIterator* NewIterator();
322320

323321
/**
324322
* Return true if the database managed by this class contains no entries.

0 commit comments

Comments
 (0)