Bitcoin ABC  0.24.7
P2P Digital Currency
db.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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_DB_H
7 #define BITCOIN_WALLET_DB_H
8 
9 #include <clientversion.h>
10 #include <fs.h>
11 #include <streams.h>
13 
14 #include <atomic>
15 #include <memory>
16 #include <string>
17 
18 struct bilingual_str;
19 
20 void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory,
21  std::string &database_filename);
22 
25 private:
26  virtual bool ReadKey(CDataStream &&key, CDataStream &value) = 0;
27  virtual bool WriteKey(CDataStream &&key, CDataStream &&value,
28  bool overwrite = true) = 0;
29  virtual bool EraseKey(CDataStream &&key) = 0;
30  virtual bool HasKey(CDataStream &&key) = 0;
31 
32 public:
33  explicit DatabaseBatch() {}
34  virtual ~DatabaseBatch() {}
35 
36  DatabaseBatch(const DatabaseBatch &) = delete;
37  DatabaseBatch &operator=(const DatabaseBatch &) = delete;
38 
39  virtual void Flush() = 0;
40  virtual void Close() = 0;
41 
42  template <typename K, typename T> bool Read(const K &key, T &value) {
44  ssKey.reserve(1000);
45  ssKey << key;
46 
48  if (!ReadKey(std::move(ssKey), ssValue)) {
49  return false;
50  }
51  try {
52  ssValue >> value;
53  return true;
54  } catch (const std::exception &) {
55  return false;
56  }
57  }
58 
59  template <typename K, typename T>
60  bool Write(const K &key, const T &value, bool fOverwrite = true) {
62  ssKey.reserve(1000);
63  ssKey << key;
64 
66  ssValue.reserve(10000);
67  ssValue << value;
68 
69  return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
70  }
71 
72  template <typename K> bool Erase(const K &key) {
74  ssKey.reserve(1000);
75  ssKey << key;
76 
77  return EraseKey(std::move(ssKey));
78  }
79 
80  template <typename K> bool Exists(const K &key) {
82  ssKey.reserve(1000);
83  ssKey << key;
84 
85  return HasKey(std::move(ssKey));
86  }
87 
88  virtual bool StartCursor() = 0;
89  virtual bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue,
90  bool &complete) = 0;
91  virtual void CloseCursor() = 0;
92  virtual bool TxnBegin() = 0;
93  virtual bool TxnCommit() = 0;
94  virtual bool TxnAbort() = 0;
95 };
96 
101 public:
105  nLastWalletUpdate(0) {}
106  virtual ~WalletDatabase(){};
107 
109  virtual void Open() = 0;
110 
113  std::atomic<int> m_refcount{0};
118  virtual void AddRef() = 0;
123  virtual void RemoveRef() = 0;
124 
129  virtual bool Rewrite(const char *pszSkip = nullptr) = 0;
130 
134  virtual bool Backup(const std::string &strDest) const = 0;
135 
139  virtual void Flush() = 0;
144  virtual void Close() = 0;
145  /* flush the wallet passively (TRY_LOCK)
146  ideal to be called periodically */
147  virtual bool PeriodicFlush() = 0;
148 
149  virtual void IncrementUpdateCounter() = 0;
150 
151  virtual void ReloadDbEnv() = 0;
152 
154  virtual std::string Filename() = 0;
155 
156  std::atomic<unsigned int> nUpdateCounter;
157  unsigned int nLastSeen;
158  unsigned int nLastFlushed;
160 
162  virtual std::unique_ptr<DatabaseBatch>
163  MakeBatch(bool flush_on_close = true) = 0;
164 };
165 
167 class DummyBatch : public DatabaseBatch {
168 private:
169  bool ReadKey(CDataStream &&key, CDataStream &value) override {
170  return true;
171  }
172  bool WriteKey(CDataStream &&key, CDataStream &&value,
173  bool overwrite = true) override {
174  return true;
175  }
176  bool EraseKey(CDataStream &&key) override { return true; }
177  bool HasKey(CDataStream &&key) override { return true; }
178 
179 public:
180  void Flush() override {}
181  void Close() override {}
182 
183  bool StartCursor() override { return true; }
184  bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue,
185  bool &complete) override {
186  return true;
187  }
188  void CloseCursor() override {}
189  bool TxnBegin() override { return true; }
190  bool TxnCommit() override { return true; }
191  bool TxnAbort() override { return true; }
192 };
193 
199 public:
200  void Open() override{};
201  void AddRef() override {}
202  void RemoveRef() override {}
203  bool Rewrite(const char *pszSkip = nullptr) override { return true; }
204  bool Backup(const std::string &strDest) const override { return true; }
205  void Close() override {}
206  void Flush() override {}
207  bool PeriodicFlush() override { return true; }
208  void IncrementUpdateCounter() override { ++nUpdateCounter; }
209  void ReloadDbEnv() override {}
210  std::string Filename() override { return "dummy"; }
211  std::unique_ptr<DatabaseBatch>
212  MakeBatch(bool flush_on_close = true) override {
213  return std::make_unique<DummyBatch>();
214  }
215 };
216 
217 enum class DatabaseFormat {
218  BERKELEY,
219 };
220 
222  bool require_existing = false;
223  bool require_create = false;
224  uint64_t create_flags = 0;
226  bool verify = true;
227 };
228 
229 enum class DatabaseStatus {
230  SUCCESS,
237  FAILED_LOAD,
240 };
241 
242 std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path &path,
243  const DatabaseOptions &options,
244  DatabaseStatus &status,
246 
247 #endif // BITCOIN_WALLET_DB_H
DatabaseOptions::require_existing
bool require_existing
Definition: db.h:222
DatabaseOptions
Definition: db.h:221
DatabaseStatus::FAILED_BAD_PATH
@ FAILED_BAD_PATH
DatabaseBatch::CloseCursor
virtual void CloseCursor()=0
DatabaseBatch::TxnCommit
virtual bool TxnCommit()=0
DummyBatch::CloseCursor
void CloseCursor() override
Definition: db.h:188
WalletDatabase::IncrementUpdateCounter
virtual void IncrementUpdateCounter()=0
WalletDatabase::Open
virtual void Open()=0
Open the database if it is not already opened.
DummyBatch::ReadKey
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: db.h:169
DatabaseBatch::TxnBegin
virtual bool TxnBegin()=0
DummyDatabase::ReloadDbEnv
void ReloadDbEnv() override
Definition: db.h:209
DatabaseOptions::verify
bool verify
Definition: db.h:226
fs.h
streams.h
DatabaseStatus::FAILED_VERIFY
@ FAILED_VERIFY
DatabaseOptions::create_passphrase
SecureString create_passphrase
Definition: db.h:225
WalletDatabase::~WalletDatabase
virtual ~WalletDatabase()
Definition: db.h:106
DatabaseBatch::Read
bool Read(const K &key, T &value)
Definition: db.h:42
bilingual_str
Bilingual messages:
Definition: translation.h:17
DatabaseBatch::WriteKey
virtual bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true)=0
DummyBatch
RAII class that provides access to a DummyDatabase.
Definition: db.h:167
DummyBatch::Close
void Close() override
Definition: db.h:181
DatabaseBatch::DatabaseBatch
DatabaseBatch()
Definition: db.h:33
DummyBatch::Flush
void Flush() override
Definition: db.h:180
DatabaseOptions::require_create
bool require_create
Definition: db.h:223
DummyDatabase::IncrementUpdateCounter
void IncrementUpdateCounter() override
Definition: db.h:208
WalletDatabase::Close
virtual void Close()=0
Flush to the database file and close the database.
clientversion.h
DatabaseFormat::BERKELEY
@ BERKELEY
WalletDatabase
An instance of this class represents one database.
Definition: db.h:100
DatabaseStatus::FAILED_BAD_FORMAT
@ FAILED_BAD_FORMAT
WalletDatabase::Backup
virtual bool Backup(const std::string &strDest) const =0
Back up the entire database to a file.
DummyBatch::TxnAbort
bool TxnAbort() override
Definition: db.h:191
DatabaseBatch::Erase
bool Erase(const K &key)
Definition: db.h:72
DatabaseStatus::FAILED_ALREADY_LOADED
@ FAILED_ALREADY_LOADED
DatabaseFormat
DatabaseFormat
Definition: db.h:217
DummyDatabase
A dummy WalletDatabase that does nothing and never fails.
Definition: db.h:198
DatabaseBatch::EraseKey
virtual bool EraseKey(CDataStream &&key)=0
DummyBatch::EraseKey
bool EraseKey(CDataStream &&key) override
Definition: db.h:176
DatabaseBatch::Write
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:60
DatabaseBatch::Flush
virtual void Flush()=0
DummyBatch::TxnBegin
bool TxnBegin() override
Definition: db.h:189
DatabaseStatus::FAILED_ENCRYPT
@ FAILED_ENCRYPT
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
DatabaseBatch::TxnAbort
virtual bool TxnAbort()=0
WalletDatabase::nUpdateCounter
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:156
WalletDatabase::nLastWalletUpdate
int64_t nLastWalletUpdate
Definition: db.h:159
DummyDatabase::Close
void Close() override
Flush to the database file and close the database.
Definition: db.h:205
DatabaseStatus::FAILED_NOT_FOUND
@ FAILED_NOT_FOUND
DatabaseStatus::FAILED_ALREADY_EXISTS
@ FAILED_ALREADY_EXISTS
WalletDatabase::RemoveRef
virtual void RemoveRef()=0
Indicate that database user has stopped using the database and that it could be flushed or closed.
DatabaseBatch::Exists
bool Exists(const K &key)
Definition: db.h:80
WalletDatabase::nLastFlushed
unsigned int nLastFlushed
Definition: db.h:158
DummyBatch::HasKey
bool HasKey(CDataStream &&key) override
Definition: db.h:177
DatabaseBatch::~DatabaseBatch
virtual ~DatabaseBatch()
Definition: db.h:34
DatabaseBatch
RAII class that provides access to a WalletDatabase.
Definition: db.h:24
DummyBatch::ReadAtCursor
bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override
Definition: db.h:184
DatabaseBatch::Close
virtual void Close()=0
DummyDatabase::MakeBatch
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: db.h:212
DummyDatabase::AddRef
void AddRef() override
Indicate the a new database user has began using the database.
Definition: db.h:201
WalletDatabase::ReloadDbEnv
virtual void ReloadDbEnv()=0
WalletDatabase::MakeBatch
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
DummyDatabase::Open
void Open() override
Open the database if it is not already opened.
Definition: db.h:200
DatabaseStatus::FAILED_LOAD
@ FAILED_LOAD
DummyDatabase::Backup
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: db.h:204
WalletDatabase::AddRef
virtual void AddRef()=0
Indicate the a new database user has began using the database.
CDataStream::reserve
void reserve(size_type n)
Definition: streams.h:283
secure.h
WalletDatabase::Flush
virtual void Flush()=0
Make sure all changes are flushed to database file.
DatabaseBatch::ReadAtCursor
virtual bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete)=0
DatabaseBatch::ReadKey
virtual bool ReadKey(CDataStream &&key, CDataStream &value)=0
DummyBatch::TxnCommit
bool TxnCommit() override
Definition: db.h:190
WalletDatabase::nLastSeen
unsigned int nLastSeen
Definition: db.h:157
CLIENT_VERSION
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:44
WalletDatabase::m_refcount
std::atomic< int > m_refcount
Counts the number of active database users to be sure that the database is not closed while someone i...
Definition: db.h:113
DummyDatabase::Flush
void Flush() override
Make sure all changes are flushed to database file.
Definition: db.h:206
WalletDatabase::Rewrite
virtual bool Rewrite(const char *pszSkip=nullptr)=0
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
DummyBatch::StartCursor
bool StartCursor() override
Definition: db.h:183
MakeDatabase
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1114
DatabaseBatch::HasKey
virtual bool HasKey(CDataStream &&key)=0
WalletDatabase::WalletDatabase
WalletDatabase()
Create dummy DB handle.
Definition: db.h:103
DatabaseStatus::SUCCESS
@ SUCCESS
DatabaseStatus
DatabaseStatus
Definition: db.h:229
SER_DISK
@ SER_DISK
Definition: serialize.h:166
DummyDatabase::Filename
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: db.h:210
WalletDatabase::PeriodicFlush
virtual bool PeriodicFlush()=0
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
WalletDatabase::Filename
virtual std::string Filename()=0
Return path to main database file for logs and error messages.
DatabaseBatch::operator=
DatabaseBatch & operator=(const DatabaseBatch &)=delete
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
DummyDatabase::Rewrite
bool Rewrite(const char *pszSkip=nullptr) override
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
Definition: db.h:203
DatabaseBatch::StartCursor
virtual bool StartCursor()=0
DummyBatch::WriteKey
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: db.h:172
SplitWalletPath
void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, std::string &database_filename)
Definition: db.cpp:11
DatabaseOptions::create_flags
uint64_t create_flags
Definition: db.h:224
DummyDatabase::PeriodicFlush
bool PeriodicFlush() override
Definition: db.h:207
DatabaseStatus::FAILED_CREATE
@ FAILED_CREATE
DummyDatabase::RemoveRef
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: db.h:202