PIVX Core  5.6.99
P2P Digital Currency
evodb.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2021 The Dash Core developers
2 // Copyright (c) 2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef PIVX_EVO_EVODB_H
7 #define PIVX_EVO_EVODB_H
8 
9 #include "dbwrapper.h"
10 #include "sync.h"
11 #include "uint256.h"
12 
13 static const std::string EVODB_BEST_BLOCK = "b_b";
14 
15 class CEvoDB;
16 
18 {
19 private:
21  bool didCommitOrRollback{false};
22 
23 public:
24  explicit CEvoDBScopedCommitter(CEvoDB& _evoDB);
26 
27  void Commit();
28  void Rollback();
29 };
30 
31 class CEvoDB
32 {
33 public:
35 
36 private:
38 
41 
45 
46 public:
47  explicit CEvoDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
48 
49  std::unique_ptr<CEvoDBScopedCommitter> BeginTransaction()
50  {
51  LOCK(cs);
52  return std::make_unique<CEvoDBScopedCommitter>(*this);
53  }
54 
56  {
57  AssertLockHeld(cs); // lock must be held from outside as long as the DB transaction is used
58  return curDBTransaction;
59  }
60 
61  template<typename K, typename V>
62  bool Read(const K& key, V& value)
63  {
64  LOCK(cs);
65  return curDBTransaction.Read(key, value);
66  }
67 
68  template<typename K, typename V>
69  void Write(const K& key, const V& value)
70  {
71  LOCK(cs);
72  curDBTransaction.Write(key, value);
73  }
74 
75  template <typename K>
76  bool Exists(const K& key)
77  {
78  LOCK(cs);
79  return curDBTransaction.Exists(key);
80  }
81 
82  template <typename K>
83  void Erase(const K& key)
84  {
85  LOCK(cs);
87  }
88 
90  {
91  return db;
92  }
93 
94  size_t GetMemoryUsage()
95  {
97  }
98 
99  bool CommitRootTransaction();
100 
101  bool VerifyBestBlock(const uint256& hash);
102  void WriteBestBlock(const uint256& hash);
103 
104 private:
105  // only CEvoDBScopedCommitter is allowed to invoke these
106  friend class CEvoDBScopedCommitter;
107  void CommitCurTransaction();
108  void RollbackCurTransaction();
109 };
110 
111 extern std::unique_ptr<CEvoDB> evoDb;
112 
113 #endif // PIVX_EVO_EVODB_H
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:46
void Write(const K &key, const V &v)
Definition: dbwrapper.h:591
void Erase(const K &key)
Definition: dbwrapper.h:658
bool Exists(const K &key)
Definition: dbwrapper.h:639
bool Read(const K &key, V &value)
Definition: dbwrapper.h:613
size_t GetMemoryUsage() const
Definition: dbwrapper.h:698
Definition: evodb.h:32
void RollbackCurTransaction()
Definition: evodb.cpp:50
bool Exists(const K &key)
Definition: evodb.h:76
CurTransaction & GetCurTransaction()
Definition: evodb.h:55
CDBWrapper db
Definition: evodb.h:37
bool VerifyBestBlock(const uint256 &hash)
Definition: evodb.cpp:66
CDBBatch rootBatch
Definition: evodb.h:42
void Erase(const K &key)
Definition: evodb.h:83
void WriteBestBlock(const uint256 &hash)
Definition: evodb.cpp:72
CEvoDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: evodb.cpp:37
CDBWrapper & GetRawDB()
Definition: evodb.h:89
CDBTransaction< RootTransaction, RootTransaction > CurTransaction
Definition: evodb.h:40
bool CommitRootTransaction()
Definition: evodb.cpp:56
void Write(const K &key, const V &value)
Definition: evodb.h:69
bool Read(const K &key, V &value)
Definition: evodb.h:62
size_t GetMemoryUsage()
Definition: evodb.h:94
std::unique_ptr< CEvoDBScopedCommitter > BeginTransaction()
Definition: evodb.h:49
void CommitCurTransaction()
Definition: evodb.cpp:44
CurTransaction curDBTransaction
Definition: evodb.h:44
RecursiveMutex cs
Definition: evodb.h:34
CDBTransaction< CDBWrapper, CDBBatch > RootTransaction
Definition: evodb.h:39
RootTransaction rootDBTransaction
Definition: evodb.h:43
CEvoDB & evoDB
Definition: evodb.h:20
CEvoDBScopedCommitter(CEvoDB &_evoDB)
Definition: evodb.cpp:12
bool didCommitOrRollback
Definition: evodb.h:21
256-bit opaque blob.
Definition: uint256.h:138
std::unique_ptr< CEvoDB > evoDb
Definition: evodb.cpp:10
@ LOCK
Definition: lockunlock.h:16
#define AssertLockHeld(cs)
Definition: sync.h:75