]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-meta/compare.c
group rewrite/map stuff in one structure and optimize more function calls
[openldap] / servers / slapd / back-meta / compare.c
index 8a5a31cb43e5f538dde7f8d8b3ad6e930ae74d47..d272ab42dc20838b5bc06054333def8238246651 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 1998-2001 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  *
  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
 #include "back-meta.h"
 
 int
-meta_back_compare(
-               Backend                 *be,
-               Connection              *conn,
-               Operation               *op,
-               const char              *dn,
-               const char              *ndn,
-               AttributeAssertion      *ava
-)
+meta_back_compare( Operation *op, SlapReply *rs )
 {
-       struct metainfo *li = ( struct metainfo * )be->be_private;
+       struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
        struct metaconn *lc;
-       struct metasingleconn **lsc;
+       struct metasingleconn *lsc;
        char *match = NULL, *err = NULL, *mmatch = NULL;
        int candidates = 0, last = 0, i, count, rc;
                int cres = LDAP_SUCCESS, rres = LDAP_SUCCESS;
        int *msgid;
 
-       lc = meta_back_getconn( li, conn, op, META_OP_ALLOW_MULTIPLE,
-                       ndn, NULL );
-       if ( !lc || !meta_back_dobind( lc, op ) ) {
+       lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
+                       &op->o_req_ndn, NULL );
+       if ( !lc ) {
+               send_ldap_result( op, rs );
+               return -1;
+       }
+       
+       if ( !meta_back_dobind( lc, op ) ) {
+               rs->sr_err = LDAP_OTHER;
+               send_ldap_result( op, rs );
                return -1;
        }
 
@@ -107,12 +107,13 @@ meta_back_compare(
        /*
         * start an asynchronous compare for each candidate target
         */
-       for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
+       for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
                char *mdn = NULL;
-               char *mapped_attr = ava->aa_desc->ad_cname->bv_val;
-               char *mapped_value = ava->aa_value->bv_val;
+               struct berval mapped_attr = op->oq_compare.rs_ava->aa_desc->ad_cname;
+               struct berval mapped_value = op->oq_compare.rs_ava->aa_value;
 
-               if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
+               if ( lsc->candidate != META_CANDIDATE ) {
+                       msgid[ i ] = -1;
                        continue;
                }
 
@@ -120,55 +121,55 @@ meta_back_compare(
                 * Rewrite the compare dn, if needed
                 */
                switch ( rewrite_session( li->targets[ i ]->rwinfo,
-                                       "compareDn", dn, conn, &mdn ) ) {
+                                       "compareDn", 
+                                       op->o_req_dn.bv_val, op->o_conn, &mdn ) ) {
                case REWRITE_REGEXEC_OK:
                        if ( mdn == NULL ) {
-                               mdn = ( char * )dn;
+                               mdn = ( char * )op->o_req_dn.bv_val;
                        }
 #ifdef NEW_LOGGING
-                       LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
-                                       "[rw] compareDn: \"%s\" -> \"%s\"\n",
-                                       dn, mdn ));
+                       LDAP_LOG( BACK_META, DETAIL1,
+                               "[rw] compareDn: \"%s\" -> \"%s\"\n",
+                               op->o_req_dn.bv_val, mdn, 0 );
 #else /* !NEW_LOGGING */
                        Debug( LDAP_DEBUG_ARGS,
-                                       "rw> compareDn: \"%s\" -> \"%s\"\n%s",
-                                       dn, mdn, "" );
+                                       "rw> compareDn: \"%s\" -> \"%s\"\n",
+                                       op->o_req_dn.bv_val, mdn, 0 );
 #endif /* !NEW_LOGGING */
                        break;
                
                case REWRITE_REGEXEC_UNWILLING:
-                       send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                                       NULL, "Unwilling to perform",
-                                       NULL, NULL );
+                       rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                       rs->sr_text = "Operation not allowed";
+                       send_ldap_result( op, rs );
                        return -1;
                        
                case REWRITE_REGEXEC_ERR:
-                       send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
-                                       NULL, "Operations error",
-                                       NULL, NULL );
+                       rs->sr_err = LDAP_OTHER;
+                       rs->sr_text = "Rewrite error";
+                       send_ldap_result( op, rs );
                        return -1;
                }
 
                /*
                 * if attr is objectClass, try to remap the value
                 */
-               if ( ava->aa_desc->ad_type->sat_oid 
-                       == slap_schema.si_ad_objectClass->ad_type->sat_oid ) {
-                       mapped_value = ldap_back_map( &li->targets[ i ]->oc_map,
-                                       ava->aa_value->bv_val, 0 );
+               if ( op->oq_compare.rs_ava->aa_desc == slap_schema.si_ad_objectClass ) {
+                       ldap_back_map( &li->targets[ i ]->oc_map,
+                                       &op->oq_compare.rs_ava->aa_value,
+                                       &mapped_value, BACKLDAP_MAP );
 
-                       if ( mapped_value == NULL ) {
-                               lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
+                       if ( mapped_value.bv_val == NULL || mapped_value.bv_val[0] == '\0' ) {
                                continue;
                        }
                /*
                 * else try to remap the attribute
                 */
                } else {
-                       mapped_attr = ldap_back_map( &li->targets[ i ]->at_map,
-                               ava->aa_desc->ad_cname->bv_val, 0 );
-                       if ( mapped_attr == NULL ) {
-                               lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
+                       ldap_back_map( &li->targets[ i ]->at_map,
+                               &op->oq_compare.rs_ava->aa_desc->ad_cname,
+                               &mapped_attr, BACKLDAP_MAP );
+                       if ( mapped_attr.bv_val == NULL || mapped_attr.bv_val[0] == '\0' ) {
                                continue;
                        }
                }
@@ -178,21 +179,20 @@ meta_back_compare(
                 * that returns determines the result; a constraint on unicity
                 * of the result ought to be enforced
                 */
-               msgid[ i ] = ldap_compare( lc->conns[ i ]->ld, mdn,
-                               mapped_attr, mapped_value );
-               if ( msgid[ i ] == -1 ) {
-                       lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
-                       continue;
-               }
-
-               if ( mdn != dn ) {
+               msgid[ i ] = ldap_compare( lc->conns[ i ].ld, mdn,
+                               mapped_attr.bv_val, mapped_value.bv_val );
+               if ( mdn != op->o_req_dn.bv_val ) {
                        free( mdn );
                }
-               if ( mapped_attr != ava->aa_desc->ad_cname->bv_val ) {
-                       free( mapped_attr );
+               if ( mapped_attr.bv_val != op->oq_compare.rs_ava->aa_desc->ad_cname.bv_val ) {
+                       free( mapped_attr.bv_val );
                }
-               if ( mapped_value != ava->aa_value->bv_val ) {
-                       free( mapped_value );
+               if ( mapped_value.bv_val != op->oq_compare.rs_ava->aa_value.bv_val ) {
+                       free( mapped_value.bv_val );
+               }
+
+               if ( msgid[ i ] == -1 ) {
+                       continue;
                }
 
                ++candidates;
@@ -206,15 +206,15 @@ meta_back_compare(
                /*
                 * FIXME: should we check for abandon?
                 */
-               for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; lsc++, i++ ) {
-                       int lrc;
-                       LDAPMessage *res = NULL;
+               for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
+                       int             lrc;
+                       LDAPMessage     *res = NULL;
 
-                       if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
+                       if ( msgid[ i ] == -1 ) {
                                continue;
                        }
 
-                       lrc = ldap_result( lsc[ 0 ]->ld, msgid[ i ],
+                       lrc = ldap_result( lsc->ld, msgid[ i ],
                                        0, NULL, &res );
 
                        if ( lrc == 0 ) {
@@ -225,16 +225,16 @@ meta_back_compare(
                                        ldap_msgfree( res );
                                }
                                continue;
+
                        } else if ( lrc == LDAP_RES_COMPARE ) {
                                if ( count > 0 ) {
-                                       rres = LDAP_OPERATIONS_ERROR;
+                                       rres = LDAP_OTHER;
                                        rc = -1;
                                        goto finish;
                                }
                                
-                               cres = ldap_result2error( lsc[ 0 ]->ld,
-                                               res, 1 );
-                               switch ( cres ) {
+                               rs->sr_err = ldap_result2error( lsc->ld, res, 1 );
+                               switch ( rs->sr_err ) {
                                case LDAP_COMPARE_TRUE:
                                case LDAP_COMPARE_FALSE:
 
@@ -243,7 +243,7 @@ meta_back_compare(
                                         * sending to cache ...
                                         */
                                        if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
-                                               ( void )meta_dncache_update_entry( &li->cache, ch_strdup( ndn ), i );
+                                               ( void )meta_dncache_update_entry( &li->cache, &op->o_req_ndn, i );
                                        }
 
                                        count++;
@@ -251,27 +251,28 @@ meta_back_compare(
                                        break;
 
                                default:
-                                       rres = ldap_back_map_result( cres );
+                                       rres = ldap_back_map_result( rs );
 
                                        if ( err != NULL ) {
                                                free( err );
                                        }
-                                       ldap_get_option( lsc[ 0 ]->ld,
+                                       ldap_get_option( lsc->ld,
                                                LDAP_OPT_ERROR_STRING, &err );
 
                                        if ( match != NULL ) {
                                                free( match );
                                        }
-                                       ldap_get_option( lsc[ 0 ]->ld,
+                                       ldap_get_option( lsc->ld,
                                                LDAP_OPT_MATCHED_DN, &match );
                                        
                                        last = i;
                                        break;
                                }
-                               lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
+                               msgid[ i ] = -1;
                                --candidates;
+
                        } else {
-                               lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
+                               msgid[ i ] = -1;
                                --candidates;
                                if ( res ) {
                                        ldap_msgfree( res );
@@ -281,7 +282,7 @@ meta_back_compare(
                }
        }
 
-finish:
+finish:;
 
        /*
         * Rewrite the matched portion of the search base, if required
@@ -307,42 +308,46 @@ finish:
                 * and none was successful
                 */
                switch ( rewrite_session( li->targets[ last ]->rwinfo,
-                                       "matchedDn", match, conn, &mmatch ) ) {
+                                       "matchedDn", match, op->o_conn,
+                                       &mmatch ) ) {
                case REWRITE_REGEXEC_OK:
                        if ( mmatch == NULL ) {
                                mmatch = ( char * )match;
                        }
 #ifdef NEW_LOGGING
-                       LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
+                       LDAP_LOG( BACK_META, DETAIL1,
                                        "[rw] matchedDn: \"%s\" -> \"%s\"\n",
-                                       match, mmatch ));
+                                       match, mmatch, 0 );
 #else /* !NEW_LOGGING */
                        Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
-                                       " \"%s\" -> \"%s\"\n%s",
-                                       match, mmatch, "" );
+                                       " \"%s\" -> \"%s\"\n",
+                                       match, mmatch, 0 );
 #endif /* !NEW_LOGGING */
                        break;
                        
                
                case REWRITE_REGEXEC_UNWILLING:
-                       send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                                       NULL, "Unwilling to perform",
-                                       NULL, NULL );
+                       rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                       rs->sr_text = "Operation not allowed";
+                       send_ldap_result( op, rs );
                        rc = -1;
                        goto cleanup;
                        
                case REWRITE_REGEXEC_ERR:
-                       send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
-                                       NULL, "Operations error",
-                                       NULL, NULL );
+                       rs->sr_err = LDAP_OTHER;
+                       rs->sr_text = "Rewrite error";
+                       send_ldap_result( op, rs );
                        rc = -1;
                        goto cleanup;
                }
        }
 
-       send_ldap_result( conn, op, rres, mmatch, err, NULL, NULL );
+       rs->sr_err = rres;
+       rs->sr_matched = mmatch;
+       send_ldap_result( op, rs );
+       rs->sr_matched = NULL;
 
-cleanup:
+cleanup:;
        if ( match != NULL ) {
                if ( mmatch != match ) {
                        free( mmatch );