Exemple #1
0
/*! \brief RPC htable.seti command to set one item to integer value */
static void htable_rpc_seti(rpc_t* rpc, void* c) {
	str htname, keyname;
	int_str keyvalue;
	ht_t *ht;

	if (rpc->scan(c, "SS.d", &htname, &keyname, &keyvalue.n) < 3) {
		rpc->fault(c, 500,
				"Not enough parameters (htable name, key name and value)");
		return;
	}

	/* Find the htable */
	ht = ht_get_table(&htname);
	if (!ht) {
		rpc->fault(c, 500, "No such htable");
		return;
	}

	if (ht->dmqreplicate>0 && ht_dmq_replicate_action(HT_DMQ_SET_CELL, &ht->name, &keyname, 0, &keyvalue, 1)!=0) {
		LM_ERR("dmq relication failed\n");
	}
	
	if(ht_set_cell(ht, &keyname, 0, &keyvalue, 1)!=0)
	{
		LM_ERR("cannot set $sht(%.*s=>%.*s)\n", htname.len, htname.s,
				keyname.len, keyname.s);
		rpc->fault(c, 500, "Failed to set the item");
		return;
	}

	return;
}
Exemple #2
0
int ht_api_set_cell(str *hname, str *name, int type,
		int_str *val, int mode)
{
	ht_t* ht;
	ht = ht_get_table(hname);
	if(ht==NULL)
		return -1;
	return ht_set_cell(ht, name, type, val, mode);
}
Exemple #3
0
int ht_dmq_replay_action(ht_dmq_action_t action, str* htname, str* cname, int type, int_str* val, int mode) {

    ht_t* ht;
    ht = ht_get_table(htname);
    if(ht==NULL) {
        LM_ERR("unable to get table\n");
        return -1;
    }

    LM_DBG("replaying action %d on %.*s=>%.*s...\n", action, htname->len, htname->s, cname->len, cname->s);

    if (action==HT_DMQ_SET_CELL) {
        return ht_set_cell(ht, cname, type, val, mode);
    } else if (action==HT_DMQ_SET_CELL_EXPIRE) {
        return ht_set_cell_expire(ht, cname, 0, val);
    } else if (action==HT_DMQ_DEL_CELL) {
        return ht_del_cell(ht, cname);
    } else if (action==HT_DMQ_RM_CELL_RE) {
        return ht_rm_cell_re(&val->s, ht, mode);
    } else {
        LM_ERR("unrecognized action");
        return -1;
    }
}
Exemple #4
0
int pv_set_ht_cell(struct sip_msg* msg, pv_param_t *param,
		int op, pv_value_t *val)
{
	str htname;
	int_str isval;
	ht_pv_t *hpv;

	hpv = (ht_pv_t*)param->pvn.u.dname;

	if(hpv->ht==NULL)
		hpv->ht = ht_get_table(&hpv->htname);
	if(hpv->ht==NULL)
		return -1;

	if(pv_printf_s(msg, hpv->pve, &htname)!=0)
	{
		LM_ERR("cannot get $sht name\n");
		return -1;
	}
	LM_DBG("set value for $sht(%.*s=>%.*s)\n", hpv->htname.len, hpv->htname.s,
			htname.len, htname.s);
	if((val==NULL) || (val->flags&PV_VAL_NULL))
	{
		/* delete it */
		if (hpv->ht->dmqreplicate>0
				&& ht_dmq_replicate_action(HT_DMQ_DEL_CELL, &hpv->htname,
					&htname, 0, NULL, 0)!=0) {
			LM_ERR("dmq relication failed\n");
		}
		ht_del_cell(hpv->ht, &htname);
		return 0;
	}

	if(val->flags&PV_TYPE_INT)
	{
		isval.n = val->ri;
		if (hpv->ht->dmqreplicate>0
				&& ht_dmq_replicate_action(HT_DMQ_SET_CELL, &hpv->htname,
					&htname, 0, &isval, 1)!=0) {
			LM_ERR("dmq relication failed\n");
		}
		if(ht_set_cell(hpv->ht, &htname, 0, &isval, 1)!=0)
		{
			LM_ERR("cannot set $sht(%.*s)\n", htname.len, htname.s);
			return -1;
		}
	} else {
		isval.s = val->rs;
		if (hpv->ht->dmqreplicate>0
				&& ht_dmq_replicate_action(HT_DMQ_SET_CELL, &hpv->htname,
					&htname, AVP_VAL_STR, &isval, 1)!=0) {
			LM_ERR("dmq relication failed\n");
		}
		if(ht_set_cell(hpv->ht, &htname, AVP_VAL_STR, &isval, 1)!=0)
		{
			LM_ERR("cannot set $sht(%.*s)\n", htname.len, htname.s);
			return -1;
		}
	}
	return 0;
}
Exemple #5
0
/**
 * load content of a db table in hash table
 */
int ht_db_load_table(ht_t *ht, str *dbtable, int mode)
{
	db_key_t db_cols[5] = {&ht_db_name_column, &ht_db_ktype_column,
		&ht_db_vtype_column, &ht_db_value_column, &ht_db_expires_column};
	db_key_t db_ord = &ht_db_name_column;
	db1_res_t* db_res = NULL;
	str kname;
	str pname;
	str hname;
	str kvalue;
	int ktype;
	int vtype;
	int last_ktype;
	int n;
	int_str val;
	int_str expires;
	int i;
	int ret;
	int cnt;
	int now;
	int ncols;

	if(ht_db_con==NULL)
	{
		LM_ERR("no db connection\n");
		return -1;
	}

	if (ht_dbf.use_table(ht_db_con, dbtable) < 0)
	{
		LM_ERR("failed to use_table\n");
		return -1;
	}

	LM_DBG("=============== loading hash table [%.*s] from database [%.*s]\n",
			ht->name.len, ht->name.s, dbtable->len, dbtable->s);
	cnt = 0;
	ncols = 4;
	if(ht->htexpire > 0 && ht_db_expires_flag!=0)
		ncols = 5;

	if (DB_CAPABILITY(ht_dbf, DB_CAP_FETCH)) {
		if(ht_dbf.query(ht_db_con,0,0,0,db_cols,0,ncols,db_ord,0) < 0)
		{
			LM_ERR("Error while querying db\n");
			return -1;
		}
		if(ht_dbf.fetch_result(ht_db_con, &db_res, ht_fetch_rows)<0)
		{
			LM_ERR("Error while fetching result\n");
			if (db_res)
				ht_dbf.free_result(ht_db_con, db_res);
			goto error;
		} else {
			if(RES_ROW_N(db_res)==0)
			{
				LM_DBG("Nothing to be loaded in hash table\n");
				return 0;
			}
		}
	} else {
		if((ret=ht_dbf.query(ht_db_con, NULL, NULL, NULL, db_cols,
				0, ncols, db_ord, &db_res))!=0
			|| RES_ROW_N(db_res)<=0 )
		{
			if( ret==0)
			{
				ht_dbf.free_result(ht_db_con, db_res);
				return 0;
			} else {
				goto error;
			}
		}
	}

	pname.len = 0;
	pname.s = "";
	n = 0;
	last_ktype = 0;
	now = (int)time(NULL);
	do {
		for(i=0; i<RES_ROW_N(db_res); i++)
		{
			/* not NULL values enforced in table definition ?!?! */
			kname.s = (char*)(RES_ROWS(db_res)[i].values[0].val.string_val);
			if(kname.s==NULL) {
				LM_ERR("null key in row %d\n", i);
				goto error;
			}
			kname.len = strlen(kname.s);

			expires.n = 0;
			if(ht->htexpire > 0 && ht_db_expires_flag!=0) {
				expires.n = RES_ROWS(db_res)[i].values[4].val.int_val;
				if (expires.n > 0 && expires.n < now) {
					LM_DBG("skipping expired entry [%.*s] (%d)\n", kname.len,
							kname.s, expires.n-now);
					continue;
				}
			}

			cnt++;
			ktype = RES_ROWS(db_res)[i].values[1].val.int_val;
			if(last_ktype==1)
			{
				if(pname.len>0
						&& (pname.len!=kname.len
							|| strncmp(pname.s, kname.s, pname.len)!=0))
				{
					/* new key name, last was an array => add its size */
					snprintf(ht_name_buf, HT_NAME_BUF_SIZE, "%.*s%.*s",
						pname.len, pname.s, ht_array_size_suffix.len,
						ht_array_size_suffix.s);
					hname.s = ht_name_buf;
					hname.len = strlen(ht_name_buf);
					val.n = n;

					if(ht_set_cell(ht, &hname, 0, &val, mode))
					{
						LM_ERR("error adding array size to hash table.\n");
						goto error;
					}
					pname.len = 0;
					pname.s = "";
					n = 0;
				}
			}
			last_ktype = ktype;
			pname = kname;
			if(ktype==1)
			{
				snprintf(ht_name_buf, HT_NAME_BUF_SIZE, "%.*s[%d]",
						kname.len, kname.s, n);
				hname.s = ht_name_buf;
				hname.len = strlen(ht_name_buf);
				n++;
			} else {
				hname = kname;
			}
			vtype = RES_ROWS(db_res)[i].values[2].val.int_val;
			kvalue.s = (char*)(RES_ROWS(db_res)[i].values[3].val.string_val);
			if(kvalue.s==NULL) {
				LM_ERR("null value in row %d\n", i);
				goto error;
			}
			kvalue.len = strlen(kvalue.s);

			/* add to hash */
			if(vtype==1)
				str2sint(&kvalue, &val.n);
			else
				val.s = kvalue;
				
			if(ht_set_cell(ht, &hname, (vtype)?0:AVP_VAL_STR, &val, mode))
			{
				LM_ERR("error adding to hash table\n");
				goto error;
			}

			/* set expiry */
			if (ht->htexpire > 0 && expires.n > 0) {
				expires.n -= now;
				if(ht_set_cell_expire(ht, &hname, 0, &expires)) {
					LM_ERR("error setting expires to hash entry [%*.s]\n", hname.len, hname.s);
					goto error;
				}
			}
	 	}
		if (DB_CAPABILITY(ht_dbf, DB_CAP_FETCH)) {
			if(ht_dbf.fetch_result(ht_db_con, &db_res, ht_fetch_rows)<0) {
				LM_ERR("Error while fetching!\n");
				goto error;
			}
		} else {
			break;
		}
	}  while(RES_ROW_N(db_res)>0);

	if(last_ktype==1)
	{
		snprintf(ht_name_buf, HT_NAME_BUF_SIZE, "%.*s%.*s",
			pname.len, pname.s, ht_array_size_suffix.len,
			ht_array_size_suffix.s);
		hname.s = ht_name_buf;
		hname.len = strlen(ht_name_buf);
		val.n = n;
		if(ht_set_cell(ht, &hname, 0, &val, mode))
		{
			LM_ERR("error adding array size to hash table.\n");
			goto error;
		}
	}

	ht_dbf.free_result(ht_db_con, db_res);
	LM_DBG("loaded %d values in hash table\n", cnt);

	return 0;
error:
	ht_dbf.free_result(ht_db_con, db_res);
	return -1;

}