aboutsummaryrefslogtreecommitdiff
path: root/db.cpp
diff options
context:
space:
mode:
authors_nakamoto <s_nakamoto@1a98c847-1fd6-4fd8-948a-caf3550aa51b>2009-10-29 20:10:46 +0000
committers_nakamoto <s_nakamoto@1a98c847-1fd6-4fd8-948a-caf3550aa51b>2009-10-29 20:10:46 +0000
commit7be46ce487e2f3c6e145b9bc56cc37d8de97df9e (patch)
tree31faa0f74ff1541946f6d0d5d8764be8e2564a66 /db.cpp
parentfc0e97a70ee295da44096fa590a1f7674b936590 (diff)
downloadbitcoin-7be46ce487e2f3c6e145b9bc56cc37d8de97df9e.tar.xz
better wallet.dat flush, consolidated QueryPerformanceCounter, PRI64d printf portability
Diffstat (limited to 'db.cpp')
-rw-r--r--db.cpp76
1 files changed, 53 insertions, 23 deletions
diff --git a/db.cpp b/db.cpp
index 315e93b73a..699a94f2ea 100644
--- a/db.cpp
+++ b/db.cpp
@@ -4,8 +4,11 @@
#include "headers.h"
+void ThreadFlushWalletDB(void* parg);
+unsigned int nWalletDBUpdated;
+
@@ -56,6 +59,8 @@ CDB::CDB(const char* pszFile, const char* pszMode, bool fTxn) : pdb(NULL)
{
if (!fDbEnvInit)
{
+ if (fShutdown)
+ return;
string strAppDir = GetAppDir();
string strLogDir = strAppDir + "\\database";
_mkdir(strLogDir.c_str());
@@ -121,12 +126,10 @@ void CDB::Close()
pdb->close(0);
delete pdb;
pdb = NULL;
+ dbenv.txn_checkpoint(0, 0, 0);
CRITICAL_BLOCK(cs_db)
- {
- dbenv.txn_checkpoint(0, 0, 0);
--mapFileUseCount[strFile];
- }
RandAddSeed();
}
@@ -499,25 +502,6 @@ bool CReviewDB::WriteReviews(uint256 hash, const vector<CReview>& vReviews)
// CWalletDB
//
-CWalletDB::~CWalletDB()
-{
- // Flush whenever all handles to wallet.dat are closed
- CRITICAL_BLOCK(cs_db)
- {
- Close(); // close includes a txn_checkpoint
- map<string, int>::iterator mi = mapFileUseCount.find(strFile);
- if (mi != mapFileUseCount.end())
- {
- int nRefCount = (*mi).second;
- if (nRefCount == 0)
- {
- dbenv.lsn_reset(strFile.c_str(), 0);
- mapFileUseCount.erase(mi++);
- }
- }
- }
-}
-
bool CWalletDB::LoadWallet(vector<unsigned char>& vchDefaultKeyRet)
{
vchDefaultKeyRet.clear();
@@ -610,7 +594,7 @@ bool CWalletDB::LoadWallet(vector<unsigned char>& vchDefaultKeyRet)
printf("fShowGenerated = %d\n", fShowGenerated);
printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
- printf("nTransactionFee = %I64d\n", nTransactionFee);
+ printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
printf("fMinimizeToTray = %d\n", fMinimizeToTray);
printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
@@ -655,5 +639,51 @@ bool LoadWallet(bool& fFirstRunRet)
CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
}
+ _beginthread(ThreadFlushWalletDB, 0, NULL);
return true;
}
+
+void ThreadFlushWalletDB(void* parg)
+{
+ static bool fOneThread;
+ if (fOneThread)
+ return;
+ fOneThread = true;
+
+ unsigned int nLastSeen = nWalletDBUpdated;
+ unsigned int nLastFlushed = nWalletDBUpdated;
+ int64 nLastWalletUpdate = GetTime();
+ while (!fShutdown)
+ {
+ Sleep(500);
+
+ if (nLastSeen != nWalletDBUpdated)
+ {
+ nLastSeen = nWalletDBUpdated;
+ nLastWalletUpdate = GetTime();
+ }
+
+ if (nLastFlushed != nWalletDBUpdated && nLastWalletUpdate < GetTime() - 1)
+ {
+ TRY_CRITICAL_BLOCK(cs_db)
+ {
+ string strFile = "wallet.dat";
+ map<string, int>::iterator mi = mapFileUseCount.find(strFile);
+ if (mi != mapFileUseCount.end())
+ {
+ int nRefCount = (*mi).second;
+ if (nRefCount == 0 && !fShutdown)
+ {
+ // Flush wallet.dat so it's self contained
+ nLastFlushed == nWalletDBUpdated;
+ int64 nStart = PerformanceCounter();
+ dbenv.txn_checkpoint(0, 0, 0);
+ dbenv.lsn_reset(strFile.c_str(), 0);
+ printf("Flushed wallet.dat %15"PRI64d"\n", PerformanceCounter() - nStart);
+ mapFileUseCount.erase(mi++);
+ }
+ }
+ }
+ }
+ }
+}