1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_DBWRAPPER_H
6 #define BITCOIN_DBWRAPPER_H
8 #include "clientversion.h"
10 #include "serialize.h"
13 #include "utilstrencodings.h"
16 #include <leveldb/db.h>
17 #include <leveldb/write_batch.h>
19 static const size_t DBWRAPPER_PREALLOC_KEY_SIZE
= 64;
20 static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE
= 1024;
22 class dbwrapper_error
: public std::runtime_error
25 explicit dbwrapper_error(const std::string
& msg
) : std::runtime_error(msg
) {}
30 /** These should be considered an implementation detail of the specific database.
32 namespace dbwrapper_private
{
34 /** Handle database error by throwing dbwrapper_error exception.
36 void HandleError(const leveldb::Status
& status
);
38 /** Work around circular dependency, as well as for testing in dbwrapper_tests.
39 * Database obfuscation should be considered an implementation detail of the
42 const std::vector
<unsigned char>& GetObfuscateKey(const CDBWrapper
&w
);
46 /** Batch of changes queued to be written to a CDBWrapper */
49 friend class CDBWrapper
;
52 const CDBWrapper
&parent
;
53 leveldb::WriteBatch batch
;
62 * @param[in] _parent CDBWrapper that this batch is to be submitted to
64 explicit CDBBatch(const CDBWrapper
&_parent
) : parent(_parent
), ssKey(SER_DISK
, CLIENT_VERSION
), ssValue(SER_DISK
, CLIENT_VERSION
), size_estimate(0) { };
72 template <typename K
, typename V
>
73 void Write(const K
& key
, const V
& value
)
75 ssKey
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
77 leveldb::Slice
slKey(ssKey
.data(), ssKey
.size());
79 ssValue
.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE
);
81 ssValue
.Xor(dbwrapper_private::GetObfuscateKey(parent
));
82 leveldb::Slice
slValue(ssValue
.data(), ssValue
.size());
84 batch
.Put(slKey
, slValue
);
85 // LevelDB serializes writes as:
87 // - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
89 // - varint: value length
91 // The formula below assumes the key and value are both less than 16k.
92 size_estimate
+= 3 + (slKey
.size() > 127) + slKey
.size() + (slValue
.size() > 127) + slValue
.size();
98 void Erase(const K
& key
)
100 ssKey
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
102 leveldb::Slice
slKey(ssKey
.data(), ssKey
.size());
105 // LevelDB serializes erases as:
107 // - varint: key length
109 // The formula below assumes the key is less than 16kB.
110 size_estimate
+= 2 + (slKey
.size() > 127) + slKey
.size();
114 size_t SizeEstimate() const { return size_estimate
; }
120 const CDBWrapper
&parent
;
121 leveldb::Iterator
*piter
;
126 * @param[in] _parent Parent CDBWrapper instance.
127 * @param[in] _piter The original leveldb iterator.
129 CDBIterator(const CDBWrapper
&_parent
, leveldb::Iterator
*_piter
) :
130 parent(_parent
), piter(_piter
) { };
137 template<typename K
> void Seek(const K
& key
) {
138 CDataStream
ssKey(SER_DISK
, CLIENT_VERSION
);
139 ssKey
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
141 leveldb::Slice
slKey(ssKey
.data(), ssKey
.size());
147 template<typename K
> bool GetKey(K
& key
) {
148 leveldb::Slice slKey
= piter
->key();
150 CDataStream
ssKey(slKey
.data(), slKey
.data() + slKey
.size(), SER_DISK
, CLIENT_VERSION
);
152 } catch (const std::exception
&) {
158 template<typename V
> bool GetValue(V
& value
) {
159 leveldb::Slice slValue
= piter
->value();
161 CDataStream
ssValue(slValue
.data(), slValue
.data() + slValue
.size(), SER_DISK
, CLIENT_VERSION
);
162 ssValue
.Xor(dbwrapper_private::GetObfuscateKey(parent
));
164 } catch (const std::exception
&) {
170 unsigned int GetValueSize() {
171 return piter
->value().size();
178 friend const std::vector
<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper
&w
);
180 //! custom environment this database is using (may be nullptr in case of default environment)
183 //! database options used
184 leveldb::Options options
;
186 //! options used when reading from the database
187 leveldb::ReadOptions readoptions
;
189 //! options used when iterating over values of the database
190 leveldb::ReadOptions iteroptions
;
192 //! options used when writing to the database
193 leveldb::WriteOptions writeoptions
;
195 //! options used when sync writing to the database
196 leveldb::WriteOptions syncoptions
;
198 //! the database itself
201 //! a key used for optional XOR-obfuscation of the database
202 std::vector
<unsigned char> obfuscate_key
;
204 //! the key under which the obfuscation key is stored
205 static const std::string OBFUSCATE_KEY_KEY
;
207 //! the length of the obfuscate key in number of bytes
208 static const unsigned int OBFUSCATE_KEY_NUM_BYTES
;
210 std::vector
<unsigned char> CreateObfuscateKey() const;
214 * @param[in] path Location in the filesystem where leveldb data will be stored.
215 * @param[in] nCacheSize Configures various leveldb cache settings.
216 * @param[in] fMemory If true, use leveldb's memory environment.
217 * @param[in] fWipe If true, remove all existing data.
218 * @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR
219 * with a zero'd byte array.
221 CDBWrapper(const fs::path
& path
, size_t nCacheSize
, bool fMemory
= false, bool fWipe
= false, bool obfuscate
= false);
224 template <typename K
, typename V
>
225 bool Read(const K
& key
, V
& value
) const
227 CDataStream
ssKey(SER_DISK
, CLIENT_VERSION
);
228 ssKey
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
230 leveldb::Slice
slKey(ssKey
.data(), ssKey
.size());
232 std::string strValue
;
233 leveldb::Status status
= pdb
->Get(readoptions
, slKey
, &strValue
);
235 if (status
.IsNotFound())
237 LogPrintf("LevelDB read failure: %s\n", status
.ToString());
238 dbwrapper_private::HandleError(status
);
241 CDataStream
ssValue(strValue
.data(), strValue
.data() + strValue
.size(), SER_DISK
, CLIENT_VERSION
);
242 ssValue
.Xor(obfuscate_key
);
244 } catch (const std::exception
&) {
250 template <typename K
, typename V
>
251 bool Write(const K
& key
, const V
& value
, bool fSync
= false)
253 CDBBatch
batch(*this);
254 batch
.Write(key
, value
);
255 return WriteBatch(batch
, fSync
);
258 template <typename K
>
259 bool Exists(const K
& key
) const
261 CDataStream
ssKey(SER_DISK
, CLIENT_VERSION
);
262 ssKey
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
264 leveldb::Slice
slKey(ssKey
.data(), ssKey
.size());
266 std::string strValue
;
267 leveldb::Status status
= pdb
->Get(readoptions
, slKey
, &strValue
);
269 if (status
.IsNotFound())
271 LogPrintf("LevelDB read failure: %s\n", status
.ToString());
272 dbwrapper_private::HandleError(status
);
277 template <typename K
>
278 bool Erase(const K
& key
, bool fSync
= false)
280 CDBBatch
batch(*this);
282 return WriteBatch(batch
, fSync
);
285 bool WriteBatch(CDBBatch
& batch
, bool fSync
= false);
287 // not available for LevelDB; provide for compatibility with BDB
295 CDBBatch
batch(*this);
296 return WriteBatch(batch
, true);
299 CDBIterator
*NewIterator()
301 return new CDBIterator(*this, pdb
->NewIterator(iteroptions
));
305 * Return true if the database managed by this class contains no entries.
310 size_t EstimateSize(const K
& key_begin
, const K
& key_end
) const
312 CDataStream
ssKey1(SER_DISK
, CLIENT_VERSION
), ssKey2(SER_DISK
, CLIENT_VERSION
);
313 ssKey1
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
314 ssKey2
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
317 leveldb::Slice
slKey1(ssKey1
.data(), ssKey1
.size());
318 leveldb::Slice
slKey2(ssKey2
.data(), ssKey2
.size());
320 leveldb::Range
range(slKey1
, slKey2
);
321 pdb
->GetApproximateSizes(&range
, 1, &size
);
326 * Compact a certain range of keys in the database.
329 void CompactRange(const K
& key_begin
, const K
& key_end
) const
331 CDataStream
ssKey1(SER_DISK
, CLIENT_VERSION
), ssKey2(SER_DISK
, CLIENT_VERSION
);
332 ssKey1
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
333 ssKey2
.reserve(DBWRAPPER_PREALLOC_KEY_SIZE
);
336 leveldb::Slice
slKey1(ssKey1
.data(), ssKey1
.size());
337 leveldb::Slice
slKey2(ssKey2
.data(), ssKey2
.size());
338 pdb
->CompactRange(&slKey1
, &slKey2
);
343 #endif // BITCOIN_DBWRAPPER_H