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(
45 bdb_filter_candidates(
53 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_filter_candidates\n"));
55 Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
58 switch ( f->f_choice ) {
59 case SLAPD_FILTER_DN_ONE:
61 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tDN ONE\n"));
63 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
65 rc = bdb_dn2idl( be, f->f_dn, DN_ONE_PREFIX, ids );
68 case SLAPD_FILTER_DN_SUBTREE:
70 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tDN SUBTREE\n"));
72 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
74 rc = bdb_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, ids );
77 case LDAP_FILTER_PRESENT:
79 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tPRESENT\n"));
81 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
83 rc = presence_candidates( be, f->f_desc, ids );
86 case LDAP_FILTER_EQUALITY:
88 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tEQUALITY\n"));
90 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
92 rc = equality_candidates( be, f->f_ava, ids, tmp );
95 case LDAP_FILTER_APPROX:
97 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tAPPROX\n"));
99 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
101 rc = approx_candidates( be, f->f_ava, ids, tmp );
104 case LDAP_FILTER_SUBSTRINGS:
106 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tSUBSTRINGS\n"));
108 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
110 rc = substring_candidates( be, f->f_sub, ids, tmp );
114 /* no GE index, use pres */
116 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tGE\n"));
118 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
120 rc = presence_candidates( be, f->f_ava->aa_desc, ids );
124 /* no LE index, use pres */
126 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tLE\n"));
128 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
130 rc = presence_candidates( be, f->f_ava->aa_desc, ids );
133 case LDAP_FILTER_NOT:
134 /* no indexing to support NOT filters */
136 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tNOT\n"));
138 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
142 case LDAP_FILTER_AND:
144 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tAND\n"));
146 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
148 rc = list_candidates( be,
149 f->f_and, LDAP_FILTER_AND, ids, tmp );
154 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tOR\n"));
156 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
158 rc = list_candidates( be,
159 f->f_or, LDAP_FILTER_OR, ids, tmp );
164 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tUNKNOWN\n"));
166 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %d\n",
172 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 ) ));
174 Debug( LDAP_DEBUG_FILTER,
175 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
177 (long) BDB_IDL_FIRST( ids ),
178 (long) BDB_IDL_LAST( ids ) );
192 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
196 /* Systems that can't increase thread stack size will die with these
197 * structures allocated on the stack. */
198 #if !defined(LDAP_PVT_THREAD_STACK_SIZE) || (LDAP_PVT_THREAD_STACK_SIZE == 0)
199 ID *save = ch_malloc(BDB_IDL_UM_SIZEOF);
201 ID save[BDB_IDL_UM_SIZE];
205 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_list_candidates: 0x%x\n", ftype));
207 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
210 if ( ftype == LDAP_FILTER_OR ) {
211 BDB_IDL_ALL( bdb, save );
214 BDB_IDL_CPY( save, ids );
217 for ( f = flist; f != NULL; f = f->f_next ) {
218 rc = bdb_filter_candidates( be, f, save, tmp );
221 if ( ftype == LDAP_FILTER_AND ) {
228 if ( ftype == LDAP_FILTER_AND ) {
229 bdb_idl_intersection( ids, save );
230 if( BDB_IDL_IS_ZERO( ids ) )
233 bdb_idl_union( ids, save );
234 BDB_IDL_ALL( bdb, save );
238 #if !defined(LDAP_PVT_THREAD_STACK_SIZE) || (LDAP_PVT_THREAD_STACK_SIZE == 0)
244 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 ) ));
246 Debug( LDAP_DEBUG_FILTER,
247 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
249 (long) BDB_IDL_FIRST(ids),
250 (long) BDB_IDL_LAST(ids) );
255 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "<= bdb_list_candidates: rc=%d\n", rc));
257 Debug( LDAP_DEBUG_FILTER,
258 "<= bdb_list_candidates: undefined rc=%d\n",
269 AttributeDescription *desc,
272 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
276 struct berval prefix = {0};
279 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_presence_candidates\n"));
281 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
284 if( desc == slap_schema.si_ad_objectClass ) {
285 BDB_IDL_ALL( bdb, ids );
289 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
290 &db, &mask, &prefix );
292 if( rc != LDAP_SUCCESS ) {
294 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_presence_candidates: index_parm returned=%d\n", rc ));
296 Debug( LDAP_DEBUG_TRACE,
297 "<= bdb_presence_candidates: index_param returned=%d\n",
306 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: not indexed\n" ));
308 Debug( LDAP_DEBUG_TRACE,
309 "<= bdb_presence_candidates: not indexed\n",
315 if( prefix.bv_val == NULL ) {
317 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: no prefix\n" ));
319 Debug( LDAP_DEBUG_TRACE,
320 "<= bdb_presence_candidates: no prefix\n",
326 rc = bdb_key_read( be, db, NULL, &prefix, ids );
328 if( rc == DB_NOTFOUND ) {
331 } else if( rc != LDAP_SUCCESS ) {
333 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: key read failed (%d)\n", rc ));
335 Debug( LDAP_DEBUG_TRACE,
336 "<= bdb_presense_candidates: key read failed (%d)\n",
343 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 ) ));
345 Debug(LDAP_DEBUG_TRACE,
346 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
348 (long) BDB_IDL_FIRST(ids),
349 (long) BDB_IDL_LAST(ids) );
359 AttributeAssertion *ava,
367 struct berval prefix = {0};
368 struct berval *keys = NULL;
372 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> equality_candidates\n"));
374 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
377 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
378 &db, &mask, &prefix );
380 if( rc != LDAP_SUCCESS ) {
382 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: index_param failed (%d)\n", rc));
384 Debug( LDAP_DEBUG_ANY,
385 "<= bdb_equality_candidates: index_param failed (%d)\n",
393 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: not indexed\n"));
395 Debug( LDAP_DEBUG_ANY,
396 "<= bdb_equality_candidates: not indexed\n", 0, 0, 0 );
401 mr = ava->aa_desc->ad_type->sat_equality;
406 if( !mr->smr_filter ) {
410 rc = (mr->smr_filter)(
411 LDAP_FILTER_EQUALITY,
413 ava->aa_desc->ad_type->sat_syntax,
419 if( rc != LDAP_SUCCESS ) {
421 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: MR filter failed (%d)\n", rc));
423 Debug( LDAP_DEBUG_TRACE,
424 "<= bdb_equality_candidates: MR filter failed (%d)\n",
432 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: no keys\n"));
434 Debug( LDAP_DEBUG_TRACE,
435 "<= bdb_equality_candidates: no keys\n",
441 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
442 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
444 if( rc == DB_NOTFOUND ) {
447 } else if( rc != LDAP_SUCCESS ) {
449 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_equality_candidates: key read failed (%d)\n", rc));
451 Debug( LDAP_DEBUG_TRACE,
452 "<= bdb_equality_candidates key read failed (%d)\n",
458 if( BDB_IDL_IS_ZERO( tmp ) ) {
460 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: NULL\n"));
462 Debug( LDAP_DEBUG_TRACE,
463 "<= bdb_equality_candidates NULL\n",
470 bdb_idl_intersection( ids, tmp );
472 if( BDB_IDL_IS_ZERO( ids ) )
476 ber_bvarray_free( keys );
479 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 ) ));
481 Debug( LDAP_DEBUG_TRACE,
482 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
484 (long) BDB_IDL_FIRST(ids),
485 (long) BDB_IDL_LAST(ids) );
494 AttributeAssertion *ava,
502 struct berval prefix = {0};
503 struct berval *keys = NULL;
507 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_approx_candidates\n"));
509 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates\n", 0, 0, 0 );
512 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
513 &db, &mask, &prefix );
515 if( rc != LDAP_SUCCESS ) {
517 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: index_param failed (%d)\n", rc ));
519 Debug( LDAP_DEBUG_ANY,
520 "<= bdb_approx_candidates: index_param failed (%d)\n",
528 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: not indexed\n" ));
530 Debug( LDAP_DEBUG_ANY,
531 "<= bdb_approx_candidates: not indexed\n", 0, 0, 0 );
536 mr = ava->aa_desc->ad_type->sat_approx;
538 /* no approx matching rule, try equality matching rule */
539 mr = ava->aa_desc->ad_type->sat_equality;
546 if( !mr->smr_filter ) {
550 rc = (mr->smr_filter)(
553 ava->aa_desc->ad_type->sat_syntax,
559 if( rc != LDAP_SUCCESS ) {
561 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: MR filter failed (%d)\n", rc ));
563 Debug( LDAP_DEBUG_TRACE,
564 "<= bdb_approx_candidates: (%s) MR filter failed (%d)\n",
565 prefix.bv_val, rc, 0 );
572 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: no keys (%s)\n", prefix.bv_val ));
574 Debug( LDAP_DEBUG_TRACE,
575 "<= bdb_approx_candidates: no keys (%s)\n",
576 prefix.bv_val, 0, 0 );
581 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
582 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
584 if( rc == DB_NOTFOUND ) {
588 } else if( rc != LDAP_SUCCESS ) {
590 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: key read failed (%d)\n", rc ));
592 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates key read failed (%d)\n",
598 if( BDB_IDL_IS_ZERO( tmp ) ) {
600 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: NULL\n" ));
602 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates NULL\n",
609 bdb_idl_intersection( ids, tmp );
611 if( BDB_IDL_IS_ZERO( ids ) )
615 ber_bvarray_free( keys );
618 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 ) ));
620 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
622 (long) BDB_IDL_FIRST(ids),
623 (long) BDB_IDL_LAST(ids) );
629 substring_candidates(
631 SubstringsAssertion *sub,
639 struct berval prefix = {0};
640 struct berval *keys = NULL;
644 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_substring_candidates\n"));
646 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates\n", 0, 0, 0 );
649 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
650 &db, &mask, &prefix );
652 if( rc != LDAP_SUCCESS ) {
654 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: index_param failed (%d)\n", rc ));
656 Debug( LDAP_DEBUG_ANY,
657 "<= bdb_substring_candidates: index_param failed (%d)\n",
665 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: not indexed\n"));
667 Debug( LDAP_DEBUG_ANY,
668 "<= bdb_substring_candidates not indexed\n",
674 mr = sub->sa_desc->ad_type->sat_substr;
680 if( !mr->smr_filter ) {
684 rc = (mr->smr_filter)(
685 LDAP_FILTER_SUBSTRINGS,
687 sub->sa_desc->ad_type->sat_syntax,
693 if( rc != LDAP_SUCCESS ) {
695 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n", sub->sa_desc->ad_cname.bv_val, rc ));
697 Debug( LDAP_DEBUG_TRACE,
698 "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n",
699 sub->sa_desc->ad_cname.bv_val, rc, 0 );
706 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n", mask, sub->sa_desc->ad_cname.bv_val ));
708 Debug( LDAP_DEBUG_TRACE,
709 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
710 mask, sub->sa_desc->ad_cname.bv_val, 0 );
715 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
716 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
718 if( rc == DB_NOTFOUND ) {
722 } else if( rc != LDAP_SUCCESS ) {
724 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: key read failed (%d)\n", rc));
726 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates key read failed (%d)\n",
732 if( BDB_IDL_IS_ZERO( tmp ) ) {
734 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: NULL \n" ));
736 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates NULL\n",
743 bdb_idl_intersection( ids, tmp );
745 if( BDB_IDL_IS_ZERO( ids ) )
749 ber_bvarray_free( keys );
752 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 ) ));
754 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates %ld, first=%ld, last=%ld\n",
756 (long) BDB_IDL_FIRST(ids),
757 (long) BDB_IDL_LAST(ids) );