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
13 #include <leveldb/db.h>
14 #include <leveldb/write_batch.h>
17 class CDiskBlockIndex;
24 // Class that provides access to a LevelDB. Note that this class is frequently
25 // instantiated on the stack and then destroyed again, so instantiation has to
26 // be very cheap. Unfortunately that means, a CTxDB instance is actually just a
27 // wrapper around some global state.
29 // A LevelDB is a key/value store that is optimized for fast usage on hard
30 // disks. It prefers long read/writes to seeks and is based on a series of
31 // sorted key/value mapping files that are stacked on top of each other, with
32 // newer files overriding older files. A background thread compacts them
33 // together when too many files stack up.
35 // Learn more: http://code.google.com/p/leveldb/
39 CTxDB(const char* pszMode="r+");
41 // Note that this is not the same as Close() because it deletes only
42 // data scoped to this TxDB object.
46 // Destroys the underlying shared global state accessed by this TxDB.
50 leveldb::DB *pdb; // Points to the global instance.
52 // A batch stores up writes and deletes for atomic application. When this
53 // field is non-NULL, writes/deletes go there instead of directly to disk.
54 leveldb::WriteBatch *activeBatch;
55 leveldb::Options options;
60 // Returns true and sets (value,false) if activeBatch contains the given key
61 // or leaves value alone and sets deleted = true if activeBatch contains a
63 bool ScanBatch(const CDataStream &key, std::string *value, bool *deleted) const;
65 template<typename K, typename T>
66 bool Read(const K& key, T& value)
68 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
73 bool readFromDb = true;
75 // First we must search for it in the currently pending set of
76 // changes to the db. If not found in the batch, go on to read disk.
78 readFromDb = ScanBatch(ssKey, &strValue, &deleted) == false;
84 leveldb::Status status = pdb->Get(leveldb::ReadOptions(),
85 ssKey.str(), &strValue);
87 if (status.IsNotFound())
89 // Some unexpected error.
90 printf("LevelDB read failure: %s\n", status.ToString().c_str());
96 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
97 SER_DISK, CLIENT_VERSION);
100 catch (const std::exception&) {
106 template<typename K, typename T>
107 bool Write(const K& key, const T& value)
110 assert(!"Write called on database in read-only mode");
112 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
115 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
116 ssValue.reserve(10000);
120 activeBatch->Put(ssKey.str(), ssValue.str());
123 leveldb::Status status = pdb->Put(leveldb::WriteOptions(), ssKey.str(), ssValue.str());
125 printf("LevelDB write failure: %s\n", status.ToString().c_str());
132 bool Erase(const K& key)
137 assert(!"Erase called on database in read-only mode");
139 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
143 activeBatch->Delete(ssKey.str());
146 leveldb::Status status = pdb->Delete(leveldb::WriteOptions(), ssKey.str());
147 return (status.ok() || status.IsNotFound());
151 bool Exists(const K& key)
153 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
160 if (ScanBatch(ssKey, &unused, &deleted) && !deleted) {
166 leveldb::Status status = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &unused);
167 return status.IsNotFound() == false;
181 bool ReadVersion(int& nVersion)
184 return Read(std::string("version"), nVersion);
187 bool WriteVersion(int nVersion)
189 return Write(std::string("version"), nVersion);
192 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
193 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
194 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
195 bool EraseTxIndex(const CTransaction& tx);
196 bool ContainsTx(uint256 hash);
197 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
198 bool ReadDiskTx(uint256 hash, CTransaction& tx);
199 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
200 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
201 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
202 bool ReadHashBestChain(uint256& hashBestChain);
203 bool WriteHashBestChain(uint256 hashBestChain);
204 bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
205 bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
206 bool ReadSyncCheckpoint(uint256& hashCheckpoint);
207 bool WriteSyncCheckpoint(uint256 hashCheckpoint);
208 bool ReadCheckpointPubKey(std::string& strPubKey);
209 bool WriteCheckpointPubKey(const std::string& strPubKey);
210 bool ReadModifierUpgradeTime(unsigned int& nUpgradeTime);
211 bool WriteModifierUpgradeTime(const unsigned int& nUpgradeTime);
212 bool LoadBlockIndex();
216 #endif // BITCOIN_DB_H