Bitcoin ABC 0.33.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 <streams.h>
12#include <util/fs.h>
13
14#include <atomic>
15#include <memory>
16#include <string>
17
18struct bilingual_str;
19
20void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory,
21 std::string &database_filename);
22
25private:
26 virtual bool ReadKey(DataStream &&key, DataStream &value) = 0;
27 virtual bool WriteKey(DataStream &&key, DataStream &&value,
28 bool overwrite = true) = 0;
29 virtual bool EraseKey(DataStream &&key) = 0;
30 virtual bool HasKey(DataStream &&key) = 0;
31
32public:
33 explicit DatabaseBatch() = default;
34 virtual ~DatabaseBatch() = default;
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) {
43 DataStream ssKey{};
44 ssKey.reserve(1000);
45 ssKey << key;
46
47 DataStream ssValue{};
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) {
61 DataStream ssKey{};
62 ssKey.reserve(1000);
63 ssKey << key;
64
65 DataStream ssValue{};
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) {
73 DataStream ssKey{};
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) {
81 DataStream ssKey{};
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(DataStream &ssKey, DataStream &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
101public:
104 virtual ~WalletDatabase() = default;
105
107 virtual void Open() = 0;
108
111 std::atomic<int> m_refcount{0};
116 virtual void AddRef() = 0;
121 virtual void RemoveRef() = 0;
122
127 virtual bool Rewrite(const char *pszSkip = nullptr) = 0;
128
132 virtual bool Backup(const std::string &strDest) const = 0;
133
137 virtual void Flush() = 0;
142 virtual void Close() = 0;
143 /* flush the wallet passively (TRY_LOCK)
144 ideal to be called periodically */
145 virtual bool PeriodicFlush() = 0;
146
147 virtual void IncrementUpdateCounter() = 0;
148
149 virtual void ReloadDbEnv() = 0;
150
152 virtual std::string Filename() = 0;
153
154 std::atomic<unsigned int> nUpdateCounter;
155 unsigned int nLastSeen{0};
156 unsigned int nLastFlushed{0};
158
160 virtual std::unique_ptr<DatabaseBatch>
161 MakeBatch(bool flush_on_close = true) = 0;
162};
163
165class DummyBatch : public DatabaseBatch {
166private:
167 bool ReadKey(DataStream &&key, DataStream &value) override { return true; }
168 bool WriteKey(DataStream &&key, DataStream &&value,
169 bool overwrite = true) override {
170 return true;
171 }
172 bool EraseKey(DataStream &&key) override { return true; }
173 bool HasKey(DataStream &&key) override { return true; }
174
175public:
176 void Flush() override {}
177 void Close() override {}
178
179 bool StartCursor() override { return true; }
180 bool ReadAtCursor(DataStream &ssKey, DataStream &ssValue,
181 bool &complete) override {
182 return true;
183 }
184 void CloseCursor() override {}
185 bool TxnBegin() override { return true; }
186 bool TxnCommit() override { return true; }
187 bool TxnAbort() override { return true; }
188};
189
195public:
196 void Open() override {};
197 void AddRef() override {}
198 void RemoveRef() override {}
199 bool Rewrite(const char *pszSkip = nullptr) override { return true; }
200 bool Backup(const std::string &strDest) const override { return true; }
201 void Close() override {}
202 void Flush() override {}
203 bool PeriodicFlush() override { return true; }
205 void ReloadDbEnv() override {}
206 std::string Filename() override { return "dummy"; }
207 std::unique_ptr<DatabaseBatch>
208 MakeBatch(bool flush_on_close = true) override {
209 return std::make_unique<DummyBatch>();
210 }
211};
212
213enum class DatabaseFormat {
214 BERKELEY,
215};
216
218 bool require_existing = false;
219 bool require_create = false;
220 uint64_t create_flags = 0;
222 bool verify = true;
223};
224
225enum class DatabaseStatus {
226 SUCCESS,
237};
238
239std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path &path,
240 const DatabaseOptions &options,
241 DatabaseStatus &status,
242 bilingual_str &error);
243
244#endif // BITCOIN_WALLET_DB_H
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:118
void reserve(size_type n)
Definition: streams.h:156
RAII class that provides access to a WalletDatabase.
Definition: db.h:24
virtual ~DatabaseBatch()=default
virtual bool ReadAtCursor(DataStream &ssKey, DataStream &ssValue, bool &complete)=0
DatabaseBatch & operator=(const DatabaseBatch &)=delete
bool Erase(const K &key)
Definition: db.h:72
DatabaseBatch(const DatabaseBatch &)=delete
virtual bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true)=0
virtual bool TxnCommit()=0
DatabaseBatch()=default
virtual bool TxnBegin()=0
virtual bool HasKey(DataStream &&key)=0
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:60
virtual bool EraseKey(DataStream &&key)=0
virtual void CloseCursor()=0
bool Read(const K &key, T &value)
Definition: db.h:42
virtual void Flush()=0
virtual bool ReadKey(DataStream &&key, DataStream &value)=0
bool Exists(const K &key)
Definition: db.h:80
virtual void Close()=0
virtual bool StartCursor()=0
virtual bool TxnAbort()=0
RAII class that provides access to a DummyDatabase.
Definition: db.h:165
bool StartCursor() override
Definition: db.h:179
void Flush() override
Definition: db.h:176
bool TxnAbort() override
Definition: db.h:187
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: db.h:168
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: db.h:167
bool HasKey(DataStream &&key) override
Definition: db.h:173
bool ReadAtCursor(DataStream &ssKey, DataStream &ssValue, bool &complete) override
Definition: db.h:180
void Close() override
Definition: db.h:177
bool TxnCommit() override
Definition: db.h:186
bool EraseKey(DataStream &&key) override
Definition: db.h:172
bool TxnBegin() override
Definition: db.h:185
void CloseCursor() override
Definition: db.h:184
A dummy WalletDatabase that does nothing and never fails.
Definition: db.h:194
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: db.h:200
void Open() override
Open the database if it is not already opened.
Definition: db.h:196
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: db.h:198
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: db.h:206
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: db.h:208
void IncrementUpdateCounter() override
Definition: db.h:204
void AddRef() override
Indicate the a new database user has began using the database.
Definition: db.h:197
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:199
bool PeriodicFlush() override
Definition: db.h:203
void ReloadDbEnv() override
Definition: db.h:205
void Close() override
Flush to the database file and close the database.
Definition: db.h:201
void Flush() override
Make sure all changes are flushed to database file.
Definition: db.h:202
An instance of this class represents one database.
Definition: db.h:100
unsigned int nLastFlushed
Definition: db.h:156
unsigned int nLastSeen
Definition: db.h:155
virtual void Open()=0
Open the database if it is not already opened.
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:154
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 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:111
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:157
virtual void IncrementUpdateCounter()=0
virtual void Close()=0
Flush to the database file and close the database.
virtual ~WalletDatabase()=default
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
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:55
bool verify
Definition: db.h:222
bool require_create
Definition: db.h:219
uint64_t create_flags
Definition: db.h:220
bool require_existing
Definition: db.h:218
SecureString create_passphrase
Definition: db.h:221
Bilingual messages:
Definition: translation.h:17
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1118
DatabaseStatus
Definition: db.h:225
@ FAILED_INVALID_BACKUP_FILE
void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, std::string &database_filename)
Definition: db.cpp:11
DatabaseFormat
Definition: db.h:213