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 approx_candidates(
37 AttributeAssertion *ava,
40 static int substring_candidates(
42 SubstringsAssertion *sub,
46 static int list_candidates(
55 bdb_filter_candidates(
63 Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
65 char *subtree="SUBTREE";
68 switch ( f->f_choice ) {
69 case SLAPD_FILTER_COMPUTED:
70 switch( f->f_result ) {
71 case SLAPD_COMPARE_UNDEFINED:
72 /* This technically is not the same as FALSE, but it
73 * certainly will produce no matches.
76 case LDAP_COMPARE_FALSE:
79 case LDAP_COMPARE_TRUE: {
80 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
81 BDB_IDL_ALL( bdb, ids );
84 /* this is a pre-computed scope, leave it alone */
88 #if 0 /* Not used any more, search calls bdb_dn2idl directly */
89 case SLAPD_FILTER_DN_ONE:
90 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
91 rc = bdb_dn2idl( op->o_bd, f->f_dn, DN_ONE_PREFIX, ids,
92 stack, op->o_tmpmemctx );
93 if( rc == DB_NOTFOUND ) {
99 case SLAPD_FILTER_DN_CHILDREN:
102 case SLAPD_FILTER_DN_SUBTREE:
103 Debug( LDAP_DEBUG_FILTER, "\tDN %s\n",
105 rc = bdb_dn2idl( op->o_bd, f->f_dn, DN_SUBTREE_PREFIX, ids,
106 stack, op->o_tmpmemctx );
109 case LDAP_FILTER_PRESENT:
110 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
111 rc = presence_candidates( op, f->f_desc, ids );
114 case LDAP_FILTER_EQUALITY:
115 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
116 rc = equality_candidates( op, f->f_ava, ids, tmp );
119 case LDAP_FILTER_APPROX:
120 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
121 rc = approx_candidates( op, f->f_ava, ids, tmp );
124 case LDAP_FILTER_SUBSTRINGS:
125 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
126 rc = substring_candidates( op, f->f_sub, ids, tmp );
130 /* no GE index, use pres */
131 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
132 rc = presence_candidates( op, f->f_ava->aa_desc, ids );
136 /* no LE index, use pres */
137 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
138 rc = presence_candidates( op, f->f_ava->aa_desc, ids );
141 case LDAP_FILTER_NOT:
142 /* no indexing to support NOT filters */
143 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
144 { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
145 BDB_IDL_ALL( bdb, ids );
149 case LDAP_FILTER_AND:
150 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
151 rc = list_candidates( op,
152 f->f_and, LDAP_FILTER_AND, ids, tmp, stack );
156 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
157 rc = list_candidates( op,
158 f->f_or, LDAP_FILTER_OR, ids, tmp, stack );
162 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %lu\n",
163 (unsigned long) f->f_choice, 0, 0 );
164 /* Must not return NULL, otherwise extended filters break */
165 { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
166 BDB_IDL_ALL( bdb, ids );
170 Debug( LDAP_DEBUG_FILTER,
171 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
173 (long) BDB_IDL_FIRST( ids ),
174 (long) BDB_IDL_LAST( ids ) );
188 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
192 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
193 for ( f = flist; f != NULL; f = f->f_next ) {
194 /* ignore precomputed scopes */
195 if ( f->f_choice == SLAPD_FILTER_COMPUTED &&
196 f->f_result == LDAP_SUCCESS ) {
199 BDB_IDL_ZERO( save );
200 rc = bdb_filter_candidates( op, f, save, tmp,
201 save+BDB_IDL_UM_SIZE );
204 if ( ftype == LDAP_FILTER_AND ) {
212 if ( ftype == LDAP_FILTER_AND ) {
214 BDB_IDL_CPY( ids, save );
216 bdb_idl_intersection( ids, save );
218 if( BDB_IDL_IS_ZERO( ids ) )
222 BDB_IDL_CPY( ids, save );
224 bdb_idl_union( ids, save );
229 if( rc == LDAP_SUCCESS ) {
230 Debug( LDAP_DEBUG_FILTER,
231 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
233 (long) BDB_IDL_FIRST(ids),
234 (long) BDB_IDL_LAST(ids) );
237 Debug( LDAP_DEBUG_FILTER,
238 "<= bdb_list_candidates: undefined rc=%d\n",
248 AttributeDescription *desc,
251 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
255 struct berval prefix = {0, NULL};
257 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates (%s)\n",
258 desc->ad_cname.bv_val, 0, 0 );
260 BDB_IDL_ALL( bdb, ids );
262 if( desc == slap_schema.si_ad_objectClass ) {
266 rc = bdb_index_param( op->o_bd, desc, LDAP_FILTER_PRESENT,
267 &db, &mask, &prefix );
269 if( rc != LDAP_SUCCESS ) {
270 Debug( LDAP_DEBUG_TRACE,
271 "<= bdb_presence_candidates: (%s) index_param "
273 desc->ad_cname.bv_val, rc, 0 );
279 Debug( LDAP_DEBUG_TRACE,
280 "<= bdb_presence_candidates: (%s) not indexed\n",
281 desc->ad_cname.bv_val, 0, 0 );
285 if( prefix.bv_val == NULL ) {
286 Debug( LDAP_DEBUG_TRACE,
287 "<= bdb_presence_candidates: (%s) no prefix\n",
288 desc->ad_cname.bv_val, 0, 0 );
292 rc = bdb_key_read( op->o_bd, db, NULL, &prefix, ids );
294 if( rc == DB_NOTFOUND ) {
297 } else if( rc != LDAP_SUCCESS ) {
298 Debug( LDAP_DEBUG_TRACE,
299 "<= bdb_presense_candidates: (%s) "
300 "key read failed (%d)\n",
301 desc->ad_cname.bv_val, rc, 0 );
305 Debug(LDAP_DEBUG_TRACE,
306 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
308 (long) BDB_IDL_FIRST(ids),
309 (long) BDB_IDL_LAST(ids) );
318 AttributeAssertion *ava,
322 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
327 struct berval prefix = {0, NULL};
328 struct berval *keys = NULL;
331 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates (%s)\n",
332 ava->aa_desc->ad_cname.bv_val, 0, 0 );
334 BDB_IDL_ALL( bdb, ids );
336 rc = bdb_index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
337 &db, &mask, &prefix );
339 if( rc != LDAP_SUCCESS ) {
340 Debug( LDAP_DEBUG_ANY,
341 "<= bdb_equality_candidates: (%s) "
342 "index_param failed (%d)\n",
343 ava->aa_desc->ad_cname.bv_val, rc, 0 );
348 Debug( LDAP_DEBUG_ANY,
349 "<= bdb_equality_candidates: (%s) not indexed\n",
350 ava->aa_desc->ad_cname.bv_val, 0, 0 );
354 mr = ava->aa_desc->ad_type->sat_equality;
359 if( !mr->smr_filter ) {
363 rc = (mr->smr_filter)(
364 LDAP_FILTER_EQUALITY,
366 ava->aa_desc->ad_type->sat_syntax,
370 &keys, op->o_tmpmemctx );
372 if( rc != LDAP_SUCCESS ) {
373 Debug( LDAP_DEBUG_TRACE,
374 "<= bdb_equality_candidates: (%s, %s) "
375 "MR filter failed (%d)\n",
376 prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
381 Debug( LDAP_DEBUG_TRACE,
382 "<= bdb_equality_candidates: (%s) no keys\n",
383 ava->aa_desc->ad_cname.bv_val, 0, 0 );
387 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
388 rc = bdb_key_read( op->o_bd, db, NULL, &keys[i], tmp );
390 if( rc == DB_NOTFOUND ) {
394 } else if( rc != LDAP_SUCCESS ) {
395 Debug( LDAP_DEBUG_TRACE,
396 "<= bdb_equality_candidates: (%s) "
397 "key read failed (%d)\n",
398 ava->aa_desc->ad_cname.bv_val, rc, 0 );
402 if( BDB_IDL_IS_ZERO( tmp ) ) {
403 Debug( LDAP_DEBUG_TRACE,
404 "<= bdb_equality_candidates: (%s) NULL\n",
405 ava->aa_desc->ad_cname.bv_val, 0, 0 );
411 BDB_IDL_CPY( ids, tmp );
413 bdb_idl_intersection( ids, tmp );
416 if( BDB_IDL_IS_ZERO( ids ) )
420 ber_bvarray_free_x( keys, op->o_tmpmemctx );
422 Debug( LDAP_DEBUG_TRACE,
423 "<= bdb_equality_candidates: id=%ld, first=%ld, last=%ld\n",
425 (long) BDB_IDL_FIRST(ids),
426 (long) BDB_IDL_LAST(ids) );
434 AttributeAssertion *ava,
438 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
443 struct berval prefix = {0, NULL};
444 struct berval *keys = NULL;
447 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates (%s)\n",
448 ava->aa_desc->ad_cname.bv_val, 0, 0 );
450 BDB_IDL_ALL( bdb, ids );
452 rc = bdb_index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_APPROX,
453 &db, &mask, &prefix );
455 if( rc != LDAP_SUCCESS ) {
456 Debug( LDAP_DEBUG_ANY,
457 "<= bdb_approx_candidates: (%s) "
458 "index_param failed (%d)\n",
459 ava->aa_desc->ad_cname.bv_val, rc, 0 );
464 Debug( LDAP_DEBUG_ANY,
465 "<= bdb_approx_candidates: (%s) not indexed\n",
466 ava->aa_desc->ad_cname.bv_val, 0, 0 );
470 mr = ava->aa_desc->ad_type->sat_approx;
472 /* no approx matching rule, try equality matching rule */
473 mr = ava->aa_desc->ad_type->sat_equality;
480 if( !mr->smr_filter ) {
484 rc = (mr->smr_filter)(
487 ava->aa_desc->ad_type->sat_syntax,
491 &keys, op->o_tmpmemctx );
493 if( rc != LDAP_SUCCESS ) {
494 Debug( LDAP_DEBUG_TRACE,
495 "<= bdb_approx_candidates: (%s, %s) "
496 "MR filter failed (%d)\n",
497 prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
502 Debug( LDAP_DEBUG_TRACE,
503 "<= bdb_approx_candidates: (%s) no keys (%s)\n",
504 prefix.bv_val, ava->aa_desc->ad_cname.bv_val, 0 );
508 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
509 rc = bdb_key_read( op->o_bd, db, NULL, &keys[i], tmp );
511 if( rc == DB_NOTFOUND ) {
515 } else if( rc != LDAP_SUCCESS ) {
516 Debug( LDAP_DEBUG_TRACE,
517 "<= bdb_approx_candidates: (%s) "
518 "key read failed (%d)\n",
519 ava->aa_desc->ad_cname.bv_val, rc, 0 );
523 if( BDB_IDL_IS_ZERO( tmp ) ) {
524 Debug( LDAP_DEBUG_TRACE,
525 "<= bdb_approx_candidates: (%s) NULL\n",
526 ava->aa_desc->ad_cname.bv_val, 0, 0 );
532 BDB_IDL_CPY( ids, tmp );
534 bdb_idl_intersection( ids, tmp );
537 if( BDB_IDL_IS_ZERO( ids ) )
541 ber_bvarray_free_x( keys, op->o_tmpmemctx );
543 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
545 (long) BDB_IDL_FIRST(ids),
546 (long) BDB_IDL_LAST(ids) );
551 substring_candidates(
553 SubstringsAssertion *sub,
557 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
562 struct berval prefix = {0, NULL};
563 struct berval *keys = NULL;
566 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates (%s)\n",
567 sub->sa_desc->ad_cname.bv_val, 0, 0 );
569 BDB_IDL_ALL( bdb, ids );
571 rc = bdb_index_param( op->o_bd, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
572 &db, &mask, &prefix );
574 if( rc != LDAP_SUCCESS ) {
575 Debug( LDAP_DEBUG_ANY,
576 "<= bdb_substring_candidates: (%s) "
577 "index_param failed (%d)\n",
578 sub->sa_desc->ad_cname.bv_val, rc, 0 );
583 Debug( LDAP_DEBUG_ANY,
584 "<= bdb_substring_candidates: (%s) not indexed\n",
585 sub->sa_desc->ad_cname.bv_val, 0, 0 );
589 mr = sub->sa_desc->ad_type->sat_substr;
595 if( !mr->smr_filter ) {
599 rc = (mr->smr_filter)(
600 LDAP_FILTER_SUBSTRINGS,
602 sub->sa_desc->ad_type->sat_syntax,
606 &keys, op->o_tmpmemctx );
608 if( rc != LDAP_SUCCESS ) {
609 Debug( LDAP_DEBUG_TRACE,
610 "<= bdb_substring_candidates: (%s) "
611 "MR filter failed (%d)\n",
612 sub->sa_desc->ad_cname.bv_val, rc, 0 );
617 Debug( LDAP_DEBUG_TRACE,
618 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
619 mask, sub->sa_desc->ad_cname.bv_val, 0 );
623 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
624 rc = bdb_key_read( op->o_bd, db, NULL, &keys[i], tmp );
626 if( rc == DB_NOTFOUND ) {
630 } else if( rc != LDAP_SUCCESS ) {
631 Debug( LDAP_DEBUG_TRACE,
632 "<= bdb_substring_candidates: (%s) "
633 "key read failed (%d)\n",
634 sub->sa_desc->ad_cname.bv_val, rc, 0 );
638 if( BDB_IDL_IS_ZERO( tmp ) ) {
639 Debug( LDAP_DEBUG_TRACE,
640 "<= bdb_substring_candidates: (%s) NULL\n",
641 sub->sa_desc->ad_cname.bv_val, 0, 0 );
647 BDB_IDL_CPY( ids, tmp );
649 bdb_idl_intersection( ids, tmp );
652 if( BDB_IDL_IS_ZERO( ids ) )
656 ber_bvarray_free_x( keys, op->o_tmpmemctx );
658 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates: %ld, first=%ld, last=%ld\n",
660 (long) BDB_IDL_FIRST(ids),
661 (long) BDB_IDL_LAST(ids) );