]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/dbcache.c
Fixed invalid cookie in pagedResults control (ITS#3089)
[openldap] / servers / slapd / back-bdb / dbcache.c
index 3eddc2ab1a29537deb82b9c8d4abdc8809b381c7..23d964deaa3bd45446d27ffc150fdc4c4a1c3d86 100644 (file)
@@ -1,8 +1,17 @@
 /* dbcache.c - manage cache of open databases */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2000-2004 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
@@ -19,6 +28,7 @@
 #include "back-bdb.h"
 #include "lutil_hash.h"
 
+#ifdef BDB_INDEX_USE_HASH
 /* Pass-thru hash function. Since the indexer is already giving us hash
  * values as keys, we don't need BDB to re-hash them.
  */
@@ -42,60 +52,45 @@ bdb_db_hash(
        }
        return ret;
 }
+#define        BDB_INDEXTYPE   DB_HASH
+#else
+#define        BDB_INDEXTYPE   DB_BTREE
+#endif
 
 int
 bdb_db_cache(
        Backend *be,
-       DB_TXN *tid,
        const char *name,
        DB **dbout )
 {
        int i;
        int rc;
-       int flags;
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
        struct bdb_db_info *db;
        char *file;
-       DBT lockobj;
-       DB_LOCK lock;
-       u_int32_t locker = 0;
 
        *dbout = NULL;
 
-       for( i=BDB_NDB; i < BDB_INDICES && bdb->bi_databases[i]; i++ ) {
+       for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
                if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
                        *dbout = bdb->bi_databases[i]->bdi_db;
                        return 0;
                }
        }
 
-       lockobj.data = "bdb_db_cache";
-       lockobj.size = sizeof("bdb_db_cache");
-
-       if (tid) {
-               locker = TXN_ID( tid );
-       } else {
-#ifdef BDB_REUSE_LOCKERS
-#define        op      NULL    /* implicit arg in LOCK_ID */
-#endif
-               rc = LOCK_ID( bdb->bi_dbenv, &locker );
-               if (rc) return rc;
-       }
-       rc = LOCK_GET( bdb->bi_dbenv, locker, 0, &lockobj,
-               DB_LOCK_WRITE, &lock );
-       if (rc) return rc;
+       ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
 
        /* check again! may have been added by another thread */
-       for( i=BDB_NDB; i < BDB_INDICES && bdb->bi_databases[i]; i++ ) {
+       for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
                if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
                        *dbout = bdb->bi_databases[i]->bdi_db;
-                       LOCK_PUT( bdb->bi_dbenv, &lock);
+                       ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
                        return 0;
                }
        }
 
        if( i >= BDB_INDICES ) {
-               LOCK_PUT( bdb->bi_dbenv, &lock);
+               ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
                return -1;
        }
 
@@ -114,12 +109,14 @@ bdb_db_cache(
                        "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
                        bdb->bi_dbenv_home, db_strerror(rc), rc );
 #endif
-               LOCK_PUT( bdb->bi_dbenv, &lock);
+               ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
                return rc;
        }
 
        rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
+#ifdef BDB_INDEX_USE_HASH
        rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
+#endif
        rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
        rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
 
@@ -129,11 +126,9 @@ bdb_db_cache(
 #ifdef HAVE_EBCDIC
        __atoe( file );
 #endif
-       flags = bdb->bi_db_opflags | DB_CREATE | DB_THREAD;
-       if ( !tid ) flags |= DB_AUTO_COMMIT;
-       rc = DB_OPEN( db->bdi_db, tid,
+       rc = DB_OPEN( db->bdi_db,
                file, NULL /* name */,
-               DB_HASH, flags,
+               BDB_INDEXTYPE, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
                bdb->bi_dbenv_mode );
 
        ch_free( file );
@@ -148,7 +143,7 @@ bdb_db_cache(
                        "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
                        name, db_strerror(rc), rc );
 #endif
-               LOCK_PUT( bdb->bi_dbenv, &lock);
+               ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
                return rc;
        }
 
@@ -157,6 +152,6 @@ bdb_db_cache(
 
        *dbout = db->bdi_db;
 
-       LOCK_PUT( bdb->bi_dbenv, &lock );
+       ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
        return 0;
 }