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.
13 #include <boost/filesystem/path.hpp>
24 class CDiskBlockIndex;
32 extern unsigned int nWalletDBUpdated;
34 void ThreadFlushWalletDB(void* parg);
35 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
36 bool DumpWallet(CWallet* pwallet, const std::string& strDest);
37 bool ImportWallet(CWallet* pwallet, const std::string& strLocation);
45 boost::filesystem::path pathEnv;
51 mutable CCriticalSection cs_db;
53 std::map<std::string, int> mapFileUseCount;
54 std::map<std::string, Db*> mapDb;
59 bool IsMock() { return fMockDb; };
62 * Verify that database file strFile is OK. If it is not,
63 * call the callback to try to recover.
64 * This must be called BEFORE strFile is opened.
65 * Returns true if strFile is OK.
67 enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
68 VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
70 * Salvage data from a file that Verify says is bad.
71 * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
72 * Appends binary key/value pairs to vResult, returns true if successful.
73 * NOTE: reads the entire database into memory, so cannot be used
76 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
77 bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
79 bool Open(boost::filesystem::path pathEnv_);
81 void Flush(bool fShutdown);
82 void CheckpointLSN(std::string strFile);
83 void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
84 bool GetDetach() { return fDetachDB; }
86 void CloseDb(const std::string& strFile);
87 bool RemoveDb(const std::string& strFile);
89 DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
92 int ret = dbenv.txn_begin(NULL, &ptxn, flags);
93 if (!ptxn || ret != 0)
102 /** RAII class that provides access to a Berkeley database */
111 explicit CDB(const char* pszFile, const char* pszMode="r+");
117 void operator=(const CDB&);
120 template<typename K, typename T>
121 bool Read(const K& key, T& value)
127 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
130 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
134 datValue.set_flags(DB_DBT_MALLOC);
135 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
136 memset(datKey.get_data(), 0, datKey.get_size());
137 if (datValue.get_data() == NULL)
142 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
145 catch (const std::exception&) {
149 // Clear and free memory
150 memset(datValue.get_data(), 0, datValue.get_size());
151 free(datValue.get_data());
155 template<typename K, typename T>
156 bool Write(const K& key, const T& value, bool fOverwrite=true)
161 assert(!"Write called on database in read-only mode");
164 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
167 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
170 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
171 ssValue.reserve(10000);
173 Dbt datValue(&ssValue[0], (uint32_t)ssValue.size());
176 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
178 // Clear memory in case it was a private key
179 memset(datKey.get_data(), 0, datKey.get_size());
180 memset(datValue.get_data(), 0, datValue.get_size());
185 bool Erase(const K& key)
190 assert(!"Erase called on database in read-only mode");
193 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
196 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
199 int ret = pdb->del(activeTxn, &datKey, 0);
202 memset(datKey.get_data(), 0, datKey.get_size());
203 return (ret == 0 || ret == DB_NOTFOUND);
207 bool Exists(const K& key)
213 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
216 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
219 int ret = pdb->exists(activeTxn, &datKey, 0);
222 memset(datKey.get_data(), 0, datKey.get_size());
231 int ret = pdb->cursor(NULL, &pcursor, 0);
237 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
241 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
243 datKey.set_data(&ssKey[0]);
244 datKey.set_size((uint32_t)ssKey.size());
247 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
249 datValue.set_data(&ssValue[0]);
250 datValue.set_size((uint32_t)ssValue.size());
252 datKey.set_flags(DB_DBT_MALLOC);
253 datValue.set_flags(DB_DBT_MALLOC);
254 int ret = pcursor->get(&datKey, &datValue, fFlags);
257 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
260 // Convert to streams
261 ssKey.SetType(SER_DISK);
263 ssKey.write((char*)datKey.get_data(), datKey.get_size());
264 ssValue.SetType(SER_DISK);
266 ssValue.write((char*)datValue.get_data(), datValue.get_size());
268 // Clear and free memory
269 memset(datKey.get_data(), 0, datKey.get_size());
270 memset(datValue.get_data(), 0, datValue.get_size());
271 free(datKey.get_data());
272 free(datValue.get_data());
279 if (!pdb || activeTxn)
281 DbTxn* ptxn = bitdb.TxnBegin();
290 if (!pdb || !activeTxn)
292 int ret = activeTxn->commit(0);
299 if (!pdb || !activeTxn)
301 int ret = activeTxn->abort();
306 bool ReadVersion(int& nVersion)
309 return Read(std::string("version"), nVersion);
312 bool WriteVersion(int nVersion)
314 return Write(std::string("version"), nVersion);
317 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
321 /** Access to the (IP) address database (peers.dat) */
325 boost::filesystem::path pathAddr;
328 bool Write(const CAddrMan& addr);
329 bool Read(CAddrMan& addr);
332 #endif // BITCOIN_DB_H