Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
bdb.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2020 The Bitcoin 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 BITCOIN_WALLET_BDB_H
7#define BITCOIN_WALLET_BDB_H
8
9#include <clientversion.h>
10#include <common/system.h>
11#include <serialize.h>
12#include <streams.h>
13#include <util/fs.h>
14#include <wallet/db.h>
15
16#include <db_cxx.h>
17
18#include <atomic>
19#include <condition_variable>
20#include <map>
21#include <memory>
22#include <string>
23#include <unordered_map>
24#include <vector>
25
26struct bilingual_str;
27
28static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
29static const bool DEFAULT_WALLET_PRIVDB = true;
30
35
37
39private:
41 bool fMockDb;
42 // Don't change into fs::path, as that can result in
43 // shutdown problems/crashes caused by a static initialized internal
44 // pointer.
45 std::string strPath;
46
47public:
48 std::unique_ptr<DbEnv> dbenv;
49 std::map<std::string, std::reference_wrapper<BerkeleyDatabase>> m_databases;
50 std::unordered_map<std::string, WalletDatabaseFileId> m_fileids;
51 std::condition_variable_any m_db_in_use;
52
56 void Reset();
57
58 void MakeMock();
59 bool IsMock() const { return fMockDb; }
60 bool IsInitialized() const { return fDbEnvInit; }
62
64 void Close();
65 void Flush(bool fShutdown);
66 void CheckpointLSN(const std::string &strFile);
67
68 void CloseDb(const std::string &strFile);
69 void ReloadDbEnv();
70
72 DbTxn *ptxn = nullptr;
73 int ret = dbenv->txn_begin(nullptr, &ptxn, flags);
74 if (!ptxn || ret != 0) {
75 return nullptr;
76 }
77 return ptxn;
78 }
79};
80
82std::shared_ptr<BerkeleyEnvironment>
84
86bool IsBerkeleyBtree(const fs::path &path);
87
88class BerkeleyBatch;
89
95public:
96 BerkeleyDatabase() = delete;
97
99 BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> envIn,
100 std::string filename)
102 strFile(std::move(filename)) {
103 auto inserted =
104 this->env->m_databases.emplace(strFile, std::ref(*this));
105 assert(inserted.second);
106 }
107
108 ~BerkeleyDatabase() override;
109
111 void Open() override;
112
117 bool Rewrite(const char *pszSkip = nullptr) override;
118
120 void AddRef() override;
121
126 void RemoveRef() override;
127
131 bool Backup(const std::string &strDest) const override;
132
136 void Flush() override;
137
142 void Close() override;
143
148 bool PeriodicFlush() override;
149
150 void IncrementUpdateCounter() override;
151
152 void ReloadDbEnv() override;
153
156
158 std::string Filename() override {
159 return fs::PathToString(env->Directory() / strFile);
160 }
161
171 std::shared_ptr<BerkeleyEnvironment> env;
172
177 std::unique_ptr<Db> m_db;
178
179 std::string strFile;
180
182 std::unique_ptr<DatabaseBatch>
183 MakeBatch(bool flush_on_close = true) override;
184};
185
191
192 public:
193 // construct Dbt with internally-managed data
194 SafeDbt();
195 // construct Dbt with provided data
196 SafeDbt(void *data, size_t size);
197 ~SafeDbt();
198
199 // delegate to Dbt
200 const void *get_data() const;
201 uint32_t get_size() const;
202
203 // conversion operator to access the underlying Dbt
204 operator Dbt *();
205 };
206
207private:
208 bool ReadKey(CDataStream &&key, CDataStream &value) override;
209 bool WriteKey(CDataStream &&key, CDataStream &&value,
210 bool overwrite = true) override;
211 bool EraseKey(CDataStream &&key) override;
212 bool HasKey(CDataStream &&key) override;
213
214protected:
216 std::string strFile;
223
224public:
225 explicit BerkeleyBatch(BerkeleyDatabase &database, const bool fReadOnly,
226 bool fFlushOnCloseIn = true);
227 ~BerkeleyBatch() override;
228
229 BerkeleyBatch(const BerkeleyBatch &) = delete;
231
232 void Flush() override;
233 void Close() override;
234
235 bool StartCursor() override;
236 bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue,
237 bool &complete) override;
238 void CloseCursor() override;
239 bool TxnBegin() override;
240 bool TxnCommit() override;
241 bool TxnAbort() override;
242};
243
244std::string BerkeleyDatabaseVersion();
245
247bool ExistsBerkeleyDatabase(const fs::path &path);
248
250std::unique_ptr<BerkeleyDatabase>
251MakeBerkeleyDatabase(const fs::path &path, const DatabaseOptions &options,
253
254#endif // BITCOIN_WALLET_BDB_H
bool ExistsBerkeleyDatabase(const fs::path &path)
Check if Berkeley database exists at specified path.
Definition bdb.cpp:900
std::unique_ptr< BerkeleyDatabase > MakeBerkeleyDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Return object giving access to Berkeley database at specified path.
Definition bdb.cpp:908
static const unsigned int DEFAULT_WALLET_DBLOGSIZE
Definition bdb.h:28
std::string BerkeleyDatabaseVersion()
Definition bdb.cpp:814
std::shared_ptr< BerkeleyEnvironment > GetWalletEnv(const fs::path &wallet_path, std::string &database_filename)
Get BerkeleyEnvironment and database filename given a wallet path.
Definition bdb.cpp:83
static const bool DEFAULT_WALLET_PRIVDB
Definition bdb.h:29
bool IsBerkeleyBtree(const fs::path &path)
Check format of database file.
int flags
RAII class that automatically cleanses its data on destruction.
Definition bdb.h:189
uint32_t get_size() const
Definition bdb.cpp:278
const void * get_data() const
Definition bdb.cpp:274
RAII class that provides access to a Berkeley database.
Definition bdb.h:187
void Close() override
Definition bdb.cpp:436
std::string strFile
Definition bdb.h:216
BerkeleyBatch(const BerkeleyBatch &)=delete
bool TxnCommit() override
Definition bdb.cpp:796
BerkeleyBatch & operator=(const BerkeleyBatch &)=delete
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition bdb.cpp:834
void Flush() override
Definition bdb.cpp:405
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition bdb.cpp:818
bool StartCursor() override
Definition bdb.cpp:738
void CloseCursor() override
Definition bdb.cpp:776
BerkeleyEnvironment * env
Definition bdb.h:221
bool TxnAbort() override
Definition bdb.cpp:805
Db * pdb
Definition bdb.h:215
bool EraseKey(CDataStream &&key) override
Definition bdb.cpp:853
~BerkeleyBatch() override
Definition bdb.cpp:431
DbTxn * activeTxn
Definition bdb.h:217
bool fFlushOnClose
Definition bdb.h:220
BerkeleyDatabase & m_database
Definition bdb.h:222
bool HasKey(CDataStream &&key) override
Definition bdb.cpp:867
bool fReadOnly
Definition bdb.h:219
Dbc * m_cursor
Definition bdb.h:218
bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override
Definition bdb.cpp:747
bool TxnBegin() override
Definition bdb.cpp:784
An instance of this class represents one database.
Definition bdb.h:94
std::shared_ptr< BerkeleyEnvironment > env
Pointer to shared database environment.
Definition bdb.h:171
void IncrementUpdateCounter() override
Definition bdb.cpp:427
void ReloadDbEnv() override
Definition bdb.cpp:734
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a BerkeleyBatch connected to this database.
Definition bdb.cpp:896
~BerkeleyDatabase() override
Definition bdb.cpp:322
std::string Filename() override
Return path to main database filename.
Definition bdb.h:158
bool Rewrite(const char *pszSkip=nullptr) override
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
Definition bdb.cpp:493
BerkeleyDatabase(std::shared_ptr< BerkeleyEnvironment > envIn, std::string filename)
Create DB handle to real database.
Definition bdb.h:99
std::string strFile
Definition bdb.h:179
void AddRef() override
Indicate the a new database user has began using the database.
Definition bdb.cpp:878
void Flush() override
Make sure all changes are flushed to database file.
Definition bdb.cpp:726
void Open() override
Open the database if it is not already opened.
Definition bdb.cpp:352
bool PeriodicFlush() override
flush the wallet passively (TRY_LOCK) ideal to be called periodically
Definition bdb.cpp:652
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition bdb.cpp:887
void Close() override
Flush to the database file and close the database.
Definition bdb.cpp:730
std::unique_ptr< Db > m_db
Database pointer.
Definition bdb.h:177
BerkeleyDatabase()=delete
bool Verify(bilingual_str &error)
Verifies the environment and database file.
Definition bdb.cpp:286
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition bdb.cpp:685
std::unordered_map< std::string, WalletDatabaseFileId > m_fileids
Definition bdb.h:50
bool IsInitialized() const
Definition bdb.h:60
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
Definition bdb.h:71
fs::path Directory() const
Definition bdb.h:61
bool IsMock() const
Definition bdb.h:59
void ReloadDbEnv()
Definition bdb.cpp:464
std::map< std::string, std::reference_wrapper< BerkeleyDatabase > > m_databases
Definition bdb.h:49
bool fDbEnvInit
Definition bdb.h:40
bool Open(bilingual_str &error)
Definition bdb.cpp:154
std::string strPath
Definition bdb.h:45
std::unique_ptr< DbEnv > dbenv
Definition bdb.h:48
std::condition_variable_any m_db_in_use
Definition bdb.h:51
void CheckpointLSN(const std::string &strFile)
Definition bdb.cpp:314
void Flush(bool fShutdown)
Definition bdb.cpp:593
BerkeleyEnvironment()
Construct an in-memory mock Berkeley environment for testing.
Definition bdb.cpp:227
void CloseDb(const std::string &strFile)
Definition bdb.cpp:452
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:177
RAII class that provides access to a WalletDatabase.
Definition db.h:24
An instance of this class represents one database.
Definition db.h:100
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:30
bool error(const char *fmt, const Args &...args)
Definition logging.h:226
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition fs.h:142
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition fs.h:165
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition rcu.h:257
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
bool operator==(const WalletDatabaseFileId &rhs) const
Definition bdb.cpp:66
uint8_t value[DB_FILE_ID_LEN]
Definition bdb.h:32
Bilingual messages:
Definition translation.h:17
assert(!tx.IsCoinBase())
DatabaseStatus
Definition db.h:229