浏览代码

usrloc: adding params

db_load: enable/disable loading from the database on mod_init
db_insert_update: insert into table, update on duplicate key
Julien Chavanton 8 年之前
父节点
当前提交
5d14f5cada

+ 42 - 0
src/modules/usrloc/doc/usrloc_admin.xml

@@ -687,6 +687,48 @@ modparam("usrloc", "db_mode", 2)
 		</example>
 	</section>
 
+	<section id="usrloc.p.db_load">
+		<title><varname>db_load</varname> (integer)</title>
+		<para>
+		Determine if the usrloc module should load contacts from the database storage during module initialization
+		A value of 0 disable the loading from the database, this parameter is ignored if db_mode 4 is set
+		</para>
+		<para>
+		<emphasis>
+			Default value is 1.
+		</emphasis>
+		</para>
+		<example>
+		<title>Set <varname>db_load</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("usrloc", "db_load", "0")
+...
+		</programlisting>
+		</example>
+	</section>
+
+	<section id="usrloc.p.db_insert_update">
+		<title><varname>db_insert_update</varname> (integer)</title>
+		<para>
+		Determine if the usrloc module should do an update when a duplicate key is found while inserting
+		A value of 1 will activate update on duplicate key
+		</para>
+		<para>
+		<emphasis>
+			Default value is 0.
+		</emphasis>
+		</para>
+		<example>
+		<title>Set <varname>db_insert_update</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("usrloc", "db_insert_update", "1")
+...
+		</programlisting>
+		</example>
+	</section>
+
 	<section id="usrloc.p.matching_mode">
 		<title><varname>matching_mode</varname> (integer)</title>
 		<para>

+ 12 - 4
src/modules/usrloc/ucontact.c

@@ -679,10 +679,18 @@ int db_insert_ucontact(ucontact_t* _c)
 		return -1;
 	}
 
-	if (ul_dbf.insert(ul_dbh, keys, vals, nr_cols) < 0) {
-		LM_ERR("inserting contact in db failed %.*s (%.*s)\n",
-				_c->aor->len, ZSW(_c->aor->s), _c->ruid.len, ZSW(_c->ruid.s));
-		return -1;
+	if (db_insert_update && ul_dbf.insert_update) {
+		if (ul_dbf.insert_update(ul_dbh, keys, vals, nr_cols) < 0) {
+			LM_ERR("inserting with update contact in db failed %.*s (%.*s)\n",
+					_c->aor->len, ZSW(_c->aor->s), _c->ruid.len, ZSW(_c->ruid.s));
+			return -1;
+		}
+	} else {
+		if (ul_dbf.insert(ul_dbh, keys, vals, nr_cols) < 0) {
+			LM_ERR("inserting contact in db failed %.*s (%.*s)\n",
+					_c->aor->len, ZSW(_c->aor->s), _c->ruid.len, ZSW(_c->ruid.s));
+			return -1;
+		}
 	}
 
 	if (ul_xavp_contact_name.s) {

+ 6 - 1
src/modules/usrloc/usrloc_mod.c

@@ -155,6 +155,8 @@ str ulattrs_last_mod_col = str_init(ULATTRS_LAST_MOD_COL);	/*!< Name of column c
 str db_url          = str_init(DEFAULT_DB_URL);	/*!< Database URL */
 int timer_interval  = 60;				/*!< Timer interval in seconds */
 int db_mode         = 0;				/*!< Database sync scheme: 0-no db, 1-write through, 2-write back, 3-only db */
+int db_load         = 1;				/*!< Database load after restart: 1- true, 0- false (only the db_mode allows it) */
+int db_insert_update = 0;				/*!< Database : update on duplicate key instead of error */
 int use_domain      = 0;				/*!< Whether usrloc should use domain part of aor */
 int desc_time_order = 0;				/*!< By default do not enable timestamp ordering */
 int handle_lost_tcp = 0;				/*!< By default do not remove contacts before expiration time */
@@ -202,6 +204,8 @@ static param_export_t params[] = {
 	{"db_url",              PARAM_STR, &db_url        },
 	{"timer_interval",      INT_PARAM, &timer_interval  },
 	{"db_mode",             INT_PARAM, &db_mode         },
+	{"db_load",             INT_PARAM, &db_load         },
+	{"db_insert_update",    INT_PARAM, &db_insert_update },
 	{"use_domain",          INT_PARAM, &use_domain      },
 	{"desc_time_order",     INT_PARAM, &desc_time_order },
 	{"user_agent_column",   PARAM_STR, &user_agent_col},
@@ -405,6 +409,7 @@ static int child_init(int _rank)
 			break;
 		case DB_READONLY:
 			/* connect to db only from child 1 for preload */
+			db_load=1; /* we always load from the db in this mode */
 			if(_rank!=PROC_SIPINIT)
 				return 0;
 			break;
@@ -416,7 +421,7 @@ static int child_init(int _rank)
 		return -1;
 	}
 	/* _rank==PROC_SIPINIT is used even when fork is disabled */
-	if (_rank==PROC_SIPINIT && db_mode!=DB_ONLY) {
+	if (_rank==PROC_SIPINIT && db_mode!=DB_ONLY && db_load) {
 		/* if cache is used, populate domains from DB */
 		for( ptr=root ; ptr ; ptr=ptr->next) {
 			if (preload_udomain(ul_dbh, ptr->d) < 0) {

+ 1 - 0
src/modules/usrloc/usrloc_mod.h

@@ -76,6 +76,7 @@ extern str ulattrs_last_mod_col;
 extern str db_url;
 extern int timer_interval;
 extern int db_mode;
+extern int db_insert_update;
 extern int use_domain;
 extern int desc_time_order;
 extern int cseq_delay;