Skip to content

Commit

Permalink
Add snapshot support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Nov 17, 2023
1 parent 6a8d27b commit 22a7319
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 61 deletions.
58 changes: 52 additions & 6 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ static const std::string DEFAULT_LEVELDB_CHECKSUM = "auto";

extern bool levelDBChecksum;

class CStorageSnapshot;

class dbwrapper_error : public std::runtime_error
{
public:
Expand Down Expand Up @@ -263,15 +265,20 @@ class CDBWrapper

template <typename K, typename V>
bool Read(const K& key, V& value) const
{
return Read(key, value, readoptions);
}

template <typename K, typename V>
bool Read(const K& key, V& value, const leveldb::ReadOptions &otherOptions) const
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(ssKey.data(), ssKey.size());
// leveldb::Slice slKey(SliceKey(key));

std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
leveldb::Status status = pdb->Get(otherOptions, slKey, &strValue);
if (!status.ok()) {
if (status.IsNotFound())
return false;
Expand All @@ -297,16 +304,20 @@ class CDBWrapper
}

template <typename K>
bool Exists(const K& key) const
bool Exists(const K& key) const {
return Exists(key, readoptions);
}

template <typename K>
bool Exists(const K& key, const leveldb::ReadOptions &otherOptions) const
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(ssKey.data(), ssKey.size());
// leveldb::Slice slKey(SliceKey(key));

std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
leveldb::Status status = pdb->Get(otherOptions, slKey, &strValue);
if (!status.ok()) {
if (status.IsNotFound())
return false;
Expand Down Expand Up @@ -343,7 +354,16 @@ class CDBWrapper

CDBIterator *NewIterator()
{
return new CDBIterator(*this, pdb->NewIterator(iteroptions));
return NewIterator(readoptions);
}

CDBIterator *NewIterator(const leveldb::ReadOptions &readOptions)
{
return new CDBIterator(*this, pdb->NewIterator(readOptions));
}

[[nodiscard]] std::unique_ptr<CStorageSnapshot> GetSnapshot() const {
return std::make_unique<CStorageSnapshot>(pdb);
}

/**
Expand Down Expand Up @@ -384,4 +404,30 @@ class CDBWrapper
}
};


class CStorageSnapshot {
public:
explicit CStorageSnapshot(leveldb::DB* db) : db(db) {
snapshot = db->GetSnapshot();
}

~CStorageSnapshot() {
if (db && snapshot) {
db->ReleaseSnapshot(snapshot);
}
}

[[nodiscard]] const leveldb::Snapshot* GetLevelDBSnapshot() const {
return snapshot;
}

CStorageSnapshot(const CStorageSnapshot&) = delete;
CStorageSnapshot& operator=(const CStorageSnapshot&) = delete;

private:
leveldb::DB* db; // Owned by pcustomcsDB
const leveldb::Snapshot* snapshot;
};


#endif // DEFI_DBWRAPPER_H
18 changes: 18 additions & 0 deletions src/dfi/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ CCustomCSView::CCustomCSView(CStorageKV &st)
CheckPrefixes();
}

CCustomCSView::CCustomCSView(std::unique_ptr<CStorageLevelDB> &st, const MapKV &changed)
: CStorageView(new CFlushableStorageKV(st, changed)) {
CheckPrefixes();
}

// cache-upon-a-cache (not a copy!) constructor
CCustomCSView::CCustomCSView(CCustomCSView &other)
: CStorageView(new CFlushableStorageKV(other.DB())),
Expand Down Expand Up @@ -1394,3 +1399,16 @@ void CalcMissingRewardTempFix(CCustomCSView &mnview, const uint32_t targetHeight
}
}
}

std::unique_ptr<CCustomCSView> GetViewSnapshot() {
LOCK(cs_main);

// Get database snapshot
auto snapshotDB = pcustomcsDB->GetSnapshotDB();

// Get any changes that have not yet been flushed
const auto &changed = pcustomcsview->GetStorage().GetRaw();

// Create new view using snapshot and change map
return std::make_unique<CCustomCSView>(snapshotDB, changed);
}
8 changes: 7 additions & 1 deletion src/dfi/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,14 @@ class CCustomCSView : public CMasternodesView,
// Increase version when underlaying tables are changed
static constexpr const int DbVersion = 1;

// Normal constructors
CCustomCSView();
explicit CCustomCSView(CStorageKV &st);

// cache-upon-a-cache (not a copy!) constructor
// Snapshot constructor
explicit CCustomCSView(std::unique_ptr<CStorageLevelDB> &st, const MapKV &changed);

// Cache-upon-a-cache constructors
CCustomCSView(CCustomCSView &other);
CCustomCSView(CCustomCSView &other,
CAccountHistoryStorage *historyView,
Expand Down Expand Up @@ -624,6 +628,8 @@ class CCustomCSView : public CMasternodesView,

std::map<CKeyID, CKey> AmISignerNow(int height, const CAnchorData::CTeam &team);

std::unique_ptr<CCustomCSView> GetViewSnapshot();

/** Global DB and view that holds enhanced chainstate data (should be protected by cs_main) */
extern std::unique_ptr<CStorageLevelDB> pcustomcsDB;
extern std::unique_ptr<CCustomCSView> pcustomcsview;
Expand Down
Loading

0 comments on commit 22a7319

Please sign in to comment.