6 #ifndef FABCOIN_WALLET_DB_H 7 #define FABCOIN_WALLET_DB_H 23 static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
24 static const bool DEFAULT_WALLET_PRIVDB =
true;
41 std::map<std::string, Db*>
mapDb;
59 typedef bool (*
recoverFunc_type)(
const std::string& strFile, std::string& out_backup_filename);
68 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> >
KeyValPair;
69 bool Salvage(
const std::string& strFile,
bool fAggressive, std::vector<KeyValPair>& vResult);
71 bool Open(
const fs::path& path);
73 void Flush(
bool fShutdown);
76 void CloseDb(
const std::string& strFile);
78 DbTxn*
TxnBegin(
int flags = DB_TXN_WRITE_NOSYNC)
80 DbTxn* ptxn =
nullptr;
81 int ret = dbenv->txn_begin(
nullptr, &ptxn, flags);
82 if (!ptxn || ret != 0)
104 nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0),
env(env_in),
strFile(strFile_in)
110 bool Rewrite(
const char* pszSkip=
nullptr);
114 bool Backup(
const std::string& strDest);
122 void Flush(
bool shutdown);
124 void IncrementUpdateCounter();
156 explicit CDB(
CWalletDBWrapper& dbw,
const char* pszMode =
"r+",
bool fFlushOnCloseIn=
true);
161 static bool Recover(
const std::string& filename,
void *callbackDataIn,
bool (*recoverKVcallback)(
void* callbackData,
CDataStream ssKey,
CDataStream ssValue), std::string& out_backup_filename);
167 static bool VerifyEnvironment(
const std::string& walletFile,
const fs::path& dataDir, std::string& errorStr);
169 static bool VerifyDatabaseFile(
const std::string& walletFile,
const fs::path& dataDir, std::string& warningStr, std::string& errorStr,
CDBEnv::recoverFunc_type recoverFunc);
173 void operator=(
const CDB&);
176 template <
typename K,
typename T>
186 Dbt datKey(ssKey.
data(), ssKey.
size());
190 datValue.set_flags(DB_DBT_MALLOC);
191 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
193 bool success =
false;
194 if (datValue.get_data() !=
nullptr) {
197 CDataStream ssValue((
char*)datValue.get_data(), (
char*)datValue.get_data() + datValue.get_size(),
SER_DISK, CLIENT_VERSION);
200 }
catch (
const std::exception&) {
206 free(datValue.get_data());
208 return ret == 0 && success;
211 template <
typename K,
typename T>
212 bool Write(
const K& key,
const T& value,
bool fOverwrite =
true)
217 assert(!
"Write called on database in read-only mode");
223 Dbt datKey(ssKey.
data(), ssKey.
size());
227 ssValue.reserve(10000);
229 Dbt datValue(ssValue.data(), ssValue.size());
232 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
240 template <
typename K>
246 assert(!
"Erase called on database in read-only mode");
252 Dbt datKey(ssKey.
data(), ssKey.
size());
255 int ret = pdb->del(activeTxn, &datKey, 0);
259 return (ret == 0 || ret == DB_NOTFOUND);
262 template <
typename K>
272 Dbt datKey(ssKey.
data(), ssKey.
size());
275 int ret = pdb->exists(activeTxn, &datKey, 0);
286 Dbc* pcursor =
nullptr;
287 int ret = pdb->cursor(
nullptr, &pcursor, 0);
297 unsigned int fFlags = DB_NEXT;
299 datKey.set_data(ssKey.
data());
300 datKey.set_size(ssKey.
size());
301 fFlags = DB_SET_RANGE;
304 datKey.set_flags(DB_DBT_MALLOC);
305 datValue.set_flags(DB_DBT_MALLOC);
306 int ret = pcursor->get(&datKey, &datValue, fFlags);
309 else if (datKey.get_data() ==
nullptr || datValue.get_data() ==
nullptr)
315 ssKey.
write((
char*)datKey.get_data(), datKey.get_size());
318 ssValue.
write((
char*)datValue.get_data(), datValue.get_size());
323 free(datKey.get_data());
324 free(datValue.get_data());
331 if (!pdb || activeTxn)
342 if (!pdb || !activeTxn)
344 int ret = activeTxn->commit(0);
351 if (!pdb || !activeTxn)
353 int ret = activeTxn->abort();
361 return Read(std::string(
"version"), nVersion);
366 return Write(std::string(
"version"), nVersion);
372 #endif // FABCOIN_WALLET_DB_H
std::map< std::string, int > mapFileUseCount
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
bool Write(const K &key, const T &value, bool fOverwrite=true)
void Flush(bool fShutdown)
assert(len-trim+(2 *lenIndices)<=WIDTH)
bool Exists(const K &key)
Double ended buffer combining vector and stream-like interfaces.
void write(const char *pch, size_t nSize)
bool Salvage(const std::string &strFile, bool fAggressive, std::vector< KeyValPair > &vResult)
CWalletDBWrapper(CDBEnv *env_in, const std::string &strFile_in)
Create DB handle to real database.
std::atomic< unsigned int > nUpdateCounter
bool IsDummy()
Return whether this database handle is a dummy for testing.
VerifyResult
Verify that database file strFile is OK.
void memory_cleanse(void *ptr, size_t len)
An instance of this class represents one database.
std::map< std::string, Db * > mapDb
bool ReadVersion(int &nVersion)
std::string GetName() const
Get a name for this database, for debugging etc.
unsigned int nLastFlushed
RAII class that provides access to a Berkeley database.
int64_t nLastWalletUpdate
bool(* recoverFunc_type)(const std::string &strFile, std::string &out_backup_filename)
bool Read(const K &key, T &value)
static bool Rewrite(CWalletDBWrapper &dbw, const char *pszSkip=nullptr)
void reserve(size_type n)
void CloseDb(const std::string &strFile)
bool WriteVersion(int nVersion)
int ReadAtCursor(Dbc *pcursor, CDataStream &ssKey, CDataStream &ssValue, bool setRange=false)
bool Open(const fs::path &path)
void CheckpointLSN(const std::string &strFile)
CWalletDBWrapper()
Create dummy DB handle.
std::pair< std::vector< unsigned char >, std::vector< unsigned char > > KeyValPair
Salvage data from a file that Verify says is bad.
CDBEnv * env
BerkeleyDB specific.
VerifyResult Verify(const std::string &strFile, recoverFunc_type recoverFunc, std::string &out_backup_filename)
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...