1 /* dbcache.c - manage cache of open databases */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/socket.h>
23 #include <ac/string.h>
29 #include "lutil_hash.h"
31 #ifdef BDB_INDEX_USE_HASH
32 /* Pass-thru hash function. Since the indexer is already giving us hash
33 * values as keys, we don't need BDB to re-hash them.
43 unsigned char *dst = (unsigned char *)&ret;
44 const unsigned char *src = (const unsigned char *)bytes;
46 if ( length > sizeof(u_int32_t) )
47 length = sizeof(u_int32_t);
55 #define BDB_INDEXTYPE DB_HASH
57 #define BDB_INDEXTYPE DB_BTREE
68 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
69 struct bdb_db_info *db;
74 for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
75 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
76 *dbout = bdb->bi_databases[i]->bdi_db;
81 ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
83 /* check again! may have been added by another thread */
84 for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
85 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
86 *dbout = bdb->bi_databases[i]->bdi_db;
87 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
92 if( i >= BDB_INDICES ) {
93 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
97 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
99 db->bdi_name = ch_strdup( name );
101 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
104 LDAP_LOG ( CACHE, ERR,
105 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
106 bdb->bi_dbenv_home, db_strerror(rc), rc );
108 Debug( LDAP_DEBUG_ANY,
109 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
110 bdb->bi_dbenv_home, db_strerror(rc), rc );
112 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
116 rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
117 #ifdef BDB_INDEX_USE_HASH
118 rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
120 rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
121 rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
123 file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
124 sprintf( file, "%s" BDB_SUFFIX, name );
129 rc = DB_OPEN( db->bdi_db,
130 file, NULL /* name */,
131 BDB_INDEXTYPE, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
132 bdb->bi_dbenv_mode );
138 LDAP_LOG ( CACHE, ERR,
139 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
140 name, db_strerror(rc), rc );
142 Debug( LDAP_DEBUG_ANY,
143 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
144 name, db_strerror(rc), rc );
146 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
150 bdb->bi_databases[i] = db;
151 bdb->bi_ndatabases = i+1;
155 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );