int bdb_con(db_con_t* con) { bdb_con_t* bcon; bdb_uri_t* buri; buri = DB_GET_PAYLOAD(con->uri); /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ bcon = (bdb_con_t*)db_pool_get(con->uri); if (bcon) { DBG("bdb: Connection to %s found in connection pool\n", buri->uri); goto found; } bcon = (bdb_con_t*)pkg_malloc(sizeof(bdb_con_t)); if (!bcon) { ERR("bdb: No memory left\n"); goto error; } memset(bcon, '\0', sizeof(bdb_con_t)); if (db_pool_entry_init(&bcon->gen, bdb_con_free, con->uri) < 0) goto error; DBG("bdb: Preparing new connection to %s\n", buri->uri); if(bdb_is_database(buri->path.s)!=0) { ERR("bdb: database [%.*s] does not exists!\n", buri->path.len, buri->path.s); goto error; } /* Put the newly created BDB connection into the pool */ db_pool_put((struct db_pool_entry*)bcon); DBG("bdb: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, bcon); con->connect = bdb_con_connect; con->disconnect = bdb_con_disconnect; return 0; error: if (bcon) { db_pool_entry_free(&bcon->gen); pkg_free(bcon); } return -1; }
bdb_db_p bdblib_get_db(str *dirpath) { int rc; bdb_db_p _db_p=NULL; if(dirpath==0 || dirpath->s==NULL || dirpath->s[0]=='\0') return NULL; if(_bdb_parms==NULL) { ERR("bdb: cache is not initialized! Check if you loaded bdb " "before any other module that uses it.\n"); return NULL; } if(!bdb_is_database(dirpath->s)) { ERR("bdb: database [%.*s] does not exists!\n", dirpath->len , dirpath->s); return NULL; } _db_p = (bdb_db_p)pkg_malloc(sizeof(bdb_db_t)); if(!_db_p) { ERR("no private memory for dbenv_t.\n"); pkg_free(_db_p); return NULL; } _db_p->name.s = (char*)pkg_malloc(dirpath->len*sizeof(char)); memcpy(_db_p->name.s, dirpath->s, dirpath->len); _db_p->name.len = dirpath->len; if ((rc = bdblib_create_dbenv(&(_db_p->dbenv), dirpath->s)) != 0) { ERR("bdblib_create_dbenv failed"); pkg_free(_db_p->name.s); pkg_free(_db_p); return NULL; } _db_p->tables=NULL; return _db_p; }