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.
33 unsigned char *dst = (unsigned char *)&ret;
34 const unsigned char *src = (const unsigned char *)bytes;
36 if ( length > sizeof(u_int32_t) )
37 length = sizeof(u_int32_t);
54 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
55 struct bdb_db_info *db;
60 for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
61 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
62 *dbout = bdb->bi_databases[i]->bdi_db;
67 ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
69 /* check again! may have been added by another thread */
70 for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
71 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
72 *dbout = bdb->bi_databases[i]->bdi_db;
73 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
78 if( i >= BDB_INDICES ) {
79 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
83 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
85 db->bdi_name = ch_strdup( name );
87 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
90 LDAP_LOG ( CACHE, ERR,
91 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
92 bdb->bi_dbenv_home, db_strerror(rc), rc );
94 Debug( LDAP_DEBUG_ANY,
95 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
96 bdb->bi_dbenv_home, db_strerror(rc), rc );
98 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
102 rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
103 rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
105 rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
106 rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
109 file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
110 sprintf( file, "%s" BDB_SUFFIX, name );
112 rc = db->bdi_db->open( db->bdi_db,
114 DB_HASH, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
115 bdb->bi_dbenv_mode );
121 LDAP_LOG ( CACHE, ERR,
122 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
123 name, db_strerror(rc), rc );
125 Debug( LDAP_DEBUG_ANY,
126 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
127 name, db_strerror(rc), rc );
129 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
133 bdb->bi_databases[i+1] = NULL;
134 bdb->bi_databases[i] = db;
135 bdb->bi_ndatabases = i+1;
139 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );