aboutsummaryrefslogtreecommitdiff
path: root/src/dbwrapper.cpp
diff options
context:
space:
mode:
authorTheCharlatan <seb.kung@gmail.com>2023-07-29 10:04:50 +0200
committerTheCharlatan <seb.kung@gmail.com>2023-08-05 10:42:59 +0200
commit586448888b72f7c87db4dcd30fc4e4044afae13b (patch)
treeffbc735cb1f4ba441b00be4c717c91f179e2fb8b /src/dbwrapper.cpp
parentdede0eef7adb7413f62f5abd68cac8e01635ba4a (diff)
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.
Diffstat (limited to 'src/dbwrapper.cpp')
-rw-r--r--src/dbwrapper.cpp32
1 files changed, 17 insertions, 15 deletions
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<std::string> CDBWrapper::ReadImpl(Span<const std::byte> 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<const std::byte> 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<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
{
return w.obfuscate_key;