1 /* dbcache.c - manage cache of open databases */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 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
60 /* If a configured size is found, return it, otherwise return 0 */
67 struct bdb_db_pgsize *bp;
70 for ( bp = bdb->bi_pagesizes; bp; bp=bp->bdp_next ) {
71 rc = strncmp( name->bv_val, bp->bdp_name.bv_val, name->bv_len );
73 if ( name->bv_len == bp->bdp_name.bv_len )
75 if ( name->bv_len < bp->bdp_name.bv_len &&
76 bp->bdp_name.bv_val[name->bv_len] == '.' )
91 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
92 struct bdb_db_info *db;
97 for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
98 if( !ber_bvcmp( &bdb->bi_databases[i]->bdi_name, name) ) {
99 *dbout = bdb->bi_databases[i]->bdi_db;
104 ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
106 /* check again! may have been added by another thread */
107 for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
108 if( !ber_bvcmp( &bdb->bi_databases[i]->bdi_name, name) ) {
109 *dbout = bdb->bi_databases[i]->bdi_db;
110 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
115 if( i >= BDB_INDICES ) {
116 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
120 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
122 ber_dupbv( &db->bdi_name, name );
124 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
126 Debug( LDAP_DEBUG_ANY,
127 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
128 bdb->bi_dbenv_home, db_strerror(rc), rc );
129 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
134 if( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
135 rc = db->bdi_db->set_flags( db->bdi_db, DB_ENCRYPT );
137 Debug( LDAP_DEBUG_ANY,
138 "bdb_db_cache: db set_flags(DB_ENCRYPT)(%s) failed: %s (%d)\n",
139 bdb->bi_dbenv_home, db_strerror(rc), rc );
140 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
141 db->bdi_db->close( db->bdi_db, 0 );
147 /* If no explicit size set, use the default */
148 flags = bdb_db_findsize( bdb, name );
149 if ( !flags ) flags = BDB_PAGESIZE;
150 rc = db->bdi_db->set_pagesize( db->bdi_db, flags );
152 #ifdef BDB_INDEX_USE_HASH
153 rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
155 rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
157 file = ch_malloc( db->bdi_name.bv_len + sizeof(BDB_SUFFIX) );
158 strcpy( file, db->bdi_name.bv_val );
159 strcpy( file+db->bdi_name.bv_len, BDB_SUFFIX );
164 flags = DB_CREATE | DB_THREAD;
165 #ifdef DB_AUTO_COMMIT
166 if ( !( slapMode & SLAP_TOOL_QUICK ))
167 flags |= DB_AUTO_COMMIT;
169 /* Cannot Truncate when Transactions are in use */
170 if ( (slapMode & (SLAP_TOOL_QUICK|SLAP_TRUNCATE_MODE)) ==
171 (SLAP_TOOL_QUICK|SLAP_TRUNCATE_MODE))
172 flags |= DB_TRUNCATE;
174 rc = DB_OPEN( db->bdi_db,
175 file, NULL /* name */,
176 BDB_INDEXTYPE, bdb->bi_db_opflags | flags, bdb->bi_dbenv_mode );
181 Debug( LDAP_DEBUG_ANY,
182 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
183 name->bv_val, db_strerror(rc), rc );
184 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
188 bdb->bi_databases[i] = db;
189 bdb->bi_ndatabases = i+1;
193 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );