5 #ifndef FABCOIN_DBWRAPPER_H 6 #define FABCOIN_DBWRAPPER_H 16 #include <leveldb/db.h> 17 #include <leveldb/write_batch.h> 19 static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
20 static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
72 template <
typename K,
typename V>
73 void Write(
const K& key,
const V& value)
75 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
77 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
79 ssValue.
reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
82 leveldb::Slice slValue(ssValue.
data(), ssValue.
size());
84 batch.Put(slKey, slValue);
92 size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
100 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
102 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
110 size_estimate += 2 + (slKey.size() > 127) + slKey.
size();
130 parent(_parent), piter(_piter) { };
137 template<
typename K>
void Seek(
const K& key) {
139 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
141 leveldb::Slice slKey(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();
161 CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(),
SER_DISK, CLIENT_VERSION);
164 }
catch (
const std::exception&) {
171 return piter->value().size();
210 std::vector<unsigned char> CreateObfuscateKey()
const;
221 CDBWrapper(
const fs::path& path,
size_t nCacheSize,
bool fMemory =
false,
bool fWipe =
false,
bool obfuscate =
false);
224 template <
typename K,
typename V>
225 bool Read(
const K& key, V& value)
const 228 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
230 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
232 std::string strValue;
233 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
235 if (status.IsNotFound())
237 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
241 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
SER_DISK, CLIENT_VERSION);
242 ssValue.
Xor(obfuscate_key);
244 }
catch (
const std::exception&) {
250 template <
typename K,
typename V>
251 bool Write(
const K& key,
const V& value,
bool fSync =
false)
254 batch.
Write(key, value);
255 return WriteBatch(batch, fSync);
258 template <
typename K>
262 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
264 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
266 std::string strValue;
267 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
269 if (status.IsNotFound())
271 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
277 template <
typename K>
278 bool Erase(
const K& key,
bool fSync =
false)
282 return WriteBatch(batch, fSync);
285 bool WriteBatch(
CDBBatch& batch,
bool fSync =
false);
296 return WriteBatch(batch,
true);
301 return new CDBIterator(*
this, pdb->NewIterator(iteroptions));
313 ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
314 ssKey2.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
317 leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
318 leveldb::Slice slKey2(ssKey2.
data(), ssKey2.
size());
320 leveldb::Range range(slKey1, slKey2);
321 pdb->GetApproximateSizes(&range, 1, &size);
332 ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
333 ssKey2.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
336 leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
337 leveldb::Slice slKey2(ssKey2.
data(), ssKey2.
size());
338 pdb->CompactRange(&slKey1, &slKey2);
343 #endif // FABCOIN_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.
size_t SizeEstimate() const
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
std::hash for asio::adress
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
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
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)
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)
uint8_t const size_t const size
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
leveldb::Options options
database options used
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database