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.
5 #ifndef BITCOIN_LEVELDBWRAPPER_H
6 #define BITCOIN_LEVELDBWRAPPER_H
13 #include <boost/filesystem/path.hpp>
15 #include <leveldb/db.h>
16 #include <leveldb/write_batch.h>
18 class leveldb_error : public std::runtime_error
21 leveldb_error(const std::string& msg) : std::runtime_error(msg) {}
24 void HandleError(const leveldb::Status& status) throw(leveldb_error);
26 // Batch of changes queued to be written to a CLevelDBWrapper
29 friend class CLevelDBWrapper;
32 leveldb::WriteBatch batch;
35 template <typename K, typename V>
36 void Write(const K& key, const V& value)
38 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
39 ssKey.reserve(ssKey.GetSerializeSize(key));
41 leveldb::Slice slKey(&ssKey[0], ssKey.size());
43 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
44 ssValue.reserve(ssValue.GetSerializeSize(value));
46 leveldb::Slice slValue(&ssValue[0], ssValue.size());
48 batch.Put(slKey, slValue);
52 void Erase(const K& key)
54 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
55 ssKey.reserve(ssKey.GetSerializeSize(key));
57 leveldb::Slice slKey(&ssKey[0], ssKey.size());
66 // custom environment this database is using (may be NULL in case of default environment)
69 // database options used
70 leveldb::Options options;
72 // options used when reading from the database
73 leveldb::ReadOptions readoptions;
75 // options used when iterating over values of the database
76 leveldb::ReadOptions iteroptions;
78 // options used when writing to the database
79 leveldb::WriteOptions writeoptions;
81 // options used when sync writing to the database
82 leveldb::WriteOptions syncoptions;
84 // the database itself
88 CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
91 template <typename K, typename V>
92 bool Read(const K& key, V& value) const throw(leveldb_error)
94 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
95 ssKey.reserve(ssKey.GetSerializeSize(key));
97 leveldb::Slice slKey(&ssKey[0], ssKey.size());
100 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
102 if (status.IsNotFound())
104 LogPrintf("LevelDB read failure: %s\n", status.ToString());
108 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
110 } catch (const std::exception&) {
116 template <typename K, typename V>
117 bool Write(const K& key, const V& value, bool fSync = false) throw(leveldb_error)
120 batch.Write(key, value);
121 return WriteBatch(batch, fSync);
124 template <typename K>
125 bool Exists(const K& key) const throw(leveldb_error)
127 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
128 ssKey.reserve(ssKey.GetSerializeSize(key));
130 leveldb::Slice slKey(&ssKey[0], ssKey.size());
132 std::string strValue;
133 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
135 if (status.IsNotFound())
137 LogPrintf("LevelDB read failure: %s\n", status.ToString());
143 template <typename K>
144 bool Erase(const K& key, bool fSync = false) throw(leveldb_error)
148 return WriteBatch(batch, fSync);
151 bool WriteBatch(CLevelDBBatch& batch, bool fSync = false) throw(leveldb_error);
153 // not available for LevelDB; provide for compatibility with BDB
159 bool Sync() throw(leveldb_error)
162 return WriteBatch(batch, true);
165 // not exactly clean encapsulation, but it's easiest for now
166 leveldb::Iterator* NewIterator()
168 return pdb->NewIterator(iteroptions);
172 #endif // BITCOIN_LEVELDBWRAPPER_H