]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/id2entry.c
Sync with HEAD for OL 2.4.5
[openldap] / servers / slapd / back-bdb / id2entry.c
index 9a1e7d520c3ad626cdccc0222685cb5cbbc5a82d..98c75bb9fa952e6b69ca561d72b4b2edf30c8c9f 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2000-2005 The OpenLDAP Foundation.
+ * Copyright 2000-2007 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -18,6 +18,7 @@
 
 #include <stdio.h>
 #include <ac/string.h>
+#include <ac/errno.h>
 
 #include "back-bdb.h"
 
@@ -92,14 +93,17 @@ int bdb_id2entry_update(
 int bdb_id2entry(
        BackendDB *be,
        DB_TXN *tid,
+       BDB_LOCKER locker,
        ID id,
        Entry **e )
 {
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
        DB *db = bdb->bi_id2entry->bdi_db;
        DBT key, data;
-       struct berval bv;
-       int rc = 0;
+       DBC *cursor;
+       EntryHeader eh;
+       char buf[16];
+       int rc = 0, off;
        ID nid;
 
        *e = NULL;
@@ -110,21 +114,59 @@ int bdb_id2entry(
        BDB_ID2DISK( id, &nid );
 
        DBTzero( &data );
-       data.flags = DB_DBT_MALLOC;
+       data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
 
        /* fetch it */
-       rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
+       rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
+       if ( rc ) return rc;
+
+       /* Use our own locker if needed */
+       if ( !tid && locker ) {
+               CURSOR_SETLOCKER( cursor, locker );
+       }
+
+       /* Get the nattrs / nvals counts first */
+       data.ulen = data.dlen = sizeof(buf);
+       data.data = buf;
+       rc = cursor->c_get( cursor, &key, &data, DB_SET );
+       if ( rc ) goto finish;
+
+
+       eh.bv.bv_val = buf;
+       eh.bv.bv_len = data.size;
+       rc = entry_header( &eh );
+       if ( rc ) goto finish;
+
+       /* Get the size */
+       data.flags ^= DB_DBT_PARTIAL;
+       data.ulen = 0;
+       rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
+       if ( rc != DB_BUFFER_SMALL ) goto finish;
+
+       /* Allocate a block and retrieve the data */
+       off = eh.data - eh.bv.bv_val;
+       eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
+       eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
+       eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
+       data.data = eh.data;
+       data.ulen = data.size;
+
+       /* skip past already parsed nattr/nvals */
+       eh.data += off;
+
+       rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
+
+finish:
+       cursor->c_close( cursor );
 
        if( rc != 0 ) {
                return rc;
        }
 
-       DBT2bv( &data, &bv );
-
 #ifdef SLAP_ZONE_ALLOC
-       rc = entry_decode(&bv, e, bdb->bi_cache.c_zctx);
+       rc = entry_decode(&eh, e, bdb->bi_cache.c_zctx);
 #else
-       rc = entry_decode(&bv, e);
+       rc = entry_decode(&eh, e);
 #endif
 
        if( rc == 0 ) {
@@ -134,11 +176,11 @@ int bdb_id2entry(
                 * decoded in place.
                 */
 #ifndef SLAP_ZONE_ALLOC
-               ch_free(data.data);
+               ch_free(eh.bv.bv_val);
 #endif
        }
 #ifdef SLAP_ZONE_ALLOC
-       ch_free(data.data);
+       ch_free(eh.bv.bv_val);
 #endif
 
        return rc;
@@ -166,68 +208,31 @@ int bdb_id2entry_delete(
        return rc;
 }
 
-#ifdef SLAP_ZONE_ALLOC
-int bdb_entry_return(
-       struct bdb_info *bdb,
-       Entry *e,
-       int zseq
-)
-#else
 int bdb_entry_return(
        Entry *e
 )
-#endif
 {
-#ifdef SLAP_ZONE_ALLOC
-       if (!slap_zn_validate(bdb->bi_cache.c_zctx, e, zseq)) {
-               return 0;
-       }
-#endif
        /* Our entries are allocated in two blocks; the data comes from
         * the db itself and the Entry structure and associated pointers
         * are allocated in entry_decode. The db data pointer is saved
-        * in e_bv. Since the Entry structure is allocated as a single
-        * block, e_attrs is always a fixed offset from e. The exception
-        * is when an entry has been modified, in which case we also need
-        * to free e_attrs.
+        * in e_bv.
         */
-
-#ifdef LDAP_COMP_MATCH
-       comp_tree_free( e->e_attrs );
-#endif
-       if( !e->e_bv.bv_val ) { /* Entry added by do_add */
-               entry_free( e );
-               return 0;
-       }
-       if( (void *) e->e_attrs != (void *) (e+1)) {
-               attrs_free( e->e_attrs );
-       }
-
-       /* See if the DNs were changed by modrdn */
-       if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
-               e->e_bv.bv_val + e->e_bv.bv_len ) {
-               ch_free(e->e_name.bv_val);
-               ch_free(e->e_nname.bv_val);
+       if ( e->e_bv.bv_val ) {
+               /* See if the DNs were changed by modrdn */
+               if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
+                       e->e_bv.bv_val + e->e_bv.bv_len ) {
+                       ch_free(e->e_name.bv_val);
+                       ch_free(e->e_nname.bv_val);
+               }
                e->e_name.bv_val = NULL;
                e->e_nname.bv_val = NULL;
+               /* In tool mode the e_bv buffer is realloc'd, leave it alone */
+               if( !(slapMode & SLAP_TOOL_MODE) ) {
+                       free( e->e_bv.bv_val );
+               }
+               BER_BVZERO( &e->e_bv );
        }
-#ifndef SLAP_ZONE_ALLOC
-#ifndef BDB_HIER
-       /* In tool mode the e_bv buffer is realloc'd, leave it alone */
-       if( !(slapMode & SLAP_TOOL_MODE) ) {
-               free( e->e_bv.bv_val );
-       }
-#else
-       free( e->e_bv.bv_val );
-#endif /* BDB_HIER */
-#endif /* !SLAP_ZONE_ALLOC */
-
-#ifdef SLAP_ZONE_ALLOC
-       slap_zn_free( e, bdb->bi_cache.c_zctx );
-#else
-       free( e );
-#endif
-
+       entry_free( e );
        return 0;
 }
 
@@ -252,19 +257,17 @@ int bdb_entry_release(
 #endif
                }
                /* free entry and reader or writer lock */
-               if ( op ) {
-                       boi = (struct bdb_op_info *)op->o_private;
-               }
+               boi = (struct bdb_op_info *)op->o_private;
+
                /* lock is freed with txn */
                if ( !boi || boi->boi_txn ) {
-                       bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
+                       bdb_unlocked_cache_return_entry_rw( bdb, e, rw );
                } else {
                        struct bdb_lock_info *bli, *prev;
                        for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
                                bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
                                if ( bli->bli_id == e->e_id ) {
-                                       bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
-                                               e, rw, &bli->bli_lock );
+                                       bdb_cache_return_entry_rw( bdb, e, rw, &bli->bli_lock );
                                        prev->bli_next = bli->bli_next;
                                        op->o_tmpfree( bli, op->o_tmpmemctx );
                                        break;
@@ -315,7 +318,7 @@ int bdb_entry_get(
        int     rc;
        const char *at_name = at ? at->ad_cname.bv_val : "(null)";
 
-       u_int32_t       locker = 0;
+       BDB_LOCKER      locker = 0;
        DB_LOCK         lock;
        int             free_lock_id = 0;
 
@@ -382,21 +385,6 @@ dn2entry_retry:
                "=> bdb_entry_get: found entry: \"%s\"\n",
                ndn->bv_val, 0, 0 ); 
 
-       /* find attribute values */
-       if( is_entry_alias( e ) ) {
-               Debug( LDAP_DEBUG_ACL,
-                       "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
-               rc = LDAP_ALIAS_PROBLEM;
-               goto return_results;
-       }
-
-       if( is_entry_referral( e ) ) {
-               Debug( LDAP_DEBUG_ACL,
-                       "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
-               rc = LDAP_REFERRAL;
-               goto return_results;
-       }
-
        if ( oc && !is_entry_objectclass( e, oc, 0 )) {
                Debug( LDAP_DEBUG_ACL,
                        "<= bdb_entry_get: failed to find objectClass %s\n",
@@ -408,7 +396,7 @@ dn2entry_retry:
 return_results:
        if( rc != LDAP_SUCCESS ) {
                /* free entry */
-               bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
+               bdb_cache_return_entry_rw(bdb, e, rw, &lock);
 
        } else {
                if ( slapMode == SLAP_SERVER_MODE ) {
@@ -435,7 +423,7 @@ return_results:
                        }
                } else {
                        *ent = entry_dup( e );
-                       bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
+                       bdb_cache_return_entry_rw(bdb, e, rw, &lock);
                }
        }