Fix database getNextRow().

This commit is contained in:
Arthur Britto
2012-05-05 16:45:33 -07:00
parent e55dd7ade6
commit e7c362457e
6 changed files with 33 additions and 38 deletions

View File

@@ -86,18 +86,18 @@ HashedObject::pointer HashedObject::retrieve(const uint256& hash)
uint32 index; uint32 index;
std::vector<unsigned char> data; std::vector<unsigned char> data;
data.reserve(8192); data.reserve(8192);
if(1)
{ {
ScopedLock sl(theApp->getHashNodeDB()->getDBLock()); ScopedLock sl(theApp->getHashNodeDB()->getDBLock());
Database* db=theApp->getHashNodeDB()->getDB(); Database* db=theApp->getHashNodeDB()->getDB();
if(!db->executeSQL(sql.c_str()) || !db->startIterRows() || !db->getNextRow()) if(!db->executeSQL(sql.c_str()) || !db->startIterRows())
return HashedObject::pointer(); return HashedObject::pointer();
std::string type; std::string type;
db->getStr("ObjType", type); db->getStr("ObjType", type);
if(type.size()==0) return HashedObject::pointer(); if(type.size()==0) return HashedObject::pointer();
index=db->getBigInt("LedgerIndex"); index=db->getBigInt("LedgerIndex");
int size=db->getBinary("Object", NULL, 0); int size=db->getBinary("Object", NULL, 0);
@@ -105,7 +105,7 @@ HashedObject::pointer HashedObject::retrieve(const uint256& hash)
db->getBinary("Object", &(data.front()), size); db->getBinary("Object", &(data.front()), size);
db->endIterRows(); db->endIterRows();
} }
HashedObjectType htype=UNKNOWN; HashedObjectType htype=UNKNOWN;
switch(type[0]) switch(type[0])
{ {
@@ -114,7 +114,7 @@ HashedObject::pointer HashedObject::retrieve(const uint256& hash)
case 'A': htype=ACCOUNT_NODE; break; case 'A': htype=ACCOUNT_NODE; break;
case 'N': htype=TRANSACTION_NODE; break; case 'N': htype=TRANSACTION_NODE; break;
} }
HashedObject::pointer obj(new HashedObject(htype, index, data)); HashedObject::pointer obj(new HashedObject(htype, index, data));
obj->mHash=hash; obj->mHash=hash;
#ifdef DEBUG #ifdef DEBUG
@@ -122,3 +122,4 @@ HashedObject::pointer HashedObject::retrieve(const uint256& hash)
#endif #endif
return obj; return obj;
} }
// vim:ts=4

View File

@@ -269,7 +269,8 @@ Ledger::pointer Ledger::getSQL(const std::string& sql)
{ {
ScopedLock sl(theApp->getLedgerDB()->getDBLock()); ScopedLock sl(theApp->getLedgerDB()->getDBLock());
Database *db = theApp->getLedgerDB()->getDB(); Database *db = theApp->getLedgerDB()->getDB();
if (!db->executeSQL(sql.c_str()) || !db->startIterRows() || !db->getNextRow())
if (!db->executeSQL(sql) || !db->startIterRows())
return Ledger::pointer(); return Ledger::pointer();
db->getStr("LedgerHash", hash); db->getStr("LedgerHash", hash);
@@ -285,7 +286,7 @@ Ledger::pointer Ledger::getSQL(const std::string& sql)
ledgerSeq = db->getBigInt("LedgerSeq"); ledgerSeq = db->getBigInt("LedgerSeq");
db->endIterRows(); db->endIterRows();
} }
Ledger::pointer ret=boost::make_shared<Ledger>(prevHash, transHash, accountHash, totCoins, closingTime, ledgerSeq); Ledger::pointer ret=boost::make_shared<Ledger>(prevHash, transHash, accountHash, totCoins, closingTime, ledgerSeq);
if (ret->getHash() != ledgerHash) if (ret->getHash() != ledgerHash)
{ {
@@ -425,3 +426,4 @@ bool Ledger::isAcquiringAS(void)
{ {
return mAccountStateMap->isSynching(); return mAccountStateMap->isSynching();
} }
// vim:ts=4

View File

@@ -19,11 +19,11 @@ CKey::pointer PubKeyCache::locate(const NewcoinAddress& id)
std::vector<unsigned char> data; std::vector<unsigned char> data;
data.reserve(65); // our public keys are actually 33 bytes data.reserve(65); // our public keys are actually 33 bytes
int pkSize; int pkSize;
if(1)
{ // is it in the database { // is it in the database
ScopedLock sl(theApp->getTxnDB()->getDBLock()); ScopedLock sl(theApp->getTxnDB()->getDBLock());
Database* db=theApp->getTxnDB()->getDB(); Database* db=theApp->getTxnDB()->getDB();
if(!db->executeSQL(sql.c_str()) || !db->startIterRows() || !db->getNextRow()) if(!db->executeSQL(sql.c_str()) || !db->startIterRows())
return CKey::pointer(); return CKey::pointer();
pkSize=db->getBinary("PubKey", &(data.front()), data.size()); pkSize=db->getBinary("PubKey", &(data.front()), data.size());
db->endIterRows(); db->endIterRows();
@@ -36,7 +36,6 @@ CKey::pointer PubKeyCache::locate(const NewcoinAddress& id)
return CKey::pointer(); return CKey::pointer();
} }
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.insert(std::make_pair(id, ckp)); mCache.insert(std::make_pair(id, ckp));
@@ -46,7 +45,6 @@ CKey::pointer PubKeyCache::locate(const NewcoinAddress& id)
CKey::pointer PubKeyCache::store(const NewcoinAddress& id, CKey::pointer key) CKey::pointer PubKeyCache::store(const NewcoinAddress& id, CKey::pointer key)
{ // stored if needed, returns cached copy (possibly the original) { // stored if needed, returns cached copy (possibly the original)
if(1)
{ {
boost::mutex::scoped_lock sl(mLock); boost::mutex::scoped_lock sl(mLock);
std::pair<std::map<NewcoinAddress,CKey::pointer>::iterator, bool> pit(mCache.insert(std::make_pair(id, key))); std::pair<std::map<NewcoinAddress,CKey::pointer>::iterator, bool> pit(mCache.insert(std::make_pair(id, key)));
@@ -74,3 +72,4 @@ void PubKeyCache::clear()
boost::mutex::scoped_lock sl(mLock); boost::mutex::scoped_lock sl(mLock);
mCache.empty(); mCache.empty();
} }
// vim:ts=4

View File

@@ -225,7 +225,7 @@ bool Transaction::save() const
theApp->getTxnDB()->getDB()->escape(static_cast<const unsigned char *>(s.getDataPtr()), s.getLength(), rawTxn); theApp->getTxnDB()->getDB()->escape(static_cast<const unsigned char *>(s.getDataPtr()), s.getLength(), rawTxn);
sql.append(rawTxn); sql.append(rawTxn);
sql.append(");"); sql.append(");");
ScopedLock sl(theApp->getTxnDB()->getDBLock()); ScopedLock sl(theApp->getTxnDB()->getDBLock());
Database* db = theApp->getTxnDB()->getDB(); Database* db = theApp->getTxnDB()->getDB();
return db->executeSQL(sql.c_str()); return db->executeSQL(sql.c_str());
@@ -237,12 +237,11 @@ Transaction::pointer Transaction::transactionFromSQL(const std::string& sql)
std::string status; std::string status;
rawTxn.reserve(2048); rawTxn.reserve(2048);
if(1)
{ {
ScopedLock sl(theApp->getTxnDB()->getDBLock()); ScopedLock sl(theApp->getTxnDB()->getDBLock());
Database* db = theApp->getTxnDB()->getDB(); Database* db = theApp->getTxnDB()->getDB();
if (!db->executeSQL(sql.c_str(), true) || !db->startIterRows() || !db->getNextRow()) if (!db->executeSQL(sql, true) || !db->startIterRows())
return Transaction::pointer(); return Transaction::pointer();
db->getStr("Status", status); db->getStr("Status", status);
@@ -258,7 +257,7 @@ Transaction::pointer Transaction::transactionFromSQL(const std::string& sql)
Transaction::pointer tr = boost::make_shared<Transaction>(txn, true); Transaction::pointer tr = boost::make_shared<Transaction>(txn, true);
TransStatus st(INVALID); TransStatus st(INVALID);
switch(status[0]) switch (status[0])
{ {
case 'N': st = NEW; break; case 'N': st = NEW; break;
case 'A': st = INCLUDED; break; case 'A': st = INCLUDED; break;
@@ -382,3 +381,4 @@ Json::Value Transaction::getJson(bool decorate, bool paid, bool credited) const
return ret; return ret;
} }
// vim:ts=4

View File

@@ -50,24 +50,18 @@ bool ValidationCollection::hasValidation(uint32 ledgerIndex,uint160& hanko,uint3
db->escape(hanko.begin(),hanko.GetSerializeSize(),hankoStr); db->escape(hanko.begin(),hanko.GetSerializeSize(),hankoStr);
string sql=strprintf("SELECT ValidationID,seqnum from Validations where LedgerIndex=%d and hanko=%s", string sql=strprintf("SELECT ValidationID,seqnum from Validations where LedgerIndex=%d and hanko=%s",
ledgerIndex,hankoStr.c_str()); ledgerIndex,hankoStr.c_str());
if(db->executeSQL(sql.c_str())) if(db->executeSQL(sql) && db->startIterRows())
{ {
if(db->startIterRows()) uint32 currentSeqNum=db->getInt(1);
if(currentSeqNum>=seqnum)
{ {
if(db->getNextRow()) db->endIterRows();
{ return(true);
uint32 currentSeqNum=db->getInt(1);
if(currentSeqNum>=seqnum)
{
db->endIterRows();
return(true);
}
// delete the old validation we were storing
sql=strprintf("DELETE FROM Validations where ValidationID=%d",db->getInt(0));
db->endIterRows();
db->executeSQL(sql.c_str());
}
} }
// delete the old validation we were storing
sql=strprintf("DELETE FROM Validations where ValidationID=%d",db->getInt(0));
db->endIterRows();
db->executeSQL(sql);
} }
return(false); return(false);
} }
@@ -191,7 +185,6 @@ void ValidationCollection::getValidations(uint32 ledgerIndex,vector<newcoin::Val
string sql=strprintf("SELECT * From Validations where LedgerIndex=%d and wecare=1",ledgerIndex); string sql=strprintf("SELECT * From Validations where LedgerIndex=%d and wecare=1",ledgerIndex);
// TODO: ValidationCollection::getValidations(uint32 ledgerIndex) // TODO: ValidationCollection::getValidations(uint32 ledgerIndex)
} }
@@ -203,7 +196,6 @@ bool ValidationCollection::getConsensusLedger(uint32 ledgerIndex, uint256& ourHa
bool ret=false; bool ret=false;
if(mIndexGroups.count(ledgerIndex)) if(mIndexGroups.count(ledgerIndex))
{ {
unsigned int maxVotes=theConfig.MIN_VOTES_FOR_CONSENSUS; unsigned int maxVotes=theConfig.MIN_VOTES_FOR_CONSENSUS;
vector< Group >& groups=mIndexGroups[ledgerIndex]; vector< Group >& groups=mIndexGroups[ledgerIndex];
Group empty; Group empty;
@@ -228,6 +220,7 @@ bool ValidationCollection::getConsensusLedger(uint32 ledgerIndex, uint256& ourHa
} }
} }
} }
return(ret); return(ret);
} }
// vim:ts=4

View File

@@ -190,7 +190,7 @@ LocalAccountFamily::pointer LocalAccountFamily::readFamily(const NewcoinAddress&
ScopedLock sl(theApp->getWalletDB()->getDBLock()); ScopedLock sl(theApp->getWalletDB()->getDBLock());
Database *db=theApp->getWalletDB()->getDB(); Database *db=theApp->getWalletDB()->getDB();
if(!db->executeSQL(sql.c_str()) || !db->startIterRows() || !db->getNextRow()) if(!db->executeSQL(sql.c_str()) || !db->startIterRows())
return LocalAccountFamily::pointer(); return LocalAccountFamily::pointer();
db->getStr("Comment", comment); db->getStr("Comment", comment);
@@ -456,8 +456,7 @@ void Wallet::load()
if(!db->startIterRows()) return; if(!db->startIterRows()) return;
while(db->getNextRow()) do {
{
std::string strGenerator, strComment; std::string strGenerator, strComment;
db->getStr("FamilyGenerator", strGenerator); db->getStr("FamilyGenerator", strGenerator);
@@ -476,7 +475,8 @@ void Wallet::load()
f->setComment(strComment); f->setComment(strComment);
} }
else assert(false); else assert(false);
} } while(db->getNextRow());
db->endIterRows(); db->endIterRows();
} }