Bitcoin Core 29.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-present 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
19class ArgsManager;
20struct bilingual_str;
21
22namespace wallet {
23// BytePrefix compares equality with other byte spans that begin with the same prefix.
24struct BytePrefix {
25 std::span<const std::byte> prefix;
26};
27bool operator<(BytePrefix a, std::span<const std::byte> b);
28bool operator<(std::span<const std::byte> a, BytePrefix b);
29
31{
32public:
33 explicit DatabaseCursor() = default;
34 virtual ~DatabaseCursor() = default;
35
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{
52private:
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
58public:
59 explicit DatabaseBatch() = default;
60 virtual ~DatabaseBatch() = default;
61
62 DatabaseBatch(const DatabaseBatch&) = delete;
64
65 virtual void Close() = 0;
66
67 template <typename K, typename T>
68 bool Read(const K& key, T& value)
69 {
70 DataStream ssKey{};
71 ssKey.reserve(1000);
72 ssKey << key;
73
74 DataStream ssValue{};
75 if (!ReadKey(std::move(ssKey), ssValue)) return false;
76 try {
77 ssValue >> value;
78 return true;
79 } catch (const std::exception&) {
80 return false;
81 }
82 }
83
84 template <typename K, typename T>
85 bool Write(const K& key, const T& value, bool fOverwrite = true)
86 {
87 DataStream ssKey{};
88 ssKey.reserve(1000);
89 ssKey << key;
90
91 DataStream ssValue{};
92 ssValue.reserve(10000);
93 ssValue << value;
94
95 return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
96 }
97
98 template <typename K>
99 bool Erase(const K& key)
100 {
101 DataStream ssKey{};
102 ssKey.reserve(1000);
103 ssKey << key;
104
105 return EraseKey(std::move(ssKey));
106 }
107
108 template <typename K>
109 bool Exists(const K& key)
110 {
111 DataStream ssKey{};
112 ssKey.reserve(1000);
113 ssKey << key;
114
115 return HasKey(std::move(ssKey));
116 }
117 virtual bool ErasePrefix(std::span<const std::byte> prefix) = 0;
118
119 virtual std::unique_ptr<DatabaseCursor> GetNewCursor() = 0;
120 virtual std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) = 0;
121 virtual bool TxnBegin() = 0;
122 virtual bool TxnCommit() = 0;
123 virtual bool TxnAbort() = 0;
124 virtual bool HasActiveTxn() = 0;
125};
126
130{
131public:
133 WalletDatabase() = default;
134 virtual ~WalletDatabase() = default;
135
137 virtual void Open() = 0;
138
140 std::atomic<int> m_refcount{0};
141
144 virtual bool Rewrite(const char* pszSkip=nullptr) = 0;
145
148 virtual bool Backup(const std::string& strDest) const = 0;
149
153 virtual void Close() = 0;
154
156 virtual std::string Filename() = 0;
157
158 virtual std::string Format() = 0;
159
161 virtual std::unique_ptr<DatabaseBatch> MakeBatch() = 0;
162};
163
164enum class DatabaseFormat {
165 SQLITE,
167};
168
170 bool require_existing = false;
171 bool require_create = false;
172 std::optional<DatabaseFormat> require_format;
173 uint64_t create_flags = 0;
175
176 // Specialized options. Not every option is supported by every backend.
177 bool verify = true;
178 bool use_unsafe_sync = false;
179 bool use_shared_memory = false;
180 int64_t max_log_mb = 100;
181};
182
183enum class DatabaseStatus {
184 SUCCESS,
195};
196
198std::vector<std::pair<fs::path, std::string>> ListDatabases(const fs::path& path);
199
200void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options);
201std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
202
203fs::path BDBDataFile(const fs::path& path);
204fs::path SQLiteDataFile(const fs::path& path);
205bool IsBDBFile(const fs::path& path);
206bool IsSQLiteFile(const fs::path& path);
207} // namespace wallet
208
209#endif // BITCOIN_WALLET_DB_H
ArgsManager & args
Definition: bitcoind.cpp:277
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
void reserve(size_type n)
Definition: streams.h:184
RAII class that provides access to a WalletDatabase.
Definition: db.h:51
bool Erase(const K &key)
Definition: db.h:99
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:85
virtual bool TxnAbort()=0
DatabaseBatch(const DatabaseBatch &)=delete
virtual ~DatabaseBatch()=default
virtual void Close()=0
bool Read(const K &key, T &value)
Definition: db.h:68
virtual bool EraseKey(DataStream &&key)=0
virtual bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true)=0
virtual bool TxnBegin()=0
DatabaseBatch & operator=(const DatabaseBatch &)=delete
virtual std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(std::span< const std::byte > prefix)=0
virtual bool ReadKey(DataStream &&key, DataStream &value)=0
bool Exists(const K &key)
Definition: db.h:109
virtual std::unique_ptr< DatabaseCursor > GetNewCursor()=0
virtual bool HasKey(DataStream &&key)=0
virtual bool HasActiveTxn()=0
virtual bool TxnCommit()=0
virtual bool ErasePrefix(std::span< const std::byte > prefix)=0
DatabaseCursor & operator=(const DatabaseCursor &)=delete
virtual ~DatabaseCursor()=default
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 std::string Format()=0
virtual ~WalletDatabase()=default
virtual void Open()=0
Open the database if it is not already opened.
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 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.
virtual std::unique_ptr< DatabaseBatch > MakeBatch()=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:140
WalletDatabase()=default
Create dummy DB handle.
virtual std::string Filename()=0
Return path to main database file for logs and error messages.
@ FAIL
Just act as if the signature was invalid.
void ReadDatabaseArgs(const ArgsManager &args, DatabaseOptions &options)
Definition: db.cpp:154
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1342
bool operator<(BytePrefix a, std::span< const std::byte > b)
Definition: db.cpp:20
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:90
DatabaseFormat
Definition: db.h:164
bool IsBDBFile(const fs::path &path)
Definition: db.cpp:95
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:76
bool IsSQLiteFile(const fs::path &path)
Definition: db.cpp:120
std::vector< std::pair< fs::path, std::string > > ListDatabases(const fs::path &wallet_dir)
Recursively list database paths in directory.
Definition: db.cpp:23
DatabaseStatus
Definition: db.h:183
const char * prefix
Definition: rest.cpp:1009
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:58
Bilingual messages:
Definition: translation.h:24
std::span< const std::byte > prefix
Definition: db.h:25
bool require_existing
Definition: db.h:170
bool verify
Check data integrity on load.
Definition: db.h:177
bool use_shared_memory
Let other processes access the database.
Definition: db.h:179
SecureString create_passphrase
Definition: db.h:174
std::optional< DatabaseFormat > require_format
Definition: db.h:172
bool use_unsafe_sync
Disable file sync for faster performance.
Definition: db.h:178
int64_t max_log_mb
Max log size to allow before consolidating.
Definition: db.h:180
uint64_t create_flags
Definition: db.h:173