From f17f4dc3054a0d8edb08185e3fc919e73631ccd7 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 20 Jun 2012 02:54:19 -0700 Subject: [PATCH] Halve no-DB startup time. --- src/Application.cpp | 19 ++++++++++++++----- src/DBInit.cpp | 23 +++++++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index 4300f0bf4b..05ea8e446d 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -1,6 +1,9 @@ #include +#include +#include + #include "../database/SqliteDatabase.h" #include "Application.h" @@ -52,6 +55,11 @@ void Application::stop() std::cerr << "Stopped: " << mIOService.stopped() << std::endl; } +static void InitDB(DatabaseCon** dbCon, const char *fileName, const char *dbInit[], int dbCount) +{ + *dbCon = new DatabaseCon(fileName, dbInit, dbCount); +} + void Application::run() { assert(mTxnDB == NULL); @@ -61,11 +69,12 @@ void Application::run() // // Construct databases. // - mTxnDB = new DatabaseCon("transaction.db", TxnDBInit, TxnDBCount); - mLedgerDB = new DatabaseCon("ledger.db", LedgerDBInit, LedgerDBCount); - mWalletDB = new DatabaseCon("wallet.db", WalletDBInit, WalletDBCount); - mHashNodeDB = new DatabaseCon("hashnode.db", HashNodeDBInit, HashNodeDBCount); - mNetNodeDB = new DatabaseCon("netnode.db", NetNodeDBInit, NetNodeDBCount); + boost::thread t1(boost::bind(&InitDB, &mTxnDB, "transaction.db", TxnDBInit, TxnDBCount)); + boost::thread t2(boost::bind(&InitDB, &mLedgerDB, "ledger.db", LedgerDBInit, LedgerDBCount)); + boost::thread t3(boost::bind(&InitDB, &mWalletDB, "wallet.db", WalletDBInit, WalletDBCount)); + boost::thread t4(boost::bind(&InitDB, &mHashNodeDB, "hashnode.db", HashNodeDBInit, HashNodeDBCount)); + boost::thread t5(boost::bind(&InitDB, &mNetNodeDB, "netnode.db", NetNodeDBInit, NetNodeDBCount)); + t1.join(); t2.join(); t3.join(); t4.join(); t5.join(); // // Begin validation and ip maintenance. diff --git a/src/DBInit.cpp b/src/DBInit.cpp index 8c6931e3b6..083d8155d3 100644 --- a/src/DBInit.cpp +++ b/src/DBInit.cpp @@ -2,6 +2,8 @@ // Transaction database holds transactions and public keys const char *TxnDBInit[] = { + "BEGIN TRANSACTION;", + "CREATE TABLE Transactions ( \ TransID CHARACTER(64) PRIMARY KEY, \ TransType CHARACTER(24), \ @@ -21,13 +23,17 @@ const char *TxnDBInit[] = { LedgerSeq BIGINT UNSIGNED \ );", "CREATE INDEX AcctTxindex ON \ - AccountTransactions(Account, LedgerSeq, TransID);" + AccountTransactions(Account, LedgerSeq, TransID);", + + "END TRANSACTION;" }; int TxnDBCount = sizeof(TxnDBInit) / sizeof(const char *); // Ledger database holds ledgers and ledger confirmations const char *LedgerDBInit[] = { + "BEGIN TRANSACTION;", + "CREATE TABLE Ledgers ( \ LedgerHash CHARACTER(64) PRIMARY KEY, \ LedgerSeq BIGINT UNSIGNED, \ @@ -46,8 +52,9 @@ const char *LedgerDBInit[] = { Signature BLOB \ );", "CREATE INDEX LedgerConfByHash ON \ - LedgerConfirmations(LedgerHash)" + LedgerConfirmations(LedgerHash)", #endif + "END TRANSACTION;" }; int LedgerDBCount = sizeof(LedgerDBInit) / sizeof(const char *); @@ -55,6 +62,8 @@ int LedgerDBCount = sizeof(LedgerDBInit) / sizeof(const char *); // Wallet database holds local accounts and trusted nodes const char *WalletDBInit[] = { // Node identity must be persisted for CAS routing and responsibilities. + "BEGIN TRANSACTION;", + "CREATE TABLE NodeIdentity ( \ PublicKey CHARACTER(53), \ PrivateKey CHARACTER(52), \ @@ -221,13 +230,17 @@ const char *WalletDBInit[] = { );", "CREATE INDEX PeerScanIndex ON \ - PeerIps(ScanNext);" + PeerIps(ScanNext);", + + "END TRANSACTION;" }; int WalletDBCount = sizeof(WalletDBInit) / sizeof(const char *); // Hash node database holds nodes indexed by hash const char *HashNodeDBInit[] = { + "BEGIN TRANSACTION;", + "CREATE TABLE CommittedObjects ( \ Hash CHARACTER(64) PRIMARY KEY, \ ObjType CHAR(1) NOT NULL, \ @@ -236,7 +249,9 @@ const char *HashNodeDBInit[] = { );", "CREATE INDEX ObjectLocate ON \ - CommittedObjects(LedgerIndex, ObjType);" + CommittedObjects(LedgerIndex, ObjType);", + + "END TRANSACTION;" }; int HashNodeDBCount = sizeof(HashNodeDBInit) / sizeof(const char *);