1 /* dbcache.c - manage cache of open databases */
4 * Copyright 1998-2003 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);
56 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
57 struct bdb_db_info *db;
65 for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
66 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
67 *dbout = bdb->bi_databases[i]->bdi_db;
72 lockobj.data = "bdb_db_cache";
73 lockobj.size = sizeof("bdb_db_cache");
76 locker = TXN_ID( tid );
78 #ifdef BDB_REUSE_LOCKERS
79 #define op NULL /* implicit arg in LOCK_ID */
81 rc = LOCK_ID( bdb->bi_dbenv, &locker );
84 rc = LOCK_GET( bdb->bi_dbenv, locker, 0, &lockobj,
85 DB_LOCK_WRITE, &lock );
88 /* check again! may have been added by another thread */
89 for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
90 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
91 *dbout = bdb->bi_databases[i]->bdi_db;
92 LOCK_PUT( bdb->bi_dbenv, &lock);
97 if( i >= BDB_INDICES ) {
98 LOCK_PUT( bdb->bi_dbenv, &lock);
102 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
104 db->bdi_name = ch_strdup( name );
106 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
109 LDAP_LOG ( CACHE, ERR,
110 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
111 bdb->bi_dbenv_home, db_strerror(rc), rc );
113 Debug( LDAP_DEBUG_ANY,
114 "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
115 bdb->bi_dbenv_home, db_strerror(rc), rc );
117 LOCK_PUT( bdb->bi_dbenv, &lock);
121 rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
122 rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
123 rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
124 rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
126 file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
127 sprintf( file, "%s" BDB_SUFFIX, name );
132 flags = bdb->bi_db_opflags | DB_CREATE | DB_THREAD;
133 if ( !tid ) flags |= DB_AUTO_COMMIT;
134 rc = DB_OPEN( db->bdi_db, tid,
135 file, NULL /* name */,
137 bdb->bi_dbenv_mode );
143 LDAP_LOG ( CACHE, ERR,
144 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
145 name, db_strerror(rc), rc );
147 Debug( LDAP_DEBUG_ANY,
148 "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
149 name, db_strerror(rc), rc );
151 LOCK_PUT( bdb->bi_dbenv, &lock);
155 bdb->bi_databases[i+1] = NULL;
156 bdb->bi_databases[i] = db;
157 bdb->bi_ndatabases = i+1;
161 LOCK_PUT( bdb->bi_dbenv, &lock );