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 | |
parent | ecc5761fa78ccb57db958467c3fc999aceadd409 (diff) |
WIP db facade
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/crepe/DB.cpp | 67 | ||||
-rw-r--r-- | src/crepe/DB.h | 34 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.cpp | 11 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.h | 11 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/example/db.cpp | 30 | ||||
-rw-r--r-- | src/example/savemgr.cpp | 1 |
9 files changed, 158 insertions, 2 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b60a0cd..de9e990 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,6 +11,7 @@ find_package(SDL2 REQUIRED) find_package(SDL2_image REQUIRED) find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) +find_library(BERKELEY_DB db) add_library(crepe SHARED) add_executable(test_main EXCLUDE_FROM_ALL) @@ -23,6 +24,7 @@ target_link_libraries(crepe PRIVATE soloud PUBLIC SDL2 PUBLIC SDL2_image + PUBLIC ${BERKELEY_DB} ) add_subdirectory(crepe) diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 97f987a..750a47d 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -12,8 +12,8 @@ target_sources(crepe PUBLIC CollisionSystem.cpp Collider.cpp SDLContext.cpp - RenderSystem.cpp + DB.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -33,6 +33,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES RenderSystem.h ValueBroker.h ValueBroker.hpp + DB.h ) add_subdirectory(api) 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; +} + diff --git a/src/crepe/DB.h b/src/crepe/DB.h new file mode 100644 index 0000000..010ef42 --- /dev/null +++ b/src/crepe/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 char * 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); + +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); +}; + +} + diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp index a05e2c7..83ff0fa 100644 --- a/src/crepe/api/SaveManager.cpp +++ b/src/crepe/api/SaveManager.cpp @@ -1,8 +1,19 @@ +#include "../DB.h" +#include "util/log.h" + #include "SaveManager.h" +using namespace std; +using namespace crepe; using namespace crepe::api; +SaveManager::SaveManager() { + dbg_trace(); + this->db = make_unique<DB>("./save.crepe.db"); +} + SaveManager & SaveManager::get_instance() { + dbg_trace(); static SaveManager instance; return instance; } diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h index 78cd4ba..110735d 100644 --- a/src/crepe/api/SaveManager.h +++ b/src/crepe/api/SaveManager.h @@ -1,7 +1,13 @@ #pragma once +#include <memory> + #include "../ValueBroker.h" +namespace crepe { +class DB; +} + namespace crepe::api { class SaveManager { @@ -22,7 +28,7 @@ public: bool has(const char * key); private: - SaveManager() = default; + SaveManager(); virtual ~SaveManager() = default; public: @@ -32,6 +38,9 @@ public: SaveManager(SaveManager &&) = delete; SaveManager & operator = (const SaveManager &) = delete; SaveManager & operator = (SaveManager &&) = delete; + +private: + std::unique_ptr<DB> db = nullptr; }; } diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 8158d60..9698c41 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -26,4 +26,5 @@ add_example(particle) add_example(physics) add_example(savemgr) add_example(proxy) +add_example(db) diff --git a/src/example/db.cpp b/src/example/db.cpp new file mode 100644 index 0000000..b1ab196 --- /dev/null +++ b/src/example/db.cpp @@ -0,0 +1,30 @@ +#include <crepe/DB.h> +#include <crepe/api/Config.h> +#include <crepe/util/log.h> + +using namespace crepe; +using namespace std; + +// run before main +static auto _ = [] () { + auto & cfg = api::Config::get_instance(); + cfg.log.level = util::LogLevel::TRACE; + return 0; +}(); + +int main() { + dbg_trace(); + + DB db("file.db"); + + const char * test_key = "test-key"; + string test_data = "Hello world!"; + + dbg_logf("DB has key = %d", db.has(test_key)); + + db.set(test_key, test_data); + + dbg_logf("key = \"%s\"", db.get(test_key).c_str()); + + return 0; +} diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp index 5c034c4..2c03e3a 100644 --- a/src/example/savemgr.cpp +++ b/src/example/savemgr.cpp @@ -14,6 +14,7 @@ using namespace crepe::util; int main() { const char * key = "mygame.test"; + SaveManager & mgr = SaveManager::get_instance(); ValueBroker<unsigned int> & prop = mgr.get<unsigned int>(key, 0); |