1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_WALLET_DB_H
7 #define BITCOIN_WALLET_DB_H
9 #include "clientversion.h"
10 #include "serialize.h"
19 #include <boost/filesystem/path.hpp>
23 extern unsigned int nWalletDBUpdated;
30 // Don't change into boost::filesystem::path, as that can result in
31 // shutdown problems/crashes caused by a static initialized internal pointer.
37 mutable CCriticalSection cs_db;
39 std::map<std::string, int> mapFileUseCount;
40 std::map<std::string, Db*> mapDb;
47 bool IsMock() { return fMockDb; }
50 * Verify that database file strFile is OK. If it is not,
51 * call the callback to try to recover.
52 * This must be called BEFORE strFile is opened.
53 * Returns true if strFile is OK.
55 enum VerifyResult { VERIFY_OK,
58 VerifyResult Verify(const std::string& strFile, bool (*recoverFunc)(CDBEnv& dbenv, const std::string& strFile));
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
66 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
67 bool Salvage(const std::string& strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
69 bool Open(const boost::filesystem::path& path);
71 void Flush(bool fShutdown);
72 void CheckpointLSN(const std::string& strFile);
74 void CloseDb(const std::string& strFile);
75 bool RemoveDb(const std::string& strFile);
77 DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
80 int ret = dbenv->txn_begin(NULL, &ptxn, flags);
81 if (!ptxn || ret != 0)
90 /** RAII class that provides access to a Berkeley database */
100 explicit CDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
109 void operator=(const CDB&);
112 template <typename K, typename T>
113 bool Read(const K& key, T& value)
119 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
122 Dbt datKey(&ssKey[0], ssKey.size());
126 datValue.set_flags(DB_DBT_MALLOC);
127 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
128 memset(datKey.get_data(), 0, datKey.get_size());
129 if (datValue.get_data() == NULL)
134 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
136 } catch (const std::exception&) {
140 // Clear and free memory
141 memset(datValue.get_data(), 0, datValue.get_size());
142 free(datValue.get_data());
146 template <typename K, typename T>
147 bool Write(const K& key, const T& value, bool fOverwrite = true)
152 assert(!"Write called on database in read-only mode");
155 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
158 Dbt datKey(&ssKey[0], ssKey.size());
161 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
162 ssValue.reserve(10000);
164 Dbt datValue(&ssValue[0], ssValue.size());
167 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
169 // Clear memory in case it was a private key
170 memset(datKey.get_data(), 0, datKey.get_size());
171 memset(datValue.get_data(), 0, datValue.get_size());
175 template <typename K>
176 bool Erase(const K& key)
181 assert(!"Erase called on database in read-only mode");
184 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
187 Dbt datKey(&ssKey[0], ssKey.size());
190 int ret = pdb->del(activeTxn, &datKey, 0);
193 memset(datKey.get_data(), 0, datKey.get_size());
194 return (ret == 0 || ret == DB_NOTFOUND);
197 template <typename K>
198 bool Exists(const K& key)
204 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
207 Dbt datKey(&ssKey[0], ssKey.size());
210 int ret = pdb->exists(activeTxn, &datKey, 0);
213 memset(datKey.get_data(), 0, datKey.get_size());
222 int ret = pdb->cursor(NULL, &pcursor, 0);
228 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags = DB_NEXT)
232 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) {
233 datKey.set_data(&ssKey[0]);
234 datKey.set_size(ssKey.size());
237 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) {
238 datValue.set_data(&ssValue[0]);
239 datValue.set_size(ssValue.size());
241 datKey.set_flags(DB_DBT_MALLOC);
242 datValue.set_flags(DB_DBT_MALLOC);
243 int ret = pcursor->get(&datKey, &datValue, fFlags);
246 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
249 // Convert to streams
250 ssKey.SetType(SER_DISK);
252 ssKey.write((char*)datKey.get_data(), datKey.get_size());
253 ssValue.SetType(SER_DISK);
255 ssValue.write((char*)datValue.get_data(), datValue.get_size());
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());
268 if (!pdb || activeTxn)
270 DbTxn* ptxn = bitdb.TxnBegin();
279 if (!pdb || !activeTxn)
281 int ret = activeTxn->commit(0);
288 if (!pdb || !activeTxn)
290 int ret = activeTxn->abort();
295 bool ReadVersion(int& nVersion)
298 return Read(std::string("version"), nVersion);
301 bool WriteVersion(int nVersion)
303 return Write(std::string("version"), nVersion);
306 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
309 #endif // BITCOIN_WALLET_DB_H