Skip to content

Commit

Permalink
Merge #1287: NFT db records optimizations
Browse files Browse the repository at this point in the history
Pull request description:

  * Adding an iterator wrapper class
  * Prevent double lookups
  * Change lookups from O(n) to log O(n)
  • Loading branch information
dexX7 committed Feb 27, 2023
2 parents e3476d1 + efa860b commit eb70615
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 232 deletions.
6 changes: 2 additions & 4 deletions src/omnicore/dbbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ void CDBBase::Clear()
int64_t nTimeStart = GetTimeMicros();
unsigned int n = 0;
leveldb::WriteBatch batch;
leveldb::Iterator* it = NewIterator();
CDBaseIterator it{NewIterator()};

for (it->SeekToFirst(); it->Valid(); it->Next()) {
for (; it; ++it) {
batch.Delete(it->key());
++n;
}

delete it;

leveldb::Status status = pdb->Write(writeoptions, &batch);
nRead = 0;
nWritten = 0;
Expand Down
45 changes: 45 additions & 0 deletions src/omnicore/dbbase.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef BITCOIN_OMNICORE_DBBASE_H
#define BITCOIN_OMNICORE_DBBASE_H

#include <algorithm>
#include <leveldb/db.h>

#include <fs.h>

#include <assert.h>
#include <memory>
#include <stddef.h>

/** Base class for LevelDB based storage.
Expand Down Expand Up @@ -93,5 +95,48 @@ class CDBBase
void Clear();
};

class CDBaseIterator
{
private:
std::unique_ptr<leveldb::Iterator> it;

public:
explicit CDBaseIterator(leveldb::Iterator* i, const std::string& first = {}) : it(i)
{
assert(it);
first.empty() ? it->SeekToFirst() : it->Seek(first);
}

CDBaseIterator& operator=(CDBaseIterator&& i)
{
it = std::move(i.it);
assert(it);
return *this;
}

CDBaseIterator& operator++()
{
assert(it->Valid());
it->Next();
return *this;
}

CDBaseIterator& operator--()
{
assert(it->Valid());
it->Prev();
return *this;
}

operator bool() const
{
return it->Valid();
}

leveldb::Iterator* operator->()
{
return it.get();
}
};

#endif // BITCOIN_OMNICORE_DBBASE_H
Loading

0 comments on commit eb70615

Please sign in to comment.