From b47a41541f9da29186523a5b7367894c304f3050 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 28 Sep 2018 21:43:20 +0200 Subject: [PATCH] write a new (expensive) in-memory db driver for orders. --- src/kernel/CMakeLists.txt | 2 +- src/kernel/db/critbit.c | 72 --------------------------------------- src/kernel/db/memory.c | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 73 deletions(-) delete mode 100644 src/kernel/db/critbit.c create mode 100644 src/kernel/db/memory.c diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 562562d8a..756f691cc 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 2.6) project(kernel C) -SET(_DBFILES db/critbit.c) +SET(_DBFILES db/memory.c) IF(SQLITE3_FOUND) SET(_DBFILES db/sqlite.c) diff --git a/src/kernel/db/critbit.c b/src/kernel/db/critbit.c deleted file mode 100644 index 69bac159e..000000000 --- a/src/kernel/db/critbit.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include "driver.h" - -#include -#include - -#include - -#include -#include -#include - -static critbit_tree cb_orders = { 0 }; -static int auto_id = -1; - -struct cb_entry { - int id; - order_data *data; -}; - -order_data *db_driver_order_load(int id) -{ - void * match; - - assert(id>0); - if (cb_find_prefix(&cb_orders, &id, sizeof(id), &match, 1, 0) > 0) { - struct cb_entry *ent = (struct cb_entry *)match; - order_data * od = ent->data; - ++od->_refcount; - return od; - } - return NULL; -} - -int db_driver_order_save(order_data *od) -{ - struct cb_entry ent; - - assert(od && od->_str); - ++od->_refcount; - ent.id = ++auto_id; - ent.data = od; - cb_insert(&cb_orders, &ent, sizeof(ent)); - return ent.id; -} - -int db_driver_faction_save(int id, int no, int turn, const char *email, const char *password) -{ - return -1; -} - -static int free_data_cb(const void *match, const void *key, size_t keylen, - void *udata) -{ - struct cb_entry * ent = (struct cb_entry *)match; - order_data *od = ent->data; - odata_release(od); - return 0; -} - -void db_driver_open(void) -{ - assert(auto_id == -1); - auto_id = 0; -} - -void db_driver_close(void) -{ - cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL); - cb_clear(&cb_orders); - auto_id = -1; -} diff --git a/src/kernel/db/memory.c b/src/kernel/db/memory.c new file mode 100644 index 000000000..5fbaade4c --- /dev/null +++ b/src/kernel/db/memory.c @@ -0,0 +1,69 @@ +#include +#include "driver.h" + +#include +#include + +#include + +#include +#include +#include + +static selist * g_orders; +static int auto_id = -1; + +struct order_data *db_driver_order_load(int id) +{ + void * match; + + assert(id>0); + match = selist_get(g_orders, id - 1); + if (match) { + char * str = (char *)match; + struct order_data * od = NULL; + odata_create(&od, strlen(str), str); + return od; + } + return NULL; +} + +int db_driver_order_save(const char * str) +{ + assert(str); + selist_push(&g_orders, str_strdup(str)); + return ++auto_id; +} + +int db_driver_faction_save(int id, int no, int turn, const char *email, const char *password) +{ + return -1; +} + +static int free_data_cb(const void *match) +{ + char *str = (char *)match; + free(str); + return 0; +} + +int db_driver_open(database_t db, const char *dbname) +{ + (void)dbname; + if (db == DB_SWAP) { + assert(auto_id == -1); + auto_id = 0; + return 0; + } + return -1; +} + +void db_driver_close(database_t db) +{ + if (db == DB_SWAP) { + selist_foreach(g_orders, free_data_cb); + selist_free(g_orders); + g_orders = NULL; + auto_id = -1; + } +}