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
16 #include <leveldb/db.h>
17 #include <leveldb/write_batch.h>
20 class CDiskBlockIndex;
27 // Class that provides access to a LevelDB. Note that this class is frequently
28 // instantiated on the stack and then destroyed again, so instantiation has to
29 // be very cheap. Unfortunately that means, a CTxDB instance is actually just a
30 // wrapper around some global state.
32 // A LevelDB is a key/value store that is optimized for fast usage on hard
33 // disks. It prefers long read/writes to seeks and is based on a series of
34 // sorted key/value mapping files that are stacked on top of each other, with
35 // newer files overriding older files. A background thread compacts them
36 // together when too many files stack up.
38 // Learn more: http://code.google.com/p/leveldb/
42 CTxDB(const char* pszMode="r+");
44 // Note that this is not the same as Close() because it deletes only
45 // data scoped to this TxDB object.
49 // Destroys the underlying shared global state accessed by this TxDB.
53 leveldb::DB *pdb; // Points to the global instance.
55 // A batch stores up writes and deletes for atomic application. When this
56 // field is non-NULL, writes/deletes go there instead of directly to disk.
57 leveldb::WriteBatch *activeBatch;
58 leveldb::Options options;
63 // Returns true and sets (value,false) if activeBatch contains the given key
64 // or leaves value alone and sets deleted = true if activeBatch contains a
66 bool ScanBatch(const CDataStream &key, std::string *value, bool *deleted) const;
68 template<typename K, typename T>
69 bool Read(const K& key, T& value)
71 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
76 bool readFromDb = true;
78 // First we must search for it in the currently pending set of
79 // changes to the db. If not found in the batch, go on to read disk.
81 readFromDb = ScanBatch(ssKey, &strValue, &deleted) == false;
87 leveldb::Status status = pdb->Get(leveldb::ReadOptions(),
88 ssKey.str(), &strValue);
90 if (status.IsNotFound())
92 // Some unexpected error.
93 printf("LevelDB read failure: %s\n", status.ToString().c_str());
99 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
100 SER_DISK, CLIENT_VERSION);
103 catch (const std::exception&) {
109 template<typename K, typename T>
110 bool Write(const K& key, const T& value)
113 assert(!"Write called on database in read-only mode");
115 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
118 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
119 ssValue.reserve(10000);
123 activeBatch->Put(ssKey.str(), ssValue.str());
126 leveldb::Status status = pdb->Put(leveldb::WriteOptions(), ssKey.str(), ssValue.str());
128 printf("LevelDB write failure: %s\n", status.ToString().c_str());
135 bool Erase(const K& key)
140 assert(!"Erase called on database in read-only mode");
142 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
146 activeBatch->Delete(ssKey.str());
149 leveldb::Status status = pdb->Delete(leveldb::WriteOptions(), ssKey.str());
150 return (status.ok() || status.IsNotFound());
154 bool Exists(const K& key)
156 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
163 if (ScanBatch(ssKey, &unused, &deleted) && !deleted) {
169 leveldb::Status status = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &unused);
170 return status.IsNotFound() == false;
184 bool ReadVersion(int& nVersion)
187 return Read(std::string("version"), nVersion);
190 bool WriteVersion(int nVersion)
192 return Write(std::string("version"), nVersion);
195 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
196 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
197 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
198 bool EraseTxIndex(const CTransaction& tx);
199 bool ContainsTx(uint256 hash);
200 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
201 bool ReadDiskTx(uint256 hash, CTransaction& tx);
202 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
203 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
204 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
205 bool ReadHashBestChain(uint256& hashBestChain);
206 bool WriteHashBestChain(uint256 hashBestChain);
207 bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
208 bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
209 bool ReadSyncCheckpoint(uint256& hashCheckpoint);
210 bool WriteSyncCheckpoint(uint256 hashCheckpoint);
211 bool ReadCheckpointPubKey(std::string& strPubKey);
212 bool WriteCheckpointPubKey(const std::string& strPubKey);
213 bool ReadModifierUpgradeTime(unsigned int& nUpgradeTime);
214 bool WriteModifierUpgradeTime(const unsigned int& nUpgradeTime);
215 bool LoadBlockIndex();
219 #endif // BITCOIN_DB_H