 |
Bitcoin Core
21.99.0
P2P Digital Currency
|
Go to the documentation of this file.
5 #ifndef BITCOIN_DBWRAPPER_H
6 #define BITCOIN_DBWRAPPER_H
16 #include <leveldb/db.h>
17 #include <leveldb/write_batch.h>
72 template <
typename K,
typename V>
73 void Write(
const K& key,
const V& value)
84 batch.Put(slKey, slValue);
92 size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
137 template<
typename K>
void Seek(
const K& key) {
141 leveldb::Slice slKey((
const char*)ssKey.
data(), ssKey.
size());
147 template<
typename K>
bool GetKey(K& key) {
148 leveldb::Slice slKey =
piter->key();
152 }
catch (
const std::exception&) {
159 leveldb::Slice slValue =
piter->value();
164 }
catch (
const std::exception&) {
171 return piter->value().size();
224 CDBWrapper(
const fs::path& path,
size_t nCacheSize,
bool fMemory =
false,
bool fWipe =
false,
bool obfuscate =
false);
230 template <
typename K,
typename V>
231 bool Read(
const K& key, V& value)
const
236 leveldb::Slice slKey((
const char*)ssKey.
data(), ssKey.
size());
238 std::string strValue;
241 if (status.IsNotFound())
243 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
250 }
catch (
const std::exception&) {
256 template <
typename K,
typename V>
257 bool Write(
const K& key,
const V& value,
bool fSync =
false)
260 batch.
Write(key, value);
264 template <
typename K>
270 leveldb::Slice slKey((
const char*)ssKey.
data(), ssKey.
size());
272 std::string strValue;
275 if (status.IsNotFound())
277 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
283 template <
typename K>
284 bool Erase(
const K& key,
bool fSync =
false)
314 leveldb::Slice slKey1((
const char*)ssKey1.data(), ssKey1.size());
315 leveldb::Slice slKey2((
const char*)ssKey2.
data(), ssKey2.
size());
317 leveldb::Range range(slKey1, slKey2);
318 pdb->GetApproximateSizes(&range, 1, &size);
333 leveldb::Slice slKey1((
const char*)ssKey1.data(), ssKey1.size());
334 leveldb::Slice slKey2((
const char*)ssKey2.
data(), ssKey2.
size());
335 pdb->CompactRange(&slKey1, &slKey2);
339 #endif // BITCOIN_DBWRAPPER_H
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter)
const CDBWrapper & parent
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE
Batch of changes queued to be written to a CDBWrapper.
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
const CDBWrapper & parent
bool Read(const K &key, V &value) const
CDBBatch(const CDBWrapper &_parent)
leveldb::ReadOptions readoptions
options used when reading from the database
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)
void Write(const K &key, const V &value)
dbwrapper_error(const std::string &msg)
CDBIterator * NewIterator()
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment)
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
leveldb::WriteBatch batch
leveldb::DB * pdb
the database itself
These should be considered an implementation detail of the specific database.
leveldb::Iterator * piter
bool Erase(const K &key, bool fSync=false)
size_t DynamicMemoryUsage() const
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
void reserve(size_type n)
leveldb::WriteOptions writeoptions
options used when writing to the database
std::string m_name
the name of this database
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
leveldb::Options options
database options used
unsigned int GetValueSize()
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
CDBWrapper & operator=(const CDBWrapper &)=delete
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Double ended buffer combining vector and stream-like interfaces.
bool IsEmpty()
Return true if the database managed by this class contains no entries.
size_t EstimateSize(const K &key_begin, const K &key_end) const
bool Write(const K &key, const V &value, bool fSync=false)
bool Exists(const K &key) const
void CompactRange(const K &key_begin, const K &key_end) const
Compact a certain range of keys in the database.
bool WriteBatch(CDBBatch &batch, bool fSync=false)
size_t SizeEstimate() const