Bitcoin Core  0.20.99
P2P Digital Currency
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 <fs.h>
11 #include <serialize.h>
12 #include <streams.h>
13 #include <util/system.h>
14 #include <wallet/db.h>
15 
16 #include <atomic>
17 #include <map>
18 #include <memory>
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #if defined(__GNUC__) && !defined(__clang__)
24 #pragma GCC diagnostic push
25 #pragma GCC diagnostic ignored "-Wsuggest-override"
26 #endif
27 #include <db_cxx.h>
28 #if defined(__GNUC__) && !defined(__clang__)
29 #pragma GCC diagnostic pop
30 #endif
31 
32 struct bilingual_str;
33 
34 static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
35 static const bool DEFAULT_WALLET_PRIVDB = true;
36 
38  u_int8_t value[DB_FILE_ID_LEN];
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<std::string, std::reference_wrapper<BerkeleyDatabase>> m_databases;
56  std::unordered_map<std::string, WalletDatabaseFileId> m_fileids;
57  std::condition_variable_any m_db_in_use;
58 
59  BerkeleyEnvironment(const fs::path& env_directory);
62  void Reset();
63 
64  bool IsMock() const { return fMockDb; }
65  bool IsInitialized() const { return fDbEnvInit; }
66  bool IsDatabaseLoaded(const std::string& db_filename) const { return m_databases.find(db_filename) != m_databases.end(); }
67  fs::path Directory() const { return strPath; }
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 std::string& strFile);
75  void ReloadDbEnv();
76 
77  DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
78  {
79  DbTxn* ptxn = nullptr;
80  int ret = dbenv->txn_begin(nullptr, &ptxn, flags);
81  if (!ptxn || ret != 0)
82  return nullptr;
83  return ptxn;
84  }
85 };
86 
88 std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);
89 
91 bool IsBDBWalletLoaded(const fs::path& wallet_path);
92 
93 class BerkeleyBatch;
94 
99 {
100 public:
101  BerkeleyDatabase() = delete;
102 
104  BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, std::string filename) :
105  WalletDatabase(), env(std::move(env)), strFile(std::move(filename))
106  {
107  auto inserted = this->env->m_databases.emplace(strFile, std::ref(*this));
108  assert(inserted.second);
109  }
110 
111  ~BerkeleyDatabase() override;
112 
115  void Open(const char* mode) override;
116 
119  bool Rewrite(const char* pszSkip=nullptr) override;
120 
122  void AddRef() override;
124  void RemoveRef() override;
125 
128  bool Backup(const std::string& strDest) const override;
129 
132  void Flush() override;
136  void Close() override;
137  /* flush the wallet passively (TRY_LOCK)
138  ideal to be called periodically */
139  bool PeriodicFlush() override;
140 
141  void IncrementUpdateCounter() override;
142 
143  void ReloadDbEnv() override;
144 
146  bool Verify(bilingual_str& error) override;
147 
157  std::shared_ptr<BerkeleyEnvironment> env;
158 
160  std::unique_ptr<Db> m_db;
161 
162  std::string strFile;
163 
165  std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override;
166 };
167 
170 {
172  class SafeDbt final
173  {
174  Dbt m_dbt;
175 
176  public:
177  // construct Dbt with internally-managed data
178  SafeDbt();
179  // construct Dbt with provided data
180  SafeDbt(void* data, size_t size);
181  ~SafeDbt();
182 
183  // delegate to Dbt
184  const void* get_data() const;
185  u_int32_t get_size() const;
186 
187  // conversion operator to access the underlying Dbt
188  operator Dbt*();
189  };
190 
191 private:
192  bool ReadKey(CDataStream&& key, CDataStream& value) override;
193  bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
194  bool EraseKey(CDataStream&& key) override;
195  bool HasKey(CDataStream&& key) override;
196 
197 protected:
198  Db* pdb;
199  std::string strFile;
200  DbTxn* activeTxn;
201  Dbc* m_cursor;
202  bool fReadOnly;
206 
207 public:
208  explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
209  ~BerkeleyBatch() override;
210 
211  BerkeleyBatch(const BerkeleyBatch&) = delete;
212  BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;
213 
214  void Flush() override;
215  void Close() override;
216 
217  bool StartCursor() override;
218  bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override;
219  void CloseCursor() override;
220  bool TxnBegin() override;
221  bool TxnCommit() override;
222  bool TxnAbort() override;
223 };
224 
225 std::string BerkeleyDatabaseVersion();
226 
227 #endif // BITCOIN_WALLET_BDB_H
bool fReadOnly
Definition: bdb.h:202
bool IsInitialized() const
Definition: bdb.h:65
bool fDbEnvInit
Definition: bdb.h:47
std::unordered_map< std::string, WalletDatabaseFileId > m_fileids
Definition: bdb.h:56
Bilingual messages:
Definition: translation.h:16
bool fFlushOnClose
Definition: bdb.h:203
BerkeleyDatabase & m_database
Definition: bdb.h:205
An instance of this class represents one database.
Definition: bdb.h:98
bool IsDatabaseLoaded(const std::string &db_filename) const
Definition: bdb.h:66
static const unsigned int DEFAULT_WALLET_DBLOGSIZE
Definition: bdb.h:34
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:201
static const bool DEFAULT_WALLET_PRIVDB
Definition: bdb.h:35
fs::path Directory() const
Definition: bdb.h:67
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
Definition: bdb.h:77
std::shared_ptr< BerkeleyEnvironment > env
Pointer to shared database environment.
Definition: bdb.h:157
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:74
std::map< std::string, std::reference_wrapper< BerkeleyDatabase > > m_databases
Definition: bdb.h:55
std::unique_ptr< DbEnv > dbenv
Definition: bdb.h:54
BerkeleyDatabase(std::shared_ptr< BerkeleyEnvironment > env, std::string filename)
Create DB handle to real database.
Definition: bdb.h:104
std::string strFile
Definition: bdb.h:199
RAII class that provides access to a WalletDatabase.
Definition: db.h:25
DbTxn * activeTxn
Definition: bdb.h:200
RAII class that provides access to a Berkeley database.
Definition: bdb.h:169
std::string strFile
Definition: bdb.h:162
bool operator==(const WalletDatabaseFileId &rhs) const
Definition: bdb.cpp:50
int flags
Definition: bitcoin-tx.cpp:509
Dbc * m_cursor
Definition: bdb.h:201
bool IsBDBWalletLoaded(const fs::path &wallet_path)
Return whether a BDB wallet database is currently loaded.
Definition: bdb.cpp:55
std::string strPath
Definition: bdb.h:51
BerkeleyEnvironment * env
Definition: bdb.h:204
u_int8_t value[DB_FILE_ID_LEN]
Definition: bdb.h:38
Db * pdb
Definition: bdb.h:198
std::string BerkeleyDatabaseVersion()
Definition: bdb.cpp:746
bool IsMock() const
Definition: bdb.h:64
bool fMockDb
Definition: bdb.h:48
std::unique_ptr< Db > m_db
Database pointer.
Definition: bdb.h:160
bool error(const char *fmt, const Args &... args)
Definition: system.h:52
RAII class that automatically cleanses its data on destruction.
Definition: bdb.h:172
An instance of this class represents one database.
Definition: db.h:104
std::condition_variable_any m_db_in_use
Definition: bdb.h:57