add berkeley db, rename some files

This commit is contained in:
Enno Rehling 2017-11-19 12:31:50 +01:00
parent 9c44c1ce63
commit 2f5f685e55
11 changed files with 88 additions and 66 deletions

View File

@ -44,8 +44,8 @@ void game_done(void)
#ifdef CLEANUP_CODE
/* Diese Routine enfernt allen allokierten Speicher wieder. Das ist nur
* zum Debugging interessant, wenn man Leak Detection hat, und nach
* nicht freigegebenem Speicher sucht, der nicht bis zum Ende benötigt
* wird (temporäre Hilsstrukturen) */
* nicht freigegebenem Speicher sucht, der nicht bis zum Ende ben<EFBFBD>tigt
* wird (tempor<EFBFBD>re Hilsstrukturen) */
free_game();
@ -60,12 +60,12 @@ void game_done(void)
free_special_directions();
free_locales();
kernel_done();
db_close();
dblib_close();
}
void game_init(void)
{
db_open();
dblib_open();
kernel_init();
register_triggers();
register_xmas();

View File

@ -37,10 +37,15 @@ jsonconf.test.c
)
SET(_DBFILES db/critbit.c)
IF(SQLITE3_FOUND)
SET(_DBFILES db/sqlite.c)
ENDIF(SQLITE3_FOUND)
IF(DB_FOUND)
SET(_DBFILES db/berkeley.c)
ENDIF(DB_FOUND)
SET(_FILES
${_DBFILES}
alliance.c

View File

@ -9,50 +9,30 @@
#include <util/log.h>
#ifdef USE_SQLITE
#include "db/sqlite.h"
#else
#include "db/critbit.h"
#endif
#include "db/driver.h"
order_data *db_load_order(int id)
order_data *dblib_load_order(int id)
{
if (id > 0) {
#ifdef USE_SQLITE
return db_sqlite_order_load(id);
#else
return db_critbit_order_load(id);
#endif
return db_driver_order_load(id);
}
return NULL;
}
int db_save_order(order_data *od)
int dblib_save_order(order_data *od)
{
if (od->_str) {
#ifdef USE_SQLITE
return db_sqlite_order_save(od);
#else
return db_critbit_order_save(od);
#endif
return db_driver_order_save(od);
}
return 0;
}
void db_open(void)
void dblib_open(void)
{
#ifdef USE_SQLITE
db_sqlite_open();
#else
db_critbit_open();
#endif
db_driver_open();
}
void db_close(void)
void dblib_close(void)
{
#ifdef USE_SQLITE
db_sqlite_close();
#else
db_critbit_close();
#endif
db_driver_close();
}

View File

@ -1,5 +1,5 @@
#ifndef H_DB
#define H_DB
#ifndef H_DATABASE
#define H_DATABASE
#include <stddef.h>
@ -9,11 +9,11 @@ extern "C" {
struct order_data;
void db_open(void);
void db_close(void);
void dblib_open(void);
void dblib_close(void);
struct order_data *db_load_order(int id);
int db_save_order(struct order_data *od);
struct order_data *dblib_load_order(int id);
int dblib_save_order(struct order_data *od);
#ifdef __cplusplus
}

43
src/kernel/db/berkeley.c Normal file
View File

@ -0,0 +1,43 @@
#include <platform.h>
#include "driver.h"
#ifdef __APPLE__
#define _DARWIN_C_SOURCE
#include <sys/types.h>
#endif
#include <db.h>
#include <assert.h>
static DB *g_dbp;
void db_driver_open(void)
{
int ret;
u_int32_t flags = DB_CREATE|DB_TRUNCATE;
const char * dbname;
ret = db_create(&g_dbp, NULL, 0);
assert(ret==0);
ret = g_dbp->open(g_dbp, NULL, dbname, NULL, DB_BTREE, flags, 0);
assert(ret==0);
}
void db_driver_close(void)
{
int ret;
ret = g_dbp->close(g_dbp, 0);
assert(ret==0);
}
int db_driver_order_save(struct order_data *od)
{
return 0;
}
struct order_data *db_driver_order_load(int id)
{
return NULL;
}

View File

@ -1,5 +1,5 @@
#include <platform.h>
#include "critbit.h"
#include "driver.h"
#include <kernel/orderdb.h>
#include <util/log.h>
@ -18,7 +18,7 @@ struct cb_entry {
order_data *data;
};
order_data *db_critbit_order_load(int id)
order_data *db_driver_order_load(int id)
{
void * match;
@ -32,7 +32,7 @@ order_data *db_critbit_order_load(int id)
return NULL;
}
int db_critbit_order_save(order_data *od)
int db_driver_order_save(order_data *od)
{
struct cb_entry ent;
@ -53,13 +53,13 @@ static int free_data_cb(const void *match, const void *key, size_t keylen,
return 0;
}
void db_critbit_open(void)
void db_driver_open(void)
{
assert(auto_id == -1);
auto_id = 0;
}
void db_critbit_close(void)
void db_driver_close(void)
{
cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL);
cb_clear(&cb_orders);

View File

@ -1,8 +0,0 @@
#pragma once
struct order_data;
void db_critbit_open(void);
void db_critbit_close(void);
int db_critbit_order_save(struct order_data *od);
struct order_data *db_critbit_order_load(int id);

8
src/kernel/db/driver.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
struct order_data;
void db_driver_open(void);
void db_driver_close(void);
int db_driver_order_save(struct order_data *od);
struct order_data *db_driver_order_load(int id);

View File

@ -6,6 +6,8 @@
#include <util/log.h>
#include "driver.h"
#include <sqlite3.h>
#include <assert.h>
@ -20,7 +22,7 @@ static sqlite3_stmt * g_stmt_select;
static int g_order_batchsize;
static int g_order_tx_size;
order_data *db_sqlite_order_load(int id)
order_data *db_driver_order_load(int id)
{
order_data * od = NULL;
int err;
@ -50,7 +52,7 @@ order_data *db_sqlite_order_load(int id)
return NULL;
}
int db_sqlite_order_save(order_data *od)
int db_driver_order_save(order_data *od)
{
int err;
sqlite3_int64 id;
@ -84,7 +86,7 @@ int db_sqlite_order_save(order_data *od)
return (int)id;
}
void db_sqlite_open(void)
void db_driver_open(void)
{
int err;
const char *dbname;
@ -108,7 +110,7 @@ void db_sqlite_open(void)
assert(err == SQLITE_OK);
}
void db_sqlite_close(void)
void db_driver_close(void)
{
int err;

View File

@ -1,8 +0,0 @@
#pragma once
struct order_data;
void db_sqlite_open(void);
void db_sqlite_close(void);
int db_sqlite_order_save(struct order_data *od);
struct order_data *db_sqlite_order_load(int id);

View File

@ -38,10 +38,10 @@ void odata_addref(order_data *od)
order_data *odata_load(int id)
{
return db_load_order(id);
return dblib_load_order(id);
}
int odata_save(order_data *od)
{
return db_save_order(od);
return dblib_save_order(od);
}