Get this exactly right.

This commit is contained in:
JoelKatz
2011-12-16 22:14:09 -08:00
parent 715bff43ef
commit 0b8aaf691f
2 changed files with 27 additions and 28 deletions

View File

@@ -27,8 +27,10 @@ void LedgerHistory::addAcceptedLedger(Ledger::pointer ledger)
Ledger::pointer LedgerHistory::getLedgerBySeq(uint32 index) Ledger::pointer LedgerHistory::getLedgerBySeq(uint32 index)
{ {
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
if(mLedgersByIndex.count(index)) return mLedgersByIndex[index]; std::map<uint32, Ledger::pointer>::iterator it(mLedgersByIndex.find(index));
if(it!=mLedgersByIndex.end()) return it->second;
sl.unlock(); sl.unlock();
Ledger::pointer ret(Ledger::loadByIndex(index)); Ledger::pointer ret(Ledger::loadByIndex(index));
if(!ret) return ret; if(!ret) return ret;
@@ -43,9 +45,10 @@ Ledger::pointer LedgerHistory::getLedgerBySeq(uint32 index)
Ledger::pointer LedgerHistory::getLedgerByHash(const uint256& hash) Ledger::pointer LedgerHistory::getLedgerByHash(const uint256& hash)
{ {
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
if(mLedgersByHash.count(hash)!=0) return mLedgersByHash[hash]; std::map<uint256, Ledger::pointer>::iterator it(mLedgersByHash.find(hash));
if(it!=mLedgersByHash.end()) return it->second;
sl.unlock(); sl.unlock();
Ledger::pointer ret=Ledger::loadByHash(hash); Ledger::pointer ret=Ledger::loadByHash(hash);
if(!ret) return ret; if(!ret) return ret;
@@ -61,11 +64,19 @@ Ledger::pointer LedgerHistory::canonicalizeLedger(Ledger::pointer ledger, bool s
uint256 h(ledger->getHash()); uint256 h(ledger->getHash());
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
if(mLedgersByHash.count(h)!=0) return mLedgersByHash[h]; if(!save)
if(!save) return ledger; { // return input ledger if not in map, otherwise, return corresponding map ledger
std::map<uint256, Ledger::pointer>::iterator it(mLedgersByHash.find(h));
assert(ledger->getHash()==h); if(it!=mLedgersByHash.end()) return it->second;
mLedgersByHash.insert(std::make_pair(h, ledger)); }
if(ledger->isAccepted()) mLedgersByIndex.insert(std::make_pair(ledger->getLedgerSeq(), ledger)); else
{ // save input ledger in map if not in map, otherwise return corresponding map ledger
std::pair<std::map<uint256,
Ledger::pointer>::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; return ledger;
} }

View File

@@ -9,14 +9,8 @@ CKey::pointer PubKeyCache::locate(const uint160& id)
if(1) if(1)
{ // is it in cache { // is it in cache
boost::mutex::scoped_lock sl(mLock); boost::mutex::scoped_lock sl(mLock);
CKey::pointer ret=mCache[id]; std::map<uint160, CKey::pointer>::iterator it(mCache.find(id));
if(ret) if(it!=mCache.end()) return it->second;
{
#ifdef DEBUG
std::cerr << "PubKey found in cache (locate)" << std::endl;
#endif
return ret;
}
} }
std::string sql="SELECT * from PubKeys WHERE ID='"; std::string sql="SELECT * from PubKeys WHERE ID='";
@@ -44,7 +38,7 @@ CKey::pointer PubKeyCache::locate(const uint160& id)
if(1) if(1)
{ // put it in cache (okay if we race with another retriever) { // put it in cache (okay if we race with another retriever)
boost::mutex::scoped_lock sl(mLock); boost::mutex::scoped_lock sl(mLock);
mCache[id]=ckp; mCache.insert(std::make_pair(id, ckp));
} }
return ckp; return ckp;
} }
@@ -54,15 +48,9 @@ CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key)
if(1) if(1)
{ {
boost::mutex::scoped_lock sl(mLock); boost::mutex::scoped_lock sl(mLock);
CKey::pointer cached(mCache[id]); std::pair<std::map<uint160,CKey::pointer>::iterator, bool> pit(mCache.insert(std::make_pair(id, key)));
if(cached) if(!pit.second) // there was an existing key
{ return pit.first->second;
#ifdef DEBUG
std::cerr << "PubKey found in cache (store)" << std::endl;
#endif
return cached;
}
mCache[id]=key;
} }
std::string sql="INSERT INTO PubKeys (ID, PubKey) VALUES ('"; std::string sql="INSERT INTO PubKeys (ID, PubKey) VALUES ('";
sql+=id.GetHex(); 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); theApp->getDB()->escape(&(pk.front()), pk.size(), encodedPK);
sql+=encodedPK; sql+=encodedPK;
sql.append(");"); sql.append(");");
ScopedLock sl(theApp->getDBLock()); ScopedLock dbl(theApp->getDBLock());
theApp->getDB()->executeSQL(sql.c_str()); theApp->getDB()->executeSQL(sql.c_str());
return key; return key;
} }