aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade/DB.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade/DB.cpp')
-rw-r--r--src/crepe/facade/DB.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp
new file mode 100644
index 0000000..7a3e473
--- /dev/null
+++ b/src/crepe/facade/DB.cpp
@@ -0,0 +1,61 @@
+#include <cstring>
+
+#include "util/dbg.h"
+
+#include "DB.h"
+
+using namespace std;
+using namespace crepe;
+
+DB::DB(const string & path) {
+ dbg_trace();
+ int ret;
+
+ // init database struct
+ libdb::DB * db;
+ if ((ret = libdb::db_create(&db, NULL, 0)) != 0)
+ 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
+ const char * file = path.empty() ? NULL : path.c_str();
+ ret = this->db->open(this->db.get(), NULL, file, NULL, libdb::DB_BTREE, DB_CREATE, 0);
+ if (ret != 0) throw runtime_error(format("db->open: {}", libdb::db_strerror(ret)));
+}
+
+libdb::DBT DB::to_thing(const string & thing) const noexcept {
+ libdb::DBT thang;
+ memset(&thang, 0, sizeof(libdb::DBT));
+ thang.data = (void *) thing.data();
+ thang.size = thing.size();
+ return thang;
+}
+
+string DB::get(const string & key) {
+ libdb::DBT db_key = this->to_thing(key);
+ libdb::DBT db_val;
+ memset(&db_val, 0, sizeof(libdb::DBT));
+
+ int ret = this->db->get(this->db.get(), NULL, &db_key, &db_val, 0);
+ if (ret == 0) return {static_cast<char *>(db_val.data), db_val.size};
+
+ string err = format("db->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 runtime_error(format("db->get: {}", libdb::db_strerror(ret)));
+}
+
+bool DB::has(const std::string & key) {
+ try {
+ this->get(key);
+ } catch (std::out_of_range &) {
+ return false;
+ }
+ return true;
+}