7 #ifndef PIVX_WALLET_DB_H
8 #define PIVX_WALLET_DB_H
20 #include <unordered_map>
25 static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
26 static const bool DEFAULT_WALLET_PRIVDB =
true;
45 std::map<std::string, Db*>
mapDb;
46 std::unordered_map<std::string, WalletDatabaseFileId>
m_fileids;
67 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;
91 int ret =
dbenv->txn_begin(
nullptr, &ptxn,
flags);
92 if (!ptxn || ret != 0)
126 static std::unique_ptr<BerkeleyDatabase>
Create(
const fs::path& path)
128 return std::make_unique<BerkeleyDatabase>(path);
134 return std::make_unique<BerkeleyDatabase>();
140 return std::make_unique<BerkeleyDatabase>(
"",
true );
145 bool Rewrite(
const char* pszSkip=
nullptr);
149 bool Backup(
const std::string& strDest);
153 void Flush(
bool shutdown);
198 static bool Recover(
const fs::path& file_path,
void *callbackDataIn,
bool (*recoverKVcallback)(
void* callbackData,
CDataStream ssKey,
CDataStream ssValue), std::string& out_backup_filename);
209 template <
typename K,
typename T>
219 Dbt datKey(ssKey.
data(), ssKey.
size());
223 datValue.set_flags(DB_DBT_MALLOC);
226 bool success =
false;
227 if (datValue.get_data() !=
nullptr) {
230 CDataStream ssValue((
char*)datValue.get_data(), (
char*)datValue.get_data() + datValue.get_size(),
SER_DISK, CLIENT_VERSION);
233 }
catch (
const std::exception&) {
239 free(datValue.get_data());
241 return ret == 0 && success;
244 template <
typename K,
typename T>
245 bool Write(
const K& key,
const T& value,
bool fOverwrite =
true)
250 assert(!
"Write called on database in read-only mode");
256 Dbt datKey(ssKey.
data(), ssKey.
size());
262 Dbt datValue(ssValue.
data(), ssValue.
size());
265 int ret =
pdb->put(
activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
273 template <
typename K>
279 assert(!
"Erase called on database in read-only mode");
285 Dbt datKey(ssKey.
data(), ssKey.
size());
292 return (ret == 0 || ret == DB_NOTFOUND);
295 template <
typename K>
305 Dbt datKey(ssKey.
data(), ssKey.
size());
319 Dbc* pcursor =
nullptr;
320 int ret =
pdb->cursor(
nullptr, &pcursor, 0);
330 unsigned int fFlags = DB_NEXT;
332 datKey.set_data(ssKey.
data());
333 datKey.set_size(ssKey.
size());
334 fFlags = DB_SET_RANGE;
337 datKey.set_flags(DB_DBT_MALLOC);
338 datValue.set_flags(DB_DBT_MALLOC);
339 int ret = pcursor->get(&datKey, &datValue, fFlags);
342 else if (datKey.get_data() ==
nullptr || datValue.get_data() ==
nullptr)
348 ssKey.
write((
char*)datKey.get_data(), datKey.get_size());
351 ssValue.
write((
char*)datValue.get_data(), datValue.get_size());
356 free(datKey.get_data());
357 free(datValue.get_data());
394 return Read(std::string(
"version"), nVersion);
399 return Write(std::string(
"version"), nVersion);
RAII class that provides access to a Berkeley database.
bool WriteVersion(int nVersion)
BerkeleyBatch(const BerkeleyBatch &)=delete
bool ReadVersion(int &nVersion)
static bool VerifyEnvironment(const fs::path &file_path, std::string &errorStr)
BerkeleyBatch & operator=(const BerkeleyBatch &)=delete
bool Exists(const K &key)
static bool PeriodicFlush(BerkeleyDatabase &database)
static bool VerifyDatabaseFile(const fs::path &file_path, std::string &warningStr, std::string &errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc)
int ReadAtCursor(Dbc *pcursor, CDataStream &ssKey, CDataStream &ssValue, bool setRange=false)
BerkeleyEnvironment * env
bool Write(const K &key, const T &value, bool fOverwrite=true)
static bool Recover(const fs::path &file_path, void *callbackDataIn, bool(*recoverKVcallback)(void *callbackData, CDataStream ssKey, CDataStream ssValue), std::string &out_backup_filename)
BerkeleyBatch(BerkeleyDatabase &database, const char *pszMode="r+", bool fFlushOnCloseIn=true)
static bool Rewrite(BerkeleyDatabase &database, const char *pszSkip=nullptr)
bool Read(const K &key, T &value)
An instance of this class represents one database.
bool Rewrite(const char *pszSkip=nullptr)
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
void Flush(bool shutdown)
Make sure all changes are flushed to disk.
BerkeleyEnvironment * env
BerkeleyDB specific.
bool Backup(const std::string &strDest)
Back up the entire database to a file.
BerkeleyDatabase(const fs::path &wallet_path, bool mock=false)
Create DB handle to real database.
void IncrementUpdateCounter()
bool IsDummy()
Return whether this database handle is a dummy for testing.
static std::unique_ptr< BerkeleyDatabase > CreateDummy()
Return object for accessing dummy database with no read/write capabilities.
int64_t nLastWalletUpdate
unsigned int nLastFlushed
static std::unique_ptr< BerkeleyDatabase > Create(const fs::path &path)
Return object for accessing database at specified path.
BerkeleyDatabase()
Create dummy DB handle.
static std::unique_ptr< BerkeleyDatabase > CreateMock()
Return object for accessing temporary in-memory database.
std::atomic< unsigned int > nUpdateCounter
std::unordered_map< std::string, WalletDatabaseFileId > m_fileids
bool IsInitialized() const
std::map< std::string, Db * > mapDb
fs::path Directory() const
std::map< std::string, int > mapFileUseCount
bool(* recoverFunc_type)(const fs::path &file_path, std::string &out_backup_filename)
BerkeleyEnvironment(const fs::path &env_directory)
VerifyResult Verify(const std::string &strFile, recoverFunc_type recoverFunc, std::string &out_backup_filename)
std::pair< std::vector< unsigned char >, std::vector< unsigned char > > KeyValPair
Salvage data from a file that Verify says is bad.
VerifyResult
Verify that database file strFile is OK.
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
std::unique_ptr< DbEnv > dbenv
std::condition_variable_any m_db_in_use
void CheckpointLSN(const std::string &strFile)
void Flush(bool fShutdown)
bool Salvage(const std::string &strFile, bool fAggressive, std::vector< KeyValPair > &vResult)
void CloseDb(const std::string &strFile)
void write(const char *pch, size_t nSize)
void reserve(size_type n)
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.
#define T(expected, seed, data)
u_int8_t value[DB_FILE_ID_LEN]
bool operator==(const WalletDatabaseFileId &rhs) const