[sr-dev] git:master: db_mysql: implemented async raw query and async insert

Daniel-Constantin Mierla miconda at gmail.com
Mon Apr 14 22:38:21 CEST 2014


Module: sip-router
Branch: master
Commit: 0d78e9a9cc56b642255fbdf5af1e1a9bf4d874cb
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=0d78e9a9cc56b642255fbdf5af1e1a9bf4d874cb

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Mon Apr 14 22:17:51 2014 +0200

db_mysql: implemented async raw query and async insert

- both use the core async framework

---

 modules/db_mysql/km_db_mysql.c |    2 +
 modules/db_mysql/km_dbase.c    |   86 ++++++++++++++++++++++++++++++++++++++++
 modules/db_mysql/km_dbase.h    |   13 ++++++
 3 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/modules/db_mysql/km_db_mysql.c b/modules/db_mysql/km_db_mysql.c
index 5758f6e..aa3da2f 100644
--- a/modules/db_mysql/km_db_mysql.c
+++ b/modules/db_mysql/km_db_mysql.c
@@ -119,6 +119,8 @@ int db_mysql_bind_api(db_func_t *dbb)
 	dbb->start_transaction= db_mysql_start_transaction;
 	dbb->end_transaction  = db_mysql_end_transaction;
 	dbb->abort_transaction= db_mysql_abort_transaction;
+	dbb->raw_query_async  = db_mysql_raw_query_async;
+	dbb->insert_async     = db_mysql_insert_async;
 
 	return 0;
 }
diff --git a/modules/db_mysql/km_dbase.c b/modules/db_mysql/km_dbase.c
index e486e5f..46efdd4 100644
--- a/modules/db_mysql/km_dbase.c
+++ b/modules/db_mysql/km_dbase.c
@@ -40,6 +40,7 @@
 #include <mysql/mysql_version.h>
 #include "../../mem/mem.h"
 #include "../../dprint.h"
+#include "../../async_task.h"
 #include "../../lib/srdb1/db_query.h"
 #include "../../lib/srdb1/db_ut.h"
 #include "mysql_mod.h"
@@ -124,6 +125,66 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
 }
 
 
+/**
+ *
+ */
+void db_mysql_async_exec_task(void *param)
+{
+	str *p;
+	db1_con_t* dbc;
+	
+	p = (str*)param;
+	
+	dbc = db_mysql_init(&p[0]);
+
+	if(dbc==NULL) {
+		LM_ERR("failed to open connection for [%.*s]\n", p[0].len, p[0].s);
+		return;
+	}
+	if(db_mysql_submit_query(dbc, &p[1])<0) {
+		LM_ERR("failed to execute query on async worker\n");
+	}
+	db_mysql_close(dbc);
+}
+
+/**
+ * Execute a raw SQL query via core async framework.
+ * \param _h handle for the database
+ * \param _s raw query string
+ * \return zero on success, negative value on failure
+ */
+int db_mysql_submit_query_async(const db1_con_t* _h, const str* _s)
+{
+	struct db_id* di;
+	async_task_t *atask;
+	int asize;
+	str *p;
+
+	di = ((struct pool_con*)_h->tail)->id;
+
+	asize = sizeof(async_task_t) + 2*sizeof(str) + di->url.len + _s->len + 2;
+	atask = shm_malloc(asize);
+	if(atask==NULL) {
+		LM_ERR("no more shared memory to allocate %d\n", asize);
+		return -1;
+	}
+
+	atask->exec = db_mysql_async_exec_task;
+	atask->param = (char*)atask + sizeof(async_task_t);
+
+	p = (str*)((char*)atask + sizeof(async_task_t));
+	p[0].s = (char*)p + 2*sizeof(str);
+	p[0].len = di->url.len;
+	strncpy(p[0].s, di->url.s, di->url.len);
+	p[1].s = p[0].s + p[0].len + 1;
+	p[1].len = _s->len;
+	strncpy(p[1].s, _s->s, _s->len);
+
+	async_task_push(atask);
+
+	return 0;
+}
+
 
 /**
  * Initialize the database module.
@@ -397,6 +458,16 @@ int db_mysql_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
 	db_mysql_store_result);
 }
 
+/**
+ * Execute a raw SQL query via core async framework.
+ * \param _h handle for the database
+ * \param _s raw query string
+ * \return zero on success, negative value on failure
+ */
+int db_mysql_raw_query_async(const db1_con_t* _h, const str* _s)
+{
+	return db_mysql_submit_query_async(_h, _s);
+}
 
 /**
  * Insert a row into a specified table.
@@ -767,6 +838,21 @@ int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k, const db_va
 	db_mysql_submit_query);
 }
 
+/**
+ * Insert a row into a specified table via core async framework.
+ * \param _h structure representing database connection
+ * \param _k key names
+ * \param _v values of the keys
+ * \param _n number of key=value pairs
+ * \return zero on success, negative value on failure
+ */
+int db_mysql_insert_async(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n)
+{
+	return db_do_insert_delayed(_h, _k, _v, _n, db_mysql_val2str,
+	db_mysql_submit_query_async);
+}
+
+
 
 /**
  * Store the name of table that will be used by subsequent database functions
diff --git a/modules/db_mysql/km_dbase.h b/modules/db_mysql/km_dbase.h
index 23fc002..367bd2b 100644
--- a/modules/db_mysql/km_dbase.h
+++ b/modules/db_mysql/km_dbase.h
@@ -82,6 +82,12 @@ int db_mysql_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r);
 
 
 /*! \brief
+ * Raw SQL query via async framework
+ */
+int db_mysql_raw_query_async(const db1_con_t* _h, const str* _s);
+
+
+/*! \brief
  * Insert a row into table
  */
 int db_mysql_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n);
@@ -150,6 +156,13 @@ int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k,
 
 
 /*! \brief
+ * Insert a row into table via async framework
+ */
+int db_mysql_insert_async(const db1_con_t* _h, const db_key_t* _k,
+		const db_val_t* _v, const int _n);
+
+
+/*! \brief
  * Store name of table that will be used by
  * subsequent database functions
  */




More information about the sr-dev mailing list