aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/CMakeLists.txt2
-rw-r--r--src/crepe/facade/DB.cpp67
-rw-r--r--src/crepe/facade/DB.h34
3 files changed, 103 insertions, 0 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
index dbddcc6..bb52e7a 100644
--- a/src/crepe/facade/CMakeLists.txt
+++ b/src/crepe/facade/CMakeLists.txt
@@ -3,6 +3,7 @@ target_sources(crepe PUBLIC
SoundContext.cpp
SDLApp.cpp
SDLContext.cpp
+ DB.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -10,5 +11,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
SoundContext.h
SDLContext.h
SDLContext.h
+ DB.h
)
diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp
new file mode 100644
index 0000000..bd2f089
--- /dev/null
+++ b/src/crepe/facade/DB.cpp
@@ -0,0 +1,67 @@
+#include <cstring>
+
+#include "util/log.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 nullptr; // TODO: exception
+ 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 nullptr;
+ }
+
+ // create cursor
+ libdb::DBC * cursor;
+ if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0) {
+ throw nullptr;
+ }
+ this->cursor = { cursor, [] (libdb::DBC * cursor) { cursor->close(cursor); } };
+}
+
+
+libdb::DBT DB::to_thing(const string & thing) const {
+ 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->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST);
+ if (ret != 0) throw nullptr; // TODO: proper exception
+ return { static_cast<char *>(db_val.data), db_val.size };
+}
+
+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 nullptr; // TODO: proper exception
+}
+
+bool DB::has(const std::string & key) noexcept {
+ try {
+ this->get(key);
+ } catch (...) {
+ return false;
+ }
+ return true;
+}
+
diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h
new file mode 100644
index 0000000..06442ad
--- /dev/null
+++ b/src/crepe/facade/DB.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <string>
+#include <functional>
+#include <memory>
+
+namespace libdb {
+extern "C" {
+#include <db.h>
+}
+}
+
+namespace crepe {
+
+class DB {
+public:
+ DB(const std::string & path);
+ virtual ~DB() = default;
+
+public:
+ std::string get(const std::string & key);
+ void set(const std::string & key, const std::string & value);
+ bool has(const std::string & key) noexcept;
+
+private:
+ std::unique_ptr<libdb::DB, std::function<void(libdb::DB *)>> db;
+ std::unique_ptr<libdb::DBC, std::function<void(libdb::DBC *)>> cursor;
+
+private:
+ libdb::DBT to_thing(const std::string & thing) const;
+};
+
+}
+