@ -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 ;