Bitcoin Core 32.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 <memory>
15#include <optional>
16#include <string>
17
18class ArgsManager;
19struct bilingual_str;
20
21namespace wallet {
22// BytePrefix compares equality with other byte spans that begin with the same prefix.
23struct BytePrefix {
24 std::span<const std::byte> prefix;
25};
26bool operator<(BytePrefix a, std::span<const std::byte> b);
27bool operator<(std::span<const std::byte> a, BytePrefix b);
28
30{
31public:
32 explicit DatabaseCursor() = default;
33 virtual ~DatabaseCursor() = default;
34
37
38 enum class Status
39 {
40 FAIL,
41 MORE,
42 DONE,
43 };
44
45 virtual Status Next(DataStream& key, DataStream& value) { return Status::FAIL; }
46};
47
50{
51private:
52 virtual bool ReadKey(DataStream&& key, DataStream& value) = 0;
53 virtual bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) = 0;
54 virtual bool EraseKey(DataStream&& key) = 0;
55 virtual bool HasKey(DataStream&& key) = 0;
56
57public:
58 explicit DatabaseBatch() = default;
59 virtual ~DatabaseBatch() = default;
60
61 DatabaseBatch(const DatabaseBatch&) = delete;
63
64 virtual void Close() = 0;
65
66 template <typename K, typename T>
67 bool Read(const K& key, T& value)
68 {
69 DataStream ssKey{};
70 ssKey.reserve(1000);
71 ssKey << key;
72
73 DataStream ssValue{};
74 if (!ReadKey(std::move(ssKey), ssValue)) return false;
75 try {
76 ssValue >> value;
77 return true;
78 } catch (const std::exception&) {
79 return false;
80 }
81 }
82
83 template <typename K, typename T>
84 bool Write(const K& key, const T& value, bool fOverwrite = true)
85 {
86 DataStream ssKey{};
87 ssKey.reserve(1000);
88 ssKey << key;
89
90 DataStream ssValue{};
91 ssValue.reserve(10000);
92 ssValue << value;
93
94 return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
95 }
96
97 template <typename K>
98 bool Erase(const K& key)
99 {
100 DataStream ssKey{};
101 ssKey.reserve(1000);
102 ssKey << key;
103
104 return EraseKey(std::move(ssKey));
105 }
106
107 template <typename K>
108 bool Exists(const K& key)
109 {
110 DataStream ssKey{};
111 ssKey.reserve(1000);
112 ssKey << key;
113
114 return HasKey(std::move(ssKey));
115 }
116 virtual bool ErasePrefix(std::span<const std::byte> prefix) = 0;
117
118 virtual std::unique_ptr<DatabaseCursor> GetNewCursor() = 0;
119 virtual std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) = 0;
120 virtual bool TxnBegin() = 0;
121 virtual bool TxnCommit() = 0;
122 virtual bool TxnAbort() = 0;
123 virtual bool HasActiveTxn() = 0;
124};
125
129{
130public:
132 WalletDatabase() = default;
133 virtual ~WalletDatabase() = default;
134
136 virtual void Open() = 0;
137
140 virtual bool Rewrite() = 0;
141
144 virtual bool Backup(const std::string& strDest) const = 0;
145
149 virtual void Close() = 0;
150
152 virtual std::string Filename() = 0;
153
155 virtual std::vector<fs::path> Files() = 0;
156
157 virtual std::string Format() = 0;
158
160 virtual std::unique_ptr<DatabaseBatch> MakeBatch() = 0;
161};
162
163enum class DatabaseFormat {
164 SQLITE,
166};
167
169 bool require_existing = false;
170 bool require_create = false;
171 std::optional<DatabaseFormat> require_format;
172 uint64_t create_flags = 0;
174
175 // Specialized options. Not every option is supported by every backend.
176 bool verify = true;
177 bool use_unsafe_sync = false;
178};
179
180enum class DatabaseStatus {
181 SUCCESS,
194};
195
197std::vector<std::pair<fs::path, std::string>> ListDatabases(const fs::path& path);
198
199void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options);
200std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
201
202fs::path BDBDataFile(const fs::path& path);
203fs::path SQLiteDataFile(const fs::path& path);
204bool IsBDBFile(const fs::path& path);
205bool IsSQLiteFile(const fs::path& path);
206} // namespace wallet
207
208#endif // BITCOIN_WALLET_DB_H
ArgsManager & args
Definition: bitcoind.cpp:280
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:165
void reserve(size_type n)
Definition: streams.h:201
RAII class that provides access to a WalletDatabase.
Definition: db.h:50
bool Erase(const K &key)
Definition: db.h:98
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:84
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:67
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:108
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:45
An instance of this class represents one database.
Definition: db.h:129
virtual std::vector< fs::path > Files()=0
Return paths to all database created files.
virtual std::string Format()=0
virtual ~WalletDatabase()=default
virtual void Open()=0
Open the database if it is not already opened.
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.
WalletDatabase()=default
Create dummy DB handle.
virtual bool Rewrite()=0
Rewrite the entire database on disk.
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:153
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1329
bool operator<(BytePrefix a, std::span< const std::byte > b)
Definition: db.cpp:20
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:89
DatabaseFormat
Definition: db.h:163
bool IsBDBFile(const fs::path &path)
Definition: db.cpp:94
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:75
bool IsSQLiteFile(const fs::path &path)
Definition: db.cpp:119
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:180
const char * prefix
Definition: rest.cpp:1195
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:53
Bilingual messages:
Definition: translation.h:24
std::span< const std::byte > prefix
Definition: db.h:24
bool require_existing
Definition: db.h:169
bool verify
Check data integrity on load.
Definition: db.h:176
SecureString create_passphrase
Definition: db.h:173
std::optional< DatabaseFormat > require_format
Definition: db.h:171
bool use_unsafe_sync
Disable file sync for faster performance.
Definition: db.h:177
uint64_t create_flags
Definition: db.h:172