1 /* filterindex.c - generate the list of candidate entries from a filter */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 static int presence_candidates(
18 AttributeDescription *desc,
21 static int equality_candidates(
23 AttributeAssertion *ava,
26 static int approx_candidates(
28 AttributeAssertion *ava,
31 static int substring_candidates(
33 SubstringsAssertion *sub,
37 static int list_candidates(
46 bdb_filter_candidates(
55 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_filter_candidates\n"));
57 Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
60 switch ( f->f_choice ) {
61 case SLAPD_FILTER_DN_ONE:
63 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tDN ONE\n"));
65 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
67 rc = bdb_dn2idl( be, f->f_dn, DN_ONE_PREFIX, ids );
70 case SLAPD_FILTER_DN_SUBTREE:
72 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tDN SUBTREE\n"));
74 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
76 rc = bdb_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, ids );
79 case LDAP_FILTER_PRESENT:
81 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tPRESENT\n"));
83 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
85 rc = presence_candidates( be, f->f_desc, ids );
88 case LDAP_FILTER_EQUALITY:
90 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tEQUALITY\n"));
92 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
94 rc = equality_candidates( be, f->f_ava, ids, tmp );
97 case LDAP_FILTER_APPROX:
99 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tAPPROX\n"));
101 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
103 rc = approx_candidates( be, f->f_ava, ids, tmp );
106 case LDAP_FILTER_SUBSTRINGS:
108 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tSUBSTRINGS\n"));
110 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
112 rc = substring_candidates( be, f->f_sub, ids, tmp );
116 /* no GE index, use pres */
118 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tGE\n"));
120 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
122 rc = presence_candidates( be, f->f_ava->aa_desc, ids );
126 /* no LE index, use pres */
128 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tLE\n"));
130 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
132 rc = presence_candidates( be, f->f_ava->aa_desc, ids );
135 case LDAP_FILTER_NOT:
136 /* no indexing to support NOT filters */
138 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tNOT\n"));
140 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
144 case LDAP_FILTER_AND:
146 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tAND\n"));
148 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
150 rc = list_candidates( be,
151 f->f_and, LDAP_FILTER_AND, ids, tmp, stack );
156 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tOR\n"));
158 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
160 rc = list_candidates( be,
161 f->f_or, LDAP_FILTER_OR, ids, tmp, stack );
166 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tUNKNOWN\n"));
168 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %lu\n",
169 (unsigned long) f->f_choice, 0, 0 );
174 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_filter_candidates: id=%ld first=%ld last=%ld\n", (long) ids[0], (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) ));
176 Debug( LDAP_DEBUG_FILTER,
177 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
179 (long) BDB_IDL_FIRST( ids ),
180 (long) BDB_IDL_LAST( ids ) );
195 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
200 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_list_candidates: 0x%x\n", ftype));
202 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
205 if ( ftype == LDAP_FILTER_OR ) {
206 BDB_IDL_ALL( bdb, save );
209 BDB_IDL_CPY( save, ids );
212 for ( f = flist; f != NULL; f = f->f_next ) {
213 rc = bdb_filter_candidates( be, f, save, tmp,
214 save+BDB_IDL_UM_SIZE );
217 if ( ftype == LDAP_FILTER_AND ) {
224 if ( ftype == LDAP_FILTER_AND ) {
225 bdb_idl_intersection( ids, save );
226 if( BDB_IDL_IS_ZERO( ids ) )
229 bdb_idl_union( ids, save );
230 BDB_IDL_ALL( bdb, save );
236 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n", (long) ids[0], (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) ));
238 Debug( LDAP_DEBUG_FILTER,
239 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
241 (long) BDB_IDL_FIRST(ids),
242 (long) BDB_IDL_LAST(ids) );
247 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "<= bdb_list_candidates: rc=%d\n", rc));
249 Debug( LDAP_DEBUG_FILTER,
250 "<= bdb_list_candidates: undefined rc=%d\n",
261 AttributeDescription *desc,
264 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
268 struct berval prefix = {0};
271 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_presence_candidates\n"));
273 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
276 if( desc == slap_schema.si_ad_objectClass ) {
277 BDB_IDL_ALL( bdb, ids );
281 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
282 &db, &mask, &prefix );
284 if( rc != LDAP_SUCCESS ) {
286 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_presence_candidates: index_parm returned=%d\n", rc ));
288 Debug( LDAP_DEBUG_TRACE,
289 "<= bdb_presence_candidates: index_param returned=%d\n",
298 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: not indexed\n" ));
300 Debug( LDAP_DEBUG_TRACE,
301 "<= bdb_presence_candidates: not indexed\n",
307 if( prefix.bv_val == NULL ) {
309 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: no prefix\n" ));
311 Debug( LDAP_DEBUG_TRACE,
312 "<= bdb_presence_candidates: no prefix\n",
318 rc = bdb_key_read( be, db, NULL, &prefix, ids );
320 if( rc == DB_NOTFOUND ) {
323 } else if( rc != LDAP_SUCCESS ) {
325 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: key read failed (%d)\n", rc ));
327 Debug( LDAP_DEBUG_TRACE,
328 "<= bdb_presense_candidates: key read failed (%d)\n",
335 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n", (long) ids[0], (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) ));
337 Debug(LDAP_DEBUG_TRACE,
338 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
340 (long) BDB_IDL_FIRST(ids),
341 (long) BDB_IDL_LAST(ids) );
351 AttributeAssertion *ava,
359 struct berval prefix = {0};
360 struct berval *keys = NULL;
364 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> equality_candidates\n"));
366 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
369 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
370 &db, &mask, &prefix );
372 if( rc != LDAP_SUCCESS ) {
374 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: index_param failed (%d)\n", rc));
376 Debug( LDAP_DEBUG_ANY,
377 "<= bdb_equality_candidates: index_param failed (%d)\n",
385 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: not indexed\n"));
387 Debug( LDAP_DEBUG_ANY,
388 "<= bdb_equality_candidates: not indexed\n", 0, 0, 0 );
393 mr = ava->aa_desc->ad_type->sat_equality;
398 if( !mr->smr_filter ) {
402 rc = (mr->smr_filter)(
403 LDAP_FILTER_EQUALITY,
405 ava->aa_desc->ad_type->sat_syntax,
411 if( rc != LDAP_SUCCESS ) {
413 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: MR filter failed (%d)\n", rc));
415 Debug( LDAP_DEBUG_TRACE,
416 "<= bdb_equality_candidates: MR filter failed (%d)\n",
424 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: no keys\n"));
426 Debug( LDAP_DEBUG_TRACE,
427 "<= bdb_equality_candidates: no keys\n",
433 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
434 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
436 if( rc == DB_NOTFOUND ) {
439 } else if( rc != LDAP_SUCCESS ) {
441 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_equality_candidates: key read failed (%d)\n", rc));
443 Debug( LDAP_DEBUG_TRACE,
444 "<= bdb_equality_candidates key read failed (%d)\n",
450 if( BDB_IDL_IS_ZERO( tmp ) ) {
452 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: NULL\n"));
454 Debug( LDAP_DEBUG_TRACE,
455 "<= bdb_equality_candidates NULL\n",
462 bdb_idl_intersection( ids, tmp );
464 if( BDB_IDL_IS_ZERO( ids ) )
468 ber_bvarray_free( keys );
471 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_equality_candidates: id=%ld first=%ld last=%ld\n", (long) ids[0], (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) ));
473 Debug( LDAP_DEBUG_TRACE,
474 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
476 (long) BDB_IDL_FIRST(ids),
477 (long) BDB_IDL_LAST(ids) );
486 AttributeAssertion *ava,
494 struct berval prefix = {0};
495 struct berval *keys = NULL;
499 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_approx_candidates\n"));
501 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates\n", 0, 0, 0 );
504 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
505 &db, &mask, &prefix );
507 if( rc != LDAP_SUCCESS ) {
509 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: index_param failed (%d)\n", rc ));
511 Debug( LDAP_DEBUG_ANY,
512 "<= bdb_approx_candidates: index_param failed (%d)\n",
520 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: not indexed\n" ));
522 Debug( LDAP_DEBUG_ANY,
523 "<= bdb_approx_candidates: not indexed\n", 0, 0, 0 );
528 mr = ava->aa_desc->ad_type->sat_approx;
530 /* no approx matching rule, try equality matching rule */
531 mr = ava->aa_desc->ad_type->sat_equality;
538 if( !mr->smr_filter ) {
542 rc = (mr->smr_filter)(
545 ava->aa_desc->ad_type->sat_syntax,
551 if( rc != LDAP_SUCCESS ) {
553 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: MR filter failed (%d)\n", rc ));
555 Debug( LDAP_DEBUG_TRACE,
556 "<= bdb_approx_candidates: (%s) MR filter failed (%d)\n",
557 prefix.bv_val, rc, 0 );
564 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: no keys (%s)\n", prefix.bv_val ));
566 Debug( LDAP_DEBUG_TRACE,
567 "<= bdb_approx_candidates: no keys (%s)\n",
568 prefix.bv_val, 0, 0 );
573 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
574 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
576 if( rc == DB_NOTFOUND ) {
580 } else if( rc != LDAP_SUCCESS ) {
582 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: key read failed (%d)\n", rc ));
584 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates key read failed (%d)\n",
590 if( BDB_IDL_IS_ZERO( tmp ) ) {
592 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: NULL\n" ));
594 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates NULL\n",
601 bdb_idl_intersection( ids, tmp );
603 if( BDB_IDL_IS_ZERO( ids ) )
607 ber_bvarray_free( keys );
610 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: id=%ld first=%ld last=%ld\n", (long) ids[0], (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) ));
612 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
614 (long) BDB_IDL_FIRST(ids),
615 (long) BDB_IDL_LAST(ids) );
621 substring_candidates(
623 SubstringsAssertion *sub,
631 struct berval prefix = {0};
632 struct berval *keys = NULL;
636 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_substring_candidates\n"));
638 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates\n", 0, 0, 0 );
641 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
642 &db, &mask, &prefix );
644 if( rc != LDAP_SUCCESS ) {
646 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: index_param failed (%d)\n", rc ));
648 Debug( LDAP_DEBUG_ANY,
649 "<= bdb_substring_candidates: index_param failed (%d)\n",
657 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: not indexed\n"));
659 Debug( LDAP_DEBUG_ANY,
660 "<= bdb_substring_candidates not indexed\n",
666 mr = sub->sa_desc->ad_type->sat_substr;
672 if( !mr->smr_filter ) {
676 rc = (mr->smr_filter)(
677 LDAP_FILTER_SUBSTRINGS,
679 sub->sa_desc->ad_type->sat_syntax,
685 if( rc != LDAP_SUCCESS ) {
687 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n", sub->sa_desc->ad_cname.bv_val, rc ));
689 Debug( LDAP_DEBUG_TRACE,
690 "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n",
691 sub->sa_desc->ad_cname.bv_val, rc, 0 );
698 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n", mask, sub->sa_desc->ad_cname.bv_val ));
700 Debug( LDAP_DEBUG_TRACE,
701 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
702 mask, sub->sa_desc->ad_cname.bv_val, 0 );
707 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
708 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
710 if( rc == DB_NOTFOUND ) {
714 } else if( rc != LDAP_SUCCESS ) {
716 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: key read failed (%d)\n", rc));
718 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates key read failed (%d)\n",
724 if( BDB_IDL_IS_ZERO( tmp ) ) {
726 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: NULL \n" ));
728 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates NULL\n",
735 bdb_idl_intersection( ids, tmp );
737 if( BDB_IDL_IS_ZERO( ids ) )
741 ber_bvarray_free( keys );
744 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: id=%ld first=%ld last=%ld\n", (long) ids[0], (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) ));
746 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates %ld, first=%ld, last=%ld\n",
748 (long) BDB_IDL_FIRST(ids),
749 (long) BDB_IDL_LAST(ids) );