Module: kamailio
Branch: master
Commit: af1c14597d9da71adbbaba265d1e859f9120fb89
URL: 
https://github.com/kamailio/kamailio/commit/af1c14597d9da71adbbaba265d1e859f9120fb89

Author: Daniel-Constantin Mierla <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2025-12-05T12:45:39+01:00

mtree: added mode attribute for mtree definition

- if set to 1, the tree is in-memory only, without (re)loading
record from database. Items can be added with modparam 'item'

---

Modified: src/modules/mtree/mtree.c
Modified: src/modules/mtree/mtree.h
Modified: src/modules/mtree/mtree_mod.c

---

Diff:  
https://github.com/kamailio/kamailio/commit/af1c14597d9da71adbbaba265d1e859f9120fb89.diff
Patch: 
https://github.com/kamailio/kamailio/commit/af1c14597d9da71adbbaba265d1e859f9120fb89.patch

---

diff --git a/src/modules/mtree/mtree.c b/src/modules/mtree/mtree.c
index ac6131890f0..39c625ad9cc 100644
--- a/src/modules/mtree/mtree.c
+++ b/src/modules/mtree/mtree.c
@@ -120,7 +120,7 @@ int mt_init_list_head(void)
  *
  */
 m_tree_t *mt_init_tree(
-               str *tname, str *dbtable, str *scols, int type, int multi)
+               str *tname, str *dbtable, str *scols, int type, int multi, int 
mode)
 {
        m_tree_t *pt = NULL;
        int i;
@@ -133,6 +133,7 @@ m_tree_t *mt_init_tree(
        }
        memset(pt, 0, sizeof(m_tree_t));
 
+       pt->mode = mode;
        pt->type = type;
        pt->multi = multi;
        pt->reload_time = (uint64_t)time(NULL);
@@ -154,7 +155,9 @@ m_tree_t *mt_init_tree(
                return NULL;
        }
        memset(pt->dbtable.s, 0, 1 + dbtable->len);
-       memcpy(pt->dbtable.s, dbtable->s, dbtable->len);
+       if(dbtable->len > 0) {
+               memcpy(pt->dbtable.s, dbtable->s, dbtable->len);
+       }
        pt->dbtable.len = dbtable->len;
 
        if(scols != NULL && scols->s != NULL && scols->len > 0) {
@@ -780,6 +783,9 @@ int mt_table_spec(char *val)
                                pit->body.s);
                if(pit->name.len == 4 && strncasecmp(pit->name.s, "name", 4) == 
0) {
                        tmp.tname = pit->body;
+               } else if(pit->name.len == 4
+                                 && strncasecmp(pit->name.s, "mode", 4) == 0) {
+                       str2sint(&pit->body, &tmp.mode);
                } else if(pit->name.len == 4
                                  && strncasecmp(pit->name.s, "type", 4) == 0) {
                        str2sint(&pit->body, &tmp.type);
@@ -799,9 +805,11 @@ int mt_table_spec(char *val)
                LM_ERR("invalid mtree name\n");
                goto error;
        }
-       if(tmp.dbtable.s == NULL) {
-               LM_ERR("no db table provided\n");
-               goto error;
+       if(tmp.mode == 0) {
+               if(tmp.dbtable.s == NULL) {
+                       LM_ERR("no db table provided\n");
+                       goto error;
+               }
        }
        if((tmp.type != 0) && (tmp.type != 1) && (tmp.type != 2)) {
                LM_ERR("unknown tree type <%d>\n", tmp.type);
@@ -839,8 +847,8 @@ int mt_table_spec(char *val)
        if(it == NULL || str_strcmp(&it->tname, &tmp.tname) > 0) {
                LM_DBG("adding new tname [%s]\n", tmp.tname.s);
 
-               ndl = mt_init_tree(
-                               &tmp.tname, &tmp.dbtable, &tmp.scols[0], 
tmp.type, tmp.multi);
+               ndl = mt_init_tree(&tmp.tname, &tmp.dbtable, &tmp.scols[0], 
tmp.type,
+                               tmp.multi, tmp.mode);
                if(ndl == NULL) {
                        LM_ERR("cannot init the tree [%.*s]\n", tmp.tname.len, 
tmp.tname.s);
                        goto error;
@@ -887,7 +895,7 @@ m_tree_t *mt_add_tree(m_tree_t **dpt, str *tname, str 
*dbtable, str *cols,
        if(it == NULL || str_strcmp(&it->tname, tname) > 0) {
                LM_DBG("adding new tname [%s]\n", tname->s);
 
-               ndl = mt_init_tree(tname, dbtable, cols, type, multi);
+               ndl = mt_init_tree(tname, dbtable, cols, type, multi, 0);
                if(ndl == NULL) {
                        LM_ERR("no more shm memory\n");
                        return NULL;
diff --git a/src/modules/mtree/mtree.h b/src/modules/mtree/mtree.h
index 7bd87373e12..8b22076538c 100644
--- a/src/modules/mtree/mtree.h
+++ b/src/modules/mtree/mtree.h
@@ -70,6 +70,7 @@ typedef struct _m_tree
 {
        str tname;
        str dbtable;
+       int mode;
        int type;
        int multi;
        int ncols;
@@ -95,7 +96,7 @@ is_t *mt_get_tvalue(m_tree_t *pt, str *tomatch, int *len);
 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, str *scols, int type, int multi);
+               str *tname, str *dbtable, str *scols, int type, int multi, int 
mode);
 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);
diff --git a/src/modules/mtree/mtree_mod.c b/src/modules/mtree/mtree_mod.c
index 6b85c3c6cd1..15cd24b990d 100644
--- a/src/modules/mtree/mtree_mod.c
+++ b/src/modules/mtree/mtree_mod.c
@@ -529,6 +529,11 @@ static int mt_load_db(m_tree_t *pt)
        m_tree_t *old_tree = NULL;
        mt_node_t *bk_head = NULL;
 
+       if(pt->mode == 1) {
+               LM_DBG("skip loading db records - in-memory only tree: 
[%.*s]\n",
+                               pt->tname.len, pt->tname.s);
+               return 0;
+       }
        if(pt->ncols > 0) {
                for(c = 0; c < pt->ncols; c++) {
                        db_cols[c] = &pt->scols[c];

_______________________________________________
Kamailio - Development Mailing List -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to