Skip to content

Commit

Permalink
htable: export sht_is_null() function to kemi
Browse files Browse the repository at this point in the history
  - return >0 if htable or item not found
  - return <0 if item found or htable defined with default value
  • Loading branch information
miconda committed Jan 28, 2020
1 parent 1e3f466 commit c943fdf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/modules/htable/ht_api.c
Expand Up @@ -851,6 +851,46 @@ ht_cell_t* ht_cell_pkg_copy(ht_t *ht, str *name, ht_cell_t *old)
return NULL;
}

int ht_cell_exists(ht_t *ht, str *name)
{
unsigned int idx;
unsigned int hid;
ht_cell_t *it;

if(ht==NULL || ht->entries==NULL)
return 0;

hid = ht_compute_hash(name);

idx = ht_get_entry(hid, ht->htsize);

/* head test and return */
if(ht->entries[idx].first==NULL)
return 0;

ht_slot_lock(ht, idx);
it = ht->entries[idx].first;
while(it!=NULL && it->cellid < hid)
it = it->next;
while(it!=NULL && it->cellid == hid) {
if(name->len==it->name.len
&& strncmp(name->s, it->name.s, name->len)==0) {
/* found */
if(ht->htexpire>0 && it->expire!=0 && it->expire<time(NULL)) {
/* entry has expired */
ht_slot_unlock(ht, idx);
return 0;
}
ht_slot_unlock(ht, idx);
return 1;
}
it = it->next;
}
ht_slot_unlock(ht, idx);
return 0;
}


int ht_dbg(void)
{
int i;
Expand Down
1 change: 1 addition & 0 deletions src/modules/htable/ht_api.h
Expand Up @@ -92,6 +92,7 @@ int ht_destroy(void);
int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode);
int ht_del_cell(ht_t *ht, str *name);
ht_cell_t* ht_cell_value_add(ht_t *ht, str *name, int val, ht_cell_t *old);
int ht_cell_exists(ht_t *ht, str *name);

int ht_dbg(void);
ht_cell_t* ht_cell_pkg_copy(ht_t *ht, str *name, ht_cell_t *old);
Expand Down
30 changes: 30 additions & 0 deletions src/modules/htable/htable.c
Expand Up @@ -998,6 +998,31 @@ static sr_kemi_xval_t* ki_ht_getw(sip_msg_t *msg, str *htname, str *itname)
}


/**
*
*/
static int ki_ht_is_null(sip_msg_t *msg, str *htname, str *itname)
{
ht_t *ht = NULL;

/* find the hash htable */
ht = ht_get_table(htname);
if (ht == NULL) {
return 2;
}

if(ht->flags==PV_VAL_INT) {
/* htable defined with default value */
return -2;
}

if(ht_cell_exists(ht, itname)>0) {
return -1;
}

return 1;
}

/**
*
*/
Expand Down Expand Up @@ -1816,6 +1841,11 @@ static sr_kemi_t sr_kemi_htable_exports[] = {
{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE }
},
{ str_init("htable"), str_init("sht_is_null"),
SR_KEMIP_INT, ki_ht_is_null,
{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
},

{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
};
Expand Down

0 comments on commit c943fdf

Please sign in to comment.