]>
Commit | Line | Data |
---|---|---|
f914f1a7 | 1 | // Copyright (c) 2012-2014 The Bitcoin Core developers |
fa94b9d5 | 2 | // Distributed under the MIT software license, see the accompanying |
43b7905e | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
b64187d0 | 4 | |
e3da7a57 JG |
5 | #ifndef BITCOIN_DBWRAPPER_H |
6 | #define BITCOIN_DBWRAPPER_H | |
43b7905e | 7 | |
71697f97 | 8 | #include "clientversion.h" |
43b7905e | 9 | #include "serialize.h" |
fa736190 | 10 | #include "streams.h" |
881a85a2 | 11 | #include "util.h" |
51ed9ec9 | 12 | #include "version.h" |
43b7905e | 13 | |
51ed9ec9 | 14 | #include <boost/filesystem/path.hpp> |
611116d4 | 15 | |
43b7905e PW |
16 | #include <leveldb/db.h> |
17 | #include <leveldb/write_batch.h> | |
18 | ||
f345c41e | 19 | class dbwrapper_error : public std::runtime_error |
421218d3 PW |
20 | { |
21 | public: | |
f345c41e | 22 | dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {} |
421218d3 PW |
23 | }; |
24 | ||
3923bcca WL |
25 | class CDBWrapper; |
26 | ||
27 | /** These should be considered an implementation detail of the specific database. | |
28 | */ | |
29 | namespace dbwrapper_private { | |
30 | ||
31 | /** Handle database error by throwing dbwrapper_error exception. | |
32 | */ | |
ecd04e91 | 33 | void HandleError(const leveldb::Status& status); |
421218d3 | 34 | |
3923bcca | 35 | }; |
809a429e | 36 | |
f345c41e JG |
37 | /** Batch of changes queued to be written to a CDBWrapper */ |
38 | class CDBBatch | |
43b7905e | 39 | { |
f345c41e | 40 | friend class CDBWrapper; |
43b7905e PW |
41 | |
42 | private: | |
809a429e | 43 | const CDBWrapper &parent; |
43b7905e PW |
44 | leveldb::WriteBatch batch; |
45 | ||
46 | public: | |
809a429e | 47 | /** |
56aa2568 | 48 | * @param[in] _parent CDBWrapper that this batch is to be submitted to |
809a429e | 49 | */ |
56aa2568 | 50 | CDBBatch(const CDBWrapper &_parent) : parent(_parent) { }; |
809a429e | 51 | |
20e01b1a PW |
52 | template <typename K, typename V> |
53 | void Write(const K& key, const V& value) | |
54 | { | |
43b7905e | 55 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
b8a65793 | 56 | ssKey.reserve(GetSerializeSize(ssKey, key)); |
43b7905e PW |
57 | ssKey << key; |
58 | leveldb::Slice slKey(&ssKey[0], ssKey.size()); | |
59 | ||
60 | CDataStream ssValue(SER_DISK, CLIENT_VERSION); | |
b8a65793 | 61 | ssValue.reserve(GetSerializeSize(ssValue, value)); |
43b7905e PW |
62 | ssValue << value; |
63 | leveldb::Slice slValue(&ssValue[0], ssValue.size()); | |
64 | ||
65 | batch.Put(slKey, slValue); | |
66 | } | |
67 | ||
20e01b1a PW |
68 | template <typename K> |
69 | void Erase(const K& key) | |
70 | { | |
43b7905e | 71 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
b8a65793 | 72 | ssKey.reserve(GetSerializeSize(ssKey, key)); |
43b7905e PW |
73 | ssKey << key; |
74 | leveldb::Slice slKey(&ssKey[0], ssKey.size()); | |
75 | ||
76 | batch.Delete(slKey); | |
77 | } | |
78 | }; | |
7249ee6d | 79 | |
f345c41e | 80 | class CDBIterator |
1ebf50b6 PW |
81 | { |
82 | private: | |
809a429e | 83 | const CDBWrapper &parent; |
1ebf50b6 PW |
84 | leveldb::Iterator *piter; |
85 | ||
86 | public: | |
0d9524ba JB |
87 | |
88 | /** | |
56aa2568 PJ |
89 | * @param[in] _parent Parent CDBWrapper instance. |
90 | * @param[in] _piter The original leveldb iterator. | |
0d9524ba | 91 | */ |
56aa2568 PJ |
92 | CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter) : |
93 | parent(_parent), piter(_piter) { }; | |
f345c41e | 94 | ~CDBIterator(); |
1ebf50b6 PW |
95 | |
96 | bool Valid(); | |
43b7905e | 97 | |
1ebf50b6 | 98 | void SeekToFirst(); |
1ebf50b6 PW |
99 | |
100 | template<typename K> void Seek(const K& key) { | |
101 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); | |
102 | ssKey.reserve(ssKey.GetSerializeSize(key)); | |
103 | ssKey << key; | |
104 | leveldb::Slice slKey(&ssKey[0], ssKey.size()); | |
105 | piter->Seek(slKey); | |
106 | } | |
107 | ||
108 | void Next(); | |
1ebf50b6 PW |
109 | |
110 | template<typename K> bool GetKey(K& key) { | |
111 | leveldb::Slice slKey = piter->key(); | |
112 | try { | |
113 | CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION); | |
114 | ssKey >> key; | |
115 | } catch(std::exception &e) { | |
116 | return false; | |
117 | } | |
118 | return true; | |
119 | } | |
120 | ||
121 | unsigned int GetKeySize() { | |
122 | return piter->key().size(); | |
123 | } | |
124 | ||
125 | template<typename V> bool GetValue(V& value) { | |
126 | leveldb::Slice slValue = piter->value(); | |
127 | try { | |
128 | CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION); | |
129 | ssValue >> value; | |
130 | } catch(std::exception &e) { | |
131 | return false; | |
132 | } | |
133 | return true; | |
134 | } | |
135 | ||
136 | unsigned int GetValueSize() { | |
137 | return piter->value().size(); | |
138 | } | |
139 | ||
140 | }; | |
7249ee6d | 141 | |
f345c41e | 142 | class CDBWrapper |
43b7905e PW |
143 | { |
144 | private: | |
fa94b9d5 | 145 | //! custom environment this database is using (may be NULL in case of default environment) |
20e01b1a | 146 | leveldb::Env* penv; |
43b7905e | 147 | |
fa94b9d5 | 148 | //! database options used |
43b7905e PW |
149 | leveldb::Options options; |
150 | ||
fa94b9d5 | 151 | //! options used when reading from the database |
43b7905e PW |
152 | leveldb::ReadOptions readoptions; |
153 | ||
fa94b9d5 | 154 | //! options used when iterating over values of the database |
43b7905e PW |
155 | leveldb::ReadOptions iteroptions; |
156 | ||
fa94b9d5 | 157 | //! options used when writing to the database |
43b7905e PW |
158 | leveldb::WriteOptions writeoptions; |
159 | ||
fa94b9d5 | 160 | //! options used when sync writing to the database |
43b7905e PW |
161 | leveldb::WriteOptions syncoptions; |
162 | ||
fa94b9d5 | 163 | //! the database itself |
20e01b1a | 164 | leveldb::DB* pdb; |
43b7905e PW |
165 | |
166 | public: | |
0d81464b JB |
167 | /** |
168 | * @param[in] path Location in the filesystem where leveldb data will be stored. | |
169 | * @param[in] nCacheSize Configures various leveldb cache settings. | |
170 | * @param[in] fMemory If true, use leveldb's memory environment. | |
171 | * @param[in] fWipe If true, remove all existing data. | |
172 | */ | |
f345c41e JG |
173 | CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false); |
174 | ~CDBWrapper(); | |
43b7905e | 175 | |
20e01b1a | 176 | template <typename K, typename V> |
ecd04e91 | 177 | bool Read(const K& key, V& value) const |
20e01b1a | 178 | { |
43b7905e | 179 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
b8a65793 | 180 | ssKey.reserve(GetSerializeSize(ssKey, key)); |
43b7905e PW |
181 | ssKey << key; |
182 | leveldb::Slice slKey(&ssKey[0], ssKey.size()); | |
183 | ||
184 | std::string strValue; | |
185 | leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); | |
186 | if (!status.ok()) { | |
187 | if (status.IsNotFound()) | |
188 | return false; | |
79d06dc6 | 189 | LogPrintf("LevelDB read failure: %s\n", status.ToString()); |
3923bcca | 190 | dbwrapper_private::HandleError(status); |
43b7905e PW |
191 | } |
192 | try { | |
193 | CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION); | |
194 | ssValue >> value; | |
20e01b1a | 195 | } catch (const std::exception&) { |
43b7905e PW |
196 | return false; |
197 | } | |
198 | return true; | |
199 | } | |
200 | ||
20e01b1a | 201 | template <typename K, typename V> |
ecd04e91 | 202 | bool Write(const K& key, const V& value, bool fSync = false) |
20e01b1a | 203 | { |
809a429e | 204 | CDBBatch batch(*this); |
43b7905e PW |
205 | batch.Write(key, value); |
206 | return WriteBatch(batch, fSync); | |
207 | } | |
208 | ||
20e01b1a | 209 | template <typename K> |
ecd04e91 | 210 | bool Exists(const K& key) const |
20e01b1a | 211 | { |
43b7905e | 212 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
b8a65793 | 213 | ssKey.reserve(GetSerializeSize(ssKey, key)); |
43b7905e PW |
214 | ssKey << key; |
215 | leveldb::Slice slKey(&ssKey[0], ssKey.size()); | |
216 | ||
217 | std::string strValue; | |
218 | leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); | |
219 | if (!status.ok()) { | |
220 | if (status.IsNotFound()) | |
221 | return false; | |
79d06dc6 | 222 | LogPrintf("LevelDB read failure: %s\n", status.ToString()); |
3923bcca | 223 | dbwrapper_private::HandleError(status); |
43b7905e PW |
224 | } |
225 | return true; | |
226 | } | |
227 | ||
20e01b1a | 228 | template <typename K> |
ecd04e91 | 229 | bool Erase(const K& key, bool fSync = false) |
20e01b1a | 230 | { |
809a429e | 231 | CDBBatch batch(*this); |
43b7905e PW |
232 | batch.Erase(key); |
233 | return WriteBatch(batch, fSync); | |
234 | } | |
235 | ||
f345c41e | 236 | bool WriteBatch(CDBBatch& batch, bool fSync = false); |
43b7905e PW |
237 | |
238 | // not available for LevelDB; provide for compatibility with BDB | |
20e01b1a PW |
239 | bool Flush() |
240 | { | |
43b7905e PW |
241 | return true; |
242 | } | |
243 | ||
ecd04e91 | 244 | bool Sync() |
20e01b1a | 245 | { |
809a429e | 246 | CDBBatch batch(*this); |
43b7905e PW |
247 | return WriteBatch(batch, true); |
248 | } | |
249 | ||
f345c41e | 250 | CDBIterator *NewIterator() |
1ebf50b6 | 251 | { |
809a429e | 252 | return new CDBIterator(*this, pdb->NewIterator(iteroptions)); |
0d9524ba | 253 | } |
0d81464b JB |
254 | |
255 | /** | |
256 | * Return true if the database managed by this class contains no entries. | |
257 | */ | |
258 | bool IsEmpty(); | |
43b7905e PW |
259 | }; |
260 | ||
e3da7a57 | 261 | #endif // BITCOIN_DBWRAPPER_H |
0d81464b | 262 |