]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-mdb/index.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / servers / slapd / back-mdb / index.c
index 15057333df4dddbf9fa24f56e8337cfec679d4e5..6acdfc09ee1a9037cf277d49f656d2d3975497fd 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2000-2011 The OpenLDAP Foundation.
+ * Copyright 2000-2012 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,8 +25,8 @@
 #include "back-mdb.h"
 #include "lutil_hash.h"
 
-static char presence_keyval[] = {0,0};
-static struct berval presence_key = BER_BVC(presence_keyval);
+static char presence_keyval[] = {0,0,0,0,0};
+static struct berval presence_key[2] = {BER_BVC(presence_keyval), BER_BVNULL};
 
 AttrInfo *mdb_index_mask(
        Backend *be,
@@ -114,7 +114,7 @@ int mdb_index_param(
        case LDAP_FILTER_PRESENT:
                type = SLAP_INDEX_PRESENT;
                if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
-                       *prefixp = presence_key;
+                       *prefixp = presence_key[0];
                        goto done;
                }
                break;
@@ -164,7 +164,7 @@ done:
 static int indexer(
        Operation *op,
        MDB_txn *txn,
-       MDB_dbi dbi,
+       struct mdb_attrinfo *ai,
        AttributeDescription *ad,
        struct berval *atname,
        BerVarray vals,
@@ -174,12 +174,35 @@ static int indexer(
 {
        int rc, i;
        struct berval *keys;
+       MDB_cursor *mc = ai->ai_cursor;
+       mdb_idl_keyfunc *keyfunc;
+       char *err;
 
        assert( mask != 0 );
 
+       if ( !mc ) {
+               err = "c_open";
+               rc = mdb_cursor_open( txn, ai->ai_dbi, &mc );
+               if ( rc ) goto done;
+               if ( slapMode & SLAP_TOOL_QUICK )
+                       ai->ai_cursor = mc;
+       }
+
+       if ( opid == SLAP_INDEX_ADD_OP ) {
+#ifdef MDB_TOOL_IDL_CACHING
+               if (( slapMode & SLAP_TOOL_QUICK ) && slap_tool_thread_max > 2 ) {
+                       keyfunc = mdb_tool_idl_add;
+                       mc = (MDB_cursor *)ai;
+               } else
+#endif
+                       keyfunc = mdb_idl_insert_keys;
+       } else
+               keyfunc = mdb_idl_delete_keys;
+
        if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
-               rc = mdb_key_change( op->o_bd, txn, dbi, &presence_key, id, opid );
+               rc = keyfunc( mc, presence_key, id );
                if( rc ) {
+                       err = "presence";
                        goto done;
                }
        }
@@ -193,14 +216,12 @@ static int indexer(
                        atname, vals, &keys, op->o_tmpmemctx );
 
                if( rc == LDAP_SUCCESS && keys != NULL ) {
-                       for( i=0; keys[i].bv_val != NULL; i++ ) {
-                               rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
-                               if( rc ) {
-                                       ber_bvarray_free_x( keys, op->o_tmpmemctx );
-                                       goto done;
-                               }
-                       }
+                       rc = keyfunc( mc, keys, id );
                        ber_bvarray_free_x( keys, op->o_tmpmemctx );
+                       if ( rc ) {
+                               err = "equality";
+                               goto done;
+                       }
                }
                rc = LDAP_SUCCESS;
        }
@@ -214,14 +235,12 @@ static int indexer(
                        atname, vals, &keys, op->o_tmpmemctx );
 
                if( rc == LDAP_SUCCESS && keys != NULL ) {
-                       for( i=0; keys[i].bv_val != NULL; i++ ) {
-                               rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
-                               if( rc ) {
-                                       ber_bvarray_free_x( keys, op->o_tmpmemctx );
-                                       goto done;
-                               }
-                       }
+                       rc = keyfunc( mc, keys, id );
                        ber_bvarray_free_x( keys, op->o_tmpmemctx );
+                       if ( rc ) {
+                               err = "approx";
+                               goto done;
+                       }
                }
 
                rc = LDAP_SUCCESS;
@@ -236,20 +255,20 @@ static int indexer(
                        atname, vals, &keys, op->o_tmpmemctx );
 
                if( rc == LDAP_SUCCESS && keys != NULL ) {
-                       for( i=0; keys[i].bv_val != NULL; i++ ) {
-                               rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
-                               if( rc ) {
-                                       ber_bvarray_free_x( keys, op->o_tmpmemctx );
-                                       goto done;
-                               }
-                       }
+                       rc = keyfunc( mc, keys, id );
                        ber_bvarray_free_x( keys, op->o_tmpmemctx );
+                       if( rc ) {
+                               err = "substr";
+                               goto done;
+                       }
                }
 
                rc = LDAP_SUCCESS;
        }
 
 done:
+       if ( !(slapMode & SLAP_TOOL_QUICK))
+               mdb_cursor_close( mc );
        switch( rc ) {
        /* The callers all know how to deal with these results */
        case 0:
@@ -297,7 +316,7 @@ static int index_at_values(
                        if ( ai->ai_cr ) {
                                ComponentReference *cr;
                                for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
-                                       rc = indexer( op, txn, ai->ai_dbi, cr->cr_ad, &type->sat_cname,
+                                       rc = indexer( op, txn, ai, cr->cr_ad, &type->sat_cname,
                                                cr->cr_nvals, id, ixop,
                                                cr->cr_indexmask );
                                }
@@ -315,7 +334,7 @@ static int index_at_values(
                         */
                                mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
                        if( mask ) {
-                               rc = indexer( op, txn, ai->ai_dbi, ad, &type->sat_cname,
+                               rc = indexer( op, txn, ai, ad, &type->sat_cname,
                                        vals, id, ixop, mask );
 
                                if( rc ) return rc;
@@ -336,7 +355,7 @@ static int index_at_values(
                                else
                                        mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
                                if ( mask ) {
-                                       rc = indexer( op, txn, ai->ai_dbi, desc, &desc->ad_cname,
+                                       rc = indexer( op, txn, ai, desc, &desc->ad_cname,
                                                vals, id, ixop, mask );
 
                                        if( rc ) {
@@ -392,11 +411,11 @@ mdb_index_recset(
        if( type->sat_ad ) {
                slot = mdb_attr_slot( mdb, type->sat_ad, NULL );
                if ( slot >= 0 ) {
-                       ir[slot].ai = mdb->mi_attrs[slot];
+                       ir[slot].ir_ai = mdb->mi_attrs[slot];
                        al = ch_malloc( sizeof( AttrList ));
                        al->attr = a;
-                       al->next = ir[slot].attrs;
-                       ir[slot].attrs = al;
+                       al->next = ir[slot].ir_attrs;
+                       ir[slot].ir_attrs = al;
                }
        }
        if( tags->bv_len ) {
@@ -406,11 +425,11 @@ mdb_index_recset(
                if( desc ) {
                        slot = mdb_attr_slot( mdb, desc, NULL );
                        if ( slot >= 0 ) {
-                               ir[slot].ai = mdb->mi_attrs[slot];
+                               ir[slot].ir_ai = mdb->mi_attrs[slot];
                                al = ch_malloc( sizeof( AttrList ));
                                al->attr = a;
-                               al->next = ir[slot].attrs;
-                               ir[slot].attrs = al;
+                               al->next = ir[slot].ir_attrs;
+                               ir[slot].ir_attrs = al;
                        }
                }
        }
@@ -420,6 +439,7 @@ mdb_index_recset(
 /* Apply the indices for the recset */
 int mdb_index_recrun(
        Operation *op,
+       MDB_txn *txn,
        struct mdb_info *mdb,
        IndexRec *ir0,
        ID id,
@@ -433,15 +453,15 @@ int mdb_index_recrun(
        if ( id == 0 )
                return 0;
 
-       for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max) {
+       for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max-1) {
                ir = ir0 + i;
-               if ( !ir->ai ) continue;
-               while (( al = ir->attrs )) {
-                       ir->attrs = al->next;
-                       rc = indexer( op, NULL, ir->ai->ai_dbi, ir->ai->ai_desc,
-                               &ir->ai->ai_desc->ad_type->sat_cname,
+               if ( !ir->ir_ai ) continue;
+               while (( al = ir->ir_attrs )) {
+                       ir->ir_attrs = al->next;
+                       rc = indexer( op, txn, ir->ir_ai, ir->ir_ai->ai_desc,
+                               &ir->ir_ai->ai_desc->ad_type->sat_cname,
                                al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
-                               ir->ai->ai_indexmask );
+                               ir->ir_ai->ai_indexmask );
                        free( al );
                        if ( rc ) break;
                }
@@ -476,7 +496,7 @@ mdb_index_entry(
 
        Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
                opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
-               (long) e->e_id, e->e_dn );
+               (long) e->e_id, e->e_dn ? e->e_dn : "" );
 
        /* add each attribute to the indexes */
        for ( ; ap != NULL; ap = ap->a_next ) {
@@ -549,7 +569,7 @@ mdb_index_entry(
 
        Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
                opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
-               (long) e->e_id, e->e_dn );
+               (long) e->e_id, e->e_dn ? e->e_dn : "" );
 
        return LDAP_SUCCESS;
 }