6 #ifndef BITCOIN_WALLET_DB_H 7 #define BITCOIN_WALLET_DB_H 21 #include <unordered_map> 26 static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
27 static const bool DEFAULT_WALLET_PRIVDB =
true;
46 std::map<std::string, Db*>
mapDb;
47 std::unordered_map<std::string, WalletDatabaseFileId>
m_fileids;
68 typedef bool (*
recoverFunc_type)(
const fs::path& file_path, std::string& out_backup_filename);
77 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> >
KeyValPair;
78 bool Salvage(
const std::string& strFile,
bool fAggressive, std::vector<KeyValPair>& vResult);
80 bool Open(
bool retry);
82 void Flush(
bool fShutdown);
85 void CloseDb(
const std::string& strFile);
90 DbTxn* ptxn =
nullptr;
92 if (!ptxn ||
ret != 0)
126 static std::unique_ptr<BerkeleyDatabase>
Create(
const fs::path& path)
128 return MakeUnique<BerkeleyDatabase>(path);
134 return MakeUnique<BerkeleyDatabase>();
140 return MakeUnique<BerkeleyDatabase>(
"",
true );
145 bool Rewrite(
const char* pszSkip=
nullptr);
149 bool Backup(
const std::string& strDest);
153 void Flush(
bool shutdown);
197 static bool Recover(
const fs::path& file_path,
void *callbackDataIn,
bool (*recoverKVcallback)(
void* callbackData,
CDataStream ssKey,
CDataStream ssValue), std::string& out_backup_filename);
208 template <
typename K,
typename T>
209 bool Read(
const K& key, T& value)
218 Dbt datKey(ssKey.
data(), ssKey.
size());
222 datValue.set_flags(DB_DBT_MALLOC);
225 bool success =
false;
226 if (datValue.get_data() !=
nullptr) {
229 CDataStream ssValue((
char*)datValue.get_data(), (
char*)datValue.get_data() + datValue.get_size(),
SER_DISK, CLIENT_VERSION);
232 }
catch (
const std::exception&) {
238 free(datValue.get_data());
240 return ret == 0 && success;
243 template <
typename K,
typename T>
244 bool Write(
const K& key,
const T& value,
bool fOverwrite =
true)
249 assert(!
"Write called on database in read-only mode");
255 Dbt datKey(ssKey.
data(), ssKey.
size());
259 ssValue.reserve(10000);
261 Dbt datValue(ssValue.data(), ssValue.size());
264 int ret =
pdb->put(
activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
272 template <
typename K>
278 assert(!
"Erase called on database in read-only mode");
284 Dbt datKey(ssKey.
data(), ssKey.
size());
291 return (
ret == 0 ||
ret == DB_NOTFOUND);
294 template <
typename K>
304 Dbt datKey(ssKey.
data(), ssKey.
size());
318 Dbc* pcursor =
nullptr;
319 int ret =
pdb->cursor(
nullptr, &pcursor, 0);
329 unsigned int fFlags = DB_NEXT;
331 datKey.set_data(ssKey.
data());
332 datKey.set_size(ssKey.
size());
333 fFlags = DB_SET_RANGE;
336 datKey.set_flags(DB_DBT_MALLOC);
337 datValue.set_flags(DB_DBT_MALLOC);
338 int ret = pcursor->get(&datKey, &datValue, fFlags);
341 else if (datKey.get_data() ==
nullptr || datValue.get_data() ==
nullptr)
347 ssKey.
write((
char*)datKey.get_data(), datKey.get_size());
350 ssValue.
write((
char*)datValue.get_data(), datValue.get_size());
355 free(datKey.get_data());
356 free(datValue.get_data());
393 return Read(std::string(
"version"), nVersion);
398 return Write(std::string(
"version"), nVersion);
404 #endif // BITCOIN_WALLET_DB_H void Flush(bool shutdown)
Make sure all changes are flushed to disk.
static std::unique_ptr< BerkeleyDatabase > CreateDummy()
Return object for accessing dummy database with no read/write capabilities.
static std::unique_ptr< BerkeleyDatabase > CreateMock()
Return object for accessing temporary in-memory database.
bool IsInitialized() const
BerkeleyDatabase(const fs::path &wallet_path, bool mock=false)
Create DB handle to real database.
VerifyResult
Verify that database file strFile is OK.
std::unordered_map< std::string, WalletDatabaseFileId > m_fileids
UniValue ret(UniValue::VARR)
bool Exists(const K &key)
bool Write(const K &key, const T &value, bool fOverwrite=true)
An instance of this class represents one database.
Double ended buffer combining vector and stream-like interfaces.
void write(const char *pch, size_t nSize)
bool Backup(const std::string &strDest)
Back up the entire database to a file.
fs::path Directory() const
static bool PeriodicFlush(BerkeleyDatabase &database)
std::map< std::string, Db * > mapDb
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
std::map< std::string, int > mapFileUseCount
std::unique_ptr< DbEnv > dbenv
void CheckpointLSN(const std::string &strFile)
std::pair< std::vector< unsigned char >, std::vector< unsigned char > > KeyValPair
Salvage data from a file that Verify says is bad.
bool Read(const K &key, T &value)
void memory_cleanse(void *ptr, size_t len)
BerkeleyEnvironment * GetWalletEnv(const fs::path &wallet_path, std::string &database_filename)
Get BerkeleyEnvironment and database filename given a wallet path.
bool Rewrite(const char *pszSkip=nullptr)
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
bool WriteVersion(int nVersion)
BerkeleyDatabase()
Create dummy DB handle.
RAII class that provides access to a Berkeley database.
bool operator==(const WalletDatabaseFileId &rhs) const
static bool VerifyEnvironment(const fs::path &file_path, std::string &errorStr)
BerkeleyEnvironment(const fs::path &env_directory)
static bool Recover(const fs::path &file_path, void *callbackDataIn, bool(*recoverKVcallback)(void *callbackData, CDataStream ssKey, CDataStream ssValue), std::string &out_backup_filename)
static std::unique_ptr< BerkeleyDatabase > Create(const fs::path &path)
Return object for accessing database at specified path.
bool Salvage(const std::string &strFile, bool fAggressive, std::vector< KeyValPair > &vResult)
static bool Rewrite(BerkeleyDatabase &database, const char *pszSkip=nullptr)
int64_t nLastWalletUpdate
void IncrementUpdateCounter()
void reserve(size_type n)
BerkeleyBatch(BerkeleyDatabase &database, const char *pszMode="r+", bool fFlushOnCloseIn=true)
unsigned int nLastFlushed
int ReadAtCursor(Dbc *pcursor, CDataStream &ssKey, CDataStream &ssValue, bool setRange=false)
BerkeleyEnvironment * env
u_int8_t value[DB_FILE_ID_LEN]
void Flush(bool fShutdown)
std::atomic< unsigned int > nUpdateCounter
bool(* recoverFunc_type)(const fs::path &file_path, std::string &out_backup_filename)
VerifyResult Verify(const std::string &strFile, recoverFunc_type recoverFunc, std::string &out_backup_filename)
bool ReadVersion(int &nVersion)
static bool VerifyDatabaseFile(const fs::path &file_path, std::string &warningStr, std::string &errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc)
void CloseDb(const std::string &strFile)
BerkeleyEnvironment * env
BerkeleyDB specific.
BerkeleyBatch & operator=(const BerkeleyBatch &)=delete
std::condition_variable_any m_db_in_use
bool IsDummy()
Return whether this database handle is a dummy for testing.