separate database logic from order_data.

laying groundwork for different database backends.
This commit is contained in:
Enno Rehling 2017-11-09 19:55:28 +01:00
parent 34808a25c5
commit fee9b6d0a8
12 changed files with 154 additions and 117 deletions

View File

@ -116,7 +116,6 @@ set (ERESSEA_SRC
magic.c
market.c
morale.c
orderdb.c
orderfile.c
randenc.c
renumber.c
@ -219,7 +218,6 @@ set(TESTS_SRC
monsters.test.c
move.test.c
names.test.c
orderdb.test.c
orderfile.test.c
piracy.test.c
prefix.test.c

View File

@ -8,7 +8,6 @@
#include "creport.h"
#include "report.h"
#include "names.h"
#include "orderdb.h"
#include "reports.h"
#include "spells.h"
#include "vortex.h"
@ -25,6 +24,7 @@
#include <util/functions.h>
#include <kernel/building.h>
#include <kernel/curse.h>
#include <kernel/db.h>
#include <kernel/equipment.h>
#include <kernel/item.h>
#include <kernel/xmlreader.h>
@ -60,15 +60,12 @@ void game_done(void)
free_special_directions();
free_locales();
kernel_done();
orderdb_close();
db_close();
}
void game_init(void)
{
db_backend choices[] = { DB_MEMORY, DB_NONE };
if (orderdb_open(choices) == DB_NONE) {
log_fatal("no orderdb backend available");
}
db_open();
kernel_init();
register_triggers();
register_xmas();

View File

@ -11,12 +11,14 @@ command.test.c
config.test.c
# connection.test.c
curse.test.c
db.test.c
equipment.test.c
faction.test.c
group.test.c
item.test.c
messages.test.c
order.test.c
orderdb.test.c
# pathfinder.test.c
plane.test.c
pool.test.c
@ -34,7 +36,10 @@ jsonconf.test.c
# xmlreader.test.c
)
SET(_DBFILES dbtrie.c)
SET(_FILES
${_DBFILES}
alliance.c
ally.c
build.c
@ -50,6 +55,7 @@ group.c
item.c
messages.c
order.c
orderdb.c
pathfinder.c
plane.c
pool.c

21
src/kernel/db.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef H_DB
#define H_DB
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
struct order_data;
void db_open(void);
void db_close(void);
struct order_data *db_load_order(int id);
int db_save_order(struct order_data *od);
#ifdef __cplusplus
}
#endif
#endif

40
src/kernel/db.test.c Normal file
View File

@ -0,0 +1,40 @@
#include <platform.h>
#include <kernel/config.h>
#include "db.h"
#include "orderdb.h"
#include <CuTest.h>
#include <tests.h>
#include <string.h>
static void test_save_load_order(CuTest *tc) {
order_data *od;
int id;
const char * s = "GIB enno 1 Hodor";
db_open();
odata_create(&od, strlen(s) + 1, s);
CuAssertTrue(tc, od->_refcount >= 1);
id = odata_save(od);
odata_release(od);
CuAssertTrue(tc, id != 0);
od = odata_load(id);
CuAssertPtrNotNull(tc, od);
CuAssertTrue(tc, od->_refcount >= 1);
CuAssertStrEquals(tc, s, od->_str);
odata_release(od);
db_close();
}
CuSuite *get_db_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_save_load_order);
return suite;
}

View File

@ -1,4 +1,5 @@
#include <platform.h>
#include "db.h"
#include "orderdb.h"
#include <util/log.h>
@ -16,9 +17,7 @@ struct cb_entry {
order_data *data;
};
static db_backend odata_backend = DB_NONE;
order_data *odata_load(int id)
order_data *db_load_order(int id)
{
void * match;
@ -33,20 +32,7 @@ order_data *odata_load(int id)
return NULL;
}
void odata_create(order_data **pdata, size_t len, const char *str)
{
order_data *data;
char *result;
data = malloc(sizeof(order_data) + len + 1);
data->_refcount = 1;
result = (char *)(data + 1);
data->_str = (len > 0) ? result : NULL;
if (str) strcpy(result, str);
if (pdata) *pdata = data;
}
int odata_save(order_data *od)
int db_save_order(order_data *od)
{
if (od->_str) {
struct cb_entry ent;
@ -59,16 +45,9 @@ int odata_save(order_data *od)
return 0;
}
void odata_release(order_data * od)
static int free_data_cb(const void *match, const void *key, size_t keylen,
void *udata)
{
if (od) {
if (--od->_refcount == 0) {
free(od);
}
}
}
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;
if (od->_refcount > 1) {
@ -78,25 +57,13 @@ int free_data_cb(const void *match, const void *key, size_t keylen, void *udata)
return 0;
}
void free_data(void) {
void db_open(void)
{
auto_id = 0;
}
void db_close(void)
{
cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL);
cb_clear(&cb_orders);
}
db_backend orderdb_open(db_backend choices[])
{
int i;
for (i = 0; choices[i] != DB_NONE; ++i) {
db_backend choice = choices[i];
if (choice == DB_MEMORY) {
auto_id = 0;
return odata_backend = choice;
}
}
return DB_NONE;
}
void orderdb_close(void)
{
free_data();
}

42
src/kernel/orderdb.c Normal file
View File

@ -0,0 +1,42 @@
#include <platform.h>
#include "db.h"
#include "orderdb.h"
#include <util/log.h>
#include <critbit.h>
#include <stdlib.h>
#include <string.h>
void odata_create(order_data **pdata, size_t len, const char *str)
{
order_data *data;
char *result;
data = malloc(sizeof(order_data) + len + 1);
data->_refcount = 1;
result = (char *)(data + 1);
data->_str = (len > 0) ? result : NULL;
if (str) strcpy(result, str);
if (pdata) *pdata = data;
}
void odata_release(order_data * od)
{
if (od) {
if (--od->_refcount == 0) {
free(od);
}
}
}
order_data *odata_load(int id)
{
return db_load_order(id);
}
int odata_save(order_data *od)
{
return db_save_order(od);
}

View File

@ -7,22 +7,11 @@
extern "C" {
#endif
typedef enum {
DB_NONE,
DB_MEMORY,
DB_MMAP,
DB_BDB,
DB_SQLITE
} db_backend;
typedef struct order_data {
const char *_str;
int _refcount;
} order_data;
db_backend orderdb_open(db_backend choices[]);
void orderdb_close(void);
void odata_create(order_data **pdata, size_t len, const char *str);
void odata_release(order_data * od);

28
src/kernel/orderdb.test.c Normal file
View File

@ -0,0 +1,28 @@
#include <platform.h>
#include <kernel/config.h>
#include "orderdb.h"
#include <CuTest.h>
#include <tests.h>
#include <string.h>
static void test_orderdb(CuTest *tc) {
order_data *od = NULL;
const char * s = "GIB enno 1 Hodor";
odata_create(&od, strlen(s) + 1, s);
CuAssertPtrNotNull(tc, od);
CuAssertStrEquals(tc, s, od->_str);
CuAssertTrue(tc, od->_refcount >= 1);
odata_release(od);
}
CuSuite *get_orderdb_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_orderdb);
return suite;
}

View File

@ -1,53 +0,0 @@
#include <platform.h>
#include <kernel/config.h>
#include "orderdb.h"
#include <CuTest.h>
#include <tests.h>
#include <string.h>
static void test_orderdb_open_close(CuTest *tc) {
db_backend choices[] = { DB_MEMORY, DB_NONE };
db_backend nochoice[] = { DB_SQLITE, DB_NONE };
CuAssertIntEquals(tc, DB_MEMORY, orderdb_open(choices));
orderdb_close();
CuAssertIntEquals(tc, DB_NONE, orderdb_open(nochoice));
orderdb_close();
}
static void test_odata_save_load(CuTest *tc) {
db_backend choices[] = { DB_MEMORY, DB_NONE };
order_data *od;
int id;
const char * s = "GIB enno 1 Hodor";
CuAssertIntEquals(tc, DB_MEMORY, orderdb_open(choices));
odata_create(&od, strlen(s) + 1, s);
CuAssertTrue(tc, od->_refcount >= 1);
id = odata_save(od);
odata_release(od);
CuAssertTrue(tc, id != 0);
od = odata_load(id);
CuAssertPtrNotNull(tc, od);
CuAssertTrue(tc, od->_refcount >= 1);
CuAssertStrEquals(tc, s, od->_str);
odata_release(od);
orderdb_close();
}
CuSuite *get_orderdb_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_orderdb_open_close);
SUITE_ADD_TEST(suite, test_odata_save_load);
return suite;
}

View File

@ -93,6 +93,7 @@ int RunAllTests(int argc, char *argv[])
/* kernel */
ADD_SUITE(alliance);
ADD_SUITE(command);
ADD_SUITE(db);
ADD_SUITE(plane);
ADD_SUITE(unit);
ADD_SUITE(faction);

View File

@ -1,6 +1,7 @@
cd c:\users\enno\documents\eressea\git\tests
"C:\Program Files (x86)\Dr. Memory\bin64\drmemory.exe" ..\build-vs14\eressea\Debug\eressea.exe -t184 test-turn.lua
REM "C:\Program Files (x86)\Dr. Memory\bin64\drmemory.exe" ..\build-vs14\eressea\Debug\test_eressea.exe -t184 test-turn.lua
del /q reports
del /q datum htpasswd parteien parteien.full passwd score turn