Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/db.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_WALLET_DB_H | #ifndef BITCOIN_WALLET_DB_H | ||||
#define BITCOIN_WALLET_DB_H | #define BITCOIN_WALLET_DB_H | ||||
#include <clientversion.h> | #include <clientversion.h> | ||||
#include <fs.h> | #include <fs.h> | ||||
#include <streams.h> | #include <streams.h> | ||||
#include <atomic> | |||||
#include <memory> | |||||
#include <string> | #include <string> | ||||
struct bilingual_str; | |||||
/** | /** | ||||
* Given a wallet directory path or legacy file path, return path to main data | * Given a wallet directory path or legacy file path, return path to main data | ||||
* file in the wallet database. */ | * file in the wallet database. */ | ||||
fs::path WalletDataFilePath(const fs::path &wallet_path); | fs::path WalletDataFilePath(const fs::path &wallet_path); | ||||
void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, | void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, | ||||
std::string &database_filename); | std::string &database_filename); | ||||
/** RAII class that provides access to a WalletDatabase */ | /** RAII class that provides access to a WalletDatabase */ | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | public: | ||||
virtual bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, | virtual bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, | ||||
bool &complete) = 0; | bool &complete) = 0; | ||||
virtual void CloseCursor() = 0; | virtual void CloseCursor() = 0; | ||||
virtual bool TxnBegin() = 0; | virtual bool TxnBegin() = 0; | ||||
virtual bool TxnCommit() = 0; | virtual bool TxnCommit() = 0; | ||||
virtual bool TxnAbort() = 0; | virtual bool TxnAbort() = 0; | ||||
}; | }; | ||||
/** | |||||
* An instance of this class represents one database. | |||||
*/ | |||||
class WalletDatabase { | |||||
public: | |||||
/** Create dummy DB handle */ | |||||
WalletDatabase() | |||||
: nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), | |||||
nLastWalletUpdate(0) {} | |||||
virtual ~WalletDatabase(){}; | |||||
/** Open the database if it is not already opened. */ | |||||
virtual void Open(const char *mode) = 0; | |||||
//! Counts the number of active database users to be sure that the database | |||||
//! is not closed while someone is using it | |||||
std::atomic<int> m_refcount{0}; | |||||
/** | |||||
* Indicate the a new database user has began using the database. | |||||
* Increments m_refcount | |||||
*/ | |||||
virtual void AddRef() = 0; | |||||
/** | |||||
* Indicate that database user has stopped using the database and that it | |||||
* could be flushed or closed. Decrement m_refcount | |||||
*/ | |||||
virtual void RemoveRef() = 0; | |||||
/** | |||||
* Rewrite the entire database on disk, with the exception of key pszSkip | |||||
* if non-zero | |||||
*/ | |||||
virtual bool Rewrite(const char *pszSkip = nullptr) = 0; | |||||
/** | |||||
* Back up the entire database to a file. | |||||
*/ | |||||
virtual bool Backup(const std::string &strDest) const = 0; | |||||
/** | |||||
* Make sure all changes are flushed to database file. | |||||
*/ | |||||
virtual void Flush() = 0; | |||||
/** | |||||
* Flush to the database file and close the database. | |||||
* Also close the environment if no other databases are open in it. | |||||
*/ | |||||
virtual void Close() = 0; | |||||
/* flush the wallet passively (TRY_LOCK) | |||||
ideal to be called periodically */ | |||||
virtual bool PeriodicFlush() = 0; | |||||
virtual void IncrementUpdateCounter() = 0; | |||||
virtual void ReloadDbEnv() = 0; | |||||
std::atomic<unsigned int> nUpdateCounter; | |||||
unsigned int nLastSeen; | |||||
unsigned int nLastFlushed; | |||||
int64_t nLastWalletUpdate; | |||||
/** Verifies the environment and database file */ | |||||
virtual bool Verify(bilingual_str &error) = 0; | |||||
std::string m_file_path; | |||||
/** Make a DatabaseBatch connected to this database */ | |||||
virtual std::unique_ptr<DatabaseBatch> | |||||
MakeBatch(const char *mode = "r+", bool flush_on_close = true) = 0; | |||||
}; | |||||
#endif // BITCOIN_WALLET_DB_H | #endif // BITCOIN_WALLET_DB_H |