dbwrapper: Move HandleError to dbwrapper_private

HandleError is implementation-specific.
This commit is contained in:
Wladimir J. van der Laan
2016-04-20 11:48:57 +02:00
committed by Jack Grigg
parent 809a429ecf
commit 3923bcca7c
3 changed files with 35 additions and 23 deletions

View File

@@ -14,20 +14,6 @@
#include <memenv.h> #include <memenv.h>
#include <stdint.h> #include <stdint.h>
void HandleError(const leveldb::Status& status)
{
if (status.ok())
return;
LogPrintf("%s\n", status.ToString());
if (status.IsCorruption())
throw dbwrapper_error("Database corrupted");
if (status.IsIOError())
throw dbwrapper_error("Database I/O error");
if (status.IsNotFound())
throw dbwrapper_error("Database entry missing");
throw dbwrapper_error("Unknown database error");
}
static leveldb::Options GetOptions(size_t nCacheSize) static leveldb::Options GetOptions(size_t nCacheSize)
{ {
leveldb::Options options; leveldb::Options options;
@@ -60,13 +46,13 @@ CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, b
if (fWipe) { if (fWipe) {
LogPrintf("Wiping LevelDB in %s\n", path.string()); LogPrintf("Wiping LevelDB in %s\n", path.string());
leveldb::Status result = leveldb::DestroyDB(path.string(), options); leveldb::Status result = leveldb::DestroyDB(path.string(), options);
HandleError(result); dbwrapper_private::HandleError(result);
} }
TryCreateDirectory(path); TryCreateDirectory(path);
LogPrintf("Opening LevelDB in %s\n", path.string()); LogPrintf("Opening LevelDB in %s\n", path.string());
} }
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb); leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
HandleError(status); dbwrapper_private::HandleError(status);
LogPrintf("Opened LevelDB successfully\n"); LogPrintf("Opened LevelDB successfully\n");
} }
@@ -85,7 +71,7 @@ CDBWrapper::~CDBWrapper()
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
{ {
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch); leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
HandleError(status); dbwrapper_private::HandleError(status);
return true; return true;
} }
@@ -100,3 +86,21 @@ CDBIterator::~CDBIterator() { delete piter; }
bool CDBIterator::Valid() { return piter->Valid(); } bool CDBIterator::Valid() { return piter->Valid(); }
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); } void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
void CDBIterator::Next() { piter->Next(); } void CDBIterator::Next() { piter->Next(); }
namespace dbwrapper_private {
void HandleError(const leveldb::Status& status)
{
if (status.ok())
return;
LogPrintf("%s\n", status.ToString());
if (status.IsCorruption())
throw dbwrapper_error("Database corrupted");
if (status.IsIOError())
throw dbwrapper_error("Database I/O error");
if (status.IsNotFound())
throw dbwrapper_error("Database entry missing");
throw dbwrapper_error("Unknown database error");
}
};

View File

@@ -22,9 +22,17 @@ public:
dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {} dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {}
}; };
class CDBWrapper;
/** These should be considered an implementation detail of the specific database.
*/
namespace dbwrapper_private {
/** Handle database error by throwing dbwrapper_error exception.
*/
void HandleError(const leveldb::Status& status); void HandleError(const leveldb::Status& status);
class CDBWrapper; };
/** Batch of changes queued to be written to a CDBWrapper */ /** Batch of changes queued to be written to a CDBWrapper */
class CDBBatch class CDBBatch
@@ -179,7 +187,7 @@ public:
if (status.IsNotFound()) if (status.IsNotFound())
return false; return false;
LogPrintf("LevelDB read failure: %s\n", status.ToString()); LogPrintf("LevelDB read failure: %s\n", status.ToString());
HandleError(status); dbwrapper_private::HandleError(status);
} }
try { try {
CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION); CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
@@ -212,7 +220,7 @@ public:
if (status.IsNotFound()) if (status.IsNotFound())
return false; return false;
LogPrintf("LevelDB read failure: %s\n", status.ToString()); LogPrintf("LevelDB read failure: %s\n", status.ToString());
HandleError(status); dbwrapper_private::HandleError(status);
} }
return true; return true;
} }

View File

@@ -38,7 +38,7 @@ PaymentDisclosureDB::PaymentDisclosureDB(const boost::filesystem::path& dbPath)
TryCreateDirectory(path); TryCreateDirectory(path);
options.create_if_missing = true; options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, path.string(), &db); leveldb::Status status = leveldb::DB::Open(options, path.string(), &db);
HandleError(status); // throws exception dbwrapper_private::HandleError(status); // throws exception
LogPrintf("PaymentDisclosure: Opened LevelDB successfully\n"); LogPrintf("PaymentDisclosure: Opened LevelDB successfully\n");
} }
@@ -62,7 +62,7 @@ bool PaymentDisclosureDB::Put(const PaymentDisclosureKey& key, const PaymentDisc
leveldb::Slice slice(&ssValue[0], ssValue.size()); leveldb::Slice slice(&ssValue[0], ssValue.size());
leveldb::Status status = db->Put(writeOptions, key.ToString(), slice); leveldb::Status status = db->Put(writeOptions, key.ToString(), slice);
HandleError(status); dbwrapper_private::HandleError(status);
return true; return true;
} }
@@ -80,7 +80,7 @@ bool PaymentDisclosureDB::Get(const PaymentDisclosureKey& key, PaymentDisclosure
if (status.IsNotFound()) if (status.IsNotFound())
return false; return false;
LogPrintf("PaymentDisclosure: LevelDB read failure: %s\n", status.ToString()); LogPrintf("PaymentDisclosure: LevelDB read failure: %s\n", status.ToString());
HandleError(status); dbwrapper_private::HandleError(status);
} }
try { try {