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.
12 #include <boost/filesystem/path.hpp>
23 class CDiskBlockIndex;
31 extern unsigned int nWalletDBUpdated;
33 void ThreadFlushWalletDB(void* parg);
34 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
35 bool DumpWallet(CWallet* pwallet, const std::string& strDest);
36 bool ImportWallet(CWallet* pwallet, const std::string& strLocation);
44 boost::filesystem::path pathEnv;
50 mutable CCriticalSection cs_db;
52 std::map<std::string, int> mapFileUseCount;
53 std::map<std::string, Db*> mapDb;
58 bool IsMock() { return fMockDb; };
61 * Verify that database file strFile is OK. If it is not,
62 * call the callback to try to recover.
63 * This must be called BEFORE strFile is opened.
64 * Returns true if strFile is OK.
66 enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
67 VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
69 * Salvage data from a file that Verify says is bad.
70 * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
71 * Appends binary key/value pairs to vResult, returns true if successful.
72 * NOTE: reads the entire database into memory, so cannot be used
75 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
76 bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
78 bool Open(boost::filesystem::path pathEnv_);
80 void Flush(bool fShutdown);
81 void CheckpointLSN(std::string strFile);
82 void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
83 bool GetDetach() { return fDetachDB; }
85 void CloseDb(const std::string& strFile);
86 bool RemoveDb(const std::string& strFile);
88 DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
91 int ret = dbenv.txn_begin(NULL, &ptxn, flags);
92 if (!ptxn || ret != 0)
101 /** RAII class that provides access to a Berkeley database */
110 explicit CDB(const char* pszFile, const char* pszMode="r+");
116 void operator=(const CDB&);
119 template<typename K, typename T>
120 bool Read(const K& key, T& value)
126 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
129 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
133 datValue.set_flags(DB_DBT_MALLOC);
134 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
135 memset(datKey.get_data(), 0, datKey.get_size());
136 if (datValue.get_data() == NULL)
141 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
144 catch (const std::exception&) {
148 // Clear and free memory
149 memset(datValue.get_data(), 0, datValue.get_size());
150 free(datValue.get_data());
154 template<typename K, typename T>
155 bool Write(const K& key, const T& value, bool fOverwrite=true)
160 assert(!"Write called on database in read-only mode");
163 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
166 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
169 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
170 ssValue.reserve(10000);
172 Dbt datValue(&ssValue[0], (uint32_t)ssValue.size());
175 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
177 // Clear memory in case it was a private key
178 memset(datKey.get_data(), 0, datKey.get_size());
179 memset(datValue.get_data(), 0, datValue.get_size());
184 bool Erase(const K& key)
189 assert(!"Erase called on database in read-only mode");
192 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
195 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
198 int ret = pdb->del(activeTxn, &datKey, 0);
201 memset(datKey.get_data(), 0, datKey.get_size());
202 return (ret == 0 || ret == DB_NOTFOUND);
206 bool Exists(const K& key)
212 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
215 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
218 int ret = pdb->exists(activeTxn, &datKey, 0);
221 memset(datKey.get_data(), 0, datKey.get_size());
230 int ret = pdb->cursor(NULL, &pcursor, 0);
236 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
240 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
242 datKey.set_data(&ssKey[0]);
243 datKey.set_size((uint32_t)ssKey.size());
246 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
248 datValue.set_data(&ssValue[0]);
249 datValue.set_size((uint32_t)ssValue.size());
251 datKey.set_flags(DB_DBT_MALLOC);
252 datValue.set_flags(DB_DBT_MALLOC);
253 int ret = pcursor->get(&datKey, &datValue, fFlags);
256 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
259 // Convert to streams
260 ssKey.SetType(SER_DISK);
262 ssKey.write((char*)datKey.get_data(), datKey.get_size());
263 ssValue.SetType(SER_DISK);
265 ssValue.write((char*)datValue.get_data(), datValue.get_size());
267 // Clear and free memory
268 memset(datKey.get_data(), 0, datKey.get_size());
269 memset(datValue.get_data(), 0, datValue.get_size());
270 free(datKey.get_data());
271 free(datValue.get_data());
278 if (!pdb || activeTxn)
280 DbTxn* ptxn = bitdb.TxnBegin();
289 if (!pdb || !activeTxn)
291 int ret = activeTxn->commit(0);
298 if (!pdb || !activeTxn)
300 int ret = activeTxn->abort();
305 bool ReadVersion(int& nVersion)
308 return Read(std::string("version"), nVersion);
311 bool WriteVersion(int nVersion)
313 return Write(std::string("version"), nVersion);
316 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
320 /** Access to the (IP) address database (peers.dat) */
324 boost::filesystem::path pathAddr;
327 bool Write(const CAddrMan& addr);
328 bool Read(CAddrMan& addr);
331 #endif // BITCOIN_DB_H