diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-27 19:33:45 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-27 19:33:45 +0100 |
commit | b2fc208fbdb55ecc3cba59e2dd51976ce829a4be (patch) | |
tree | 1a60e74b7533403831868b04943978987cec2c75 /src/crepe/DB.cpp | |
parent | ecc5761fa78ccb57db958467c3fc999aceadd409 (diff) |
WIP db facade
Diffstat (limited to 'src/crepe/DB.cpp')
-rw-r--r-- | src/crepe/DB.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/crepe/DB.cpp b/src/crepe/DB.cpp new file mode 100644 index 0000000..b8448a7 --- /dev/null +++ b/src/crepe/DB.cpp @@ -0,0 +1,67 @@ +#include <cstring> + +#include "util/log.h" + +#include "DB.h" + +using namespace std; +using namespace crepe; + +DB::DB(const char * 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, 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) { + 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(), NULL, &db_key, &db_val); + return ""; +} + +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); + // TODO: check flags + // TODO: check ret +} + +bool DB::has(const std::string & key) { + try { + this->get(key); + } catch (...) { + return false; + } + return true; +} + |