Module: sip-router
Branch: master
Commit: 25ee7ea17b6553ce44eda6c0665ff80cfc41db35
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=25ee7ea…
Author: Juha Heinanen <jh(a)tutpro.com>
Committer: Juha Heinanen <jh(a)tutpro.com>
Date: Fri Nov 1 21:27:31 2013 +0200
modules/mtree: added 'multi' param to mtree definition
- new 'multi' param makes it possible to store both integer and string
typed mtrees into single db table
---
modules/mtree/README | 8 ++++++--
modules/mtree/doc/mtree_admin.xml | 9 +++++++--
modules/mtree/mtree.c | 23 ++++++++++++++++++-----
modules/mtree/mtree.h | 5 +++--
modules/mtree/mtree_mod.c | 36 +++++++++++++++++++++++++-----------
5 files changed, 59 insertions(+), 22 deletions(-)
diff --git a/modules/mtree/README b/modules/mtree/README
index d548e89..c35fb7c 100644
--- a/modules/mtree/README
+++ b/modules/mtree/README
@@ -186,13 +186,17 @@ modparam("mtree", "db_table",
"mymtrees")
3.3. mtree (string)
- Definition of memory tree
+ Definition of memory tree with parameters name, dbtable, type, and
+ multi. Name is name of the tree, dbtable is name of dbtable where tree
+ is stored, type is type of tree elements (0 = string, 2 = integer), and
+ multi tells if dbtable can contain more than one tree (0 = one tree, 1
+ = more than one tree identified by tname column).
Default value is "none".
Example 1.3. Set mtree parameter
...
-modparam("mtree", "mtree",
"name=mytable;dbtable=routes;type=0;")
+modparam("mtree", "mtree",
"name=mytree;dbtable=routes;type=0;multi=1")
...
3.4. tname_column (string)
diff --git a/modules/mtree/doc/mtree_admin.xml b/modules/mtree/doc/mtree_admin.xml
index 0d81e72..0d8fe33 100644
--- a/modules/mtree/doc/mtree_admin.xml
+++ b/modules/mtree/doc/mtree_admin.xml
@@ -97,7 +97,12 @@ modparam("mtree", "db_table",
"mymtrees")
<section>
<title><varname>mtree</varname> (string)</title>
<para>
- Definition of memory tree
+ Definition of memory tree with parameters name, dbtable,
+ type, and multi. Name is name of the tree, dbtable is name
+ of dbtable where tree is stored, type is type of tree elements
+ (0 = string, 2 = integer), and multi tells if dbtable can
+ contain more than one tree (0 = one tree, 1 = more than
+ one tree identified by tname column).
</para>
<para>
<emphasis>
@@ -108,7 +113,7 @@ modparam("mtree", "db_table",
"mymtrees")
<title>Set <varname>mtree</varname> parameter</title>
<programlisting format="linespecific">
...
-modparam("mtree", "mtree",
"name=mytable;dbtable=routes;type=0;")
+modparam("mtree", "mtree",
"name=mytree;dbtable=routes;type=0;multi=1")
...
</programlisting>
</example>
diff --git a/modules/mtree/mtree.c b/modules/mtree/mtree.c
index 6587909..2f8550c 100644
--- a/modules/mtree/mtree.c
+++ b/modules/mtree/mtree.c
@@ -98,7 +98,7 @@ int mt_init_list_head(void)
/**
*
*/
-m_tree_t* mt_init_tree(str* tname, str *dbtable, int type)
+m_tree_t* mt_init_tree(str* tname, str *dbtable, int type, int multi)
{
m_tree_t *pt = NULL;
@@ -111,6 +111,7 @@ m_tree_t* mt_init_tree(str* tname, str *dbtable, int type)
memset(pt, 0, sizeof(m_tree_t));
pt->type = type;
+ pt->multi = multi;
pt->tname.s = (char*)shm_malloc((1+tname->len)*sizeof(char));
if(pt->tname.s==NULL)
{
@@ -156,6 +157,9 @@ int mt_add_to_tree(m_tree_t *pt, str *sp, str *svalue)
return -1;
}
+ LM_DBG("adding to tree <%.*s> of type <%d>\n", pt->tname.len,
+ pt->tname.s, pt->type);
+
if ((pt->type == MT_TREE_IVAL) && (str2sint(svalue, &ivalue) != 0)) {
LM_ERR("bad integer string <%.*s>\n", svalue->len, svalue->s);
return -1;
@@ -720,6 +724,9 @@ int mt_table_spec(char* val)
} else if(pit->name.len==4
&& strncasecmp(pit->name.s, "type", 4)==0) {
str2sint(&pit->body, &tmp.type);
+ } else if(pit->name.len==5
+ && strncasecmp(pit->name.s, "multi", 5)==0) {
+ str2sint(&pit->body, &tmp.multi);
} else if(pit->name.len==7
&& strncasecmp(pit->name.s, "dbtable", 7)==0) {
tmp.dbtable = pit->body;
@@ -740,7 +747,11 @@ int mt_table_spec(char* val)
LM_ERR("unknown tree type <%d>\n", tmp.type);
goto error;
}
-
+ if ((tmp.multi != 0) && (tmp.multi != 1)) {
+ LM_ERR("unknown multi value <%d>\n", tmp.multi);
+ goto error;
+ }
+
/* check for same tree */
if(_ptree == 0)
{
@@ -773,7 +784,8 @@ int mt_table_spec(char* val)
{
LM_DBG("adding new tname [%s]\n", tmp.tname.s);
- ndl = mt_init_tree(&tmp.tname, &tmp.dbtable, tmp.type);
+ ndl = mt_init_tree(&tmp.tname, &tmp.dbtable, tmp.type,
+ tmp.multi);
if(ndl==NULL)
{
LM_ERR("no more shm memory\n");
@@ -797,7 +809,8 @@ error:
return -1;
}
-m_tree_t *mt_add_tree(m_tree_t **dpt, str *tname, str *dbtable, int type)
+m_tree_t *mt_add_tree(m_tree_t **dpt, str *tname, str *dbtable, int type,
+ int multi)
{
m_tree_t *it = NULL;
m_tree_t *prev = NULL;
@@ -824,7 +837,7 @@ m_tree_t *mt_add_tree(m_tree_t **dpt, str *tname, str *dbtable, int
type)
{
LM_DBG("adding new tname [%s]\n", tname->s);
- ndl = mt_init_tree(tname, dbtable, type);
+ ndl = mt_init_tree(tname, dbtable, type, multi);
if(ndl==NULL)
{
LM_ERR("no more shm memory\n");
diff --git a/modules/mtree/mtree.h b/modules/mtree/mtree.h
index 69d8830..3a33db8 100644
--- a/modules/mtree/mtree.h
+++ b/modules/mtree/mtree.h
@@ -66,6 +66,7 @@ typedef struct _m_tree
str tname;
str dbtable;
int type;
+ int multi;
unsigned int nrnodes;
unsigned int nritems;
unsigned int memsize;
@@ -84,7 +85,7 @@ is_t* mt_get_tvalue(m_tree_t *pt, str *tomatch);
int mt_match_prefix(struct sip_msg *msg, m_tree_t *pt,
str *tomatch, int mode);
-m_tree_t* mt_init_tree(str* tname, str* dbtable, int type);
+m_tree_t* mt_init_tree(str* tname, str* dbtable, int type, int multi);
void mt_free_tree(m_tree_t *pt);
int mt_print_tree(m_tree_t *pt);
void mt_free_node(mt_node_t *pn, int type);
@@ -100,7 +101,7 @@ int mt_defined_trees(void);
m_tree_t *mt_swap_list_head(m_tree_t *ntree);
int mt_init_list_head(void);
m_tree_t *mt_add_tree(m_tree_t **dpt, str *tname, str *dbtable,
- int type);
+ int type, int multi);
#endif
diff --git a/modules/mtree/mtree_mod.c b/modules/mtree/mtree_mod.c
index 0f4348c..9099d5b 100644
--- a/modules/mtree/mtree_mod.c
+++ b/modules/mtree/mtree_mod.c
@@ -120,7 +120,7 @@ static struct mi_root* mt_mi_reload(struct mi_root*, void* param);
static struct mi_root* mt_mi_list(struct mi_root*, void* param);
static struct mi_root* mt_mi_summary(struct mi_root*, void* param);
-static int mt_load_db(str *tname);
+static int mt_load_db(m_tree_t *pt);
static int mt_load_db_trees();
static cmd_export_t cmds[]={
@@ -291,8 +291,11 @@ static int mod_init(void)
while(pt!=NULL)
{
+ LM_DBG("loading from tree <%.*s>\n",
+ pt->tname.len, pt->tname.s);
+
/* loading all information from database */
- if(mt_load_db(&pt->tname)!=0)
+ if(mt_load_db(pt)!=0)
{
LM_ERR("cannot load info from database\n");
goto error1;
@@ -488,9 +491,12 @@ error:
}
-static int mt_load_db(str *tname)
+static int mt_load_db(m_tree_t *pt)
{
db_key_t db_cols[3] = {&tprefix_column, &tvalue_column};
+ db_key_t key_cols[1];
+ db_op_t op[1] = {OP_EQ};
+ db_val_t vals[1];
str tprefix, tvalue;
db1_res_t* db_res = NULL;
int i, ret;
@@ -498,16 +504,22 @@ static int mt_load_db(str *tname)
m_tree_t *old_tree = NULL;
mt_node_t *bk_head = NULL;
+ key_cols[0] = &tname_column;
+ VAL_TYPE(vals) = DB1_STRING;
+ VAL_NULL(vals) = 0;
+ VAL_STRING(vals) = pt->tname.s;
+
if(db_con==NULL)
{
LM_ERR("no db connection\n");
return -1;
}
- old_tree = mt_get_tree(tname);
+ old_tree = mt_get_tree(&(pt->tname));
if(old_tree==NULL)
{
- LM_ERR("tree definition not found [%.*s]\n", tname->len, tname->s);
+ LM_ERR("tree definition not found [%.*s]\n", pt->tname.len,
+ pt->tname.s);
return -1;
}
memcpy(&new_tree, old_tree, sizeof(m_tree_t));
@@ -521,7 +533,8 @@ static int mt_load_db(str *tname)
}
if (DB_CAPABILITY(mt_dbf, DB_CAP_FETCH)) {
- if(mt_dbf.query(db_con, 0, 0, 0, db_cols, 0, 2, 0, 0) < 0)
+ if(mt_dbf.query(db_con, key_cols, op, vals, db_cols, pt->multi,
+ 2, 0, 0) < 0)
{
LM_ERR("Error while querying db\n");
return -1;
@@ -539,8 +552,8 @@ static int mt_load_db(str *tname)
}
}
} else {
- if((ret=mt_dbf.query(db_con, NULL, NULL, NULL, db_cols,
- 0, 2, 0, &db_res))!=0
+ if((ret=mt_dbf.query(db_con, key_cols, op, vals, db_cols,
+ pt->multi, 2, 0, &db_res))!=0
|| RES_ROW_N(db_res)<=0 )
{
mt_dbf.free_result(db_con, db_res);
@@ -697,7 +710,8 @@ static int mt_load_db_trees()
LM_ERR("Error - bad values in db\n");
continue;
}
- new_tree = mt_add_tree(&new_head, &tname, &db_table, _mt_tree_type);
+ new_tree = mt_add_tree(&new_head, &tname, &db_table,
+ _mt_tree_type, 0);
if(new_tree==NULL)
{
LM_ERR("New tree cannot be initialized\n");
@@ -797,7 +811,7 @@ static struct mi_root* mt_mi_reload(struct mi_root *cmd_tree, void
*param)
&& strncmp(pt->tname.s, tname.s, tname.len)==0))
{
/* re-loading table from database */
- if(mt_load_db(&pt->tname)!=0)
+ if(mt_load_db(pt)!=0)
{
LM_ERR("cannot re-load info from database\n");
goto error;
@@ -1081,7 +1095,7 @@ void rpc_mtree_reload(rpc_t* rpc, void* c)
&& strncmp(pt->tname.s, tname.s, tname.len)==0))
{
/* re-loading table from database */
- if(mt_load_db(&pt->tname)!=0)
+ if(mt_load_db(pt)!=0)
{
LM_ERR("cannot re-load mtree from database\n");
goto error;