1 /* dbcache.c - manage cache of open databases */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
20 #include "lutil_hash.h"
22 /* Pass-thru hash function. Since the indexer is already giving us hash
23 * values as keys, we don't need BDB to re-hash them.
25 #if LUTIL_HASH_BYTES == 4
33 u_int32_t *ret = (u_int32_t *)bytes;
46 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
47 struct bdb_db_info *db;
52 for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
53 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
54 *dbout = bdb->bi_databases[i]->bdi_db;
59 ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
61 /* check again! may have been added by another thread */
62 for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
63 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
64 *dbout = bdb->bi_databases[i]->bdi_db;
65 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
70 if( i >= BDB_INDICES ) {
71 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
75 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
77 db->bdi_name = ch_strdup( name );
79 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
81 Debug( LDAP_DEBUG_ANY,
82 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
83 bdb->bi_dbenv_home, db_strerror(rc), rc );
84 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
88 rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
89 #if LUTIL_HASH_BYTES == 4
90 rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
93 rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
94 rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
97 file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
98 sprintf( file, "%s" BDB_SUFFIX, name );
100 rc = db->bdi_db->open( db->bdi_db,
102 DB_HASH, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
103 bdb->bi_dbenv_mode );
108 Debug( LDAP_DEBUG_ANY,
109 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
110 name, db_strerror(rc), rc );
111 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
115 bdb->bi_databases[i+1] = NULL;
116 bdb->bi_databases[i] = db;
117 bdb->bi_ndatabases = i+1;
121 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );