diff --git a/src/limitedmap.h b/src/limitedmap.h index c719c88f99..c330611865 100644 --- a/src/limitedmap.h +++ b/src/limitedmap.h @@ -54,6 +54,14 @@ public: if (ret.second) prune(); } + void insert_or_update(const value_type& x) + { + std::pair ret = map.insert(x); + if (ret.second) + prune(); + else + ret.first->second = x.second; + } void erase(const key_type& k) { map.erase(k); diff --git a/src/txdb.cpp b/src/txdb.cpp index ff508de52b..b80210bd2c 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -151,7 +151,7 @@ size_t CCoinsViewDB::EstimateSize() const return db.EstimateSize(DB_COIN, (char)(DB_COIN+1)); } -CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) { +CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe), mapHasTxIndexCache(10000, 20000) { } bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) { @@ -241,18 +241,36 @@ bool CBlockTreeDB::WriteBatchSync(const std::vectorsecond; + } + } + bool r = Exists(std::make_pair(DB_TXINDEX, txid)); + LOCK(cs); + mapHasTxIndexCache.insert(std::make_pair(txid, r)); + return r; } bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) { - return Read(std::make_pair(DB_TXINDEX, txid), pos); + bool r = Read(std::make_pair(DB_TXINDEX, txid), pos); + LOCK(cs); + mapHasTxIndexCache.insert_or_update(std::make_pair(txid, r)); + return r; } bool CBlockTreeDB::WriteTxIndex(const std::vector >&vect) { CDBBatch batch(*this); for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) batch.Write(std::make_pair(DB_TXINDEX, it->first), it->second); - return WriteBatch(batch); + bool ret = WriteBatch(batch); + LOCK(cs); + for (auto& p : vect) { + mapHasTxIndexCache.insert_or_update(std::make_pair(p.first, true)); + } + return ret; } bool CBlockTreeDB::ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) { diff --git a/src/txdb.h b/src/txdb.h index 372ee805d6..6861e5dc74 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -9,7 +9,9 @@ #include #include #include +#include #include +#include #include #include @@ -110,6 +112,10 @@ private: /** Access to the block database (blocks/index/) */ class CBlockTreeDB : public CDBWrapper { +private: + CCriticalSection cs; + unordered_limitedmap mapHasTxIndexCache; + public: explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);