| 1 | // Copyright (c) 2012-2014 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. |
| 4 | |
| 5 | #ifndef BITCOIN_DBWRAPPER_H |
| 6 | #define BITCOIN_DBWRAPPER_H |
| 7 | |
| 8 | #include "clientversion.h" |
| 9 | #include "serialize.h" |
| 10 | #include "streams.h" |
| 11 | #include "util.h" |
| 12 | #include "version.h" |
| 13 | |
| 14 | #include <boost/filesystem/path.hpp> |
| 15 | |
| 16 | #include <leveldb/db.h> |
| 17 | #include <leveldb/write_batch.h> |
| 18 | |
| 19 | class dbwrapper_error : public std::runtime_error |
| 20 | { |
| 21 | public: |
| 22 | dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {} |
| 23 | }; |
| 24 | |
| 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 | */ |
| 33 | void HandleError(const leveldb::Status& status); |
| 34 | |
| 35 | }; |
| 36 | |
| 37 | /** Batch of changes queued to be written to a CDBWrapper */ |
| 38 | class CDBBatch |
| 39 | { |
| 40 | friend class CDBWrapper; |
| 41 | |
| 42 | private: |
| 43 | const CDBWrapper &parent; |
| 44 | leveldb::WriteBatch batch; |
| 45 | |
| 46 | public: |
| 47 | /** |
| 48 | * @param[in] _parent CDBWrapper that this batch is to be submitted to |
| 49 | */ |
| 50 | CDBBatch(const CDBWrapper &_parent) : parent(_parent) { }; |
| 51 | |
| 52 | template <typename K, typename V> |
| 53 | void Write(const K& key, const V& value) |
| 54 | { |
| 55 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
| 56 | ssKey.reserve(GetSerializeSize(ssKey, key)); |
| 57 | ssKey << key; |
| 58 | leveldb::Slice slKey(&ssKey[0], ssKey.size()); |
| 59 | |
| 60 | CDataStream ssValue(SER_DISK, CLIENT_VERSION); |
| 61 | ssValue.reserve(GetSerializeSize(ssValue, value)); |
| 62 | ssValue << value; |
| 63 | leveldb::Slice slValue(&ssValue[0], ssValue.size()); |
| 64 | |
| 65 | batch.Put(slKey, slValue); |
| 66 | } |
| 67 | |
| 68 | template <typename K> |
| 69 | void Erase(const K& key) |
| 70 | { |
| 71 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
| 72 | ssKey.reserve(GetSerializeSize(ssKey, key)); |
| 73 | ssKey << key; |
| 74 | leveldb::Slice slKey(&ssKey[0], ssKey.size()); |
| 75 | |
| 76 | batch.Delete(slKey); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | class CDBIterator |
| 81 | { |
| 82 | private: |
| 83 | const CDBWrapper &parent; |
| 84 | leveldb::Iterator *piter; |
| 85 | |
| 86 | public: |
| 87 | |
| 88 | /** |
| 89 | * @param[in] _parent Parent CDBWrapper instance. |
| 90 | * @param[in] _piter The original leveldb iterator. |
| 91 | */ |
| 92 | CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter) : |
| 93 | parent(_parent), piter(_piter) { }; |
| 94 | ~CDBIterator(); |
| 95 | |
| 96 | bool Valid(); |
| 97 | |
| 98 | void SeekToFirst(); |
| 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(); |
| 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 | }; |
| 141 | |
| 142 | class CDBWrapper |
| 143 | { |
| 144 | private: |
| 145 | //! custom environment this database is using (may be NULL in case of default environment) |
| 146 | leveldb::Env* penv; |
| 147 | |
| 148 | //! database options used |
| 149 | leveldb::Options options; |
| 150 | |
| 151 | //! options used when reading from the database |
| 152 | leveldb::ReadOptions readoptions; |
| 153 | |
| 154 | //! options used when iterating over values of the database |
| 155 | leveldb::ReadOptions iteroptions; |
| 156 | |
| 157 | //! options used when writing to the database |
| 158 | leveldb::WriteOptions writeoptions; |
| 159 | |
| 160 | //! options used when sync writing to the database |
| 161 | leveldb::WriteOptions syncoptions; |
| 162 | |
| 163 | //! the database itself |
| 164 | leveldb::DB* pdb; |
| 165 | |
| 166 | public: |
| 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 | */ |
| 173 | CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false); |
| 174 | ~CDBWrapper(); |
| 175 | |
| 176 | template <typename K, typename V> |
| 177 | bool Read(const K& key, V& value) const |
| 178 | { |
| 179 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
| 180 | ssKey.reserve(GetSerializeSize(ssKey, key)); |
| 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; |
| 189 | LogPrintf("LevelDB read failure: %s\n", status.ToString()); |
| 190 | dbwrapper_private::HandleError(status); |
| 191 | } |
| 192 | try { |
| 193 | CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION); |
| 194 | ssValue >> value; |
| 195 | } catch (const std::exception&) { |
| 196 | return false; |
| 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | template <typename K, typename V> |
| 202 | bool Write(const K& key, const V& value, bool fSync = false) |
| 203 | { |
| 204 | CDBBatch batch(*this); |
| 205 | batch.Write(key, value); |
| 206 | return WriteBatch(batch, fSync); |
| 207 | } |
| 208 | |
| 209 | template <typename K> |
| 210 | bool Exists(const K& key) const |
| 211 | { |
| 212 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
| 213 | ssKey.reserve(GetSerializeSize(ssKey, key)); |
| 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; |
| 222 | LogPrintf("LevelDB read failure: %s\n", status.ToString()); |
| 223 | dbwrapper_private::HandleError(status); |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | template <typename K> |
| 229 | bool Erase(const K& key, bool fSync = false) |
| 230 | { |
| 231 | CDBBatch batch(*this); |
| 232 | batch.Erase(key); |
| 233 | return WriteBatch(batch, fSync); |
| 234 | } |
| 235 | |
| 236 | bool WriteBatch(CDBBatch& batch, bool fSync = false); |
| 237 | |
| 238 | // not available for LevelDB; provide for compatibility with BDB |
| 239 | bool Flush() |
| 240 | { |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | bool Sync() |
| 245 | { |
| 246 | CDBBatch batch(*this); |
| 247 | return WriteBatch(batch, true); |
| 248 | } |
| 249 | |
| 250 | CDBIterator *NewIterator() |
| 251 | { |
| 252 | return new CDBIterator(*this, pdb->NewIterator(iteroptions)); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Return true if the database managed by this class contains no entries. |
| 257 | */ |
| 258 | bool IsEmpty(); |
| 259 | }; |
| 260 | |
| 261 | #endif // BITCOIN_DBWRAPPER_H |
| 262 | |