Line data Source code
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 : 17 : class CEvoDBScopedCommitter 18 : { 19 : private: 20 : CEvoDB& evoDB; 21 : bool didCommitOrRollback{false}; 22 : 23 : public: 24 : explicit CEvoDBScopedCommitter(CEvoDB& _evoDB); 25 : ~CEvoDBScopedCommitter(); 26 : 27 : void Commit(); 28 : void Rollback(); 29 : }; 30 : 31 : class CEvoDB 32 : { 33 : public: 34 : RecursiveMutex cs; 35 : 36 : private: 37 : CDBWrapper db; 38 : 39 : typedef CDBTransaction<CDBWrapper, CDBBatch> RootTransaction; 40 : typedef CDBTransaction<RootTransaction, RootTransaction> CurTransaction; 41 : 42 : CDBBatch rootBatch; 43 : RootTransaction rootDBTransaction; 44 : CurTransaction curDBTransaction; 45 : 46 : public: 47 : explicit CEvoDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false); 48 : 49 57231 : std::unique_ptr<CEvoDBScopedCommitter> BeginTransaction() 50 : { 51 57231 : LOCK(cs); 52 114462 : return std::make_unique<CEvoDBScopedCommitter>(*this); 53 : } 54 : 55 10415 : CurTransaction& GetCurTransaction() 56 : { 57 10415 : AssertLockHeld(cs); // lock must be held from outside as long as the DB transaction is used 58 10415 : return curDBTransaction; 59 : } 60 : 61 : template<typename K, typename V> 62 68477 : bool Read(const K& key, V& value) 63 : { 64 68477 : LOCK(cs); 65 136954 : return curDBTransaction.Read(key, value); 66 : } 67 : 68 : template<typename K, typename V> 69 51711 : void Write(const K& key, const V& value) 70 : { 71 51711 : LOCK(cs); 72 51711 : curDBTransaction.Write(key, value); 73 51711 : } 74 : 75 : template <typename K> 76 687 : bool Exists(const K& key) 77 : { 78 687 : LOCK(cs); 79 1374 : return curDBTransaction.Exists(key); 80 : } 81 : 82 : template <typename K> 83 0 : void Erase(const K& key) 84 : { 85 0 : LOCK(cs); 86 0 : curDBTransaction.Erase(key); 87 0 : } 88 : 89 108 : CDBWrapper& GetRawDB() 90 : { 91 108 : return db; 92 : } 93 : 94 83793 : size_t GetMemoryUsage() 95 : { 96 83793 : return rootDBTransaction.GetMemoryUsage(); 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