Module: sip-router Branch: janakj/bdb Commit: f024b7f592a60db35d21bc94d00d0785d5f7deca URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=f024b7f5...
Author: Henning Westerholt henning.westerholt@1und1.de Committer: Henning Westerholt henning.westerholt@1und1.de Date: Tue Jan 22 13:55:43 2008 +0000
- port database interface (almost) complete from 'const char*' to 'str' for more safety (e.g. not null terminated strings) and performance (save strlen calls in core and library code) - adapt existing modules to use the new interface - change bind_dbmod and use_table function to the naming scheme of the other functions of the API - port existing module to use 'str' values for database related parameters - if modules uses already 'str' functions internally, remove the unnecessary temporary variables - make functions and parameter that needs only locally be used static - introduce new column variables needed for the keys for modules that don't have them already, this could be used to make them configurable later on - add 'const' to more db related functions - fix postgres driver, async queries must be finished, otherwise the next one will not complete - smaller whitespace and formatting changes and cleanups all over the place - reviewed and tested with testcases, but probably not error free because of the change size
- basic DB operations should work, but no extensive load testing has yet be done, as some work still remains: - remove dead code and superflous comments in postgres driver - clean up memory management, at the moment there exists two flavors: don't copy that much (mysql, unixodbc), don't trust the driver, copy everything (postgres, db_berkeley) - after this is unified, then only one implementation for the memory management of the db API could be used for all modules
- Foundations of this change have been created from Frederick Bullik, Frederick.Bullik at 1und1 dot de
- Please review and test.
git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@3563 689a6050-402a-0410-94f2-e92a70836424
---
modules/db_berkeley/km_bdb_res.c | 61 +++++++++++++++++++++------------- modules/db_berkeley/km_db_berkeley.c | 50 +++++++++------------------- modules/db_berkeley/km_db_berkeley.h | 4 +- 3 files changed, 56 insertions(+), 59 deletions(-)
diff --git a/modules/db_berkeley/km_bdb_res.c b/modules/db_berkeley/km_bdb_res.c index 78f2ac5..0e80d41 100644 --- a/modules/db_berkeley/km_bdb_res.c +++ b/modules/db_berkeley/km_bdb_res.c @@ -34,9 +34,6 @@ #include "bdb_res.h"
-/** -* -*/ int bdb_get_columns(table_p _tp, db_res_t* _res, int* _lres, int _nc) { int col, len; @@ -105,24 +102,40 @@ int bdb_get_columns(table_p _tp, db_res_t* _res, int* _lres, int _nc) column_p cp = NULL; cp = (_lres) ? _tp->colp[_lres[col]] : _tp->colp[col]; len = cp->name.len; - RES_NAMES(_res)[col] = pkg_malloc(len+1); + + RES_NAMES(_res)[col] = (str*)pkg_malloc(sizeof(str)); + if (! RES_NAMES(_res)[col]) { + LM_ERR("no private memory left\n"); + pkg_free(RES_NAMES(_res)); + pkg_free(RES_TYPES(_res)); + // FIXME we should also free all previous allocated RES_NAMES[col] + return -5; + } #ifdef BDB_EXTRA_DEBUG LM_DBG("%p=pkg_malloc(%d) RES_NAMES[%d]\n" - , RES_NAMES(_res)[col], len+1, col); + , RES_NAMES(_res)[col], len, col); +#endif + + RES_NAMES(_res)[col]->s = (char*)pkg_malloc(len); + +#ifdef BDB_EXTRA_DEBUG + LM_DBG("%p=pkg_malloc(%d) RES_NAMES[%d]->s\n" + , RES_NAMES(_res)[col]->s, len, col); #endif
- if (! RES_NAMES(_res)[col]) + if (! RES_NAMES(_res)[col]->s) { LM_ERR("Failed to allocate %d bytes to hold column name\n", len+1); return -1; } - memset((char *)RES_NAMES(_res)[col], 0, len+1); - strncpy((char *)RES_NAMES(_res)[col], cp->name.s, len); + memset((char *)RES_NAMES(_res)[col]->s, 0, len); + strncpy((char *)RES_NAMES(_res)[col]->s, cp->name.s, len); + RES_NAMES(_res)[col]->len = len;
- LM_DBG("RES_NAMES(%p)[%d]=[%s]\n", RES_NAMES(_res)[col] - , col, RES_NAMES(_res)[col]); + LM_DBG("RES_NAMES(%p)[%d]=[%.*s]\n", RES_NAMES(_res)[col] + , col, RES_NAMES(_res)[col]->len, RES_NAMES(_res)[col]->s);
RES_TYPES(_res)[col] = cp->type; } @@ -136,7 +149,7 @@ int bdb_get_columns(table_p _tp, db_res_t* _res, int* _lres, int _nc) */ int bdb_convert_row(db_res_t* _res, char *bdb_result, int* _lres) { - int col, len, i, j; + int col, len, i, j; char **row_buf, *s; db_row_t* row = NULL; col = len = i = j = 0; @@ -275,8 +288,8 @@ int bdb_convert_row(db_res_t* _res, char *bdb_result, int* _lres) break; default: #ifdef BDB_EXTRA_DEBUG - LM_DBG("col[%d] Col[%s] Type[%d] Freeing row_buf[%p]\n", col - , RES_NAMES(_res)[col], RES_TYPES(_res)[col] + LM_DBG("col[%d] Col[%.*s] Type[%d] Freeing row_buf[%p]\n", col + , RES_NAMES(_res)[col]->len, RES_NAMES(_res)[col]->s, RES_TYPES(_res)[col] , (char*) row_buf[col]); LM_DBG("%p=pkg_free() row_buf[%d]\n", (char *)row_buf[col], col); @@ -440,8 +453,8 @@ int bdb_append_row(db_res_t* _res, char *bdb_result, int* _lres, int _rx) if (RES_TYPES(_res)[col] != DB_STRING) { #ifdef BDB_EXTRA_DEBUG - LM_DBG("[%d][%d] Col[%s] Type[%d] Freeing row_buf[%i]\n" - , _rx, col, RES_NAMES(_res)[col], RES_TYPES(_res)[col], col); + LM_DBG("[%d][%d] Col[%.*s] Type[%d] Freeing row_buf[%i]\n" + , _rx, col, RES_NAMES(_res)[col]->len, RES_NAMES(_res)[col]->s, RES_TYPES(_res)[col], col); #endif pkg_free((char *)row_buf[col]); } @@ -478,8 +491,8 @@ int* bdb_get_colmap(table_p _dtp, db_key_t* _k, int _n) { for(j=0; j<_dtp->ncols; j++) { - if(strlen(_k[i])==_dtp->colp[j]->name.len - && !strncasecmp(_k[i], _dtp->colp[j]->name.s, + if(_k[i]->len==_dtp->colp[j]->name.len + && !strncasecmp(_k[i]->s, _dtp->colp[j]->name.s, _dtp->colp[j]->name.len)) { _lref[i] = j; @@ -489,7 +502,7 @@ int* bdb_get_colmap(table_p _dtp, db_key_t* _k, int _n) if(i>=_dtp->ncols) { - LM_DBG("ERROR column <%s> not found\n", _k[i]); + LM_DBG("ERROR column <%.*s> not found\n", _k[i]->len, _k[i]->s); pkg_free(_lref); return NULL; } @@ -592,13 +605,15 @@ int bdb_free_columns(db_res_t* _res) for(col = 0; col < RES_COL_N(_res); col++) { #ifdef BDB_EXTRA_DEBUG - LM_DBG("Freeing RES_NAMES(%p)[%d] -> free(%p) '%s'\n", _res - , col, RES_NAMES(_res)[col], RES_NAMES(_res)[col]); LM_DBG("%p=pkg_free() RES_NAMES[%d]\n", RES_NAMES(_res)[col], col); -#endif - + LM_DBG("Freeing RES_NAMES(%p)[%d] -> free(%p) '%s'\n", _res + , col, RES_NAMES(_res)[col], RES_NAMES(_res)[col]->s); + LM_DBG("%p=pkg_free() RES_NAMES[%d]->s\n", RES_NAMES(_res)[col]->s, col); +#endif + + pkg_free((char *)RES_NAMES(_res)[col]->s); pkg_free((char *)RES_NAMES(_res)[col]); - RES_NAMES(_res)[col] = (char *)NULL; + RES_NAMES(_res)[col] = (str*)NULL; } if (RES_NAMES(_res)) diff --git a/modules/db_berkeley/km_db_berkeley.c b/modules/db_berkeley/km_db_berkeley.c index 8049baf..74fea05 100644 --- a/modules/db_berkeley/km_db_berkeley.c +++ b/modules/db_berkeley/km_db_berkeley.c @@ -33,6 +33,7 @@
#include "../../str.h" +#include "../../ut.h" #include "../../mem/mem.h"
#include "../../sr_module.h" @@ -132,7 +133,7 @@ static void destroy(void) bdblib_destroy(); }
-int bdb_use_table(db_con_t* _h, const char* _t) +int bdb_use_table(db_con_t* _h, const str* _t) { return db_use_table(_h, _t); } @@ -140,17 +141,19 @@ int bdb_use_table(db_con_t* _h, const char* _t) /* * Initialize database connection */ -db_con_t* bdb_init(const char* _sqlurl) +db_con_t* bdb_init(const str* _sqlurl) { db_con_t* _res; str _s; char bdb_path[BDB_PATH_LEN]; - if (!_sqlurl) - return NULL; + if (!_sqlurl || !_sqlurl->s) { + LM_ERR("invalid parameter value\n"); + return 0; + } - _s.s = (char*)_sqlurl; - _s.len = strlen(_sqlurl); + _s.s = _sqlurl->s; + _s.len = _sqlurl->len; if(_s.len <= BDB_ID_LEN || strncmp(_s.s, BDB_ID, BDB_ID_LEN)!=0) { LM_ERR("invalid database URL - should be:" @@ -237,7 +240,6 @@ int bdb_reload(char* _n) */ void bdb_check_reload(db_con_t* _con) { - str s; char* p; int rc, len; @@ -277,8 +279,8 @@ void bdb_check_reload(db_con_t* _con) p++; /*get table name*/ - s.s = (char*)CON_TABLE(_con); - s.len = strlen(CON_TABLE(_con)); + s.s = CON_TABLE(_con)->s; + s.len = CON_TABLE(_con)->len; len+=s.len; if((len>MAX_ROW_SIZE) || (s.len > MAX_TABLENAME_SIZE) ) @@ -345,7 +347,6 @@ int bdb_query(db_con_t* _con, db_key_t* _k, db_op_t* _op, db_val_t* _v, u_int32_t i, len, ret; int klen=MAX_ROW_SIZE; int *lkey=NULL, *lres=NULL; - str s; DBT key, data; DB *db; DBC *dbcp; @@ -363,10 +364,7 @@ int bdb_query(db_con_t* _con, db_key_t* _k, db_op_t* _op, db_val_t* _v, if(auto_reload) bdb_check_reload(_con);
- s.s = (char*)CON_TABLE(_con); - s.len = strlen(CON_TABLE(_con)); - - _tbc = bdblib_get_table(BDB_CON_CONNECTION(_con), &s); + _tbc = bdblib_get_table(BDB_CON_CONNECTION(_con), (str*)CON_TABLE(_con)); if(!_tbc) { LM_WARN("table does not exist!\n"); return -1; @@ -629,7 +627,6 @@ int bdb_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) int *lkey=NULL; DBT key, data; DB *db; - str s;
i = j = ret = 0; klen=MAX_ROW_SIZE; @@ -647,10 +644,7 @@ int bdb_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) return -2; }
- s.s = (char*)CON_TABLE(_h); - s.len = strlen(CON_TABLE(_h)); - - _tbc = bdblib_get_table(BDB_CON_CONNECTION(_h), &s); + _tbc = bdblib_get_table(BDB_CON_CONNECTION(_h), (str*)CON_TABLE(_h)); if(!_tbc) { LM_WARN("table does not exist!\n"); return -3; @@ -769,7 +763,6 @@ int bdb_delete(db_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, int _n) DBT key; DB *db; DBC *dbcp; - str s;
i = j = ret = 0; klen=MAX_ROW_SIZE; @@ -780,10 +773,7 @@ int bdb_delete(db_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, int _n) if ((!_h) || !CON_TABLE(_h)) return -1;
- s.s = (char*)CON_TABLE(_h); - s.len = strlen(CON_TABLE(_h)); - - _tbc = bdblib_get_table(BDB_CON_CONNECTION(_h), &s); + _tbc = bdblib_get_table(BDB_CON_CONNECTION(_h), (str*)CON_TABLE(_h)); if(!_tbc) { LM_WARN("table does not exist!\n"); return -3; @@ -901,17 +891,13 @@ int _bdb_delete_cursor(db_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, i DB *db; DBC *dbcp; int *lkey=NULL; - str s; i = ret = 0; if ((!_h) || !CON_TABLE(_h)) return -1;
- s.s = (char*)CON_TABLE(_h); - s.len = strlen(CON_TABLE(_h)); - - _tbc = bdblib_get_table(BDB_CON_CONNECTION(_h), &s); + _tbc = bdblib_get_table(BDB_CON_CONNECTION(_h), (str*)CON_TABLE(_h)); if(!_tbc) { LM_WARN("table does not exist!\n"); return -3; @@ -1023,7 +1009,6 @@ error: int bdb_update(db_con_t* _con, db_key_t* _k, db_op_t* _op, db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) { - str s; char *c, *t; int ret, i, qcol, len, sum; int *lkey=NULL; @@ -1039,11 +1024,8 @@ int bdb_update(db_con_t* _con, db_key_t* _k, db_op_t* _op, db_val_t* _v, if (!_con || !CON_TABLE(_con) || !_uk || !_uv || _un <= 0) return -1; - - s.s = (char*)CON_TABLE(_con); - s.len = strlen(CON_TABLE(_con));
- _tbc = bdblib_get_table(BDB_CON_CONNECTION(_con), &s); + _tbc = bdblib_get_table(BDB_CON_CONNECTION(_con), (str*)CON_TABLE(_con)); if(!_tbc) { LM_ERR("table does not exist\n"); return -1; diff --git a/modules/db_berkeley/km_db_berkeley.h b/modules/db_berkeley/km_db_berkeley.h index a9f9793..869983a 100644 --- a/modules/db_berkeley/km_db_berkeley.h +++ b/modules/db_berkeley/km_db_berkeley.h @@ -41,12 +41,12 @@ int bdb_reload(char* _n);
void bdb_check_reload(db_con_t* _con); -int bdb_use_table(db_con_t* _h, const char* _t); +int bdb_use_table(db_con_t* _h, const str* _t);
/* * Initialize database connection */ -db_con_t* bdb_init(const char* _sqlurl); +db_con_t* bdb_init(const str* _sqlurl);
/*