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(
52 Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
54 switch ( f->f_choice ) {
55 case SLAPD_FILTER_DN_ONE:
56 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
57 rc = bdb_dn2idl( be, f->f_dn, DN_ONE_PREFIX, ids );
60 case SLAPD_FILTER_DN_SUBTREE:
61 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
62 rc = bdb_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, ids );
65 case LDAP_FILTER_PRESENT:
66 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
67 rc = presence_candidates( be, f->f_desc, ids );
70 case LDAP_FILTER_EQUALITY:
71 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
72 rc = equality_candidates( be, f->f_ava, ids, tmp );
75 case LDAP_FILTER_APPROX:
76 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
77 rc = approx_candidates( be, f->f_ava, ids, tmp );
80 case LDAP_FILTER_SUBSTRINGS:
81 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
82 rc = substring_candidates( be, f->f_sub, ids, tmp );
86 /* no GE index, use pres */
87 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
88 rc = presence_candidates( be, f->f_ava->aa_desc, ids );
92 /* no LE index, use pres */
93 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
94 rc = presence_candidates( be, f->f_ava->aa_desc, ids );
98 /* no indexing to support NOT filters */
99 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
102 case LDAP_FILTER_AND:
103 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
104 rc = list_candidates( be,
105 f->f_and, LDAP_FILTER_AND, ids, tmp );
109 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
110 rc = list_candidates( be,
111 f->f_or, LDAP_FILTER_OR, ids, tmp );
115 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %d\n",
119 Debug( LDAP_DEBUG_FILTER,
120 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
122 (long) BDB_IDL_FIRST( ids ),
123 (long) BDB_IDL_LAST( ids ) );
136 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
140 /* Systems that can't increase thread stack size will die with these
141 * structures allocated on the stack. */
142 #if !defined(LDAP_PVT_THREAD_STACK_SIZE) || (LDAP_PVT_THREAD_STACK_SIZE == 0)
143 ID *save = ch_malloc(BDB_IDL_UM_SIZEOF);
145 ID save[BDB_IDL_UM_SIZE];
148 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
150 if ( ftype == LDAP_FILTER_OR ) {
151 BDB_IDL_ALL( bdb, save );
154 BDB_IDL_CPY( save, ids );
157 for ( f = flist; f != NULL; f = f->f_next ) {
158 rc = bdb_filter_candidates( be, f, save, tmp );
161 if ( ftype == LDAP_FILTER_AND ) {
168 if ( ftype == LDAP_FILTER_AND ) {
169 bdb_idl_intersection( ids, save );
170 if( BDB_IDL_IS_ZERO( ids ) )
173 bdb_idl_union( ids, save );
174 BDB_IDL_ALL( bdb, save );
178 #if !defined(LDAP_PVT_THREAD_STACK_SIZE) || (LDAP_PVT_THREAD_STACK_SIZE == 0)
183 Debug( LDAP_DEBUG_FILTER,
184 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
186 (long) BDB_IDL_FIRST(ids),
187 (long) BDB_IDL_LAST(ids) );
190 Debug( LDAP_DEBUG_FILTER,
191 "<= bdb_list_candidates: undefined rc=%d\n",
201 AttributeDescription *desc,
204 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
208 struct berval prefix = {0};
210 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
212 if( desc == slap_schema.si_ad_objectClass ) {
213 BDB_IDL_ALL( bdb, ids );
217 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
218 &db, &mask, &prefix );
220 if( rc != LDAP_SUCCESS ) {
221 Debug( LDAP_DEBUG_TRACE,
222 "<= bdb_presence_candidates: index_param returned=%d\n",
229 Debug( LDAP_DEBUG_TRACE,
230 "<= bdb_presence_candidates: not indexed\n",
235 if( prefix.bv_val == NULL ) {
236 Debug( LDAP_DEBUG_TRACE,
237 "<= bdb_presence_candidates: no prefix\n",
242 rc = bdb_key_read( be, db, NULL, &prefix, ids );
244 if( rc == DB_NOTFOUND ) {
247 } else if( rc != LDAP_SUCCESS ) {
248 Debug( LDAP_DEBUG_TRACE,
249 "<= bdb_presense_candidates: key read failed (%d)\n",
254 Debug(LDAP_DEBUG_TRACE,
255 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
257 (long) BDB_IDL_FIRST(ids),
258 (long) BDB_IDL_LAST(ids) );
267 AttributeAssertion *ava,
275 struct berval prefix = {0};
276 struct berval *keys = NULL;
279 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
281 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
282 &db, &mask, &prefix );
284 if( rc != LDAP_SUCCESS ) {
285 Debug( LDAP_DEBUG_ANY,
286 "<= bdb_equality_candidates: index_param failed (%d)\n",
292 Debug( LDAP_DEBUG_ANY,
293 "<= bdb_equality_candidates: not indexed\n", 0, 0, 0 );
297 mr = ava->aa_desc->ad_type->sat_equality;
302 if( !mr->smr_filter ) {
306 rc = (mr->smr_filter)(
307 LDAP_FILTER_EQUALITY,
309 ava->aa_desc->ad_type->sat_syntax,
315 if( rc != LDAP_SUCCESS ) {
316 Debug( LDAP_DEBUG_TRACE,
317 "<= bdb_equality_candidates: MR filter failed (%d)\n",
323 Debug( LDAP_DEBUG_TRACE,
324 "<= bdb_equality_candidates: no keys\n",
329 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
330 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
332 if( rc == DB_NOTFOUND ) {
335 } else if( rc != LDAP_SUCCESS ) {
336 Debug( LDAP_DEBUG_TRACE,
337 "<= bdb_equality_candidates key read failed (%d)\n",
342 if( BDB_IDL_IS_ZERO( tmp ) ) {
343 Debug( LDAP_DEBUG_TRACE,
344 "<= bdb_equality_candidates NULL\n",
350 bdb_idl_intersection( ids, tmp );
352 if( BDB_IDL_IS_ZERO( ids ) )
356 ber_bvarray_free( keys );
358 Debug( LDAP_DEBUG_TRACE,
359 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
361 (long) BDB_IDL_FIRST(ids),
362 (long) BDB_IDL_LAST(ids) );
370 AttributeAssertion *ava,
378 struct berval prefix = {0};
379 struct berval *keys = NULL;
382 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates\n", 0, 0, 0 );
384 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
385 &db, &mask, &prefix );
387 if( rc != LDAP_SUCCESS ) {
388 Debug( LDAP_DEBUG_ANY,
389 "<= bdb_approx_candidates: index_param failed (%d)\n",
395 Debug( LDAP_DEBUG_ANY,
396 "<= bdb_approx_candidates: not indexed\n", 0, 0, 0 );
400 mr = ava->aa_desc->ad_type->sat_approx;
402 /* no approx matching rule, try equality matching rule */
403 mr = ava->aa_desc->ad_type->sat_equality;
410 if( !mr->smr_filter ) {
414 rc = (mr->smr_filter)(
417 ava->aa_desc->ad_type->sat_syntax,
423 if( rc != LDAP_SUCCESS ) {
424 Debug( LDAP_DEBUG_TRACE,
425 "<= bdb_approx_candidates: (%s) MR filter failed (%d)\n",
426 prefix.bv_val, rc, 0 );
431 Debug( LDAP_DEBUG_TRACE,
432 "<= bdb_approx_candidates: no keys (%s)\n",
433 prefix.bv_val, 0, 0 );
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 ) {
444 } else if( rc != LDAP_SUCCESS ) {
445 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates key read failed (%d)\n",
450 if( BDB_IDL_IS_ZERO( tmp ) ) {
451 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates NULL\n",
457 bdb_idl_intersection( ids, tmp );
459 if( BDB_IDL_IS_ZERO( ids ) )
463 ber_bvarray_free( keys );
465 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
467 (long) BDB_IDL_FIRST(ids),
468 (long) BDB_IDL_LAST(ids) );
474 substring_candidates(
476 SubstringsAssertion *sub,
484 struct berval prefix = {0};
485 struct berval *keys = NULL;
488 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates\n", 0, 0, 0 );
490 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
491 &db, &mask, &prefix );
493 if( rc != LDAP_SUCCESS ) {
494 Debug( LDAP_DEBUG_ANY,
495 "<= bdb_substring_candidates: index_param failed (%d)\n",
501 Debug( LDAP_DEBUG_ANY,
502 "<= bdb_substring_candidates not indexed\n",
507 mr = sub->sa_desc->ad_type->sat_substr;
513 if( !mr->smr_filter ) {
517 rc = (mr->smr_filter)(
518 LDAP_FILTER_SUBSTRINGS,
520 sub->sa_desc->ad_type->sat_syntax,
526 if( rc != LDAP_SUCCESS ) {
527 Debug( LDAP_DEBUG_TRACE,
528 "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n",
529 sub->sa_desc->ad_cname.bv_val, rc, 0 );
534 Debug( LDAP_DEBUG_TRACE,
535 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
536 mask, sub->sa_desc->ad_cname.bv_val, 0 );
540 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
541 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
543 if( rc == DB_NOTFOUND ) {
547 } else if( rc != LDAP_SUCCESS ) {
548 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates key read failed (%d)\n",
553 if( BDB_IDL_IS_ZERO( tmp ) ) {
554 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates NULL\n",
560 bdb_idl_intersection( ids, tmp );
562 if( BDB_IDL_IS_ZERO( ids ) )
566 ber_bvarray_free( keys );
568 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates %ld, first=%ld, last=%ld\n",
570 (long) BDB_IDL_FIRST(ids),
571 (long) BDB_IDL_LAST(ids) );