Bitcoin Core 31.99.0
P2P Digital Currency
dbwrapper.h
Go to the documentation of this file.
1// Copyright (c) 2012-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_DBWRAPPER_H
6#define BITCOIN_DBWRAPPER_H
7
8#include <attributes.h>
9#include <serialize.h>
10#include <span.h>
11#include <streams.h>
12#include <util/byte_units.h>
13#include <util/check.h>
14#include <util/fs.h>
15
16#include <cstddef>
17#include <exception>
18#include <memory>
19#include <optional>
20#include <stdexcept>
21#include <string>
22
23static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
24static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
25static const size_t DBWRAPPER_MAX_FILE_SIZE{32_MiB};
26
28struct DBOptions {
30 bool force_compact = false;
31};
32
34struct DBParams {
36 fs::path path;
40 bool memory_only = false;
42 bool wipe_data = false;
45 bool obfuscate = false;
48};
49
50class dbwrapper_error : public std::runtime_error
51{
52public:
53 explicit dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {}
54};
55
56class CDBWrapper;
57
60namespace dbwrapper_private {
61
67}; // namespace dbwrapper_private
68
69bool DestroyDB(const std::string& path_str);
70
73{
74 friend class CDBWrapper;
75
76private:
78
79 struct WriteBatchImpl;
80 const std::unique_ptr<WriteBatchImpl> m_impl_batch;
81
84
85 void WriteImpl(std::span<const std::byte> key, DataStream& ssValue);
86 void EraseImpl(std::span<const std::byte> key);
87
88public:
92 explicit CDBBatch(const CDBWrapper& _parent);
94 void Clear();
95
96 template <typename K, typename V>
97 void Write(const K& key, const V& value)
98 {
101 ssKey << key;
102 ssValue << value;
104 ssKey.clear();
105 ssValue.clear();
106 }
107
108 template <typename K>
109 void Erase(const K& key)
110 {
112 ssKey << key;
114 ssKey.clear();
115 }
116
117 size_t ApproximateSize() const;
118};
119
121{
122public:
123 struct IteratorImpl;
124
125private:
127 const std::unique_ptr<IteratorImpl> m_impl_iter;
128
129 void SeekImpl(std::span<const std::byte> key);
130 std::span<const std::byte> GetKeyImpl() const;
131 std::span<const std::byte> GetValueImpl() const;
132
133public:
134
139 CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter);
141
142 bool Valid() const;
143
144 void SeekToFirst();
145
146 template<typename K> void Seek(const K& key) {
147 DataStream ssKey{};
149 ssKey << key;
150 SeekImpl(ssKey);
151 }
152
153 void Next();
154
155 template<typename K> bool GetKey(K& key) {
156 try {
157 SpanReader ssKey{GetKeyImpl()};
158 ssKey >> key;
159 } catch (const std::exception&) {
160 return false;
161 }
162 return true;
163 }
164
165 template<typename V> bool GetValue(V& value) {
166 try {
167 DataStream ssValue{GetValueImpl()};
169 ssValue >> value;
170 } catch (const std::exception&) {
171 return false;
172 }
173 return true;
174 }
175};
176
177struct LevelDBContext;
178
180{
182private:
184 std::unique_ptr<LevelDBContext> m_db_context;
185
187 std::string m_name;
188
191
193 inline static const std::string OBFUSCATION_KEY{"\000obfuscate_key", 14}; // explicit size to avoid truncation at leading \0
194
195 std::optional<std::string> ReadImpl(std::span<const std::byte> key) const;
196 bool ExistsImpl(std::span<const std::byte> key) const;
197 size_t EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const;
198 auto& DBContext() const LIFETIMEBOUND { return *Assert(m_db_context); }
199
200public:
201 CDBWrapper(const DBParams& params);
202 ~CDBWrapper();
203
204 CDBWrapper(const CDBWrapper&) = delete;
205 CDBWrapper& operator=(const CDBWrapper&) = delete;
206
207 template <typename K, typename V>
208 bool Read(const K& key, V& value) const
209 {
210 DataStream ssKey{};
212 ssKey << key;
213 std::optional<std::string> strValue{ReadImpl(ssKey)};
214 if (!strValue) {
215 return false;
216 }
217 try {
218 std::span ssValue{MakeWritableByteSpan(*strValue)};
219 m_obfuscation(ssValue);
220 SpanReader{ssValue} >> value;
221 } catch (const std::exception&) {
222 return false;
223 }
224 return true;
225 }
226
227 template <typename K, typename V>
228 void Write(const K& key, const V& value, bool fSync = false)
229 {
230 CDBBatch batch(*this);
231 batch.Write(key, value);
232 WriteBatch(batch, fSync);
233 }
234
235 template <typename K>
236 bool Exists(const K& key) const
237 {
238 DataStream ssKey{};
240 ssKey << key;
241 return ExistsImpl(ssKey);
242 }
243
244 template <typename K>
245 void Erase(const K& key, bool fSync = false)
246 {
247 CDBBatch batch(*this);
248 batch.Erase(key);
249 WriteBatch(batch, fSync);
250 }
251
252 void WriteBatch(CDBBatch& batch, bool fSync = false);
253
254 // Get an estimate of LevelDB memory usage (in bytes).
255 size_t DynamicMemoryUsage() const;
256
258
262 bool IsEmpty();
263
264 template<typename K>
265 size_t EstimateSize(const K& key_begin, const K& key_end) const
266 {
267 DataStream ssKey1{}, ssKey2{};
269 ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
270 ssKey1 << key_begin;
271 ssKey2 << key_end;
272 return EstimateSizeImpl(ssKey1, ssKey2);
273 }
274};
275
276#endif // BITCOIN_DBWRAPPER_H
#define LIFETIMEBOUND
Definition: attributes.h:16
#define Assert(val)
Identity function.
Definition: check.h:116
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:73
void Erase(const K &key)
Definition: dbwrapper.h:109
void WriteImpl(std::span< const std::byte > key, DataStream &ssValue)
Definition: dbwrapper.cpp:176
const std::unique_ptr< WriteBatchImpl > m_impl_batch
Definition: dbwrapper.h:80
DataStream ssKey
Definition: dbwrapper.h:82
void Write(const K &key, const V &value)
Definition: dbwrapper.h:97
void EraseImpl(std::span< const std::byte > key)
Definition: dbwrapper.cpp:184
DataStream ssValue
Definition: dbwrapper.h:83
void Clear()
Definition: dbwrapper.cpp:171
CDBBatch(const CDBWrapper &_parent)
Definition: dbwrapper.cpp:162
size_t ApproximateSize() const
Definition: dbwrapper.cpp:190
const CDBWrapper & parent
Definition: dbwrapper.h:77
CDBIterator(const CDBWrapper &_parent, std::unique_ptr< IteratorImpl > _piter)
Definition: dbwrapper.cpp:358
bool GetValue(V &value)
Definition: dbwrapper.h:165
const std::unique_ptr< IteratorImpl > m_impl_iter
Definition: dbwrapper.h:127
void SeekImpl(std::span< const std::byte > key)
Definition: dbwrapper.cpp:366
std::span< const std::byte > GetKeyImpl() const
Definition: dbwrapper.cpp:372
bool GetKey(K &key)
Definition: dbwrapper.h:155
void Seek(const K &key)
Definition: dbwrapper.h:146
const CDBWrapper & parent
Definition: dbwrapper.h:126
bool Valid() const
Definition: dbwrapper.cpp:385
void SeekToFirst()
Definition: dbwrapper.cpp:386
void Next()
Definition: dbwrapper.cpp:387
std::span< const std::byte > GetValueImpl() const
Definition: dbwrapper.cpp:379
CDBWrapper(const CDBWrapper &)=delete
std::optional< std::string > ReadImpl(std::span< const std::byte > key) const
Definition: dbwrapper.cpp:306
size_t EstimateSizeImpl(std::span< const std::byte > key1, std::span< const std::byte > key2) const
Definition: dbwrapper.cpp:335
size_t DynamicMemoryUsage() const
Definition: dbwrapper.cpp:295
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:208
CDBIterator * NewIterator()
Definition: dbwrapper.cpp:361
std::string m_name
the name of this database
Definition: dbwrapper.h:187
bool Exists(const K &key) const
Definition: dbwrapper.h:236
CDBWrapper(const DBParams &params)
Definition: dbwrapper.cpp:218
void Erase(const K &key, bool fSync=false)
Definition: dbwrapper.h:245
void WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:279
bool ExistsImpl(std::span< const std::byte > key) const
Definition: dbwrapper.cpp:320
void Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:228
Obfuscation m_obfuscation
optional XOR-obfuscation of the database
Definition: dbwrapper.h:190
std::unique_ptr< LevelDBContext > m_db_context
holds all leveldb-specific fields of this class
Definition: dbwrapper.h:184
CDBWrapper & operator=(const CDBWrapper &)=delete
static const std::string OBFUSCATION_KEY
obfuscation key storage key, null-prefixed to avoid collisions
Definition: dbwrapper.h:193
auto & DBContext() const LIFETIMEBOUND
Definition: dbwrapper.h:198
bool IsEmpty()
Return true if the database managed by this class contains no entries.
Definition: dbwrapper.cpp:345
size_t EstimateSize(const K &key_begin, const K &key_end) const
Definition: dbwrapper.h:265
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:165
void reserve(size_type n)
Definition: streams.h:202
void clear()
Definition: streams.h:205
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:83
dbwrapper_error(const std::string &msg)
Definition: dbwrapper.h:53
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE
Definition: dbwrapper.h:23
bool DestroyDB(const std::string &path_str)
Definition: dbwrapper.cpp:40
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE
Definition: dbwrapper.h:24
static const size_t DBWRAPPER_MAX_FILE_SIZE
Definition: dbwrapper.h:25
These should be considered an implementation detail of the specific database.
Definition: dbwrapper.cpp:389
const Obfuscation & GetObfuscation(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Definition: dbwrapper.cpp:391
Definition: common.h:30
auto MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:89
User-controlled performance and debug options.
Definition: dbwrapper.h:28
bool force_compact
Compact database on startup.
Definition: dbwrapper.h:30
Application-specific storage settings.
Definition: dbwrapper.h:34
DBOptions options
Passed-through options.
Definition: dbwrapper.h:47
bool obfuscate
If true, store data obfuscated via simple XOR.
Definition: dbwrapper.h:45
bool wipe_data
If true, remove all existing data.
Definition: dbwrapper.h:42
size_t cache_bytes
Configures various leveldb cache settings.
Definition: dbwrapper.h:38
fs::path path
Location in the filesystem where leveldb data will be stored.
Definition: dbwrapper.h:36
bool memory_only
If true, use leveldb's memory environment.
Definition: dbwrapper.h:40