123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
- #ifndef ECOIN_DB_H
- #define ECOIN_DB_H
- #include "main.h"
- #include <map>
- #include <string>
- #include <vector>
- #include <db_cxx.h>
- class CAddress;
- class CAddrMan;
- class CBlockLocator;
- class CDiskBlockIndex;
- class CDiskTxPos;
- class CMasterKey;
- class COutPoint;
- class CTxIndex;
- class CWallet;
- class CWalletTx;
- extern unsigned int nWalletDBUpdated;
- void ThreadFlushWalletDB(void* parg);
- bool BackupWallet(const CWallet& wallet, const std::string& strDest);
- class CDBEnv
- {
- private:
- bool fDetachDB;
- bool fDbEnvInit;
- bool fMockDb;
- boost::filesystem::path pathEnv;
- std::string strPath;
- void EnvShutdown();
- public:
- mutable CCriticalSection cs_db;
- DbEnv dbenv;
- std::map<std::string, int> mapFileUseCount;
- std::map<std::string, Db*> mapDb;
- CDBEnv();
- ~CDBEnv();
- void MakeMock();
- bool IsMock() { return fMockDb; };
- enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
- VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
- typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
- bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
- bool Open(boost::filesystem::path pathEnv_);
- void Close();
- void Flush(bool fShutdown);
- void CheckpointLSN(std::string strFile);
- void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
- bool GetDetach() { return fDetachDB; }
- void CloseDb(const std::string& strFile);
- bool RemoveDb(const std::string& strFile);
- DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
- {
- DbTxn* ptxn = NULL;
- int ret = dbenv.txn_begin(NULL, &ptxn, flags);
- if (!ptxn || ret != 0)
- return NULL;
- return ptxn;
- }
- };
- extern CDBEnv bitdb;
- class CDB
- {
- protected:
- Db* pdb;
- std::string strFile;
- DbTxn *activeTxn;
- bool fReadOnly;
- explicit CDB(const char* pszFile, const char* pszMode="r+");
- ~CDB() { Close(); }
- public:
- void Close();
- private:
- CDB(const CDB&);
- void operator=(const CDB&);
- protected:
- template<typename K, typename T>
- bool Read(const K& key, T& value)
- {
- if (!pdb)
- return false;
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- Dbt datKey(&ssKey[0], ssKey.size());
- Dbt datValue;
- datValue.set_flags(DB_DBT_MALLOC);
- int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
- memset(datKey.get_data(), 0, datKey.get_size());
- if (datValue.get_data() == NULL)
- return false;
- try {
- CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
- ssValue >> value;
- }
- catch (std::exception &e) {
- return false;
- }
- memset(datValue.get_data(), 0, datValue.get_size());
- free(datValue.get_data());
- return (ret == 0);
- }
- template<typename K, typename T>
- bool Write(const K& key, const T& value, bool fOverwrite=true)
- {
- if (!pdb)
- return false;
- if (fReadOnly)
- assert(!"Write called on database in read-only mode");
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- Dbt datKey(&ssKey[0], ssKey.size());
- CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- ssValue.reserve(10000);
- ssValue << value;
- Dbt datValue(&ssValue[0], ssValue.size());
- int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
- memset(datKey.get_data(), 0, datKey.get_size());
- memset(datValue.get_data(), 0, datValue.get_size());
- return (ret == 0);
- }
- template<typename K>
- bool Erase(const K& key)
- {
- if (!pdb)
- return false;
- if (fReadOnly)
- assert(!"Erase called on database in read-only mode");
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- Dbt datKey(&ssKey[0], ssKey.size());
- int ret = pdb->del(activeTxn, &datKey, 0);
- memset(datKey.get_data(), 0, datKey.get_size());
- return (ret == 0 || ret == DB_NOTFOUND);
- }
- template<typename K>
- bool Exists(const K& key)
- {
- if (!pdb)
- return false;
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- Dbt datKey(&ssKey[0], ssKey.size());
- int ret = pdb->exists(activeTxn, &datKey, 0);
- memset(datKey.get_data(), 0, datKey.get_size());
- return (ret == 0);
- }
- Dbc* GetCursor()
- {
- if (!pdb)
- return NULL;
- Dbc* pcursor = NULL;
- int ret = pdb->cursor(NULL, &pcursor, 0);
- if (ret != 0)
- return NULL;
- return pcursor;
- }
- int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
- {
- Dbt datKey;
- if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
- {
- datKey.set_data(&ssKey[0]);
- datKey.set_size(ssKey.size());
- }
- Dbt datValue;
- if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
- {
- datValue.set_data(&ssValue[0]);
- datValue.set_size(ssValue.size());
- }
- datKey.set_flags(DB_DBT_MALLOC);
- datValue.set_flags(DB_DBT_MALLOC);
- int ret = pcursor->get(&datKey, &datValue, fFlags);
- if (ret != 0)
- return ret;
- else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
- return 99999;
- ssKey.SetType(SER_DISK);
- ssKey.clear();
- ssKey.write((char*)datKey.get_data(), datKey.get_size());
- ssValue.SetType(SER_DISK);
- ssValue.clear();
- ssValue.write((char*)datValue.get_data(), datValue.get_size());
- memset(datKey.get_data(), 0, datKey.get_size());
- memset(datValue.get_data(), 0, datValue.get_size());
- free(datKey.get_data());
- free(datValue.get_data());
- return 0;
- }
- public:
- bool TxnBegin()
- {
- if (!pdb || activeTxn)
- return false;
- DbTxn* ptxn = bitdb.TxnBegin();
- if (!ptxn)
- return false;
- activeTxn = ptxn;
- return true;
- }
- bool TxnCommit()
- {
- if (!pdb || !activeTxn)
- return false;
- int ret = activeTxn->commit(0);
- activeTxn = NULL;
- return (ret == 0);
- }
- bool TxnAbort()
- {
- if (!pdb || !activeTxn)
- return false;
- int ret = activeTxn->abort();
- activeTxn = NULL;
- return (ret == 0);
- }
- bool ReadVersion(int& nVersion)
- {
- nVersion = 0;
- return Read(std::string("version"), nVersion);
- }
- bool WriteVersion(int nVersion)
- {
- return Write(std::string("version"), nVersion);
- }
- bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
- };
- class CAddrDB
- {
- private:
- boost::filesystem::path pathAddr;
- public:
- CAddrDB();
- bool Write(const CAddrMan& addr);
- bool Read(CAddrMan& addr);
- };
- #endif // ECOIN_DB_H
|