1 /* dbcache.c - manage cache of open databases */
4 * Copyright 1998-2001 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
29 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30 struct bdb_db_info *db;
35 for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
36 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
37 *dbout = bdb->bi_databases[i]->bdi_db;
42 ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
44 /* check again! may have been added by another thread */
45 for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
46 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
47 *dbout = bdb->bi_databases[i]->bdi_db;
48 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
53 if( i >= BDB_INDICES ) {
54 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
58 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
60 db->bdi_name = ch_strdup( name );
62 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
64 Debug( LDAP_DEBUG_ANY,
65 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
66 bdb->bi_dbenv_home, db_strerror(rc), rc );
67 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
71 rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
73 rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
74 rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
77 file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
78 sprintf( file, "%s" BDB_SUFFIX, name );
80 rc = db->bdi_db->open( db->bdi_db,
82 DB_BTREE, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
88 Debug( LDAP_DEBUG_ANY,
89 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
90 name, db_strerror(rc), rc );
91 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
95 bdb->bi_databases[i+1] = NULL;
96 bdb->bi_databases[i] = db;
97 bdb->bi_ndatabases = i+1;
101 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );