db.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_DB_H
  3. #define ECOIN_DB_H
  4. #include "main.h"
  5. #include <map>
  6. #include <string>
  7. #include <vector>
  8. #include <db_cxx.h>
  9. class CAddress;
  10. class CAddrMan;
  11. class CBlockLocator;
  12. class CDiskBlockIndex;
  13. class CDiskTxPos;
  14. class CMasterKey;
  15. class COutPoint;
  16. class CTxIndex;
  17. class CWallet;
  18. class CWalletTx;
  19. extern unsigned int nWalletDBUpdated;
  20. void ThreadFlushWalletDB(void* parg);
  21. bool BackupWallet(const CWallet& wallet, const std::string& strDest);
  22. class CDBEnv
  23. {
  24. private:
  25. bool fDetachDB;
  26. bool fDbEnvInit;
  27. bool fMockDb;
  28. boost::filesystem::path pathEnv;
  29. std::string strPath;
  30. void EnvShutdown();
  31. public:
  32. mutable CCriticalSection cs_db;
  33. DbEnv dbenv;
  34. std::map<std::string, int> mapFileUseCount;
  35. std::map<std::string, Db*> mapDb;
  36. CDBEnv();
  37. ~CDBEnv();
  38. void MakeMock();
  39. bool IsMock() { return fMockDb; };
  40. enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
  41. VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
  42. typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
  43. bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
  44. bool Open(boost::filesystem::path pathEnv_);
  45. void Close();
  46. void Flush(bool fShutdown);
  47. void CheckpointLSN(std::string strFile);
  48. void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
  49. bool GetDetach() { return fDetachDB; }
  50. void CloseDb(const std::string& strFile);
  51. bool RemoveDb(const std::string& strFile);
  52. DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
  53. {
  54. DbTxn* ptxn = NULL;
  55. int ret = dbenv.txn_begin(NULL, &ptxn, flags);
  56. if (!ptxn || ret != 0)
  57. return NULL;
  58. return ptxn;
  59. }
  60. };
  61. extern CDBEnv bitdb;
  62. class CDB
  63. {
  64. protected:
  65. Db* pdb;
  66. std::string strFile;
  67. DbTxn *activeTxn;
  68. bool fReadOnly;
  69. explicit CDB(const char* pszFile, const char* pszMode="r+");
  70. ~CDB() { Close(); }
  71. public:
  72. void Close();
  73. private:
  74. CDB(const CDB&);
  75. void operator=(const CDB&);
  76. protected:
  77. template<typename K, typename T>
  78. bool Read(const K& key, T& value)
  79. {
  80. if (!pdb)
  81. return false;
  82. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  83. ssKey.reserve(1000);
  84. ssKey << key;
  85. Dbt datKey(&ssKey[0], ssKey.size());
  86. Dbt datValue;
  87. datValue.set_flags(DB_DBT_MALLOC);
  88. int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
  89. memset(datKey.get_data(), 0, datKey.get_size());
  90. if (datValue.get_data() == NULL)
  91. return false;
  92. try {
  93. CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
  94. ssValue >> value;
  95. }
  96. catch (std::exception &e) {
  97. return false;
  98. }
  99. memset(datValue.get_data(), 0, datValue.get_size());
  100. free(datValue.get_data());
  101. return (ret == 0);
  102. }
  103. template<typename K, typename T>
  104. bool Write(const K& key, const T& value, bool fOverwrite=true)
  105. {
  106. if (!pdb)
  107. return false;
  108. if (fReadOnly)
  109. assert(!"Write called on database in read-only mode");
  110. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  111. ssKey.reserve(1000);
  112. ssKey << key;
  113. Dbt datKey(&ssKey[0], ssKey.size());
  114. CDataStream ssValue(SER_DISK, CLIENT_VERSION);
  115. ssValue.reserve(10000);
  116. ssValue << value;
  117. Dbt datValue(&ssValue[0], ssValue.size());
  118. int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
  119. memset(datKey.get_data(), 0, datKey.get_size());
  120. memset(datValue.get_data(), 0, datValue.get_size());
  121. return (ret == 0);
  122. }
  123. template<typename K>
  124. bool Erase(const K& key)
  125. {
  126. if (!pdb)
  127. return false;
  128. if (fReadOnly)
  129. assert(!"Erase called on database in read-only mode");
  130. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  131. ssKey.reserve(1000);
  132. ssKey << key;
  133. Dbt datKey(&ssKey[0], ssKey.size());
  134. int ret = pdb->del(activeTxn, &datKey, 0);
  135. memset(datKey.get_data(), 0, datKey.get_size());
  136. return (ret == 0 || ret == DB_NOTFOUND);
  137. }
  138. template<typename K>
  139. bool Exists(const K& key)
  140. {
  141. if (!pdb)
  142. return false;
  143. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  144. ssKey.reserve(1000);
  145. ssKey << key;
  146. Dbt datKey(&ssKey[0], ssKey.size());
  147. int ret = pdb->exists(activeTxn, &datKey, 0);
  148. memset(datKey.get_data(), 0, datKey.get_size());
  149. return (ret == 0);
  150. }
  151. Dbc* GetCursor()
  152. {
  153. if (!pdb)
  154. return NULL;
  155. Dbc* pcursor = NULL;
  156. int ret = pdb->cursor(NULL, &pcursor, 0);
  157. if (ret != 0)
  158. return NULL;
  159. return pcursor;
  160. }
  161. int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
  162. {
  163. Dbt datKey;
  164. if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
  165. {
  166. datKey.set_data(&ssKey[0]);
  167. datKey.set_size(ssKey.size());
  168. }
  169. Dbt datValue;
  170. if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
  171. {
  172. datValue.set_data(&ssValue[0]);
  173. datValue.set_size(ssValue.size());
  174. }
  175. datKey.set_flags(DB_DBT_MALLOC);
  176. datValue.set_flags(DB_DBT_MALLOC);
  177. int ret = pcursor->get(&datKey, &datValue, fFlags);
  178. if (ret != 0)
  179. return ret;
  180. else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
  181. return 99999;
  182. ssKey.SetType(SER_DISK);
  183. ssKey.clear();
  184. ssKey.write((char*)datKey.get_data(), datKey.get_size());
  185. ssValue.SetType(SER_DISK);
  186. ssValue.clear();
  187. ssValue.write((char*)datValue.get_data(), datValue.get_size());
  188. memset(datKey.get_data(), 0, datKey.get_size());
  189. memset(datValue.get_data(), 0, datValue.get_size());
  190. free(datKey.get_data());
  191. free(datValue.get_data());
  192. return 0;
  193. }
  194. public:
  195. bool TxnBegin()
  196. {
  197. if (!pdb || activeTxn)
  198. return false;
  199. DbTxn* ptxn = bitdb.TxnBegin();
  200. if (!ptxn)
  201. return false;
  202. activeTxn = ptxn;
  203. return true;
  204. }
  205. bool TxnCommit()
  206. {
  207. if (!pdb || !activeTxn)
  208. return false;
  209. int ret = activeTxn->commit(0);
  210. activeTxn = NULL;
  211. return (ret == 0);
  212. }
  213. bool TxnAbort()
  214. {
  215. if (!pdb || !activeTxn)
  216. return false;
  217. int ret = activeTxn->abort();
  218. activeTxn = NULL;
  219. return (ret == 0);
  220. }
  221. bool ReadVersion(int& nVersion)
  222. {
  223. nVersion = 0;
  224. return Read(std::string("version"), nVersion);
  225. }
  226. bool WriteVersion(int nVersion)
  227. {
  228. return Write(std::string("version"), nVersion);
  229. }
  230. bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
  231. };
  232. class CAddrDB
  233. {
  234. private:
  235. boost::filesystem::path pathAddr;
  236. public:
  237. CAddrDB();
  238. bool Write(const CAddrMan& addr);
  239. bool Read(CAddrMan& addr);
  240. };
  241. #endif // ECOIN_DB_H