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);
37 boost::filesystem::path pathEnv;
42 mutable CCriticalSection cs_db;
44 std::map<std::string, int> mapFileUseCount;
45 std::map<std::string, Db*> mapDb;
50 bool IsMock() { return fMockDb; };
53 * Verify that database file strFile is OK. If it is not,
54 * call the callback to try to recover.
55 * This must be called BEFORE strFile is opened.
56 * Returns true if strFile is OK.
58 enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
59 VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
61 * Salvage data from a file that Verify says is bad.
62 * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
63 * Appends binary key/value pairs to vResult, returns true if successful.
64 * NOTE: reads the entire database into memory, so cannot be used
67 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
68 bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
70 bool Open(boost::filesystem::path pathEnv_);
72 void Flush(bool fShutdown);
73 void CheckpointLSN(std::string strFile);
74 void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
75 bool GetDetach() { return fDetachDB; }
77 void CloseDb(const std::string& strFile);
78 bool RemoveDb(const std::string& strFile);
80 DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
83 int ret = dbenv.txn_begin(NULL, &ptxn, flags);
84 if (!ptxn || ret != 0)
93 /** RAII class that provides access to a Berkeley database */
102 explicit CDB(const char* pszFile, const char* pszMode="r+");
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);
137 catch (std::exception &e) {
141 // Clear and free memory
142 memset(datValue.get_data(), 0, datValue.get_size());
143 free(datValue.get_data());
147 template<typename K, typename T>
148 bool Write(const K& key, const T& value, bool fOverwrite=true)
153 assert(!"Write called on database in read-only mode");
156 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
159 Dbt datKey(&ssKey[0], ssKey.size());
162 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
163 ssValue.reserve(10000);
165 Dbt datValue(&ssValue[0], ssValue.size());
168 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
170 // Clear memory in case it was a private key
171 memset(datKey.get_data(), 0, datKey.get_size());
172 memset(datValue.get_data(), 0, datValue.get_size());
177 bool Erase(const K& key)
182 assert(!"Erase called on database in read-only mode");
185 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
188 Dbt datKey(&ssKey[0], ssKey.size());
191 int ret = pdb->del(activeTxn, &datKey, 0);
194 memset(datKey.get_data(), 0, datKey.get_size());
195 return (ret == 0 || ret == DB_NOTFOUND);
199 bool Exists(const K& key)
205 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
208 Dbt datKey(&ssKey[0], ssKey.size());
211 int ret = pdb->exists(activeTxn, &datKey, 0);
214 memset(datKey.get_data(), 0, datKey.get_size());
223 int ret = pdb->cursor(NULL, &pcursor, 0);
229 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
233 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
235 datKey.set_data(&ssKey[0]);
236 datKey.set_size(ssKey.size());
239 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
241 datValue.set_data(&ssValue[0]);
242 datValue.set_size(ssValue.size());
244 datKey.set_flags(DB_DBT_MALLOC);
245 datValue.set_flags(DB_DBT_MALLOC);
246 int ret = pcursor->get(&datKey, &datValue, fFlags);
249 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
252 // Convert to streams
253 ssKey.SetType(SER_DISK);
255 ssKey.write((char*)datKey.get_data(), datKey.get_size());
256 ssValue.SetType(SER_DISK);
258 ssValue.write((char*)datValue.get_data(), datValue.get_size());
260 // Clear and free memory
261 memset(datKey.get_data(), 0, datKey.get_size());
262 memset(datValue.get_data(), 0, datValue.get_size());
263 free(datKey.get_data());
264 free(datValue.get_data());
271 if (!pdb || activeTxn)
273 DbTxn* ptxn = bitdb.TxnBegin();
282 if (!pdb || !activeTxn)
284 int ret = activeTxn->commit(0);
291 if (!pdb || !activeTxn)
293 int ret = activeTxn->abort();
298 bool ReadVersion(int& nVersion)
301 return Read(std::string("version"), nVersion);
304 bool WriteVersion(int nVersion)
306 return Write(std::string("version"), nVersion);
309 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
312 /** Access to the (IP) address database (peers.dat) */
316 boost::filesystem::path pathAddr;
319 bool Write(const CAddrMan& addr);
320 bool Read(CAddrMan& addr);
323 #endif // BITCOIN_DB_H