Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

dbwrapper.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_DBWRAPPER_H
6 #define BITCOIN_DBWRAPPER_H
7 
8 #include <clientversion.h>
9 #include <fs.h>
10 #include <serialize.h>
11 #include <streams.h>
12 #include <util.h>
13 #include <utilstrencodings.h>
14 #include <version.h>
15 
16 #include <typeindex>
17 
18 #include <leveldb/db.h>
19 #include <leveldb/write_batch.h>
20 
21 static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
22 static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
23 
24 class dbwrapper_error : public std::runtime_error
25 {
26 public:
27  explicit dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {}
28 };
29 
30 class CDBWrapper;
31 
34 namespace dbwrapper_private {
35 
38 void HandleError(const leveldb::Status& status);
39 
44 const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w);
45 
46 };
47 
49 class CDBBatch
50 {
51  friend class CDBWrapper;
52 
53 private:
55  leveldb::WriteBatch batch;
56 
59 
60  size_t size_estimate;
61 
62 public:
67 
68  void Clear()
69  {
70  batch.Clear();
71  size_estimate = 0;
72  }
73 
74  template <typename K, typename V>
75  void Write(const K& key, const V& value)
76  {
78  ssKey << key;
79  Write(ssKey, value);
80  ssKey.clear();
81  }
82 
83  template <typename V>
84  void Write(const CDataStream& _ssKey, const V& value)
85  {
86  leveldb::Slice slKey(_ssKey.data(), _ssKey.size());
87 
89  ssValue << value;
91  leveldb::Slice slValue(ssValue.data(), ssValue.size());
92 
93  batch.Put(slKey, slValue);
94  // - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
95  // - byte[]: key
96  // - varint: value length
97  // - byte[]: value
98  // The formula below assumes the key and value are both less than 16k.
99  size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
100  ssValue.clear();
101  }
102 
103  template <typename K>
104  void Erase(const K& key)
105  {
107  ssKey << key;
108  Erase(ssKey);
109  ssKey.clear();
110  }
111 
112  void Erase(const CDataStream& _ssKey) {
113  leveldb::Slice slKey(_ssKey.data(), _ssKey.size());
114 
115  batch.Delete(slKey);
116  // - byte: header
117  // - varint: key length
118  // - byte[]: key
119  // The formula below assumes the key is less than 16kB.
120  size_estimate += 2 + (slKey.size() > 127) + slKey.size();
121  }
122 
123  size_t SizeEstimate() const { return size_estimate; }
124 };
125 
127 {
128 private:
130  leveldb::Iterator *piter;
131 
132 public:
133 
138  CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter) :
139  parent(_parent), piter(_piter) { };
140  ~CDBIterator();
141 
142  bool Valid() const;
143 
144  void SeekToFirst();
145 
146  template<typename K> void Seek(const K& key) {
149  ssKey << key;
150  Seek(ssKey);
151  }
152 
153  void Seek(const CDataStream& ssKey) {
154  leveldb::Slice slKey(ssKey.data(), ssKey.size());
155  piter->Seek(slKey);
156  }
157 
158  void Next();
159 
160  template<typename K> bool GetKey(K& key) {
161  try {
162  CDataStream ssKey = GetKey();
163  ssKey >> key;
164  } catch (const std::exception&) {
165  return false;
166  }
167  return true;
168  }
169 
171  leveldb::Slice slKey = piter->key();
172  return CDataStream(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
173  }
174 
175  unsigned int GetKeySize() {
176  return piter->key().size();
177  }
178 
179  template<typename V> bool GetValue(V& value) {
180  leveldb::Slice slValue = piter->value();
181  try {
182  CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
184  ssValue >> value;
185  } catch (const std::exception&) {
186  return false;
187  }
188  return true;
189  }
190 
191  unsigned int GetValueSize() {
192  return piter->value().size();
193  }
194 
195 };
196 
198 {
199  friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
200 private:
202  leveldb::Env* penv;
203 
205  leveldb::Options options;
206 
208  leveldb::ReadOptions readoptions;
209 
211  leveldb::ReadOptions iteroptions;
212 
214  leveldb::WriteOptions writeoptions;
215 
217  leveldb::WriteOptions syncoptions;
218 
221 
223  std::vector<unsigned char> obfuscate_key;
224 
226  static const std::string OBFUSCATE_KEY_KEY;
227 
229  static const unsigned int OBFUSCATE_KEY_NUM_BYTES;
230 
231  std::vector<unsigned char> CreateObfuscateKey() const;
232 
233 public:
242  CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false);
243  ~CDBWrapper();
244 
245  template <typename K>
246  bool ReadDataStream(const K& key, CDataStream& ssValue) const
247  {
250  ssKey << key;
251  return ReadDataStream(ssKey, ssValue);
252  }
253 
254  bool ReadDataStream(const CDataStream& ssKey, CDataStream& ssValue) const
255  {
256  leveldb::Slice slKey(ssKey.data(), ssKey.size());
257 
258  std::string strValue;
259  leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
260  if (!status.ok()) {
261  if (status.IsNotFound())
262  return false;
263  LogPrintf("LevelDB read failure: %s\n", status.ToString());
265  }
266  CDataStream ssValueTmp(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
267  ssValueTmp.Xor(obfuscate_key);
268  ssValue = std::move(ssValueTmp);
269  return true;
270  }
271 
272  template <typename K, typename V>
273  bool Read(const K& key, V& value) const
274  {
277  ssKey << key;
278  return Read(ssKey, value);
279  }
280 
281  template <typename V>
282  bool Read(const CDataStream& ssKey, V& value) const
283  {
285  if (!ReadDataStream(ssKey, ssValue)) {
286  return false;
287  }
288 
289  try {
290  ssValue >> value;
291  } catch (const std::exception&) {
292  return false;
293  }
294  return true;
295  }
296 
297  template <typename K, typename V>
298  bool Write(const K& key, const V& value, bool fSync = false)
299  {
300  CDBBatch batch(*this);
301  batch.Write(key, value);
302  return WriteBatch(batch, fSync);
303  }
304 
305  template <typename K>
306  bool Exists(const K& key) const
307  {
310  ssKey << key;
311  return Exists(ssKey);
312  }
313 
314  bool Exists(const CDataStream& key) const
315  {
316  leveldb::Slice slKey(key.data(), key.size());
317 
318  std::string strValue;
319  leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
320  if (!status.ok()) {
321  if (status.IsNotFound())
322  return false;
323  LogPrintf("LevelDB read failure: %s\n", status.ToString());
325  }
326  return true;
327  }
328 
329  template <typename K>
330  bool Erase(const K& key, bool fSync = false)
331  {
332  CDBBatch batch(*this);
333  batch.Erase(key);
334  return WriteBatch(batch, fSync);
335  }
336 
337  bool WriteBatch(CDBBatch& batch, bool fSync = false);
338 
339  // not available for LevelDB; provide for compatibility with BDB
340  bool Flush()
341  {
342  return true;
343  }
344 
345  bool Sync()
346  {
347  CDBBatch batch(*this);
348  return WriteBatch(batch, true);
349  }
350 
352  {
353  return new CDBIterator(*this, pdb->NewIterator(iteroptions));
354  }
355 
359  bool IsEmpty();
360 
361  template<typename K>
362  size_t EstimateSize(const K& key_begin, const K& key_end) const
363  {
365  ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
367  ssKey1 << key_begin;
368  ssKey2 << key_end;
369  leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
370  leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
371  uint64_t size = 0;
372  leveldb::Range range(slKey1, slKey2);
373  pdb->GetApproximateSizes(&range, 1, &size);
374  return size;
375  }
376 
380  template<typename K>
381  void CompactRange(const K& key_begin, const K& key_end) const
382  {
384  ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
386  ssKey1 << key_begin;
387  ssKey2 << key_end;
388  leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
389  leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
390  pdb->CompactRange(&slKey1, &slKey2);
391  }
392 
393  void CompactFull() const
394  {
395  pdb->CompactRange(nullptr, nullptr);
396  }
397 
398 };
399 
400 template<typename CDBTransaction>
402 {
403 private:
405 
406  typedef typename std::remove_pointer<decltype(transaction.parent.NewIterator())>::type ParentIterator;
407 
408  // We maintain 2 iterators, one for the transaction and one for the parent
409  // At all times, only one of both provides the current value. The decision is made by comparing the current keys
410  // of both iterators, so that always the smaller key is the current one. On Next(), the previously chosen iterator
411  // is advanced.
412  typename CDBTransaction::WritesMap::iterator transactionIt;
413  std::unique_ptr<ParentIterator> parentIt;
415  bool curIsParent{false};
416 
417 public:
418  explicit CDBTransactionIterator(CDBTransaction& _transaction) :
419  transaction(_transaction),
421  {
423  parentIt = std::unique_ptr<ParentIterator>(transaction.parent.NewIterator());
424  }
425 
426  void SeekToFirst() {
427  transactionIt = transaction.writes.begin();
428  parentIt->SeekToFirst();
430  DecideCur();
431  }
432 
433  template<typename K>
434  void Seek(const K& key) {
436  }
437 
438  void Seek(const CDataStream& ssKey) {
439  transactionIt = transaction.writes.lower_bound(ssKey);
440  parentIt->Seek(ssKey);
442  DecideCur();
443  }
444 
445  bool Valid() {
446  return transactionIt != transaction.writes.end() || parentIt->Valid();
447  }
448 
449  void Next() {
450  if (transactionIt == transaction.writes.end() && !parentIt->Valid()) {
451  return;
452  }
453  if (curIsParent) {
454  assert(parentIt->Valid());
455  parentIt->Next();
457  } else {
458  assert(transactionIt != transaction.writes.end());
459  ++transactionIt;
460  }
461  DecideCur();
462  }
463 
464  template<typename K>
465  bool GetKey(K& key) {
466  if (!Valid()) {
467  return false;
468  }
469 
470  if (curIsParent) {
471  try {
472  // TODO try to avoid this copy (we need a stream that allows reading from external buffers)
473  CDataStream ssKey = parentKey;
474  ssKey >> key;
475  } catch (const std::exception&) {
476  return false;
477  }
478  return true;
479  } else {
480  try {
481  // TODO try to avoid this copy (we need a stream that allows reading from external buffers)
482  CDataStream ssKey = transactionIt->first;
483  ssKey >> key;
484  } catch (const std::exception&) {
485  return false;
486  }
487  return true;
488  }
489  }
490 
492  if (!Valid()) {
494  }
495  if (curIsParent) {
496  return parentKey;
497  } else {
498  return transactionIt->first;
499  }
500  }
501 
502  unsigned int GetKeySize() {
503  if (!Valid()) {
504  return 0;
505  }
506  if (curIsParent) {
507  return parentIt->GetKeySize();
508  } else {
509  return transactionIt->first.vKey.size();
510  }
511  }
512 
513  template<typename V>
514  bool GetValue(V& value) {
515  if (!Valid()) {
516  return false;
517  }
518  if (curIsParent) {
519  return transaction.Read(parentKey, value);
520  } else {
521  return transaction.Read(transactionIt->first, value);
522  }
523  };
524 
525 private:
527  while (parentIt->Valid()) {
528  parentKey = parentIt->GetKey();
529  if (!transaction.deletes.count(parentKey) && !transaction.writes.count(parentKey)) {
530  break;
531  }
532  parentIt->Next();
533  }
534  }
535 
536  void DecideCur() {
537  if (transactionIt != transaction.writes.end() && !parentIt->Valid()) {
538  curIsParent = false;
539  } else if (transactionIt == transaction.writes.end() && parentIt->Valid()) {
540  curIsParent = true;
541  } else if (transactionIt != transaction.writes.end() && parentIt->Valid()) {
543  curIsParent = false;
544  } else {
545  curIsParent = true;
546  }
547  }
548  }
549 };
550 
551 template<typename Parent, typename CommitTarget>
554 
555 protected:
556  Parent &parent;
557  CommitTarget &commitTarget;
558  ssize_t memoryUsage{0}; // signed, just in case we made an error in the calculations so that we don't get an overflow
559 
560  struct DataStreamCmp {
561  static bool less(const CDataStream& a, const CDataStream& b) {
562  return std::lexicographical_compare(
563  (const uint8_t*)a.data(), (const uint8_t*)a.data() + a.size(),
564  (const uint8_t*)b.data(), (const uint8_t*)b.data() + b.size());
565  }
566  bool operator()(const CDataStream& a, const CDataStream& b) const {
567  return less(a, b);
568  }
569  };
570 
571  struct ValueHolder {
572  size_t memoryUsage;
573  ValueHolder(size_t _memoryUsage) : memoryUsage(_memoryUsage) {}
574  virtual ~ValueHolder() = default;
575  virtual void Write(const CDataStream& ssKey, CommitTarget &parent) = 0;
576  };
577  typedef std::unique_ptr<ValueHolder> ValueHolderPtr;
578 
579  template <typename V>
581  ValueHolderImpl(const V &_value, size_t _memoryUsage) : ValueHolder(_memoryUsage), value(_value) {}
582 
583  virtual void Write(const CDataStream& ssKey, CommitTarget &commitTarget) {
584  // we're moving the value instead of copying it. This means that Write() can only be called once per
585  // ValueHolderImpl instance. Commit() clears the write maps, so this ok.
586  commitTarget.Write(ssKey, std::move(value));
587  }
588  V value;
589  };
590 
591  template<typename K>
592  static CDataStream KeyToDataStream(const K& key) {
595  ssKey << key;
596  return ssKey;
597  }
598 
599  typedef std::map<CDataStream, ValueHolderPtr, DataStreamCmp> WritesMap;
600  typedef std::set<CDataStream, DataStreamCmp> DeletesSet;
601 
604 
605 public:
606  CDBTransaction(Parent &_parent, CommitTarget &_commitTarget) : parent(_parent), commitTarget(_commitTarget) {}
607 
608  template <typename K, typename V>
609  void Write(const K& key, const V& v) {
610  Write(KeyToDataStream(key), v);
611  }
612 
613  template <typename V>
614  void Write(const CDataStream& ssKey, const V& v) {
615  auto valueMemoryUsage = ::GetSerializeSize(v, SER_DISK, CLIENT_VERSION);
616 
617  if (deletes.erase(ssKey)) {
618  memoryUsage -= ssKey.size();
619  }
620  auto it = writes.emplace(ssKey, nullptr).first;
621  if (it->second) {
622  memoryUsage -= ssKey.size() + it->second->memoryUsage;
623  }
624  it->second = std::make_unique<ValueHolderImpl<V>>(v, valueMemoryUsage);
625 
626  memoryUsage += ssKey.size() + valueMemoryUsage;
627  }
628 
629  template <typename K, typename V>
630  bool Read(const K& key, V& value) {
631  return Read(KeyToDataStream(key), value);
632  }
633 
634  template <typename V>
635  bool Read(const CDataStream& ssKey, V& value) {
636  if (deletes.count(ssKey)) {
637  return false;
638  }
639 
640  auto it = writes.find(ssKey);
641  if (it != writes.end()) {
642  auto *impl = dynamic_cast<ValueHolderImpl<V> *>(it->second.get());
643  if (!impl) {
644  throw std::runtime_error("Read called with V != previously written type");
645  }
646  value = impl->value;
647  return true;
648  }
649 
650  return parent.Read(ssKey, value);
651  }
652 
653  template <typename K>
654  bool Exists(const K& key) {
655  return Exists(KeyToDataStream(key));
656  }
657 
658  bool Exists(const CDataStream& ssKey) {
659  if (deletes.count(ssKey)) {
660  return false;
661  }
662 
663  if (writes.count(ssKey)) {
664  return true;
665  }
666 
667  return parent.Exists(ssKey);
668  }
669 
670  template <typename K>
671  void Erase(const K& key) {
672  return Erase(KeyToDataStream(key));
673  }
674 
675  void Erase(const CDataStream& ssKey) {
676  auto it = writes.find(ssKey);
677  if (it != writes.end()) {
678  memoryUsage -= ssKey.size() + it->second->memoryUsage;
679  writes.erase(it);
680  }
681  if (deletes.emplace(ssKey).second) {
682  memoryUsage += ssKey.size();
683  }
684  }
685 
686  void Clear() {
687  writes.clear();
688  deletes.clear();
689  memoryUsage = 0;
690  }
691 
692  void Commit() {
693  for (const auto &k : deletes) {
694  commitTarget.Erase(k);
695  }
696  for (auto &p : writes) {
697  p.second->Write(p.first, commitTarget);
698  }
699  Clear();
700  }
701 
702  bool IsClean() {
703  return writes.empty() && deletes.empty();
704  }
705 
706  size_t GetMemoryUsage() const {
707  if (memoryUsage < 0) {
708  // something went wrong when we accounted/calculated used memory...
709  static volatile bool didPrint = false;
710  if (!didPrint) {
711  LogPrintf("CDBTransaction::%s -- negative memoryUsage (%d)", __func__, memoryUsage);
712  didPrint = true;
713  }
714  return 0;
715  }
716  return (size_t)memoryUsage;
717  }
718 
720  return new CDBTransactionIterator<CDBTransaction>(*this);
721  }
722  std::unique_ptr<CDBTransactionIterator<CDBTransaction>> NewIteratorUniquePtr() {
723  return std::make_unique<CDBTransactionIterator<CDBTransaction>>(*this);
724  }
725 };
726 
727 #endif // BITCOIN_DBWRAPPER_H
bool Exists(const K &key) const
Definition: dbwrapper.h:306
bool GetKey(K &key)
Definition: dbwrapper.h:465
bool GetKey(K &key)
Definition: dbwrapper.h:160
dbwrapper_error(const std::string &msg)
Definition: dbwrapper.h:27
bool Flush()
Definition: dbwrapper.h:340
These should be considered an implementation detail of the specific database.
Definition: dbwrapper.cpp:196
void Clear()
Definition: dbwrapper.h:68
bool Exists(const CDataStream &key) const
Definition: dbwrapper.h:314
bool Exists(const K &key)
Definition: dbwrapper.h:654
void Write(const CDataStream &ssKey, const V &v)
Definition: dbwrapper.h:614
void SeekToFirst()
Definition: dbwrapper.cpp:193
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:49
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
Definition: dbwrapper.cpp:92
unsigned int GetKeySize()
Definition: dbwrapper.h:502
unsigned int GetKeySize()
Definition: dbwrapper.h:175
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:1295
size_t size_estimate
Definition: dbwrapper.h:60
ValueHolderImpl(const V &_value, size_t _memoryUsage)
Definition: dbwrapper.h:581
void Erase(const CDataStream &_ssKey)
Definition: dbwrapper.h:112
bool Read(const K &key, V &value)
Definition: dbwrapper.h:630
void Erase(const K &key)
Definition: dbwrapper.h:104
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE
Definition: dbwrapper.h:21
void Seek(const K &key)
Definition: dbwrapper.h:434
void SkipDeletedAndOverwritten()
Definition: dbwrapper.h:526
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:376
value_type * data()
Definition: streams.h:203
CDataStream GetKey()
Definition: dbwrapper.h:170
Definition: box.hpp:161
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
Definition: dbwrapper.cpp:198
std::remove_pointer< decltype(transaction.parent.NewIterator())>::type ParentIterator
Definition: dbwrapper.h:406
bool ReadDataStream(const K &key, CDataStream &ssValue) const
Definition: dbwrapper.h:246
leveldb::WriteBatch batch
Definition: dbwrapper.h:55
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key...
Definition: dbwrapper.cpp:176
CDBIterator * NewIterator()
Definition: dbwrapper.h:351
virtual void Write(const CDataStream &ssKey, CommitTarget &commitTarget)
Definition: dbwrapper.h:583
bool IsClean()
Definition: dbwrapper.h:702
CommitTarget & commitTarget
Definition: dbwrapper.h:557
leveldb::ReadOptions readoptions
options used when reading from the database
Definition: dbwrapper.h:208
const CDBWrapper & parent
Definition: dbwrapper.h:129
bool GetValue(V &value)
Definition: dbwrapper.h:179
bool Exists(const CDataStream &ssKey)
Definition: dbwrapper.h:658
leveldb::WriteOptions syncoptions
options used when sync writing to the database
Definition: dbwrapper.h:217
WritesMap writes
Definition: dbwrapper.h:602
#define LogPrintf(...)
Definition: util.h:203
const CDBWrapper & parent
Definition: dbwrapper.h:54
void Clear()
Definition: dbwrapper.h:686
CDataStream parentKey
Definition: dbwrapper.h:414
size_type size() const
Definition: streams.h:194
CDataStream GetKey()
Definition: dbwrapper.h:491
bool Erase(const K &key, bool fSync=false)
Definition: dbwrapper.h:330
CDBTransactionIterator< CDBTransaction > * NewIterator()
Definition: dbwrapper.h:719
leveldb::DB * pdb
the database itself
Definition: dbwrapper.h:220
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
Definition: dbwrapper.h:211
virtual void Write(const CDataStream &ssKey, CommitTarget &parent)=0
void Write(const CDataStream &_ssKey, const V &value)
Definition: dbwrapper.h:84
void Erase(const K &key)
Definition: dbwrapper.h:671
size_t GetMemoryUsage() const
Definition: dbwrapper.h:706
DeletesSet deletes
Definition: dbwrapper.h:603
void Write(const K &key, const V &value)
Definition: dbwrapper.h:75
size_t SizeEstimate() const
Definition: dbwrapper.h:123
leveldb::WriteOptions writeoptions
options used when writing to the database
Definition: dbwrapper.h:214
ValueHolder(size_t _memoryUsage)
Definition: dbwrapper.h:573
void Erase(const CDataStream &ssKey)
Definition: dbwrapper.h:675
CDBTransactionIterator(CDBTransaction &_transaction)
Definition: dbwrapper.h:418
bool IsEmpty()
Return true if the database managed by this class contains no entries.
Definition: dbwrapper.cpp:184
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:273
std::unique_ptr< ParentIterator > parentIt
Definition: dbwrapper.h:413
void Commit()
Definition: dbwrapper.h:692
static CDataStream KeyToDataStream(const K &key)
Definition: dbwrapper.h:592
void CompactFull() const
Definition: dbwrapper.h:393
CDBTransaction(Parent &_parent, CommitTarget &_commitTarget)
Definition: dbwrapper.h:606
unsigned int GetValueSize()
Definition: dbwrapper.h:191
void Seek(const CDataStream &ssKey)
Definition: dbwrapper.h:153
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Definition: dbwrapper.cpp:212
virtual ~ValueHolder()=default
leveldb::Iterator * piter
Definition: dbwrapper.h:130
void Next()
Definition: dbwrapper.cpp:194
bool GetValue(V &value)
Definition: dbwrapper.h:514
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE
Definition: dbwrapper.h:22
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
Definition: dbwrapper.h:229
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:298
void CompactRange(const K &key_begin, const K &key_end) const
Compact a certain range of keys in the database.
Definition: dbwrapper.h:381
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment) ...
Definition: dbwrapper.h:202
void Seek(const CDataStream &ssKey)
Definition: dbwrapper.h:438
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter)
Definition: dbwrapper.h:138
void reserve(size_type n)
Definition: streams.h:197
bool Read(const CDataStream &ssKey, V &value)
Definition: dbwrapper.h:635
bool Read(const CDataStream &ssKey, V &value) const
Definition: dbwrapper.h:282
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
Definition: dbwrapper.h:226
CDataStream ssKey
Definition: dbwrapper.h:57
CDBTransaction & transaction
Definition: dbwrapper.h:404
bool Sync()
Definition: dbwrapper.h:345
std::unique_ptr< ValueHolder > ValueHolderPtr
Definition: dbwrapper.h:577
void Seek(const K &key)
Definition: dbwrapper.h:146
CDBBatch(const CDBWrapper &_parent)
Definition: dbwrapper.h:66
void Write(const K &key, const V &v)
Definition: dbwrapper.h:609
bool Valid() const
Definition: dbwrapper.cpp:192
CDataStream ssValue
Definition: dbwrapper.h:58
void clear()
Definition: streams.h:200
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:157
CDBTransaction::WritesMap::iterator transactionIt
Definition: dbwrapper.h:412
Parent & parent
Definition: dbwrapper.h:556
leveldb::Options options
database options used
Definition: dbwrapper.h:205
bool operator()(const CDataStream &a, const CDataStream &b) const
Definition: dbwrapper.h:566
bool ReadDataStream(const CDataStream &ssKey, CDataStream &ssValue) const
Definition: dbwrapper.h:254
std::unique_ptr< CDBTransactionIterator< CDBTransaction > > NewIteratorUniquePtr()
Definition: dbwrapper.h:722
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
Definition: dbwrapper.h:223
size_t EstimateSize(const K &key_begin, const K &key_end) const
Definition: dbwrapper.h:362
static const int CLIENT_VERSION
dashd-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
std::map< CDataStream, ValueHolderPtr, DataStreamCmp > WritesMap
Definition: dbwrapper.h:599
std::set< CDataStream, DataStreamCmp > DeletesSet
Definition: dbwrapper.h:600
static bool less(const CDataStream &a, const CDataStream &b)
Definition: dbwrapper.h:561
ssize_t memoryUsage
Definition: dbwrapper.h:558
Released under the MIT license