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
9 #include <leveldb/db.h>
10 #include <leveldb/write_batch.h>
12 #include <boost/filesystem/path.hpp>
14 // Batch of changes queued to be written to a CLevelDB
17 friend class CLevelDB;
20 leveldb::WriteBatch batch;
23 template<typename K, typename V> void Write(const K& key, const V& value) {
24 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
25 ssKey.reserve(ssKey.GetSerializeSize(key));
27 leveldb::Slice slKey(&ssKey[0], ssKey.size());
29 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
30 ssValue.reserve(ssValue.GetSerializeSize(value));
32 leveldb::Slice slValue(&ssValue[0], ssValue.size());
34 batch.Put(slKey, slValue);
37 template<typename K> void Erase(const K& key) {
38 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
39 ssKey.reserve(ssKey.GetSerializeSize(key));
41 leveldb::Slice slKey(&ssKey[0], ssKey.size());
50 // custom environment this database is using (may be NULL in case of default environment)
53 // database options used
54 leveldb::Options options;
56 // options used when reading from the database
57 leveldb::ReadOptions readoptions;
59 // options used when iterating over values of the database
60 leveldb::ReadOptions iteroptions;
62 // options used when writing to the database
63 leveldb::WriteOptions writeoptions;
65 // options used when sync writing to the database
66 leveldb::WriteOptions syncoptions;
68 // the database itself
72 CLevelDB(const boost::filesystem::path &path, bool fMemory = false);
75 template<typename K, typename V> bool Read(const K& key, V& value) {
76 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
77 ssKey.reserve(ssKey.GetSerializeSize(key));
79 leveldb::Slice slKey(&ssKey[0], ssKey.size());
82 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
84 if (status.IsNotFound())
86 printf("LevelDB read failure: %s\n", status.ToString().c_str());
89 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
91 } catch(std::exception &e) {
97 template<typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) {
99 batch.Write(key, value);
100 return WriteBatch(batch, fSync);
103 template<typename K> bool Exists(const K& key) {
104 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
105 ssKey.reserve(ssKey.GetSerializeSize(key));
107 leveldb::Slice slKey(&ssKey[0], ssKey.size());
109 std::string strValue;
110 leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
112 if (status.IsNotFound())
114 printf("LevelDB read failure: %s\n", status.ToString().c_str());
119 template<typename K> bool Erase(const K& key, bool fSync = false) {
122 return WriteBatch(batch, fSync);
125 bool WriteBatch(CLevelDBBatch &batch, bool fSync = false);
127 // not available for LevelDB; provide for compatibility with BDB
134 return WriteBatch(batch, true);
137 // not exactly clean encapsulation, but it's easiest for now
138 leveldb::Iterator *NewIterator() {
139 return pdb->NewIterator(iteroptions);
143 #endif // BITCOIN_LEVELDB_H