You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db.h 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_WALLET_DB_H
  6. #define BITCOIN_WALLET_DB_H
  7. #include "clientversion.h"
  8. #include "serialize.h"
  9. #include "streams.h"
  10. #include "sync.h"
  11. #include "version.h"
  12. #include <map>
  13. #include <string>
  14. #include <vector>
  15. #include <boost/filesystem/path.hpp>
  16. #include <db_cxx.h>
  17. class CDiskBlockIndex;
  18. class COutPoint;
  19. extern unsigned int nWalletDBUpdated;
  20. class CDBEnv
  21. {
  22. private:
  23. bool fDbEnvInit;
  24. bool fMockDb;
  25. boost::filesystem::path path;
  26. void EnvShutdown();
  27. public:
  28. mutable CCriticalSection cs_db;
  29. DbEnv *dbenv;
  30. std::map<std::string, int> mapFileUseCount;
  31. std::map<std::string, Db*> mapDb;
  32. CDBEnv();
  33. ~CDBEnv();
  34. void Reset();
  35. void MakeMock();
  36. bool IsMock() { return fMockDb; }
  37. /**
  38. * Verify that database file strFile is OK. If it is not,
  39. * call the callback to try to recover.
  40. * This must be called BEFORE strFile is opened.
  41. * Returns true if strFile is OK.
  42. */
  43. enum VerifyResult { VERIFY_OK,
  44. RECOVER_OK,
  45. RECOVER_FAIL };
  46. VerifyResult Verify(const std::string& strFile, bool (*recoverFunc)(CDBEnv& dbenv, const std::string& strFile));
  47. /**
  48. * Salvage data from a file that Verify says is bad.
  49. * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
  50. * Appends binary key/value pairs to vResult, returns true if successful.
  51. * NOTE: reads the entire database into memory, so cannot be used
  52. * for huge databases.
  53. */
  54. typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
  55. bool Salvage(const std::string& strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
  56. bool Open(const boost::filesystem::path& path);
  57. void Close();
  58. void Flush(bool fShutdown);
  59. void CheckpointLSN(const std::string& strFile);
  60. void CloseDb(const std::string& strFile);
  61. bool RemoveDb(const std::string& strFile);
  62. DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
  63. {
  64. DbTxn* ptxn = NULL;
  65. int ret = dbenv->txn_begin(NULL, &ptxn, flags);
  66. if (!ptxn || ret != 0)
  67. return NULL;
  68. return ptxn;
  69. }
  70. };
  71. extern CDBEnv bitdb;
  72. /** RAII class that provides access to a Berkeley database */
  73. class CDB
  74. {
  75. protected:
  76. Db* pdb;
  77. std::string strFile;
  78. DbTxn* activeTxn;
  79. bool fReadOnly;
  80. bool fFlushOnClose;
  81. explicit CDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
  82. ~CDB() { Close(); }
  83. public:
  84. void Flush();
  85. void Close();
  86. private:
  87. CDB(const CDB&);
  88. void operator=(const CDB&);
  89. protected:
  90. template <typename K, typename T>
  91. bool Read(const K& key, T& value)
  92. {
  93. if (!pdb)
  94. return false;
  95. // Key
  96. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  97. ssKey.reserve(1000);
  98. ssKey << key;
  99. Dbt datKey(&ssKey[0], ssKey.size());
  100. // Read
  101. Dbt datValue;
  102. datValue.set_flags(DB_DBT_MALLOC);
  103. int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
  104. memset(datKey.get_data(), 0, datKey.get_size());
  105. if (datValue.get_data() == NULL)
  106. return false;
  107. // Unserialize value
  108. try {
  109. CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
  110. ssValue >> value;
  111. } catch (const std::exception&) {
  112. return false;
  113. }
  114. // Clear and free memory
  115. memset(datValue.get_data(), 0, datValue.get_size());
  116. free(datValue.get_data());
  117. return (ret == 0);
  118. }
  119. template <typename K, typename T>
  120. bool Write(const K& key, const T& value, bool fOverwrite = true)
  121. {
  122. if (!pdb)
  123. return false;
  124. if (fReadOnly)
  125. assert(!"Write called on database in read-only mode");
  126. // Key
  127. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  128. ssKey.reserve(1000);
  129. ssKey << key;
  130. Dbt datKey(&ssKey[0], ssKey.size());
  131. // Value
  132. CDataStream ssValue(SER_DISK, CLIENT_VERSION);
  133. ssValue.reserve(10000);
  134. ssValue << value;
  135. Dbt datValue(&ssValue[0], ssValue.size());
  136. // Write
  137. int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
  138. // Clear memory in case it was a private key
  139. memset(datKey.get_data(), 0, datKey.get_size());
  140. memset(datValue.get_data(), 0, datValue.get_size());
  141. return (ret == 0);
  142. }
  143. template <typename K>
  144. bool Erase(const K& key)
  145. {
  146. if (!pdb)
  147. return false;
  148. if (fReadOnly)
  149. assert(!"Erase called on database in read-only mode");
  150. // Key
  151. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  152. ssKey.reserve(1000);
  153. ssKey << key;
  154. Dbt datKey(&ssKey[0], ssKey.size());
  155. // Erase
  156. int ret = pdb->del(activeTxn, &datKey, 0);
  157. // Clear memory
  158. memset(datKey.get_data(), 0, datKey.get_size());
  159. return (ret == 0 || ret == DB_NOTFOUND);
  160. }
  161. template <typename K>
  162. bool Exists(const K& key)
  163. {
  164. if (!pdb)
  165. return false;
  166. // Key
  167. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  168. ssKey.reserve(1000);
  169. ssKey << key;
  170. Dbt datKey(&ssKey[0], ssKey.size());
  171. // Exists
  172. int ret = pdb->exists(activeTxn, &datKey, 0);
  173. // Clear memory
  174. memset(datKey.get_data(), 0, datKey.get_size());
  175. return (ret == 0);
  176. }
  177. Dbc* GetCursor()
  178. {
  179. if (!pdb)
  180. return NULL;
  181. Dbc* pcursor = NULL;
  182. int ret = pdb->cursor(NULL, &pcursor, 0);
  183. if (ret != 0)
  184. return NULL;
  185. return pcursor;
  186. }
  187. int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags = DB_NEXT)
  188. {
  189. // Read at cursor
  190. Dbt datKey;
  191. if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) {
  192. datKey.set_data(&ssKey[0]);
  193. datKey.set_size(ssKey.size());
  194. }
  195. Dbt datValue;
  196. if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) {
  197. datValue.set_data(&ssValue[0]);
  198. datValue.set_size(ssValue.size());
  199. }
  200. datKey.set_flags(DB_DBT_MALLOC);
  201. datValue.set_flags(DB_DBT_MALLOC);
  202. int ret = pcursor->get(&datKey, &datValue, fFlags);
  203. if (ret != 0)
  204. return ret;
  205. else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
  206. return 99999;
  207. // Convert to streams
  208. ssKey.SetType(SER_DISK);
  209. ssKey.clear();
  210. ssKey.write((char*)datKey.get_data(), datKey.get_size());
  211. ssValue.SetType(SER_DISK);
  212. ssValue.clear();
  213. ssValue.write((char*)datValue.get_data(), datValue.get_size());
  214. // Clear and free memory
  215. memset(datKey.get_data(), 0, datKey.get_size());
  216. memset(datValue.get_data(), 0, datValue.get_size());
  217. free(datKey.get_data());
  218. free(datValue.get_data());
  219. return 0;
  220. }
  221. public:
  222. bool TxnBegin()
  223. {
  224. if (!pdb || activeTxn)
  225. return false;
  226. DbTxn* ptxn = bitdb.TxnBegin();
  227. if (!ptxn)
  228. return false;
  229. activeTxn = ptxn;
  230. return true;
  231. }
  232. bool TxnCommit()
  233. {
  234. if (!pdb || !activeTxn)
  235. return false;
  236. int ret = activeTxn->commit(0);
  237. activeTxn = NULL;
  238. return (ret == 0);
  239. }
  240. bool TxnAbort()
  241. {
  242. if (!pdb || !activeTxn)
  243. return false;
  244. int ret = activeTxn->abort();
  245. activeTxn = NULL;
  246. return (ret == 0);
  247. }
  248. bool ReadVersion(int& nVersion)
  249. {
  250. nVersion = 0;
  251. return Read(std::string("version"), nVersion);
  252. }
  253. bool WriteVersion(int nVersion)
  254. {
  255. return Write(std::string("version"), nVersion);
  256. }
  257. bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
  258. };
  259. #endif // BITCOIN_WALLET_DB_H