aboutsummaryrefslogtreecommitdiff
path: root/src/leveldb.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2013-01-29 01:44:19 +0100
committerPieter Wuille <pieterw@google.com>2013-01-30 04:30:02 +0100
commit421218d3040279c84616891e8d14b05576b07c57 (patch)
treea1d1546c823d593008866a3790c0c71cbe4c10de /src/leveldb.h
parent7851033dd687e826df82c1ec841480710468ce8c (diff)
Deal with LevelDB errors
Diffstat (limited to 'src/leveldb.h')
-rw-r--r--src/leveldb.h23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/leveldb.h b/src/leveldb.h
index 0b83432072..79262edbb5 100644
--- a/src/leveldb.h
+++ b/src/leveldb.h
@@ -11,6 +11,14 @@
#include <boost/filesystem/path.hpp>
+class leveldb_error : public std::runtime_error
+{
+public:
+ leveldb_error(const std::string &msg) : std::runtime_error(msg) {}
+};
+
+void HandleError(const leveldb::Status &status) throw(leveldb_error);
+
// Batch of changes queued to be written to a CLevelDB
class CLevelDBBatch
{
@@ -72,7 +80,7 @@ public:
CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
~CLevelDB();
- template<typename K, typename V> bool Read(const K& key, V& value) {
+ template<typename K, typename V> bool Read(const K& key, V& value) throw(leveldb_error) {
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key));
ssKey << key;
@@ -84,6 +92,7 @@ public:
if (status.IsNotFound())
return false;
printf("LevelDB read failure: %s\n", status.ToString().c_str());
+ HandleError(status);
}
try {
CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
@@ -94,13 +103,13 @@ public:
return true;
}
- template<typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) {
+ template<typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) throw(leveldb_error) {
CLevelDBBatch batch;
batch.Write(key, value);
return WriteBatch(batch, fSync);
}
- template<typename K> bool Exists(const K& key) {
+ template<typename K> bool Exists(const K& key) throw(leveldb_error) {
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key));
ssKey << key;
@@ -112,24 +121,25 @@ public:
if (status.IsNotFound())
return false;
printf("LevelDB read failure: %s\n", status.ToString().c_str());
+ HandleError(status);
}
return true;
}
- template<typename K> bool Erase(const K& key, bool fSync = false) {
+ template<typename K> bool Erase(const K& key, bool fSync = false) throw(leveldb_error) {
CLevelDBBatch batch;
batch.Erase(key);
return WriteBatch(batch, fSync);
}
- bool WriteBatch(CLevelDBBatch &batch, bool fSync = false);
+ bool WriteBatch(CLevelDBBatch &batch, bool fSync = false) throw(leveldb_error);
// not available for LevelDB; provide for compatibility with BDB
bool Flush() {
return true;
}
- bool Sync() {
+ bool Sync() throw(leveldb_error) {
CLevelDBBatch batch;
return WriteBatch(batch, true);
}
@@ -141,4 +151,3 @@ public:
};
#endif // BITCOIN_LEVELDB_H
- \ No newline at end of file