5 #ifndef BITCOIN_DBWRAPPER_H
6 #define BITCOIN_DBWRAPPER_H
16 #include <leveldb/db.h>
17 #include <leveldb/write_batch.h>
27 :
std::runtime_error(msg) {}
33 using leveldb::DestroyDB;
79 template <
typename K,
typename V>
void Write(
const K &key,
const V &value) {
89 batch.Put(slKey, slValue);
98 (slValue.size() > 127) + slValue.size();
103 template <
typename K>
void Erase(
const K &key) {
139 template <
typename K>
void Seek(
const K &key) {
143 leveldb::Slice slKey((
const char *)ssKey.
data(), ssKey.
size());
149 template <
typename K>
bool GetKey(K &key) {
150 leveldb::Slice slKey =
piter->key();
154 }
catch (
const std::exception &) {
161 leveldb::Slice slValue =
piter->value();
167 }
catch (
const std::exception &) {
177 friend const std::vector<uint8_t> &
235 bool fWipe =
false,
bool obfuscate =
false);
241 template <
typename K,
typename V>
bool Read(
const K &key, V &value)
const {
245 leveldb::Slice slKey((
const char *)ssKey.
data(), ssKey.
size());
247 std::string strValue;
250 if (status.IsNotFound())
return false;
251 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
259 }
catch (
const std::exception &) {
265 template <
typename K,
typename V>
266 bool Write(
const K &key,
const V &value,
bool fSync =
false) {
268 batch.
Write(key, value);
280 template <
typename K>
bool Exists(
const K &key)
const {
284 leveldb::Slice slKey((
const char *)ssKey.
data(), ssKey.
size());
286 std::string strValue;
289 if (status.IsNotFound())
return false;
290 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
296 template <
typename K>
bool Erase(
const K &key,
bool fSync =
false) {
316 template <
typename K>
324 leveldb::Slice slKey1((
const char *)ssKey1.data(), ssKey1.size());
325 leveldb::Slice slKey2((
const char *)ssKey2.
data(), ssKey2.
size());
327 leveldb::Range range(slKey1, slKey2);
328 pdb->GetApproximateSizes(&range, 1, &size);
335 template <
typename K>
343 leveldb::Slice slKey1((
const char *)ssKey1.data(), ssKey1.size());
344 leveldb::Slice slKey2((
const char *)ssKey2.
data(), ssKey2.
size());
345 pdb->CompactRange(&slKey1, &slKey2);
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
unsigned int GetValueSize()
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::vector< uint8_t > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
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< uint8_t > obfuscate_key
a key used for optional XOR-obfuscation of the database
CDBWrapper & operator=(const CDBWrapper &)=delete
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
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
bool m_is_memory
whether or not the database resides in memory
leveldb::DB * pdb
the database itself
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
void CompactRange(const K &key_begin, const K &key_end) const
Compact a certain range of keys in 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 reserve(size_type n)
void Xor(const std::vector< uint8_t > &key)
XOR the contents of this stream with a certain key.
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 constexpr 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< uint8_t > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Span< const std::byte > MakeByteSpan(V &&v) noexcept