Skip to content

Commit

Permalink
cachedb: Add easy-to-use dict append primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Mar 13, 2018
1 parent 610a97a commit 829402a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 5 deletions.
8 changes: 4 additions & 4 deletions cachedb/cachedb.h
Expand Up @@ -74,9 +74,9 @@ typedef struct cachedb_funcs_t {
int (*truncate) (cachedb_con *con);

int (*db_query_trans) (cachedb_con *con, const str *table,
const db_key_t* _k, const db_op_t* _op, const db_val_t* _v,
const db_key_t* _c, int _n, int _nc, const db_key_t _o,
db_res_t** _r);
const db_key_t* _k, const db_op_t* _op, const db_val_t* _v,
const db_key_t* _c, int _n, int _nc, const db_key_t _o,
db_res_t** _r);
int (*db_free_trans) (cachedb_con* con, db_res_t* _r);
int (*db_insert_trans) (cachedb_con *con, const str *table,
const db_key_t* _k, const db_val_t* _v, int _n);
Expand All @@ -97,7 +97,7 @@ typedef struct cachedb_funcs_t {
* @filter: NULL, one or more AND'ed filters for the query.
* @res: Will contain zero or more results.
*
* Return: 0 on success, -1 otherwise. @res is always safe to free.
* Return: 0 on success, -1 otherwise. @res is always safe to clean.
*/
int (*query) (cachedb_con *con, const cdb_filter_t *filter, cdb_res_t *res);

Expand Down
90 changes: 90 additions & 0 deletions cachedb/cachedb_dict.c
Expand Up @@ -22,6 +22,96 @@

#include "cachedb_types.h"

int cdb_dict_add_str(cdb_dict_t *dest, const char *key, int key_len,
const str *val)
{
cdb_key_t _key;
cdb_pair_t *pair;

_key.name.s = (char *)key;
_key.name.len = key_len;
_key.is_pk = 0;

pair = cdb_mk_pair(&_key, NULL);
if (!pair) {
LM_ERR("oom\n");
return -1;
}

pair->val.type = CDB_STR;
pair->val.val.st = *val;

cdb_dict_add(pair, dest);
return 0;
}

int cdb_dict_add_int32(cdb_dict_t *dest, const char *key, int key_len,
uint32_t v)
{
cdb_key_t _key;
cdb_pair_t *pair;

_key.name.s = (char *)key;
_key.name.len = key_len;
_key.is_pk = 0;

pair = cdb_mk_pair(&_key, NULL);
if (!pair) {
LM_ERR("oom\n");
return -1;
}

pair->val.type = CDB_INT32;
pair->val.val.i32 = v;

cdb_dict_add(pair, dest);
return 0;
}

int cdb_dict_add_int64(cdb_dict_t *dest, const char *key, int key_len,
uint64_t v)
{
cdb_key_t _key;
cdb_pair_t *pair;

_key.name.s = (char *)key;
_key.name.len = key_len;
_key.is_pk = 0;

pair = cdb_mk_pair(&_key, NULL);
if (!pair) {
LM_ERR("oom\n");
return -1;
}

pair->val.type = CDB_INT64;
pair->val.val.i64 = v;

cdb_dict_add(pair, dest);
return 0;
}

int cdb_dict_add_null(cdb_dict_t *dest, const char *key, int key_len)
{
cdb_key_t _key;
cdb_pair_t *pair;

_key.name.s = (char *)key;
_key.name.len = key_len;
_key.is_pk = 0;

pair = cdb_mk_pair(&_key, NULL);
if (!pair) {
LM_ERR("oom\n");
return -1;
}

pair->val.type = CDB_NULL;

cdb_dict_add(pair, dest);
return 0;
}

void cdb_dict_add(struct cdb_pair *pair, cdb_dict_t *dict)
{
list_add(&pair->list, dict);
Expand Down
19 changes: 19 additions & 0 deletions cachedb/cachedb_dict.h
Expand Up @@ -36,6 +36,25 @@ static inline void cdb_dict_init(cdb_dict_t *dict)
INIT_LIST_HEAD(dict);
}

int cdb_dict_add_str(cdb_dict_t *dest, const char *key, int key_len,
const str *val);
#define CDB_DICT_ADD_STR(dest, key, val) \
cdb_dict_add_str(dest, key, strlen(key), val)

int cdb_dict_add_int32(cdb_dict_t *dest, const char *key, int key_len,
uint32_t v);
#define CDB_DICT_ADD_INT32(dest, key, val) \
cdb_dict_add_int32(dest, key, strlen(key), val)

int cdb_dict_add_int64(cdb_dict_t *dest, const char *key, int key_len,
uint64_t v);
#define CDB_DICT_ADD_INT64(dest, key, val) \
cdb_dict_add_int64(dest, key, strlen(key), val)

int cdb_dict_add_null(cdb_dict_t *dest, const char *key, int key_len);
#define CDB_DICT_ADD_NULL(dest, key) \
cdb_dict_add_null(dest, key, strlen(key))

void cdb_dict_add(struct cdb_pair *pair, cdb_dict_t *dict);
void cdb_free_entries(cdb_dict_t *dict);

Expand Down
2 changes: 1 addition & 1 deletion cachedb/cachedb_types.h
Expand Up @@ -89,7 +89,7 @@ typedef struct cdb_pair {
} cdb_pair_t;

typedef struct {
cdb_dict_t dict;
cdb_dict_t dict; /* list of cdb_pair_t */

struct list_head list;
} cdb_row_t;
Expand Down

0 comments on commit 829402a

Please sign in to comment.