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)
{
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();
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<uint256, Ledger::pointer>::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<uint256, Ledger::pointer>::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<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;
}

View File

@@ -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<uint160, CKey::pointer>::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<std::map<uint160,CKey::pointer>::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;
}