Skip to content

Commit 5864488

Browse files
committed
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.
1 parent dede0ee commit 5864488

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/dbwrapper.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ bool DestroyDB(const std::string& path_str)
3838
return leveldb::DestroyDB(path_str, {}).ok();
3939
}
4040

41+
/** Handle database error by throwing dbwrapper_error exception.
42+
*/
43+
static void HandleError(const leveldb::Status& status)
44+
{
45+
if (status.ok())
46+
return;
47+
const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
48+
LogPrintf("%s\n", errmsg);
49+
LogPrintf("You can use -debug=leveldb to get more complete diagnostic messages\n");
50+
throw dbwrapper_error(errmsg);
51+
}
52+
4153
class CBitcoinLevelDBLogger : public leveldb::Logger {
4254
public:
4355
// This code is adapted from posix_logger.h, which is why it is using vsprintf.
@@ -199,7 +211,7 @@ CDBWrapper::CDBWrapper(const DBParams& params)
199211
if (params.wipe_data) {
200212
LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(params.path));
201213
leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), options);
202-
dbwrapper_private::HandleError(result);
214+
HandleError(result);
203215
}
204216
TryCreateDirectories(params.path);
205217
LogPrintf("Opening LevelDB in %s\n", fs::PathToString(params.path));
@@ -209,7 +221,7 @@ CDBWrapper::CDBWrapper(const DBParams& params)
209221
// on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
210222
// (see env_posix.cc and env_windows.cc).
211223
leveldb::Status status = leveldb::DB::Open(options, fs::PathToString(params.path), &pdb);
212-
dbwrapper_private::HandleError(status);
224+
HandleError(status);
213225
LogPrintf("Opened LevelDB successfully\n");
214226

215227
if (params.options.force_compact) {
@@ -260,7 +272,7 @@ bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
260272
mem_before = DynamicMemoryUsage() / 1024.0 / 1024;
261273
}
262274
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.m_impl_batch->batch);
263-
dbwrapper_private::HandleError(status);
275+
HandleError(status);
264276
if (log_memory) {
265277
double mem_after = DynamicMemoryUsage() / 1024.0 / 1024;
266278
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
308320
if (status.IsNotFound())
309321
return std::nullopt;
310322
LogPrintf("LevelDB read failure: %s\n", status.ToString());
311-
dbwrapper_private::HandleError(status);
323+
HandleError(status);
312324
}
313325
return strValue;
314326
}
@@ -323,7 +335,7 @@ bool CDBWrapper::ExistsImpl(Span<const std::byte> ssKey) const
323335
if (status.IsNotFound())
324336
return false;
325337
LogPrintf("LevelDB read failure: %s\n", status.ToString());
326-
dbwrapper_private::HandleError(status);
338+
HandleError(status);
327339
}
328340
return true;
329341
}
@@ -371,16 +383,6 @@ void CDBIterator::Next() { m_impl_iter->iter->Next(); }
371383

372384
namespace dbwrapper_private {
373385

374-
void HandleError(const leveldb::Status& status)
375-
{
376-
if (status.ok())
377-
return;
378-
const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
379-
LogPrintf("%s\n", errmsg);
380-
LogPrintf("You can use -debug=leveldb to get more complete diagnostic messages\n");
381-
throw dbwrapper_error(errmsg);
382-
}
383-
384386
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
385387
{
386388
return w.obfuscate_key;

src/dbwrapper.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <vector>
2525
namespace leveldb {
2626
class Env;
27-
class Status;
2827
}
2928

3029
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
@@ -67,10 +66,6 @@ class CDBWrapper;
6766
*/
6867
namespace dbwrapper_private {
6968

70-
/** Handle database error by throwing dbwrapper_error exception.
71-
*/
72-
void HandleError(const leveldb::Status& status);
73-
7469
/** Work around circular dependency, as well as for testing in dbwrapper_tests.
7570
* Database obfuscation should be considered an implementation detail of the
7671
* specific database.

0 commit comments

Comments
 (0)