Use the Sqlite3 prepared statement API for an expensive operation on startup.

This commit is contained in:
JoelKatz
2013-01-27 23:41:30 -08:00
parent d522f04385
commit 663c4bd8e5

View File

@@ -7,6 +7,8 @@
#include "../json/writer.h"
#include "../database/SqliteDatabase.h"
#include "Application.h"
#include "Ledger.h"
#include "utils.h"
@@ -566,6 +568,39 @@ uint256 Ledger::getHashByIndex(uint32 ledgerIndex)
bool Ledger::getHashesByIndex(uint32 ledgerIndex, uint256& ledgerHash, uint256& parentHash)
{
#ifndef NO_SQLITE3_PREPARE
DatabaseCon *con = theApp->getLedgerDB();
ScopedLock sl(con->getDBLock());
static SqliteStatement pSt(con->getDB()->getSqliteDB(),
"SELECT LedgerHash,PrevHash FROM Ledgers Where LedgerSeq = ?;");
pSt.reset();
pSt.bind(1, ledgerIndex);
int ret = pSt.step();
if (pSt.isDone(ret))
{
cLog(lsTRACE) << "Don't have ledger " << ledgerIndex;
return false;
}
if (!pSt.isRow(ret))
{
assert(false);
cLog(lsFATAL) << "Unexpected statement result " << ret;
return false;
}
ledgerHash.SetHex(pSt.peekString(0));
parentHash.SetHex(pSt.peekString(1));
assert(ledgerHash.isNonZero() && parentHash.isNonZero());
return true;
#else
std::string sql="SELECT LedgerHash,PrevHash FROM Ledgers WHERE LedgerSeq='";
sql.append(boost::lexical_cast<std::string>(ledgerIndex));
sql.append("';");
@@ -587,6 +622,8 @@ bool Ledger::getHashesByIndex(uint32 ledgerIndex, uint256& ledgerHash, uint256&
assert(ledgerHash.isNonZero() && ((ledgerIndex == 0) || parentHash.isNonZero()));
return true;
#endif
}
Ledger::pointer Ledger::loadByIndex(uint32 ledgerIndex)