changeset 2635:c8269e1811c5 draft

Further CDBEnv encapsulation work.
author Jeff Garzik <jgarzik@exmulti.com>
date Fri, 18 May 2012 02:49:50 -0400 (2012-05-18)
parents a558beaa5c57
children 967a37af0b50
files src/db.cpp src/db.h src/walletdb.cpp
diffstat 3 files changed, 24 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/db.cpp
+++ b/src/db.cpp
@@ -27,8 +27,6 @@
 //
 
 CDBEnv bitdb;
-map<string, int> mapFileUseCount;
-static map<string, Db*> mapDb;
 
 void CDBEnv::EnvShutdown()
 {
@@ -129,8 +127,8 @@
             throw runtime_error("env open failed");
 
         strFile = pszFile;
-        ++mapFileUseCount[strFile];
-        pdb = mapDb[strFile];
+        ++bitdb.mapFileUseCount[strFile];
+        pdb = bitdb.mapDb[strFile];
         if (pdb == NULL)
         {
             pdb = new Db(&bitdb.dbenv, 0);
@@ -148,7 +146,7 @@
                 pdb = NULL;
                 {
                      LOCK(bitdb.cs_db);
-                    --mapFileUseCount[strFile];
+                    --bitdb.mapFileUseCount[strFile];
                 }
                 strFile = "";
                 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
@@ -162,7 +160,7 @@
                 fReadOnly = fTmp;
             }
 
-            mapDb[strFile] = pdb;
+            bitdb.mapDb[strFile] = pdb;
         }
     }
 }
@@ -189,14 +187,14 @@
 
     {
         LOCK(bitdb.cs_db);
-        --mapFileUseCount[strFile];
+        --bitdb.mapFileUseCount[strFile];
     }
 }
 
-void CloseDb(const string& strFile)
+void CDBEnv::CloseDb(const string& strFile)
 {
     {
-        LOCK(bitdb.cs_db);
+        LOCK(cs_db);
         if (mapDb[strFile] != NULL)
         {
             // Close the database handle
@@ -214,12 +212,12 @@
     {
         {
             LOCK(bitdb.cs_db);
-            if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
+            if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0)
             {
                 // Flush log data to the dat file
-                CloseDb(strFile);
+                bitdb.CloseDb(strFile);
                 bitdb.CheckpointLSN(strFile);
-                mapFileUseCount.erase(strFile);
+                bitdb.mapFileUseCount.erase(strFile);
 
                 bool fSuccess = true;
                 printf("Rewriting %s...\n", strFile.c_str());
@@ -276,7 +274,7 @@
                     if (fSuccess)
                     {
                         db.Close();
-                        CloseDb(strFile);
+                        bitdb.CloseDb(strFile);
                         if (pdbCopy->close(0))
                             fSuccess = false;
                         delete pdbCopy;
--- a/src/db.h
+++ b/src/db.h
@@ -42,6 +42,8 @@
 public:
     mutable CCriticalSection cs_db;
     DbEnv dbenv;
+    std::map<std::string, int> mapFileUseCount;
+    std::map<std::string, Db*> mapDb;
 
     CDBEnv();
     ~CDBEnv();
@@ -51,6 +53,8 @@
     void CheckpointLSN(std::string strFile);
     void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
 
+    void CloseDb(const std::string& strFile);
+
     DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
     {
         DbTxn* ptxn = NULL;
--- a/src/walletdb.cpp
+++ b/src/walletdb.cpp
@@ -13,9 +13,6 @@
 
 static uint64 nAccountingEntryNumber = 0;
 
-extern map<string, int> mapFileUseCount;
-extern void CloseDb(const string& strFile);
-
 //
 // CWalletDB
 //
@@ -354,8 +351,8 @@
             {
                 // Don't do this if any databases are in use
                 int nRefCount = 0;
-                map<string, int>::iterator mi = mapFileUseCount.begin();
-                while (mi != mapFileUseCount.end())
+                map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
+                while (mi != bitdb.mapFileUseCount.end())
                 {
                     nRefCount += (*mi).second;
                     mi++;
@@ -363,18 +360,18 @@
 
                 if (nRefCount == 0 && !fShutdown)
                 {
-                    map<string, int>::iterator mi = mapFileUseCount.find(strFile);
-                    if (mi != mapFileUseCount.end())
+                    map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
+                    if (mi != bitdb.mapFileUseCount.end())
                     {
                         printf("Flushing wallet.dat\n");
                         nLastFlushed = nWalletDBUpdated;
                         int64 nStart = GetTimeMillis();
 
                         // Flush wallet.dat so it's self contained
-                        CloseDb(strFile);
+                        bitdb.CloseDb(strFile);
                         bitdb.CheckpointLSN(strFile);
 
-                        mapFileUseCount.erase(mi++);
+                        bitdb.mapFileUseCount.erase(mi++);
                         printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
                     }
                 }
@@ -391,12 +388,12 @@
     {
         {
             LOCK(bitdb.cs_db);
-            if (!mapFileUseCount.count(wallet.strWalletFile) || mapFileUseCount[wallet.strWalletFile] == 0)
+            if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
             {
                 // Flush log data to the dat file
-                CloseDb(wallet.strWalletFile);
+                bitdb.CloseDb(wallet.strWalletFile);
                 bitdb.CheckpointLSN(wallet.strWalletFile);
-                mapFileUseCount.erase(wallet.strWalletFile);
+                bitdb.mapFileUseCount.erase(wallet.strWalletFile);
 
                 // Copy wallet.dat
                 filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;