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;
27 extern unsigned int nWalletDBUpdated;
29 void ThreadFlushWalletDB(void* parg);
30 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
31 bool DumpWallet(CWallet* pwallet, const std::string& strDest);
32 bool ImportWallet(CWallet* pwallet, const std::string& strLocation);
40 boost::filesystem::path pathEnv;
46 mutable CCriticalSection cs_db;
48 std::map<std::string, int> mapFileUseCount;
49 std::map<std::string, Db*> mapDb;
54 bool IsMock() { return fMockDb; };
57 * Verify that database file strFile is OK. If it is not,
58 * call the callback to try to recover.
59 * This must be called BEFORE strFile is opened.
60 * Returns true if strFile is OK.
62 enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
63 VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
65 * Salvage data from a file that Verify says is bad.
66 * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
67 * Appends binary key/value pairs to vResult, returns true if successful.
68 * NOTE: reads the entire database into memory, so cannot be used
71 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
72 bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
74 bool Open(boost::filesystem::path pathEnv_);
76 void Flush(bool fShutdown);
77 void CheckpointLSN(std::string strFile);
78 void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
79 bool GetDetach() { return fDetachDB; }
81 void CloseDb(const std::string& strFile);
82 bool RemoveDb(const std::string& strFile);
84 DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
87 int ret = dbenv.txn_begin(NULL, &ptxn, flags);
88 if (!ptxn || ret != 0)
97 /** RAII class that provides access to a Berkeley database */
106 explicit CDB(const char* pszFile, const char* pszMode="r+");
112 void operator=(const CDB&);
115 template<typename K, typename T>
116 bool Read(const K& key, T& value)
122 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
125 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
129 datValue.set_flags(DB_DBT_MALLOC);
130 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
131 memset(datKey.get_data(), 0, datKey.get_size());
132 if (datValue.get_data() == NULL)
137 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
140 catch (std::exception &e) {
145 // Clear and free memory
146 memset(datValue.get_data(), 0, datValue.get_size());
147 free(datValue.get_data());
151 template<typename K, typename T>
152 bool Write(const K& key, const T& value, bool fOverwrite=true)
157 assert(!"Write called on database in read-only mode");
160 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
163 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
166 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
167 ssValue.reserve(10000);
169 Dbt datValue(&ssValue[0], (uint32_t)ssValue.size());
172 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
174 // Clear memory in case it was a private key
175 memset(datKey.get_data(), 0, datKey.get_size());
176 memset(datValue.get_data(), 0, datValue.get_size());
181 bool Erase(const K& key)
186 assert(!"Erase called on database in read-only mode");
189 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
192 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
195 int ret = pdb->del(activeTxn, &datKey, 0);
198 memset(datKey.get_data(), 0, datKey.get_size());
199 return (ret == 0 || ret == DB_NOTFOUND);
203 bool Exists(const K& key)
209 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
212 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
215 int ret = pdb->exists(activeTxn, &datKey, 0);
218 memset(datKey.get_data(), 0, datKey.get_size());
227 int ret = pdb->cursor(NULL, &pcursor, 0);
233 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
237 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
239 datKey.set_data(&ssKey[0]);
240 datKey.set_size((uint32_t)ssKey.size());
243 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
245 datValue.set_data(&ssValue[0]);
246 datValue.set_size((uint32_t)ssValue.size());
248 datKey.set_flags(DB_DBT_MALLOC);
249 datValue.set_flags(DB_DBT_MALLOC);
250 int ret = pcursor->get(&datKey, &datValue, fFlags);
253 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
256 // Convert to streams
257 ssKey.SetType(SER_DISK);
259 ssKey.write((char*)datKey.get_data(), datKey.get_size());
260 ssValue.SetType(SER_DISK);
262 ssValue.write((char*)datValue.get_data(), datValue.get_size());
264 // Clear and free memory
265 memset(datKey.get_data(), 0, datKey.get_size());
266 memset(datValue.get_data(), 0, datValue.get_size());
267 free(datKey.get_data());
268 free(datValue.get_data());
275 if (!pdb || activeTxn)
277 DbTxn* ptxn = bitdb.TxnBegin();
286 if (!pdb || !activeTxn)
288 int ret = activeTxn->commit(0);
295 if (!pdb || !activeTxn)
297 int ret = activeTxn->abort();
302 bool ReadVersion(int& nVersion)
305 return Read(std::string("version"), nVersion);
308 bool WriteVersion(int nVersion)
310 return Write(std::string("version"), nVersion);
313 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
317 /** Access to the (IP) address database (peers.dat) */
321 boost::filesystem::path pathAddr;
324 bool Write(const CAddrMan& addr);
325 bool Read(CAddrMan& addr);
328 #endif // BITCOIN_DB_H