Separate the databases, give them their own connections and locks.

This commit is contained in:
JoelKatz
2011-12-29 20:55:03 -08:00
parent 8c5918b576
commit 91cbe75066
9 changed files with 88 additions and 61 deletions

View File

@@ -25,14 +25,24 @@ What needs to happen:
*/ */
Application::Application() DatabaseCon::DatabaseCon(const std::string& name)
{
std::string path=strprintf("%s%s", theConfig.DATA_DIR.c_str(), name.c_str());
mDatabase=new SqliteDatabase(path.c_str());
mDatabase->connect();
}
DatabaseCon::~DatabaseCon()
{
mDatabase->disconnect();
delete mDatabase;
}
Application::Application() :
mTxnDB(NULL), mLedgerDB(NULL), mWalletDB(NULL), mHashNodeDB(NULL), mNetNodeDB(NULL),
mPeerDoor(NULL), mRPCDoor(NULL)
{ {
theConfig.load(); theConfig.load();
//mUNL.load();
mPeerDoor=NULL;
mRPCDoor=NULL;
mDatabase=NULL;
uint160 rootFamily=mWallet.addFamily("This is my payphrase.", true); uint160 rootFamily=mWallet.addFamily("This is my payphrase.", true);
LocalAccount::pointer rootAccount=mWallet.getLocalAccount(rootFamily, 0); LocalAccount::pointer rootAccount=mWallet.getLocalAccount(rootFamily, 0);
@@ -53,9 +63,11 @@ Application::Application()
void Application::run() void Application::run()
{ {
std::string filename=strprintf("%sdata.db",theConfig.DATA_DIR.c_str()); mTxnDB=new DatabaseCon("transaction.db");
theApp->setDB(new SqliteDatabase(filename.c_str())); mLedgerDB=new DatabaseCon("ledger.db");
mDatabase->connect(); mWalletDB=new DatabaseCon("wallet.db");
mHashNodeDB=new DatabaseCon("hashnode.db");
mNetNodeDB=new DatabaseCon("netnode.db");
if(theConfig.PEER_PORT) if(theConfig.PEER_PORT)
{ {

View File

@@ -18,6 +18,19 @@
class RPCDoor; class RPCDoor;
class PeerDoor; class PeerDoor;
class DatabaseCon
{
protected:
Database *mDatabase;
boost::recursive_mutex mLock;
public:
DatabaseCon(const std::string& name);
~DatabaseCon();
Database* getDB() { return mDatabase; }
ScopedLock getDBLock() { return ScopedLock(mLock); }
};
class Application class Application
{ {
NetworkOPs mNetOps; NetworkOPs mNetOps;
@@ -28,7 +41,8 @@ class Application
KnownNodeList mKnownNodes; KnownNodeList mKnownNodes;
PubKeyCache mPKCache; PubKeyCache mPKCache;
LedgerMaster mMasterLedger; LedgerMaster mMasterLedger;
Database* mDatabase;
DatabaseCon *mTxnDB, *mLedgerDB, *mWalletDB, *mHashNodeDB, *mNetNodeDB;
ConnectionPool mConnectionPool; ConnectionPool mConnectionPool;
PeerDoor* mPeerDoor; PeerDoor* mPeerDoor;
@@ -38,7 +52,6 @@ class Application
boost::recursive_mutex mPeerMapLock; boost::recursive_mutex mPeerMapLock;
boost::asio::io_service mIOService; boost::asio::io_service mIOService;
boost::recursive_mutex dbLock;
public: public:
@@ -57,9 +70,11 @@ public:
LedgerMaster& getMasterLedger() { return mMasterLedger; } LedgerMaster& getMasterLedger() { return mMasterLedger; }
ScopedLock getDBLock() { return ScopedLock(dbLock); } DatabaseCon* getTxnDB() { return mTxnDB; }
void setDB(Database* db) { mDatabase=db; } DatabaseCon* getLedgerDB() { return mLedgerDB; }
Database* getDB() { return(mDatabase); } DatabaseCon* getWalletDB() { return mWalletDB; }
DatabaseCon* getHashNodeDB() { return mHashNodeDB; }
DatabaseCon* getNetNodeDB() { return mNetNodeDB; }
//Serializer* getSerializer(){ return(mSerializer); } //Serializer* getSerializer(){ return(mSerializer); }
//void setSerializer(Serializer* ser){ mSerializer=ser; } //void setSerializer(Serializer* ser){ mSerializer=ser; }

View File

@@ -57,12 +57,12 @@ bool HashedObject::store(HashedObjectType type, uint32 index, const std::vector<
sql.append("',"); sql.append("',");
std::string obj; std::string obj;
theApp->getDB()->escape(&(data.front()), data.size(), obj); theApp->getHashNodeDB()->getDB()->escape(&(data.front()), data.size(), obj);
sql.append(obj); sql.append(obj);
sql.append(");"); sql.append(");");
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getHashNodeDB()->getDBLock());
Database* db=theApp->getDB(); Database* db=theApp->getHashNodeDB()->getDB();
return db->executeSQL(sql.c_str()); return db->executeSQL(sql.c_str());
} }
@@ -86,8 +86,8 @@ HashedObject::pointer HashedObject::retrieve(const uint256& hash)
data.reserve(8192); data.reserve(8192);
if(1) if(1)
{ {
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getHashNodeDB()->getDBLock());
Database* db=theApp->getDB(); Database* db=theApp->getHashNodeDB()->getDB();
if(!db->executeSQL(sql.c_str())) return HashedObject::pointer(); if(!db->executeSQL(sql.c_str())) return HashedObject::pointer();
if(!db->getNextRow()) return HashedObject::pointer(); if(!db->getNextRow()) return HashedObject::pointer();
@@ -101,6 +101,7 @@ HashedObject::pointer HashedObject::retrieve(const uint256& hash)
int size=db->getBinary("Object", NULL, 0); int size=db->getBinary("Object", NULL, 0);
data.resize(size); data.resize(size);
db->getBinary("Object", &(data.front()), size); db->getBinary("Object", &(data.front()), size);
db->endIterRows();
} }
HashedObjectType htype=UNKNOWN; HashedObjectType htype=UNKNOWN;

View File

@@ -321,8 +321,8 @@ void Ledger::saveAcceptedLedger(Ledger::pointer ledger)
sql.append(ledger->mTransHash.GetHex()); sql.append(ledger->mTransHash.GetHex());
sql.append("');"); sql.append("');");
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getLedgerDB()->getDBLock());
theApp->getDB()->executeSQL(sql.c_str()); theApp->getLedgerDB()->getDB()->executeSQL(sql.c_str());
// write out dirty nodes // write out dirty nodes
while(ledger->mTransactionMap->flushDirty(64, TRANSACTION_NODE, ledger->mLedgerSeq)) while(ledger->mTransactionMap->flushDirty(64, TRANSACTION_NODE, ledger->mLedgerSeq))
@@ -341,8 +341,8 @@ Ledger::pointer Ledger::getSQL(const std::string& sql)
if(1) if(1)
{ {
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getLedgerDB()->getDBLock());
Database *db=theApp->getDB(); Database *db=theApp->getLedgerDB()->getDB();
if(!db->executeSQL(sql.c_str()) || !db->getNextRow()) if(!db->executeSQL(sql.c_str()) || !db->getNextRow())
return Ledger::pointer(); return Ledger::pointer();
@@ -357,6 +357,7 @@ Ledger::pointer Ledger::getSQL(const std::string& sql)
feeHeld=db->getBigInt("FeeHeld"); feeHeld=db->getBigInt("FeeHeld");
closingTime=db->getBigInt("ClosingTime"); closingTime=db->getBigInt("ClosingTime");
ledgerSeq=db->getBigInt("LedgerSeq"); ledgerSeq=db->getBigInt("LedgerSeq");
db->endIterRows();
} }
Ledger::pointer ret(new Ledger(prevHash, transHash, accountHash, feeHeld, closingTime, ledgerSeq)); Ledger::pointer ret(new Ledger(prevHash, transHash, accountHash, feeHeld, closingTime, ledgerSeq));

View File

@@ -21,11 +21,12 @@ CKey::pointer PubKeyCache::locate(const uint160& id)
int pkSize; int pkSize;
if(1) if(1)
{ // is it in the database { // is it in the database
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getTxnDB()->getDBLock());
Database* db=theApp->getDB(); Database* db=theApp->getTxnDB()->getDB();
if(!db->executeSQL(sql.c_str())) return CKey::pointer(); if(!db->executeSQL(sql.c_str())) return CKey::pointer();
if(!db->getNextRow()) return CKey::pointer(); if(!db->getNextRow()) return CKey::pointer();
pkSize=db->getBinary("PubKey", &(data.front()), data.size()); pkSize=db->getBinary("PubKey", &(data.front()), data.size());
db->endIterRows();
} }
data.resize(pkSize); data.resize(pkSize);
CKey::pointer ckp(new CKey()); CKey::pointer ckp(new CKey());
@@ -58,11 +59,11 @@ CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key)
std::vector<unsigned char> pk=key->GetPubKey(); std::vector<unsigned char> pk=key->GetPubKey();
std::string encodedPK; std::string encodedPK;
theApp->getDB()->escape(&(pk.front()), pk.size(), encodedPK); theApp->getTxnDB()->getDB()->escape(&(pk.front()), pk.size(), encodedPK);
sql+=encodedPK; sql+=encodedPK;
sql.append(");"); sql.append(");");
ScopedLock dbl(theApp->getDBLock()); ScopedLock dbl(theApp->getTxnDB()->getDBLock());
theApp->getDB()->executeSQL(sql.c_str()); theApp->getTxnDB()->getDB()->executeSQL(sql.c_str());
return key; return key;
} }

2
RPC.h
View File

@@ -27,7 +27,7 @@ extern std::string JSONRPCRequest(const std::string& strMethod, const Json::Valu
const Json::Value& id); const Json::Value& id);
extern std::string createHTTPPost(const std::string& strMsg, extern std::string createHTTPPost(const std::string& strMsg,
const std::map<std::string,std::string>& mapRequestHeaders); const std::map<std::string, std::string>& mapRequestHeaders);
extern int ReadHTTP(std::basic_istream<char>& stream, extern int ReadHTTP(std::basic_istream<char>& stream,
std::map<std::string, std::string>& mapHeadersRet, std::string& strMessageRet); std::map<std::string, std::string>& mapHeadersRet, std::string& strMessageRet);

View File

@@ -154,12 +154,12 @@ bool Transaction::save() const
default: sql.append("','U',"); break; default: sql.append("','U',"); break;
} }
std::string signature; std::string signature;
theApp->getDB()->escape(&(mSignature.front()), mSignature.size(), signature); theApp->getTxnDB()->getDB()->escape(&(mSignature.front()), mSignature.size(), signature);
sql.append(signature); sql.append(signature);
sql.append(");"); sql.append(");");
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getTxnDB()->getDBLock());
Database* db=theApp->getDB(); Database* db=theApp->getTxnDB()->getDB();
return db->executeSQL(sql.c_str()); return db->executeSQL(sql.c_str());
} }
@@ -172,8 +172,8 @@ Transaction::pointer Transaction::transactionFromSQL(const std::string& sql)
signature.reserve(78); signature.reserve(78);
if(1) if(1)
{ {
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getTxnDB()->getDBLock());
Database* db=theApp->getDB(); Database* db=theApp->getTxnDB()->getDB();
if(!db->executeSQL(sql.c_str())) return Transaction::pointer(); if(!db->executeSQL(sql.c_str())) return Transaction::pointer();
if(!db->getNextRow()) return Transaction::pointer(); if(!db->getNextRow()) return Transaction::pointer();
@@ -190,6 +190,7 @@ Transaction::pointer Transaction::transactionFromSQL(const std::string& sql)
db->getStr("Status", status); db->getStr("Status", status);
int sigSize=db->getBinary("Signature", &(signature.front()), signature.size()); int sigSize=db->getBinary("Signature", &(signature.front()), signature.size());
signature.resize(sigSize); signature.resize(sigSize);
db->endIterRows();
} }
uint256 trID; uint256 trID;

View File

@@ -2,13 +2,11 @@
#include "Application.h" #include "Application.h"
#include "Conversion.h" #include "Conversion.h"
using namespace std; void UniqueNodeList::addNode(uint160& hanko, std::vector<unsigned char>& publicKey)
void UniqueNodeList::addNode(uint160& hanko, vector<unsigned char>& publicKey)
{ {
Database* db=theApp->getDB(); Database* db=theApp->getNetNodeDB()->getDB();
string sql="INSERT INTO UNL (Hanko,PubKey) values ("; std::string sql="INSERT INTO UNL (Hanko,PubKey) values (";
string hashStr; std::string hashStr;
db->escape(hanko.begin(), hanko.GetSerializeSize(), hashStr); db->escape(hanko.begin(), hanko.GetSerializeSize(), hashStr);
sql.append(hashStr); sql.append(hashStr);
sql.append(","); sql.append(",");
@@ -16,19 +14,19 @@ void UniqueNodeList::addNode(uint160& hanko, vector<unsigned char>& publicKey)
sql.append(hashStr); sql.append(hashStr);
sql.append(")"); sql.append(")");
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getNetNodeDB()->getDBLock());
db->executeSQL(sql.c_str()); db->executeSQL(sql.c_str());
} }
void UniqueNodeList::removeNode(uint160& hanko) void UniqueNodeList::removeNode(uint160& hanko)
{ {
Database* db=theApp->getDB(); Database* db=theApp->getNetNodeDB()->getDB();
string sql="DELETE FROM UNL where hanko="; std::string sql="DELETE FROM UNL where hanko=";
string hashStr; std::string hashStr;
db->escape(hanko.begin(), hanko.GetSerializeSize(), hashStr); db->escape(hanko.begin(), hanko.GetSerializeSize(), hashStr);
sql.append(hashStr); sql.append(hashStr);
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getNetNodeDB()->getDBLock());
db->executeSQL(sql.c_str()); db->executeSQL(sql.c_str());
} }
@@ -36,13 +34,13 @@ void UniqueNodeList::removeNode(uint160& hanko)
#if 0 #if 0
int UniqueNodeList::checkValid(newcoin::Validation& valid) int UniqueNodeList::checkValid(newcoin::Validation& valid)
{ {
Database* db=theApp->getDB(); Database* db=theApp->getNetNodeDB()->getDB();
string sql="SELECT pubkey from UNL where hanko="; std::string sql="SELECT pubkey from UNL where hanko=";
string hashStr; std::string hashStr;
db->escape((unsigned char*) &(valid.hanko()[0]),valid.hanko().size(),hashStr); db->escape((unsigned char*) &(valid.hanko()[0]),valid.hanko().size(),hashStr);
sql.append(hashStr); sql.append(hashStr);
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getNetNodeDB()->getDBLock());
if( db->executeSQL(sql.c_str()) ) if( db->executeSQL(sql.c_str()) )
{ {
if(db->startIterRows() && db->getNextRow()) if(db->startIterRows() && db->getNextRow())
@@ -52,11 +50,8 @@ int UniqueNodeList::checkValid(newcoin::Validation& valid)
// TODO: check that the public key makes the correct signature of the validation // TODO: check that the public key makes the correct signature of the validation
db->endIterRows(); db->endIterRows();
return(1); return(1);
}else
{
db->endIterRows();
} }
else db->endIterRows();
} }
return(0); // not on our list return(0); // not on our list
} }
@@ -64,10 +59,10 @@ int UniqueNodeList::checkValid(newcoin::Validation& valid)
void UniqueNodeList::dumpUNL(std::string& retStr) void UniqueNodeList::dumpUNL(std::string& retStr)
{ {
Database* db=theApp->getDB(); Database* db=theApp->getNetNodeDB()->getDB();
string sql="SELECT * FROM UNL"; std::string sql="SELECT * FROM UNL";
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getNetNodeDB()->getDBLock());
if( db->executeSQL(sql.c_str()) ) if( db->executeSQL(sql.c_str()) )
{ {
db->startIterRows(); db->startIterRows();
@@ -75,7 +70,7 @@ void UniqueNodeList::dumpUNL(std::string& retStr)
{ {
uint160 hanko; uint160 hanko;
int size=db->getBinary("Hanko", hanko.begin(), hanko.GetSerializeSize()); int size=db->getBinary("Hanko", hanko.begin(), hanko.GetSerializeSize());
string tstr; std::string tstr;
u160ToHuman(hanko, tstr); u160ToHuman(hanko, tstr);
retStr.append(tstr); retStr.append(tstr);

View File

@@ -137,10 +137,10 @@ std::string LocalAccountFamily::getSQL() const
ret.append("',"); ret.append("',");
std::string esc; std::string esc;
theApp->getDB()->escape((const unsigned char *) mName.c_str(), mName.size(), esc); theApp->getWalletDB()->getDB()->escape((const unsigned char *) mName.c_str(), mName.size(), esc);
ret.append(esc); ret.append(esc);
ret.append(","); ret.append(",");
theApp->getDB()->escape((const unsigned char *) mComment.c_str(), mComment.size(), esc); theApp->getWalletDB()->getDB()->escape((const unsigned char *) mComment.c_str(), mComment.size(), esc);
ret.append(esc); ret.append(esc);
ret.append(")"); ret.append(")");
@@ -221,8 +221,8 @@ void Wallet::load()
{ {
std::string sql("SELECT * FROM LocalAcctFamilies"); std::string sql("SELECT * FROM LocalAcctFamilies");
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getWalletDB()->getDBLock());
Database *db=theApp->getDB(); Database *db=theApp->getWalletDB()->getDB();
if(!db->executeSQL(sql.c_str())) return; if(!db->executeSQL(sql.c_str())) return;
while(db->getNextRow()) while(db->getNextRow())
@@ -247,6 +247,7 @@ void Wallet::load()
} }
else assert(false); else assert(false);
} }
db->endIterRows();
} }
std::string Wallet::getPubKeyHex(const uint160& famBase) std::string Wallet::getPubKeyHex(const uint160& famBase)