This commit is contained in:
twinaphex 2015-04-04 21:10:36 +02:00
parent a7a1d60ecb
commit d41b55b25b
3 changed files with 100 additions and 94 deletions

View File

@ -13,8 +13,8 @@ void bintree_new(struct bintree *t, bintree_cmp_func cmp,
void *ctx) void *ctx)
{ {
t->root = new_nil_node(NULL); t->root = new_nil_node(NULL);
t->cmp = cmp; t->cmp = cmp;
t->ctx = ctx; t->ctx = ctx;
} }
static struct bintree_node *new_nil_node(struct bintree_node *parent) static struct bintree_node *new_nil_node(struct bintree_node *parent)

View File

@ -3,91 +3,97 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int libretrodb_lua_to_rmsgpack_value( int libretrodb_lua_to_rmsgpack_value(lua_State * L, int index, struct rmsgpack_dom_value * out)
lua_State * L, {
int index, lua_Number tmp_num;
struct rmsgpack_dom_value * out size_t tmp_len;
) { int i, rv = -1;
const char * tmp_string = NULL;
char * tmp_buff = NULL;
struct rmsgpack_dom_value * tmp_value;
const int key_idx = -2;
const int value_idx = -1;
const int MAX_FIELDS = 100;
int rv = -1; out->type = RDT_MAP;
int i; out->map.len = 0;
const char * tmp_string = NULL; out->map.items = calloc(MAX_FIELDS, sizeof(struct rmsgpack_dom_pair));
char * tmp_buff = NULL; lua_pushnil(L);
struct rmsgpack_dom_value * tmp_value;
const int key_idx = -2;
const int value_idx = -1;
const int MAX_FIELDS = 100;
size_t tmp_len;
lua_Number tmp_num;
out->type = RDT_MAP; while (lua_next(L, index - 1) != 0)
out->map.len = 0; {
out->map.items = calloc(MAX_FIELDS, sizeof(struct rmsgpack_dom_pair)); if (out->map.len > MAX_FIELDS)
lua_pushnil(L); printf("skipping due to too many keys\n");
while (lua_next(L, index - 1) != 0) { else if (!lua_isstring(L, key_idx))
if (out->map.len > MAX_FIELDS) { printf("skipping non string key\n");
printf("skipping due to too many keys\n"); else if (lua_isnil(L, value_idx))
} else if (!lua_isstring(L, key_idx)) { {
printf("skipping non string key\n"); // Skipping nil value fields to save disk space
} else if (lua_isnil(L, value_idx)) { }
// Skipping nil value fields to save disk space else
} else { {
i = out->map.len; i = out->map.len;
tmp_buff = strdup(lua_tostring(L, key_idx)); tmp_buff = strdup(lua_tostring(L, key_idx));
out->map.items[i].key.type = RDT_STRING; out->map.items[i].key.type = RDT_STRING;
out->map.items[i].key.string.len = strlen(tmp_buff); out->map.items[i].key.string.len = strlen(tmp_buff);
out->map.items[i].key.string.buff = tmp_buff; out->map.items[i].key.string.buff = tmp_buff;
tmp_value = &out->map.items[i].value; tmp_value = &out->map.items[i].value;
switch (lua_type(L, value_idx)) { switch (lua_type(L, value_idx))
case LUA_TNUMBER: {
tmp_num = lua_tonumber(L, value_idx); case LUA_TNUMBER:
tmp_value->type = RDT_INT; tmp_num = lua_tonumber(L, value_idx);
tmp_value->int_ = tmp_num; tmp_value->type = RDT_INT;
break; tmp_value->int_ = tmp_num;
case LUA_TBOOLEAN: break;
tmp_value->type = RDT_BOOL; case LUA_TBOOLEAN:
tmp_value->bool_ = lua_toboolean(L, value_idx); tmp_value->type = RDT_BOOL;
break; tmp_value->bool_ = lua_toboolean(L, value_idx);
case LUA_TSTRING: break;
tmp_buff = strdup(lua_tostring(L, value_idx)); case LUA_TSTRING:
tmp_value->type = RDT_STRING; tmp_buff = strdup(lua_tostring(L, value_idx));
tmp_value->string.len = strlen(tmp_buff); tmp_value->type = RDT_STRING;
tmp_value->string.buff = tmp_buff; tmp_value->string.len = strlen(tmp_buff);
break; tmp_value->string.buff = tmp_buff;
case LUA_TTABLE: break;
lua_getfield(L, value_idx, "binary"); case LUA_TTABLE:
if (!lua_isstring(L, -1)) { lua_getfield(L, value_idx, "binary");
lua_pop(L, 1); if (!lua_isstring(L, -1))
lua_getfield(L, value_idx, "uint"); {
if (!lua_isnumber(L, -1)) { lua_pop(L, 1);
lua_pop(L, 1); lua_getfield(L, value_idx, "uint");
goto set_nil; if (!lua_isnumber(L, -1))
} else { {
tmp_num = lua_tonumber(L, -1); lua_pop(L, 1);
tmp_value->type = RDT_UINT; goto set_nil;
tmp_value->uint_ = tmp_num; }
lua_pop(L, 1); else
} {
} else { tmp_num = lua_tonumber(L, -1);
tmp_string = lua_tolstring(L, -1, &tmp_len); tmp_value->type = RDT_UINT;
tmp_buff = malloc(tmp_len); tmp_value->uint_ = tmp_num;
memcpy(tmp_buff, tmp_string, tmp_len); lua_pop(L, 1);
tmp_value->type = RDT_BINARY; }
tmp_value->binary.len = tmp_len; }
tmp_value->binary.buff = tmp_buff; else
lua_pop(L, 1); {
} tmp_string = lua_tolstring(L, -1, &tmp_len);
break; tmp_buff = malloc(tmp_len);
default: memcpy(tmp_buff, tmp_string, tmp_len);
tmp_value->type = RDT_BINARY;
tmp_value->binary.len = tmp_len;
tmp_value->binary.buff = tmp_buff;
lua_pop(L, 1);
}
break;
default:
set_nil: set_nil:
tmp_value->type = RDT_NULL; tmp_value->type = RDT_NULL;
} }
out->map.len++; out->map.len++;
} }
lua_pop(L, 1); lua_pop(L, 1);
} }
rv = 0; rv = 0;
return rv; return rv;
} }

View File

@ -21,17 +21,17 @@ static char tmp_error_buff [MAX_ERROR_LEN] = {};
struct buffer struct buffer
{ {
const char *data; const char *data;
size_t len; size_t len;
off_t offset; off_t offset;
}; };
/* Errors */ /* Errors */
static void raise_too_many_arguments(const char **error) static void raise_too_many_arguments(const char **error)
{ {
snprintf(tmp_error_buff, MAX_ERROR_LEN, snprintf(tmp_error_buff, MAX_ERROR_LEN,
"Too many arguments in function call."); "Too many arguments in function call.");
*error = tmp_error_buff; *error = tmp_error_buff;
} }
static void raise_expected_number(off_t where, const char **error) static void raise_expected_number(off_t where, const char **error)
@ -345,11 +345,11 @@ static struct rmsgpack_dom_value q_glob(struct rmsgpack_dom_value input,
static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input, static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
unsigned argc, const struct argument *argv) unsigned argc, const struct argument *argv)
{ {
unsigned i;
struct rmsgpack_dom_value res; struct rmsgpack_dom_value res;
struct rmsgpack_dom_value *value = NULL;
struct argument arg; struct argument arg;
struct rmsgpack_dom_value nil_value; struct rmsgpack_dom_value nil_value;
unsigned i; struct rmsgpack_dom_value *value = NULL;
nil_value.type = RDT_NULL; nil_value.type = RDT_NULL;
res.type = RDT_BOOL; res.type = RDT_BOOL;
@ -477,7 +477,7 @@ static struct buffer parse_string(struct buffer buff,
{ {
const char * str_start; const char * str_start;
char terminator = '\0'; char terminator = '\0';
char c = '\0'; char c = '\0';
(void)c; (void)c;
@ -872,7 +872,7 @@ void *libretrodb_query_compile(libretrodb_t *db,
buff.offset = 0; buff.offset = 0;
*error = NULL; *error = NULL;
buff = chomp(buff); buff = chomp(buff);
if (peek(buff, "{")) if (peek(buff, "{"))
{ {