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