Bitcoin ABC 0.32.4
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() {}
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) {
43 DataStream ssKey{};
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) {
61 DataStream ssKey{};
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) {
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:
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
167class DummyBatch : public DatabaseBatch {
168private:
169 bool ReadKey(DataStream &&key, DataStream &value) override { return true; }
170 bool WriteKey(DataStream &&key, DataStream &&value,
171 bool overwrite = true) override {
172 return true;
173 }
174 bool EraseKey(DataStream &&key) override { return true; }
175 bool HasKey(DataStream &&key) override { return true; }
176
177public:
178 void Flush() override {}
179 void Close() override {}
180
181 bool StartCursor() override { return true; }
182 bool ReadAtCursor(DataStream &ssKey, DataStream &ssValue,
183 bool &complete) override {
184 return true;
185 }
186 void CloseCursor() override {}
187 bool TxnBegin() override { return true; }
188 bool TxnCommit() override { return true; }
189 bool TxnAbort() override { return true; }
190};
191
197public:
198 void Open() override{};
199 void AddRef() override {}
200 void RemoveRef() override {}
201 bool Rewrite(const char *pszSkip = nullptr) override { return true; }
202 bool Backup(const std::string &strDest) const override { return true; }
203 void Close() override {}
204 void Flush() override {}
205 bool PeriodicFlush() override { return true; }
207 void ReloadDbEnv() override {}
208 std::string Filename() override { return "dummy"; }
209 std::unique_ptr<DatabaseBatch>
210 MakeBatch(bool flush_on_close = true) override {
211 return std::make_unique<DummyBatch>();
212 }
213};
214
215enum class DatabaseFormat {
216 BERKELEY,
217};
218
220 bool require_existing = false;
221 bool require_create = false;
222 uint64_t create_flags = 0;
224 bool verify = true;
225};
226
227enum class DatabaseStatus {
228 SUCCESS,
239};
240
241std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path &path,
242 const DatabaseOptions &options,
243 DatabaseStatus &status,
244 bilingual_str &error);
245
246#endif // BITCOIN_WALLET_DB_H
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:173
void reserve(size_type n)
Definition: streams.h:211
RAII class that provides access to a WalletDatabase.
Definition: db.h:24
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
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
virtual ~DatabaseBatch()
Definition: db.h:34
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
DatabaseBatch()
Definition: db.h:33
virtual void Close()=0
virtual bool StartCursor()=0
virtual bool TxnAbort()=0
RAII class that provides access to a DummyDatabase.
Definition: db.h:167
bool StartCursor() override
Definition: db.h:181
void Flush() override
Definition: db.h:178
bool TxnAbort() override
Definition: db.h:189
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: db.h:170
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: db.h:169
bool HasKey(DataStream &&key) override
Definition: db.h:175
bool ReadAtCursor(DataStream &ssKey, DataStream &ssValue, bool &complete) override
Definition: db.h:182
void Close() override
Definition: db.h:179
bool TxnCommit() override
Definition: db.h:188
bool EraseKey(DataStream &&key) override
Definition: db.h:174
bool TxnBegin() override
Definition: db.h:187
void CloseCursor() override
Definition: db.h:186
A dummy WalletDatabase that does nothing and never fails.
Definition: db.h:196
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: db.h:202
void Open() override
Open the database if it is not already opened.
Definition: db.h:198
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: db.h:200
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: db.h:208
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: db.h:210
void IncrementUpdateCounter() override
Definition: db.h:206
void AddRef() override
Indicate the a new database user has began using the database.
Definition: db.h:199
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:201
bool PeriodicFlush() override
Definition: db.h:205
void ReloadDbEnv() override
Definition: db.h:207
void Close() override
Flush to the database file and close the database.
Definition: db.h:203
void Flush() override
Make sure all changes are flushed to database file.
Definition: db.h:204
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
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: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 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:155
bool verify
Definition: db.h:224
bool require_create
Definition: db.h:221
uint64_t create_flags
Definition: db.h:222
bool require_existing
Definition: db.h:220
SecureString create_passphrase
Definition: db.h:223
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:227
@ 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:215