PRCYCoin  2.0.0.7rc1
P2P Digital Currency
dbwrapper.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2014 The Bitcoin 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 "version.h"
14 
15 
16 #include <leveldb/db.h>
17 #include <leveldb/write_batch.h>
18 
19 class dbwrapper_error : public std::runtime_error
20 {
21 public:
22  dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {}
23 };
24 
25 class CDBWrapper;
26 
29 namespace dbwrapper_private {
30 
33 void HandleError(const leveldb::Status& status);
34 
35 };
36 
37 
39 class CDBBatch
40 {
41  friend class CDBWrapper;
42 
43 private:
44  leveldb::WriteBatch batch;
45 
46 public:
47 
48  template <typename K, typename V>
49  void Write(const K& key, const V& value)
50  {
51  CDataStream ssKey(SER_DISK, CLIENT_VERSION);
52  ssKey.reserve(ssKey.GetSerializeSize(key));
53  ssKey << key;
54  leveldb::Slice slKey(&ssKey[0], ssKey.size());
55 
56  CDataStream ssValue(SER_DISK, CLIENT_VERSION);
57  ssValue.reserve(ssValue.GetSerializeSize(value));
58  ssValue << value;
59  leveldb::Slice slValue(&ssValue[0], ssValue.size());
60 
61  batch.Put(slKey, slValue);
62  }
63 
64  template <typename K>
65  void Erase(const K& key)
66  {
67  CDataStream ssKey(SER_DISK, CLIENT_VERSION);
68  ssKey.reserve(ssKey.GetSerializeSize(key));
69  ssKey << key;
70  leveldb::Slice slKey(&ssKey[0], ssKey.size());
71 
72  batch.Delete(slKey);
73  }
74 };
75 
77 {
78 private:
79  leveldb::Iterator *piter;
80 
81 public:
85  CDBIterator(leveldb::Iterator *_piter) : piter(_piter) { };
86  ~CDBIterator();
87 
88  bool Valid();
89 
90  void SeekToFirst();
91 
92  template<typename K> void Seek(const K& key) {
93  CDataStream ssKey(SER_DISK, CLIENT_VERSION);
94  ssKey.reserve(ssKey.GetSerializeSize(key));
95  ssKey << key;
96  leveldb::Slice slKey(&ssKey[0], ssKey.size());
97  piter->Seek(slKey);
98  }
99 
100  void Next();
101 
102  template<typename K> bool GetKey(K& key) {
103  leveldb::Slice slKey = piter->key();
104  try {
105  CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
106  ssKey >> key;
107  } catch(std::exception &e) {
108  return false;
109  }
110  return true;
111  }
112 
113  unsigned int GetKeySize() {
114  return piter->key().size();
115  }
116 
117  template<typename V> bool GetValue(V& value) {
118  leveldb::Slice slValue = piter->value();
119  try {
120  CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
121  ssValue >> value;
122  } catch(std::exception &e) {
123  return false;
124  }
125  return true;
126  }
127 
128  unsigned int GetValueSize() {
129  return piter->value().size();
130  }
131 
132 };
133 
135 {
136 private:
138  leveldb::Env* penv;
139 
141  leveldb::Options options;
142 
144  leveldb::ReadOptions readoptions;
145 
147  leveldb::ReadOptions iteroptions;
148 
150  leveldb::WriteOptions writeoptions;
151 
153  leveldb::WriteOptions syncoptions;
154 
157 
158 public:
165  CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
166  ~CDBWrapper();
167 
168  template <typename K, typename V>
169  bool Read(const K& key, V& value) const
170  {
171  CDataStream ssKey(SER_DISK, CLIENT_VERSION);
172  ssKey.reserve(ssKey.GetSerializeSize(key));
173  ssKey << key;
174  leveldb::Slice slKey(&ssKey[0], ssKey.size());
175 
176  std::string strValue;
177  leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
178  if (!status.ok()) {
179  if (status.IsNotFound())
180  return false;
181  LogPrintf("LevelDB read failure: %s\n", status.ToString());
183  }
184  try {
185  CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
186  ssValue >> value;
187  } catch (const std::exception&) {
188  return false;
189  }
190  return true;
191  }
192 
193  template <typename K, typename V>
194  bool Write(const K& key, const V& value, bool fSync = false)
195  {
196  CDBBatch batch;
197  batch.Write(key, value);
198  return WriteBatch(batch, fSync);
199  }
200 
201  template <typename K>
202  bool Exists(const K& key) const
203  {
204  CDataStream ssKey(SER_DISK, CLIENT_VERSION);
205  ssKey.reserve(ssKey.GetSerializeSize(key));
206  ssKey << key;
207  leveldb::Slice slKey(&ssKey[0], ssKey.size());
208 
209  std::string strValue;
210  leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
211  if (!status.ok()) {
212  if (status.IsNotFound())
213  return false;
214  LogPrintf("LevelDB read failure: %s\n", status.ToString());
216  }
217  return true;
218  }
219 
220  template <typename K>
221  bool Erase(const K& key, bool fSync = false)
222  {
223  CDBBatch batch;
224  batch.Erase(key);
225  return WriteBatch(batch, fSync);
226  }
227 
228  bool WriteBatch(CDBBatch& batch, bool fSync = false);
229 
230  // not available for LevelDB; provide for compatibility with BDB
231  bool Flush()
232  {
233  return true;
234  }
235 
236  bool Sync()
237  {
238  CDBBatch batch;
239  return WriteBatch(batch, true);
240  }
241 
242  // not exactly clean encapsulation, but it's easiest for now
244  {
245  return new CDBIterator(pdb->NewIterator(iteroptions));
246  }
247 
251  bool IsEmpty();
252 
253 };
254 
255 #endif // BITCOIN_DBWRAPPER_H
fs.h
CDBBatch
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:39
streams.h
clientversion.h
CDBWrapper::Read
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:169
CDBWrapper::readoptions
leveldb::ReadOptions readoptions
options used when reading from the database
Definition: dbwrapper.h:144
CDBWrapper::syncoptions
leveldb::WriteOptions syncoptions
options used when sync writing to the database
Definition: dbwrapper.h:153
version.h
CDBIterator::CDBIterator
CDBIterator(leveldb::Iterator *_piter)
Definition: dbwrapper.h:85
CDBIterator::Next
void Next()
Definition: dbwrapper.cpp:113
CDBBatch::Write
void Write(const K &key, const V &value)
Definition: dbwrapper.h:49
CDBIterator::Valid
bool Valid()
Definition: dbwrapper.cpp:111
dbwrapper_error::dbwrapper_error
dbwrapper_error(const std::string &msg)
Definition: dbwrapper.h:22
CDBWrapper::NewIterator
CDBIterator * NewIterator()
Definition: dbwrapper.h:243
CDBIterator::GetValue
bool GetValue(V &value)
Definition: dbwrapper.h:117
CDBIterator::GetKeySize
unsigned int GetKeySize()
Definition: dbwrapper.h:113
CDBWrapper::iteroptions
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
Definition: dbwrapper.h:147
CDBWrapper::penv
leveldb::Env * penv
custom environment this database is using (may be NULL in case of default environment)
Definition: dbwrapper.h:138
CDBIterator::~CDBIterator
~CDBIterator()
Definition: dbwrapper.cpp:110
LogPrintf
#define LogPrintf(...)
Definition: logging.h:147
SER_DISK
@ SER_DISK
Definition: serialize.h:160
CDBBatch::batch
leveldb::WriteBatch batch
Definition: dbwrapper.h:44
CDBWrapper::pdb
leveldb::DB * pdb
the database itself
Definition: dbwrapper.h:156
dbwrapper_private
These should be considered an implementation detail of the specific database.
Definition: dbwrapper.cpp:115
CDBIterator::piter
leveldb::Iterator * piter
Definition: dbwrapper.h:79
CDBWrapper::Erase
bool Erase(const K &key, bool fSync=false)
Definition: dbwrapper.h:221
BCLog::DB
@ DB
Definition: logging.h:46
CDataStream::reserve
void reserve(size_type n)
Definition: streams.h:123
CDBIterator::Seek
void Seek(const K &key)
Definition: dbwrapper.h:92
CDBWrapper::writeoptions
leveldb::WriteOptions writeoptions
options used when writing to the database
Definition: dbwrapper.h:150
CDataStream::size
size_type size() const
Definition: streams.h:120
CDBWrapper::Sync
bool Sync()
Definition: dbwrapper.h:236
dbwrapper_private::HandleError
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
Definition: dbwrapper.cpp:117
CDBWrapper
Definition: dbwrapper.h:134
dbwrapper_error
Definition: dbwrapper.h:19
CDBWrapper::options
leveldb::Options options
database options used
Definition: dbwrapper.h:141
key
CKey key
Definition: bip38tooldialog.cpp:173
CDBWrapper::~CDBWrapper
~CDBWrapper()
Definition: dbwrapper.cpp:84
std
Definition: adjacency_graphs.hpp:25
CDBIterator::GetKey
bool GetKey(K &key)
Definition: dbwrapper.h:102
CDBIterator::GetValueSize
unsigned int GetValueSize()
Definition: dbwrapper.h:128
CDBBatch::Erase
void Erase(const K &key)
Definition: dbwrapper.h:65
CDBWrapper::CDBWrapper
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: dbwrapper.cpp:58
CDBIterator::SeekToFirst
void SeekToFirst()
Definition: dbwrapper.cpp:112
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:34
CDBWrapper::IsEmpty
bool IsEmpty()
Return true if the database managed by this class contains no entries.
Definition: dbwrapper.cpp:103
CDBWrapper::Write
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:194
CDBWrapper::Exists
bool Exists(const K &key) const
Definition: dbwrapper.h:202
CDBWrapper::Flush
bool Flush()
Definition: dbwrapper.h:231
CDataStream::GetSerializeSize
unsigned int GetSerializeSize(const T &obj)
Definition: streams.h:267
util.h
CDBWrapper::WriteBatch
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:96
CDBIterator
Definition: dbwrapper.h:76