Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/bdb.h
Show First 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | |||||
bool IsBDBWalletLoaded(const fs::path &wallet_path); | bool IsBDBWalletLoaded(const fs::path &wallet_path); | ||||
class BerkeleyBatch; | class BerkeleyBatch; | ||||
/** | /** | ||||
* An instance of this class represents one database. | * An instance of this class represents one database. | ||||
* For BerkeleyDB this is just a (env, strFile) tuple. | * For BerkeleyDB this is just a (env, strFile) tuple. | ||||
*/ | */ | ||||
class BerkeleyDatabase { | class BerkeleyDatabase : public WalletDatabase { | ||||
friend class BerkeleyBatch; | friend class BerkeleyBatch; | ||||
public: | public: | ||||
/** Create dummy DB handle */ | /** Create dummy DB handle */ | ||||
BerkeleyDatabase() | BerkeleyDatabase() : WalletDatabase(), env(nullptr) {} | ||||
: nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), | |||||
nLastWalletUpdate(0), env(nullptr) {} | |||||
/** Create DB handle to real database */ | /** Create DB handle to real database */ | ||||
BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> envIn, | BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> envIn, | ||||
std::string filename) | std::string filename) | ||||
: nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), | : WalletDatabase(), env(std::move(envIn)), | ||||
nLastWalletUpdate(0), env(std::move(envIn)), | |||||
strFile(std::move(filename)) { | strFile(std::move(filename)) { | ||||
auto inserted = | auto inserted = | ||||
this->env->m_databases.emplace(strFile, std::ref(*this)); | this->env->m_databases.emplace(strFile, std::ref(*this)); | ||||
assert(inserted.second); | assert(inserted.second); | ||||
} | } | ||||
~BerkeleyDatabase(); | ~BerkeleyDatabase() override; | ||||
/** | /** | ||||
* Open the database if it is not already opened. | * Open the database if it is not already opened. | ||||
* Dummy function, doesn't do anything right now, but is needed for class | * Dummy function, doesn't do anything right now, but is needed for class | ||||
* abstraction | * abstraction | ||||
*/ | */ | ||||
void Open(const char *mode); | void Open(const char *mode) override; | ||||
/** | /** | ||||
* Rewrite the entire database on disk, with the exception of key pszSkip if | * Rewrite the entire database on disk, with the exception of key pszSkip if | ||||
* non-zero | * non-zero | ||||
*/ | */ | ||||
bool Rewrite(const char *pszSkip = nullptr); | bool Rewrite(const char *pszSkip = nullptr) override; | ||||
/** Indicate the a new database user has began using the database. */ | /** Indicate the a new database user has began using the database. */ | ||||
void AddRef(); | void AddRef() override; | ||||
/** | /** | ||||
* Indicate that database user has stopped using the database and that it | * Indicate that database user has stopped using the database and that it | ||||
* could be flushed or closed. | * could be flushed or closed. | ||||
*/ | */ | ||||
void RemoveRef(); | void RemoveRef() override; | ||||
/** | /** | ||||
* Back up the entire database to a file. | * Back up the entire database to a file. | ||||
*/ | */ | ||||
bool Backup(const std::string &strDest) const; | bool Backup(const std::string &strDest) const override; | ||||
/** | /** | ||||
* Make sure all changes are flushed to database file. | * Make sure all changes are flushed to database file. | ||||
*/ | */ | ||||
void Flush(); | void Flush() override; | ||||
/** | /** | ||||
* Flush to the database file and close the database. | * Flush to the database file and close the database. | ||||
* Also close the environment if no other databases are open in it. | * Also close the environment if no other databases are open in it. | ||||
*/ | */ | ||||
void Close(); | void Close() override; | ||||
/** | /** | ||||
* flush the wallet passively (TRY_LOCK) | * flush the wallet passively (TRY_LOCK) | ||||
* ideal to be called periodically | * ideal to be called periodically | ||||
*/ | */ | ||||
bool PeriodicFlush(); | bool PeriodicFlush() override; | ||||
void IncrementUpdateCounter(); | void IncrementUpdateCounter() override; | ||||
void ReloadDbEnv(); | |||||
std::atomic<unsigned int> nUpdateCounter; | void ReloadDbEnv() override; | ||||
unsigned int nLastSeen; | |||||
unsigned int nLastFlushed; | |||||
int64_t nLastWalletUpdate; | |||||
/** Verifies the environment and database file */ | /** Verifies the environment and database file */ | ||||
bool Verify(bilingual_str &error); | bool Verify(bilingual_str &error) override; | ||||
/** | /** | ||||
* Pointer to shared database environment. | * Pointer to shared database environment. | ||||
* | * | ||||
* Normally there is only one BerkeleyDatabase object per | * Normally there is only one BerkeleyDatabase object per | ||||
* BerkeleyEnvivonment, but in the special, backwards compatible case where | * BerkeleyEnvivonment, but in the special, backwards compatible case where | ||||
* multiple wallet BDB data files are loaded from the same directory, this | * multiple wallet BDB data files are loaded from the same directory, this | ||||
* will point to a shared instance that gets freed when the last data file | * will point to a shared instance that gets freed when the last data file | ||||
* is closed. | * is closed. | ||||
*/ | */ | ||||
std::shared_ptr<BerkeleyEnvironment> env; | std::shared_ptr<BerkeleyEnvironment> env; | ||||
/** | /** | ||||
* Database pointer. This is initialized lazily and reset during flushes, | * Database pointer. This is initialized lazily and reset during flushes, | ||||
* so it can be null. | * so it can be null. | ||||
*/ | */ | ||||
std::unique_ptr<Db> m_db; | std::unique_ptr<Db> m_db; | ||||
/** Make a BerkeleyBatch connected to this database */ | /** Make a BerkeleyBatch connected to this database */ | ||||
std::unique_ptr<BerkeleyBatch> MakeBatch(const char *mode, | std::unique_ptr<DatabaseBatch> | ||||
bool flush_on_close); | MakeBatch(const char *mode = "r+", bool flush_on_close = true) override; | ||||
private: | private: | ||||
std::string strFile; | std::string strFile; | ||||
/** | /** | ||||
* Return whether this database handle is a dummy for testing. | * Return whether this database handle is a dummy for testing. | ||||
* Only to be used at a low level, application should ideally not care | * Only to be used at a low level, application should ideally not care | ||||
* about this. | * about this. | ||||
▲ Show 20 Lines • Show All 64 Lines • Show Last 20 Lines |