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 /* Pass-thru hash function. Since the indexer is already giving us hash
32 * values as keys, we don't need BDB to re-hash them.
42 unsigned char *dst = (unsigned char *)&ret;
43 const unsigned char *src = (const unsigned char *)bytes;
45 if ( length > sizeof(u_int32_t) )
46 length = sizeof(u_int32_t);
63 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
64 struct bdb_db_info *db;
69 for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
70 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
71 *dbout = bdb->bi_databases[i]->bdi_db;
76 ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
78 /* check again! may have been added by another thread */
79 for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
80 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
81 *dbout = bdb->bi_databases[i]->bdi_db;
82 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
87 if( i >= BDB_INDICES ) {
88 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
92 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
94 db->bdi_name = ch_strdup( name );
96 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
99 LDAP_LOG ( CACHE, ERR,
100 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
101 bdb->bi_dbenv_home, db_strerror(rc), rc );
103 Debug( LDAP_DEBUG_ANY,
104 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
105 bdb->bi_dbenv_home, db_strerror(rc), rc );
107 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
111 rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
112 rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
113 rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
114 rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
116 file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
117 sprintf( file, "%s" BDB_SUFFIX, name );
122 rc = DB_OPEN( db->bdi_db,
123 file, NULL /* name */,
124 DB_HASH, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
125 bdb->bi_dbenv_mode );
131 LDAP_LOG ( CACHE, ERR,
132 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
133 name, db_strerror(rc), rc );
135 Debug( LDAP_DEBUG_ANY,
136 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
137 name, db_strerror(rc), rc );
139 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
143 bdb->bi_databases[i] = db;
144 bdb->bi_ndatabases = i+1;
148 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );