Bitcoin Core  27.99.0
P2P Digital Currency
db.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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 <streams.h>
12 #include <util/fs.h>
13 
14 #include <atomic>
15 #include <memory>
16 #include <optional>
17 #include <string>
18 
19 class ArgsManager;
20 struct bilingual_str;
21 
22 namespace wallet {
23 // BytePrefix compares equality with other byte spans that begin with the same prefix.
24 struct BytePrefix {
26 };
29 
31 {
32 public:
33  explicit DatabaseCursor() = default;
34  virtual ~DatabaseCursor() = default;
35 
36  DatabaseCursor(const DatabaseCursor&) = delete;
38 
39  enum class Status
40  {
41  FAIL,
42  MORE,
43  DONE,
44  };
45 
46  virtual Status Next(DataStream& key, DataStream& value) { return Status::FAIL; }
47 };
48 
51 {
52 private:
53  virtual bool ReadKey(DataStream&& key, DataStream& value) = 0;
54  virtual bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) = 0;
55  virtual bool EraseKey(DataStream&& key) = 0;
56  virtual bool HasKey(DataStream&& key) = 0;
57 
58 public:
59  explicit DatabaseBatch() = default;
60  virtual ~DatabaseBatch() = default;
61 
62  DatabaseBatch(const DatabaseBatch&) = delete;
64 
65  virtual void Flush() = 0;
66  virtual void Close() = 0;
67 
68  template <typename K, typename T>
69  bool Read(const K& key, T& value)
70  {
71  DataStream ssKey{};
72  ssKey.reserve(1000);
73  ssKey << key;
74 
75  DataStream ssValue{};
76  if (!ReadKey(std::move(ssKey), ssValue)) return false;
77  try {
78  ssValue >> value;
79  return true;
80  } catch (const std::exception&) {
81  return false;
82  }
83  }
84 
85  template <typename K, typename T>
86  bool Write(const K& key, const T& value, bool fOverwrite = true)
87  {
88  DataStream ssKey{};
89  ssKey.reserve(1000);
90  ssKey << key;
91 
92  DataStream ssValue{};
93  ssValue.reserve(10000);
94  ssValue << value;
95 
96  return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
97  }
98 
99  template <typename K>
100  bool Erase(const K& key)
101  {
102  DataStream ssKey{};
103  ssKey.reserve(1000);
104  ssKey << key;
105 
106  return EraseKey(std::move(ssKey));
107  }
108 
109  template <typename K>
110  bool Exists(const K& key)
111  {
112  DataStream ssKey{};
113  ssKey.reserve(1000);
114  ssKey << key;
115 
116  return HasKey(std::move(ssKey));
117  }
119 
120  virtual std::unique_ptr<DatabaseCursor> GetNewCursor() = 0;
121  virtual std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) = 0;
122  virtual bool TxnBegin() = 0;
123  virtual bool TxnCommit() = 0;
124  virtual bool TxnAbort() = 0;
125 };
126 
130 {
131 public:
134  virtual ~WalletDatabase() = default;
135 
137  virtual void Open() = 0;
138 
140  std::atomic<int> m_refcount{0};
142  virtual void AddRef() = 0;
144  virtual void RemoveRef() = 0;
145 
148  virtual bool Rewrite(const char* pszSkip=nullptr) = 0;
149 
152  virtual bool Backup(const std::string& strDest) const = 0;
153 
156  virtual void Flush() = 0;
160  virtual void Close() = 0;
161  /* flush the wallet passively (TRY_LOCK)
162  ideal to be called periodically */
163  virtual bool PeriodicFlush() = 0;
164 
165  virtual void IncrementUpdateCounter() = 0;
166 
167  virtual void ReloadDbEnv() = 0;
168 
170  virtual std::string Filename() = 0;
171 
172  virtual std::string Format() = 0;
173 
174  std::atomic<unsigned int> nUpdateCounter;
175  unsigned int nLastSeen{0};
176  unsigned int nLastFlushed{0};
177  int64_t nLastWalletUpdate{0};
178 
180  virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
181 };
182 
183 enum class DatabaseFormat {
184  BERKELEY,
185  SQLITE,
186  BERKELEY_RO,
188 };
189 
191  bool require_existing = false;
192  bool require_create = false;
193  std::optional<DatabaseFormat> require_format;
194  uint64_t create_flags = 0;
196 
197  // Specialized options. Not every option is supported by every backend.
198  bool verify = true;
199  bool use_unsafe_sync = false;
200  bool use_shared_memory = false;
201  int64_t max_log_mb = 100;
202 };
203 
204 enum class DatabaseStatus {
205  SUCCESS,
212  FAILED_LOAD,
216 };
217 
219 std::vector<fs::path> ListDatabases(const fs::path& path);
220 
221 void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options);
222 std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
223 
224 fs::path BDBDataFile(const fs::path& path);
225 fs::path SQLiteDataFile(const fs::path& path);
226 bool IsBDBFile(const fs::path& path);
227 bool IsSQLiteFile(const fs::path& path);
228 } // namespace wallet
229 
230 #endif // BITCOIN_WALLET_DB_H
ArgsManager & args
Definition: bitcoind.cpp:270
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
void reserve(size_type n)
Definition: streams.h:184
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 WalletDatabase.
Definition: db.h:51
bool Erase(const K &key)
Definition: db.h:100
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:86
virtual bool TxnAbort()=0
DatabaseBatch(const DatabaseBatch &)=delete
virtual void Flush()=0
virtual ~DatabaseBatch()=default
virtual void Close()=0
bool Read(const K &key, T &value)
Definition: db.h:69
virtual bool EraseKey(DataStream &&key)=0
virtual bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true)=0
virtual bool TxnBegin()=0
virtual std::unique_ptr< DatabaseCursor > GetNewCursor()=0
DatabaseBatch & operator=(const DatabaseBatch &)=delete
virtual bool ReadKey(DataStream &&key, DataStream &value)=0
bool Exists(const K &key)
Definition: db.h:110
virtual bool ErasePrefix(Span< const std::byte > prefix)=0
virtual std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix)=0
virtual bool HasKey(DataStream &&key)=0
virtual bool TxnCommit()=0
virtual ~DatabaseCursor()=default
DatabaseCursor & operator=(const DatabaseCursor &)=delete
DatabaseCursor(const DatabaseCursor &)=delete
virtual Status Next(DataStream &key, DataStream &value)
Definition: db.h:46
An instance of this class represents one database.
Definition: db.h:130
virtual bool PeriodicFlush()=0
virtual void AddRef()=0
Indicate the a new database user has began using the database.
virtual std::string Format()=0
virtual ~WalletDatabase()=default
virtual void IncrementUpdateCounter()=0
virtual void Open()=0
Open the database if it is not already opened.
int64_t nLastWalletUpdate
Definition: db.h:177
virtual bool Rewrite(const char *pszSkip=nullptr)=0
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
virtual void ReloadDbEnv()=0
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
virtual void Close()=0
Flush to the database file and close the database.
virtual bool Backup(const std::string &strDest) const =0
Back up the entire database to a file.
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:174
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:140
virtual void RemoveRef()=0
Indicate that database user has stopped using the database and that it could be flushed or closed.
unsigned int nLastFlushed
Definition: db.h:176
WalletDatabase()
Create dummy DB handle.
Definition: db.h:133
virtual std::string Filename()=0
Return path to main database file for logs and error messages.
unsigned int nLastSeen
Definition: db.h:175
virtual void Flush()=0
Make sure all changes are flushed to database file.
void ReadDatabaseArgs(const ArgsManager &args, DatabaseOptions &options)
Definition: db.cpp:145
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1349
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:81
std::vector< fs::path > ListDatabases(const fs::path &wallet_dir)
Recursively list database paths in directory.
Definition: db.cpp:22
DatabaseFormat
Definition: db.h:183
bool IsBDBFile(const fs::path &path)
Definition: db.cpp:86
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:67
bool operator<(BytePrefix a, Span< const std::byte > b)
Definition: db.cpp:19
bool IsSQLiteFile(const fs::path &path)
Definition: db.cpp:111
DatabaseStatus
Definition: db.h:204
const char * prefix
Definition: rest.cpp:1007
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:58
Bilingual messages:
Definition: translation.h:18
Span< const std::byte > prefix
Definition: db.h:25
bool require_existing
Definition: db.h:191
bool verify
Check data integrity on load.
Definition: db.h:198
bool use_shared_memory
Let other processes access the database.
Definition: db.h:200
SecureString create_passphrase
Definition: db.h:195
std::optional< DatabaseFormat > require_format
Definition: db.h:193
bool use_unsafe_sync
Disable file sync for faster performance.
Definition: db.h:199
int64_t max_log_mb
Max log size to allow before consolidating.
Definition: db.h:201
uint64_t create_flags
Definition: db.h:194