X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fdb.cpp;h=5b6d66b12e296fcaf8fdab55e4aac38b5e8123d3;hb=b1a1f9e9693114bfea4118828fb08ff19ea9f006;hp=142952c59bd2724d24ffc1513c6bbcc164a9dc52;hpb=adb8391acdf671640adb1e8be564e68b254fca69;p=novacoin.git diff --git a/src/db.cpp b/src/db.cpp index 142952c..5b6d66b 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -7,7 +7,7 @@ #include "net.h" #include "util.h" #include "main.h" -#include "ui_interface.h" +#include "interface.h" #include #include @@ -20,7 +20,7 @@ using namespace boost; unsigned int nWalletDBUpdated; - +extern bool fUseMemoryLog; // @@ -39,14 +39,10 @@ void CDBEnv::EnvShutdown() if (ret != 0) printf("EnvShutdown exception: %s (%d)\n", DbEnv::strerror(ret), ret); if (!fMockDb) - DbEnv(0).remove(strPath.c_str(), 0); + DbEnv(0u).remove(strPath.c_str(), 0); } -CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS) -{ - fDbEnvInit = false; - fMockDb = false; -} +CDBEnv::CDBEnv() : fDetachDB(false), fDbEnvInit(false), fMockDb(false), dbenv(DB_CXX_NO_EXCEPTIONS) { } CDBEnv::~CDBEnv() { @@ -67,18 +63,18 @@ bool CDBEnv::Open(boost::filesystem::path pathEnv_) return false; pathEnv = pathEnv_; - filesystem::path pathDataDir = pathEnv; + boost::filesystem::path pathDataDir = pathEnv; strPath = pathDataDir.string(); - filesystem::path pathLogDir = pathDataDir / "database"; - filesystem::create_directory(pathLogDir); - filesystem::path pathErrorFile = pathDataDir / "db.log"; + boost::filesystem::path pathLogDir = pathDataDir / "database"; + boost::filesystem::create_directory(pathLogDir); + boost::filesystem::path pathErrorFile = pathDataDir / "db.log"; printf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string().c_str(), pathErrorFile.string().c_str()); unsigned int nEnvFlags = 0; if (GetBoolArg("-privdb", true)) nEnvFlags |= DB_PRIVATE; - int nDbCache = GetArg("-dbcache", 25); + int nDbCache = GetArgInt("-dbcache", 25); dbenv.set_lg_dir(pathLogDir.string().c_str()); dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024)*1048576, 1); dbenv.set_lg_bsize(1048576); @@ -111,29 +107,6 @@ bool CDBEnv::Open(boost::filesystem::path pathEnv_) fDbEnvInit = true; fMockDb = false; - // Check that the number of locks is sufficient (to prevent chain fork possibility, read http://bitcoin.org/may15 for more info) - u_int32_t nMaxLocks; - if (!dbenv.get_lk_max_locks(&nMaxLocks)) - { - int nBlocks, nDeepReorg; - std::string strMessage; - - nBlocks = nMaxLocks / 48768; - nDeepReorg = (nBlocks - 1) / 2; - - printf("Final lk_max_locks is %lu, sufficient for (worst case) %d block%s in a single transaction (up to a %d-deep reorganization)\n", (unsigned long)nMaxLocks, nBlocks, (nBlocks == 1) ? "" : "s", nDeepReorg); - if (nDeepReorg < 3) - { - if (nBlocks < 1) - strMessage = strprintf(_("Warning: DB_CONFIG has set_lk_max_locks %lu, which may be too low for a single block. If this limit is reached, NovaCoin may stop working."), (unsigned long)nMaxLocks); - else - strMessage = strprintf(_("Warning: DB_CONFIG has set_lk_max_locks %lu, which may be too low for a common blockchain reorganization. If this limit is reached, NovaCoin may stop working."), (unsigned long)nMaxLocks); - - strMiscWarning = strMessage; - printf("*** %s\n", strMessage.c_str()); - } - } - return true; } @@ -154,7 +127,7 @@ void CDBEnv::MakeMock() dbenv.set_lk_max_objects(10000); dbenv.set_flags(DB_AUTO_COMMIT, 1); #ifdef DB_LOG_IN_MEMORY - dbenv.log_set_config(DB_LOG_IN_MEMORY, 1); + dbenv.log_set_config(DB_LOG_IN_MEMORY, fUseMemoryLog ? 1 : 0); #endif int ret = dbenv.open(NULL, DB_CREATE | @@ -224,7 +197,7 @@ bool CDBEnv::Salvage(std::string strFile, bool fAggressive, 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))); @@ -252,7 +225,7 @@ CDB::CDB(const char *pszFile, const char* pszMode) : return; fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w')); - bool fCreate = strchr(pszMode, 'c'); + bool fCreate = strchr(pszMode, 'c') != NULL; unsigned int nFlags = DB_THREAD; if (fCreate) nFlags |= DB_CREATE; @@ -290,7 +263,7 @@ CDB::CDB(const char *pszFile, const char* pszMode) : delete pdb; pdb = NULL; --bitdb.mapFileUseCount[strFile]; - strFile = ""; + strFile.clear(); throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret)); } @@ -333,7 +306,7 @@ void CDB::Close() if (IsChainFile(strFile) && IsInitialBlockDownload()) nMinutes = 5; - bitdb.dbenv.txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100)*1024 : 0, nMinutes, 0); + bitdb.dbenv.txn_checkpoint(nMinutes ? GetArgUInt("-dblogsize", 100)*1024 : 0, nMinutes, 0); { LOCK(bitdb.cs_db); @@ -398,7 +371,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) } Dbc* pcursor = db.GetCursor(); - if (pcursor) + if (pcursor) { while (fSuccess) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); @@ -415,9 +388,14 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) fSuccess = false; break; } - if (pszSkip && - strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0) - continue; + + if (pszSkip != NULL) + { + size_t pszSkipLen = strlen(pszSkip); + if (strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), pszSkipLen)) == 0) + continue; + } + if (strncmp(&ssKey[0], "\x07version", 8) == 0) { // Update version: @@ -430,6 +408,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) if (ret2 > 0) fSuccess = false; } + } if (fSuccess) { db.Close(); @@ -461,7 +440,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) void CDBEnv::Flush(bool fShutdown) { - int64 nStart = GetTimeMillis(); + int64_t nStart = GetTimeMillis(); // Flush log data to the actual data file // on all files that are not in use printf("Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started"); @@ -492,7 +471,7 @@ void CDBEnv::Flush(bool fShutdown) else mi++; } - printf("DBFlush(%s)%s ended %15"PRI64d"ms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started", GetTimeMillis() - nStart); + printf("DBFlush(%s)%s ended %15" PRId64 "ms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started", GetTimeMillis() - nStart); if (fShutdown) { char** listp; @@ -541,7 +520,7 @@ bool CAddrDB::Write(const CAddrMan& addr) try { fileout << ssPeers; } - catch (std::exception &e) { + catch (const std::exception&) { return error("CAddrman::Write() : I/O error"); } FileCommit(fileout); @@ -576,7 +555,7 @@ bool CAddrDB::Read(CAddrMan& addr) filein.read((char *)&vchData[0], dataSize); filein >> hashIn; } - catch (std::exception &e) { + catch (const std::exception&) { return error("CAddrman::Read() 2 : I/O error or stream data corrupted"); } filein.fclose(); @@ -600,7 +579,7 @@ bool CAddrDB::Read(CAddrMan& addr) // de-serialize address data into one CAddrMan object ssPeers >> addr; } - catch (std::exception &e) { + catch (const std::exception&) { return error("CAddrman::Read() : I/O error or stream data corrupted"); }