diff options
| author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-05 16:12:47 +0100 |
|---|---|---|
| committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-05 16:12:47 +0100 |
| commit | e36ea050972fcaaf3d85d672755bad4ebb2dcd80 (patch) | |
| tree | 5145e0b66650eea1df301106b7d197a586be65f3 /src/crepe/DB.cpp | |
| parent | 333b07775be1ef20fdb5909672c1e4dcabec1b40 (diff) | |
| parent | b770475741b7c33d57331f3139c55a3f237ad274 (diff) | |
merge `master` into `loek/savemgr`
Diffstat (limited to 'src/crepe/DB.cpp')
| -rw-r--r-- | src/crepe/DB.cpp | 67 |
1 files changed, 0 insertions, 67 deletions
diff --git a/src/crepe/DB.cpp b/src/crepe/DB.cpp deleted file mode 100644 index bd2f089..0000000 --- a/src/crepe/DB.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#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; -} - |