diff options
Diffstat (limited to 'src/db.cpp')
-rw-r--r-- | src/db.cpp | 144 |
1 files changed, 58 insertions, 86 deletions
diff --git a/src/db.cpp b/src/db.cpp index 24206d34e1..12650e459f 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -30,7 +30,6 @@ using namespace boost; unsigned int nWalletDBUpdated; - // // CDB // @@ -94,15 +93,15 @@ bool CDBEnv::Open(const boost::filesystem::path& pathIn) dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1); dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1); int ret = dbenv.open(path.string().c_str(), - DB_CREATE | - DB_INIT_LOCK | - DB_INIT_LOG | - DB_INIT_MPOOL | - DB_INIT_TXN | - DB_THREAD | - DB_RECOVER | - nEnvFlags, - S_IRUSR | S_IWUSR); + DB_CREATE | + DB_INIT_LOCK | + DB_INIT_LOG | + DB_INIT_MPOOL | + DB_INIT_TXN | + DB_THREAD | + DB_RECOVER | + nEnvFlags, + S_IRUSR | S_IWUSR); if (ret != 0) return error("CDBEnv::Open : Error %d opening database environment: %s\n", ret, DbEnv::strerror(ret)); @@ -121,21 +120,21 @@ void CDBEnv::MakeMock() LogPrint("db", "CDBEnv::MakeMock\n"); dbenv.set_cachesize(1, 0, 1); - dbenv.set_lg_bsize(10485760*4); + dbenv.set_lg_bsize(10485760 * 4); dbenv.set_lg_max(10485760); dbenv.set_lk_max_locks(10000); dbenv.set_lk_max_objects(10000); dbenv.set_flags(DB_AUTO_COMMIT, 1); dbenv.log_set_config(DB_LOG_IN_MEMORY, 1); int ret = dbenv.open(NULL, - DB_CREATE | - DB_INIT_LOCK | - DB_INIT_LOG | - DB_INIT_MPOOL | - DB_INIT_TXN | - DB_THREAD | - DB_PRIVATE, - S_IRUSR | S_IWUSR); + DB_CREATE | + DB_INIT_LOCK | + DB_INIT_LOG | + DB_INIT_MPOOL | + DB_INIT_TXN | + DB_THREAD | + DB_PRIVATE, + S_IRUSR | S_IWUSR); if (ret > 0) throw runtime_error(strprintf("CDBEnv::MakeMock : Error %d opening database environment.", ret)); @@ -160,30 +159,27 @@ CDBEnv::VerifyResult CDBEnv::Verify(std::string strFile, bool (*recoverFunc)(CDB return (fRecovered ? RECOVER_OK : RECOVER_FAIL); } -bool CDBEnv::Salvage(std::string strFile, bool fAggressive, - std::vector<CDBEnv::KeyValPair >& vResult) +bool CDBEnv::Salvage(std::string strFile, bool fAggressive, std::vector<CDBEnv::KeyValPair>& vResult) { LOCK(cs_db); assert(mapFileUseCount.count(strFile) == 0); u_int32_t flags = DB_SALVAGE; - if (fAggressive) flags |= DB_AGGRESSIVE; + if (fAggressive) + flags |= DB_AGGRESSIVE; stringstream strDump; Db db(&dbenv, 0); int result = db.verify(strFile.c_str(), NULL, &strDump, flags); - if (result == DB_VERIFY_BAD) - { + if (result == DB_VERIFY_BAD) { LogPrintf("CDBEnv::Salvage : Database salvage found errors, all data may not be recoverable.\n"); - if (!fAggressive) - { + if (!fAggressive) { LogPrintf("CDBEnv::Salvage : Rerun with aggressive mode to ignore errors and continue.\n"); return false; } } - if (result != 0 && result != DB_VERIFY_BAD) - { + if (result != 0 && result != DB_VERIFY_BAD) { LogPrintf("CDBEnv::Salvage : Database salvage failed with result %d.\n", result); return false; } @@ -201,13 +197,11 @@ bool CDBEnv::Salvage(std::string strFile, bool fAggressive, getline(strDump, strLine); // Skip past header std::string keyHex, valueHex; - while (!strDump.eof() && keyHex != "DATA=END") - { + while (!strDump.eof() && keyHex != "DATA=END") { getline(strDump, keyHex); - if (keyHex != "DATA_END") - { + if (keyHex != "DATA_END") { getline(strDump, valueHex); - vResult.push_back(make_pair(ParseHex(keyHex),ParseHex(valueHex))); + vResult.push_back(make_pair(ParseHex(keyHex), ParseHex(valueHex))); } } @@ -224,8 +218,7 @@ void CDBEnv::CheckpointLSN(const std::string& strFile) } -CDB::CDB(const std::string& strFilename, const char* pszMode) : - pdb(NULL), activeTxn(NULL) +CDB::CDB(const std::string& strFilename, const char* pszMode) : pdb(NULL), activeTxn(NULL) { int ret; fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w')); @@ -245,28 +238,25 @@ CDB::CDB(const std::string& strFilename, const char* pszMode) : strFile = strFilename; ++bitdb.mapFileUseCount[strFile]; pdb = bitdb.mapDb[strFile]; - if (pdb == NULL) - { + if (pdb == NULL) { pdb = new Db(&bitdb.dbenv, 0); bool fMockDb = bitdb.IsMock(); - if (fMockDb) - { - DbMpoolFile*mpf = pdb->get_mpf(); + if (fMockDb) { + DbMpoolFile* mpf = pdb->get_mpf(); ret = mpf->set_flags(DB_MPOOL_NOFILE, 1); if (ret != 0) throw runtime_error(strprintf("CDB : Failed to configure for no temp file backing for database %s", strFile)); } - ret = pdb->open(NULL, // Txn pointer - fMockDb ? NULL : strFile.c_str(), // Filename + ret = pdb->open(NULL, // Txn pointer + fMockDb ? NULL : strFile.c_str(), // Filename fMockDb ? strFile.c_str() : "main", // Logical db name - DB_BTREE, // Database type - nFlags, // Flags + DB_BTREE, // Database type + nFlags, // Flags 0); - if (ret != 0) - { + if (ret != 0) { delete pdb; pdb = NULL; --bitdb.mapFileUseCount[strFile]; @@ -274,8 +264,7 @@ CDB::CDB(const std::string& strFilename, const char* pszMode) : throw runtime_error(strprintf("CDB : Error %d, can't open database %s", ret, strFile)); } - if (fCreate && !Exists(string("version"))) - { + if (fCreate && !Exists(string("version"))) { bool fTmp = fReadOnly; fReadOnly = false; WriteVersion(CLIENT_VERSION); @@ -297,7 +286,7 @@ void CDB::Flush() if (fReadOnly) nMinutes = 1; - bitdb.dbenv.txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100)*1024 : 0, nMinutes, 0); + bitdb.dbenv.txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0); } void CDB::Close() @@ -321,8 +310,7 @@ void CDBEnv::CloseDb(const string& strFile) { { LOCK(cs_db); - if (mapDb[strFile] != NULL) - { + if (mapDb[strFile] != NULL) { // Close the database handle Db* pdb = mapDb[strFile]; pdb->close(0); @@ -343,12 +331,10 @@ bool CDBEnv::RemoveDb(const string& strFile) bool CDB::Rewrite(const string& strFile, const char* pszSkip) { - while (true) - { + while (true) { { LOCK(bitdb.cs_db); - if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0) - { + if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0) { // Flush log data to the dat file bitdb.CloseDb(strFile); bitdb.CheckpointLSN(strFile); @@ -361,32 +347,27 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) CDB db(strFile.c_str(), "r"); Db* pdbCopy = new Db(&bitdb.dbenv, 0); - int ret = pdbCopy->open(NULL, // Txn pointer - strFileRes.c_str(), // Filename - "main", // Logical db name - DB_BTREE, // Database type - DB_CREATE, // Flags + int ret = pdbCopy->open(NULL, // Txn pointer + strFileRes.c_str(), // Filename + "main", // Logical db name + DB_BTREE, // Database type + DB_CREATE, // Flags 0); - if (ret > 0) - { + if (ret > 0) { LogPrintf("CDB::Rewrite : Can't create database file %s\n", strFileRes); fSuccess = false; } Dbc* pcursor = db.GetCursor(); if (pcursor) - while (fSuccess) - { + while (fSuccess) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT); - if (ret == DB_NOTFOUND) - { + if (ret == DB_NOTFOUND) { pcursor->close(); break; - } - else if (ret != 0) - { + } else if (ret != 0) { pcursor->close(); fSuccess = false; break; @@ -394,8 +375,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) if (pszSkip && strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0) continue; - if (strncmp(&ssKey[0], "\x07version", 8) == 0) - { + if (strncmp(&ssKey[0], "\x07version", 8) == 0) { // Update version: ssValue.clear(); ssValue << CLIENT_VERSION; @@ -406,8 +386,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) if (ret2 > 0) fSuccess = false; } - if (fSuccess) - { + if (fSuccess) { db.Close(); bitdb.CloseDb(strFile); if (pdbCopy->close(0)) @@ -415,8 +394,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) delete pdbCopy; } } - if (fSuccess) - { + if (fSuccess) { Db dbA(&bitdb.dbenv, 0); if (dbA.remove(strFile.c_str(), NULL, 0)) fSuccess = false; @@ -445,13 +423,11 @@ void CDBEnv::Flush(bool fShutdown) { LOCK(cs_db); map<string, int>::iterator mi = mapFileUseCount.begin(); - while (mi != mapFileUseCount.end()) - { + while (mi != mapFileUseCount.end()) { string strFile = (*mi).first; int nRefCount = (*mi).second; LogPrint("db", "CDBEnv::Flush : Flushing %s (refcount = %d)...\n", strFile, nRefCount); - if (nRefCount == 0) - { + if (nRefCount == 0) { // Move log data to the dat file CloseDb(strFile); LogPrint("db", "CDBEnv::Flush : %s checkpoint\n", strFile); @@ -461,16 +437,13 @@ void CDBEnv::Flush(bool fShutdown) dbenv.lsn_reset(strFile.c_str(), 0); LogPrint("db", "CDBEnv::Flush : %s closed\n", strFile); mapFileUseCount.erase(mi++); - } - else + } else mi++; } LogPrint("db", "CDBEnv::Flush : Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart); - if (fShutdown) - { + if (fShutdown) { char** listp; - if (mapFileUseCount.empty()) - { + if (mapFileUseCount.empty()) { dbenv.log_archive(&listp, DB_ARCH_REMOVE); Close(); if (!fMockDb) @@ -479,4 +452,3 @@ void CDBEnv::Flush(bool fShutdown) } } } - |