1 /* filterindex.c - generate the list of candidate entries from a filter */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
16 #include "back-ldbm.h"
18 static ID_BLOCK *presence_candidates(
20 AttributeDescription *desc );
21 static ID_BLOCK *equality_candidates(
22 Operation *op, AttributeAssertion *ava );
23 static ID_BLOCK *approx_candidates(
24 Operation *op, AttributeAssertion *ava );
25 static ID_BLOCK *substring_candidates(
27 SubstringsAssertion *sub );
28 static ID_BLOCK *list_candidates(
42 LDAP_LOG( FILTER, ENTRY, "filter_candidates: enter\n", 0, 0, 0 );
44 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
49 switch ( f->f_choice ) {
50 case SLAPD_FILTER_COMPUTED:
51 switch( f->f_result ) {
52 case SLAPD_COMPARE_UNDEFINED:
53 /* This technically is not the same as FALSE, but it
54 * certainly will produce no matches. list_candidates
55 * will take care of ignoring this filter.
58 case LDAP_COMPARE_FALSE:
61 case LDAP_COMPARE_TRUE:
62 result = idl_allids( op->o_bd );
67 case SLAPD_FILTER_DN_ONE:
69 LDAP_LOG( FILTER, DETAIL1,
70 "filter_candidates: DN ONE (%s)\n", f->f_dn, 0, 0 );
72 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
75 /* an error is treated as an empty list */
76 if ( dn2idl( op->o_bd, f->f_dn, DN_ONE_PREFIX, &result ) != 0
83 case SLAPD_FILTER_DN_SUBTREE:
85 LDAP_LOG( FILTER, DETAIL1,
86 "filter_candidates: DN SUBTREE (%s)\n", f->f_dn, 0, 0 );
88 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
91 /* an error is treated as an empty list */
92 if ( dn2idl( op->o_bd, f->f_dn, DN_SUBTREE_PREFIX, &result ) != 0
99 case LDAP_FILTER_PRESENT:
101 LDAP_LOG( FILTER, DETAIL1,
102 "filter_candidates: Present (%s)\n",
103 f->f_desc->ad_cname.bv_val, 0, 0 );
105 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
108 result = presence_candidates( op, f->f_desc );
111 case LDAP_FILTER_EQUALITY:
113 LDAP_LOG( FILTER, DETAIL1,
114 "filter_candidates: EQUALITY (%s),(%s)\n",
115 f->f_ava->aa_desc->ad_cname.bv_val,
116 f->f_ava->aa_value.bv_val, 0 );
118 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
121 result = equality_candidates( op, f->f_ava );
124 case LDAP_FILTER_APPROX:
126 LDAP_LOG( FILTER, DETAIL1,
127 "filter_candidates: APPROX (%s), (%s)\n",
128 f->f_ava->aa_desc->ad_cname.bv_val,
129 f->f_ava->aa_value.bv_val, 0 );
131 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
134 result = approx_candidates( op, f->f_ava );
137 case LDAP_FILTER_SUBSTRINGS:
139 LDAP_LOG( FILTER, DETAIL1,
140 "filter_candidates: SUBSTRINGS\n", 0, 0, 0 );
142 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
145 result = substring_candidates( op, f->f_sub );
150 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: GE\n", 0, 0, 0 );
152 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
155 result = presence_candidates( op, f->f_ava->aa_desc );
160 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: LE\n", 0, 0, 0 );
162 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
165 result = presence_candidates( op, f->f_ava->aa_desc );
168 case LDAP_FILTER_AND:
170 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: AND\n", 0, 0, 0 );
172 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
175 result = list_candidates( op, f->f_and, LDAP_FILTER_AND );
180 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: OR\n", 0, 0, 0 );
182 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
185 result = list_candidates( op, f->f_or, LDAP_FILTER_OR );
188 case LDAP_FILTER_NOT:
190 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: NOT\n", 0, 0, 0 );
192 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
196 * As candidates lists may contain entries which do
197 * not match the assertion, negation of the inner candidate
198 * list could result in matching entries be excluded from
199 * the returned candidate list.
201 result = idl_allids( op->o_bd );
205 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: UNKNOWN\n", 0, 0, 0 );
207 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN\n", 0, 0, 0 );
209 /* unknown filters must not return NULL, to allow
210 * extended filter processing to be done later.
212 result = idl_allids( op->o_bd );
217 LDAP_LOG( FILTER, ENTRY,
218 "filter_candidates: return %ld\n",
219 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
221 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
222 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
231 AttributeDescription *desc
239 struct berval prefix = {0, NULL};
242 LDAP_LOG( FILTER, ENTRY, "presence_candidates: enter\n", 0, 0, 0 );
244 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
247 idl = idl_allids( op->o_bd );
249 if( desc == slap_schema.si_ad_objectClass ) {
253 rc = index_param( op->o_bd, desc, LDAP_FILTER_PRESENT,
254 &dbname, &mask, &prefix );
256 if( rc != LDAP_SUCCESS ) {
258 LDAP_LOG( FILTER, INFO,
259 "presence_candidates: index_param returned %d\n", rc, 0, 0 );
261 Debug( LDAP_DEBUG_TRACE,
262 "<= presence_candidates: index_param returned=%d\n",
269 if( dbname == NULL ) {
272 LDAP_LOG( FILTER, INFO, "presence_candidates: not indexed\n", 0, 0, 0 );
274 Debug( LDAP_DEBUG_TRACE,
275 "<= presense_candidates: not indexed\n",
282 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
286 LDAP_LOG( FILTER, INFO,
287 "presence_candidates: db open failed (%s%s)\n",
288 dbname, LDBM_SUFFIX, 0 );
290 Debug( LDAP_DEBUG_ANY,
291 "<= presense_candidates db open failed (%s%s)\n",
292 dbname, LDBM_SUFFIX, 0 );
298 if( prefix.bv_val != NULL ) {
302 rc = key_read( op->o_bd, db, &prefix, &idl );
304 if( rc != LDAP_SUCCESS ) {
306 LDAP_LOG( FILTER, ERR,
307 "presence_candidates: key read failed (%d)\n", rc, 0, 0 );
309 Debug( LDAP_DEBUG_TRACE,
310 "<= presense_candidates key read failed (%d)\n",
315 } else if( idl == NULL ) {
317 LDAP_LOG( FILTER, DETAIL1, "presence_candidates: NULL\n", 0, 0, 0 );
319 Debug( LDAP_DEBUG_TRACE,
320 "<= presense_candidates NULL\n",
327 ldbm_cache_close( op->o_bd, db );
330 LDAP_LOG( FILTER, ENTRY,
331 "presence_candidates: return %ld\n",
332 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
334 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
335 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
344 AttributeAssertion *ava
353 struct berval prefix = {0, NULL};
354 struct berval *keys = NULL;
358 LDAP_LOG( FILTER, ENTRY, "equality_candidates: enter\n", 0, 0, 0 );
360 Debug( LDAP_DEBUG_TRACE, "=> equality_candidates\n", 0, 0, 0 );
364 idl = idl_allids( op->o_bd );
366 rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
367 &dbname, &mask, &prefix );
369 if( rc != LDAP_SUCCESS ) {
371 LDAP_LOG( FILTER, ERR,
372 "equality_candidates: index_param returned %d\n", rc, 0, 0 );
374 Debug( LDAP_DEBUG_TRACE,
375 "<= equality_candidates: index_param returned=%d\n",
382 if( dbname == NULL ) {
385 LDAP_LOG( FILTER, ERR, "equality_candidates: not indexed\n", 0, 0, 0 );
387 Debug( LDAP_DEBUG_TRACE,
388 "<= equality_candidates: not indexed\n",
395 mr = ava->aa_desc->ad_type->sat_equality;
400 if( !mr->smr_filter ) {
404 rc = (mr->smr_filter)(
405 LDAP_FILTER_EQUALITY,
407 ava->aa_desc->ad_type->sat_syntax,
411 &keys, op->o_tmpmemctx );
413 if( rc != LDAP_SUCCESS ) {
415 LDAP_LOG( FILTER, ERR,
416 "equality_candidates: (%s%s) MR filter failed (%d\n",
417 dbname, LDBM_SUFFIX, rc );
419 Debug( LDAP_DEBUG_TRACE,
420 "<= equality_candidates: (%s%s) MR filter failed (%d)\n",
421 dbname, LDBM_SUFFIX, rc );
429 LDAP_LOG( FILTER, ERR,
430 "equality_candidates: no keys (%s%s)\n", dbname, LDBM_SUFFIX, 0 );
432 Debug( LDAP_DEBUG_TRACE,
433 "<= equality_candidates: no keys (%s%s)\n",
434 dbname, LDBM_SUFFIX, 0 );
440 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
444 LDAP_LOG( FILTER, ERR, "equality_candidates: db open failed (%s%s)\n",
445 dbname, LDBM_SUFFIX, 0 );
447 Debug( LDAP_DEBUG_ANY,
448 "<= equality_candidates db open failed (%s%s)\n",
449 dbname, LDBM_SUFFIX, 0 );
455 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
459 rc = key_read( op->o_bd, db, &keys[i], &tmp );
461 if( rc != LDAP_SUCCESS ) {
465 LDAP_LOG( FILTER, ERR,
466 "equality_candidates: key read failed (%d)\n", rc, 0, 0 );
468 Debug( LDAP_DEBUG_TRACE,
469 "<= equality_candidates key read failed (%d)\n",
480 LDAP_LOG( FILTER, INFO, "equality_candidates NULL\n", 0, 0, 0 );
482 Debug( LDAP_DEBUG_TRACE,
483 "<= equality_candidates NULL\n",
491 idl = idl_intersection( op->o_bd, idl, tmp );
495 if( idl == NULL ) break;
498 ber_bvarray_free_x( keys, op->o_tmpmemctx );
500 ldbm_cache_close( op->o_bd, db );
504 LDAP_LOG( FILTER, ENTRY,
505 "equality_candidates: return %ld\n",
506 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
508 Debug( LDAP_DEBUG_TRACE, "<= equality_candidates %ld\n",
509 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
518 AttributeAssertion *ava
527 struct berval prefix = {0, NULL};
528 struct berval *keys = NULL;
532 LDAP_LOG( FILTER, ENTRY, "approx_candidates: enter\n", 0, 0, 0 );
534 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
538 idl = idl_allids( op->o_bd );
540 rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_APPROX,
541 &dbname, &mask, &prefix );
543 if( rc != LDAP_SUCCESS ) {
545 LDAP_LOG( FILTER, ERR,
546 "approx_candidates: index_param returned %d\n", rc, 0, 0 );
548 Debug( LDAP_DEBUG_TRACE,
549 "<= approx_candidates: index_param returned=%d\n",
556 if( dbname == NULL ) {
559 LDAP_LOG( FILTER, ERR, "approx_candidates: not indexed\n", 0, 0, 0 );
561 Debug( LDAP_DEBUG_ANY,
562 "<= approx_candidates: not indexed\n",
569 mr = ava->aa_desc->ad_type->sat_approx;
571 /* no approx matching rule, try equality matching rule */
572 mr = ava->aa_desc->ad_type->sat_equality;
579 if( !mr->smr_filter ) {
583 rc = (mr->smr_filter)(
586 ava->aa_desc->ad_type->sat_syntax,
590 &keys, op->o_tmpmemctx );
592 if( rc != LDAP_SUCCESS ) {
594 LDAP_LOG( FILTER, ERR,
595 "approx_candidates: (%s%s) MR filter failed (%d)\n",
596 dbname, LDBM_SUFFIX, rc );
598 Debug( LDAP_DEBUG_TRACE,
599 "<= approx_candidates: (%s%s) MR filter failed (%d)\n",
600 dbname, LDBM_SUFFIX, rc );
608 LDAP_LOG( FILTER, INFO,
609 "approx_candidates: no keys (%s%s)\n",
610 dbname, LDBM_SUFFIX, 0 );
612 Debug( LDAP_DEBUG_TRACE,
613 "<= approx_candidates: no keys (%s%s)\n",
614 dbname, LDBM_SUFFIX, 0 );
620 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
624 LDAP_LOG( FILTER, ERR,
625 "approx_candidates db open failed (%s%s)\n",
626 dbname, LDBM_SUFFIX, 0 );
628 Debug( LDAP_DEBUG_ANY,
629 "<= approx_candidates db open failed (%s%s)\n",
630 dbname, LDBM_SUFFIX, 0 );
636 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
640 rc = key_read( op->o_bd, db, &keys[i], &tmp );
642 if( rc != LDAP_SUCCESS ) {
646 LDAP_LOG( FILTER, ERR,
647 "approx_candidates: key read failed (%d)\n", rc, 0, 0 );
649 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
660 LDAP_LOG( FILTER, INFO, "approx_candidates: NULL\n", 0, 0, 0 );
662 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
670 idl = idl_intersection( op->o_bd, idl, tmp );
674 if( idl == NULL ) break;
677 ber_bvarray_free_x( keys, op->o_tmpmemctx );
679 ldbm_cache_close( op->o_bd, db );
682 LDAP_LOG( FILTER, ENTRY,
683 "approx_candidates: return %ld\n",
684 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
686 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
687 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
700 ID_BLOCK *idl, *tmp, *tmp2;
704 LDAP_LOG( FILTER, ENTRY, "list_candidates: 0x%x\n", ftype, 0, 0 );
706 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
711 for ( f = flist; f != NULL; f = f->f_next ) {
712 if ( f->f_choice == SLAPD_FILTER_COMPUTED &&
713 f->f_result == SLAPD_COMPARE_UNDEFINED ) {
716 if ( (tmp = filter_candidates( op, f )) == NULL &&
717 ftype == LDAP_FILTER_AND ) {
719 LDAP_LOG( FILTER, INFO, "list_candidates: NULL\n", 0, 0, 0 );
721 Debug( LDAP_DEBUG_TRACE,
722 "<= list_candidates NULL\n", 0, 0, 0 );
732 } else if ( ftype == LDAP_FILTER_AND ) {
733 idl = idl_intersection( op->o_bd, idl, tmp );
737 idl = idl_union( op->o_bd, idl, tmp );
744 LDAP_LOG( FILTER, ENTRY, "list_candidates: return %ld\n",
745 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
747 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
748 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
755 substring_candidates(
757 SubstringsAssertion *sub
766 struct berval prefix = {0, NULL};
767 struct berval *keys = NULL;
771 LDAP_LOG( FILTER, ENTRY, "substrings_candidates: enter\n", 0, 0, 0 );
773 Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
777 idl = idl_allids( op->o_bd );
779 rc = index_param( op->o_bd, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
780 &dbname, &mask, &prefix );
782 if( rc != LDAP_SUCCESS ) {
784 LDAP_LOG( FILTER, ERR,
785 "substrings_candidates: index_param returned %d\n", rc, 0, 0 );
787 Debug( LDAP_DEBUG_TRACE,
788 "<= substrings_candidates: index_param returned=%d\n",
795 if( dbname == NULL ) {
798 LDAP_LOG( FILTER, ERR, "substrings_candidates: not indexed\n", 0, 0, 0);
800 Debug( LDAP_DEBUG_ANY,
801 "<= substrings_candidates: not indexed\n",
808 mr = sub->sa_desc->ad_type->sat_substr;
814 if( !mr->smr_filter ) {
818 rc = (mr->smr_filter)(
819 LDAP_FILTER_SUBSTRINGS,
821 sub->sa_desc->ad_type->sat_syntax,
825 &keys, op->o_tmpmemctx );
827 if( rc != LDAP_SUCCESS ) {
829 LDAP_LOG( FILTER, ERR,
830 "substrings_candidates: (%s%s) MR filter failed (%d)\n",
831 dbname, LDBM_SUFFIX, rc );
833 Debug( LDAP_DEBUG_TRACE,
834 "<= substrings_candidates: (%s%s) MR filter failed (%d)\n",
835 dbname, LDBM_SUFFIX, rc );
843 LDAP_LOG( FILTER, ERR,
844 "substrings_candidates: (0x%04lx) no keys (%s%s)\n",
845 mask, dbname, LDBM_SUFFIX );
847 Debug( LDAP_DEBUG_TRACE,
848 "<= substrings_candidates: (0x%04lx) no keys (%s%s)\n",
849 mask, dbname, LDBM_SUFFIX );
855 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
859 LDAP_LOG( FILTER, ERR,
860 "substrings_candidates: db open failed (%s%s)\n",
861 dbname, LDBM_SUFFIX, 0 );
863 Debug( LDAP_DEBUG_ANY,
864 "<= substrings_candidates db open failed (%s%s)\n",
865 dbname, LDBM_SUFFIX, 0 );
871 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
875 rc = key_read( op->o_bd, db, &keys[i], &tmp );
877 if( rc != LDAP_SUCCESS ) {
881 LDAP_LOG( FILTER, ERR,
882 "substrings_candidates: key read failed (%d)\n", rc, 0, 0 );
884 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
895 LDAP_LOG( FILTER, INFO, "substrings_candidates: NULL\n", 0, 0, 0 );
897 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
905 idl = idl_intersection( op->o_bd, idl, tmp );
909 if( idl == NULL ) break;
912 ber_bvarray_free_x( keys, op->o_tmpmemctx );
914 ldbm_cache_close( op->o_bd, db );
917 LDAP_LOG( FILTER, ENTRY,
918 "substrings_candidates: return %ld\n",
919 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
921 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld\n",
922 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );