From 586448888b72f7c87db4dcd30fc4e4044afae13b Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Sat, 29 Jul 2023 10:04:50 +0200 Subject: refactor: Move HandleError to dbwrapper implementation Make it a static function in dbwrapper.cpp, since it is not used elsewhere and when left in the header, would expose a leveldb type. The context of this commit is an effort to decouple the dbwrapper header file from leveldb includes. To this end, the includes are moved to the dbwrapper implementation file. This is done as part of the kernel project to reduce the number of required includes for users of the kernel. --- src/dbwrapper.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'src/dbwrapper.cpp') diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 88eb7e4aac..fe0a2e66f2 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -38,6 +38,18 @@ bool DestroyDB(const std::string& path_str) return leveldb::DestroyDB(path_str, {}).ok(); } +/** Handle database error by throwing dbwrapper_error exception. + */ +static void HandleError(const leveldb::Status& status) +{ + if (status.ok()) + return; + const std::string errmsg = "Fatal LevelDB error: " + status.ToString(); + LogPrintf("%s\n", errmsg); + LogPrintf("You can use -debug=leveldb to get more complete diagnostic messages\n"); + throw dbwrapper_error(errmsg); +} + class CBitcoinLevelDBLogger : public leveldb::Logger { public: // This code is adapted from posix_logger.h, which is why it is using vsprintf. @@ -199,7 +211,7 @@ CDBWrapper::CDBWrapper(const DBParams& params) if (params.wipe_data) { LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(params.path)); leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), options); - dbwrapper_private::HandleError(result); + HandleError(result); } TryCreateDirectories(params.path); LogPrintf("Opening LevelDB in %s\n", fs::PathToString(params.path)); @@ -209,7 +221,7 @@ CDBWrapper::CDBWrapper(const DBParams& params) // on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW // (see env_posix.cc and env_windows.cc). leveldb::Status status = leveldb::DB::Open(options, fs::PathToString(params.path), &pdb); - dbwrapper_private::HandleError(status); + HandleError(status); LogPrintf("Opened LevelDB successfully\n"); if (params.options.force_compact) { @@ -260,7 +272,7 @@ bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) mem_before = DynamicMemoryUsage() / 1024.0 / 1024; } leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.m_impl_batch->batch); - dbwrapper_private::HandleError(status); + HandleError(status); if (log_memory) { double mem_after = DynamicMemoryUsage() / 1024.0 / 1024; LogPrint(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n", @@ -308,7 +320,7 @@ std::optional CDBWrapper::ReadImpl(Span ssKey) con if (status.IsNotFound()) return std::nullopt; LogPrintf("LevelDB read failure: %s\n", status.ToString()); - dbwrapper_private::HandleError(status); + HandleError(status); } return strValue; } @@ -323,7 +335,7 @@ bool CDBWrapper::ExistsImpl(Span ssKey) const if (status.IsNotFound()) return false; LogPrintf("LevelDB read failure: %s\n", status.ToString()); - dbwrapper_private::HandleError(status); + HandleError(status); } return true; } @@ -371,16 +383,6 @@ void CDBIterator::Next() { m_impl_iter->iter->Next(); } namespace dbwrapper_private { -void HandleError(const leveldb::Status& status) -{ - if (status.ok()) - return; - const std::string errmsg = "Fatal LevelDB error: " + status.ToString(); - LogPrintf("%s\n", errmsg); - LogPrintf("You can use -debug=leveldb to get more complete diagnostic messages\n"); - throw dbwrapper_error(errmsg); -} - const std::vector& GetObfuscateKey(const CDBWrapper &w) { return w.obfuscate_key; -- cgit v1.2.3