From 0b8aaf691fd780d94d79296e912df6e83975c504 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 16 Dec 2011 22:14:09 -0800 Subject: [PATCH] Get this exactly right. --- LedgerHistory.cpp | 29 ++++++++++++++++++++--------- PubKeyCache.cpp | 26 +++++++------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/LedgerHistory.cpp b/LedgerHistory.cpp index 0e86e05b1..80ec1b1e5 100644 --- a/LedgerHistory.cpp +++ b/LedgerHistory.cpp @@ -27,8 +27,10 @@ void LedgerHistory::addAcceptedLedger(Ledger::pointer ledger) Ledger::pointer LedgerHistory::getLedgerBySeq(uint32 index) { boost::recursive_mutex::scoped_lock sl(mLock); - if(mLedgersByIndex.count(index)) return mLedgersByIndex[index]; + std::map::iterator it(mLedgersByIndex.find(index)); + if(it!=mLedgersByIndex.end()) return it->second; sl.unlock(); + Ledger::pointer ret(Ledger::loadByIndex(index)); if(!ret) return ret; @@ -43,9 +45,10 @@ Ledger::pointer LedgerHistory::getLedgerBySeq(uint32 index) Ledger::pointer LedgerHistory::getLedgerByHash(const uint256& hash) { boost::recursive_mutex::scoped_lock sl(mLock); - if(mLedgersByHash.count(hash)!=0) return mLedgersByHash[hash]; - + std::map::iterator it(mLedgersByHash.find(hash)); + if(it!=mLedgersByHash.end()) return it->second; sl.unlock(); + Ledger::pointer ret=Ledger::loadByHash(hash); if(!ret) return ret; @@ -61,11 +64,19 @@ Ledger::pointer LedgerHistory::canonicalizeLedger(Ledger::pointer ledger, bool s uint256 h(ledger->getHash()); boost::recursive_mutex::scoped_lock sl(mLock); - if(mLedgersByHash.count(h)!=0) return mLedgersByHash[h]; - if(!save) return ledger; - - assert(ledger->getHash()==h); - mLedgersByHash.insert(std::make_pair(h, ledger)); - if(ledger->isAccepted()) mLedgersByIndex.insert(std::make_pair(ledger->getLedgerSeq(), ledger)); + if(!save) + { // return input ledger if not in map, otherwise, return corresponding map ledger + std::map::iterator it(mLedgersByHash.find(h)); + if(it!=mLedgersByHash.end()) return it->second; + } + else + { // save input ledger in map if not in map, otherwise return corresponding map ledger + std::pair::iterator, bool> sp(mLedgersByHash.insert(std::make_pair(h, ledger))); + if(!sp.second) // ledger was not inserted + return sp.first->second; + if(ledger->isAccepted()) + mLedgersByIndex.insert(std::make_pair(ledger->getLedgerSeq(), ledger)); + } return ledger; } diff --git a/PubKeyCache.cpp b/PubKeyCache.cpp index a6f4cfe1b..130257a25 100644 --- a/PubKeyCache.cpp +++ b/PubKeyCache.cpp @@ -9,14 +9,8 @@ CKey::pointer PubKeyCache::locate(const uint160& id) if(1) { // is it in cache boost::mutex::scoped_lock sl(mLock); - CKey::pointer ret=mCache[id]; - if(ret) - { -#ifdef DEBUG - std::cerr << "PubKey found in cache (locate)" << std::endl; -#endif - return ret; - } + std::map::iterator it(mCache.find(id)); + if(it!=mCache.end()) return it->second; } std::string sql="SELECT * from PubKeys WHERE ID='"; @@ -44,7 +38,7 @@ CKey::pointer PubKeyCache::locate(const uint160& id) if(1) { // put it in cache (okay if we race with another retriever) boost::mutex::scoped_lock sl(mLock); - mCache[id]=ckp; + mCache.insert(std::make_pair(id, ckp)); } return ckp; } @@ -54,15 +48,9 @@ CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key) if(1) { boost::mutex::scoped_lock sl(mLock); - CKey::pointer cached(mCache[id]); - if(cached) - { -#ifdef DEBUG - std::cerr << "PubKey found in cache (store)" << std::endl; -#endif - return cached; - } - mCache[id]=key; + std::pair::iterator, bool> pit(mCache.insert(std::make_pair(id, key))); + if(!pit.second) // there was an existing key + return pit.first->second; } std::string sql="INSERT INTO PubKeys (ID, PubKey) VALUES ('"; sql+=id.GetHex(); @@ -73,7 +61,7 @@ CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key) theApp->getDB()->escape(&(pk.front()), pk.size(), encodedPK); sql+=encodedPK; sql.append(");"); - ScopedLock sl(theApp->getDBLock()); + ScopedLock dbl(theApp->getDBLock()); theApp->getDB()->executeSQL(sql.c_str()); return key; }