Bitcoin Core  27.99.0
P2P Digital Currency
bdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 <atomic>
17 #include <condition_variable>
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 struct bilingual_str;
25 
26 class DbEnv;
27 class DbTxn;
28 class Db;
29 class Dbc;
30 
31 // This constant was introduced in BDB 4.0.14 and has never changed, but there
32 // is a belt-and-suspenders check in the cpp file just in case.
33 #define BDB_DB_FILE_ID_LEN 20 /* Unique file ID length. */
34 
35 namespace wallet {
36 
39  bool operator==(const WalletDatabaseFileId& rhs) const;
40 };
41 
42 class BerkeleyDatabase;
43 
45 {
46 private:
47  bool fDbEnvInit;
48  bool fMockDb;
49  // Don't change into fs::path, as that can result in
50  // shutdown problems/crashes caused by a static initialized internal pointer.
51  std::string strPath;
52 
53 public:
54  std::unique_ptr<DbEnv> dbenv;
55  std::map<fs::path, std::reference_wrapper<BerkeleyDatabase>> m_databases;
56  std::unordered_map<std::string, WalletDatabaseFileId> m_fileids;
57  std::condition_variable_any m_db_in_use;
59 
60  explicit BerkeleyEnvironment(const fs::path& env_directory, bool use_shared_memory);
63  void Reset();
64 
65  bool IsMock() const { return fMockDb; }
66  bool IsInitialized() const { return fDbEnvInit; }
68 
69  bool Open(bilingual_str& error);
70  void Close();
71  void Flush(bool fShutdown);
72  void CheckpointLSN(const std::string& strFile);
73 
74  void CloseDb(const fs::path& filename);
75  void ReloadDbEnv();
76 
77  DbTxn* TxnBegin(int flags);
78 };
79 
81 std::shared_ptr<BerkeleyEnvironment> GetBerkeleyEnv(const fs::path& env_directory, bool use_shared_memory);
82 
83 class BerkeleyBatch;
84 
89 {
90 public:
91  BerkeleyDatabase() = delete;
92 
94  BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, fs::path filename, const DatabaseOptions& options);
95 
96  ~BerkeleyDatabase() override;
97 
99  void Open() override;
100 
103  bool Rewrite(const char* pszSkip=nullptr) override;
104 
106  void AddRef() override;
108  void RemoveRef() override;
109 
112  bool Backup(const std::string& strDest) const override;
113 
116  void Flush() override;
120  void Close() override;
121  /* flush the wallet passively (TRY_LOCK)
122  ideal to be called periodically */
123  bool PeriodicFlush() override;
124 
125  void IncrementUpdateCounter() override;
126 
127  void ReloadDbEnv() override;
128 
130  bool Verify(bilingual_str& error);
131 
133  std::string Filename() override { return fs::PathToString(env->Directory() / m_filename); }
134 
135  std::string Format() override { return "bdb"; }
145  std::shared_ptr<BerkeleyEnvironment> env;
146 
148  std::unique_ptr<Db> m_db;
149 
150  // Whether to byteswap
152 
154  int64_t m_max_log_mb;
155 
157  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
158 };
159 
161 {
162 private:
163  Dbc* m_cursor;
164  std::vector<std::byte> m_key_prefix;
165  bool m_first{true};
166 
167 public:
168  // Constructor for cursor for records matching the prefix
169  // To match all records, an empty prefix may be provided.
170  explicit BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch, Span<const std::byte> prefix = {});
171  ~BerkeleyCursor() override;
172 
173  Status Next(DataStream& key, DataStream& value) override;
174  Dbc* dbc() const { return m_cursor; }
175 };
176 
179 {
180 private:
181  bool ReadKey(DataStream&& key, DataStream& value) override;
182  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override;
183  bool EraseKey(DataStream&& key) override;
184  bool HasKey(DataStream&& key) override;
186 
187 protected:
188  Db* pdb{nullptr};
189  std::string strFile;
190  DbTxn* activeTxn{nullptr};
191  bool fReadOnly;
195 
196 public:
197  explicit BerkeleyBatch(BerkeleyDatabase& database, const bool fReadOnly, bool fFlushOnCloseIn=true);
198  ~BerkeleyBatch() override;
199 
200  BerkeleyBatch(const BerkeleyBatch&) = delete;
202 
203  void Flush() override;
204  void Close() override;
205 
206  std::unique_ptr<DatabaseCursor> GetNewCursor() override;
207  std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override;
208  bool TxnBegin() override;
209  bool TxnCommit() override;
210  bool TxnAbort() override;
211  DbTxn* txn() const { return activeTxn; }
212 };
213 
214 std::string BerkeleyDatabaseVersion();
215 
219 
221 std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
222 } // namespace wallet
223 
224 #endif // BITCOIN_WALLET_BDB_H
#define BDB_DB_FILE_ID_LEN
Definition: bdb.h:33
int flags
Definition: bitcoin-tx.cpp:533
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
RAII class that provides access to a Berkeley database.
Definition: bdb.h:179
bool fFlushOnClose
Definition: bdb.h:192
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: bdb.cpp:846
void Close() override
Definition: bdb.cpp:450
bool HasKey(DataStream &&key) override
Definition: bdb.cpp:891
BerkeleyDatabase & m_database
Definition: bdb.h:194
bool EraseKey(DataStream &&key) override
Definition: bdb.cpp:878
BerkeleyBatch & operator=(const BerkeleyBatch &)=delete
~BerkeleyBatch() override
Definition: bdb.cpp:444
BerkeleyBatch(BerkeleyDatabase &database, const bool fReadOnly, bool fFlushOnCloseIn=true)
Definition: bdb.cpp:363
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: bdb.cpp:789
void Flush() override
Definition: bdb.cpp:424
DbTxn * txn() const
Definition: bdb.h:211
bool TxnCommit() override
Definition: bdb.cpp:806
BerkeleyEnvironment * env
Definition: bdb.h:193
bool TxnAbort() override
Definition: bdb.cpp:815
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: bdb.cpp:783
BerkeleyBatch(const BerkeleyBatch &)=delete
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: bdb.cpp:902
std::string strFile
Definition: bdb.h:189
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: bdb.cpp:863
DbTxn * activeTxn
Definition: bdb.h:190
bool TxnBegin() override
Definition: bdb.cpp:795
~BerkeleyCursor() override
Definition: bdb.cpp:776
std::vector< std::byte > m_key_prefix
Definition: bdb.h:164
BerkeleyCursor(BerkeleyDatabase &database, const BerkeleyBatch &batch, Span< const std::byte > prefix={})
Definition: bdb.cpp:729
Dbc * dbc() const
Definition: bdb.h:174
Status Next(DataStream &key, DataStream &value) override
Definition: bdb.cpp:743
An instance of this class represents one database.
Definition: bdb.h:89
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:515
void Open() override
Open the database if it is not already opened.
Definition: bdb.cpp:374
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: bdb.cpp:676
int64_t m_max_log_mb
Definition: bdb.h:154
fs::path m_filename
Definition: bdb.h:153
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: bdb.cpp:936
std::string Format() override
Definition: bdb.h:135
std::shared_ptr< BerkeleyEnvironment > env
Pointer to shared database environment.
Definition: bdb.h:145
void IncrementUpdateCounter() override
Definition: bdb.cpp:439
void ReloadDbEnv() override
Definition: bdb.cpp:724
void AddRef() override
Indicate that a new database user has begun using the database.
Definition: bdb.cpp:926
std::string Filename() override
Return path to main database filename.
Definition: bdb.h:133
std::unique_ptr< Db > m_db
Database pointer.
Definition: bdb.h:148
void Flush() override
Make sure all changes are flushed to database file.
Definition: bdb.cpp:714
bool PeriodicFlush() override
Definition: bdb.cpp:648
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a BerkeleyBatch connected to this database.
Definition: bdb.cpp:943
bool Verify(bilingual_str &error)
Verifies the environment and database file.
Definition: bdb.cpp:315
~BerkeleyDatabase() override
Definition: bdb.cpp:351
void Close() override
Flush to the database file and close the database.
Definition: bdb.cpp:719
fs::path Directory() const
Definition: bdb.h:67
std::condition_variable_any m_db_in_use
Definition: bdb.h:57
void CloseDb(const fs::path &filename)
Definition: bdb.cpp:463
bool IsMock() const
Definition: bdb.h:65
std::unique_ptr< DbEnv > dbenv
Definition: bdb.h:54
BerkeleyEnvironment()
Construct an in-memory mock Berkeley environment for testing.
Definition: bdb.cpp:210
bool IsInitialized() const
Definition: bdb.h:66
void CheckpointLSN(const std::string &strFile)
Definition: bdb.cpp:343
std::unordered_map< std::string, WalletDatabaseFileId > m_fileids
Definition: bdb.h:56
void Flush(bool fShutdown)
Definition: bdb.cpp:604
DbTxn * TxnBegin(int flags)
Definition: bdb.cpp:506
std::string strPath
Definition: bdb.h:51
bool Open(bilingual_str &error)
Definition: bdb.cpp:146
std::map< fs::path, std::reference_wrapper< BerkeleyDatabase > > m_databases
Definition: bdb.h:55
RAII class that provides access to a WalletDatabase.
Definition: db.h:51
An instance of this class represents one database.
Definition: db.h:130
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:174
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:948
std::string BerkeleyDatabaseVersion()
Definition: bdb.cpp:841
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory, bool use_shared_memory)
Get BerkeleyEnvironment given a directory path.
Definition: bdb.cpp:81
bool BerkeleyDatabaseSanityCheck()
Perform sanity check of runtime BDB version versus linked BDB version.
Definition: bdb.cpp:824
DatabaseStatus
Definition: db.h:204
const char * prefix
Definition: rest.cpp:1007
Bilingual messages:
Definition: translation.h:18
bool operator==(const WalletDatabaseFileId &rhs) const
Definition: bdb.cpp:70
uint8_t value[BDB_DB_FILE_ID_LEN]
Definition: bdb.h:38