Skip to content

Commit

Permalink
Full rework of the module
Browse files Browse the repository at this point in the history
Leave the connection management handling to the cachedb interface
Allow multiple connections exposed to script
  • Loading branch information
vladpaiu committed May 16, 2014
1 parent b58dc5d commit 48fbecc
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 95 deletions.
6 changes: 5 additions & 1 deletion cachedb/cachedb_id.c
Expand Up @@ -95,12 +95,14 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
}

LM_DBG("parsing [%.*s]\n",url->len,url->s);

/* Initialize all attributes to 0 */
memset(id, 0, sizeof(struct cachedb_id));
st = ST_SCHEME;
begin = url->s;

if (dupl_string(&id->initial_url,url->s,url->s+url->len) < 0)
goto err;

for(i = 0; i < len; i++) {
switch(st) {
case ST_SCHEME:
Expand Down Expand Up @@ -267,6 +269,7 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
return 0;

err:
if (id->initial_url) pkg_free(id->initial_url);
if (id->scheme) pkg_free(id->scheme);
if (id->username) pkg_free(id->username);
if (id->password) pkg_free(id->password);
Expand Down Expand Up @@ -373,6 +376,7 @@ void free_cachedb_id(struct cachedb_id* id)
{
if (!id) return;

if (id->initial_url) pkg_free(id->initial_url);
if (id->scheme) pkg_free(id->scheme);
if (id->group_name) pkg_free(id->group_name);
if (id->username) pkg_free(id->username);
Expand Down
3 changes: 2 additions & 1 deletion cachedb/cachedb_id.h
Expand Up @@ -37,7 +37,8 @@ struct cachedb_id {
char* host; /**< Host or IP, case insensitive */
unsigned short port; /**< Port number */
char* database; /**< Database, case sensitive */
int flags; /**< Flags for signaling various events */
char* initial_url; /**< Initial full URL */
int flags; /**< Flags for signaling various events */
};

#define CACHEDB_ID_NO_URL (1<<0)
Expand Down
29 changes: 29 additions & 0 deletions cachedb/cachedb_pool.c
Expand Up @@ -25,6 +25,7 @@


#include "../dprint.h"
#include "../mem/mem.h"
#include "cachedb_pool.h"
#include <string.h>

Expand All @@ -43,6 +44,34 @@ cachedb_pool_con* cachedb_pool_get(struct cachedb_id *id)
return 0;
}

cachedb_pool_con** filter_pool_by_scheme(str *scheme,int* lst_size)
{
cachedb_pool_con *it;
cachedb_pool_con **lst=NULL;
int size = 0;
int alloc_size = 0;

for (it=cachedb_pool;it;it=it->next) {
if (memcmp(scheme->s,it->id->scheme,scheme->len) == 0) {
if (alloc_size - size == 0) {
alloc_size=(alloc_size==0)?2:2*alloc_size;
lst = pkg_realloc(lst,alloc_size * sizeof(cachedb_pool_con*));
if (lst == NULL) {
LM_ERR("No more pkg \n");
*lst_size = 0;
return NULL;
}
}

lst[size]=it;
size++;
}
}

*lst_size = size;
return lst;
}

void cachedb_pool_insert(cachedb_pool_con *con)
{
if (!con)
Expand Down
1 change: 1 addition & 0 deletions cachedb/cachedb_pool.h
Expand Up @@ -36,6 +36,7 @@ typedef struct cachedb_pool_con_t{
} cachedb_pool_con;

cachedb_pool_con* cachedb_pool_get(struct cachedb_id* id);
cachedb_pool_con** filter_pool_by_scheme(str *scheme,int *size);
void cachedb_pool_insert(cachedb_pool_con *con);
int cachedb_pool_remove(cachedb_pool_con *con);

Expand Down
33 changes: 28 additions & 5 deletions modules/cachedb_sql/README
Expand Up @@ -25,7 +25,7 @@ Vladut-Stefan Paiu

1.5. Exported Parameters

1.5.1. db_url (string)
1.5.1. cachedb_url (string)
1.5.2. db_table (string)
1.5.3. key_column (string)
1.5.4. value_column (string)
Expand All @@ -34,6 +34,8 @@ Vladut-Stefan Paiu
1.5.7. cache_clean_period (int)
1.5.8. Exported Functions

2. Frequently Asked Questions

List of Examples

1.1. Set db_url parameter
Expand Down Expand Up @@ -82,20 +84,32 @@ Chapter 1. Admin Guide

1.5. Exported Parameters

1.5.1. db_url (string)
1.5.1. cachedb_url (string)

The url of the Database that OpenSIPS will connect to in order
to use the from script cache_store,cache_fetch, etc operations.

The format to follow is : sql:[conn_id]-dburl

The parameter can be set multiple times to create multiple
connections accessible from the OpenSIPS script.

Example 1.1. Set db_url parameter
...
modparam("cachedb_sql", "db_url","mysql://localhost/my_database");
modparam("cachedb_sql", "cachedb_url", "sql:1st-mysql://root:vlad@localh
ost/opensips_sql")
...

Example 1.2. Usage example
...
cache_store("sql","key","$ru value");
cache_add("sql","counter",10);
modparam("cachedb_sql", "cachedb_url", "sql:1st-mysql://root:vlad@localh
ost/opensips_sql")
modparam("cachedb_sql", "cachedb_url", "sql:2nd-postgres://root:vlad@loc
alhost/opensips_pg")
...
...
cache_store("sql:1st-mysql","key","$ru value");
cache_store("sql:2nd-postgres","counter","10");
...

1.5.2. db_table (string)
Expand Down Expand Up @@ -159,3 +173,12 @@ modparam("cachedb_sql", "cache_clean_period",10);

The module does not export functions to be used in
configuration script.

Chapter 2. Frequently Asked Questions

2.1.

What happend with the old “db_url” module parameter?

It was replaced with the “cachedb_url” parameter. See the
documentation for the usage of the “cachedb_url” parameter.

0 comments on commit 48fbecc

Please sign in to comment.