1 // Copyright (c) 2009-2012 The Bitcoin Developers.
2 // Authored by Google, Inc.
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_LEVELDB_H
7 #define BITCOIN_LEVELDB_H
15 #include <leveldb/db.h>
16 #include <leveldb/write_batch.h>
19 class CDiskBlockIndex;
26 // Class that provides access to a LevelDB. Note that this class is frequently
27 // instantiated on the stack and then destroyed again, so instantiation has to
28 // be very cheap. Unfortunately that means, a CTxDB instance is actually just a
29 // wrapper around some global state.
31 // A LevelDB is a key/value store that is optimized for fast usage on hard
32 // disks. It prefers long read/writes to seeks and is based on a series of
33 // sorted key/value mapping files that are stacked on top of each other, with
34 // newer files overriding older files. A background thread compacts them
35 // together when too many files stack up.
37 // Learn more: http://code.google.com/p/leveldb/
41 CTxDB(const char* pszMode="r+");
43 // Note that this is not the same as Close() because it deletes only
44 // data scoped to this TxDB object.
48 // Destroys the underlying shared global state accessed by this TxDB.
52 leveldb::DB *pdb; // Points to the global instance.
54 // A batch stores up writes and deletes for atomic application. When this
55 // field is non-NULL, writes/deletes go there instead of directly to disk.
56 leveldb::WriteBatch *activeBatch;
57 leveldb::Options options;
62 // Returns true and sets (value,false) if activeBatch contains the given key
63 // or leaves value alone and sets deleted = true if activeBatch contains a
65 bool ScanBatch(const CDataStream &key, std::string *value, bool *deleted) const;
67 template<typename K, typename T>
68 bool Read(const K& key, T& value)
70 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
75 bool readFromDb = true;
77 // First we must search for it in the currently pending set of
78 // changes to the db. If not found in the batch, go on to read disk.
80 readFromDb = ScanBatch(ssKey, &strValue, &deleted) == false;
86 leveldb::Status status = pdb->Get(leveldb::ReadOptions(),
87 ssKey.str(), &strValue);
89 if (status.IsNotFound())
91 // Some unexpected error.
92 printf("LevelDB read failure: %s\n", status.ToString().c_str());
98 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
99 SER_DISK, CLIENT_VERSION);
102 catch (const std::exception&) {
108 template<typename K, typename T>
109 bool Write(const K& key, const T& value)
112 assert(!"Write called on database in read-only mode");
114 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
117 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
118 ssValue.reserve(10000);
122 activeBatch->Put(ssKey.str(), ssValue.str());
125 leveldb::Status status = pdb->Put(leveldb::WriteOptions(), ssKey.str(), ssValue.str());
127 printf("LevelDB write failure: %s\n", status.ToString().c_str());
134 bool Erase(const K& key)
139 assert(!"Erase called on database in read-only mode");
141 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
145 activeBatch->Delete(ssKey.str());
148 leveldb::Status status = pdb->Delete(leveldb::WriteOptions(), ssKey.str());
149 return (status.ok() || status.IsNotFound());
153 bool Exists(const K& key)
155 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
162 if (ScanBatch(ssKey, &unused, &deleted) && !deleted) {
168 leveldb::Status status = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &unused);
169 return status.IsNotFound() == false;
183 bool ReadVersion(int& nVersion)
186 return Read(std::string("version"), nVersion);
189 bool WriteVersion(int nVersion)
191 return Write(std::string("version"), nVersion);
194 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
195 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
196 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
197 bool EraseTxIndex(const CTransaction& tx);
198 bool ContainsTx(uint256 hash);
199 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
200 bool ReadDiskTx(uint256 hash, CTransaction& tx);
201 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
202 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
203 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
204 bool ReadHashBestChain(uint256& hashBestChain);
205 bool WriteHashBestChain(uint256 hashBestChain);
206 bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
207 bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
208 bool ReadSyncCheckpoint(uint256& hashCheckpoint);
209 bool WriteSyncCheckpoint(uint256 hashCheckpoint);
210 bool ReadCheckpointPubKey(std::string& strPubKey);
211 bool WriteCheckpointPubKey(const std::string& strPubKey);
212 bool ReadModifierUpgradeTime(unsigned int& nUpgradeTime);
213 bool WriteModifierUpgradeTime(const unsigned int& nUpgradeTime);
214 bool LoadBlockIndex();
218 #endif // BITCOIN_DB_H