* The function is called to create a handle to a db table. * * On startup, we do not create any of the db handles. * Instead it is done on first-use (lazy-initialized) to only create handles to * files (db) that we require. * * There is one db file per opensips table (eg. acc), and they should exist * in your DB_PATH (refer to opensips-cli) directory. * * This function does _not_ create t
| 558 | * |
| 559 | */ |
| 560 | table_p bdblib_create_table(database_p _db, str *_s) |
| 561 | { |
| 562 | |
| 563 | int rc,i,flags; |
| 564 | DB *bdb = NULL; |
| 565 | table_p tp = NULL; |
| 566 | char tblname[MAX_TABLENAME_SIZE]; |
| 567 | |
| 568 | if(!_db || !_db->dbenv) |
| 569 | { |
| 570 | LM_ERR("no database_p or dbenv.\n"); |
| 571 | return NULL; |
| 572 | } |
| 573 | |
| 574 | tp = (table_p)pkg_malloc(sizeof(table_t)); |
| 575 | if(!tp) |
| 576 | { |
| 577 | LM_ERR("no private memory for table_t.\n"); |
| 578 | return NULL; |
| 579 | } |
| 580 | |
| 581 | if ((rc = db_create(&bdb, _db->dbenv, 0)) != 0) |
| 582 | { |
| 583 | _db->dbenv->err(_db->dbenv, rc, "database create"); |
| 584 | LM_ERR("error in db_create, bdb error: %s.\n",db_strerror(rc)); |
| 585 | pkg_free(tp); |
| 586 | return NULL; |
| 587 | } |
| 588 | |
| 589 | memset(tblname, 0, MAX_TABLENAME_SIZE); |
| 590 | memcpy(tblname, _s->s, _s->len); |
| 591 | |
| 592 | #ifdef BDB_EXTRA_DEBUG |
| 593 | LM_DBG("CREATE TABLE = %s\n", tblname); |
| 594 | #endif |
| 595 | |
| 596 | flags = DB_THREAD; |
| 597 | |
| 598 | if ((rc = bdb->open(bdb, NULL, tblname, NULL, DB_HASH, flags, 0664)) != 0) |
| 599 | { |
| 600 | _db->dbenv->err(_db->dbenv, rc, "DB->open: %s", tblname); |
| 601 | LM_ERR("bdb open failed: %s.\n",db_strerror(rc)); |
| 602 | pkg_free(tp); |
| 603 | return NULL; |
| 604 | } |
| 605 | |
| 606 | if(!lock_init(&tp->sem)) |
| 607 | { |
| 608 | goto error; |
| 609 | } |
| 610 | |
| 611 | tp->name.s = (char*)pkg_malloc(_s->len*sizeof(char)); |
| 612 | memcpy(tp->name.s, _s->s, _s->len); |
| 613 | tp->name.len = _s->len; |
| 614 | tp->db=bdb; |
| 615 | tp->ncols=0; |
| 616 | tp->nkeys=0; |
| 617 | tp->ro=0; /*0=ReadWrite ; 1=ReadOnly*/ |
no test coverage detected