write a new (expensive) in-memory db driver for orders.

This commit is contained in:
Enno Rehling 2018-09-28 21:43:20 +02:00
parent 91c49659ef
commit b47a41541f
3 changed files with 70 additions and 73 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 2.6)
project(kernel C) project(kernel C)
SET(_DBFILES db/critbit.c) SET(_DBFILES db/memory.c)
IF(SQLITE3_FOUND) IF(SQLITE3_FOUND)
SET(_DBFILES db/sqlite.c) SET(_DBFILES db/sqlite.c)

View File

@ -1,72 +0,0 @@
#include <platform.h>
#include "driver.h"
#include <kernel/orderdb.h>
#include <util/log.h>
#include <critbit.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
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;
}

69
src/kernel/db/memory.c Normal file
View File

@ -0,0 +1,69 @@
#include <platform.h>
#include "driver.h"
#include <util/log.h>
#include <util/strings.h>
#include <selist.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
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;
}
}