Skip to content

Commit

Permalink
cachedb_local: Add support for iterating all cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Nov 20, 2024
1 parent cac2517 commit 41aea5f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cachedb/cachedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ typedef struct cachedb_funcs_t {
int expires, int *new_val);
int (*sub) (cachedb_con *con, str *attr, int val,
int expires, int *new_val);
int (*iter_keys) (cachedb_con *con,
int (*kv_func)(const str *key, const str *value));

/* bi-dimensional array will be returned */
int (*raw_query) (cachedb_con *con, str *query, cdb_raw_entry ***reply,
int num_cols, int *num_rows);
Expand Down
7 changes: 5 additions & 2 deletions cachedb/cachedb_cap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ typedef enum {
CACHEDB_CAP_BINARY_VALUE = 1<<5,
CACHEDB_CAP_RAW = 1<<6,
CACHEDB_CAP_TRUNCATE = 1<<7,
CACHEDB_CAP_ITER_KEYS = 1<<8,

CACHEDB_CAP_QUERY = 1<<8,
CACHEDB_CAP_UPDATE = 1<<9,
CACHEDB_CAP_QUERY = 1<<9,
CACHEDB_CAP_UPDATE = 1<<10,
CACHEDB_CAP_COL_ORIENTED = (CACHEDB_CAP_QUERY|CACHEDB_CAP_UPDATE),

CACHEDB_CAP_MAP_GET = 1<<10,
Expand Down Expand Up @@ -85,6 +86,8 @@ static inline int check_cachedb_api(cachedb_engine *cde)
cde->cdb_func.capability |= CACHEDB_CAP_RAW;
if (cde->cdb_func.truncate)
cde->cdb_func.capability |= CACHEDB_CAP_TRUNCATE;
if (cde->cdb_func.iter_keys)
cde->cdb_func.capability |= CACHEDB_CAP_ITER_KEYS;

if (cde->cdb_func.query)
cde->cdb_func.capability |= CACHEDB_CAP_QUERY;
Expand Down
1 change: 1 addition & 0 deletions modules/cachedb_local/cachedb_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ static int mod_init(void)
cde.cdb_func.add = lcache_htable_add;
cde.cdb_func.sub = lcache_htable_sub;
cde.cdb_func.is_replicated = lcache_is_replicated;
cde.cdb_func.iter_keys = lcache_htable_iter_keys;

cde.cdb_func.capability = CACHEDB_CAP_BINARY_VALUE;

Expand Down
31 changes: 31 additions & 0 deletions modules/cachedb_local/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,34 @@ int lcache_htable_fetch_counter(cachedb_con* con,str* attr,int *val)
cdb_slow_queries, cdb_total_queries);
return -2;
}

int lcache_htable_iter_keys(cachedb_con *con,
int (*kv_func)(const str *_k, const str *_v))
{
lcache_t* cache_htable;
lcache_col_t* cache_col;
lcache_entry_t* it;
int i, n = 0;

cache_col = ((lcache_con *)con->data)->col;
if (!cache_col) {
LM_ERR("url <%.*s> does not have any collection associated with!",
con->url.len, con->url.s);
return -1;
}

cache_htable = cache_col->col_htable->htable;

for (i = 0; i < cache_col->size; i++) {
lock_get(&cache_htable[i].lock);

for (it = cache_htable[i].entries; it; it = it->next)
if (kv_func(&it->attr, &it->value) == 0)
n++;

lock_release(&cache_htable[i].lock);
}

/* number of successfully processed keys */
return n;
}
1 change: 1 addition & 0 deletions modules/cachedb_local/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ int lcache_htable_fetch(cachedb_con *con,str* attr, str* val);
int lcache_htable_add(cachedb_con *con,str *attr,int val,int expires,int *new_val);
int lcache_htable_sub(cachedb_con *con,str *attr,int val,int expires,int *new_val);
int lcache_htable_fetch_counter(cachedb_con* con,str* attr,int *val);
int lcache_htable_iter_keys(cachedb_con *con, int (*kv_func)(const str *, const str *));

#endif

0 comments on commit 41aea5f

Please sign in to comment.