Bitcoin Core  21.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-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  explicit BerkeleyEnvironment(const fs::path& env_directory);
62  void Reset();
63 
64  bool IsMock() const { return fMockDb; }
65  bool IsInitialized() const { return fDbEnvInit; }
66  fs::path Directory() const { return strPath; }
67 
68  bool Open(bilingual_str& error);
69  void Close();
70  void Flush(bool fShutdown);
71  void CheckpointLSN(const std::string& strFile);
72 
73  void CloseDb(const std::string& strFile);
74  void ReloadDbEnv();
75 
76  DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
77  {
78  DbTxn* ptxn = nullptr;
79  int ret = dbenv->txn_begin(nullptr, &ptxn, flags);
80  if (!ptxn || ret != 0)
81  return nullptr;
82  return ptxn;
83  }
84 };
85 
87 std::shared_ptr<BerkeleyEnvironment> GetBerkeleyEnv(const fs::path& env_directory);
88 
89 class BerkeleyBatch;
90 
95 {
96 public:
97  BerkeleyDatabase() = delete;
98 
100  BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, std::string filename) :
101  WalletDatabase(), env(std::move(env)), strFile(std::move(filename))
102  {
103  auto inserted = this->env->m_databases.emplace(strFile, std::ref(*this));
104  assert(inserted.second);
105  }
106 
107  ~BerkeleyDatabase() override;
108 
110  void Open() override;
111 
114  bool Rewrite(const char* pszSkip=nullptr) override;
115 
117  void AddRef() override;
119  void RemoveRef() override;
120 
123  bool Backup(const std::string& strDest) const override;
124 
127  void Flush() override;
131  void Close() override;
132  /* flush the wallet passively (TRY_LOCK)
133  ideal to be called periodically */
134  bool PeriodicFlush() override;
135 
136  void IncrementUpdateCounter() override;
137 
138  void ReloadDbEnv() override;
139 
141  bool Verify(bilingual_str& error);
142 
144  std::string Filename() override { return (env->Directory() / strFile).string(); }
145 
146  std::string Format() override { return "bdb"; }
156  std::shared_ptr<BerkeleyEnvironment> env;
157 
159  std::unique_ptr<Db> m_db;
160 
161  std::string strFile;
162 
164  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
165 };
166 
169 {
171  class SafeDbt final
172  {
173  Dbt m_dbt;
174 
175  public:
176  // construct Dbt with internally-managed data
177  SafeDbt();
178  // construct Dbt with provided data
179  SafeDbt(void* data, size_t size);
180  ~SafeDbt();
181 
182  // delegate to Dbt
183  const void* get_data() const;
184  u_int32_t get_size() const;
185 
186  // conversion operator to access the underlying Dbt
187  operator Dbt*();
188  };
189 
190 private:
191  bool ReadKey(CDataStream&& key, CDataStream& value) override;
192  bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
193  bool EraseKey(CDataStream&& key) override;
194  bool HasKey(CDataStream&& key) override;
195 
196 protected:
197  Db* pdb;
198  std::string strFile;
199  DbTxn* activeTxn;
200  Dbc* m_cursor;
201  bool fReadOnly;
205 
206 public:
207  explicit BerkeleyBatch(BerkeleyDatabase& database, const bool fReadOnly, bool fFlushOnCloseIn=true);
208  ~BerkeleyBatch() override;
209 
210  BerkeleyBatch(const BerkeleyBatch&) = delete;
211  BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;
212 
213  void Flush() override;
214  void Close() override;
215 
216  bool StartCursor() override;
217  bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override;
218  void CloseCursor() override;
219  bool TxnBegin() override;
220  bool TxnCommit() override;
221  bool TxnAbort() override;
222 };
223 
224 std::string BerkeleyDatabaseVersion();
225 
229 
231 std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
232 
233 #endif // BITCOIN_WALLET_BDB_H
BerkeleyEnvironment::BerkeleyEnvironment
BerkeleyEnvironment()
Construct an in-memory mock Berkeley environment for testing.
Definition: bdb.cpp:189
DatabaseOptions
Definition: db.h:203
BerkeleyBatch::SafeDbt::SafeDbt
SafeDbt()
Definition: bdb.cpp:219
BerkeleyEnvironment::dbenv
std::unique_ptr< DbEnv > dbenv
Definition: bdb.h:54
assert
assert(!tx.IsCoinBase())
GetBerkeleyEnv
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory)
Get BerkeleyEnvironment given a directory path.
Definition: bdb.cpp:61
DatabaseStatus
DatabaseStatus
Definition: db.h:212
BerkeleyDatabaseSanityCheck
bool BerkeleyDatabaseSanityCheck()
Perform sanity check of runtime BDB version versus linked BDB version.
Definition: bdb.cpp:726
fs.h
flags
int flags
Definition: bitcoin-tx.cpp:512
BerkeleyDatabase::Flush
void Flush() override
Make sure all changes are flushed to database file.
Definition: bdb.cpp:640
streams.h
bilingual_str
Bilingual messages:
Definition: translation.h:16
BerkeleyDatabase::Open
void Open() override
Open the database if it is not already opened.
Definition: bdb.cpp:322
BerkeleyBatch::SafeDbt
RAII class that automatically cleanses its data on destruction.
Definition: bdb.h:171
BerkeleyBatch::fFlushOnClose
bool fFlushOnClose
Definition: bdb.h:202
BerkeleyBatch::activeTxn
DbTxn * activeTxn
Definition: bdb.h:199
clientversion.h
WalletDatabase
An instance of this class represents one database.
Definition: db.h:103
BerkeleyDatabase::AddRef
void AddRef() override
Indicate the a new database user has began using the database.
Definition: bdb.cpp:803
BerkeleyBatch::operator=
BerkeleyBatch & operator=(const BerkeleyBatch &)=delete
BerkeleyEnvironment::~BerkeleyEnvironment
~BerkeleyEnvironment()
Definition: bdb.cpp:119
BerkeleyDatabase::Rewrite
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:449
BerkeleyEnvironment::fMockDb
bool fMockDb
Definition: bdb.h:48
BerkeleyBatch::ReadKey
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: bdb.cpp:748
BerkeleyEnvironment::m_db_in_use
std::condition_variable_any m_db_in_use
Definition: bdb.h:57
db.h
BerkeleyBatch::TxnAbort
bool TxnAbort() override
Definition: bdb.cpp:717
BerkeleyDatabase::m_db
std::unique_ptr< Db > m_db
Database pointer.
Definition: bdb.h:159
BerkeleyBatch::SafeDbt::m_dbt
Dbt m_dbt
Definition: bdb.h:173
BerkeleyEnvironment::ReloadDbEnv
void ReloadDbEnv()
Definition: bdb.cpp:422
WalletDatabaseFileId::operator==
bool operator==(const WalletDatabaseFileId &rhs) const
Definition: bdb.cpp:50
BerkeleyBatch::Flush
void Flush() override
Definition: bdb.cpp:367
BerkeleyEnvironment::fDbEnvInit
bool fDbEnvInit
Definition: bdb.h:47
BerkeleyDatabase::strFile
std::string strFile
Definition: bdb.h:161
BerkeleyBatch::BerkeleyBatch
BerkeleyBatch(BerkeleyDatabase &database, const bool fReadOnly, bool fFlushOnCloseIn=true)
Definition: bdb.cpp:305
BerkeleyDatabase::env
std::shared_ptr< BerkeleyEnvironment > env
Pointer to shared database environment.
Definition: bdb.h:156
BerkeleyBatch::~BerkeleyBatch
~BerkeleyBatch() override
Definition: bdb.cpp:387
BerkeleyEnvironment::m_fileids
std::unordered_map< std::string, WalletDatabaseFileId > m_fileids
Definition: bdb.h:56
BerkeleyDatabase
An instance of this class represents one database.
Definition: bdb.h:94
BerkeleyEnvironment::Flush
void Flush(bool fShutdown)
Definition: bdb.cpp:533
BerkeleyBatch::SafeDbt::get_data
const void * get_data() const
Definition: bdb.cpp:243
BerkeleyDatabase::Verify
bool Verify(bilingual_str &error)
Verifies the environment and database file.
Definition: bdb.cpp:258
BerkeleyBatch::WriteKey
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: bdb.cpp:764
BerkeleyBatch::fReadOnly
bool fReadOnly
Definition: bdb.h:201
BerkeleyDatabase::~BerkeleyDatabase
~BerkeleyDatabase() override
Definition: bdb.cpp:293
BerkeleyBatch::strFile
std::string strFile
Definition: bdb.h:198
BerkeleyBatch::Close
void Close() override
Definition: bdb.cpp:393
BerkeleyDatabase::ReloadDbEnv
void ReloadDbEnv() override
Definition: bdb.cpp:650
BerkeleyEnvironment::IsInitialized
bool IsInitialized() const
Definition: bdb.h:65
BerkeleyEnvironment::Directory
fs::path Directory() const
Definition: bdb.h:66
BerkeleyDatabaseVersion
std::string BerkeleyDatabaseVersion()
Definition: bdb.cpp:743
BerkeleyEnvironment::Reset
void Reset()
Definition: bdb.cpp:107
DatabaseBatch
RAII class that provides access to a WalletDatabase.
Definition: db.h:24
WalletDatabaseFileId
Definition: bdb.h:37
BerkeleyBatch::HasKey
bool HasKey(CDataStream &&key) override
Definition: bdb.cpp:792
BerkeleyBatch::EraseKey
bool EraseKey(CDataStream &&key) override
Definition: bdb.cpp:779
DEFAULT_WALLET_PRIVDB
static const bool DEFAULT_WALLET_PRIVDB
Definition: bdb.h:35
BerkeleyBatch::SafeDbt::~SafeDbt
~SafeDbt()
Definition: bdb.cpp:229
BerkeleyBatch::m_cursor
Dbc * m_cursor
Definition: bdb.h:200
system.h
BerkeleyBatch::m_database
BerkeleyDatabase & m_database
Definition: bdb.h:204
WalletDatabaseFileId::value
u_int8_t value[DB_FILE_ID_LEN]
Definition: bdb.h:38
BerkeleyDatabase::PeriodicFlush
bool PeriodicFlush() override
Definition: bdb.cpp:576
BerkeleyBatch
RAII class that provides access to a Berkeley database.
Definition: bdb.h:168
BerkeleyBatch::StartCursor
bool StartCursor() override
Definition: bdb.cpp:655
BerkeleyEnvironment::m_databases
std::map< std::string, std::reference_wrapper< BerkeleyDatabase > > m_databases
Definition: bdb.h:55
MakeBerkeleyDatabase
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:825
BerkeleyEnvironment::CheckpointLSN
void CheckpointLSN(const std::string &strFile)
Definition: bdb.cpp:285
BerkeleyEnvironment::Close
void Close()
Definition: bdb.cpp:77
BerkeleyBatch::SafeDbt::get_size
u_int32_t get_size() const
Definition: bdb.cpp:248
BerkeleyEnvironment::IsMock
bool IsMock() const
Definition: bdb.h:64
BerkeleyDatabase::Format
std::string Format() override
Definition: bdb.h:146
BerkeleyEnvironment::strPath
std::string strPath
Definition: bdb.h:51
BerkeleyDatabase::Backup
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: bdb.cpp:603
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
BerkeleyBatch::env
BerkeleyEnvironment * env
Definition: bdb.h:203
BerkeleyBatch::TxnCommit
bool TxnCommit() override
Definition: bdb.cpp:708
BerkeleyBatch::pdb
Db * pdb
Definition: bdb.h:197
BerkeleyDatabase::BerkeleyDatabase
BerkeleyDatabase()=delete
BerkeleyDatabase::IncrementUpdateCounter
void IncrementUpdateCounter() override
Definition: bdb.cpp:382
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
BerkeleyEnvironment::Open
bool Open(bilingual_str &error)
Definition: bdb.cpp:126
BerkeleyBatch::CloseCursor
void CloseCursor() override
Definition: bdb.cpp:690
BerkeleyDatabase::Filename
std::string Filename() override
Return path to main database filename.
Definition: bdb.h:144
BerkeleyDatabase::Close
void Close() override
Flush to the database file and close the database.
Definition: bdb.cpp:645
BerkeleyBatch::TxnBegin
bool TxnBegin() override
Definition: bdb.cpp:697
BerkeleyDatabase::RemoveRef
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: bdb.cpp:813
BerkeleyDatabase::BerkeleyDatabase
BerkeleyDatabase(std::shared_ptr< BerkeleyEnvironment > env, std::string filename)
Create DB handle to real database.
Definition: bdb.h:100
DEFAULT_WALLET_DBLOGSIZE
static const unsigned int DEFAULT_WALLET_DBLOGSIZE
Definition: bdb.h:34
BerkeleyEnvironment::CloseDb
void CloseDb(const std::string &strFile)
Definition: bdb.cpp:407
BerkeleyBatch::ReadAtCursor
bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override
Definition: bdb.cpp:664
BerkeleyDatabase::MakeBatch
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a BerkeleyBatch connected to this database.
Definition: bdb.cpp:820
BerkeleyEnvironment
Definition: bdb.h:44
BerkeleyEnvironment::TxnBegin
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
Definition: bdb.h:76