mirror of
https://github.com/libretro/RetroArch
synced 2025-04-15 23:42:30 +00:00
(libretrodb.c) Indenting nits
This commit is contained in:
parent
2ce025884f
commit
da54b5dedf
@ -112,7 +112,7 @@ static int validate_document(const struct rmsgpack_dom_value *doc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int libretrodb_create(int fd, libretrodb_value_provider value_provider,
|
int libretrodb_create(int fd, libretrodb_value_provider value_provider,
|
||||||
void *ctx)
|
void *ctx)
|
||||||
{
|
{
|
||||||
int rv;
|
int rv;
|
||||||
off_t root;
|
off_t root;
|
||||||
@ -167,19 +167,19 @@ static int libretrodb_read_index_header(int fd, libretrodb_index_t *idx)
|
|||||||
|
|
||||||
static void libretrodb_write_index_header(int fd, libretrodb_index_t *idx)
|
static void libretrodb_write_index_header(int fd, libretrodb_index_t *idx)
|
||||||
{
|
{
|
||||||
rmsgpack_write_map_header(fd, 3);
|
rmsgpack_write_map_header(fd, 3);
|
||||||
rmsgpack_write_string(fd, "name", strlen("name"));
|
rmsgpack_write_string(fd, "name", strlen("name"));
|
||||||
rmsgpack_write_string(fd, idx->name, strlen(idx->name));
|
rmsgpack_write_string(fd, idx->name, strlen(idx->name));
|
||||||
rmsgpack_write_string(fd, "key_size", strlen("key_size"));
|
rmsgpack_write_string(fd, "key_size", strlen("key_size"));
|
||||||
rmsgpack_write_uint(fd, idx->key_size);
|
rmsgpack_write_uint(fd, idx->key_size);
|
||||||
rmsgpack_write_string(fd, "next", strlen("next"));
|
rmsgpack_write_string(fd, "next", strlen("next"));
|
||||||
rmsgpack_write_uint(fd, idx->next);
|
rmsgpack_write_uint(fd, idx->next);
|
||||||
}
|
}
|
||||||
|
|
||||||
void libretrodb_close(libretrodb_t *db)
|
void libretrodb_close(libretrodb_t *db)
|
||||||
{
|
{
|
||||||
close(db->fd);
|
close(db->fd);
|
||||||
db->fd = -1;
|
db->fd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int libretrodb_open(const char *path, libretrodb_t *db)
|
int libretrodb_open(const char *path, libretrodb_t *db)
|
||||||
@ -278,7 +278,7 @@ static int binsearch(const void *buff, const void *item,
|
|||||||
}
|
}
|
||||||
|
|
||||||
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_index_t idx;
|
libretrodb_index_t idx;
|
||||||
int rv;
|
int rv;
|
||||||
@ -332,8 +332,8 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
|
|||||||
**/
|
**/
|
||||||
int libretrodb_cursor_reset(libretrodb_cursor_t *cursor)
|
int libretrodb_cursor_reset(libretrodb_cursor_t *cursor)
|
||||||
{
|
{
|
||||||
cursor->eof = 0;
|
cursor->eof = 0;
|
||||||
return lseek(cursor->fd,
|
return lseek(cursor->fd,
|
||||||
cursor->db->root + sizeof(libretrodb_header_t),
|
cursor->db->root + sizeof(libretrodb_header_t),
|
||||||
SEEK_SET);
|
SEEK_SET);
|
||||||
}
|
}
|
||||||
@ -380,16 +380,16 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
|
|||||||
if (!cursor)
|
if (!cursor)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
close(cursor->fd);
|
close(cursor->fd);
|
||||||
cursor->is_valid = 0;
|
cursor->is_valid = 0;
|
||||||
cursor->fd = -1;
|
cursor->fd = -1;
|
||||||
cursor->eof = 1;
|
cursor->eof = 1;
|
||||||
cursor->db = NULL;
|
cursor->db = NULL;
|
||||||
|
|
||||||
if (cursor->query)
|
if (cursor->query)
|
||||||
libretrodb_query_free(cursor->query);
|
libretrodb_query_free(cursor->query);
|
||||||
|
|
||||||
cursor->query = NULL;
|
cursor->query = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -423,36 +423,36 @@ int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
|
|||||||
|
|
||||||
static int node_iter(void *value, void *ctx)
|
static int node_iter(void *value, void *ctx)
|
||||||
{
|
{
|
||||||
struct node_iter_ctx *nictx = (struct node_iter_ctx*)ctx;
|
struct node_iter_ctx *nictx = (struct node_iter_ctx*)ctx;
|
||||||
|
|
||||||
if (write(nictx->db->fd, value,
|
if (write(nictx->db->fd, value,
|
||||||
nictx->idx->key_size + sizeof(uint64_t)) > 0)
|
nictx->idx->key_size + sizeof(uint64_t)) > 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint64_t libretrodb_tell(libretrodb_t *db)
|
static uint64_t libretrodb_tell(libretrodb_t *db)
|
||||||
{
|
{
|
||||||
return lseek(db->fd, 0, SEEK_CUR);
|
return lseek(db->fd, 0, SEEK_CUR);
|
||||||
}
|
}
|
||||||
|
|
||||||
int libretrodb_create_index(libretrodb_t *db,
|
int libretrodb_create_index(libretrodb_t *db,
|
||||||
const char *name, const char *field_name)
|
const char *name, const char *field_name)
|
||||||
{
|
{
|
||||||
int rv;
|
int rv;
|
||||||
struct node_iter_ctx nictx;
|
struct node_iter_ctx nictx;
|
||||||
struct rmsgpack_dom_value key;
|
struct rmsgpack_dom_value key;
|
||||||
libretrodb_index_t idx;
|
libretrodb_index_t idx;
|
||||||
struct rmsgpack_dom_value item;
|
struct rmsgpack_dom_value item;
|
||||||
struct rmsgpack_dom_value *field;
|
struct rmsgpack_dom_value *field;
|
||||||
uint64_t idx_header_offset;
|
uint64_t idx_header_offset;
|
||||||
libretrodb_cursor_t cur = {0};
|
libretrodb_cursor_t cur = {0};
|
||||||
void *buff = NULL;
|
void *buff = NULL;
|
||||||
uint64_t *buff_u64 = NULL;
|
uint64_t *buff_u64 = NULL;
|
||||||
uint8_t field_size = 0;
|
uint8_t field_size = 0;
|
||||||
uint64_t item_loc = libretrodb_tell(db);
|
uint64_t item_loc = libretrodb_tell(db);
|
||||||
bintree_t *tree = bintree_new(node_compare, &field_size);
|
bintree_t *tree = bintree_new(node_compare, &field_size);
|
||||||
|
|
||||||
if (!tree)
|
if (!tree)
|
||||||
{
|
{
|
||||||
@ -460,108 +460,108 @@ int libretrodb_create_index(libretrodb_t *db,
|
|||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (libretrodb_cursor_open(db, &cur, NULL) != 0)
|
if (libretrodb_cursor_open(db, &cur, NULL) != 0)
|
||||||
{
|
{
|
||||||
rv = -1;
|
rv = -1;
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
key.type = RDT_STRING;
|
key.type = RDT_STRING;
|
||||||
key.val.string.len = strlen(field_name);
|
key.val.string.len = strlen(field_name);
|
||||||
|
|
||||||
/* We know we aren't going to change it */
|
/* We know we aren't going to change it */
|
||||||
key.val.string.buff = (char *) field_name;
|
key.val.string.buff = (char *) field_name;
|
||||||
|
|
||||||
while (libretrodb_cursor_read_item(&cur, &item) == 0)
|
while (libretrodb_cursor_read_item(&cur, &item) == 0)
|
||||||
{
|
{
|
||||||
if (item.type != RDT_MAP)
|
if (item.type != RDT_MAP)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("Only map keys are supported\n");
|
printf("Only map keys are supported\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
field = rmsgpack_dom_value_map_value(&item, &key);
|
field = rmsgpack_dom_value_map_value(&item, &key);
|
||||||
|
|
||||||
if (!field)
|
if (!field)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("field not found in item\n");
|
printf("field not found in item\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field->type != RDT_BINARY)
|
if (field->type != RDT_BINARY)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("field is not binary\n");
|
printf("field is not binary\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field->val.binary.len == 0)
|
if (field->val.binary.len == 0)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("field is empty\n");
|
printf("field is empty\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field_size == 0)
|
if (field_size == 0)
|
||||||
field_size = field->val.binary.len;
|
field_size = field->val.binary.len;
|
||||||
else if (field->val.binary.len != field_size)
|
else if (field->val.binary.len != field_size)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("field is not of correct size\n");
|
printf("field is not of correct size\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
buff = malloc(field_size + sizeof(uint64_t));
|
buff = malloc(field_size + sizeof(uint64_t));
|
||||||
if (!buff)
|
if (!buff)
|
||||||
{
|
{
|
||||||
rv = -ENOMEM;
|
rv = -ENOMEM;
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(buff, field->val.binary.buff, field_size);
|
memcpy(buff, field->val.binary.buff, field_size);
|
||||||
|
|
||||||
buff_u64 = (uint64_t *)buff + field_size;
|
buff_u64 = (uint64_t *)buff + field_size;
|
||||||
|
|
||||||
memcpy(buff_u64, &item_loc, sizeof(uint64_t));
|
memcpy(buff_u64, &item_loc, sizeof(uint64_t));
|
||||||
|
|
||||||
if (bintree_insert(tree, buff) != 0)
|
if (bintree_insert(tree, buff) != 0)
|
||||||
{
|
{
|
||||||
printf("Value is not unique: ");
|
printf("Value is not unique: ");
|
||||||
rmsgpack_dom_value_print(field);
|
rmsgpack_dom_value_print(field);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
buff = NULL;
|
buff = NULL;
|
||||||
rmsgpack_dom_value_free(&item);
|
rmsgpack_dom_value_free(&item);
|
||||||
item_loc = libretrodb_tell(db);
|
item_loc = libretrodb_tell(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
(void)rv;
|
(void)rv;
|
||||||
(void)idx_header_offset;
|
(void)idx_header_offset;
|
||||||
|
|
||||||
idx_header_offset = lseek(db->fd, 0, SEEK_END);
|
idx_header_offset = lseek(db->fd, 0, SEEK_END);
|
||||||
strncpy(idx.name, name, 50);
|
strncpy(idx.name, name, 50);
|
||||||
|
|
||||||
idx.name[49] = '\0';
|
idx.name[49] = '\0';
|
||||||
idx.key_size = field_size;
|
idx.key_size = field_size;
|
||||||
idx.next = db->count * (field_size + sizeof(uint64_t));
|
idx.next = db->count * (field_size + sizeof(uint64_t));
|
||||||
libretrodb_write_index_header(db->fd, &idx);
|
libretrodb_write_index_header(db->fd, &idx);
|
||||||
|
|
||||||
nictx.db = db;
|
nictx.db = db;
|
||||||
nictx.idx = &idx;
|
nictx.idx = &idx;
|
||||||
bintree_iterate(tree, node_iter, &nictx);
|
bintree_iterate(tree, node_iter, &nictx);
|
||||||
bintree_free(tree);
|
bintree_free(tree);
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rmsgpack_dom_value_free(&item);
|
rmsgpack_dom_value_free(&item);
|
||||||
if (buff)
|
if (buff)
|
||||||
free(buff);
|
free(buff);
|
||||||
if (cur.is_valid)
|
if (cur.is_valid)
|
||||||
libretrodb_cursor_close(&cur);
|
libretrodb_cursor_close(&cur);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
libretrodb_cursor_t *libretrodb_cursor_new(void)
|
libretrodb_cursor_t *libretrodb_cursor_new(void)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user