Bitcoin Core  27.99.0
P2P Digital Currency
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
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>
16 #include <atomic>
17 #include <condition_variable>
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
24 struct bilingual_str;
26 class DbEnv;
27 class DbTxn;
28 class Db;
29 class Dbc;
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. */
35 namespace wallet {
39  bool operator==(const WalletDatabaseFileId& rhs) const;
40 };
42 class BerkeleyDatabase;
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;
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;
60  explicit BerkeleyEnvironment(const fs::path& env_directory, bool use_shared_memory);
63  void Reset();
65  bool IsMock() const { return fMockDb; }
66  bool IsInitialized() const { return fDbEnvInit; }
69  bool Open(bilingual_str& error);
70  void Close();
71  void Flush(bool fShutdown);
72  void CheckpointLSN(const std::string& strFile);
74  void CloseDb(const fs::path& filename);
75  void ReloadDbEnv();
77  DbTxn* TxnBegin(int flags);
78 };
81 std::shared_ptr<BerkeleyEnvironment> GetBerkeleyEnv(const fs::path& env_directory, bool use_shared_memory);
83 class BerkeleyBatch;
89 {
90 public:
91  BerkeleyDatabase() = delete;
94  BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, fs::path filename, const DatabaseOptions& options);
96  ~BerkeleyDatabase() override;
99  void Open() override;
103  bool Rewrite(const char* pszSkip=nullptr) override;
106  void AddRef() override;
108  void RemoveRef() override;
112  bool Backup(const std::string& strDest) const override;
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;
125  void IncrementUpdateCounter() override;
127  void ReloadDbEnv() override;
130  bool Verify(bilingual_str& error);
133  std::string Filename() override { return fs::PathToString(env->Directory() / m_filename); }
135  std::string Format() override { return "bdb"; }
145  std::shared_ptr<BerkeleyEnvironment> env;
148  std::unique_ptr<Db> m_db;
151  int64_t m_max_log_mb;
154  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
155 };
158 {
159 private:
160  Dbc* m_cursor;
161  std::vector<std::byte> m_key_prefix;
162  bool m_first{true};
164 public:
165  // Constructor for cursor for records matching the prefix
166  // To match all records, an empty prefix may be provided.
167  explicit BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch, Span<const std::byte> prefix = {});
168  ~BerkeleyCursor() override;
170  Status Next(DataStream& key, DataStream& value) override;
171  Dbc* dbc() const { return m_cursor; }
172 };
176 {
177 private:
178  bool ReadKey(DataStream&& key, DataStream& value) override;
179  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override;
180  bool EraseKey(DataStream&& key) override;
181  bool HasKey(DataStream&& key) override;
184 protected:
185  Db* pdb{nullptr};
186  std::string strFile;
187  DbTxn* activeTxn{nullptr};
188  bool fReadOnly;
193 public:
194  explicit BerkeleyBatch(BerkeleyDatabase& database, const bool fReadOnly, bool fFlushOnCloseIn=true);
195  ~BerkeleyBatch() override;
197  BerkeleyBatch(const BerkeleyBatch&) = delete;
200  void Flush() override;
201  void Close() override;
203  std::unique_ptr<DatabaseCursor> GetNewCursor() override;
204  std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override;
205  bool TxnBegin() override;
206  bool TxnCommit() override;
207  bool TxnAbort() override;
208  DbTxn* txn() const { return activeTxn; }
209 };
211 std::string BerkeleyDatabaseVersion();
218 std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
219 } // namespace wallet
221 #endif // BITCOIN_WALLET_BDB_H
Definition: bdb.h:33
int flags
Definition: bitcoin-tx.cpp:530
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:176
bool fFlushOnClose
Definition: bdb.h:189
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: bdb.cpp:832
void Close() override
Definition: bdb.cpp:440
bool HasKey(DataStream &&key) override
Definition: bdb.cpp:877
BerkeleyDatabase & m_database
Definition: bdb.h:191
bool EraseKey(DataStream &&key) override
Definition: bdb.cpp:864
BerkeleyBatch & operator=(const BerkeleyBatch &)=delete
~BerkeleyBatch() override
Definition: bdb.cpp:434
BerkeleyBatch(BerkeleyDatabase &database, const bool fReadOnly, bool fFlushOnCloseIn=true)
Definition: bdb.cpp:357
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: bdb.cpp:775
void Flush() override
Definition: bdb.cpp:414
DbTxn * txn() const
Definition: bdb.h:208
bool TxnCommit() override
Definition: bdb.cpp:792
BerkeleyEnvironment * env
Definition: bdb.h:190
bool TxnAbort() override
Definition: bdb.cpp:801
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: bdb.cpp:769
BerkeleyBatch(const BerkeleyBatch &)=delete
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: bdb.cpp:888
std::string strFile
Definition: bdb.h:186
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: bdb.cpp:849
DbTxn * activeTxn
Definition: bdb.h:187
bool TxnBegin() override
Definition: bdb.cpp:781
~BerkeleyCursor() override
Definition: bdb.cpp:762
std::vector< std::byte > m_key_prefix
Definition: bdb.h:161
BerkeleyCursor(BerkeleyDatabase &database, const BerkeleyBatch &batch, Span< const std::byte > prefix={})
Definition: bdb.cpp:715
Dbc * dbc() const
Definition: bdb.h:171
Status Next(DataStream &key, DataStream &value) override
Definition: bdb.cpp:729
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:505
void Open() override
Open the database if it is not already opened.
Definition: bdb.cpp:368
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: bdb.cpp:662
int64_t m_max_log_mb
Definition: bdb.h:151
fs::path m_filename
Definition: bdb.h:150
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: bdb.cpp:922
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:429
void ReloadDbEnv() override
Definition: bdb.cpp:710
void AddRef() override
Indicate that a new database user has begun using the database.
Definition: bdb.cpp:912
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:700
bool PeriodicFlush() override
Definition: bdb.cpp:634
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a BerkeleyBatch connected to this database.
Definition: bdb.cpp:929
bool Verify(bilingual_str &error)
Verifies the environment and database file.
Definition: bdb.cpp:309
~BerkeleyDatabase() override
Definition: bdb.cpp:345
void Close() override
Flush to the database file and close the database.
Definition: bdb.cpp:705
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:453
bool IsMock() const
Definition: bdb.h:65
std::unique_ptr< DbEnv > dbenv
Definition: bdb.h:54
Construct an in-memory mock Berkeley environment for testing.
Definition: bdb.cpp:208
bool IsInitialized() const
Definition: bdb.h:66
void CheckpointLSN(const std::string &strFile)
Definition: bdb.cpp:337
std::unordered_map< std::string, WalletDatabaseFileId > m_fileids
Definition: bdb.h:56
void Flush(bool fShutdown)
Definition: bdb.cpp:590
DbTxn * TxnBegin(int flags)
Definition: bdb.cpp:496
std::string strPath
Definition: bdb.h:51
bool Open(bilingual_str &error)
Definition: bdb.cpp:144
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:45
An instance of this class represents one database.
Definition: db.h:124
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:934
std::string BerkeleyDatabaseVersion()
Definition: bdb.cpp:827
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory, bool use_shared_memory)
Get BerkeleyEnvironment given a directory path.
Definition: bdb.cpp:79
bool BerkeleyDatabaseSanityCheck()
Perform sanity check of runtime BDB version versus linked BDB version.
Definition: bdb.cpp:810
Definition: db.h:196
const char * prefix
Definition: rest.cpp:1007
Bilingual messages:
Definition: translation.h:18
bool operator==(const WalletDatabaseFileId &rhs) const
Definition: bdb.cpp:68
uint8_t value[BDB_DB_FILE_ID_LEN]
Definition: bdb.h:38