Bitcoin Core 22.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-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_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 <optional>
17#include <string>
18
19struct bilingual_str;
20
21void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
22
25{
26private:
27 virtual bool ReadKey(CDataStream&& key, CDataStream& value) = 0;
28 virtual bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) = 0;
29 virtual bool EraseKey(CDataStream&& key) = 0;
30 virtual bool HasKey(CDataStream&& key) = 0;
31
32public:
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>
43 bool Read(const K& key, T& value)
44 {
46 ssKey.reserve(1000);
47 ssKey << key;
48
50 if (!ReadKey(std::move(ssKey), ssValue)) return false;
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)
61 {
63 ssKey.reserve(1000);
64 ssKey << key;
65
67 ssValue.reserve(10000);
68 ssValue << value;
69
70 return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
71 }
72
73 template <typename K>
74 bool Erase(const K& key)
75 {
77 ssKey.reserve(1000);
78 ssKey << key;
79
80 return EraseKey(std::move(ssKey));
81 }
82
83 template <typename K>
84 bool Exists(const K& key)
85 {
87 ssKey.reserve(1000);
88 ssKey << key;
89
90 return HasKey(std::move(ssKey));
91 }
92
93 virtual bool StartCursor() = 0;
94 virtual bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) = 0;
95 virtual void CloseCursor() = 0;
96 virtual bool TxnBegin() = 0;
97 virtual bool TxnCommit() = 0;
98 virtual bool TxnAbort() = 0;
99};
100
104{
105public:
108 virtual ~WalletDatabase() {};
109
111 virtual void Open() = 0;
112
114 std::atomic<int> m_refcount{0};
116 virtual void AddRef() = 0;
118 virtual void RemoveRef() = 0;
119
122 virtual bool Rewrite(const char* pszSkip=nullptr) = 0;
123
126 virtual bool Backup(const std::string& strDest) const = 0;
127
130 virtual void Flush() = 0;
134 virtual void Close() = 0;
135 /* flush the wallet passively (TRY_LOCK)
136 ideal to be called periodically */
137 virtual bool PeriodicFlush() = 0;
138
139 virtual void IncrementUpdateCounter() = 0;
140
141 virtual void ReloadDbEnv() = 0;
142
144 virtual std::string Filename() = 0;
145
146 virtual std::string Format() = 0;
147
148 std::atomic<unsigned int> nUpdateCounter;
149 unsigned int nLastSeen;
150 unsigned int nLastFlushed;
152
154 virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
155};
156
159{
160private:
161 bool ReadKey(CDataStream&& key, CDataStream& value) override { return true; }
162 bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) override { return true; }
163 bool EraseKey(CDataStream&& key) override { return true; }
164 bool HasKey(CDataStream&& key) override { return true; }
165
166public:
167 void Flush() override {}
168 void Close() override {}
169
170 bool StartCursor() override { return true; }
171 bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override { return true; }
172 void CloseCursor() override {}
173 bool TxnBegin() override { return true; }
174 bool TxnCommit() override { return true; }
175 bool TxnAbort() override { return true; }
176};
177
181{
182public:
183 void Open() override {};
184 void AddRef() override {}
185 void RemoveRef() override {}
186 bool Rewrite(const char* pszSkip=nullptr) override { return true; }
187 bool Backup(const std::string& strDest) const override { return true; }
188 void Close() override {}
189 void Flush() override {}
190 bool PeriodicFlush() override { return true; }
192 void ReloadDbEnv() override {}
193 std::string Filename() override { return "dummy"; }
194 std::string Format() override { return "dummy"; }
195 std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<DummyBatch>(); }
196};
197
198enum class DatabaseFormat {
199 BERKELEY,
200 SQLITE,
201};
202
204 bool require_existing = false;
205 bool require_create = false;
206 std::optional<DatabaseFormat> require_format;
207 uint64_t create_flags = 0;
209 bool verify = true;
210};
211
212enum class DatabaseStatus {
213 SUCCESS,
223};
224
226std::vector<fs::path> ListDatabases(const fs::path& path);
227
228std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
229
230fs::path BDBDataFile(const fs::path& path);
231fs::path SQLiteDataFile(const fs::path& path);
232bool IsBDBFile(const fs::path& path);
233bool IsSQLiteFile(const fs::path& path);
234
235#endif // BITCOIN_WALLET_DB_H
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
void reserve(size_type n)
Definition: streams.h:258
RAII class that provides access to a WalletDatabase.
Definition: db.h:25
DatabaseBatch & operator=(const DatabaseBatch &)=delete
bool Erase(const K &key)
Definition: db.h:74
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
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:43
virtual void Flush()=0
bool Exists(const K &key)
Definition: db.h:84
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:159
bool StartCursor() override
Definition: db.h:170
void Flush() override
Definition: db.h:167
bool TxnAbort() override
Definition: db.h:175
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: db.h:161
bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override
Definition: db.h:171
void Close() override
Definition: db.h:168
bool TxnCommit() override
Definition: db.h:174
bool EraseKey(CDataStream &&key) override
Definition: db.h:163
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: db.h:162
bool TxnBegin() override
Definition: db.h:173
void CloseCursor() override
Definition: db.h:172
bool HasKey(CDataStream &&key) override
Definition: db.h:164
A dummy WalletDatabase that does nothing and never fails.
Definition: db.h:181
std::string Format() override
Definition: db.h:194
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: db.h:187
void Open() override
Open the database if it is not already opened.
Definition: db.h:183
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: db.h:185
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: db.h:193
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: db.h:195
void IncrementUpdateCounter() override
Definition: db.h:191
void AddRef() override
Indicate the a new database user has began using the database.
Definition: db.h:184
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:186
bool PeriodicFlush() override
Definition: db.h:190
void ReloadDbEnv() override
Definition: db.h:192
void Close() override
Flush to the database file and close the database.
Definition: db.h:188
void Flush() override
Make sure all changes are flushed to database file.
Definition: db.h:189
An instance of this class represents one database.
Definition: db.h:104
unsigned int nLastFlushed
Definition: db.h:150
unsigned int nLastSeen
Definition: db.h:149
virtual void Open()=0
Open the database if it is not already opened.
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:148
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:107
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:108
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
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:114
virtual void AddRef()=0
Indicate the a new database user has began using the database.
virtual std::string Format()=0
virtual std::string Filename()=0
Return path to main database file for logs and error messages.
int64_t nLastWalletUpdate
Definition: db.h:151
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 prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1103
fs::path BDBDataFile(const fs::path &path)
Definition: db.cpp:58
DatabaseStatus
Definition: db.h:212
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:72
void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, std::string &database_filename)
std::vector< fs::path > ListDatabases(const fs::path &path)
Recursively list database paths in directory.
Definition: db.cpp:13
DatabaseFormat
Definition: db.h:198
bool IsBDBFile(const fs::path &path)
Definition: db.cpp:77
bool IsSQLiteFile(const fs::path &path)
Definition: db.cpp:102
#define T(expected, seed, data)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:59
@ SER_DISK
Definition: serialize.h:139
bool verify
Definition: db.h:209
bool require_create
Definition: db.h:205
std::optional< DatabaseFormat > require_format
Definition: db.h:206
uint64_t create_flags
Definition: db.h:207
bool require_existing
Definition: db.h:204
SecureString create_passphrase
Definition: db.h:208
Bilingual messages:
Definition: translation.h:16
bool error(const char *fmt, const Args &... args)
Definition: system.h:49