fix bdb load/save, use recno db

This commit is contained in:
Enno Rehling 2017-11-19 16:19:16 +01:00
parent 2ff820d1b4
commit 41f20c02b2
2 changed files with 22 additions and 26 deletions

View File

@ -26,7 +26,7 @@ ENDIF()
IF (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang") IF (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
# SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wno-sign-conversion") # SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wno-sign-conversion")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-compare -Wall -Werror -Wno-unknown-pragmas -Wstrict-prototypes -Wpointer-arith -Wno-char-subscripts -Wno-long-long") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-compare -Wall -Werror -Wno-unknown-pragmas -Wstrict-prototypes -Wpointer-arith -Wno-char-subscripts -Wno-long-long")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89") # SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89")
ELSEIF(MSVC) ELSEIF(MSVC)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Wall /WX /MP") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Wall /WX /MP")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG set(CMAKE_EXE_LINKER_FLAGS_DEBUG

View File

@ -1,39 +1,32 @@
#ifdef __APPLE__
#define _DARWIN_C_SOURCE
#endif
#include <sys/types.h>
#include <db.h>
#include <platform.h> #include <platform.h>
#include "driver.h" #include "driver.h"
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/orderdb.h> #include <kernel/orderdb.h>
#ifdef __APPLE__
#define _DARWIN_C_SOURCE
#include <sys/types.h>
#endif
#include <db.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
static DB *g_dbp; static DB *g_dbp;
static int order_id = -1;
void db_driver_open(void) void db_driver_open(void)
{ {
int ret; int ret;
u_int32_t flags = DB_CREATE|DB_TRUNCATE; u_int32_t flags = DB_CREATE;
const char * dbname; const char * dbname;
dbname = config_get("game.dbname"); dbname = config_get("game.dbname");
if (!dbname) {
dbname = "temporary.db";
}
ret = db_create(&g_dbp, NULL, 0); ret = db_create(&g_dbp, NULL, 0);
assert(ret==0); assert(ret==0);
ret = g_dbp->open(g_dbp, NULL, dbname, NULL, DB_BTREE, flags, 0); ret = g_dbp->open(g_dbp, NULL, dbname, NULL, DB_RECNO, flags, 0);
assert(ret==0); assert(ret==0);
assert(order_id == -1);
order_id = 0;
} }
void db_driver_close(void) void db_driver_close(void)
@ -41,24 +34,24 @@ void db_driver_close(void)
int ret; int ret;
ret = g_dbp->close(g_dbp, 0); ret = g_dbp->close(g_dbp, 0);
assert(ret==0); assert(ret==0);
order_id = -1;
} }
int db_driver_order_save(struct order_data *od) int db_driver_order_save(struct order_data *od)
{ {
int ret; int ret;
DBT key, data; DBT key, data;
db_recno_t recno;
assert(od && od->_str); assert(od && od->_str);
++order_id; key.data = &recno;
key.data = &order_id; key.size = sizeof(recno);
key.size = sizeof(int); key.flags = DB_DBT_USERMEM;
data.data = (void *)od->_str; data.data = (void *)od->_str;
data.size = strlen(od->_str) + 1; data.size = strlen(od->_str) + 1;
data.flags = DB_DBT_USERMEM; data.flags = DB_DBT_USERMEM;
ret = g_dbp->put(g_dbp, NULL, &key, &data, DB_NOOVERWRITE); ret = g_dbp->put(g_dbp, NULL, &key, &data, DB_APPEND);
assert(ret == 0); assert(ret == 0);
return order_id; return (int)recno;
} }
struct order_data *db_driver_order_load(int id) struct order_data *db_driver_order_load(int id)
@ -66,15 +59,18 @@ struct order_data *db_driver_order_load(int id)
int ret; int ret;
order_data *od = NULL; order_data *od = NULL;
DBT key, data; DBT key, data;
db_recno_t recno;
assert(id>0 && id <= order_id); assert(id>0);
memset(&key, 0, sizeof(DBT)); memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT));
key.data = &id; recno = (db_recno_t)id;
key.size = sizeof(int); key.data = &recno;
key.size = sizeof(recno);
key.flags = DB_DBT_USERMEM;
ret = g_dbp->get(g_dbp, NULL, &key, &data, 0); ret = g_dbp->get(g_dbp, NULL, &key, &data, 0);
if (ret == 0) { if (ret == 0) {
odata_create(&od, data.ulen, data.data); odata_create(&od, data.size, data.data);
} }
return od; return od;
} }