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 
25 {
26 public:
27  explicit DatabaseCursor() {}
28  virtual ~DatabaseCursor() {}
29 
30  DatabaseCursor(const DatabaseCursor&) = delete;
32 
33  enum class Status
34  {
35  FAIL,
36  MORE,
37  DONE,
38  };
39 
40  virtual Status Next(DataStream& key, DataStream& value) { return Status::FAIL; }
41 };
42 
45 {
46 private:
47  virtual bool ReadKey(DataStream&& key, DataStream& value) = 0;
48  virtual bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) = 0;
49  virtual bool EraseKey(DataStream&& key) = 0;
50  virtual bool HasKey(DataStream&& key) = 0;
51 
52 public:
53  explicit DatabaseBatch() {}
54  virtual ~DatabaseBatch() {}
55 
56  DatabaseBatch(const DatabaseBatch&) = delete;
58 
59  virtual void Flush() = 0;
60  virtual void Close() = 0;
61 
62  template <typename K, typename T>
63  bool Read(const K& key, T& value)
64  {
65  DataStream ssKey{};
66  ssKey.reserve(1000);
67  ssKey << key;
68 
69  DataStream ssValue{};
70  if (!ReadKey(std::move(ssKey), ssValue)) return false;
71  try {
72  ssValue >> value;
73  return true;
74  } catch (const std::exception&) {
75  return false;
76  }
77  }
78 
79  template <typename K, typename T>
80  bool Write(const K& key, const T& value, bool fOverwrite = true)
81  {
82  DataStream ssKey{};
83  ssKey.reserve(1000);
84  ssKey << key;
85 
86  DataStream ssValue{};
87  ssValue.reserve(10000);
88  ssValue << value;
89 
90  return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
91  }
92 
93  template <typename K>
94  bool Erase(const K& key)
95  {
96  DataStream ssKey{};
97  ssKey.reserve(1000);
98  ssKey << key;
99 
100  return EraseKey(std::move(ssKey));
101  }
102 
103  template <typename K>
104  bool Exists(const K& key)
105  {
106  DataStream ssKey{};
107  ssKey.reserve(1000);
108  ssKey << key;
109 
110  return HasKey(std::move(ssKey));
111  }
113 
114  virtual std::unique_ptr<DatabaseCursor> GetNewCursor() = 0;
115  virtual std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) = 0;
116  virtual bool TxnBegin() = 0;
117  virtual bool TxnCommit() = 0;
118  virtual bool TxnAbort() = 0;
119 };
120 
124 {
125 public:
128  virtual ~WalletDatabase() {};
129 
131  virtual void Open() = 0;
132 
134  std::atomic<int> m_refcount{0};
136  virtual void AddRef() = 0;
138  virtual void RemoveRef() = 0;
139 
142  virtual bool Rewrite(const char* pszSkip=nullptr) = 0;
143 
146  virtual bool Backup(const std::string& strDest) const = 0;
147 
150  virtual void Flush() = 0;
154  virtual void Close() = 0;
155  /* flush the wallet passively (TRY_LOCK)
156  ideal to be called periodically */
157  virtual bool PeriodicFlush() = 0;
158 
159  virtual void IncrementUpdateCounter() = 0;
160 
161  virtual void ReloadDbEnv() = 0;
162 
164  virtual std::string Filename() = 0;
165 
166  virtual std::string Format() = 0;
167 
168  std::atomic<unsigned int> nUpdateCounter;
169  unsigned int nLastSeen{0};
170  unsigned int nLastFlushed{0};
171  int64_t nLastWalletUpdate{0};
172 
174  virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
175 };
176 
177 enum class DatabaseFormat {
178  BERKELEY,
179  SQLITE,
180 };
181 
183  bool require_existing = false;
184  bool require_create = false;
185  std::optional<DatabaseFormat> require_format;
186  uint64_t create_flags = 0;
188 
189  // Specialized options. Not every option is supported by every backend.
190  bool verify = true;
191  bool use_unsafe_sync = false;
192  bool use_shared_memory = false;
193  int64_t max_log_mb = 100;
194 };
195 
196 enum class DatabaseStatus {
197  SUCCESS,
204  FAILED_LOAD,
208 };
209 
211 std::vector<fs::path> ListDatabases(const fs::path& path);
212 
213 void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options);
214 std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
215 
216 fs::path BDBDataFile(const fs::path& path);
217 fs::path SQLiteDataFile(const fs::path& path);
218 bool IsBDBFile(const fs::path& path);
219 bool IsSQLiteFile(const fs::path& path);
220 } // namespace wallet
221 
222 #endif // BITCOIN_WALLET_DB_H
ArgsManager & args
Definition: bitcoind.cpp:268
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:45
bool Erase(const K &key)
Definition: db.h:94
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:80
virtual bool TxnAbort()=0
DatabaseBatch(const DatabaseBatch &)=delete
virtual void Flush()=0
virtual void Close()=0
bool Read(const K &key, T &value)
Definition: db.h:63
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:104
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 ~DatabaseBatch()
Definition: db.h:54
DatabaseCursor & operator=(const DatabaseCursor &)=delete
virtual ~DatabaseCursor()
Definition: db.h:28
DatabaseCursor(const DatabaseCursor &)=delete
virtual Status Next(DataStream &key, DataStream &value)
Definition: db.h:40
An instance of this class represents one database.
Definition: db.h:124
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 void IncrementUpdateCounter()=0
virtual void Open()=0
Open the database if it is not already opened.
int64_t nLastWalletUpdate
Definition: db.h:171
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:168
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:134
virtual void RemoveRef()=0
Indicate that database user has stopped using the database and that it could be flushed or closed.
virtual ~WalletDatabase()
Definition: db.h:128
unsigned int nLastFlushed
Definition: db.h:170
WalletDatabase()
Create dummy DB handle.
Definition: db.h:127
virtual std::string Filename()=0
Return path to main database file for logs and error messages.
unsigned int nLastSeen
Definition: db.h:169
virtual void Flush()=0
Make sure all changes are flushed to database file.
void ReadDatabaseArgs(const ArgsManager &args, DatabaseOptions &options)
Definition: db.cpp:142
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1350
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:78
std::vector< fs::path > ListDatabases(const fs::path &wallet_dir)
Recursively list database paths in directory.
Definition: db.cpp:19
DatabaseFormat
Definition: db.h:177
bool IsBDBFile(const fs::path &path)
Definition: db.cpp:83
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:64
bool IsSQLiteFile(const fs::path &path)
Definition: db.cpp:108
DatabaseStatus
Definition: db.h:196
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
bool require_existing
Definition: db.h:183
bool verify
Check data integrity on load.
Definition: db.h:190
bool use_shared_memory
Let other processes access the database.
Definition: db.h:192
SecureString create_passphrase
Definition: db.h:187
std::optional< DatabaseFormat > require_format
Definition: db.h:185
bool use_unsafe_sync
Disable file sync for faster performance.
Definition: db.h:191
int64_t max_log_mb
Max log size to allow before consolidating.
Definition: db.h:193
uint64_t create_flags
Definition: db.h:186