]>
Commit | Line | Data |
---|---|---|
0a61b0df | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
db0e8ccd | 2 | // Copyright (c) 2009-2013 The Bitcoin developers |
0a61b0df | 3 | // Distributed under the MIT/X11 software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
51ed9ec9 | 5 | |
223b6f1b WL |
6 | #ifndef BITCOIN_DB_H |
7 | #define BITCOIN_DB_H | |
8 | ||
336fe971 | 9 | #include "serialize.h" |
51ed9ec9 BD |
10 | #include "sync.h" |
11 | #include "version.h" | |
223b6f1b WL |
12 | |
13 | #include <map> | |
14 | #include <string> | |
15 | #include <vector> | |
16 | ||
51ed9ec9 | 17 | #include <boost/filesystem/path.hpp> |
611116d4 | 18 | |
223b6f1b | 19 | #include <db_cxx.h> |
0a61b0df | 20 | |
0a61b0df | 21 | class CDiskBlockIndex; |
0a61b0df | 22 | class COutPoint; |
0a61b0df | 23 | |
611116d4 PK |
24 | struct CBlockLocator; |
25 | ||
0a61b0df | 26 | extern unsigned int nWalletDBUpdated; |
0a61b0df | 27 | |
b31499ec | 28 | void ThreadFlushWalletDB(const std::string& strWalletFile); |
0a61b0df | 29 | |
30 | ||
cd9696fc JG |
31 | class CDBEnv |
32 | { | |
33 | private: | |
cd9696fc | 34 | bool fDbEnvInit; |
148e107d | 35 | bool fMockDb; |
ccda03b5 | 36 | boost::filesystem::path path; |
cd9696fc JG |
37 | |
38 | void EnvShutdown(); | |
39 | ||
40 | public: | |
41 | mutable CCriticalSection cs_db; | |
42 | DbEnv dbenv; | |
ffe8b77a JG |
43 | std::map<std::string, int> mapFileUseCount; |
44 | std::map<std::string, Db*> mapDb; | |
cd9696fc JG |
45 | |
46 | CDBEnv(); | |
47 | ~CDBEnv(); | |
148e107d | 48 | void MakeMock(); |
c74bae0f | 49 | bool IsMock() { return fMockDb; } |
eed1785f GA |
50 | |
51 | /* | |
52 | * Verify that database file strFile is OK. If it is not, | |
53 | * call the callback to try to recover. | |
54 | * This must be called BEFORE strFile is opened. | |
55 | * Returns true if strFile is OK. | |
56 | */ | |
57 | enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL }; | |
58 | VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile)); | |
59 | /* | |
60 | * Salvage data from a file that Verify says is bad. | |
61 | * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation). | |
62 | * Appends binary key/value pairs to vResult, returns true if successful. | |
63 | * NOTE: reads the entire database into memory, so cannot be used | |
64 | * for huge databases. | |
65 | */ | |
66 | typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair; | |
67 | bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult); | |
68 | ||
c74bae0f | 69 | bool Open(const boost::filesystem::path &path); |
cd9696fc JG |
70 | void Close(); |
71 | void Flush(bool fShutdown); | |
72 | void CheckpointLSN(std::string strFile); | |
24b57e3c | 73 | |
ffe8b77a | 74 | void CloseDb(const std::string& strFile); |
eed1785f | 75 | bool RemoveDb(const std::string& strFile); |
ffe8b77a | 76 | |
8b1202c5 | 77 | DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC) |
24b57e3c JG |
78 | { |
79 | DbTxn* ptxn = NULL; | |
8b1202c5 | 80 | int ret = dbenv.txn_begin(NULL, &ptxn, flags); |
24b57e3c JG |
81 | if (!ptxn || ret != 0) |
82 | return NULL; | |
83 | return ptxn; | |
84 | } | |
cd9696fc JG |
85 | }; |
86 | ||
87 | extern CDBEnv bitdb; | |
88 | ||
89 | ||
6b8de05d | 90 | /** RAII class that provides access to a Berkeley database */ |
0a61b0df | 91 | class CDB |
92 | { | |
93 | protected: | |
94 | Db* pdb; | |
223b6f1b | 95 | std::string strFile; |
8b1202c5 | 96 | DbTxn *activeTxn; |
0a61b0df | 97 | bool fReadOnly; |
98 | ||
99 | explicit CDB(const char* pszFile, const char* pszMode="r+"); | |
100 | ~CDB() { Close(); } | |
101 | public: | |
ae8bfd12 | 102 | void Flush(); |
0a61b0df | 103 | void Close(); |
104 | private: | |
105 | CDB(const CDB&); | |
106 | void operator=(const CDB&); | |
107 | ||
108 | protected: | |
109 | template<typename K, typename T> | |
110 | bool Read(const K& key, T& value) | |
111 | { | |
112 | if (!pdb) | |
113 | return false; | |
114 | ||
115 | // Key | |
6b6aaa16 | 116 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
0a61b0df | 117 | ssKey.reserve(1000); |
118 | ssKey << key; | |
119 | Dbt datKey(&ssKey[0], ssKey.size()); | |
120 | ||
121 | // Read | |
122 | Dbt datValue; | |
123 | datValue.set_flags(DB_DBT_MALLOC); | |
8b1202c5 | 124 | int ret = pdb->get(activeTxn, &datKey, &datValue, 0); |
0a61b0df | 125 | memset(datKey.get_data(), 0, datKey.get_size()); |
126 | if (datValue.get_data() == NULL) | |
127 | return false; | |
128 | ||
129 | // Unserialize value | |
f94b64c2 JG |
130 | try { |
131 | CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION); | |
132 | ssValue >> value; | |
133 | } | |
ec91092d | 134 | catch (const std::exception &) { |
f94b64c2 JG |
135 | return false; |
136 | } | |
0a61b0df | 137 | |
138 | // Clear and free memory | |
139 | memset(datValue.get_data(), 0, datValue.get_size()); | |
140 | free(datValue.get_data()); | |
141 | return (ret == 0); | |
142 | } | |
143 | ||
144 | template<typename K, typename T> | |
145 | bool Write(const K& key, const T& value, bool fOverwrite=true) | |
146 | { | |
147 | if (!pdb) | |
148 | return false; | |
149 | if (fReadOnly) | |
ecf1c79a | 150 | assert(!"Write called on database in read-only mode"); |
0a61b0df | 151 | |
152 | // Key | |
6b6aaa16 | 153 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
0a61b0df | 154 | ssKey.reserve(1000); |
155 | ssKey << key; | |
156 | Dbt datKey(&ssKey[0], ssKey.size()); | |
157 | ||
158 | // Value | |
6b6aaa16 | 159 | CDataStream ssValue(SER_DISK, CLIENT_VERSION); |
0a61b0df | 160 | ssValue.reserve(10000); |
161 | ssValue << value; | |
162 | Dbt datValue(&ssValue[0], ssValue.size()); | |
163 | ||
164 | // Write | |
8b1202c5 | 165 | int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE)); |
0a61b0df | 166 | |
167 | // Clear memory in case it was a private key | |
168 | memset(datKey.get_data(), 0, datKey.get_size()); | |
169 | memset(datValue.get_data(), 0, datValue.get_size()); | |
170 | return (ret == 0); | |
171 | } | |
172 | ||
173 | template<typename K> | |
174 | bool Erase(const K& key) | |
175 | { | |
176 | if (!pdb) | |
177 | return false; | |
178 | if (fReadOnly) | |
ecf1c79a | 179 | assert(!"Erase called on database in read-only mode"); |
0a61b0df | 180 | |
181 | // Key | |
6b6aaa16 | 182 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
0a61b0df | 183 | ssKey.reserve(1000); |
184 | ssKey << key; | |
185 | Dbt datKey(&ssKey[0], ssKey.size()); | |
186 | ||
187 | // Erase | |
8b1202c5 | 188 | int ret = pdb->del(activeTxn, &datKey, 0); |
0a61b0df | 189 | |
190 | // Clear memory | |
191 | memset(datKey.get_data(), 0, datKey.get_size()); | |
192 | return (ret == 0 || ret == DB_NOTFOUND); | |
193 | } | |
194 | ||
195 | template<typename K> | |
196 | bool Exists(const K& key) | |
197 | { | |
198 | if (!pdb) | |
199 | return false; | |
200 | ||
201 | // Key | |
6b6aaa16 | 202 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
0a61b0df | 203 | ssKey.reserve(1000); |
204 | ssKey << key; | |
205 | Dbt datKey(&ssKey[0], ssKey.size()); | |
206 | ||
207 | // Exists | |
8b1202c5 | 208 | int ret = pdb->exists(activeTxn, &datKey, 0); |
0a61b0df | 209 | |
210 | // Clear memory | |
211 | memset(datKey.get_data(), 0, datKey.get_size()); | |
212 | return (ret == 0); | |
213 | } | |
214 | ||
215 | Dbc* GetCursor() | |
216 | { | |
217 | if (!pdb) | |
218 | return NULL; | |
219 | Dbc* pcursor = NULL; | |
220 | int ret = pdb->cursor(NULL, &pcursor, 0); | |
221 | if (ret != 0) | |
222 | return NULL; | |
223 | return pcursor; | |
224 | } | |
225 | ||
226 | int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT) | |
227 | { | |
228 | // Read at cursor | |
229 | Dbt datKey; | |
230 | if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) | |
231 | { | |
232 | datKey.set_data(&ssKey[0]); | |
233 | datKey.set_size(ssKey.size()); | |
234 | } | |
235 | Dbt datValue; | |
236 | if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) | |
237 | { | |
238 | datValue.set_data(&ssValue[0]); | |
239 | datValue.set_size(ssValue.size()); | |
240 | } | |
241 | datKey.set_flags(DB_DBT_MALLOC); | |
242 | datValue.set_flags(DB_DBT_MALLOC); | |
243 | int ret = pcursor->get(&datKey, &datValue, fFlags); | |
244 | if (ret != 0) | |
245 | return ret; | |
246 | else if (datKey.get_data() == NULL || datValue.get_data() == NULL) | |
247 | return 99999; | |
248 | ||
249 | // Convert to streams | |
250 | ssKey.SetType(SER_DISK); | |
251 | ssKey.clear(); | |
252 | ssKey.write((char*)datKey.get_data(), datKey.get_size()); | |
253 | ssValue.SetType(SER_DISK); | |
254 | ssValue.clear(); | |
255 | ssValue.write((char*)datValue.get_data(), datValue.get_size()); | |
256 | ||
257 | // Clear and free memory | |
258 | memset(datKey.get_data(), 0, datKey.get_size()); | |
259 | memset(datValue.get_data(), 0, datValue.get_size()); | |
260 | free(datKey.get_data()); | |
261 | free(datValue.get_data()); | |
262 | return 0; | |
263 | } | |
264 | ||
0a61b0df | 265 | public: |
266 | bool TxnBegin() | |
267 | { | |
8b1202c5 | 268 | if (!pdb || activeTxn) |
0a61b0df | 269 | return false; |
8b1202c5 | 270 | DbTxn* ptxn = bitdb.TxnBegin(); |
24b57e3c | 271 | if (!ptxn) |
0a61b0df | 272 | return false; |
8b1202c5 | 273 | activeTxn = ptxn; |
0a61b0df | 274 | return true; |
275 | } | |
276 | ||
277 | bool TxnCommit() | |
278 | { | |
8b1202c5 | 279 | if (!pdb || !activeTxn) |
0a61b0df | 280 | return false; |
8b1202c5 JG |
281 | int ret = activeTxn->commit(0); |
282 | activeTxn = NULL; | |
0a61b0df | 283 | return (ret == 0); |
284 | } | |
285 | ||
286 | bool TxnAbort() | |
287 | { | |
8b1202c5 | 288 | if (!pdb || !activeTxn) |
0a61b0df | 289 | return false; |
8b1202c5 JG |
290 | int ret = activeTxn->abort(); |
291 | activeTxn = NULL; | |
0a61b0df | 292 | return (ret == 0); |
293 | } | |
294 | ||
295 | bool ReadVersion(int& nVersion) | |
296 | { | |
297 | nVersion = 0; | |
223b6f1b | 298 | return Read(std::string("version"), nVersion); |
0a61b0df | 299 | } |
300 | ||
301 | bool WriteVersion(int nVersion) | |
302 | { | |
223b6f1b | 303 | return Write(std::string("version"), nVersion); |
0a61b0df | 304 | } |
9e9869d0 | 305 | |
d764d916 | 306 | bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL); |
0a61b0df | 307 | }; |
308 | ||
9eace6b1 | 309 | #endif // BITCOIN_DB_H |