(libretro-db) libretrodb.c - cleanups

This commit is contained in:
twinaphex 2015-09-17 09:46:26 +02:00
parent 9fcb7d744e
commit 2ce025884f
4 changed files with 134 additions and 66 deletions

View File

@ -447,15 +447,18 @@ void database_info_free(database_info_handle_t *db)
database_info_list_t *database_info_list_new( database_info_list_t *database_info_list_new(
const char *rdb_path, const char *query) const char *rdb_path, const char *query)
{ {
libretrodb_t db;
libretrodb_cursor_t cur;
int ret = 0; int ret = 0;
unsigned k = 0; unsigned k = 0;
database_info_t *database_info = NULL; database_info_t *database_info = NULL;
database_info_list_t *database_info_list = NULL; database_info_list_t *database_info_list = NULL;
libretrodb_t *db = libretrodb_new();
libretrodb_cursor_t *cur = libretrodb_cursor_new();
if ((database_cursor_open(&db, &cur, rdb_path, query) != 0)) if (!db || !cur)
return NULL; goto end;
if ((database_cursor_open(db, cur, rdb_path, query) != 0))
goto end;
database_info_list = (database_info_list_t*) database_info_list = (database_info_list_t*)
calloc(1, sizeof(*database_info_list)); calloc(1, sizeof(*database_info_list));
@ -466,7 +469,7 @@ database_info_list_t *database_info_list_new(
while (ret != -1) while (ret != -1)
{ {
database_info_t db_info = {0}; database_info_t db_info = {0};
ret = database_cursor_iterate(&cur, &db_info); ret = database_cursor_iterate(cur, &db_info);
if (ret == 0) if (ret == 0)
{ {
@ -496,7 +499,12 @@ database_info_list_t *database_info_list_new(
database_info_list->count = k; database_info_list->count = k;
end: end:
database_cursor_close(&db, &cur); database_cursor_close(db, cur);
if (db)
libretrodb_free(db);
if (cur)
libretrodb_cursor_free(cur);
return database_info_list; return database_info_list;
} }

View File

@ -19,6 +19,7 @@
#include "bintree.h" #include "bintree.h"
#include "libretrodb_endian.h" #include "libretrodb_endian.h"
#include "query.h" #include "query.h"
#include "libretrodb.h"
struct node_iter_ctx struct node_iter_ctx
{ {
@ -26,6 +27,42 @@ struct node_iter_ctx
libretrodb_index_t *idx; libretrodb_index_t *idx;
}; };
typedef struct libretrodb
{
int fd;
uint64_t root;
uint64_t count;
uint64_t first_index_offset;
char path[1024];
} libretrodb_t;
typedef struct libretrodb_index
{
char name[50];
uint64_t key_size;
uint64_t next;
} libretrodb_index_t;
typedef struct libretrodb_metadata
{
uint64_t count;
} libretrodb_metadata_t;
typedef struct libretrodb_header
{
char magic_number[sizeof(MAGIC_NUMBER)-1];
uint64_t metadata_offset;
} libretrodb_header_t;
typedef struct libretrodb_cursor
{
int is_valid;
int fd;
int eof;
libretrodb_query_t *query;
libretrodb_t *db;
} libretrodb_cursor_t;
static struct rmsgpack_dom_value sentinal; static struct rmsgpack_dom_value sentinal;
static int libretrodb_read_metadata(int fd, libretrodb_metadata_t *md) static int libretrodb_read_metadata(int fd, libretrodb_metadata_t *md)
@ -526,3 +563,40 @@ clean:
libretrodb_cursor_close(&cur); libretrodb_cursor_close(&cur);
return 0; return 0;
} }
libretrodb_cursor_t *libretrodb_cursor_new(void)
{
libretrodb_cursor_t *dbc = (libretrodb_cursor_t*)
calloc(1, sizeof(*dbc));
if (!dbc)
return NULL;
return dbc;
}
void libretrodb_cursor_free(libretrodb_cursor_t *dbc)
{
if (!dbc)
return;
free(dbc);
}
libretrodb_t *libretrodb_new(void)
{
libretrodb_t *db = (libretrodb_t*)calloc(1, sizeof(*db));
if (!db)
return NULL;
return db;
}
void libretrodb_free(libretrodb_t *db)
{
if (!db)
return;
free(db);
}

View File

@ -17,47 +17,15 @@ extern "C" {
typedef struct libretrodb_query libretrodb_query_t; typedef struct libretrodb_query libretrodb_query_t;
typedef struct libretrodb typedef struct libretrodb libretrodb_t;
{
int fd;
uint64_t root;
uint64_t count;
uint64_t first_index_offset;
char path[1024];
} libretrodb_t;
typedef struct libretrodb_index typedef struct libretrodb_cursor libretrodb_cursor_t;
{
char name[50];
uint64_t key_size;
uint64_t next;
} libretrodb_index_t;
typedef struct libretrodb_metadata typedef struct libretrodb_index libretrodb_index_t;
{
uint64_t count;
} libretrodb_metadata_t;
typedef struct libretrodb_header typedef int (*libretrodb_value_provider)(void *ctx, struct rmsgpack_dom_value *out);
{
char magic_number[sizeof(MAGIC_NUMBER)-1];
uint64_t metadata_offset;
} libretrodb_header_t;
typedef struct libretrodb_cursor int libretrodb_create(int fd, libretrodb_value_provider value_provider, void *ctx);
{
int is_valid;
int fd;
int eof;
libretrodb_query_t *query;
libretrodb_t *db;
} libretrodb_cursor_t;
typedef int (*libretrodb_value_provider)(void *ctx,
struct rmsgpack_dom_value *out);
int libretrodb_create(int fd,
libretrodb_value_provider value_provider, void *ctx);
void libretrodb_close(libretrodb_t *db); void libretrodb_close(libretrodb_t *db);
@ -69,6 +37,14 @@ int libretrodb_create_index(libretrodb_t *db, const char *name,
int libretrodb_find_entry(libretrodb_t *db, const char *index_name, int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
const void *key, struct rmsgpack_dom_value *out); const void *key, struct rmsgpack_dom_value *out);
libretrodb_t *libretrodb_new(void);
void libretrodb_free(libretrodb_t *db);
libretrodb_cursor_t *libretrodb_cursor_new(void);
void libretrodb_cursor_free(libretrodb_cursor_t *dbc);
/** /**
* libretrodb_cursor_open: * libretrodb_cursor_open:
* @db : Handle to database. * @db : Handle to database.
@ -79,11 +55,9 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
* *
* Returns: 0 if successful, otherwise negative. * Returns: 0 if successful, otherwise negative.
**/ **/
int libretrodb_cursor_open( int libretrodb_cursor_open(libretrodb_t *db,
libretrodb_t *db,
libretrodb_cursor_t *cursor, libretrodb_cursor_t *cursor,
libretrodb_query_t *query libretrodb_query_t *query);
);
/** /**
* libretrodb_cursor_reset: * libretrodb_cursor_reset:

View File

@ -7,8 +7,8 @@
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
int rv; int rv;
libretrodb_t db; libretrodb_t *db;
libretrodb_cursor_t cur; libretrodb_cursor_t *cur;
libretrodb_query_t *q; libretrodb_query_t *q;
struct rmsgpack_dom_value item; struct rmsgpack_dom_value item;
const char *command, *path, *query_exp, *error; const char *command, *path, *query_exp, *error;
@ -26,26 +26,32 @@ int main(int argc, char ** argv)
command = argv[2]; command = argv[2];
path = argv[1]; path = argv[1];
if ((rv = libretrodb_open(path, &db)) != 0) db = libretrodb_new();
cur = libretrodb_cursor_new();
if (!db || !cur)
goto error;
if ((rv = libretrodb_open(path, db)) != 0)
{ {
printf("Could not open db file '%s': %s\n", path, strerror(-rv)); printf("Could not open db file '%s': %s\n", path, strerror(-rv));
return 1; goto error;
} }
else if (strcmp(command, "list") == 0) else if (strcmp(command, "list") == 0)
{ {
if ((rv = libretrodb_cursor_open(&db, &cur, NULL)) != 0) if ((rv = libretrodb_cursor_open(db, cur, NULL)) != 0)
{ {
printf("Could not open cursor: %s\n", strerror(-rv)); printf("Could not open cursor: %s\n", strerror(-rv));
return 1; goto error;
} }
if (argc != 3) if (argc != 3)
{ {
printf("Usage: %s <db file> list\n", argv[0]); printf("Usage: %s <db file> list\n", argv[0]);
return 1; goto error;
} }
while (libretrodb_cursor_read_item(&cur, &item) == 0) while (libretrodb_cursor_read_item(cur, &item) == 0)
{ {
rmsgpack_dom_value_print(&item); rmsgpack_dom_value_print(&item);
printf("\n"); printf("\n");
@ -57,26 +63,26 @@ int main(int argc, char ** argv)
if (argc != 4) if (argc != 4)
{ {
printf("Usage: %s <db file> find <query expression>\n", argv[0]); printf("Usage: %s <db file> find <query expression>\n", argv[0]);
return 1; goto error;
} }
query_exp = argv[3]; query_exp = argv[3];
error = NULL; error = NULL;
q = libretrodb_query_compile(&db, query_exp, strlen(query_exp), &error); q = libretrodb_query_compile(db, query_exp, strlen(query_exp), &error);
if (error) if (error)
{ {
printf("%s\n", error); printf("%s\n", error);
return 1; goto error;
} }
if ((rv = libretrodb_cursor_open(&db, &cur, q)) != 0) if ((rv = libretrodb_cursor_open(db, cur, q)) != 0)
{ {
printf("Could not open cursor: %s\n", strerror(-rv)); printf("Could not open cursor: %s\n", strerror(-rv));
return 1; goto error;
} }
while (libretrodb_cursor_read_item(&cur, &item) == 0) while (libretrodb_cursor_read_item(cur, &item) == 0)
{ {
rmsgpack_dom_value_print(&item); rmsgpack_dom_value_print(&item);
printf("\n"); printf("\n");
@ -90,20 +96,26 @@ int main(int argc, char ** argv)
if (argc != 5) if (argc != 5)
{ {
printf("Usage: %s <db file> create-index <index name> <field name>\n", argv[0]); printf("Usage: %s <db file> create-index <index name> <field name>\n", argv[0]);
return 1; goto error;
} }
index_name = argv[3]; index_name = argv[3];
field_name = argv[4]; field_name = argv[4];
libretrodb_create_index(&db, index_name, field_name); libretrodb_create_index(db, index_name, field_name);
} }
else else
{ {
printf("Unknown command %s\n", argv[2]); printf("Unknown command %s\n", argv[2]);
return 1; goto error;
} }
libretrodb_close(&db); libretrodb_close(db);
error:
if (db)
libretrodb_free(db);
if (cur)
libretrodb_cursor_free(cur);
return 1; return 1;
} }