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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_DB_H
  6. #define BITCOIN_DB_H
  7. #include "serialize.h"
  8. #include "sync.h"
  9. #include "version.h"
  10. #include <map>
  11. #include <string>
  12. #include <vector>
  13. #include <boost/filesystem/path.hpp>
  14. #include <db_cxx.h>
  15. class CAddrMan;
  16. struct CBlockLocator;
  17. class CDiskBlockIndex;
  18. class COutPoint;
  19. extern unsigned int nWalletDBUpdated;
  20. void ThreadFlushWalletDB(const std::string& strWalletFile);
  21. class CDBEnv
  22. {
  23. private:
  24. bool fDbEnvInit;
  25. bool fMockDb;
  26. boost::filesystem::path path;
  27. void EnvShutdown();
  28. public:
  29. mutable CCriticalSection cs_db;
  30. DbEnv dbenv;
  31. std::map<std::string, int> mapFileUseCount;
  32. std::map<std::string, Db*> mapDb;
  33. CDBEnv();
  34. ~CDBEnv();
  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, RECOVER_OK, RECOVER_FAIL };
  44. VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
  45. /*
  46. * Salvage data from a file that Verify says is bad.
  47. * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
  48. * Appends binary key/value pairs to vResult, returns true if successful.
  49. * NOTE: reads the entire database into memory, so cannot be used
  50. * for huge databases.
  51. */
  52. typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
  53. bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
  54. bool Open(const boost::filesystem::path &path);
  55. void Close();
  56. void Flush(bool fShutdown);
  57. void CheckpointLSN(std::string strFile);
  58. void CloseDb(const std::string& strFile);
  59. bool RemoveDb(const std::string& strFile);
  60. DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
  61. {
  62. DbTxn* ptxn = NULL;
  63. int ret = dbenv.txn_begin(NULL, &ptxn, flags);
  64. if (!ptxn || ret != 0)
  65. return NULL;
  66. return ptxn;
  67. }
  68. };
  69. extern CDBEnv bitdb;
  70. /** RAII class that provides access to a Berkeley database */
  71. class CDB
  72. {
  73. protected:
  74. Db* pdb;
  75. std::string strFile;
  76. DbTxn *activeTxn;
  77. bool fReadOnly;
  78. explicit CDB(const char* pszFile, const char* pszMode="r+");
  79. ~CDB() { Close(); }
  80. public:
  81. void Flush();
  82. void Close();
  83. private:
  84. CDB(const CDB&);
  85. void operator=(const CDB&);
  86. protected:
  87. template<typename K, typename T>
  88. bool Read(const K& key, T& value)
  89. {
  90. if (!pdb)
  91. return false;
  92. // Key
  93. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  94. ssKey.reserve(1000);
  95. ssKey << key;
  96. Dbt datKey(&ssKey[0], ssKey.size());
  97. // Read
  98. Dbt datValue;
  99. datValue.set_flags(DB_DBT_MALLOC);
  100. int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
  101. memset(datKey.get_data(), 0, datKey.get_size());
  102. if (datValue.get_data() == NULL)
  103. return false;
  104. // Unserialize value
  105. try {
  106. CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
  107. ssValue >> value;
  108. }
  109. catch (std::exception &e) {
  110. return false;
  111. }
  112. // Clear and free memory
  113. memset(datValue.get_data(), 0, datValue.get_size());
  114. free(datValue.get_data());
  115. return (ret == 0);
  116. }
  117. template<typename K, typename T>
  118. bool Write(const K& key, const T& value, bool fOverwrite=true)
  119. {
  120. if (!pdb)
  121. return false;
  122. if (fReadOnly)
  123. assert(!"Write called on database in read-only mode");
  124. // Key
  125. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  126. ssKey.reserve(1000);
  127. ssKey << key;
  128. Dbt datKey(&ssKey[0], ssKey.size());
  129. // Value
  130. CDataStream ssValue(SER_DISK, CLIENT_VERSION);
  131. ssValue.reserve(10000);
  132. ssValue << value;
  133. Dbt datValue(&ssValue[0], ssValue.size());
  134. // Write
  135. int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
  136. // Clear memory in case it was a private key
  137. memset(datKey.get_data(), 0, datKey.get_size());
  138. memset(datValue.get_data(), 0, datValue.get_size());
  139. return (ret == 0);
  140. }
  141. template<typename K>
  142. bool Erase(const K& key)
  143. {
  144. if (!pdb)
  145. return false;
  146. if (fReadOnly)
  147. assert(!"Erase called on database in read-only mode");
  148. // Key
  149. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  150. ssKey.reserve(1000);
  151. ssKey << key;
  152. Dbt datKey(&ssKey[0], ssKey.size());
  153. // Erase
  154. int ret = pdb->del(activeTxn, &datKey, 0);
  155. // Clear memory
  156. memset(datKey.get_data(), 0, datKey.get_size());
  157. return (ret == 0 || ret == DB_NOTFOUND);
  158. }
  159. template<typename K>
  160. bool Exists(const K& key)
  161. {
  162. if (!pdb)
  163. return false;
  164. // Key
  165. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  166. ssKey.reserve(1000);
  167. ssKey << key;
  168. Dbt datKey(&ssKey[0], ssKey.size());
  169. // Exists
  170. int ret = pdb->exists(activeTxn, &datKey, 0);
  171. // Clear memory
  172. memset(datKey.get_data(), 0, datKey.get_size());
  173. return (ret == 0);
  174. }
  175. Dbc* GetCursor()
  176. {
  177. if (!pdb)
  178. return NULL;
  179. Dbc* pcursor = NULL;
  180. int ret = pdb->cursor(NULL, &pcursor, 0);
  181. if (ret != 0)
  182. return NULL;
  183. return pcursor;
  184. }
  185. int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
  186. {
  187. // Read at cursor
  188. Dbt datKey;
  189. if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
  190. {
  191. datKey.set_data(&ssKey[0]);
  192. datKey.set_size(ssKey.size());
  193. }
  194. Dbt datValue;
  195. if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
  196. {
  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. /** Access to the (IP) address database (peers.dat) */
  260. class CAddrDB
  261. {
  262. private:
  263. boost::filesystem::path pathAddr;
  264. public:
  265. CAddrDB();
  266. bool Write(const CAddrMan& addr);
  267. bool Read(CAddrMan& addr);
  268. };
  269. #endif // BITCOIN_DB_H