Bitcoin v0.2.0 · 2009-12-16
db.h
1// Copyright (c) 2009 Satoshi Nakamoto2// Distributed under the MIT/X11 software license, see the accompanying3// file license.txt or http://www.opensource.org/licenses/mit-license.php.45#include <db_cxx.h>6class CTransaction;7class CTxIndex;8class CDiskBlockIndex;9class CDiskTxPos;10class COutPoint;11class CUser;12class CReview;13class CAddress;14class CWalletTx;1516extern map<string, string> mapAddressBook;17extern bool fClient;181920extern unsigned int nWalletDBUpdated;21extern DbEnv dbenv;222324extern void DBFlush(bool fShutdown);2526272829class CDB30{31protected:32 Db* pdb;33 string strFile;34 vector<DbTxn*> vTxn;35 bool fReadOnly;3637 explicit CDB(const char* pszFile, const char* pszMode="r+");38 ~CDB() { Close(); }39public:40 void Close();41private:42 CDB(const CDB&);43 void operator=(const CDB&);4445protected:46 template<typename K, typename T>47 bool Read(const K& key, T& value)48 {49 if (!pdb)50 return false;5152 // Key53 CDataStream ssKey(SER_DISK);54 ssKey.reserve(1000);55 ssKey << key;56 Dbt datKey(&ssKey[0], ssKey.size());5758 // Read59 Dbt datValue;60 datValue.set_flags(DB_DBT_MALLOC);61 int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);62 memset(datKey.get_data(), 0, datKey.get_size());63 if (datValue.get_data() == NULL)64 return false;6566 // Unserialize value67 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);68 ssValue >> value;6970 // Clear and free memory71 memset(datValue.get_data(), 0, datValue.get_size());72 free(datValue.get_data());73 return (ret == 0);74 }7576 template<typename K, typename T>77 bool Write(const K& key, const T& value, bool fOverwrite=true)78 {79 if (!pdb)80 return false;81 if (fReadOnly)82 assert(("Write called on database in read-only mode", false));8384 // Key85 CDataStream ssKey(SER_DISK);86 ssKey.reserve(1000);87 ssKey << key;88 Dbt datKey(&ssKey[0], ssKey.size());8990 // Value91 CDataStream ssValue(SER_DISK);92 ssValue.reserve(10000);93 ssValue << value;94 Dbt datValue(&ssValue[0], ssValue.size());9596 // Write97 int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));9899 // Clear memory in case it was a private key100 memset(datKey.get_data(), 0, datKey.get_size());101 memset(datValue.get_data(), 0, datValue.get_size());102 return (ret == 0);103 }104105 template<typename K>106 bool Erase(const K& key)107 {108 if (!pdb)109 return false;110 if (fReadOnly)111 assert(("Erase called on database in read-only mode", false));112113 // Key114 CDataStream ssKey(SER_DISK);115 ssKey.reserve(1000);116 ssKey << key;117 Dbt datKey(&ssKey[0], ssKey.size());118119 // Erase120 int ret = pdb->del(GetTxn(), &datKey, 0);121122 // Clear memory123 memset(datKey.get_data(), 0, datKey.get_size());124 return (ret == 0 || ret == DB_NOTFOUND);125 }126127 template<typename K>128 bool Exists(const K& key)129 {130 if (!pdb)131 return false;132133 // Key134 CDataStream ssKey(SER_DISK);135 ssKey.reserve(1000);136 ssKey << key;137 Dbt datKey(&ssKey[0], ssKey.size());138139 // Exists140 int ret = pdb->exists(GetTxn(), &datKey, 0);141142 // Clear memory143 memset(datKey.get_data(), 0, datKey.get_size());144 return (ret == 0);145 }146147 Dbc* GetCursor()148 {149 if (!pdb)150 return NULL;151 Dbc* pcursor = NULL;152 int ret = pdb->cursor(NULL, &pcursor, 0);153 if (ret != 0)154 return NULL;155 return pcursor;156 }157158 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)159 {160 // Read at cursor161 Dbt datKey;162 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)163 {164 datKey.set_data(&ssKey[0]);165 datKey.set_size(ssKey.size());166 }167 Dbt datValue;168 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)169 {170 datValue.set_data(&ssValue[0]);171 datValue.set_size(ssValue.size());172 }173 datKey.set_flags(DB_DBT_MALLOC);174 datValue.set_flags(DB_DBT_MALLOC);175 int ret = pcursor->get(&datKey, &datValue, fFlags);176 if (ret != 0)177 return ret;178 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)179 return 99999;180181 // Convert to streams182 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());188189 // Clear and free memory190 memset(datKey.get_data(), 0, datKey.get_size());191 memset(datValue.get_data(), 0, datValue.get_size());192 free(datKey.get_data());193 free(datValue.get_data());194 return 0;195 }196197 DbTxn* GetTxn()198 {199 if (!vTxn.empty())200 return vTxn.back();201 else202 return NULL;203 }204205public:206 bool TxnBegin()207 {208 if (!pdb)209 return false;210 DbTxn* ptxn = NULL;211 int ret = dbenv.txn_begin(GetTxn(), &ptxn, 0);212 if (!ptxn || ret != 0)213 return false;214 vTxn.push_back(ptxn);215 return true;216 }217218 bool TxnCommit()219 {220 if (!pdb)221 return false;222 if (vTxn.empty())223 return false;224 int ret = vTxn.back()->commit(0);225 vTxn.pop_back();226 return (ret == 0);227 }228229 bool TxnAbort()230 {231 if (!pdb)232 return false;233 if (vTxn.empty())234 return false;235 int ret = vTxn.back()->abort();236 vTxn.pop_back();237 return (ret == 0);238 }239240 bool ReadVersion(int& nVersion)241 {242 nVersion = 0;243 return Read(string("version"), nVersion);244 }245246 bool WriteVersion(int nVersion)247 {248 return Write(string("version"), nVersion);249 }250};251252253254255256257258259class CTxDB : public CDB260{261public:262 CTxDB(const char* pszMode="r+") : CDB(!fClient ? "blkindex.dat" : NULL, pszMode) { }263private:264 CTxDB(const CTxDB&);265 void operator=(const CTxDB&);266public:267 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);268 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);269 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);270 bool EraseTxIndex(const CTransaction& tx);271 bool ContainsTx(uint256 hash);272 bool ReadOwnerTxes(uint160 hash160, int nHeight, vector<CTransaction>& vtx);273 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);274 bool ReadDiskTx(uint256 hash, CTransaction& tx);275 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);276 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);277 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);278 bool EraseBlockIndex(uint256 hash);279 bool ReadHashBestChain(uint256& hashBestChain);280 bool WriteHashBestChain(uint256 hashBestChain);281 bool LoadBlockIndex();282};283284285286287288class CReviewDB : public CDB289{290public:291 CReviewDB(const char* pszMode="r+") : CDB("reviews.dat", pszMode) { }292private:293 CReviewDB(const CReviewDB&);294 void operator=(const CReviewDB&);295public:296 bool ReadUser(uint256 hash, CUser& user)297 {298 return Read(make_pair(string("user"), hash), user);299 }300301 bool WriteUser(uint256 hash, const CUser& user)302 {303 return Write(make_pair(string("user"), hash), user);304 }305306 bool ReadReviews(uint256 hash, vector<CReview>& vReviews);307 bool WriteReviews(uint256 hash, const vector<CReview>& vReviews);308};309310311312313314class CMarketDB : public CDB315{316public:317 CMarketDB(const char* pszMode="r+") : CDB("market.dat", pszMode) { }318private:319 CMarketDB(const CMarketDB&);320 void operator=(const CMarketDB&);321};322323324325326327class CAddrDB : public CDB328{329public:330 CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }331private:332 CAddrDB(const CAddrDB&);333 void operator=(const CAddrDB&);334public:335 bool WriteAddress(const CAddress& addr);336 bool LoadAddresses();337};338339bool LoadAddresses();340341342343344345346class CWalletDB : public CDB347{348public:349 CWalletDB(const char* pszMode="r+") : CDB("wallet.dat", pszMode) { }350private:351 CWalletDB(const CWalletDB&);352 void operator=(const CWalletDB&);353public:354 bool ReadName(const string& strAddress, string& strName)355 {356 strName = "";357 return Read(make_pair(string("name"), strAddress), strName);358 }359360 bool WriteName(const string& strAddress, const string& strName)361 {362 nWalletDBUpdated++;363 mapAddressBook[strAddress] = strName;364 return Write(make_pair(string("name"), strAddress), strName);365 }366367 bool EraseName(const string& strAddress)368 {369 nWalletDBUpdated++;370 mapAddressBook.erase(strAddress);371 return Erase(make_pair(string("name"), strAddress));372 }373374 bool ReadTx(uint256 hash, CWalletTx& wtx)375 {376 return Read(make_pair(string("tx"), hash), wtx);377 }378379 bool WriteTx(uint256 hash, const CWalletTx& wtx)380 {381 nWalletDBUpdated++;382 return Write(make_pair(string("tx"), hash), wtx);383 }384385 bool EraseTx(uint256 hash)386 {387 nWalletDBUpdated++;388 return Erase(make_pair(string("tx"), hash));389 }390391 bool ReadKey(const vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)392 {393 vchPrivKey.clear();394 return Read(make_pair(string("key"), vchPubKey), vchPrivKey);395 }396397 bool WriteKey(const vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)398 {399 nWalletDBUpdated++;400 return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false);401 }402403 bool ReadDefaultKey(vector<unsigned char>& vchPubKey)404 {405 vchPubKey.clear();406 return Read(string("defaultkey"), vchPubKey);407 }408409 bool WriteDefaultKey(const vector<unsigned char>& vchPubKey)410 {411 nWalletDBUpdated++;412 return Write(string("defaultkey"), vchPubKey);413 }414415 template<typename T>416 bool ReadSetting(const string& strKey, T& value)417 {418 return Read(make_pair(string("setting"), strKey), value);419 }420421 template<typename T>422 bool WriteSetting(const string& strKey, const T& value)423 {424 nWalletDBUpdated++;425 return Write(make_pair(string("setting"), strKey), value);426 }427428 bool LoadWallet(vector<unsigned char>& vchDefaultKeyRet);429};430431bool LoadWallet(bool& fFirstRunRet);432433inline bool SetAddressBookName(const string& strAddress, const string& strName)434{435 return CWalletDB().WriteName(strAddress, strName);436}
Click a line number to link to it; use #L10-L20 for a range. "=" next to a version means the file is identical there.
