Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
leveldbwrapper.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_LEVELDBWRAPPER_H
6 #define BITCOIN_LEVELDBWRAPPER_H
7 
8 #include "serialize.h"
9 #include "util.h"
10 #include "version.h"
11 
12 #include <boost/filesystem/path.hpp>
13 #include <leveldb/db.h>
14 #include <leveldb/write_batch.h>
15 
16 class leveldb_error : public std::runtime_error
17 {
18 public:
19  leveldb_error(const std::string &msg) : std::runtime_error(msg) {}
20 };
21 
22 void HandleError(const leveldb::Status &status) throw(leveldb_error);
23 
24 // Batch of changes queued to be written to a CLevelDBWrapper
26 {
27  friend class CLevelDBWrapper;
28 
29 private:
30  leveldb::WriteBatch batch;
31 
32 public:
33  template<typename K, typename V> void Write(const K& key, const V& value) {
35  ssKey.reserve(ssKey.GetSerializeSize(key));
36  ssKey << key;
37  leveldb::Slice slKey(&ssKey[0], ssKey.size());
38 
40  ssValue.reserve(ssValue.GetSerializeSize(value));
41  ssValue << value;
42  leveldb::Slice slValue(&ssValue[0], ssValue.size());
43 
44  batch.Put(slKey, slValue);
45  }
46 
47  template<typename K> void Erase(const K& key) {
49  ssKey.reserve(ssKey.GetSerializeSize(key));
50  ssKey << key;
51  leveldb::Slice slKey(&ssKey[0], ssKey.size());
52 
53  batch.Delete(slKey);
54  }
55 };
56 
58 {
59 private:
60  // custom environment this database is using (may be NULL in case of default environment)
61  leveldb::Env *penv;
62 
63  // database options used
64  leveldb::Options options;
65 
66  // options used when reading from the database
67  leveldb::ReadOptions readoptions;
68 
69  // options used when iterating over values of the database
70  leveldb::ReadOptions iteroptions;
71 
72  // options used when writing to the database
73  leveldb::WriteOptions writeoptions;
74 
75  // options used when sync writing to the database
76  leveldb::WriteOptions syncoptions;
77 
78  // the database itself
79  leveldb::DB *pdb;
80 
81 public:
82  CLevelDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
84 
85  template<typename K, typename V> bool Read(const K& key, V& value) throw(leveldb_error) {
87  ssKey.reserve(ssKey.GetSerializeSize(key));
88  ssKey << key;
89  leveldb::Slice slKey(&ssKey[0], ssKey.size());
90 
91  std::string strValue;
92  leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
93  if (!status.ok()) {
94  if (status.IsNotFound())
95  return false;
96  LogPrintf("LevelDB read failure: %s\n", status.ToString().c_str());
97  HandleError(status);
98  }
99  try {
100  CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
101  ssValue >> value;
102  } catch(std::exception &e) {
103  return false;
104  }
105  return true;
106  }
107 
108  template<typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) throw(leveldb_error) {
109  CLevelDBBatch batch;
110  batch.Write(key, value);
111  return WriteBatch(batch, fSync);
112  }
113 
114  template<typename K> bool Exists(const K& key) throw(leveldb_error) {
116  ssKey.reserve(ssKey.GetSerializeSize(key));
117  ssKey << key;
118  leveldb::Slice slKey(&ssKey[0], ssKey.size());
119 
120  std::string strValue;
121  leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
122  if (!status.ok()) {
123  if (status.IsNotFound())
124  return false;
125  LogPrintf("LevelDB read failure: %s\n", status.ToString().c_str());
126  HandleError(status);
127  }
128  return true;
129  }
130 
131  template<typename K> bool Erase(const K& key, bool fSync = false) throw(leveldb_error) {
132  CLevelDBBatch batch;
133  batch.Erase(key);
134  return WriteBatch(batch, fSync);
135  }
136 
137  bool WriteBatch(CLevelDBBatch &batch, bool fSync = false) throw(leveldb_error);
138 
139  // not available for LevelDB; provide for compatibility with BDB
140  bool Flush() {
141  return true;
142  }
143 
144  bool Sync() throw(leveldb_error) {
145  CLevelDBBatch batch;
146  return WriteBatch(batch, true);
147  }
148 
149  // not exactly clean encapsulation, but it's easiest for now
150  leveldb::Iterator *NewIterator() {
151  return pdb->NewIterator(iteroptions);
152  }
153 };
154 
155 #endif // BITCOIN_LEVELDBWRAPPER_H
bool Erase(const K &key, bool fSync=false)
static const int CLIENT_VERSION
Definition: version.h:15
leveldb::Env * penv
STL namespace.
bool Write(const K &key, const V &value, bool fSync=false)
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:839
leveldb::WriteOptions syncoptions
leveldb::DB * pdb
void Write(const K &key, const V &value)
bool Exists(const K &key)
leveldb::WriteBatch batch
#define LogPrintf(...)
Definition: util.h:117
CLevelDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false)
leveldb::WriteOptions writeoptions
size_type size() const
Definition: serialize.h:928
leveldb::ReadOptions iteroptions
bool Read(const K &key, V &value)
bool WriteBatch(CLevelDBBatch &batch, bool fSync=false)
leveldb::Options options
void reserve(size_type n)
Definition: serialize.h:931
void Erase(const K &key)
unsigned int GetSerializeSize(const T &obj)
Definition: serialize.h:1102
leveldb_error(const std::string &msg)
void HandleError(const leveldb::Status &status)
leveldb::Iterator * NewIterator()
leveldb::ReadOptions readoptions