5 #ifndef BITCOIN_DBWRAPPER_H
6 #define BITCOIN_DBWRAPPER_H
18 #include <leveldb/db.h>
19 #include <leveldb/iterator.h>
20 #include <leveldb/options.h>
21 #include <leveldb/slice.h>
22 #include <leveldb/status.h>
23 #include <leveldb/write_batch.h>
66 using leveldb::DestroyDB;
111 template <
typename K,
typename V>
123 batch.Put(slKey, slValue);
131 size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
136 template <
typename K>
176 template<
typename K>
void Seek(
const K& key) {
180 leveldb::Slice slKey((
const char*)ssKey.data(), ssKey.size());
187 leveldb::Slice slKey =
piter->key();
191 }
catch (
const std::exception&) {
198 leveldb::Slice slValue =
piter->value();
203 }
catch (
const std::exception&) {
262 template <
typename K,
typename V>
263 bool Read(
const K& key, V& value)
const
268 leveldb::Slice slKey((
const char*)ssKey.data(), ssKey.size());
270 std::string strValue;
273 if (status.IsNotFound())
275 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
282 }
catch (
const std::exception&) {
288 template <
typename K,
typename V>
289 bool Write(
const K& key,
const V& value,
bool fSync =
false)
292 batch.
Write(key, value);
304 template <
typename K>
310 leveldb::Slice slKey((
const char*)ssKey.data(), ssKey.size());
312 std::string strValue;
315 if (status.IsNotFound())
317 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
323 template <
typename K>
324 bool Erase(
const K& key,
bool fSync =
false)
354 leveldb::Slice slKey1((
const char*)ssKey1.data(), ssKey1.size());
355 leveldb::Slice slKey2((
const char*)ssKey2.data(), ssKey2.size());
357 leveldb::Range range(slKey1, slKey2);
358 pdb->GetApproximateSizes(&range, 1, &size);
Batch of changes queued to be written to a CDBWrapper.
size_t SizeEstimate() const
void Write(const K &key, const V &value)
CDBBatch(const CDBWrapper &_parent)
leveldb::WriteBatch batch
const CDBWrapper & parent
leveldb::Iterator * piter
const CDBWrapper & parent
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter)
CDBWrapper(const CDBWrapper &)=delete
CDBIterator * NewIterator()
size_t DynamicMemoryUsage() const
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment)
bool WriteBatch(CDBBatch &batch, bool fSync=false)
bool Read(const K &key, V &value) const
std::string m_name
the name of this database
bool Erase(const K &key, bool fSync=false)
bool Write(const K &key, const V &value, bool fSync=false)
bool Exists(const K &key) const
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
CDBWrapper & operator=(const CDBWrapper &)=delete
CDBWrapper(const DBParams ¶ms)
std::optional< fs::path > StoragePath()
leveldb::Options options
database options used
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
leveldb::WriteOptions writeoptions
options used when writing to the database
const fs::path m_path
path to filesystem storage
leveldb::WriteOptions syncoptions
options used when sync writing to the database
bool m_is_memory
whether or not the database resides in memory
leveldb::DB * pdb
the database itself
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
bool IsEmpty()
Return true if the database managed by this class contains no entries.
leveldb::ReadOptions readoptions
options used when reading from the database
size_t EstimateSize(const K &key_begin, const K &key_end) const
Double ended buffer combining vector and stream-like interfaces.
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
void reserve(size_type n)
dbwrapper_error(const std::string &msg)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE
These should be considered an implementation detail of the specific database.
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Span< const std::byte > MakeByteSpan(V &&v) noexcept
User-controlled performance and debug options.
bool force_compact
Compact database on startup.
Application-specific storage settings.
DBOptions options
Passed-through options.
bool obfuscate
If true, store data obfuscated via simple XOR.
bool wipe_data
If true, remove all existing data.
size_t cache_bytes
Configures various leveldb cache settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
bool memory_only
If true, use leveldb's memory environment.