5 #ifndef BITCOIN_DBWRAPPER_H
6 #define BITCOIN_DBWRAPPER_H
15 #include <leveldb/db.h>
16 #include <leveldb/write_batch.h>
71 template <
typename K,
typename V>
72 void Write(
const K& key,
const V& value)
76 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
81 leveldb::Slice slValue(ssValue.
data(), ssValue.
size());
83 batch.Put(slKey, slValue);
91 size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
101 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
109 size_estimate += 2 + (slKey.size() > 127) + slKey.
size();
129 parent(_parent), piter(_piter) { };
136 template<
typename K>
void Seek(
const K& key) {
140 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
146 template<
typename K>
bool GetKey(K& key) {
147 leveldb::Slice slKey = piter->key();
151 }
catch (
const std::exception&) {
158 leveldb::Slice slValue = piter->value();
163 }
catch (
const std::exception&) {
170 return piter->value().size();
223 CDBWrapper(
const fs::path& path,
size_t nCacheSize,
bool fMemory =
false,
bool fWipe =
false,
bool obfuscate =
false);
229 template <
typename K,
typename V>
230 bool Read(
const K& key, V& value)
const
235 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
237 std::string strValue;
238 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
240 if (status.IsNotFound())
242 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
247 ssValue.
Xor(obfuscate_key);
249 }
catch (
const std::exception&) {
255 template <
typename K,
typename V>
256 bool Write(
const K& key,
const V& value,
bool fSync =
false)
259 batch.
Write(key, value);
263 template <
typename K>
269 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
271 std::string strValue;
272 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
274 if (status.IsNotFound())
276 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
282 template <
typename K>
283 bool Erase(
const K& key,
bool fSync =
false)
297 return new CDBIterator(*
this, pdb->NewIterator(iteroptions));
313 leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
314 leveldb::Slice slKey2(ssKey2.
data(), ssKey2.
size());
316 leveldb::Range range(slKey1, slKey2);
317 pdb->GetApproximateSizes(&range, 1, &size);
332 leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
333 leveldb::Slice slKey2(ssKey2.
data(), ssKey2.
size());
334 pdb->CompactRange(&slKey1, &slKey2);
339 #endif // BITCOIN_DBWRAPPER_H
dbwrapper_error(const std::string &msg)
These should be considered an implementation detail of the specific database.
Batch of changes queued to be written to a CDBWrapper.
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
static void LogPrintf(const char *fmt, const Args &...args)
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE
size_t SizeEstimate() const
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Double ended buffer combining vector and stream-like interfaces.
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
leveldb::WriteBatch batch
CDBIterator * NewIterator()
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key...
leveldb::ReadOptions readoptions
options used when reading from the database
const CDBWrapper & parent
leveldb::WriteOptions syncoptions
options used when sync writing to the database
const CDBWrapper & parent
CDBWrapper & operator=(const CDBWrapper &)=delete
bool Erase(const K &key, bool fSync=false)
leveldb::DB * pdb
the database itself
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
size_t EstimateSize(const K &key_begin, const K &key_end) const
void Write(const K &key, const V &value)
leveldb::WriteOptions writeoptions
options used when writing to the database
size_t DynamicMemoryUsage() const
bool IsEmpty()
Return true if the database managed by this class contains no entries.
unsigned int GetValueSize()
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
leveldb::Iterator * piter
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
bool Write(const K &key, const V &value, bool fSync=false)
std::string m_name
the name of this database
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment) ...
void CompactRange(const K &key_begin, const K &key_end) const
Compact a certain range of keys in the database.
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter)
void reserve(size_type n)
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
CDBBatch(const CDBWrapper &_parent)
bool Exists(const K &key) const
bool Read(const K &key, V &value) const
bool WriteBatch(CDBBatch &batch, bool fSync=false)
leveldb::Options options
database options used
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.