diff --git a/Application.cpp b/Application.cpp index ad4890d42c..5ef48be03d 100644 --- a/Application.cpp +++ b/Application.cpp @@ -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(); - //mUNL.load(); - mPeerDoor=NULL; - mRPCDoor=NULL; - mDatabase=NULL; - uint160 rootFamily=mWallet.addFamily("This is my payphrase.", true); LocalAccount::pointer rootAccount=mWallet.getLocalAccount(rootFamily, 0); @@ -53,9 +63,11 @@ Application::Application() void Application::run() { - std::string filename=strprintf("%sdata.db",theConfig.DATA_DIR.c_str()); - theApp->setDB(new SqliteDatabase(filename.c_str())); - mDatabase->connect(); + mTxnDB=new DatabaseCon("transaction.db"); + mLedgerDB=new DatabaseCon("ledger.db"); + mWalletDB=new DatabaseCon("wallet.db"); + mHashNodeDB=new DatabaseCon("hashnode.db"); + mNetNodeDB=new DatabaseCon("netnode.db"); if(theConfig.PEER_PORT) { diff --git a/Application.h b/Application.h index 5a14abe5d5..ca0c945a64 100644 --- a/Application.h +++ b/Application.h @@ -18,6 +18,19 @@ class RPCDoor; 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 { NetworkOPs mNetOps; @@ -28,7 +41,8 @@ class Application KnownNodeList mKnownNodes; PubKeyCache mPKCache; LedgerMaster mMasterLedger; - Database* mDatabase; + + DatabaseCon *mTxnDB, *mLedgerDB, *mWalletDB, *mHashNodeDB, *mNetNodeDB; ConnectionPool mConnectionPool; PeerDoor* mPeerDoor; @@ -38,7 +52,6 @@ class Application boost::recursive_mutex mPeerMapLock; boost::asio::io_service mIOService; - boost::recursive_mutex dbLock; public: @@ -57,9 +70,11 @@ public: LedgerMaster& getMasterLedger() { return mMasterLedger; } - ScopedLock getDBLock() { return ScopedLock(dbLock); } - void setDB(Database* db) { mDatabase=db; } - Database* getDB() { return(mDatabase); } + DatabaseCon* getTxnDB() { return mTxnDB; } + DatabaseCon* getLedgerDB() { return mLedgerDB; } + DatabaseCon* getWalletDB() { return mWalletDB; } + DatabaseCon* getHashNodeDB() { return mHashNodeDB; } + DatabaseCon* getNetNodeDB() { return mNetNodeDB; } //Serializer* getSerializer(){ return(mSerializer); } //void setSerializer(Serializer* ser){ mSerializer=ser; } diff --git a/HashedObject.cpp b/HashedObject.cpp index 41a66b0278..c620d7eadc 100644 --- a/HashedObject.cpp +++ b/HashedObject.cpp @@ -57,12 +57,12 @@ bool HashedObject::store(HashedObjectType type, uint32 index, const std::vector< sql.append("',"); 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(");"); - ScopedLock sl(theApp->getDBLock()); - Database* db=theApp->getDB(); + ScopedLock sl(theApp->getHashNodeDB()->getDBLock()); + Database* db=theApp->getHashNodeDB()->getDB(); return db->executeSQL(sql.c_str()); } @@ -86,8 +86,8 @@ HashedObject::pointer HashedObject::retrieve(const uint256& hash) data.reserve(8192); if(1) { - ScopedLock sl(theApp->getDBLock()); - Database* db=theApp->getDB(); + ScopedLock sl(theApp->getHashNodeDB()->getDBLock()); + Database* db=theApp->getHashNodeDB()->getDB(); if(!db->executeSQL(sql.c_str())) 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); data.resize(size); db->getBinary("Object", &(data.front()), size); + db->endIterRows(); } HashedObjectType htype=UNKNOWN; diff --git a/Ledger.cpp b/Ledger.cpp index 54e77f2082..9572eec9fc 100644 --- a/Ledger.cpp +++ b/Ledger.cpp @@ -321,8 +321,8 @@ void Ledger::saveAcceptedLedger(Ledger::pointer ledger) sql.append(ledger->mTransHash.GetHex()); sql.append("');"); - ScopedLock sl(theApp->getDBLock()); - theApp->getDB()->executeSQL(sql.c_str()); + ScopedLock sl(theApp->getLedgerDB()->getDBLock()); + theApp->getLedgerDB()->getDB()->executeSQL(sql.c_str()); // write out dirty nodes while(ledger->mTransactionMap->flushDirty(64, TRANSACTION_NODE, ledger->mLedgerSeq)) @@ -341,8 +341,8 @@ Ledger::pointer Ledger::getSQL(const std::string& sql) if(1) { - ScopedLock sl(theApp->getDBLock()); - Database *db=theApp->getDB(); + ScopedLock sl(theApp->getLedgerDB()->getDBLock()); + Database *db=theApp->getLedgerDB()->getDB(); if(!db->executeSQL(sql.c_str()) || !db->getNextRow()) return Ledger::pointer(); @@ -357,6 +357,7 @@ Ledger::pointer Ledger::getSQL(const std::string& sql) feeHeld=db->getBigInt("FeeHeld"); closingTime=db->getBigInt("ClosingTime"); ledgerSeq=db->getBigInt("LedgerSeq"); + db->endIterRows(); } Ledger::pointer ret(new Ledger(prevHash, transHash, accountHash, feeHeld, closingTime, ledgerSeq)); diff --git a/PubKeyCache.cpp b/PubKeyCache.cpp index 130257a256..61ec3ce2fa 100644 --- a/PubKeyCache.cpp +++ b/PubKeyCache.cpp @@ -21,11 +21,12 @@ CKey::pointer PubKeyCache::locate(const uint160& id) int pkSize; if(1) { // is it in the database - ScopedLock sl(theApp->getDBLock()); - Database* db=theApp->getDB(); + ScopedLock sl(theApp->getTxnDB()->getDBLock()); + Database* db=theApp->getTxnDB()->getDB(); if(!db->executeSQL(sql.c_str())) return CKey::pointer(); if(!db->getNextRow()) return CKey::pointer(); pkSize=db->getBinary("PubKey", &(data.front()), data.size()); + db->endIterRows(); } data.resize(pkSize); CKey::pointer ckp(new CKey()); @@ -58,11 +59,11 @@ CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key) std::vector pk=key->GetPubKey(); std::string encodedPK; - theApp->getDB()->escape(&(pk.front()), pk.size(), encodedPK); + theApp->getTxnDB()->getDB()->escape(&(pk.front()), pk.size(), encodedPK); sql+=encodedPK; sql.append(");"); - ScopedLock dbl(theApp->getDBLock()); - theApp->getDB()->executeSQL(sql.c_str()); + ScopedLock dbl(theApp->getTxnDB()->getDBLock()); + theApp->getTxnDB()->getDB()->executeSQL(sql.c_str()); return key; } diff --git a/RPC.h b/RPC.h index 051bd70975..ccfdf9650c 100644 --- a/RPC.h +++ b/RPC.h @@ -27,7 +27,7 @@ extern std::string JSONRPCRequest(const std::string& strMethod, const Json::Valu const Json::Value& id); extern std::string createHTTPPost(const std::string& strMsg, - const std::map& mapRequestHeaders); + const std::map& mapRequestHeaders); extern int ReadHTTP(std::basic_istream& stream, std::map& mapHeadersRet, std::string& strMessageRet); diff --git a/Transaction.cpp b/Transaction.cpp index 1cb7a4febe..f66b9f1b98 100644 --- a/Transaction.cpp +++ b/Transaction.cpp @@ -154,12 +154,12 @@ bool Transaction::save() const default: sql.append("','U',"); break; } 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(");"); - ScopedLock sl(theApp->getDBLock()); - Database* db=theApp->getDB(); + ScopedLock sl(theApp->getTxnDB()->getDBLock()); + Database* db=theApp->getTxnDB()->getDB(); return db->executeSQL(sql.c_str()); } @@ -172,8 +172,8 @@ Transaction::pointer Transaction::transactionFromSQL(const std::string& sql) signature.reserve(78); if(1) { - ScopedLock sl(theApp->getDBLock()); - Database* db=theApp->getDB(); + ScopedLock sl(theApp->getTxnDB()->getDBLock()); + Database* db=theApp->getTxnDB()->getDB(); if(!db->executeSQL(sql.c_str())) 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); int sigSize=db->getBinary("Signature", &(signature.front()), signature.size()); signature.resize(sigSize); + db->endIterRows(); } uint256 trID; diff --git a/UniqueNodeList.cpp b/UniqueNodeList.cpp index 77500afb60..3f13b85211 100644 --- a/UniqueNodeList.cpp +++ b/UniqueNodeList.cpp @@ -2,13 +2,11 @@ #include "Application.h" #include "Conversion.h" -using namespace std; - -void UniqueNodeList::addNode(uint160& hanko, vector& publicKey) +void UniqueNodeList::addNode(uint160& hanko, std::vector& publicKey) { - Database* db=theApp->getDB(); - string sql="INSERT INTO UNL (Hanko,PubKey) values ("; - string hashStr; + Database* db=theApp->getNetNodeDB()->getDB(); + std::string sql="INSERT INTO UNL (Hanko,PubKey) values ("; + std::string hashStr; db->escape(hanko.begin(), hanko.GetSerializeSize(), hashStr); sql.append(hashStr); sql.append(","); @@ -16,19 +14,19 @@ void UniqueNodeList::addNode(uint160& hanko, vector& publicKey) sql.append(hashStr); sql.append(")"); - ScopedLock sl(theApp->getDBLock()); + ScopedLock sl(theApp->getNetNodeDB()->getDBLock()); db->executeSQL(sql.c_str()); } void UniqueNodeList::removeNode(uint160& hanko) { - Database* db=theApp->getDB(); - string sql="DELETE FROM UNL where hanko="; - string hashStr; + Database* db=theApp->getNetNodeDB()->getDB(); + std::string sql="DELETE FROM UNL where hanko="; + std::string hashStr; db->escape(hanko.begin(), hanko.GetSerializeSize(), hashStr); sql.append(hashStr); - ScopedLock sl(theApp->getDBLock()); + ScopedLock sl(theApp->getNetNodeDB()->getDBLock()); db->executeSQL(sql.c_str()); } @@ -36,13 +34,13 @@ void UniqueNodeList::removeNode(uint160& hanko) #if 0 int UniqueNodeList::checkValid(newcoin::Validation& valid) { - Database* db=theApp->getDB(); - string sql="SELECT pubkey from UNL where hanko="; - string hashStr; + Database* db=theApp->getNetNodeDB()->getDB(); + std::string sql="SELECT pubkey from UNL where hanko="; + std::string hashStr; db->escape((unsigned char*) &(valid.hanko()[0]),valid.hanko().size(),hashStr); sql.append(hashStr); - ScopedLock sl(theApp->getDBLock()); + ScopedLock sl(theApp->getNetNodeDB()->getDBLock()); if( db->executeSQL(sql.c_str()) ) { 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 db->endIterRows(); return(1); - }else - { - db->endIterRows(); - } + else db->endIterRows(); } return(0); // not on our list } @@ -64,10 +59,10 @@ int UniqueNodeList::checkValid(newcoin::Validation& valid) void UniqueNodeList::dumpUNL(std::string& retStr) { - Database* db=theApp->getDB(); - string sql="SELECT * FROM UNL"; + Database* db=theApp->getNetNodeDB()->getDB(); + std::string sql="SELECT * FROM UNL"; - ScopedLock sl(theApp->getDBLock()); + ScopedLock sl(theApp->getNetNodeDB()->getDBLock()); if( db->executeSQL(sql.c_str()) ) { db->startIterRows(); @@ -75,7 +70,7 @@ void UniqueNodeList::dumpUNL(std::string& retStr) { uint160 hanko; int size=db->getBinary("Hanko", hanko.begin(), hanko.GetSerializeSize()); - string tstr; + std::string tstr; u160ToHuman(hanko, tstr); retStr.append(tstr); diff --git a/Wallet.cpp b/Wallet.cpp index bfab4536fc..5e381cfa58 100644 --- a/Wallet.cpp +++ b/Wallet.cpp @@ -137,10 +137,10 @@ std::string LocalAccountFamily::getSQL() const ret.append("',"); 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(","); - 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(")"); @@ -221,8 +221,8 @@ void Wallet::load() { std::string sql("SELECT * FROM LocalAcctFamilies"); - ScopedLock sl(theApp->getDBLock()); - Database *db=theApp->getDB(); + ScopedLock sl(theApp->getWalletDB()->getDBLock()); + Database *db=theApp->getWalletDB()->getDB(); if(!db->executeSQL(sql.c_str())) return; while(db->getNextRow()) @@ -247,6 +247,7 @@ void Wallet::load() } else assert(false); } + db->endIterRows(); } std::string Wallet::getPubKeyHex(const uint160& famBase)