6 #ifndef RAVEN_DBWRAPPER_H 7 #define RAVEN_DBWRAPPER_H 17 #include <leveldb/db.h> 18 #include <leveldb/write_batch.h> 20 static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
21 static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
73 template <
typename K,
typename V>
74 void Write(
const K& key,
const V& value)
76 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
78 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
80 ssValue.
reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
83 leveldb::Slice slValue(ssValue.
data(), ssValue.
size());
85 batch.Put(slKey, slValue);
93 size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
101 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
103 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
111 size_estimate += 2 + (slKey.size() > 127) + slKey.
size();
131 parent(_parent), piter(_piter) { };
138 template<
typename K>
void Seek(
const K& key) {
140 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
142 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
148 template<
typename K>
bool GetKey(K& key) {
149 leveldb::Slice slKey = piter->key();
153 }
catch (
const std::exception&) {
160 leveldb::Slice slValue = piter->value();
162 CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(),
SER_DISK, CLIENT_VERSION);
165 }
catch (
const std::exception&) {
172 return piter->value().size();
211 std::vector<unsigned char> CreateObfuscateKey()
const;
222 CDBWrapper(
const fs::path& path,
size_t nCacheSize,
bool fMemory =
false,
bool fWipe =
false,
bool obfuscate =
false,
size_t maxFileSize = 2 << 20);
225 template <
typename K,
typename V>
226 bool Read(
const K& key, V& value)
const 229 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
231 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
233 std::string strValue;
234 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
236 if (status.IsNotFound())
238 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
242 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
SER_DISK, CLIENT_VERSION);
243 ssValue.
Xor(obfuscate_key);
245 }
catch (
const std::exception&) {
251 template <
typename K,
typename V>
252 bool Write(
const K& key,
const V& value,
bool fSync =
false)
255 batch.
Write(key, value);
256 return WriteBatch(batch, fSync);
259 template <
typename K>
263 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
265 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
267 std::string strValue;
268 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
270 if (status.IsNotFound())
272 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
278 template <
typename K>
279 bool Erase(
const K& key,
bool fSync =
false)
283 return WriteBatch(batch, fSync);
286 bool WriteBatch(
CDBBatch& batch,
bool fSync =
false);
297 return WriteBatch(batch,
true);
302 return new CDBIterator(*
this, pdb->NewIterator(iteroptions));
314 ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
315 ssKey2.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
318 leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
319 leveldb::Slice slKey2(ssKey2.
data(), ssKey2.
size());
321 leveldb::Range range(slKey1, slKey2);
322 pdb->GetApproximateSizes(&range, 1, &size);
333 ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
334 ssKey2.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
337 leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
338 leveldb::Slice slKey2(ssKey2.
data(), ssKey2.
size());
339 pdb->CompactRange(&slKey1, &slKey2);
344 #endif // RAVEN_DBWRAPPER_H bool Exists(const K &key) const
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.
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()
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
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
void Write(const K &key, const V &value)
size_t SizeEstimate() const
leveldb::WriteOptions writeoptions
options used when writing to the database
bool Read(const K &key, V &value) const
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 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)
void CompactRange(const K &key_begin, const K &key_end) const
Compact a certain range of keys in the database.
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment) ...
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)
leveldb::Options options
database options used
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
size_t EstimateSize(const K &key_begin, const K &key_end) const