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 );
68 if( rc == DB_NOTFOUND ) {
74 case SLAPD_FILTER_DN_SUBTREE:
76 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tDN SUBTREE\n"));
78 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
80 rc = bdb_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, ids );
83 case LDAP_FILTER_PRESENT:
85 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tPRESENT\n"));
87 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
89 rc = presence_candidates( be, f->f_desc, ids );
92 case LDAP_FILTER_EQUALITY:
94 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tEQUALITY\n"));
96 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
98 rc = equality_candidates( be, f->f_ava, ids, tmp );
101 case LDAP_FILTER_APPROX:
103 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tAPPROX\n"));
105 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
107 rc = approx_candidates( be, f->f_ava, ids, tmp );
110 case LDAP_FILTER_SUBSTRINGS:
112 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tSUBSTRINGS\n"));
114 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
116 rc = substring_candidates( be, f->f_sub, ids, tmp );
120 /* no GE index, use pres */
122 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tGE\n"));
124 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
126 rc = presence_candidates( be, f->f_ava->aa_desc, ids );
130 /* no LE index, use pres */
132 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tLE\n"));
134 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
136 rc = presence_candidates( be, f->f_ava->aa_desc, ids );
139 case LDAP_FILTER_NOT:
140 /* no indexing to support NOT filters */
142 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tNOT\n"));
144 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
148 case LDAP_FILTER_AND:
150 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tAND\n"));
152 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
154 rc = list_candidates( be,
155 f->f_and, LDAP_FILTER_AND, ids, tmp, stack );
160 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tOR\n"));
162 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
164 rc = list_candidates( be,
165 f->f_or, LDAP_FILTER_OR, ids, tmp, stack );
170 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_filter_candidates: \tUNKNOWN\n"));
172 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %lu\n",
173 (unsigned long) f->f_choice, 0, 0 );
178 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 ) ));
180 Debug( LDAP_DEBUG_FILTER,
181 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
183 (long) BDB_IDL_FIRST( ids ),
184 (long) BDB_IDL_LAST( ids ) );
199 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
204 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "=> bdb_list_candidates: 0x%x\n", ftype));
206 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
209 if ( ftype == LDAP_FILTER_OR ) {
210 BDB_IDL_ALL( bdb, save );
213 BDB_IDL_CPY( save, ids );
216 for ( f = flist; f != NULL; f = f->f_next ) {
217 rc = bdb_filter_candidates( be, f, save, tmp,
218 save+BDB_IDL_UM_SIZE );
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 );
240 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 ) ));
242 Debug( LDAP_DEBUG_FILTER,
243 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
245 (long) BDB_IDL_FIRST(ids),
246 (long) BDB_IDL_LAST(ids) );
251 LDAP_LOG (( "filterindex", LDAP_LEVEL_ARGS, "<= bdb_list_candidates: rc=%d\n", rc));
253 Debug( LDAP_DEBUG_FILTER,
254 "<= bdb_list_candidates: undefined rc=%d\n",
265 AttributeDescription *desc,
268 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
272 struct berval prefix = {0};
275 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_presence_candidates\n"));
277 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
280 if( desc == slap_schema.si_ad_objectClass ) {
281 BDB_IDL_ALL( bdb, ids );
285 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
286 &db, &mask, &prefix );
288 if( rc != LDAP_SUCCESS ) {
290 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_presence_candidates: index_parm returned=%d\n", rc ));
292 Debug( LDAP_DEBUG_TRACE,
293 "<= bdb_presence_candidates: index_param returned=%d\n",
302 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: not indexed\n" ));
304 Debug( LDAP_DEBUG_TRACE,
305 "<= bdb_presence_candidates: not indexed\n",
311 if( prefix.bv_val == NULL ) {
313 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: no prefix\n" ));
315 Debug( LDAP_DEBUG_TRACE,
316 "<= bdb_presence_candidates: no prefix\n",
322 rc = bdb_key_read( be, db, NULL, &prefix, ids );
324 if( rc == DB_NOTFOUND ) {
327 } else if( rc != LDAP_SUCCESS ) {
329 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_presence_candidates: key read failed (%d)\n", rc ));
331 Debug( LDAP_DEBUG_TRACE,
332 "<= bdb_presense_candidates: key read failed (%d)\n",
339 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 ) ));
341 Debug(LDAP_DEBUG_TRACE,
342 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
344 (long) BDB_IDL_FIRST(ids),
345 (long) BDB_IDL_LAST(ids) );
355 AttributeAssertion *ava,
363 struct berval prefix = {0};
364 struct berval *keys = NULL;
368 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> equality_candidates\n"));
370 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
373 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
374 &db, &mask, &prefix );
376 if( rc != LDAP_SUCCESS ) {
378 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: index_param failed (%d)\n", rc));
380 Debug( LDAP_DEBUG_ANY,
381 "<= bdb_equality_candidates: index_param failed (%d)\n",
389 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: not indexed\n"));
391 Debug( LDAP_DEBUG_ANY,
392 "<= bdb_equality_candidates: not indexed\n", 0, 0, 0 );
397 mr = ava->aa_desc->ad_type->sat_equality;
402 if( !mr->smr_filter ) {
406 rc = (mr->smr_filter)(
407 LDAP_FILTER_EQUALITY,
409 ava->aa_desc->ad_type->sat_syntax,
415 if( rc != LDAP_SUCCESS ) {
417 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: MR filter failed (%d)\n", rc));
419 Debug( LDAP_DEBUG_TRACE,
420 "<= bdb_equality_candidates: MR filter failed (%d)\n",
428 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: no keys\n"));
430 Debug( LDAP_DEBUG_TRACE,
431 "<= bdb_equality_candidates: no keys\n",
437 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
438 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
440 if( rc == DB_NOTFOUND ) {
443 } else if( rc != LDAP_SUCCESS ) {
445 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_equality_candidates: key read failed (%d)\n", rc));
447 Debug( LDAP_DEBUG_TRACE,
448 "<= bdb_equality_candidates key read failed (%d)\n",
454 if( BDB_IDL_IS_ZERO( tmp ) ) {
456 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "=> bdb_equality_candidates: NULL\n"));
458 Debug( LDAP_DEBUG_TRACE,
459 "<= bdb_equality_candidates NULL\n",
466 bdb_idl_intersection( ids, tmp );
468 if( BDB_IDL_IS_ZERO( ids ) )
472 ber_bvarray_free( keys );
475 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 ) ));
477 Debug( LDAP_DEBUG_TRACE,
478 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
480 (long) BDB_IDL_FIRST(ids),
481 (long) BDB_IDL_LAST(ids) );
490 AttributeAssertion *ava,
498 struct berval prefix = {0};
499 struct berval *keys = NULL;
503 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_approx_candidates\n"));
505 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates\n", 0, 0, 0 );
508 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
509 &db, &mask, &prefix );
511 if( rc != LDAP_SUCCESS ) {
513 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: index_param failed (%d)\n", rc ));
515 Debug( LDAP_DEBUG_ANY,
516 "<= bdb_approx_candidates: index_param failed (%d)\n",
524 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: not indexed\n" ));
526 Debug( LDAP_DEBUG_ANY,
527 "<= bdb_approx_candidates: not indexed\n", 0, 0, 0 );
532 mr = ava->aa_desc->ad_type->sat_approx;
534 /* no approx matching rule, try equality matching rule */
535 mr = ava->aa_desc->ad_type->sat_equality;
542 if( !mr->smr_filter ) {
546 rc = (mr->smr_filter)(
549 ava->aa_desc->ad_type->sat_syntax,
555 if( rc != LDAP_SUCCESS ) {
557 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: MR filter failed (%d)\n", rc ));
559 Debug( LDAP_DEBUG_TRACE,
560 "<= bdb_approx_candidates: (%s) MR filter failed (%d)\n",
561 prefix.bv_val, rc, 0 );
568 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: no keys (%s)\n", prefix.bv_val ));
570 Debug( LDAP_DEBUG_TRACE,
571 "<= bdb_approx_candidates: no keys (%s)\n",
572 prefix.bv_val, 0, 0 );
577 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
578 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
580 if( rc == DB_NOTFOUND ) {
584 } else if( rc != LDAP_SUCCESS ) {
586 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: key read failed (%d)\n", rc ));
588 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates key read failed (%d)\n",
594 if( BDB_IDL_IS_ZERO( tmp ) ) {
596 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_approx_candidates: NULL\n" ));
598 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates NULL\n",
605 bdb_idl_intersection( ids, tmp );
607 if( BDB_IDL_IS_ZERO( ids ) )
611 ber_bvarray_free( keys );
614 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 ) ));
616 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
618 (long) BDB_IDL_FIRST(ids),
619 (long) BDB_IDL_LAST(ids) );
625 substring_candidates(
627 SubstringsAssertion *sub,
635 struct berval prefix = {0};
636 struct berval *keys = NULL;
640 LDAP_LOG (( "filterindex", LDAP_LEVEL_ENTRY, "=> bdb_substring_candidates\n"));
642 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates\n", 0, 0, 0 );
645 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
646 &db, &mask, &prefix );
648 if( rc != LDAP_SUCCESS ) {
650 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: index_param failed (%d)\n", rc ));
652 Debug( LDAP_DEBUG_ANY,
653 "<= bdb_substring_candidates: index_param failed (%d)\n",
661 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: not indexed\n"));
663 Debug( LDAP_DEBUG_ANY,
664 "<= bdb_substring_candidates not indexed\n",
670 mr = sub->sa_desc->ad_type->sat_substr;
676 if( !mr->smr_filter ) {
680 rc = (mr->smr_filter)(
681 LDAP_FILTER_SUBSTRINGS,
683 sub->sa_desc->ad_type->sat_syntax,
689 if( rc != LDAP_SUCCESS ) {
691 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n", sub->sa_desc->ad_cname.bv_val, rc ));
693 Debug( LDAP_DEBUG_TRACE,
694 "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n",
695 sub->sa_desc->ad_cname.bv_val, rc, 0 );
702 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n", mask, sub->sa_desc->ad_cname.bv_val ));
704 Debug( LDAP_DEBUG_TRACE,
705 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
706 mask, sub->sa_desc->ad_cname.bv_val, 0 );
711 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
712 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
714 if( rc == DB_NOTFOUND ) {
718 } else if( rc != LDAP_SUCCESS ) {
720 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: key read failed (%d)\n", rc));
722 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates key read failed (%d)\n",
728 if( BDB_IDL_IS_ZERO( tmp ) ) {
730 LDAP_LOG (( "filterindex", LDAP_LEVEL_RESULTS, "<= bdb_substring_candidates: NULL \n" ));
732 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates NULL\n",
739 bdb_idl_intersection( ids, tmp );
741 if( BDB_IDL_IS_ZERO( ids ) )
745 ber_bvarray_free( keys );
748 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 ) ));
750 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates %ld, first=%ld, last=%ld\n",
752 (long) BDB_IDL_FIRST(ids),
753 (long) BDB_IDL_LAST(ids) );