6 #ifndef BITCOIN_WALLET_DB_H 7 #define BITCOIN_WALLET_DB_H 19 #include <boost/filesystem/path.hpp> 43 std::map<std::string, Db*>
mapDb;
69 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> >
KeyValPair;
70 bool Salvage(
const std::string& strFile,
bool fAggressive, std::vector<KeyValPair>& vResult);
72 bool Open(
const boost::filesystem::path& path);
74 void Flush(
bool fShutdown);
77 void CloseDb(
const std::string& strFile);
78 bool RemoveDb(
const std::string& strFile);
83 int ret =
dbenv->txn_begin(NULL, &ptxn,
flags);
84 if (!ptxn || ret != 0)
103 explicit CDB(
const std::string& strFilename,
const char* pszMode =
"r+",
bool fFlushOnCloseIn=
true);
115 template <
typename K,
typename T>
125 Dbt datKey(&ssKey[0], ssKey.
size());
129 datValue.set_flags(DB_DBT_MALLOC);
131 memset(datKey.get_data(), 0, datKey.get_size());
132 if (datValue.get_data() == NULL)
139 }
catch (
const std::exception&) {
144 memset(datValue.get_data(), 0, datValue.get_size());
145 free(datValue.get_data());
149 template <
typename K,
typename T>
150 bool Write(
const K&
key,
const T& value,
bool fOverwrite =
true)
155 assert(!
"Write called on database in read-only mode");
161 Dbt datKey(&ssKey[0], ssKey.
size());
165 ssValue.reserve(10000);
167 Dbt datValue(&ssValue[0], ssValue.size());
170 int ret =
pdb->put(
activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
173 memset(datKey.get_data(), 0, datKey.get_size());
174 memset(datValue.get_data(), 0, datValue.get_size());
178 template <
typename K>
184 assert(!
"Erase called on database in read-only mode");
190 Dbt datKey(&ssKey[0], ssKey.
size());
196 memset(datKey.get_data(), 0, datKey.get_size());
197 return (ret == 0 || ret == DB_NOTFOUND);
200 template <
typename K>
210 Dbt datKey(&ssKey[0], ssKey.
size());
216 memset(datKey.get_data(), 0, datKey.get_size());
225 int ret =
pdb->cursor(NULL, &pcursor, 0);
235 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) {
241 datValue.set_data(&ssValue[0]);
242 datValue.set_size(ssValue.
size());
244 datKey.set_flags(DB_DBT_MALLOC);
245 datValue.set_flags(DB_DBT_MALLOC);
246 int ret = pcursor->get(&datKey, &datValue, fFlags);
249 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
255 ssKey.
write((
char*)datKey.get_data(), datKey.get_size());
258 ssValue.
write((
char*)datValue.get_data(), datValue.get_size());
261 memset(datKey.get_data(), 0, datKey.get_size());
262 memset(datValue.get_data(), 0, datValue.get_size());
263 free(datKey.get_data());
264 free(datValue.get_data());
301 return Read(std::string(
"version"), nVersion);
306 return Write(std::string(
"version"), nVersion);
309 bool static Rewrite(
const std::string&
strFile,
const char* pszSkip = NULL);
312 #endif // BITCOIN_WALLET_DB_H VerifyResult Verify(const std::string &strFile, bool(*recoverFunc)(CDBEnv &dbenv, const std::string &strFile))
bool RemoveDb(const std::string &strFile)
std::map< std::string, Db * > mapDb
void operator=(const CDB &)
CDB(const std::string &strFilename, const char *pszMode="r+", bool fFlushOnCloseIn=true)
std::pair< std::vector< unsigned char >, std::vector< unsigned char > > KeyValPair
bool Exists(const K &key)
unsigned int nWalletDBUpdated
bool Read(const K &key, T &value)
CDataStream & write(const char *pch, size_t nSize)
void reserve(size_type n)
static bool Rewrite(const std::string &strFile, const char *pszSkip=NULL)
std::map< std::string, int > mapFileUseCount
bool Write(const K &key, const T &value, bool fOverwrite=true)
bool Open(const boost::filesystem::path &path)
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
int ReadAtCursor(Dbc *pcursor, CDataStream &ssKey, CDataStream &ssValue, unsigned int fFlags=DB_NEXT)
static const unsigned int DEFAULT_WALLET_DBLOGSIZE
void Flush(bool fShutdown)
void CheckpointLSN(const std::string &strFile)
bool Salvage(const std::string &strFile, bool fAggressive, std::vector< KeyValPair > &vResult)
bool WriteVersion(int nVersion)
static const bool DEFAULT_WALLET_PRIVDB
bool ReadVersion(int &nVersion)
void CloseDb(const std::string &strFile)
static const int CLIENT_VERSION