km_db_berkeley.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. * $Id$
  3. *
  4. * db_berkeley module, portions of this code were templated using
  5. * the dbtext and postgres modules.
  6. * Copyright (C) 2007 Cisco Systems
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * SIP-router is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2007-09-19 genesis (wiquan)
  27. */
  28. /*! \file
  29. * Berkeley DB :
  30. *
  31. * \ingroup database
  32. */
  33. #include <stdio.h>
  34. #include <unistd.h>
  35. #include <sys/stat.h>
  36. #include "../../str.h"
  37. #include "../../ut.h"
  38. #include "../../mem/mem.h"
  39. #include "../../sr_module.h"
  40. #include "../../lib/srdb1/db_res.h"
  41. #include "../../lib/srdb1/db.h"
  42. #include "../../lib/srdb1/db_query.h"
  43. #include "km_db_berkeley.h"
  44. #include "km_bdb_lib.h"
  45. #include "km_bdb_res.h"
  46. #include "km_bdb_mi.h"
  47. #include "bdb_mod.h"
  48. #include "bdb_crs_compat.h"
  49. #ifndef CFG_DIR
  50. #define CFG_DIR "/tmp"
  51. #endif
  52. #define BDB_ID "berkeley://"
  53. #define BDB_ID_LEN (sizeof(BDB_ID)-1)
  54. #define BDB_PATH_LEN 256
  55. #define BDB_KEY 1
  56. #define BDB_VALUE 0
  57. /*MODULE_VERSION*/
  58. int bdb_bind_api(db_func_t *dbb);
  59. /*
  60. * Exported functions
  61. */
  62. static kam_cmd_export_t cmds[] = {
  63. {"db_bind_api", (cmd_function)bdb_bind_api, 0, 0, 0, 0},
  64. {0, 0, 0, 0, 0, 0}
  65. };
  66. /*
  67. * Exported parameters
  68. */
  69. static param_export_t params[] = {
  70. {"auto_reload", INT_PARAM, &auto_reload },
  71. {"log_enable", INT_PARAM, &log_enable },
  72. {"journal_roll_interval", INT_PARAM, &journal_roll_interval },
  73. {0, 0, 0}
  74. };
  75. /*
  76. * Exported MI functions
  77. */
  78. static mi_export_t mi_cmds[] = {
  79. { MI_BDB_RELOAD, mi_bdb_reload, 0, 0, 0 },
  80. { 0, 0, 0, 0, 0}
  81. };
  82. struct kam_module_exports kam_exports = {
  83. "db_berkeley",
  84. DEFAULT_DLFLAGS, /* dlopen flags */
  85. cmds, /* Exported functions */
  86. params, /* Exported parameters */
  87. 0, /* exported statistics */
  88. mi_cmds, /* exported MI functions */
  89. 0, /* exported pseudo-variables */
  90. 0, /* extra processes */
  91. km_mod_init, /* module initialization function */
  92. 0, /* response function*/
  93. km_destroy, /* destroy function */
  94. 0 /* per-child init function */
  95. };
  96. int km_mod_init(void)
  97. {
  98. db_parms_t p;
  99. if(register_mi_mod(kam_exports.name, mi_cmds)!=0)
  100. {
  101. LM_ERR("failed to register MI commands\n");
  102. return -1;
  103. }
  104. p.auto_reload = auto_reload;
  105. p.log_enable = log_enable;
  106. p.cache_size = (4 * 1024 * 1024); //4Mb
  107. p.journal_roll_interval = journal_roll_interval;
  108. if(km_bdblib_init(&p))
  109. return -1;
  110. return 0;
  111. }
  112. void km_destroy(void)
  113. {
  114. km_bdblib_destroy();
  115. }
  116. int bdb_bind_api(db_func_t *dbb)
  117. {
  118. if(dbb==NULL)
  119. return -1;
  120. memset(dbb, 0, sizeof(db_func_t));
  121. dbb->use_table = bdb_use_table;
  122. dbb->init = bdb_init;
  123. dbb->close = bdb_close;
  124. dbb->query = (db_query_f)km_bdb_query;
  125. dbb->free_result = bdb_free_query;
  126. dbb->insert = (db_insert_f)bdb_insert;
  127. dbb->delete = (db_delete_f)bdb_delete;
  128. dbb->update = (db_update_f)bdb_update;
  129. return 0;
  130. }
  131. int bdb_use_table(db1_con_t* _h, const str* _t)
  132. {
  133. return db_use_table(_h, _t);
  134. }
  135. /*
  136. * Initialize database connection
  137. */
  138. db1_con_t* bdb_init(const str* _sqlurl)
  139. {
  140. db1_con_t* _res;
  141. str _s;
  142. char bdb_path[BDB_PATH_LEN];
  143. if (!_sqlurl || !_sqlurl->s) {
  144. LM_ERR("invalid parameter value\n");
  145. return 0;
  146. }
  147. _s.s = _sqlurl->s;
  148. _s.len = _sqlurl->len;
  149. if(_s.len <= BDB_ID_LEN || strncmp(_s.s, BDB_ID, BDB_ID_LEN)!=0)
  150. {
  151. LM_ERR("invalid database URL - should be:"
  152. " <%s[/]path/to/directory>\n", BDB_ID);
  153. return NULL;
  154. }
  155. _s.s += BDB_ID_LEN;
  156. _s.len -= BDB_ID_LEN;
  157. if(_s.s[0]!='/')
  158. {
  159. if(sizeof(CFG_DIR)+_s.len+2 > BDB_PATH_LEN)
  160. {
  161. LM_ERR("path to database is too long\n");
  162. return NULL;
  163. }
  164. strcpy(bdb_path, CFG_DIR);
  165. bdb_path[sizeof(CFG_DIR)] = '/';
  166. strncpy(&bdb_path[sizeof(CFG_DIR)+1], _s.s, _s.len);
  167. _s.len += sizeof(CFG_DIR);
  168. _s.s = bdb_path;
  169. }
  170. _res = pkg_malloc(sizeof(db1_con_t)+sizeof(bdb_con_t));
  171. if (!_res)
  172. {
  173. LM_ERR("No private memory left\n");
  174. return NULL;
  175. }
  176. memset(_res, 0, sizeof(db1_con_t) + sizeof(bdb_con_t));
  177. _res->tail = (unsigned long)((char*)_res+sizeof(db1_con_t));
  178. LM_INFO("using database at: %.*s\n", _s.len, _s.s);
  179. BDB_CON_CONNECTION(_res) = km_bdblib_get_db(&_s);
  180. if (!BDB_CON_CONNECTION(_res))
  181. {
  182. LM_ERR("cannot get the link to database\n");
  183. return NULL;
  184. }
  185. return _res;
  186. }
  187. /*
  188. * Close a database connection
  189. */
  190. void bdb_close(db1_con_t* _h)
  191. {
  192. if(BDB_CON_RESULT(_h))
  193. db_free_result(BDB_CON_RESULT(_h));
  194. pkg_free(_h);
  195. }
  196. /*
  197. * n can be the dbenv path or a table name
  198. */
  199. int bdb_reload(char* _n)
  200. {
  201. int rc = 0;
  202. #ifdef BDB_EXTRA_DEBUG
  203. LM_DBG("[bdb_reload] Initiate RELOAD in %s\n", _n);
  204. #endif
  205. if ((rc = km_bdblib_close(_n)) != 0)
  206. { LM_ERR("[bdb_reload] Error while closing db_berkeley DB.\n");
  207. return rc;
  208. }
  209. if ((rc = km_bdblib_reopen(_n)) != 0)
  210. { LM_ERR("[bdb_reload] Error while reopening db_berkeley DB.\n");
  211. return rc;
  212. }
  213. #ifdef BDB_EXTRA_DEBUG
  214. LM_DBG("[bdb_reload] RELOAD successful in %s\n", _n);
  215. #endif
  216. return rc;
  217. }
  218. /*
  219. * Attempts to reload a Berkeley database; reloads when the inode changes
  220. */
  221. void bdb_check_reload(db1_con_t* _con)
  222. {
  223. str s;
  224. char* p;
  225. int rc, len;
  226. struct stat st;
  227. database_p db;
  228. char n[MAX_ROW_SIZE];
  229. char t[MAX_TABLENAME_SIZE];
  230. table_p tp = NULL;
  231. tbl_cache_p tbc = NULL;
  232. p=n;
  233. rc = len = 0;
  234. /*get dbenv name*/
  235. db = BDB_CON_CONNECTION(_con);
  236. if(!db->dbenv) return;
  237. s.s = db->name.s;
  238. s.len = db->name.len;
  239. len+=s.len;
  240. if(len > MAX_ROW_SIZE)
  241. { LM_ERR("dbenv name too long \n");
  242. return;
  243. }
  244. strncpy(p, s.s, s.len);
  245. p+=s.len;
  246. len++;
  247. if(len > MAX_ROW_SIZE)
  248. { LM_ERR("dbenv name too long \n");
  249. return;
  250. }
  251. /*append slash */
  252. *p = '/';
  253. p++;
  254. /*get table name*/
  255. s.s = CON_TABLE(_con)->s;
  256. s.len = CON_TABLE(_con)->len;
  257. len+=s.len;
  258. if((len>MAX_ROW_SIZE) || (s.len > MAX_TABLENAME_SIZE) )
  259. { LM_ERR("table name too long \n");
  260. return;
  261. }
  262. strncpy(t, s.s, s.len);
  263. t[s.len] = 0;
  264. strncpy(p, s.s, s.len);
  265. p+=s.len;
  266. *p=0;
  267. if( (tbc = km_bdblib_get_table(db, &s)) == NULL)
  268. return;
  269. if( (tp = tbc->dtp) == NULL)
  270. return;
  271. LM_DBG("stat file [%.*s]\n", len, n);
  272. rc = stat(n, &st);
  273. if(!rc)
  274. { if((tp->ino!=0) && (st.st_ino != tp->ino))
  275. bdb_reload(t); /*file changed on disk*/
  276. tp->ino = st.st_ino;
  277. }
  278. }
  279. /*
  280. * Free all memory allocated by get_result
  281. */
  282. int bdb_free_query(db1_con_t* _h, db1_res_t* _r)
  283. {
  284. if(_r)
  285. db_free_result(_r);
  286. if(_h)
  287. BDB_CON_RESULT(_h) = NULL;
  288. return 0;
  289. }
  290. /*
  291. * Query table for specified rows
  292. * _con: structure representing database connection
  293. * _k: key names
  294. * _op: operators
  295. * _v: values of the keys that must match
  296. * _c: column names to return
  297. * _n: number of key=values pairs to compare
  298. * _nc: number of columns to return
  299. * _o: order by the specified column
  300. */
  301. int km_bdb_query(db1_con_t* _con, db_key_t* _k, db_op_t* _op, db_val_t* _v,
  302. db_key_t* _c, int _n, int _nc, db_key_t _o, db1_res_t** _r)
  303. {
  304. tbl_cache_p _tbc = NULL;
  305. table_p _tp = NULL;
  306. char kbuf[MAX_ROW_SIZE];
  307. char dbuf[MAX_ROW_SIZE];
  308. u_int32_t i, len, ret;
  309. int klen=MAX_ROW_SIZE;
  310. int *lkey=NULL, *lres=NULL;
  311. DBT key, data;
  312. DB *db;
  313. DBC *dbcp;
  314. if ((!_con) || (!_r) || !CON_TABLE(_con))
  315. {
  316. #ifdef BDB_EXTRA_DEBUG
  317. LM_ERR("Invalid parameter value\n");
  318. #endif
  319. return -1;
  320. }
  321. *_r = NULL;
  322. /*check if underlying DB file has changed inode */
  323. if(auto_reload)
  324. bdb_check_reload(_con);
  325. _tbc = km_bdblib_get_table(BDB_CON_CONNECTION(_con), (str*)CON_TABLE(_con));
  326. if(!_tbc)
  327. { LM_WARN("table does not exist!\n");
  328. return -1;
  329. }
  330. _tp = _tbc->dtp;
  331. if(!_tp)
  332. { LM_WARN("table not loaded!\n");
  333. return -1;
  334. }
  335. #ifdef BDB_EXTRA_DEBUG
  336. LM_DBG("QUERY in %.*s\n", _tp->name.len, _tp->name.s);
  337. if (_o) LM_DBG("DONT-CARE : _o: order by the specified column \n");
  338. if (_op) LM_DBG("DONT-CARE : _op: operators for refining query \n");
  339. #endif
  340. db = _tp->db;
  341. if(!db) return -1;
  342. memset(&key, 0, sizeof(DBT));
  343. memset(kbuf, 0, MAX_ROW_SIZE);
  344. memset(&data, 0, sizeof(DBT));
  345. memset(dbuf, 0, MAX_ROW_SIZE);
  346. data.data = dbuf;
  347. data.ulen = MAX_ROW_SIZE;
  348. data.flags = DB_DBT_USERMEM;
  349. /* if _c is NULL and _nc is zero, you will get all table
  350. columns in the result
  351. */
  352. if (_c)
  353. { lres = bdb_get_colmap(_tbc->dtp, _c, _nc);
  354. if(!lres)
  355. { ret = -1;
  356. goto error;
  357. }
  358. }
  359. if(_k)
  360. { lkey = bdb_get_colmap(_tbc->dtp, _k, _n);
  361. if(!lkey)
  362. { ret = -1;
  363. goto error;
  364. }
  365. }
  366. else
  367. {
  368. DB_HASH_STAT st;
  369. memset(&st, 0, sizeof(DB_HASH_STAT));
  370. i =0 ;
  371. #ifdef BDB_EXTRA_DEBUG
  372. LM_DBG("SELECT * FROM %.*s\n", _tp->name.len, _tp->name.s);
  373. #endif
  374. /* Acquire a cursor for the database. */
  375. if ((ret = db->cursor(db, NULL, &dbcp, 0)) != 0)
  376. { LM_ERR("Error creating cursor\n");
  377. goto error;
  378. }
  379. /*count the number of records*/
  380. while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0)
  381. { if(!strncasecmp((char*)key.data,"METADATA",8))
  382. continue;
  383. i++;
  384. }
  385. dbcp->CLOSE_CURSOR(dbcp);
  386. ret=0;
  387. #ifdef BDB_EXTRA_DEBUG
  388. LM_DBG("%i = SELECT COUNT(*) FROM %.*s\n", i, _tp->name.len, _tp->name.s);
  389. #endif
  390. *_r = db_new_result();
  391. if (!*_r)
  392. { LM_ERR("no memory left for result \n");
  393. ret = -2;
  394. goto error;
  395. }
  396. if(i == 0)
  397. {
  398. /*return empty table*/
  399. RES_ROW_N(*_r) = 0;
  400. BDB_CON_RESULT(_con) = *_r;
  401. return 0;
  402. }
  403. /*allocate N rows in the result*/
  404. RES_ROW_N(*_r) = i;
  405. len = sizeof(db_row_t) * i;
  406. RES_ROWS(*_r) = (db_row_t*)pkg_malloc( len );
  407. memset(RES_ROWS(*_r), 0, len);
  408. /*fill in the column part of db1_res_t (metadata) */
  409. if ((ret = bdb_get_columns(_tbc->dtp, *_r, lres, _nc)) < 0)
  410. { LM_ERR("Error while getting column names\n");
  411. goto error;
  412. }
  413. /* Acquire a cursor for the database. */
  414. if ((ret = db->cursor(db, NULL, &dbcp, 0)) != 0)
  415. { LM_ERR("Error creating cursor\n");
  416. goto error;
  417. }
  418. /*convert each record into a row in the result*/
  419. i =0 ;
  420. while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0)
  421. {
  422. if(!strncasecmp((char*)key.data,"METADATA",8))
  423. continue;
  424. #ifdef BDB_EXTRA_DEBUG
  425. LM_DBG("KEY: [%.*s]\nDATA: [%.*s]\n"
  426. , (int) key.size
  427. , (char *)key.data
  428. , (int) data.size
  429. , (char *)data.data);
  430. #endif
  431. /*fill in the row part of db1_res_t */
  432. if ((ret=bdb_append_row( *_r, dbuf, lres, i)) < 0)
  433. { LM_ERR("Error while converting row\n");
  434. goto error;
  435. }
  436. i++;
  437. }
  438. dbcp->CLOSE_CURSOR(dbcp);
  439. BDB_CON_RESULT(_con) = *_r;
  440. return 0;
  441. }
  442. if ( (ret = km_bdblib_valtochar(_tp, lkey, kbuf, &klen, _v, _n, BDB_KEY)) != 0 )
  443. { LM_ERR("error in query key \n");
  444. goto error;
  445. }
  446. key.data = kbuf;
  447. key.ulen = MAX_ROW_SIZE;
  448. key.flags = DB_DBT_USERMEM;
  449. key.size = klen;
  450. data.data = dbuf;
  451. data.ulen = MAX_ROW_SIZE;
  452. data.flags = DB_DBT_USERMEM;
  453. /*create an empty db1_res_t which gets returned even if no result*/
  454. *_r = db_new_result();
  455. if (!*_r)
  456. { LM_ERR("no memory left for result \n");
  457. ret = -2;
  458. goto error;
  459. }
  460. RES_ROW_N(*_r) = 0;
  461. BDB_CON_RESULT(_con) = *_r;
  462. #ifdef BDB_EXTRA_DEBUG
  463. LM_DBG("SELECT KEY: [%.*s]\n"
  464. , (int) key.size
  465. , (char *)key.data );
  466. #endif
  467. /*query Berkely DB*/
  468. if ((ret = db->get(db, NULL, &key, &data, 0)) == 0)
  469. {
  470. #ifdef BDB_EXTRA_DEBUG
  471. LM_DBG("RESULT\nKEY: [%.*s]\nDATA: [%.*s]\n"
  472. , (int) key.size
  473. , (char *)key.data
  474. , (int) data.size
  475. , (char *)data.data);
  476. #endif
  477. /*fill in the col part of db1_res_t */
  478. if ((ret = bdb_get_columns(_tbc->dtp, *_r, lres, _nc)) < 0)
  479. { LM_ERR("Error while getting column names\n");
  480. goto error;
  481. }
  482. /*fill in the row part of db1_res_t */
  483. if ((ret=bdb_convert_row( *_r, dbuf, lres)) < 0)
  484. { LM_ERR("Error while converting row\n");
  485. goto error;
  486. }
  487. }
  488. else
  489. {
  490. /*Berkeley DB error handler*/
  491. switch(ret)
  492. {
  493. case DB_NOTFOUND:
  494. #ifdef BDB_EXTRA_DEBUG
  495. LM_DBG("NO RESULT for QUERY \n");
  496. #endif
  497. ret=0;
  498. break;
  499. /*The following are all critical/fatal */
  500. case DB_LOCK_DEADLOCK:
  501. // The operation was selected to resolve a deadlock.
  502. case DB_SECONDARY_BAD:
  503. // A secondary index references a nonexistent primary key.
  504. case DB_RUNRECOVERY:
  505. default:
  506. LM_CRIT("DB->get error: %s.\n", db_strerror(ret));
  507. km_bdblib_recover(_tp,ret);
  508. goto error;
  509. }
  510. }
  511. if(lkey)
  512. pkg_free(lkey);
  513. if(lres)
  514. pkg_free(lres);
  515. return ret;
  516. error:
  517. if(lkey)
  518. pkg_free(lkey);
  519. if(lres)
  520. pkg_free(lres);
  521. if(*_r)
  522. db_free_result(*_r);
  523. *_r = NULL;
  524. return ret;
  525. }
  526. /*
  527. * Raw SQL query
  528. */
  529. int bdb_raw_query(db1_con_t* _h, char* _s, db1_res_t** _r)
  530. {
  531. LM_CRIT("DB RAW QUERY not implemented!\n");
  532. return -1;
  533. }
  534. /*
  535. * Insert a row into table
  536. */
  537. int bdb_insert(db1_con_t* _h, db_key_t* _k, db_val_t* _v, int _n)
  538. {
  539. tbl_cache_p _tbc = NULL;
  540. table_p _tp = NULL;
  541. char kbuf[MAX_ROW_SIZE];
  542. char dbuf[MAX_ROW_SIZE];
  543. int i, j, ret, klen, dlen;
  544. int *lkey=NULL;
  545. DBT key, data;
  546. DB *db;
  547. i = j = ret = 0;
  548. klen=MAX_ROW_SIZE;
  549. dlen=MAX_ROW_SIZE;
  550. if ((!_h) || (!_v) || !CON_TABLE(_h))
  551. { return -1;
  552. }
  553. if (!_k)
  554. {
  555. #ifdef BDB_EXTRA_DEBUG
  556. LM_ERR("DB INSERT without KEYs not implemented! \n");
  557. #endif
  558. return -2;
  559. }
  560. _tbc = km_bdblib_get_table(BDB_CON_CONNECTION(_h), (str*)CON_TABLE(_h));
  561. if(!_tbc)
  562. { LM_WARN("table does not exist!\n");
  563. return -3;
  564. }
  565. _tp = _tbc->dtp;
  566. if(!_tp)
  567. { LM_WARN("table not loaded!\n");
  568. return -4;
  569. }
  570. #ifdef BDB_EXTRA_DEBUG
  571. LM_DBG("INSERT in %.*s\n", _tp->name.len, _tp->name.s );
  572. #endif
  573. db = _tp->db;
  574. memset(&key, 0, sizeof(DBT));
  575. memset(kbuf, 0, klen);
  576. if(_tp->ncols<_n)
  577. { LM_WARN("more values than columns!!\n");
  578. return -5;
  579. }
  580. lkey = bdb_get_colmap(_tp, _k, _n);
  581. if(!lkey) return -7;
  582. /* verify col types provided */
  583. for(i=0; i<_n; i++)
  584. { j = (lkey)?lkey[i]:i;
  585. if(bdb_is_neq_type(_tp->colp[j]->type, _v[i].type))
  586. {
  587. LM_WARN("incompatible types v[%d] - c[%d]!\n", i, j);
  588. ret = -8;
  589. goto error;
  590. }
  591. }
  592. /* make the key */
  593. if ( (ret = km_bdblib_valtochar(_tp, lkey, kbuf, &klen, _v, _n, BDB_KEY)) != 0 )
  594. { LM_ERR("Error in km_bdblib_valtochar \n");
  595. ret = -9;
  596. goto error;
  597. }
  598. key.data = kbuf;
  599. key.ulen = MAX_ROW_SIZE;
  600. key.flags = DB_DBT_USERMEM;
  601. key.size = klen;
  602. //make the value (row)
  603. memset(&data, 0, sizeof(DBT));
  604. memset(dbuf, 0, MAX_ROW_SIZE);
  605. if ( (ret = km_bdblib_valtochar(_tp, lkey, dbuf, &dlen, _v, _n, BDB_VALUE)) != 0 )
  606. { LM_ERR("Error in km_bdblib_valtochar \n");
  607. ret = -9;
  608. goto error;
  609. }
  610. data.data = dbuf;
  611. data.ulen = MAX_ROW_SIZE;
  612. data.flags = DB_DBT_USERMEM;
  613. data.size = dlen;
  614. if ((ret = db->put(db, NULL, &key, &data, 0)) == 0)
  615. {
  616. km_bdblib_log(JLOG_INSERT, _tp, dbuf, dlen);
  617. #ifdef BDB_EXTRA_DEBUG
  618. LM_DBG("INSERT\nKEY: [%.*s]\nDATA: [%.*s]\n"
  619. , (int) key.size
  620. , (char *)key.data
  621. , (int) data.size
  622. , (char *)data.data);
  623. #endif
  624. }
  625. else
  626. { /*Berkeley DB error handler*/
  627. switch(ret)
  628. {
  629. /*The following are all critical/fatal */
  630. case DB_LOCK_DEADLOCK:
  631. /* The operation was selected to resolve a deadlock. */
  632. case DB_RUNRECOVERY:
  633. default:
  634. LM_CRIT("DB->put error: %s.\n", db_strerror(ret));
  635. km_bdblib_recover(_tp, ret);
  636. goto error;
  637. }
  638. }
  639. error:
  640. if(lkey)
  641. pkg_free(lkey);
  642. return ret;
  643. }
  644. /*
  645. * Delete a row from table
  646. *
  647. * To delete ALL rows:
  648. * do Not specify any keys, or values, and _n <=0
  649. *
  650. */
  651. int bdb_delete(db1_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, int _n)
  652. {
  653. tbl_cache_p _tbc = NULL;
  654. table_p _tp = NULL;
  655. char kbuf[MAX_ROW_SIZE];
  656. int i, j, ret, klen;
  657. int *lkey=NULL;
  658. DBT key;
  659. DB *db;
  660. DBC *dbcp;
  661. i = j = ret = 0;
  662. klen=MAX_ROW_SIZE;
  663. if (_op)
  664. return ( _bdb_delete_cursor(_h, _k, _op, _v, _n) );
  665. if ((!_h) || !CON_TABLE(_h))
  666. return -1;
  667. _tbc = km_bdblib_get_table(BDB_CON_CONNECTION(_h), (str*)CON_TABLE(_h));
  668. if(!_tbc)
  669. { LM_WARN("table does not exist!\n");
  670. return -3;
  671. }
  672. _tp = _tbc->dtp;
  673. if(!_tp)
  674. { LM_WARN("table not loaded!\n");
  675. return -4;
  676. }
  677. #ifdef BDB_EXTRA_DEBUG
  678. LM_DBG("DELETE in %.*s\n", _tp->name.len, _tp->name.s );
  679. #endif
  680. db = _tp->db;
  681. memset(&key, 0, sizeof(DBT));
  682. memset(kbuf, 0, klen);
  683. if(!_k || !_v || _n<=0)
  684. {
  685. /* Acquire a cursor for the database. */
  686. if ((ret = db->cursor(db, NULL, &dbcp, DB_WRITECURSOR) ) != 0)
  687. { LM_ERR("Error creating cursor\n");
  688. goto error;
  689. }
  690. while ((ret = dbcp->c_get(dbcp, &key, NULL, DB_NEXT)) == 0)
  691. {
  692. if(!strncasecmp((char*)key.data,"METADATA",8))
  693. continue;
  694. #ifdef BDB_EXTRA_DEBUG
  695. LM_DBG("KEY: [%.*s]\n"
  696. , (int) key.size
  697. , (char *)key.data);
  698. #endif
  699. ret = dbcp->c_del(dbcp, 0);
  700. }
  701. dbcp->CLOSE_CURSOR(dbcp);
  702. return 0;
  703. }
  704. lkey = bdb_get_colmap(_tp, _k, _n);
  705. if(!lkey) return -5;
  706. /* make the key */
  707. if ( (ret = km_bdblib_valtochar(_tp, lkey, kbuf, &klen, _v, _n, BDB_KEY)) != 0 )
  708. { LM_ERR("Error in bdblib_makekey\n");
  709. ret = -6;
  710. goto error;
  711. }
  712. key.data = kbuf;
  713. key.ulen = MAX_ROW_SIZE;
  714. key.flags = DB_DBT_USERMEM;
  715. key.size = klen;
  716. if ((ret = db->del(db, NULL, &key, 0)) == 0)
  717. {
  718. km_bdblib_log(JLOG_DELETE, _tp, kbuf, klen);
  719. #ifdef BDB_EXTRA_DEBUG
  720. LM_DBG("DELETED ROW \n KEY: %s \n", (char *)key.data);
  721. #endif
  722. }
  723. else
  724. { /*Berkeley DB error handler*/
  725. switch(ret){
  726. case DB_NOTFOUND:
  727. ret = 0;
  728. break;
  729. /*The following are all critical/fatal */
  730. case DB_LOCK_DEADLOCK:
  731. /* The operation was selected to resolve a deadlock. */
  732. case DB_SECONDARY_BAD:
  733. /* A secondary index references a nonexistent primary key. */
  734. case DB_RUNRECOVERY:
  735. default:
  736. LM_CRIT("DB->del error: %s.\n"
  737. , db_strerror(ret));
  738. km_bdblib_recover(_tp, ret);
  739. goto error;
  740. }
  741. }
  742. ret = 0;
  743. error:
  744. if(lkey)
  745. pkg_free(lkey);
  746. return ret;
  747. }
  748. /*
  749. _bdb_delete_cursor -- called from bdb_delete when the query involves operators
  750. other than equal '='. Adds support for queries like this:
  751. DELETE from SomeTable WHERE _k[0] < _v[0]
  752. In this case, the keys _k are not the actually schema keys, so we need to
  753. iterate via cursor to perform this operation.
  754. */
  755. int _bdb_delete_cursor(db1_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, int _n)
  756. {
  757. tbl_cache_p _tbc = NULL;
  758. table_p _tp = NULL;
  759. db1_res_t* _r = NULL;
  760. char kbuf[MAX_ROW_SIZE];
  761. char dbuf[MAX_ROW_SIZE];
  762. int i, ret, klen=MAX_ROW_SIZE;
  763. DBT key, data;
  764. DB *db;
  765. DBC *dbcp;
  766. int *lkey=NULL;
  767. i = ret = 0;
  768. if ((!_h) || !CON_TABLE(_h))
  769. return -1;
  770. _tbc = km_bdblib_get_table(BDB_CON_CONNECTION(_h), (str*)CON_TABLE(_h));
  771. if(!_tbc)
  772. { LM_WARN("table does not exist!\n");
  773. return -3;
  774. }
  775. _tp = _tbc->dtp;
  776. if(!_tp)
  777. { LM_WARN("table not loaded!\n");
  778. return -4;
  779. }
  780. #ifdef BDB_EXTRA_DEBUG
  781. LM_DBG("DELETE by cursor in %.*s\n", _tp->name.len, _tp->name.s );
  782. #endif
  783. if(_k)
  784. { lkey = bdb_get_colmap(_tp, _k, _n);
  785. if(!lkey)
  786. { ret = -1;
  787. goto error;
  788. }
  789. }
  790. /* create an empty db1_res_t which gets returned even if no result */
  791. _r = db_new_result();
  792. if (!_r)
  793. { LM_ERR("no memory for result \n");
  794. }
  795. RES_ROW_N(_r) = 0;
  796. /* fill in the col part of db1_res_t */
  797. if ((ret = bdb_get_columns(_tp, _r, 0, 0)) != 0)
  798. { LM_ERR("Error while getting column names\n");
  799. goto error;
  800. }
  801. db = _tp->db;
  802. memset(&key, 0, sizeof(DBT));
  803. memset(kbuf, 0, klen);
  804. memset(&data, 0, sizeof(DBT));
  805. memset(dbuf, 0, MAX_ROW_SIZE);
  806. data.data = dbuf;
  807. data.ulen = MAX_ROW_SIZE;
  808. data.flags = DB_DBT_USERMEM;
  809. /* Acquire a cursor for the database. */
  810. if ((ret = db->cursor(db, NULL, &dbcp, DB_WRITECURSOR)) != 0)
  811. { LM_ERR("Error creating cursor\n");
  812. }
  813. while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0)
  814. {
  815. if(!strncasecmp((char*)key.data,"METADATA",8))
  816. continue;
  817. /*fill in the row part of db1_res_t */
  818. if ((ret=bdb_convert_row( _r, dbuf, 0)) < 0)
  819. { LM_ERR("Error while converting row\n");
  820. goto error;
  821. }
  822. if(bdb_row_match(_k, _op, _v, _n, _r, lkey ))
  823. {
  824. #ifdef BDB_EXTRA_DEBUG
  825. LM_DBG("DELETE ROW by KEY: [%.*s]\n", (int) key.size,
  826. (char *)key.data);
  827. #endif
  828. if((ret = dbcp->c_del(dbcp, 0)) != 0)
  829. {
  830. /* Berkeley DB error handler */
  831. LM_CRIT("DB->get error: %s.\n", db_strerror(ret));
  832. km_bdblib_recover(_tp,ret);
  833. }
  834. }
  835. memset(dbuf, 0, MAX_ROW_SIZE);
  836. db_free_rows( _r);
  837. }
  838. ret = 0;
  839. error:
  840. if(dbcp)
  841. dbcp->CLOSE_CURSOR(dbcp);
  842. if(_r)
  843. db_free_result(_r);
  844. if(lkey)
  845. pkg_free(lkey);
  846. return ret;
  847. }
  848. /*
  849. * Updates a row in table
  850. * Limitation: only knows how to update a single row
  851. *
  852. * _con: structure representing database connection
  853. * _k: key names
  854. * _op: operators
  855. * _v: values of the keys that must match
  856. * _uk: update keys; cols that need to be updated
  857. * _uv: update values; col values that need to be commited
  858. * _un: number of rows to update
  859. */
  860. int bdb_update(db1_con_t* _con, db_key_t* _k, db_op_t* _op, db_val_t* _v,
  861. db_key_t* _uk, db_val_t* _uv, int _n, int _un)
  862. {
  863. char *c, *t;
  864. int ret, i, qcol, len, sum;
  865. int *lkey=NULL;
  866. tbl_cache_p _tbc = NULL;
  867. table_p _tp = NULL;
  868. char kbuf[MAX_ROW_SIZE];
  869. char qbuf[MAX_ROW_SIZE];
  870. char ubuf[MAX_ROW_SIZE];
  871. DBT key, qdata, udata;
  872. DB *db;
  873. sum = ret = i = qcol = len = 0;
  874. if (!_con || !CON_TABLE(_con) || !_uk || !_uv || _un <= 0)
  875. return -1;
  876. _tbc = km_bdblib_get_table(BDB_CON_CONNECTION(_con), (str*)CON_TABLE(_con));
  877. if(!_tbc)
  878. { LM_ERR("table does not exist\n");
  879. return -1;
  880. }
  881. _tp = _tbc->dtp;
  882. if(!_tp)
  883. { LM_ERR("table not loaded\n");
  884. return -1;
  885. }
  886. db = _tp->db;
  887. if(!db)
  888. { LM_ERR("DB null ptr\n");
  889. return -1;
  890. }
  891. #ifdef BDB_EXTRA_DEBUG
  892. LM_DBG("UPDATE in %.*s\n", _tp->name.len, _tp->name.s);
  893. if (_op) LM_DBG("DONT-CARE : _op: operators for refining query \n");
  894. #endif
  895. memset(&key, 0, sizeof(DBT));
  896. memset(kbuf, 0, MAX_ROW_SIZE);
  897. memset(&qdata, 0, sizeof(DBT));
  898. memset(qbuf, 0, MAX_ROW_SIZE);
  899. qdata.data = qbuf;
  900. qdata.ulen = MAX_ROW_SIZE;
  901. qdata.flags = DB_DBT_USERMEM;
  902. if(_k)
  903. { lkey = bdb_get_colmap(_tbc->dtp, _k, _n);
  904. if(!lkey) return -4;
  905. }
  906. else
  907. {
  908. LM_ERR("Null keys in update _k=0 \n");
  909. return -1;
  910. }
  911. len = MAX_ROW_SIZE;
  912. if ( (ret = km_bdblib_valtochar(_tp, lkey, kbuf, &len, _v, _n, BDB_KEY)) != 0 )
  913. { LM_ERR("Error in query key \n");
  914. goto cleanup;
  915. }
  916. if(lkey) pkg_free(lkey);
  917. key.data = kbuf;
  918. key.ulen = MAX_ROW_SIZE;
  919. key.flags = DB_DBT_USERMEM;
  920. key.size = len;
  921. /*stage 1: QUERY Berkely DB*/
  922. if ((ret = db->get(db, NULL, &key, &qdata, 0)) == 0)
  923. {
  924. #ifdef BDB_EXTRA_DEBUG
  925. LM_DBG("RESULT\nKEY: [%.*s]\nDATA: [%.*s]\n"
  926. , (int) key.size
  927. , (char *)key.data
  928. , (int) qdata.size
  929. , (char *)qdata.data);
  930. #endif
  931. }
  932. else
  933. { goto db_error;
  934. }
  935. /* stage 2: UPDATE row with new values */
  936. /* map the provided keys to those in our schema */
  937. lkey = bdb_get_colmap(_tbc->dtp, _uk, _un);
  938. if(!lkey) return -4;
  939. /* build a new row for update data (udata) */
  940. memset(&udata, 0, sizeof(DBT));
  941. memset(ubuf, 0, MAX_ROW_SIZE);
  942. /* loop over each column of the qbuf and copy it to our new ubuf unless
  943. its a field that needs to update
  944. */
  945. c = strtok(qbuf, DELIM);
  946. t = ubuf;
  947. while( c!=NULL)
  948. { char* delim = DELIM;
  949. int k;
  950. len = strlen(c);
  951. sum+=len;
  952. if(sum > MAX_ROW_SIZE)
  953. { LM_ERR("value too long for string \n");
  954. ret = -3;
  955. goto cleanup;
  956. }
  957. for(i=0;i<_un;i++)
  958. {
  959. k = lkey[i];
  960. if (qcol == k)
  961. { /* update this col */
  962. int j = MAX_ROW_SIZE - sum;
  963. if( km_bdb_val2str( &_uv[i], t, &j) )
  964. { LM_ERR("value too long for string \n");
  965. ret = -3;
  966. goto cleanup;
  967. }
  968. goto next;
  969. }
  970. }
  971. /* copy original column to the new column */
  972. strncpy(t, c, len);
  973. next:
  974. t+=len;
  975. /* append DELIM */
  976. sum += DELIM_LEN;
  977. if(sum > MAX_ROW_SIZE)
  978. { LM_ERR("value too long for string \n");
  979. ret = -3;
  980. goto cleanup;
  981. }
  982. strncpy(t, delim, DELIM_LEN);
  983. t += DELIM_LEN;
  984. c = strtok(NULL, DELIM);
  985. qcol++;
  986. }
  987. ubuf[sum] = '0';
  988. udata.data = ubuf;
  989. udata.ulen = MAX_ROW_SIZE;
  990. udata.flags = DB_DBT_USERMEM;
  991. udata.size = sum;
  992. #ifdef BDB_EXTRA_DEBUG
  993. LM_DBG("MODIFIED Data\nKEY: [%.*s]\nDATA: [%.*s]\n"
  994. , (int) key.size
  995. , (char *)key.data
  996. , (int) udata.size
  997. , (char *)udata.data);
  998. #endif
  999. /* stage 3: DELETE old row using key*/
  1000. if ((ret = db->del(db, NULL, &key, 0)) == 0)
  1001. {
  1002. #ifdef BDB_EXTRA_DEBUG
  1003. LM_DBG("DELETED ROW\nKEY: %s \n", (char *)key.data);
  1004. #endif
  1005. }
  1006. else
  1007. { goto db_error;
  1008. }
  1009. /* stage 4: INSERT new row with key*/
  1010. if ((ret = db->put(db, NULL, &key, &udata, 0)) == 0)
  1011. {
  1012. km_bdblib_log(JLOG_UPDATE, _tp, ubuf, sum);
  1013. #ifdef BDB_EXTRA_DEBUG
  1014. LM_DBG("INSERT \nKEY: [%.*s]\nDATA: [%.*s]\n"
  1015. , (int) key.size
  1016. , (char *)key.data
  1017. , (int) udata.size
  1018. , (char *)udata.data);
  1019. #endif
  1020. }
  1021. else
  1022. { goto db_error;
  1023. }
  1024. #ifdef BDB_EXTRA_DEBUG
  1025. LM_DBG("UPDATE COMPLETE \n");
  1026. #endif
  1027. cleanup:
  1028. if(lkey)
  1029. pkg_free(lkey);
  1030. return ret;
  1031. db_error:
  1032. /*Berkeley DB error handler*/
  1033. switch(ret)
  1034. {
  1035. case DB_NOTFOUND:
  1036. #ifdef BDB_EXTRA_DEBUG
  1037. LM_DBG("NO RESULT \n");
  1038. #endif
  1039. return -1;
  1040. /* The following are all critical/fatal */
  1041. case DB_LOCK_DEADLOCK:
  1042. /* The operation was selected to resolve a deadlock. */
  1043. case DB_SECONDARY_BAD:
  1044. /* A secondary index references a nonexistent primary key.*/
  1045. case DB_RUNRECOVERY:
  1046. default:
  1047. LM_CRIT("DB->get error: %s.\n", db_strerror(ret));
  1048. km_bdblib_recover(_tp,ret);
  1049. }
  1050. if(lkey)
  1051. pkg_free(lkey);
  1052. return ret;
  1053. }