]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldbm/filterindex.c
Remove abandon cruft
[openldap] / servers / slapd / back-ldbm / filterindex.c
index 733d011506820b3d030a946ae4c265a9c5fd261d..9b5db4549291189ddd85d3449416734de4c75d41 100644 (file)
@@ -36,74 +36,168 @@ filter_candidates(
     Filter     *f
 )
 {
-       ID_BLOCK        *result, *tmp1, *tmp2;
+       ID_BLOCK        *result;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY, "filter_candidates: enter\n"));
+#else
        Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
+#endif
+
 
        result = NULL;
        switch ( f->f_choice ) {
        case SLAPD_FILTER_DN_ONE:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  DN ONE (%s)\n", f->f_dn ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
-               result = dn2idl( be, f->f_dn, DN_ONE_PREFIX );
+#endif
+
+               /* an error is treated as an empty list */
+               if ( dn2idl( be, f->f_dn, DN_ONE_PREFIX, &result ) != 0
+                               && result != NULL ) {
+                       idl_free( result );
+                       result = NULL;
+               }
                break;
 
        case SLAPD_FILTER_DN_SUBTREE:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  DN SUBTREE (%s)\n", f->f_dn ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
-               result = dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX );
+#endif
+
+               /* an error is treated as an empty list */
+               if ( dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, &result ) != 0
+                               && result != NULL ) {
+                       idl_free( result );
+                       result = NULL;
+               }
                break;
 
        case LDAP_FILTER_PRESENT:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  Present (%s)\n", f->f_desc->ad_cname.bv_val ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
+#endif
+
                result = presence_candidates( be, f->f_desc );
                break;
 
        case LDAP_FILTER_EQUALITY:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  EQUALITY (%s),(%s)\n",
+                          f->f_ava->aa_desc->ad_cname.bv_val,
+                          f->f_ava->aa_value.bv_val ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
+#endif
+
                result = equality_candidates( be, f->f_ava );
                break;
 
        case LDAP_FILTER_APPROX:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  APPROX (%s), (%s)\n",
+                          f->f_ava->aa_desc->ad_cname.bv_val,
+                          f->f_ava->aa_value.bv_val ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
+#endif
+
                result = approx_candidates( be, f->f_ava );
                break;
 
        case LDAP_FILTER_SUBSTRINGS:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  SUBSTRINGS\n"));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
+#endif
+
                result = substring_candidates( be, f->f_sub );
                break;
 
        case LDAP_FILTER_GE:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  GE\n"));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
-               result = idl_allids( be );
+#endif
+
+               result = presence_candidates( be, f->f_desc );
                break;
 
        case LDAP_FILTER_LE:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  LE\n" ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
-               result = idl_allids( be );
+#endif
+
+               result = presence_candidates( be, f->f_desc );
                break;
 
        case LDAP_FILTER_AND:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  AND\n" ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
+#endif
+
                result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
                break;
 
        case LDAP_FILTER_OR:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  OR\n" ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
+#endif
+
                result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
                break;
 
        case LDAP_FILTER_NOT:
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                          "filter_candidates:  NOT\n" ));
+#else
                Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
-               tmp1 = idl_allids( be );
-               tmp2 = filter_candidates( be, f->f_not );
-               result = idl_notin( be, tmp1, tmp2 );
-               idl_free( tmp2 );
-               idl_free( tmp1 );
+#endif
+
+               /*
+                * As candidates lists may contain entries which do
+                * not match the assertion, negation of the inner candidate
+                * list could result in matching entries be excluded from
+                * the returned candidate list.
+                */
+               result = idl_allids( be );
                break;
        }
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "filter_candidates: return %ld\n",
+                  result ? ID_BLOCK_NIDS(result) : 0 ));
+#else
        Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
            result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
+#endif
+
        return( result );
 }
 
@@ -118,64 +212,109 @@ presence_candidates(
        int rc;
        char *dbname;
        slap_mask_t mask;
-       struct berval *prefix;
+       struct berval prefix = {0};
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "presence_candidates: enter\n" ));
+#else
        Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
+#endif
 
        idl = idl_allids( be );
 
+       if( desc == slap_schema.si_ad_objectClass ) {
+               return idl;
+       }
+
        rc = index_param( be, desc, LDAP_FILTER_PRESENT,
                &dbname, &mask, &prefix );
 
        if( rc != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_ANY,
-                   "<= presence_candidates: index_param failed (%d)\n",
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
+                          "presence_candidates: index_param returned %d\n",
+                          rc ));
+#else
+               Debug( LDAP_DEBUG_TRACE,
+                   "<= presence_candidates: index_param returned=%d\n",
                        rc, 0, 0 );
+#endif
+
                return idl;
        }
 
        if( dbname == NULL ) {
                /* not indexed */
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
+                          "presence_candidates: not indexed\n" ));
+#else
                Debug( LDAP_DEBUG_TRACE,
                    "<= presense_candidates: not indexed\n",
                        0, 0, 0 );
-               ber_bvfree( prefix );
+#endif
+
                return idl;
        }
 
-       db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_READER );
+       db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
        
        if ( db == NULL ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
+                          "presence_candidates: db open failed (%s%s)\n",
+                          dbname, LDBM_SUFFIX ));
+#else
                Debug( LDAP_DEBUG_ANY,
                    "<= presense_candidates db open failed (%s%s)\n",
                        dbname, LDBM_SUFFIX, 0 );
-               ber_bvfree( prefix );
+#endif
+
                return idl;
        }
 
-       if( prefix != NULL ) {
+       if( prefix.bv_val != NULL ) {
                idl_free( idl );
                idl = NULL;
 
-               rc = key_read( be, db, prefix, &idl );
+               rc = key_read( be, db, &prefix, &idl );
 
                if( rc != LDAP_SUCCESS ) {
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                                  "presence_candidates: key read failed (%d)\n", rc ));
+#else
                        Debug( LDAP_DEBUG_TRACE,
                                "<= presense_candidates key read failed (%d)\n",
                            rc, 0, 0 );
+#endif
+
 
                } else if( idl == NULL ) {
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+                                  "presence_candidates: NULL\n" ));
+#else
                        Debug( LDAP_DEBUG_TRACE,
                                "<= presense_candidates NULL\n",
                            0, 0, 0 );
+#endif
+
                }
        }
 
        ldbm_cache_close( be, db );
-       ber_bvfree( prefix );
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "presence_candidates:  return %ld\n",
+                  idl ? ID_BLOCK_NIDS(idl) : 0 ));
+#else
        Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
            idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
+#endif
+
        return( idl );
 }
 
@@ -191,11 +330,17 @@ equality_candidates(
        int rc;
        char *dbname;
        slap_mask_t mask;
-       struct berval *prefix;
-       struct berval **keys = NULL;
+       struct berval prefix = {0};
+       struct berval *keys = NULL;
        MatchingRule *mr;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "equality_candidates: enter\n" ));
+#else
        Debug( LDAP_DEBUG_TRACE, "=> equality_candidates\n", 0, 0, 0 );
+#endif
+
 
        idl = idl_allids( be );
 
@@ -203,29 +348,38 @@ equality_candidates(
                &dbname, &mask, &prefix );
 
        if( rc != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_ANY,
-                   "<= equality_candidates: index_param failed (%d)\n",
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "equality_candidates:  index_param returned %d\n", rc ));
+#else
+               Debug( LDAP_DEBUG_TRACE,
+                   "<= equality_candidates: index_param returned=%d\n",
                        rc, 0, 0 );
+#endif
+
                return idl;
        }
 
        if( dbname == NULL ) {
                /* not indexed */
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "equality_candidates: not indexed\n" ));
+#else
                Debug( LDAP_DEBUG_TRACE,
                    "<= equality_candidates: not indexed\n",
                        0, 0, 0 );
-               ber_bvfree( prefix );
+#endif
+
                return idl;
        }
 
        mr = ava->aa_desc->ad_type->sat_equality;
        if( !mr ) {
-               ber_bvfree( prefix );
                return idl;
        }
 
        if( !mr->smr_filter ) {
-               ber_bvfree( prefix );
                return idl;
        }
 
@@ -234,56 +388,87 @@ equality_candidates(
                mask,
                ava->aa_desc->ad_type->sat_syntax,
                mr,
-               prefix,
-               ava->aa_value,
+               &prefix,
+               &ava->aa_value,
                &keys );
 
-       ber_bvfree( prefix );
-
        if( rc != LDAP_SUCCESS ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "equality_candidates: (%s%s) MR filter failed (%d\n",
+                          dbname, LDBM_SUFFIX, rc ));
+#else
                Debug( LDAP_DEBUG_TRACE,
                    "<= equality_candidates: (%s%s) MR filter failed (%d)\n",
                        dbname, LDBM_SUFFIX, rc );
+#endif
+
                return idl;
        }
 
        if( keys == NULL ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "equality_candidates: no keys (%s%s)\n",
+                          dbname, LDBM_SUFFIX ));
+#else
                Debug( LDAP_DEBUG_TRACE,
                    "<= equality_candidates: no keys (%s%s)\n",
                        dbname, LDBM_SUFFIX, 0 );
+#endif
+
                return idl;
        }
 
-       db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_READER );
+       db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
        
        if ( db == NULL ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "equality_candidates: db open failed (%s%s)\n",
+                          dbname, LDBM_SUFFIX ));
+#else
                Debug( LDAP_DEBUG_ANY,
                    "<= equality_candidates db open failed (%s%s)\n",
                        dbname, LDBM_SUFFIX, 0 );
+#endif
+
                return idl;
        }
 
-       for ( i= 0; keys[i] != NULL; i++ ) {
+       for ( i= 0; keys[i].bv_val != NULL; i++ ) {
                ID_BLOCK *save;
                ID_BLOCK *tmp;
 
-               rc = key_read( be, db, keys[i], &tmp );
+               rc = key_read( be, db, &keys[i], &tmp );
 
                if( rc != LDAP_SUCCESS ) {
                        idl_free( idl );
                        idl = NULL;
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                                  "equality_candidates: key read failed (%d)\n", rc ));
+#else
                        Debug( LDAP_DEBUG_TRACE,
                                "<= equality_candidates key read failed (%d)\n",
                            rc, 0, 0 );
+#endif
+
                        break;
                }
 
                if( tmp == NULL ) {
                        idl_free( idl );
                        idl = NULL;
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
+                                  "equality_candidates NULL\n" ));
+#else
                        Debug( LDAP_DEBUG_TRACE,
                                "<= equality_candidates NULL\n",
                            0, 0, 0 );
+#endif
+
                        break;
                }
 
@@ -295,13 +480,20 @@ equality_candidates(
                if( idl == NULL ) break;
        }
 
-       ber_bvecfree( keys );
+       bvarray_free( keys );
 
        ldbm_cache_close( be, db );
 
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "equality_candidates: return %ld\n",
+                  idl ? ID_BLOCK_NIDS(idl) : 0 ));
+#else
        Debug( LDAP_DEBUG_TRACE, "<= equality_candidates %ld\n",
            idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
+#endif
+
        return( idl );
 }
 
@@ -317,11 +509,17 @@ approx_candidates(
        int rc;
        char *dbname;
        slap_mask_t mask;
-       struct berval *prefix;
-       struct berval **keys = NULL;
+       struct berval prefix = {0};
+       struct berval *keys = NULL;
        MatchingRule *mr;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "approx_candidates: enter\n" ));
+#else
        Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
+#endif
+
 
        idl = idl_allids( be );
 
@@ -329,18 +527,29 @@ approx_candidates(
                &dbname, &mask, &prefix );
 
        if( rc != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_ANY,
-                   "<= approx_candidates: index_param failed (%d)\n",
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "approx_candidates: index_param returned %d\n", rc ));
+#else
+               Debug( LDAP_DEBUG_TRACE,
+                   "<= approx_candidates: index_param returned=%d\n",
                        rc, 0, 0 );
+#endif
+
                return idl;
        }
 
        if( dbname == NULL ) {
                /* not indexed */
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "approx_candidates: not indexed\n" ));
+#else
                Debug( LDAP_DEBUG_ANY,
                    "<= approx_candidates: not indexed\n",
                        0, 0, 0 );
-               ber_bvfree( prefix );
+#endif
+
                return idl;
        }
 
@@ -351,12 +560,10 @@ approx_candidates(
        }
 
        if( !mr ) {
-               ber_bvfree( prefix );
                return idl;
        }
 
        if( !mr->smr_filter ) {
-               ber_bvfree( prefix );
                return idl;
        }
 
@@ -365,70 +572,109 @@ approx_candidates(
                mask,
                ava->aa_desc->ad_type->sat_syntax,
                mr,
-               prefix,
-               ava->aa_value,
+               &prefix,
+               &ava->aa_value,
                &keys );
 
-       ber_bvfree( prefix );
-
        if( rc != LDAP_SUCCESS ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "approx_candidates: (%s%s) MR filter failed (%d)\n",
+                          dbname, LDBM_SUFFIX, rc ));
+#else
                Debug( LDAP_DEBUG_TRACE,
                    "<= approx_candidates: (%s%s) MR filter failed (%d)\n",
                        dbname, LDBM_SUFFIX, rc );
+#endif
+
                return idl;
        }
 
        if( keys == NULL ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
+                          "approx_candidates: no keys (%s%s)\n",
+                          dbname, LDBM_SUFFIX ));
+#else
                Debug( LDAP_DEBUG_TRACE,
                    "<= approx_candidates: no keys (%s%s)\n",
                        dbname, LDBM_SUFFIX, 0 );
+#endif
+
                return idl;
        }
 
-       db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_READER );
+       db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
        
        if ( db == NULL ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "approx_candidates db open failed (%s%s)\n",
+                          dbname, LDBM_SUFFIX ));
+#else
                Debug( LDAP_DEBUG_ANY,
                    "<= approx_candidates db open failed (%s%s)\n",
                        dbname, LDBM_SUFFIX, 0 );
+#endif
+
                return idl;
        }
 
-       for ( i= 0; keys[i] != NULL; i++ ) {
+       for ( i= 0; keys[i].bv_val != NULL; i++ ) {
                ID_BLOCK *save;
                ID_BLOCK *tmp;
 
-               rc = key_read( be, db, keys[i], &tmp );
+               rc = key_read( be, db, &keys[i], &tmp );
 
                if( rc != LDAP_SUCCESS ) {
                        idl_free( idl );
                        idl = NULL;
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                                  "approx_candidates: key read failed (%d)\n", rc ));
+#else
                        Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
                            rc, 0, 0 );
+#endif
+
                        break;
                }
 
                if( tmp == NULL ) {
                        idl_free( idl );
                        idl = NULL;
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
+                                  "approx_candidates: NULL\n" ));
+#else
                        Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
                            0, 0, 0 );
+#endif
+
                        break;
                }
 
                save = idl;
                idl = idl_intersection( be, idl, tmp );
                idl_free( save );
+               idl_free( tmp );
 
                if( idl == NULL ) break;
        }
 
-       ber_bvecfree( keys );
+       bvarray_free( keys );
 
        ldbm_cache_close( be, db );
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "approx_candidates: return %ld\n",
+                  idl ? ID_BLOCK_NIDS(idl) : 0 ));
+#else
        Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
            idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
+#endif
+
        return( idl );
 }
 
@@ -442,16 +688,28 @@ list_candidates(
        ID_BLOCK        *idl, *tmp, *tmp2;
        Filter  *f;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "list_candidates: 0x%x\n", ftype ));
+#else
        Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
+#endif
+
 
        idl = NULL;
        for ( f = flist; f != NULL; f = f->f_next ) {
                if ( (tmp = filter_candidates( be, f )) == NULL &&
                    ftype == LDAP_FILTER_AND ) {
-                               Debug( LDAP_DEBUG_TRACE,
-                                   "<= list_candidates NULL\n", 0, 0, 0 );
-                               idl_free( idl );
-                               return( NULL );
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
+                                  "list_candidates: NULL\n" ));
+#else
+                       Debug( LDAP_DEBUG_TRACE,
+                              "<= list_candidates NULL\n", 0, 0, 0 );
+#endif
+
+                       idl_free( idl );
+                       return( NULL );
                }
 
                tmp2 = idl;
@@ -468,8 +726,15 @@ list_candidates(
                }
        }
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "list_candidates: return %ld\n",
+                  idl ? ID_BLOCK_NIDS(idl) : 0 ));
+#else
        Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
            idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
+#endif
+
        return( idl );
 }
 
@@ -485,11 +750,17 @@ substring_candidates(
        int rc;
        char *dbname;
        slap_mask_t mask;
-       struct berval *prefix;
-       struct berval **keys = NULL;
+       struct berval prefix = {0};
+       struct berval *keys = NULL;
        MatchingRule *mr;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "substrings_candidates: enter\n" ));
+#else
        Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
+#endif
+
 
        idl = idl_allids( be );
 
@@ -497,30 +768,39 @@ substring_candidates(
                &dbname, &mask, &prefix );
 
        if( rc != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_ANY,
-                   "<= substrings_candidates: index_param failed (%d)\n",
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "substrings_candidates: index_param returned %d\n", rc ));
+#else
+               Debug( LDAP_DEBUG_TRACE,
+                   "<= substrings_candidates: index_param returned=%d\n",
                        rc, 0, 0 );
+#endif
+
                return idl;
        }
 
        if( dbname == NULL ) {
                /* not indexed */
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "substrings_candidates: not indexed\n" ));
+#else
                Debug( LDAP_DEBUG_ANY,
                    "<= substrings_candidates: not indexed\n",
                        0, 0, 0 );
-               ber_bvfree( prefix );
+#endif
+
                return idl;
        }
 
        mr = sub->sa_desc->ad_type->sat_substr;
 
        if( !mr ) {
-               ber_bvfree( prefix );
                return idl;
        }
 
        if( !mr->smr_filter ) {
-               ber_bvfree( prefix );
                return idl;
        }
 
@@ -529,69 +809,109 @@ substring_candidates(
                mask,
                sub->sa_desc->ad_type->sat_syntax,
                mr,
-               prefix,
+               &prefix,
                sub,
                &keys );
 
-       ber_bvfree( prefix );
-
        if( rc != LDAP_SUCCESS ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "substrings_candidates: (%s%s) MR filter failed (%d)\n",
+                          dbname, LDBM_SUFFIX, rc ));
+#else
                Debug( LDAP_DEBUG_TRACE,
                    "<= substrings_candidates: (%s%s) MR filter failed (%d)\n",
                        dbname, LDBM_SUFFIX, rc );
+#endif
+
                return idl;
        }
 
        if( keys == NULL ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "substrings_candidates: (0x%04lx) no keys (%s%s)\n",
+                          mask, dbname, LDBM_SUFFIX ));
+#else
                Debug( LDAP_DEBUG_TRACE,
                    "<= substrings_candidates: (0x%04lx) no keys (%s%s)\n",
                        mask, dbname, LDBM_SUFFIX );
+#endif
+
                return idl;
        }
 
-       db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_READER );
+       db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
        
        if ( db == NULL ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                          "substrings_candidates: db open failed (%s%s)\n",
+                          dbname, LDBM_SUFFIX ));
+#else
                Debug( LDAP_DEBUG_ANY,
                    "<= substrings_candidates db open failed (%s%s)\n",
                        dbname, LDBM_SUFFIX, 0 );
+#endif
+
                return idl;
        }
 
-       for ( i= 0; keys[i] != NULL; i++ ) {
+       for ( i= 0; keys[i].bv_val != NULL; i++ ) {
                ID_BLOCK *save;
                ID_BLOCK *tmp;
 
-               rc = key_read( be, db, keys[i], &tmp );
+               rc = key_read( be, db, &keys[i], &tmp );
 
                if( rc != LDAP_SUCCESS ) {
                        idl_free( idl );
                        idl = NULL;
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
+                                  "substrings_candidates: key read failed (%d)\n",
+                                  rc ));
+#else
                        Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
                            rc, 0, 0 );
+#endif
+
                        break;
                }
 
                if( tmp == NULL ) {
                        idl_free( idl );
                        idl = NULL;
+#ifdef NEW_LOGGING
+                       LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
+                                  "substrings_candidates: NULL\n" ));
+#else
                        Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
                            0, 0, 0 );
+#endif
+
                        break;
                }
 
                save = idl;
                idl = idl_intersection( be, idl, tmp );
                idl_free( save );
+               idl_free( tmp );
 
                if( idl == NULL ) break;
        }
 
-       ber_bvecfree( keys );
+       bvarray_free( keys );
 
        ldbm_cache_close( be, db );
 
+#ifdef NEW_LOGGING
+       LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+                  "substrings_candidates: return %ld\n",
+                  idl ? ID_BLOCK_NIDS(idl) : 0 ));
+#else
        Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld\n",
            idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
+#endif
+
        return( idl );
 }