aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade/DB.cpp
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-11-19 20:49:09 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-11-19 20:49:09 +0100
commit0f9f379a4dec7ad9d1dce30af5e2259931692c5f (patch)
treed84a76559d99fd6a3541d0087538564d79e39470 /src/crepe/facade/DB.cpp
parent38a2476bcfa41b6d83a9a72d35f5acb684dc87fd (diff)
parent0476a8e9dbe7afb422862f7b1c15aaed7f3c416e (diff)
merge with master
Diffstat (limited to 'src/crepe/facade/DB.cpp')
-rw-r--r--src/crepe/facade/DB.cpp29
1 files changed, 15 insertions, 14 deletions
diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp
index 0a2f455..d5d19dc 100644
--- a/src/crepe/facade/DB.cpp
+++ b/src/crepe/facade/DB.cpp
@@ -1,7 +1,6 @@
#include <cstring>
-#include "Exception.h"
-#include "util/log.h"
+#include "util/Log.h"
#include "DB.h"
@@ -15,19 +14,18 @@ DB::DB(const string & path) {
// init database struct
libdb::DB * db;
if ((ret = libdb::db_create(&db, NULL, 0)) != 0)
- throw Exception("db_create: %s", libdb::db_strerror(ret));
+ throw runtime_error(format("db_create: {}", libdb::db_strerror(ret)));
this->db = {db, [](libdb::DB * db) { db->close(db, 0); }};
// load or create database file
- if ((ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL,
- libdb::DB_BTREE, DB_CREATE, 0))
- != 0)
- throw Exception("db->open: %s", libdb::db_strerror(ret));
+ ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE,
+ 0);
+ if (ret != 0) throw runtime_error(format("db->open: {}", libdb::db_strerror(ret)));
// create cursor
libdb::DBC * cursor;
- if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0)
- throw Exception("db->cursor: %s", libdb::db_strerror(ret));
+ ret = this->db->cursor(this->db.get(), NULL, &cursor, 0);
+ if (ret != 0) throw runtime_error(format("db->cursor: {}", libdb::db_strerror(ret)));
this->cursor = {cursor, [](libdb::DBC * cursor) { cursor->close(cursor); }};
}
@@ -45,21 +43,24 @@ string DB::get(const string & key) {
memset(&db_val, 0, sizeof(libdb::DBT));
int ret = this->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST);
- if (ret != 0) throw Exception("cursor->get: %s", libdb::db_strerror(ret));
- return {static_cast<char *>(db_val.data), db_val.size};
+ if (ret == 0) return {static_cast<char *>(db_val.data), db_val.size};
+
+ string err = format("cursor->get: {}", libdb::db_strerror(ret));
+ if (ret == DB_NOTFOUND) throw out_of_range(err);
+ else throw runtime_error(err);
}
void DB::set(const string & key, const string & value) {
libdb::DBT db_key = this->to_thing(key);
libdb::DBT db_val = this->to_thing(value);
int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0);
- if (ret != 0) throw Exception("cursor->get: %s", libdb::db_strerror(ret));
+ if (ret != 0) throw runtime_error(format("cursor->get: {}", libdb::db_strerror(ret)));
}
-bool DB::has(const std::string & key) noexcept {
+bool DB::has(const std::string & key) {
try {
this->get(key);
- } catch (...) {
+ } catch (std::out_of_range &) {
return false;
}
return true;