43 std::map<std::string, Db*>
mapDb;
69 bool Compact(
const std::string& strFile);
70 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> >
KeyValPair;
71 bool Salvage(std::string strFile,
bool fAggressive, std::vector<KeyValPair>& vResult);
73 bool Open(
const fs::path& path);
75 void Flush(
bool fShutdown);
78 void CloseDb(
const std::string& strFile);
79 bool RemoveDb(
const std::string& strFile);
84 int ret =
dbenv->txn_begin(NULL, &ptxn,
flags);
85 if (!ptxn || ret != 0)
104 explicit CDB(
const std::string& strFilename,
const char* pszMode =
"r+",
bool fFlushOnCloseIn=
true);
116 template <
typename K,
typename T>
126 Dbt datKey(&ssKey[0], ssKey.
size());
130 datValue.set_flags(DB_DBT_MALLOC);
133 bool success =
false;
134 if (datValue.get_data() != NULL) {
137 CDataStream ssValue((
char*)datValue.get_data(), (
char*)datValue.get_data() + datValue.get_size(),
SER_DISK, CLIENT_VERSION);
140 }
catch (
const std::exception&) {
146 free(datValue.get_data());
148 return ret == 0 && success;
151 template <
typename K,
typename T>
152 bool Write(
const K&
key,
const T& value,
bool fOverwrite =
true)
157 assert(!
"Write called on database in read-only mode");
163 Dbt datKey(&ssKey[0], ssKey.
size());
169 Dbt datValue(&ssValue[0], ssValue.
size());
172 int ret =
pdb->put(
activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
180 template <
typename K>
186 assert(!
"Erase called on database in read-only mode");
192 Dbt datKey(&ssKey[0], ssKey.
size());
199 return (ret == 0 || ret == DB_NOTFOUND);
202 template <
typename K>
212 Dbt datKey(&ssKey[0], ssKey.
size());
227 int ret =
pdb->cursor(NULL, &pcursor, 0);
237 datKey.set_data(NULL);
238 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) {
239 datKey.set_data(&ssKey[0]);
240 datKey.set_size(ssKey.
size());
243 datValue.set_data(NULL);
244 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) {
245 datValue.set_data(&ssValue[0]);
246 datValue.set_size(ssValue.
size());
248 datKey.set_flags(DB_DBT_MALLOC);
249 datValue.set_flags(DB_DBT_MALLOC);
250 int ret = pcursor->get(&datKey, &datValue, fFlags);
252 if (datKey.get_data() != NULL)
253 free(datKey.get_data());
254 if (datValue.get_data() != NULL)
255 free(datValue.get_data());
259 else if (datKey.get_data() == NULL || datValue.get_data() == NULL) {
260 if (datKey.get_data() != NULL)
261 free(datKey.get_data());
262 if (datValue.get_data() != NULL)
263 free(datValue.get_data());
271 ssKey.
write((
char*)datKey.get_data(), datKey.get_size());
274 ssValue.
write((
char*)datValue.get_data(), datValue.get_size());
279 free(datKey.get_data());
280 free(datValue.get_data());
317 return Read(std::string(
"version"), nVersion);
322 return Write(std::string(
"version"), nVersion);
325 bool static Rewrite(
const std::string&
strFile,
const char* pszSkip = NULL);
328 #endif // BITCOIN_DB_H