mirror of
https://github.com/libretro/RetroArch
synced 2025-02-02 05:54:16 +00:00
(libretro-db) libretrodb.c - cleanups
This commit is contained in:
parent
9fcb7d744e
commit
2ce025884f
@ -447,15 +447,18 @@ void database_info_free(database_info_handle_t *db)
|
||||
database_info_list_t *database_info_list_new(
|
||||
const char *rdb_path, const char *query)
|
||||
{
|
||||
libretrodb_t db;
|
||||
libretrodb_cursor_t cur;
|
||||
int ret = 0;
|
||||
unsigned k = 0;
|
||||
database_info_t *database_info = 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))
|
||||
return NULL;
|
||||
if (!db || !cur)
|
||||
goto end;
|
||||
|
||||
if ((database_cursor_open(db, cur, rdb_path, query) != 0))
|
||||
goto end;
|
||||
|
||||
database_info_list = (database_info_list_t*)
|
||||
calloc(1, sizeof(*database_info_list));
|
||||
@ -466,7 +469,7 @@ database_info_list_t *database_info_list_new(
|
||||
while (ret != -1)
|
||||
{
|
||||
database_info_t db_info = {0};
|
||||
ret = database_cursor_iterate(&cur, &db_info);
|
||||
ret = database_cursor_iterate(cur, &db_info);
|
||||
|
||||
if (ret == 0)
|
||||
{
|
||||
@ -496,7 +499,12 @@ database_info_list_t *database_info_list_new(
|
||||
database_info_list->count = k;
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "bintree.h"
|
||||
#include "libretrodb_endian.h"
|
||||
#include "query.h"
|
||||
#include "libretrodb.h"
|
||||
|
||||
struct node_iter_ctx
|
||||
{
|
||||
@ -26,6 +27,42 @@ struct node_iter_ctx
|
||||
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 int libretrodb_read_metadata(int fd, libretrodb_metadata_t *md)
|
||||
@ -526,3 +563,40 @@ clean:
|
||||
libretrodb_cursor_close(&cur);
|
||||
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);
|
||||
}
|
||||
|
@ -17,47 +17,15 @@ extern "C" {
|
||||
|
||||
typedef struct libretrodb_query libretrodb_query_t;
|
||||
|
||||
typedef struct libretrodb
|
||||
{
|
||||
int fd;
|
||||
uint64_t root;
|
||||
uint64_t count;
|
||||
uint64_t first_index_offset;
|
||||
char path[1024];
|
||||
} libretrodb_t;
|
||||
typedef struct libretrodb libretrodb_t;
|
||||
|
||||
typedef struct libretrodb_index
|
||||
{
|
||||
char name[50];
|
||||
uint64_t key_size;
|
||||
uint64_t next;
|
||||
} libretrodb_index_t;
|
||||
typedef struct libretrodb_cursor libretrodb_cursor_t;
|
||||
|
||||
typedef struct libretrodb_metadata
|
||||
{
|
||||
uint64_t count;
|
||||
} libretrodb_metadata_t;
|
||||
typedef struct libretrodb_index libretrodb_index_t;
|
||||
|
||||
typedef struct libretrodb_header
|
||||
{
|
||||
char magic_number[sizeof(MAGIC_NUMBER)-1];
|
||||
uint64_t metadata_offset;
|
||||
} libretrodb_header_t;
|
||||
typedef int (*libretrodb_value_provider)(void *ctx, struct rmsgpack_dom_value *out);
|
||||
|
||||
typedef struct libretrodb_cursor
|
||||
{
|
||||
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);
|
||||
int libretrodb_create(int fd, libretrodb_value_provider value_provider, void *ctx);
|
||||
|
||||
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,
|
||||
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:
|
||||
* @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.
|
||||
**/
|
||||
int libretrodb_cursor_open(
|
||||
libretrodb_t *db,
|
||||
libretrodb_cursor_t *cursor,
|
||||
libretrodb_query_t *query
|
||||
);
|
||||
int libretrodb_cursor_open(libretrodb_t *db,
|
||||
libretrodb_cursor_t *cursor,
|
||||
libretrodb_query_t *query);
|
||||
|
||||
/**
|
||||
* libretrodb_cursor_reset:
|
||||
|
@ -7,8 +7,8 @@
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
int rv;
|
||||
libretrodb_t db;
|
||||
libretrodb_cursor_t cur;
|
||||
libretrodb_t *db;
|
||||
libretrodb_cursor_t *cur;
|
||||
libretrodb_query_t *q;
|
||||
struct rmsgpack_dom_value item;
|
||||
const char *command, *path, *query_exp, *error;
|
||||
@ -26,26 +26,32 @@ int main(int argc, char ** argv)
|
||||
command = argv[2];
|
||||
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));
|
||||
return 1;
|
||||
goto error;
|
||||
}
|
||||
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));
|
||||
return 1;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
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);
|
||||
printf("\n");
|
||||
@ -57,26 +63,26 @@ int main(int argc, char ** argv)
|
||||
if (argc != 4)
|
||||
{
|
||||
printf("Usage: %s <db file> find <query expression>\n", argv[0]);
|
||||
return 1;
|
||||
goto error;
|
||||
}
|
||||
|
||||
query_exp = argv[3];
|
||||
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)
|
||||
{
|
||||
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));
|
||||
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);
|
||||
printf("\n");
|
||||
@ -90,20 +96,26 @@ int main(int argc, char ** argv)
|
||||
if (argc != 5)
|
||||
{
|
||||
printf("Usage: %s <db file> create-index <index name> <field name>\n", argv[0]);
|
||||
return 1;
|
||||
goto error;
|
||||
}
|
||||
|
||||
index_name = argv[3];
|
||||
field_name = argv[4];
|
||||
|
||||
libretrodb_create_index(&db, index_name, field_name);
|
||||
libretrodb_create_index(db, index_name, field_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user