From db96a938442c85ebb38768461bf9c00162aadc41 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Tue, 21 Jan 2020 17:22:19 +0200 Subject: [PATCH] Ensure database directories existence Signed-off-by: Anthony Fieroni --- src/claimtrie/trie.cpp | 6 +++--- src/claimtrie/trie.h | 1 + src/flatfile.cpp | 3 ++- src/index/base.cpp | 17 +++++++---------- src/index/blockfilterindex.cpp | 4 ++-- src/logging.cpp | 1 - src/logging.h | 1 - src/txdb.cpp | 20 ++++++-------------- src/validation.cpp | 9 +++------ src/validation.h | 5 ++--- 10 files changed, 26 insertions(+), 41 deletions(-) diff --git a/src/claimtrie/trie.cpp b/src/claimtrie/trie.cpp index a55707919..0621afd4f 100644 --- a/src/claimtrie/trie.cpp +++ b/src/claimtrie/trie.cpp @@ -44,9 +44,6 @@ void applyPragmas(sqlite::database& db, std::size_t cache) db << "PRAGMA journal_mode=WAL"; db << "PRAGMA temp_store=MEMORY"; db << "PRAGMA case_sensitive_like=true"; - - db.define("POPS", [](std::string s) -> std::string { if (!s.empty()) s.pop_back(); return s; }); - db.define("REVERSE", [](std::vector s) -> std::vector { std::reverse(s.begin(), s.end()); return s; }); } CClaimTrie::CClaimTrie(std::size_t cacheBytes, bool fWipe, int height, @@ -522,6 +519,9 @@ CClaimTrieCacheBase::CClaimTrieCacheBase(CClaimTrie* base) nNextHeight = base->nNextHeight; applyPragmas(db, base->dbCacheBytes >> 10U); // in KB + + db.define("POPS", [](std::string s) -> std::string { if (!s.empty()) s.pop_back(); return s; }); + db.define("REVERSE", [](std::vector s) -> std::vector { std::reverse(s.begin(), s.end()); return s; }); } CClaimTrieCacheBase::CClaimTrieCacheBase(CClaimTrieCacheBase&& o) diff --git a/src/claimtrie/trie.h b/src/claimtrie/trie.h index 04d25008a..dfb6b8e25 100644 --- a/src/claimtrie/trie.h +++ b/src/claimtrie/trie.h @@ -13,6 +13,7 @@ #include #include +void applyPragmas(sqlite::database& db, std::size_t cache); uint256 getValueHash(const COutPoint& outPoint, int nHeightOfLastTakeover); class CClaimTrie diff --git a/src/flatfile.cpp b/src/flatfile.cpp index 8a8f7b681..ed1f46e4c 100644 --- a/src/flatfile.cpp +++ b/src/flatfile.cpp @@ -66,7 +66,8 @@ size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_ if (CheckDiskSpace(m_dir, inc_size)) { FILE *file = Open(pos); if (file) { - LogPrintf("Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile); + // it bothers too much + // LogPrintf("Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile); AllocateFileRange(file, pos.nPos, inc_size); fclose(file); return inc_size; diff --git a/src/index/base.cpp b/src/index/base.cpp index d83a0cbf4..af95c0f20 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include #include @@ -32,13 +33,9 @@ static const sqlite::sqlite_config sharedConfig { }; BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool fMemory, bool fWipe) - : sqlite::database(fMemory ? ":memory:" : (path / "index.sqlite").string(), sharedConfig) + : sqlite::database(fMemory ? ":memory:" : (path / "db.sqlite").string(), sharedConfig) { - (*this) << "PRAGMA cache_size=-" + std::to_string(n_cache_size >> 10); // in -KB - (*this) << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit - (*this) << "PRAGMA journal_mode=MEMORY"; - (*this) << "PRAGMA temp_store=MEMORY"; - (*this) << "PRAGMA case_sensitive_like=true"; + applyPragmas(*this, n_cache_size >> 10); // in -KB (*this) << "CREATE TABLE IF NOT EXISTS locator (branch BLOB NOT NULL COLLATE BINARY);"; (*this) << "CREATE TABLE IF NOT EXISTS file_pos (file INTEGER NOT NULL, pos INTEGER NOT NULL);"; @@ -53,7 +50,7 @@ BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool fMemory, bool (*this) << "DELETE FROM file_pos"; (*this) << "DELETE FROM block"; } - (*this) << "begin"; + (*this) << "BEGIN"; } bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const @@ -207,11 +204,11 @@ void BaseIndex::ThreadSync() bool BaseIndex::Commit() { if (!CommitInternal() || sqlite::commit(GetDB()) != SQLITE_OK) { - GetDB() << "rollback"; - GetDB() << "begin"; + GetDB() << "ROLLBACK"; + GetDB() << "BEGIN"; return error("%s: Failed to commit latest %s state", __func__, GetName()); } - GetDB() << "begin"; + GetDB() << "BEGIN"; return true; } diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp index 197939185..ddc937dc7 100644 --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -50,11 +50,11 @@ BlockFilterIndex::BlockFilterIndex(BlockFilterType filter_type, const std::string& filter_name = BlockFilterTypeName(filter_type); if (filter_name.empty()) throw std::invalid_argument("unknown filter_type"); - fs::path path = GetDataDir() / "indexes" / "blockfilter" / filter_name; + fs::path path = GetBlocksDir() / "filter" / filter_name; fs::create_directories(path); m_name = filter_name + " block filter index"; - m_db = MakeUnique(path / "db", n_cache_size, f_memory, f_wipe); + m_db = MakeUnique(path, n_cache_size, f_memory, f_wipe); m_filter_fileseq = MakeUnique(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE); } diff --git a/src/logging.cpp b/src/logging.cpp index d938a16f0..27330a9a9 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -150,7 +150,6 @@ const CLogCategoryDesc LogCategories[] = {BCLog::LIBEVENT, "libevent"}, {BCLog::COINDB, "coindb"}, {BCLog::QT, "qt"}, - {BCLog::LEVELDB, "leveldb"}, {BCLog::CLAIMS, "claims"}, {BCLog::ALL, "1"}, {BCLog::ALL, "all"}, diff --git a/src/logging.h b/src/logging.h index 134b456bd..03ad12cde 100644 --- a/src/logging.h +++ b/src/logging.h @@ -54,7 +54,6 @@ namespace BCLog { LIBEVENT = (1 << 17), COINDB = (1 << 18), QT = (1 << 19), - LEVELDB = (1 << 20), CLAIMS = (1 << 30), ALL = ~(uint32_t)0, }; diff --git a/src/txdb.cpp b/src/txdb.cpp index 46a237e27..dc2f2159c 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -5,7 +5,7 @@ #include -#include +#include #include #include #include @@ -28,11 +28,7 @@ static const sqlite::sqlite_config sharedConfig { CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe) : db(fMemory ? ":memory:" : (ldb_path / "coins.sqlite").string(), sharedConfig) { - db << "PRAGMA cache_size=-" + std::to_string(nCacheSize >> 10); // in -KB - db << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit - db << "PRAGMA journal_mode=WAL"; - db << "PRAGMA temp_store=MEMORY"; - db << "PRAGMA case_sensitive_like=true"; + applyPragmas(db, nCacheSize >> 10); // in -KB db << "CREATE TABLE IF NOT EXISTS unspent (txID BLOB NOT NULL COLLATE BINARY, txN INTEGER NOT NULL, " "isCoinbase INTEGER NOT NULL, blockHeight INTEGER NOT NULL, amount INTEGER NOT NULL, " @@ -52,7 +48,7 @@ CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, b bool CCoinsViewDB::GetCoin(const COutPoint &outpoint, Coin &coin) const { auto query = db << "SELECT isCoinbase, blockHeight, amount, script FROM unspent " - "WHERE txID = ? AND txN = ?" << outpoint.hash << outpoint.n; + "WHERE txID = ? AND txN = ?" << outpoint.hash << outpoint.n; for (auto&& row: query) { uint32_t coinbase = 0, height = 0; row >> coinbase >> height >> coin.out.nValue >> coin.out.scriptPubKey; @@ -65,7 +61,7 @@ bool CCoinsViewDB::GetCoin(const COutPoint &outpoint, Coin &coin) const { bool CCoinsViewDB::HaveCoin(const COutPoint &outpoint) const { auto query = db << "SELECT 1 FROM unspent " - "WHERE txID = ? AND txN = ?" << outpoint.hash << outpoint.n; + "WHERE txID = ? AND txN = ?" << outpoint.hash << outpoint.n; return query.begin() != query.end(); } @@ -170,13 +166,9 @@ size_t CCoinsViewDB::EstimateSize() const } CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) - : db(fMemory ? ":memory:" : (GetDataDir() / "block_index.sqlite").string(), sharedConfig) + : db(fMemory ? ":memory:" : (GetBlocksDir() / "index.sqlite").string(), sharedConfig) { - db << "PRAGMA cache_size=-" + std::to_string(nCacheSize >> 10); // in -KB - db << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit - db << "PRAGMA journal_mode=WAL"; - db << "PRAGMA temp_store=MEMORY"; - db << "PRAGMA case_sensitive_like=true"; + applyPragmas(db, nCacheSize >> 10); // in -KB db << "CREATE TABLE IF NOT EXISTS block_file (" "file INTEGER NOT NULL PRIMARY KEY, " diff --git a/src/validation.cpp b/src/validation.cpp index 2909a7551..182945070 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1339,11 +1339,10 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams) } CoinsViews::CoinsViews( - std::string ldb_name, size_t cache_size_bytes, bool in_memory, bool should_wipe) : m_dbview( - GetDataDir() / ldb_name, cache_size_bytes, in_memory, should_wipe), + GetDataDir(), cache_size_bytes, in_memory, should_wipe), m_catcherview(&m_dbview) {} void CoinsViews::InitCache() @@ -1359,11 +1358,9 @@ CChainState::CChainState() : m_blockman(g_blockman) {} void CChainState::InitCoinsDB( size_t cache_size_bytes, bool in_memory, - bool should_wipe, - std::string leveldb_name) + bool should_wipe) { - m_coins_views = MakeUnique( - leveldb_name, cache_size_bytes, in_memory, should_wipe); + m_coins_views = MakeUnique(cache_size_bytes, in_memory, should_wipe); } void CChainState::InitCoinsCache() diff --git a/src/validation.h b/src/validation.h index b5898da19..7d03d5524 100644 --- a/src/validation.h +++ b/src/validation.h @@ -532,7 +532,7 @@ public: //! state to disk, which should not be done until the health of the database is verified. //! //! All arguments forwarded onto CCoinsViewDB. - CoinsViews(std::string ldb_name, size_t cache_size_bytes, bool in_memory, bool should_wipe); + CoinsViews(size_t cache_size_bytes, bool in_memory, bool should_wipe); //! Initialize the CCoinsViewCache member. void InitCache() EXCLUSIVE_LOCKS_REQUIRED(::cs_main); @@ -602,8 +602,7 @@ public: void InitCoinsDB( size_t cache_size_bytes, bool in_memory, - bool should_wipe, - std::string leveldb_name = "chainstate"); + bool should_wipe); //! Initialize the in-memory coins cache (to be done after the health of the on-disk database //! is verified).