1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
19 class CDiskBlockIndex;
25 extern unsigned int nWalletDBUpdated;
27 void ThreadFlushWalletDB(void* parg);
28 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
41 mutable CCriticalSection cs_db;
43 std::map<std::string, int> mapFileUseCount;
44 std::map<std::string, Db*> mapDb;
49 bool IsMock() { return fMockDb; }
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.
57 enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
58 VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, 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(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(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 */
99 explicit CDB(const char* pszFile, const char* pszMode="r+");
106 void operator=(const CDB&);
109 template<typename K, typename T>
110 bool Read(const K& key, T& value)
116 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
119 Dbt datKey(&ssKey[0], ssKey.size());
123 datValue.set_flags(DB_DBT_MALLOC);
124 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
125 memset(datKey.get_data(), 0, datKey.get_size());
126 if (datValue.get_data() == NULL)
131 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
134 catch (std::exception &e) {
138 // Clear and free memory
139 memset(datValue.get_data(), 0, datValue.get_size());
140 free(datValue.get_data());
144 template<typename K, typename T>
145 bool Write(const K& key, const T& value, bool fOverwrite=true)
150 assert(!"Write called on database in read-only mode");
153 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
156 Dbt datKey(&ssKey[0], ssKey.size());
159 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
160 ssValue.reserve(10000);
162 Dbt datValue(&ssValue[0], ssValue.size());
165 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
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());
174 bool Erase(const K& key)
179 assert(!"Erase called on database in read-only mode");
182 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
185 Dbt datKey(&ssKey[0], ssKey.size());
188 int ret = pdb->del(activeTxn, &datKey, 0);
191 memset(datKey.get_data(), 0, datKey.get_size());
192 return (ret == 0 || ret == DB_NOTFOUND);
196 bool Exists(const K& key)
202 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
205 Dbt datKey(&ssKey[0], ssKey.size());
208 int ret = pdb->exists(activeTxn, &datKey, 0);
211 memset(datKey.get_data(), 0, datKey.get_size());
220 int ret = pdb->cursor(NULL, &pcursor, 0);
226 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
230 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
232 datKey.set_data(&ssKey[0]);
233 datKey.set_size(ssKey.size());
236 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);
316 /** Access to the (IP) address database (peers.dat) */
320 boost::filesystem::path pathAddr;
323 bool Write(const CAddrMan& addr);
324 bool Read(CAddrMan& addr);
327 #endif // BITCOIN_DB_H