Simplify and fix possible bugs in the way accepted ledgers are written

to SQL. Set up for ledger scraping.
This commit is contained in:
JoelKatz
2013-03-11 23:20:26 -07:00
parent 01655f4fe1
commit 1cd65213d8

View File

@@ -405,7 +405,9 @@ void Ledger::saveAcceptedLedger(Job&, bool fromConsensus)
cLog(lsTRACE) << "saveAcceptedLedger " << (fromConsensus ? "fromConsensus " : "fromAcquire ") << getLedgerSeq();
static boost::format ledgerExists("SELECT LedgerSeq FROM Ledgers INDEXED BY SeqLedger where LedgerSeq = %u;");
static boost::format deleteLedger("DELETE FROM Ledgers WHERE LedgerSeq = %u;");
static boost::format AcctTransExists("SELECT LedgerSeq FROM AccountTransactions WHERE TransID = '%s';");
static boost::format deleteTrans1("DELETE FROM Transactions WHERE LedgerSeq = %u;");
static boost::format deleteTrans2("DELETE FROM AccountTransactions WHERE LedgerSeq = %u;");
static boost::format deleteAcctTrans("DELTEE FROM AccountTransactions WHERE TransID = '%s';");
static boost::format transExists("SELECT Status FROM Transactions WHERE TransID = '%s';");
static boost::format
updateTx("UPDATE Transactions SET LedgerSeq = %u, Status = '%c', TxnMeta = %s WHERE TransID = '%s';");
@@ -430,32 +432,29 @@ void Ledger::saveAcceptedLedger(Job&, bool fromConsensus)
AcceptedLedger::pointer aLedger = AcceptedLedger::makeAcceptedLedger(shared_from_this());
{
{
ScopedLock sl(theApp->getLedgerDB()->getDBLock());
if (SQL_EXISTS(theApp->getLedgerDB()->getDB(), boost::str(ledgerExists % mLedgerSeq)))
theApp->getLedgerDB()->getDB()->executeSQL(boost::str(deleteLedger % mLedgerSeq));
}
Database *db = theApp->getTxnDB()->getDB();
{
Database *db = theApp->getTxnDB()->getDB();
ScopedLock dbLock(theApp->getTxnDB()->getDBLock());
db->executeSQL("BEGIN TRANSACTION;");
db->executeSQL(boost::str(deleteTrans1 % mLedgerSeq));
db->executeSQL(boost::str(deleteTrans2 % mLedgerSeq));
BOOST_FOREACH(const AcceptedLedger::value_type& vt, aLedger->getMap())
{
cLog(lsTRACE) << "Saving: " << vt.second.getJson(0);
uint256 txID = vt.second.getTransactionID();
theApp->getMasterTransaction().inLedger(txID, mLedgerSeq);
// Make sure transaction is in AccountTransactions.
if (!SQL_EXISTS(db, boost::str(AcctTransExists % txID.GetHex())))
{
// Transaction not in AccountTransactions
db->executeSQL(boost::str(deleteAcctTrans % txID.GetHex()));
const std::vector<RippleAddress>& accts = vt.second.getAffected();
if (!accts.empty())
{
std::string sql = "INSERT OR REPLACE INTO AccountTransactions (TransID, Account, LedgerSeq) VALUES ";
bool first = true;
for (std::vector<RippleAddress>::const_iterator it = accts.begin(), end = accts.end(); it != end; ++it)
@@ -480,7 +479,6 @@ void Ledger::saveAcceptedLedger(Job&, bool fromConsensus)
}
else
cLog(lsWARNING) << "Transaction in ledger " << mLedgerSeq << " affects no accounts";
}
if (SQL_EXISTS(db, boost::str(transExists % txID.GetHex())))
{
@@ -513,7 +511,6 @@ void Ledger::saveAcceptedLedger(Job&, bool fromConsensus)
mCloseResolution % mCloseFlags %
mAccountHash.GetHex() % mTransHash.GetHex()));
}
}
if (!fromConsensus)
dropCache();