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;
55 * Verify that database file strFile is OK. If it is not,
56 * call the callback to try to recover.
57 * This must be called BEFORE strFile is opened.
58 * Returns true if strFile is OK.
60 enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
61 VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
63 * Salvage data from a file that Verify says is bad.
64 * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
65 * Appends binary key/value pairs to vResult, returns true if successful.
66 * NOTE: reads the entire database into memory, so cannot be used
69 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
70 bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
72 bool Open(boost::filesystem::path pathEnv_);
74 void Flush(bool fShutdown);
75 void CheckpointLSN(std::string strFile);
76 void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
77 bool GetDetach() { return fDetachDB; }
79 void CloseDb(const std::string& strFile);
80 bool RemoveDb(const std::string& strFile);
82 DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC);
88 /** RAII class that provides access to a Berkeley database */
97 explicit CDB(const char* pszFile, const char* pszMode="r+");
103 void operator=(const CDB&);
106 template<typename K, typename T>
107 bool Read(const K& key, T& value)
113 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
116 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
120 datValue.set_flags(DB_DBT_MALLOC);
121 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
122 memset(datKey.get_data(), 0, datKey.get_size());
123 if (datValue.get_data() == NULL)
128 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
131 catch (const std::exception&) {
135 // Clear and free memory
136 memset(datValue.get_data(), 0, datValue.get_size());
137 free(datValue.get_data());
141 template<typename K, typename T>
142 bool Write(const K& key, const T& value, bool fOverwrite=true)
147 assert(!"Write called on database in read-only mode");
150 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
153 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
156 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
157 ssValue.reserve(10000);
159 Dbt datValue(&ssValue[0], (uint32_t)ssValue.size());
162 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
164 // Clear memory in case it was a private key
165 memset(datKey.get_data(), 0, datKey.get_size());
166 memset(datValue.get_data(), 0, datValue.get_size());
171 bool Erase(const K& key)
176 assert(!"Erase called on database in read-only mode");
179 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
182 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
185 int ret = pdb->del(activeTxn, &datKey, 0);
188 memset(datKey.get_data(), 0, datKey.get_size());
189 return (ret == 0 || ret == DB_NOTFOUND);
193 bool Exists(const K& key)
199 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
202 Dbt datKey(&ssKey[0], (uint32_t)ssKey.size());
205 int ret = pdb->exists(activeTxn, &datKey, 0);
208 memset(datKey.get_data(), 0, datKey.get_size());
213 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT);
219 bool ReadVersion(int& nVersion);
220 bool WriteVersion(int nVersion);
221 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
225 /** Access to the (IP) address database (peers.dat) */
229 boost::filesystem::path pathAddr;
232 bool Write(const CAddrMan& addr);
233 bool Read(CAddrMan& addr);
236 #endif // BITCOIN_DB_H