mirror of
https://github.com/dogecoin/dogecoin.git
synced 2026-02-14 17:28:45 +00:00
Encapsulate CLevelDB iterators cleanly
This commit is contained in:
parent
b8893fe7db
commit
cc7a3e39c6
@ -43,6 +43,13 @@ static leveldb::Options GetOptions(size_t nCacheSize)
|
||||
return options;
|
||||
}
|
||||
|
||||
CLevelDBIterator::~CLevelDBIterator() { delete piter; }
|
||||
bool CLevelDBIterator::Valid() { return piter->Valid(); }
|
||||
void CLevelDBIterator::SeekToFirst() { piter->SeekToFirst(); }
|
||||
void CLevelDBIterator::SeekToLast() { piter->SeekToLast(); }
|
||||
void CLevelDBIterator::Next() { piter->Next(); }
|
||||
void CLevelDBIterator::Prev() { piter->Prev(); }
|
||||
|
||||
CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe)
|
||||
{
|
||||
penv = NULL;
|
||||
|
||||
@ -61,6 +61,63 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class CLevelDBIterator
|
||||
{
|
||||
private:
|
||||
leveldb::Iterator *piter;
|
||||
|
||||
public:
|
||||
CLevelDBIterator(leveldb::Iterator *piterIn) : piter(piterIn) {}
|
||||
~CLevelDBIterator();
|
||||
|
||||
bool Valid();
|
||||
|
||||
void SeekToFirst();
|
||||
void SeekToLast();
|
||||
|
||||
template<typename K> void Seek(const K& key) {
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(ssKey.GetSerializeSize(key));
|
||||
ssKey << key;
|
||||
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
||||
piter->Seek(slKey);
|
||||
}
|
||||
|
||||
void Next();
|
||||
void Prev();
|
||||
|
||||
template<typename K> bool GetKey(K& key) {
|
||||
leveldb::Slice slKey = piter->key();
|
||||
try {
|
||||
CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
|
||||
ssKey >> key;
|
||||
} catch(std::exception &e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned int GetKeySize() {
|
||||
return piter->key().size();
|
||||
}
|
||||
|
||||
template<typename V> bool GetValue(V& value) {
|
||||
leveldb::Slice slValue = piter->value();
|
||||
try {
|
||||
CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
|
||||
ssValue >> value;
|
||||
} catch(std::exception &e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned int GetValueSize() {
|
||||
return piter->value().size();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class CLevelDBWrapper
|
||||
{
|
||||
private:
|
||||
@ -163,10 +220,8 @@ public:
|
||||
return WriteBatch(batch, true);
|
||||
}
|
||||
|
||||
// not exactly clean encapsulation, but it's easiest for now
|
||||
leveldb::Iterator* NewIterator()
|
||||
{
|
||||
return pdb->NewIterator(iteroptions);
|
||||
CLevelDBIterator *NewIterator() {
|
||||
return new CLevelDBIterator(pdb->NewIterator(iteroptions));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
61
src/txdb.cpp
61
src/txdb.cpp
@ -106,8 +106,8 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
|
||||
/* It seems that there are no "const iterators" for LevelDB. Since we
|
||||
only need read operations on it, use a const-cast to get around
|
||||
that restriction. */
|
||||
boost::scoped_ptr<leveldb::Iterator> pcursor(const_cast<CLevelDBWrapper*>(&db)->NewIterator());
|
||||
pcursor->SeekToFirst();
|
||||
boost::scoped_ptr<CLevelDBIterator> pcursor(const_cast<CLevelDBWrapper*>(&db)->NewIterator());
|
||||
pcursor->Seek('c');
|
||||
|
||||
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
|
||||
stats.hashBlock = GetBestBlock();
|
||||
@ -115,22 +115,10 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
|
||||
arith_uint256 nTotalAmount = 0;
|
||||
while (pcursor->Valid()) {
|
||||
boost::this_thread::interruption_point();
|
||||
try {
|
||||
leveldb::Slice slKey = pcursor->key();
|
||||
CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
|
||||
char chType;
|
||||
ssKey >> chType;
|
||||
if (chType == DB_COINS) {
|
||||
leveldb::Slice slValue = pcursor->value();
|
||||
CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
|
||||
CCoins coins;
|
||||
ssValue >> coins;
|
||||
uint256 txhash;
|
||||
ssKey >> txhash;
|
||||
ss << txhash;
|
||||
ss << VARINT(coins.nVersion);
|
||||
ss << (coins.fCoinBase ? 'c' : 'n');
|
||||
ss << VARINT(coins.nHeight);
|
||||
std::pair<char, uint256> key;
|
||||
CCoins coins;
|
||||
if (pcursor->GetKey(key) && key.first == DB_COINS) {
|
||||
if (pcursor->GetValue(coins)) {
|
||||
stats.nTransactions++;
|
||||
for (unsigned int i=0; i<coins.vout.size(); i++) {
|
||||
const CTxOut &out = coins.vout[i];
|
||||
@ -141,13 +129,15 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
|
||||
nTotalAmount += out.nValue;
|
||||
}
|
||||
}
|
||||
stats.nSerializedSize += 32 + slValue.size();
|
||||
stats.nSerializedSize += 32 + pcursor->GetKeySize();
|
||||
ss << VARINT(0);
|
||||
} else {
|
||||
return error("CCoinsViewDB::GetStats() : unable to read value");
|
||||
}
|
||||
pcursor->Next();
|
||||
} catch (const std::exception& e) {
|
||||
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
pcursor->Next();
|
||||
}
|
||||
stats.nHeight = mapBlockIndex.find(GetBestBlock())->second->nHeight;
|
||||
stats.hashSerialized = ss.GetHash();
|
||||
@ -192,26 +182,17 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
|
||||
|
||||
bool CBlockTreeDB::LoadBlockIndexGuts()
|
||||
{
|
||||
boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
|
||||
boost::scoped_ptr<CLevelDBIterator> pcursor(NewIterator());
|
||||
|
||||
CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
|
||||
ssKeySet << make_pair(DB_BLOCK_INDEX, uint256());
|
||||
pcursor->Seek(ssKeySet.str());
|
||||
pcursor->Seek(make_pair(DB_BLOCK_INDEX, uint256()));
|
||||
|
||||
// Load mapBlockIndex
|
||||
while (pcursor->Valid()) {
|
||||
boost::this_thread::interruption_point();
|
||||
try {
|
||||
leveldb::Slice slKey = pcursor->key();
|
||||
CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
|
||||
char chType;
|
||||
ssKey >> chType;
|
||||
if (chType == DB_BLOCK_INDEX) {
|
||||
leveldb::Slice slValue = pcursor->value();
|
||||
CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
|
||||
CDiskBlockIndex diskindex;
|
||||
ssValue >> diskindex;
|
||||
|
||||
std::pair<char, uint256> key;
|
||||
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
|
||||
CDiskBlockIndex diskindex;
|
||||
if (pcursor->GetValue(diskindex)) {
|
||||
// Construct block index object
|
||||
CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
|
||||
pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
|
||||
@ -240,10 +221,10 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
|
||||
|
||||
pcursor->Next();
|
||||
} else {
|
||||
break; // if shutdown requested or finished loading block index
|
||||
return error("LoadBlockIndex() : failed to read value");
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user