17 #include <boost/filesystem/path.hpp>
35 boost::filesystem::path
path;
43 std::map<std::string, Db*>
mapDb;
65 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> >
KeyValPair;
66 bool Salvage(std::string strFile,
bool fAggressive, std::vector<KeyValPair>& vResult);
68 bool Open(
const boost::filesystem::path &path);
70 void Flush(
bool fShutdown);
73 void CloseDb(
const std::string& strFile);
74 bool RemoveDb(
const std::string& strFile);
79 int ret = dbenv.txn_begin(NULL, &ptxn, flags);
80 if (!ptxn || ret != 0)
98 explicit CDB(
const char* pszFile,
const char* pszMode=
"r+");
108 template<
typename K,
typename T>
109 bool Read(
const K& key, T& value)
118 Dbt datKey(&ssKey[0], ssKey.
size());
122 datValue.set_flags(DB_DBT_MALLOC);
123 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
124 memset(datKey.get_data(), 0, datKey.get_size());
125 if (datValue.get_data() == NULL)
133 catch (std::exception &e) {
138 memset(datValue.get_data(), 0, datValue.get_size());
139 free(datValue.get_data());
143 template<
typename K,
typename T>
144 bool Write(
const K& key,
const T& value,
bool fOverwrite=
true)
149 assert(!
"Write called on database in read-only mode");
155 Dbt datKey(&ssKey[0], ssKey.
size());
159 ssValue.reserve(10000);
161 Dbt datValue(&ssValue[0], ssValue.size());
164 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
167 memset(datKey.get_data(), 0, datKey.get_size());
168 memset(datValue.get_data(), 0, datValue.get_size());
178 assert(!
"Erase called on database in read-only mode");
184 Dbt datKey(&ssKey[0], ssKey.
size());
187 int ret = pdb->del(activeTxn, &datKey, 0);
190 memset(datKey.get_data(), 0, datKey.get_size());
191 return (ret == 0 || ret == DB_NOTFOUND);
204 Dbt datKey(&ssKey[0], ssKey.
size());
207 int ret = pdb->exists(activeTxn, &datKey, 0);
210 memset(datKey.get_data(), 0, datKey.get_size());
219 int ret = pdb->cursor(NULL, &pcursor, 0);
229 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
231 datKey.set_data(&ssKey[0]);
232 datKey.set_size(ssKey.
size());
235 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
237 datValue.set_data(&ssValue[0]);
238 datValue.set_size(ssValue.
size());
240 datKey.set_flags(DB_DBT_MALLOC);
241 datValue.set_flags(DB_DBT_MALLOC);
242 int ret = pcursor->get(&datKey, &datValue, fFlags);
245 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
251 ssKey.
write((
char*)datKey.get_data(), datKey.get_size());
254 ssValue.
write((
char*)datValue.get_data(), datValue.get_size());
257 memset(datKey.get_data(), 0, datKey.get_size());
258 memset(datValue.get_data(), 0, datValue.get_size());
259 free(datKey.get_data());
260 free(datValue.get_data());
267 if (!pdb || activeTxn)
278 if (!pdb || !activeTxn)
280 int ret = activeTxn->commit(0);
287 if (!pdb || !activeTxn)
289 int ret = activeTxn->abort();
297 return Read(std::string(
"version"), nVersion);
302 return Write(std::string(
"version"), nVersion);
305 bool static Rewrite(
const std::string& strFile,
const char* pszSkip = NULL);
308 #endif // BITCOIN_DB_H
std::map< std::string, int > mapFileUseCount
void CheckpointLSN(std::string strFile)
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
int ReadAtCursor(Dbc *pcursor, CDataStream &ssKey, CDataStream &ssValue, unsigned int fFlags=DB_NEXT)
static bool Rewrite(const std::string &strFile, const char *pszSkip=NULL)
bool Write(const K &key, const T &value, bool fOverwrite=true)
static const int CLIENT_VERSION
std::string strWalletFile
void Flush(bool fShutdown)
bool Exists(const K &key)
Double ended buffer combining vector and stream-like interfaces.
unsigned int nWalletDBUpdated
boost::filesystem::path path
Stochastical (IP) address manager.
std::map< std::string, Db * > mapDb
bool ReadVersion(int &nVersion)
RAII class that provides access to a Berkeley database.
Used to marshal pointers into hashes for db storage.
An outpoint - a combination of a transaction hash and an index n into its vout.
bool RemoveDb(const std::string &strFile)
bool Read(const K &key, T &value)
void operator=(const CDB &)
bool Salvage(std::string strFile, bool fAggressive, std::vector< KeyValPair > &vResult)
CDataStream & write(const char *pch, int nSize)
void reserve(size_type n)
void CloseDb(const std::string &strFile)
bool WriteVersion(int nVersion)
CDB(const char *pszFile, const char *pszMode="r+")
VerifyResult Verify(std::string strFile, bool(*recoverFunc)(CDBEnv &dbenv, std::string strFile))
std::pair< std::vector< unsigned char >, std::vector< unsigned char > > KeyValPair
bool Open(const boost::filesystem::path &path)
void ThreadFlushWalletDB(const std::string &strWalletFile)