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);
29 bool DumpWallet(CWallet* pwallet, const std::string& strDest);
30 bool ImportWallet(CWallet* pwallet, const std::string& strLocation);
38 boost::filesystem::path pathEnv;
43 mutable CCriticalSection cs_db;
45 std::map<std::string, int> mapFileUseCount;
46 std::map<std::string, Db*> mapDb;
51 bool IsMock() { return fMockDb; };
54 * Verify that database file strFile is OK. If it is not,
55 * call the callback to try to recover.
56 * This must be called BEFORE strFile is opened.
57 * Returns true if strFile is OK.
59 enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
60 VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
62 * Salvage data from a file that Verify says is bad.
63 * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
64 * Appends binary key/value pairs to vResult, returns true if successful.
65 * NOTE: reads the entire database into memory, so cannot be used
68 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
69 bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
71 bool Open(boost::filesystem::path pathEnv_);
73 void Flush(bool fShutdown);
74 void CheckpointLSN(std::string strFile);
75 void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
76 bool GetDetach() { return fDetachDB; }
78 void CloseDb(const std::string& strFile);
79 bool RemoveDb(const std::string& strFile);
81 DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
84 int ret = dbenv.txn_begin(NULL, &ptxn, flags);
85 if (!ptxn || ret != 0)
94 /** RAII class that provides access to a Berkeley database */
103 explicit CDB(const char* pszFile, const char* pszMode="r+");
110 void operator=(const CDB&);
113 template<typename K, typename T>
114 bool Read(const K& key, T& value)
120 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
123 Dbt datKey(&ssKey[0], ssKey.size());
127 datValue.set_flags(DB_DBT_MALLOC);
128 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
129 memset(datKey.get_data(), 0, datKey.get_size());
130 if (datValue.get_data() == NULL)
135 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
138 catch (std::exception &e) {
142 // Clear and free memory
143 memset(datValue.get_data(), 0, datValue.get_size());
144 free(datValue.get_data());
148 template<typename K, typename T>
149 bool Write(const K& key, const T& value, bool fOverwrite=true)
154 assert(!"Write called on database in read-only mode");
157 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
160 Dbt datKey(&ssKey[0], ssKey.size());
163 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
164 ssValue.reserve(10000);
166 Dbt datValue(&ssValue[0], ssValue.size());
169 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
171 // Clear memory in case it was a private key
172 memset(datKey.get_data(), 0, datKey.get_size());
173 memset(datValue.get_data(), 0, datValue.get_size());
178 bool Erase(const K& key)
183 assert(!"Erase called on database in read-only mode");
186 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
189 Dbt datKey(&ssKey[0], ssKey.size());
192 int ret = pdb->del(activeTxn, &datKey, 0);
195 memset(datKey.get_data(), 0, datKey.get_size());
196 return (ret == 0 || ret == DB_NOTFOUND);
200 bool Exists(const K& key)
206 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
209 Dbt datKey(&ssKey[0], ssKey.size());
212 int ret = pdb->exists(activeTxn, &datKey, 0);
215 memset(datKey.get_data(), 0, datKey.get_size());
224 int ret = pdb->cursor(NULL, &pcursor, 0);
230 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
234 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
236 datKey.set_data(&ssKey[0]);
237 datKey.set_size(ssKey.size());
240 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
242 datValue.set_data(&ssValue[0]);
243 datValue.set_size(ssValue.size());
245 datKey.set_flags(DB_DBT_MALLOC);
246 datValue.set_flags(DB_DBT_MALLOC);
247 int ret = pcursor->get(&datKey, &datValue, fFlags);
250 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
253 // Convert to streams
254 ssKey.SetType(SER_DISK);
256 ssKey.write((char*)datKey.get_data(), datKey.get_size());
257 ssValue.SetType(SER_DISK);
259 ssValue.write((char*)datValue.get_data(), datValue.get_size());
261 // Clear and free memory
262 memset(datKey.get_data(), 0, datKey.get_size());
263 memset(datValue.get_data(), 0, datValue.get_size());
264 free(datKey.get_data());
265 free(datValue.get_data());
272 if (!pdb || activeTxn)
274 DbTxn* ptxn = bitdb.TxnBegin();
283 if (!pdb || !activeTxn)
285 int ret = activeTxn->commit(0);
292 if (!pdb || !activeTxn)
294 int ret = activeTxn->abort();
299 bool ReadVersion(int& nVersion)
302 return Read(std::string("version"), nVersion);
305 bool WriteVersion(int nVersion)
307 return Write(std::string("version"), nVersion);
310 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
313 /** Access to the (IP) address database (peers.dat) */
317 boost::filesystem::path pathAddr;
320 bool Write(const CAddrMan& addr);
321 bool Read(CAddrMan& addr);
324 #endif // BITCOIN_DB_H