Module: kamailio
Branch: master
Commit: ed4d7fbee914c7fc4c5da438f4c4f021648d1e75
URL:
https://github.com/kamailio/kamailio/commit/ed4d7fbee914c7fc4c5da438f4c4f02…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2015-04-10T16:48:33+02:00
ndb_mongodb: added mongodb_find_one()
- find first matching document and return it
- faster when knowing it is supposed to be only one, or needing only one
---
Modified: modules/ndb_mongodb/mongodb_client.c
Modified: modules/ndb_mongodb/mongodb_client.h
Modified: modules/ndb_mongodb/ndb_mongodb_mod.c
---
Diff:
https://github.com/kamailio/kamailio/commit/ed4d7fbee914c7fc4c5da438f4c4f02…
Patch:
https://github.com/kamailio/kamailio/commit/ed4d7fbee914c7fc4c5da438f4c4f02…
---
diff --git a/modules/ndb_mongodb/mongodb_client.c b/modules/ndb_mongodb/mongodb_client.c
index 7202e6c..5b82c44 100644
--- a/modules/ndb_mongodb/mongodb_client.c
+++ b/modules/ndb_mongodb/mongodb_client.c
@@ -217,6 +217,7 @@ int mongodbc_exec_cmd(str *srv, str *dname, str *cname, str *cmd, str
*res, int
bson_t reply;
const bson_t *cdoc;
char c;
+ int nres;
int ret;
if(srv==NULL || cmd==NULL || res==NULL)
@@ -283,10 +284,12 @@ int mongodbc_exec_cmd(str *srv, str *dname, str *cname, str *cmd,
str *res, int
NULL,
0);
} else {
+ nres = 0;
+ if(emode==3) nres = 1; /* return one result */
rpl->cursor = mongoc_collection_find (rpl->collection,
MONGOC_QUERY_NONE,
0,
- 0,
+ nres,
0,
&command,
NULL,
@@ -343,6 +346,14 @@ int mongodbc_find(str *srv, str *dname, str *cname, str *cmd, str
*res)
/**
*
*/
+int mongodbc_find_one(str *srv, str *dname, str *cname, str *cmd, str *res)
+{
+ return mongodbc_exec_cmd(srv, dname, cname, cmd, res, 3);
+}
+
+/**
+ *
+ */
mongodbc_reply_t *mongodbc_get_reply(str *name)
{
mongodbc_reply_t *rpl;
diff --git a/modules/ndb_mongodb/mongodb_client.h b/modules/ndb_mongodb/mongodb_client.h
index 1583db9..b0892e9 100644
--- a/modules/ndb_mongodb/mongodb_client.h
+++ b/modules/ndb_mongodb/mongodb_client.h
@@ -35,6 +35,7 @@ int mongodbc_add_server(char *spec);
int mongodbc_exec_simple(str *srv, str *dname, str *cname, str *cmd, str *res);
int mongodbc_exec(str *srv, str *dname, str *cname, str *cmd, str *res);
int mongodbc_find(str *srv, str *dname, str *cname, str *cmd, str *res);
+int mongodbc_find_one(str *srv, str *dname, str *cname, str *cmd, str *res);
typedef struct mongodbc_server {
str *sname;
diff --git a/modules/ndb_mongodb/ndb_mongodb_mod.c
b/modules/ndb_mongodb/ndb_mongodb_mod.c
index 75bee9f..25c7fef 100644
--- a/modules/ndb_mongodb/ndb_mongodb_mod.c
+++ b/modules/ndb_mongodb/ndb_mongodb_mod.c
@@ -39,6 +39,8 @@ MODULE_VERSION
int mongodb_srv_param(modparam_t type, void *val);
static int w_mongodb_find(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
char* scmd, char* sres);
+static int w_mongodb_find_one(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
+ char* scmd, char* sres);
static int w_mongodb_cmd_simple(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
char* scmd, char* sres);
static int w_mongodb_cmd(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
@@ -64,6 +66,8 @@ static pv_export_t mod_pvs[] = {
static cmd_export_t cmds[]={
{"mongodb_find", (cmd_function)w_mongodb_find, 5, fixup_mongodb_cmd,
0, ANY_ROUTE},
+ {"mongodb_find_one", (cmd_function)w_mongodb_find_one, 5, fixup_mongodb_cmd,
+ 0, ANY_ROUTE},
{"mongodb_cmd_simple", (cmd_function)w_mongodb_cmd_simple, 5,
fixup_mongodb_cmd,
0, ANY_ROUTE},
{"mongodb_cmd", (cmd_function)w_mongodb_cmd, 5, fixup_mongodb_cmd,
@@ -155,12 +159,15 @@ static int w_mongodb_do_cmd(sip_msg_t* msg, char* ssrv, char
*sdname, char *scna
LM_ERR("no mongodb reply name\n");
return -1;
}
+ ret = -1;
if(ctype==0) {
ret = mongodbc_exec_simple(&s[0], &s[1], &s[2], &s[3], &s[4]);
} else if(ctype==1) {
ret = mongodbc_exec(&s[0], &s[1], &s[2], &s[3], &s[4]);
- } else {
+ } else if(ctype==2) {
ret = mongodbc_find(&s[0], &s[1], &s[2], &s[3], &s[4]);
+ } else if(ctype==3) {
+ ret = mongodbc_find_one(&s[0], &s[1], &s[2], &s[3], &s[4]);
}
if(ret<0)
return -1;
@@ -197,6 +204,15 @@ static int w_mongodb_find(sip_msg_t* msg, char* ssrv, char *sdname,
char *scname
/**
*
*/
+static int w_mongodb_find_one(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
+ char* scmd, char* sres)
+{
+ return w_mongodb_do_cmd(msg, ssrv, sdname, scname, scmd, sres, 3);
+}
+
+/**
+ *
+ */
static int fixup_mongodb_cmd(void** param, int param_no)
{
return fixup_spve_null(param, 1);