1 /* filterindex.c - generate the list of candidate entries from a filter */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
25 static int presence_candidates(
27 AttributeDescription *desc,
30 static int equality_candidates(
32 AttributeAssertion *ava,
35 static int inequality_candidates(
37 AttributeAssertion *ava,
41 static int approx_candidates(
43 AttributeAssertion *ava,
46 static int substring_candidates(
48 SubstringsAssertion *sub,
52 static int list_candidates(
61 bdb_filter_candidates(
69 Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
71 char *subtree="SUBTREE";
74 switch ( f->f_choice ) {
75 case SLAPD_FILTER_COMPUTED:
76 switch( f->f_result ) {
77 case SLAPD_COMPARE_UNDEFINED:
78 /* This technically is not the same as FALSE, but it
79 * certainly will produce no matches.
82 case LDAP_COMPARE_FALSE:
85 case LDAP_COMPARE_TRUE: {
86 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
87 BDB_IDL_ALL( bdb, ids );
90 /* this is a pre-computed scope, leave it alone */
94 #if 0 /* Not used any more, search calls bdb_dn2idl directly */
95 case SLAPD_FILTER_DN_ONE:
96 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
97 rc = bdb_dn2idl( op->o_bd, f->f_dn, DN_ONE_PREFIX, ids,
98 stack, op->o_tmpmemctx );
99 if( rc == DB_NOTFOUND ) {
105 case SLAPD_FILTER_DN_CHILDREN:
108 case SLAPD_FILTER_DN_SUBTREE:
109 Debug( LDAP_DEBUG_FILTER, "\tDN %s\n",
111 rc = bdb_dn2idl( op->o_bd, f->f_dn, DN_SUBTREE_PREFIX, ids,
112 stack, op->o_tmpmemctx );
115 case LDAP_FILTER_PRESENT:
116 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
117 rc = presence_candidates( op, f->f_desc, ids );
120 case LDAP_FILTER_EQUALITY:
121 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
122 rc = equality_candidates( op, f->f_ava, ids, tmp );
125 case LDAP_FILTER_APPROX:
126 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
127 rc = approx_candidates( op, f->f_ava, ids, tmp );
130 case LDAP_FILTER_SUBSTRINGS:
131 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
132 rc = substring_candidates( op, f->f_sub, ids, tmp );
136 /* if no GE index, use pres */
137 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
138 if( f->f_ava->aa_desc->ad_type->sat_ordering &&
139 ( f->f_ava->aa_desc->ad_type->sat_ordering->smr_usage && SLAP_MR_ORDERED_INDEX ) )
140 rc = inequality_candidates( op, f->f_ava, ids, tmp, LDAP_FILTER_GE );
142 rc = presence_candidates( op, f->f_ava->aa_desc, ids );
146 /* if no LE index, use pres */
147 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
148 if( f->f_ava->aa_desc->ad_type->sat_ordering &&
149 ( f->f_ava->aa_desc->ad_type->sat_ordering->smr_usage && SLAP_MR_ORDERED_INDEX ) )
150 rc = inequality_candidates( op, f->f_ava, ids, tmp, LDAP_FILTER_LE );
152 rc = presence_candidates( op, f->f_ava->aa_desc, ids );
155 case LDAP_FILTER_NOT:
156 /* no indexing to support NOT filters */
157 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
158 { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
159 BDB_IDL_ALL( bdb, ids );
163 case LDAP_FILTER_AND:
164 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
165 rc = list_candidates( op,
166 f->f_and, LDAP_FILTER_AND, ids, tmp, stack );
170 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
171 rc = list_candidates( op,
172 f->f_or, LDAP_FILTER_OR, ids, tmp, stack );
176 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %lu\n",
177 (unsigned long) f->f_choice, 0, 0 );
178 /* Must not return NULL, otherwise extended filters break */
179 { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
180 BDB_IDL_ALL( bdb, ids );
184 Debug( LDAP_DEBUG_FILTER,
185 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
187 (long) BDB_IDL_FIRST( ids ),
188 (long) BDB_IDL_LAST( ids ) );
202 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
206 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
207 for ( f = flist; f != NULL; f = f->f_next ) {
208 /* ignore precomputed scopes */
209 if ( f->f_choice == SLAPD_FILTER_COMPUTED &&
210 f->f_result == LDAP_SUCCESS ) {
213 BDB_IDL_ZERO( save );
214 rc = bdb_filter_candidates( op, f, save, tmp,
215 save+BDB_IDL_UM_SIZE );
218 if ( ftype == LDAP_FILTER_AND ) {
226 if ( ftype == LDAP_FILTER_AND ) {
228 BDB_IDL_CPY( ids, save );
230 bdb_idl_intersection( ids, save );
232 if( BDB_IDL_IS_ZERO( ids ) )
236 BDB_IDL_CPY( ids, save );
238 bdb_idl_union( ids, save );
243 if( rc == LDAP_SUCCESS ) {
244 Debug( LDAP_DEBUG_FILTER,
245 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
247 (long) BDB_IDL_FIRST(ids),
248 (long) BDB_IDL_LAST(ids) );
251 Debug( LDAP_DEBUG_FILTER,
252 "<= bdb_list_candidates: undefined rc=%d\n",
262 AttributeDescription *desc,
265 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
269 struct berval prefix = {0, NULL};
271 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates (%s)\n",
272 desc->ad_cname.bv_val, 0, 0 );
274 BDB_IDL_ALL( bdb, ids );
276 if( desc == slap_schema.si_ad_objectClass ) {
280 rc = bdb_index_param( op->o_bd, desc, LDAP_FILTER_PRESENT,
281 &db, &mask, &prefix );
283 if( rc != LDAP_SUCCESS ) {
284 Debug( LDAP_DEBUG_TRACE,
285 "<= bdb_presence_candidates: (%s) index_param "
287 desc->ad_cname.bv_val, rc, 0 );
293 Debug( LDAP_DEBUG_TRACE,
294 "<= bdb_presence_candidates: (%s) not indexed\n",
295 desc->ad_cname.bv_val, 0, 0 );
299 if( prefix.bv_val == NULL ) {
300 Debug( LDAP_DEBUG_TRACE,
301 "<= bdb_presence_candidates: (%s) no prefix\n",
302 desc->ad_cname.bv_val, 0, 0 );
306 rc = bdb_key_read( op->o_bd, db, NULL, &prefix, ids, NULL, 0 );
308 if( rc == DB_NOTFOUND ) {
311 } else if( rc != LDAP_SUCCESS ) {
312 Debug( LDAP_DEBUG_TRACE,
313 "<= bdb_presense_candidates: (%s) "
314 "key read failed (%d)\n",
315 desc->ad_cname.bv_val, rc, 0 );
319 Debug(LDAP_DEBUG_TRACE,
320 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
322 (long) BDB_IDL_FIRST(ids),
323 (long) BDB_IDL_LAST(ids) );
332 AttributeAssertion *ava,
336 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
341 struct berval prefix = {0, NULL};
342 struct berval *keys = NULL;
345 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates (%s)\n",
346 ava->aa_desc->ad_cname.bv_val, 0, 0 );
348 BDB_IDL_ALL( bdb, ids );
350 rc = bdb_index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
351 &db, &mask, &prefix );
353 if( rc != LDAP_SUCCESS ) {
354 Debug( LDAP_DEBUG_ANY,
355 "<= bdb_equality_candidates: (%s) "
356 "index_param failed (%d)\n",
357 ava->aa_desc->ad_cname.bv_val, rc, 0 );
362 Debug( LDAP_DEBUG_ANY,
363 "<= bdb_equality_candidates: (%s) not indexed\n",
364 ava->aa_desc->ad_cname.bv_val, 0, 0 );
368 mr = ava->aa_desc->ad_type->sat_equality;
373 if( !mr->smr_filter ) {
377 rc = (mr->smr_filter)(
378 LDAP_FILTER_EQUALITY,
380 ava->aa_desc->ad_type->sat_syntax,
384 &keys, op->o_tmpmemctx );
386 if( rc != LDAP_SUCCESS ) {
387 Debug( LDAP_DEBUG_TRACE,
388 "<= bdb_equality_candidates: (%s, %s) "
389 "MR filter failed (%d)\n",
390 prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
395 Debug( LDAP_DEBUG_TRACE,
396 "<= bdb_equality_candidates: (%s) no keys\n",
397 ava->aa_desc->ad_cname.bv_val, 0, 0 );
401 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
402 rc = bdb_key_read( op->o_bd, db, NULL, &keys[i], tmp, NULL, 0 );
404 if( rc == DB_NOTFOUND ) {
408 } else if( rc != LDAP_SUCCESS ) {
409 Debug( LDAP_DEBUG_TRACE,
410 "<= bdb_equality_candidates: (%s) "
411 "key read failed (%d)\n",
412 ava->aa_desc->ad_cname.bv_val, rc, 0 );
416 if( BDB_IDL_IS_ZERO( tmp ) ) {
417 Debug( LDAP_DEBUG_TRACE,
418 "<= bdb_equality_candidates: (%s) NULL\n",
419 ava->aa_desc->ad_cname.bv_val, 0, 0 );
425 BDB_IDL_CPY( ids, tmp );
427 bdb_idl_intersection( ids, tmp );
430 if( BDB_IDL_IS_ZERO( ids ) )
434 ber_bvarray_free_x( keys, op->o_tmpmemctx );
436 Debug( LDAP_DEBUG_TRACE,
437 "<= bdb_equality_candidates: id=%ld, first=%ld, last=%ld\n",
439 (long) BDB_IDL_FIRST(ids),
440 (long) BDB_IDL_LAST(ids) );
448 AttributeAssertion *ava,
452 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
457 struct berval prefix = {0, NULL};
458 struct berval *keys = NULL;
461 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates (%s)\n",
462 ava->aa_desc->ad_cname.bv_val, 0, 0 );
464 BDB_IDL_ALL( bdb, ids );
466 rc = bdb_index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_APPROX,
467 &db, &mask, &prefix );
469 if( rc != LDAP_SUCCESS ) {
470 Debug( LDAP_DEBUG_ANY,
471 "<= bdb_approx_candidates: (%s) "
472 "index_param failed (%d)\n",
473 ava->aa_desc->ad_cname.bv_val, rc, 0 );
478 Debug( LDAP_DEBUG_ANY,
479 "<= bdb_approx_candidates: (%s) not indexed\n",
480 ava->aa_desc->ad_cname.bv_val, 0, 0 );
484 mr = ava->aa_desc->ad_type->sat_approx;
486 /* no approx matching rule, try equality matching rule */
487 mr = ava->aa_desc->ad_type->sat_equality;
494 if( !mr->smr_filter ) {
498 rc = (mr->smr_filter)(
501 ava->aa_desc->ad_type->sat_syntax,
505 &keys, op->o_tmpmemctx );
507 if( rc != LDAP_SUCCESS ) {
508 Debug( LDAP_DEBUG_TRACE,
509 "<= bdb_approx_candidates: (%s, %s) "
510 "MR filter failed (%d)\n",
511 prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
516 Debug( LDAP_DEBUG_TRACE,
517 "<= bdb_approx_candidates: (%s) no keys (%s)\n",
518 prefix.bv_val, ava->aa_desc->ad_cname.bv_val, 0 );
522 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
523 rc = bdb_key_read( op->o_bd, db, NULL, &keys[i], tmp, NULL, 0 );
525 if( rc == DB_NOTFOUND ) {
529 } else if( rc != LDAP_SUCCESS ) {
530 Debug( LDAP_DEBUG_TRACE,
531 "<= bdb_approx_candidates: (%s) "
532 "key read failed (%d)\n",
533 ava->aa_desc->ad_cname.bv_val, rc, 0 );
537 if( BDB_IDL_IS_ZERO( tmp ) ) {
538 Debug( LDAP_DEBUG_TRACE,
539 "<= bdb_approx_candidates: (%s) NULL\n",
540 ava->aa_desc->ad_cname.bv_val, 0, 0 );
546 BDB_IDL_CPY( ids, tmp );
548 bdb_idl_intersection( ids, tmp );
551 if( BDB_IDL_IS_ZERO( ids ) )
555 ber_bvarray_free_x( keys, op->o_tmpmemctx );
557 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
559 (long) BDB_IDL_FIRST(ids),
560 (long) BDB_IDL_LAST(ids) );
565 substring_candidates(
567 SubstringsAssertion *sub,
571 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
576 struct berval prefix = {0, NULL};
577 struct berval *keys = NULL;
580 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates (%s)\n",
581 sub->sa_desc->ad_cname.bv_val, 0, 0 );
583 BDB_IDL_ALL( bdb, ids );
585 rc = bdb_index_param( op->o_bd, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
586 &db, &mask, &prefix );
588 if( rc != LDAP_SUCCESS ) {
589 Debug( LDAP_DEBUG_ANY,
590 "<= bdb_substring_candidates: (%s) "
591 "index_param failed (%d)\n",
592 sub->sa_desc->ad_cname.bv_val, rc, 0 );
597 Debug( LDAP_DEBUG_ANY,
598 "<= bdb_substring_candidates: (%s) not indexed\n",
599 sub->sa_desc->ad_cname.bv_val, 0, 0 );
603 mr = sub->sa_desc->ad_type->sat_substr;
609 if( !mr->smr_filter ) {
613 rc = (mr->smr_filter)(
614 LDAP_FILTER_SUBSTRINGS,
616 sub->sa_desc->ad_type->sat_syntax,
620 &keys, op->o_tmpmemctx );
622 if( rc != LDAP_SUCCESS ) {
623 Debug( LDAP_DEBUG_TRACE,
624 "<= bdb_substring_candidates: (%s) "
625 "MR filter failed (%d)\n",
626 sub->sa_desc->ad_cname.bv_val, rc, 0 );
631 Debug( LDAP_DEBUG_TRACE,
632 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
633 mask, sub->sa_desc->ad_cname.bv_val, 0 );
637 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
638 rc = bdb_key_read( op->o_bd, db, NULL, &keys[i], tmp, NULL, 0 );
640 if( rc == DB_NOTFOUND ) {
644 } else if( rc != LDAP_SUCCESS ) {
645 Debug( LDAP_DEBUG_TRACE,
646 "<= bdb_substring_candidates: (%s) "
647 "key read failed (%d)\n",
648 sub->sa_desc->ad_cname.bv_val, rc, 0 );
652 if( BDB_IDL_IS_ZERO( tmp ) ) {
653 Debug( LDAP_DEBUG_TRACE,
654 "<= bdb_substring_candidates: (%s) NULL\n",
655 sub->sa_desc->ad_cname.bv_val, 0, 0 );
661 BDB_IDL_CPY( ids, tmp );
663 bdb_idl_intersection( ids, tmp );
666 if( BDB_IDL_IS_ZERO( ids ) )
670 ber_bvarray_free_x( keys, op->o_tmpmemctx );
672 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates: %ld, first=%ld, last=%ld\n",
674 (long) BDB_IDL_FIRST(ids),
675 (long) BDB_IDL_LAST(ids) );
680 inequality_candidates(
682 AttributeAssertion *ava,
687 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
692 struct berval prefix = {0, NULL};
693 struct berval *keys = NULL;
697 Debug( LDAP_DEBUG_TRACE, "=> bdb_inequality_candidates (%s)\n",
698 ava->aa_desc->ad_cname.bv_val, 0, 0 );
700 BDB_IDL_ALL( bdb, ids );
702 rc = bdb_index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
703 &db, &mask, &prefix );
705 if( rc != LDAP_SUCCESS ) {
706 Debug( LDAP_DEBUG_ANY,
707 "<= bdb_inequality_candidates: (%s) "
708 "index_param failed (%d)\n",
709 ava->aa_desc->ad_cname.bv_val, rc, 0 );
714 Debug( LDAP_DEBUG_ANY,
715 "<= bdb_inequality_candidates: (%s) not indexed\n",
716 ava->aa_desc->ad_cname.bv_val, 0, 0 );
720 mr = ava->aa_desc->ad_type->sat_equality;
725 if( !mr->smr_filter ) {
729 rc = (mr->smr_filter)(
730 LDAP_FILTER_EQUALITY,
732 ava->aa_desc->ad_type->sat_syntax,
736 &keys, op->o_tmpmemctx );
738 if( rc != LDAP_SUCCESS ) {
739 Debug( LDAP_DEBUG_TRACE,
740 "<= bdb_inequality_candidates: (%s, %s) "
741 "MR filter failed (%d)\n",
742 prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
747 Debug( LDAP_DEBUG_TRACE,
748 "<= bdb_inequality_candidates: (%s) no keys\n",
749 ava->aa_desc->ad_cname.bv_val, 0, 0 );
755 rc = bdb_key_read( op->o_bd, db, NULL, &keys[0], tmp, &cursor, gtorlt );
757 if( rc == DB_NOTFOUND ) {
760 } else if( rc != LDAP_SUCCESS ) {
761 Debug( LDAP_DEBUG_TRACE,
762 "<= bdb_inequality_candidates: (%s) "
763 "key read failed (%d)\n",
764 ava->aa_desc->ad_cname.bv_val, rc, 0 );
768 if( BDB_IDL_IS_ZERO( tmp ) ) {
769 Debug( LDAP_DEBUG_TRACE,
770 "<= bdb_inequality_candidates: (%s) NULL\n",
771 ava->aa_desc->ad_cname.bv_val, 0, 0 );
775 bdb_idl_union( ids, tmp );
777 if( BDB_IDL_IS_ZERO( ids ) )
781 ber_bvarray_free_x( keys, op->o_tmpmemctx );
783 Debug( LDAP_DEBUG_TRACE,
784 "<= bdb_inequality_candidates: id=%ld, first=%ld, last=%ld\n",
786 (long) BDB_IDL_FIRST(ids),
787 (long) BDB_IDL_LAST(ids) );