typedef std::map<uint256, CCoins> to CCoinsMap
This makes it possible to switch to a more efficient map type without changing all occurences manually. Merges half of #4413.
This commit is contained in:
parent
8d9cc7d743
commit
dd638dd712
4 changed files with 17 additions and 16 deletions
|
@ -55,7 +55,7 @@ bool CCoinsView::SetCoins(const uint256 &txid, const CCoins &coins) { return fal
|
||||||
bool CCoinsView::HaveCoins(const uint256 &txid) { return false; }
|
bool CCoinsView::HaveCoins(const uint256 &txid) { return false; }
|
||||||
uint256 CCoinsView::GetBestBlock() { return uint256(0); }
|
uint256 CCoinsView::GetBestBlock() { return uint256(0); }
|
||||||
bool CCoinsView::SetBestBlock(const uint256 &hashBlock) { return false; }
|
bool CCoinsView::SetBestBlock(const uint256 &hashBlock) { return false; }
|
||||||
bool CCoinsView::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock) { return false; }
|
bool CCoinsView::BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
|
||||||
bool CCoinsView::GetStats(CCoinsStats &stats) { return false; }
|
bool CCoinsView::GetStats(CCoinsStats &stats) { return false; }
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ bool CCoinsViewBacked::HaveCoins(const uint256 &txid) { return base->HaveCoins(t
|
||||||
uint256 CCoinsViewBacked::GetBestBlock() { return base->GetBestBlock(); }
|
uint256 CCoinsViewBacked::GetBestBlock() { return base->GetBestBlock(); }
|
||||||
bool CCoinsViewBacked::SetBestBlock(const uint256 &hashBlock) { return base->SetBestBlock(hashBlock); }
|
bool CCoinsViewBacked::SetBestBlock(const uint256 &hashBlock) { return base->SetBestBlock(hashBlock); }
|
||||||
void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
|
void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
|
||||||
bool CCoinsViewBacked::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
|
bool CCoinsViewBacked::BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
|
||||||
bool CCoinsViewBacked::GetStats(CCoinsStats &stats) { return base->GetStats(stats); }
|
bool CCoinsViewBacked::GetStats(CCoinsStats &stats) { return base->GetStats(stats); }
|
||||||
|
|
||||||
CCoinsViewCache::CCoinsViewCache(CCoinsView &baseIn, bool fDummy) : CCoinsViewBacked(baseIn), hashBlock(0) { }
|
CCoinsViewCache::CCoinsViewCache(CCoinsView &baseIn, bool fDummy) : CCoinsViewBacked(baseIn), hashBlock(0) { }
|
||||||
|
@ -83,20 +83,20 @@ bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<uint256,CCoins>::iterator CCoinsViewCache::FetchCoins(const uint256 &txid) {
|
CCoinsMap::iterator CCoinsViewCache::FetchCoins(const uint256 &txid) {
|
||||||
std::map<uint256,CCoins>::iterator it = cacheCoins.lower_bound(txid);
|
CCoinsMap::iterator it = cacheCoins.lower_bound(txid);
|
||||||
if (it != cacheCoins.end() && it->first == txid)
|
if (it != cacheCoins.end() && it->first == txid)
|
||||||
return it;
|
return it;
|
||||||
CCoins tmp;
|
CCoins tmp;
|
||||||
if (!base->GetCoins(txid,tmp))
|
if (!base->GetCoins(txid,tmp))
|
||||||
return cacheCoins.end();
|
return cacheCoins.end();
|
||||||
std::map<uint256,CCoins>::iterator ret = cacheCoins.insert(it, std::make_pair(txid, CCoins()));
|
CCoinsMap::iterator ret = cacheCoins.insert(it, std::make_pair(txid, CCoins()));
|
||||||
tmp.swap(ret->second);
|
tmp.swap(ret->second);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
CCoins &CCoinsViewCache::GetCoins(const uint256 &txid) {
|
CCoins &CCoinsViewCache::GetCoins(const uint256 &txid) {
|
||||||
std::map<uint256,CCoins>::iterator it = FetchCoins(txid);
|
CCoinsMap::iterator it = FetchCoins(txid);
|
||||||
assert(it != cacheCoins.end());
|
assert(it != cacheCoins.end());
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
@ -121,8 +121,8 @@ bool CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCoinsViewCache::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlockIn) {
|
bool CCoinsViewCache::BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
|
||||||
for (std::map<uint256, CCoins>::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
|
for (CCoinsMap::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
|
||||||
cacheCoins[it->first] = it->second;
|
cacheCoins[it->first] = it->second;
|
||||||
hashBlock = hashBlockIn;
|
hashBlock = hashBlockIn;
|
||||||
return true;
|
return true;
|
||||||
|
|
11
src/coins.h
11
src/coins.h
|
@ -239,6 +239,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef std::map<uint256,CCoins> CCoinsMap;
|
||||||
|
|
||||||
struct CCoinsStats
|
struct CCoinsStats
|
||||||
{
|
{
|
||||||
|
@ -275,7 +276,7 @@ public:
|
||||||
virtual bool SetBestBlock(const uint256 &hashBlock);
|
virtual bool SetBestBlock(const uint256 &hashBlock);
|
||||||
|
|
||||||
// Do a bulk modification (multiple SetCoins + one SetBestBlock)
|
// Do a bulk modification (multiple SetCoins + one SetBestBlock)
|
||||||
virtual bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
|
virtual bool BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock);
|
||||||
|
|
||||||
// Calculate statistics about the unspent transaction output set
|
// Calculate statistics about the unspent transaction output set
|
||||||
virtual bool GetStats(CCoinsStats &stats);
|
virtual bool GetStats(CCoinsStats &stats);
|
||||||
|
@ -299,7 +300,7 @@ public:
|
||||||
uint256 GetBestBlock();
|
uint256 GetBestBlock();
|
||||||
bool SetBestBlock(const uint256 &hashBlock);
|
bool SetBestBlock(const uint256 &hashBlock);
|
||||||
void SetBackend(CCoinsView &viewIn);
|
void SetBackend(CCoinsView &viewIn);
|
||||||
bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
|
bool BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock);
|
||||||
bool GetStats(CCoinsStats &stats);
|
bool GetStats(CCoinsStats &stats);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -309,7 +310,7 @@ class CCoinsViewCache : public CCoinsViewBacked
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
uint256 hashBlock;
|
uint256 hashBlock;
|
||||||
std::map<uint256,CCoins> cacheCoins;
|
CCoinsMap cacheCoins;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CCoinsViewCache(CCoinsView &baseIn, bool fDummy = false);
|
CCoinsViewCache(CCoinsView &baseIn, bool fDummy = false);
|
||||||
|
@ -320,7 +321,7 @@ public:
|
||||||
bool HaveCoins(const uint256 &txid);
|
bool HaveCoins(const uint256 &txid);
|
||||||
uint256 GetBestBlock();
|
uint256 GetBestBlock();
|
||||||
bool SetBestBlock(const uint256 &hashBlock);
|
bool SetBestBlock(const uint256 &hashBlock);
|
||||||
bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
|
bool BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock);
|
||||||
|
|
||||||
// Return a modifiable reference to a CCoins. Check HaveCoins first.
|
// Return a modifiable reference to a CCoins. Check HaveCoins first.
|
||||||
// Many methods explicitly require a CCoinsViewCache because of this method, to reduce
|
// Many methods explicitly require a CCoinsViewCache because of this method, to reduce
|
||||||
|
@ -352,7 +353,7 @@ public:
|
||||||
const CTxOut &GetOutputFor(const CTxIn& input);
|
const CTxOut &GetOutputFor(const CTxIn& input);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<uint256,CCoins>::iterator FetchCoins(const uint256 &txid);
|
CCoinsMap::iterator FetchCoins(const uint256 &txid);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -54,11 +54,11 @@ bool CCoinsViewDB::SetBestBlock(const uint256 &hashBlock) {
|
||||||
return db.WriteBatch(batch);
|
return db.WriteBatch(batch);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCoinsViewDB::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock) {
|
bool CCoinsViewDB::BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock) {
|
||||||
LogPrint("coindb", "Committing %u changed transactions to coin database...\n", (unsigned int)mapCoins.size());
|
LogPrint("coindb", "Committing %u changed transactions to coin database...\n", (unsigned int)mapCoins.size());
|
||||||
|
|
||||||
CLevelDBBatch batch;
|
CLevelDBBatch batch;
|
||||||
for (std::map<uint256, CCoins>::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
|
for (CCoinsMap::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
|
||||||
BatchWriteCoins(batch, it->first, it->second);
|
BatchWriteCoins(batch, it->first, it->second);
|
||||||
if (hashBlock != uint256(0))
|
if (hashBlock != uint256(0))
|
||||||
BatchWriteHashBestChain(batch, hashBlock);
|
BatchWriteHashBestChain(batch, hashBlock);
|
||||||
|
|
|
@ -37,7 +37,7 @@ public:
|
||||||
bool HaveCoins(const uint256 &txid);
|
bool HaveCoins(const uint256 &txid);
|
||||||
uint256 GetBestBlock();
|
uint256 GetBestBlock();
|
||||||
bool SetBestBlock(const uint256 &hashBlock);
|
bool SetBestBlock(const uint256 &hashBlock);
|
||||||
bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
|
bool BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock);
|
||||||
bool GetStats(CCoinsStats &stats);
|
bool GetStats(CCoinsStats &stats);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue