Bitcoin ABC  0.28.12
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;
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
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:199
void reserve(size_type n)
Definition: streams.h:249
RAII class that provides access to a WalletDatabase.
Definition: db.h:24
bool Erase(const K &key)
Definition: db.h:72
DatabaseBatch(const DatabaseBatch &)=delete
virtual bool TxnCommit()=0
virtual bool TxnBegin()=0
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:60
virtual bool EraseKey(CDataStream &&key)=0
DatabaseBatch & operator=(const DatabaseBatch &)=delete
virtual bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete)=0
virtual void CloseCursor()=0
virtual ~DatabaseBatch()
Definition: db.h:34
bool Read(const K &key, T &value)
Definition: db.h:42
virtual void Flush()=0
bool Exists(const K &key)
Definition: db.h:80
DatabaseBatch()
Definition: db.h:33
virtual bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true)=0
virtual void Close()=0
virtual bool HasKey(CDataStream &&key)=0
virtual bool StartCursor()=0
virtual bool TxnAbort()=0
virtual bool ReadKey(CDataStream &&key, CDataStream &value)=0
RAII class that provides access to a DummyDatabase.
Definition: db.h:167
bool StartCursor() override
Definition: db.h:183
void Flush() override
Definition: db.h:180
bool TxnAbort() override
Definition: db.h:191
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: db.h:169
bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override
Definition: db.h:184
void Close() override
Definition: db.h:181
bool TxnCommit() override
Definition: db.h:190
bool EraseKey(CDataStream &&key) override
Definition: db.h:176
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: db.h:172
bool TxnBegin() override
Definition: db.h:189
void CloseCursor() override
Definition: db.h:188
bool HasKey(CDataStream &&key) override
Definition: db.h:177
A dummy WalletDatabase that does nothing and never fails.
Definition: db.h:198
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: db.h:212
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: db.h:204
void Open() override
Open the database if it is not already opened.
Definition: db.h:200
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: db.h:202
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: db.h:210
void IncrementUpdateCounter() override
Definition: db.h:208
void AddRef() override
Indicate the a new database user has began using the database.
Definition: db.h:201
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
bool PeriodicFlush() override
Definition: db.h:207
void ReloadDbEnv() override
Definition: db.h:209
void Close() override
Flush to the database file and close the database.
Definition: db.h:205
void Flush() override
Make sure all changes are flushed to database file.
Definition: db.h:206
An instance of this class represents one database.
Definition: db.h:100
unsigned int nLastFlushed
Definition: db.h:158
unsigned int nLastSeen
Definition: db.h:157
virtual void Open()=0
Open the database if it is not already opened.
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:156
virtual void RemoveRef()=0
Indicate that database user has stopped using the database and that it could be flushed or closed.
virtual void ReloadDbEnv()=0
virtual void Flush()=0
Make sure all changes are flushed to database file.
virtual bool Backup(const std::string &strDest) const =0
Back up the entire database to a file.
WalletDatabase()
Create dummy DB handle.
Definition: db.h:103
virtual bool Rewrite(const char *pszSkip=nullptr)=0
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
virtual ~WalletDatabase()
Definition: db.h:106
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
virtual void AddRef()=0
Indicate the a new database user has began using the database.
virtual std::string Filename()=0
Return path to main database file for logs and error messages.
int64_t nLastWalletUpdate
Definition: db.h:159
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
virtual void IncrementUpdateCounter()=0
virtual void Close()=0
Flush to the database file and close the database.
virtual bool PeriodicFlush()=0
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:55
@ SER_DISK
Definition: serialize.h:167
bool verify
Definition: db.h:226
bool require_create
Definition: db.h:223
uint64_t create_flags
Definition: db.h:224
bool require_existing
Definition: db.h:222
SecureString create_passphrase
Definition: db.h:225
Bilingual messages:
Definition: translation.h:17
bool error(const char *fmt, const Args &...args)
Definition: system.h:45
DatabaseStatus
Definition: db.h:229
void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, std::string &database_filename)
Definition: db.cpp:11
DatabaseFormat
Definition: db.h:217
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1120