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 /* Error: treat as undefined */
165 if ( ftype == LDAP_FILTER_AND ) {
166 bdb_idl_intersection( ids, save );
167 if( BDB_IDL_IS_ZERO( ids ) )
170 bdb_idl_union( ids, save );
171 BDB_IDL_ALL( bdb, save );
174 #if !defined(LDAP_PVT_THREAD_STACK_SIZE) || (LDAP_PVT_THREAD_STACK_SIZE == 0)
178 Debug( LDAP_DEBUG_FILTER,
179 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
181 (long) BDB_IDL_FIRST(ids),
182 (long) BDB_IDL_LAST(ids) );
189 AttributeDescription *desc,
192 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
196 struct berval prefix = {0};
198 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
200 if( desc == slap_schema.si_ad_objectClass ) {
201 BDB_IDL_ALL( bdb, ids );
205 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
206 &db, &mask, &prefix );
208 if( rc != LDAP_SUCCESS ) {
209 Debug( LDAP_DEBUG_TRACE,
210 "<= bdb_presence_candidates: index_param returned=%d\n",
217 Debug( LDAP_DEBUG_TRACE,
218 "<= bdb_presence_candidates: not indexed\n",
223 if( prefix.bv_val == NULL ) {
224 Debug( LDAP_DEBUG_TRACE,
225 "<= bdb_presence_candidates: no prefix\n",
230 rc = bdb_key_read( be, db, NULL, &prefix, ids );
232 if( rc == DB_NOTFOUND ) {
235 } else if( rc != LDAP_SUCCESS ) {
236 Debug( LDAP_DEBUG_TRACE,
237 "<= bdb_presense_candidates: key read failed (%d)\n",
242 Debug(LDAP_DEBUG_TRACE,
243 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
245 (long) BDB_IDL_FIRST(ids),
246 (long) BDB_IDL_LAST(ids) );
255 AttributeAssertion *ava,
263 struct berval prefix = {0};
264 struct berval *keys = NULL;
267 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
269 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
270 &db, &mask, &prefix );
272 if( rc != LDAP_SUCCESS ) {
273 Debug( LDAP_DEBUG_ANY,
274 "<= bdb_equality_candidates: index_param failed (%d)\n",
280 Debug( LDAP_DEBUG_ANY,
281 "<= bdb_equality_candidates: not indexed\n", 0, 0, 0 );
285 mr = ava->aa_desc->ad_type->sat_equality;
290 if( !mr->smr_filter ) {
294 rc = (mr->smr_filter)(
295 LDAP_FILTER_EQUALITY,
297 ava->aa_desc->ad_type->sat_syntax,
303 if( rc != LDAP_SUCCESS ) {
304 Debug( LDAP_DEBUG_TRACE,
305 "<= bdb_equality_candidates: MR filter failed (%d)\n",
311 Debug( LDAP_DEBUG_TRACE,
312 "<= bdb_equality_candidates: no keys\n",
317 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
318 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
320 if( rc == DB_NOTFOUND ) {
323 } else if( rc != LDAP_SUCCESS ) {
324 Debug( LDAP_DEBUG_TRACE,
325 "<= bdb_equality_candidates key read failed (%d)\n",
330 if( BDB_IDL_IS_ZERO( tmp ) ) {
331 Debug( LDAP_DEBUG_TRACE,
332 "<= bdb_equality_candidates NULL\n",
338 bdb_idl_intersection( ids, tmp );
340 if( BDB_IDL_IS_ZERO( ids ) )
344 ber_bvarray_free( keys );
346 Debug( LDAP_DEBUG_TRACE,
347 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
349 (long) BDB_IDL_FIRST(ids),
350 (long) BDB_IDL_LAST(ids) );
358 AttributeAssertion *ava,
366 struct berval prefix = {0};
367 struct berval *keys = NULL;
370 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates\n", 0, 0, 0 );
372 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
373 &db, &mask, &prefix );
375 if( rc != LDAP_SUCCESS ) {
376 Debug( LDAP_DEBUG_ANY,
377 "<= bdb_approx_candidates: index_param failed (%d)\n",
383 Debug( LDAP_DEBUG_ANY,
384 "<= bdb_approx_candidates: not indexed\n", 0, 0, 0 );
388 mr = ava->aa_desc->ad_type->sat_approx;
390 /* no approx matching rule, try equality matching rule */
391 mr = ava->aa_desc->ad_type->sat_equality;
398 if( !mr->smr_filter ) {
402 rc = (mr->smr_filter)(
405 ava->aa_desc->ad_type->sat_syntax,
411 if( rc != LDAP_SUCCESS ) {
412 Debug( LDAP_DEBUG_TRACE,
413 "<= bdb_approx_candidates: (%s) MR filter failed (%d)\n",
414 prefix.bv_val, rc, 0 );
419 Debug( LDAP_DEBUG_TRACE,
420 "<= bdb_approx_candidates: no keys (%s)\n",
421 prefix.bv_val, 0, 0 );
425 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
426 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
428 if( rc == DB_NOTFOUND ) {
432 } else if( rc != LDAP_SUCCESS ) {
433 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates key read failed (%d)\n",
438 if( BDB_IDL_IS_ZERO( tmp ) ) {
439 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates NULL\n",
445 bdb_idl_intersection( ids, tmp );
447 if( BDB_IDL_IS_ZERO( ids ) )
451 ber_bvarray_free( keys );
453 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
455 (long) BDB_IDL_FIRST(ids),
456 (long) BDB_IDL_LAST(ids) );
462 substring_candidates(
464 SubstringsAssertion *sub,
472 struct berval prefix = {0};
473 struct berval *keys = NULL;
476 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates\n", 0, 0, 0 );
478 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
479 &db, &mask, &prefix );
481 if( rc != LDAP_SUCCESS ) {
482 Debug( LDAP_DEBUG_ANY,
483 "<= bdb_substring_candidates: index_param failed (%d)\n",
489 Debug( LDAP_DEBUG_ANY,
490 "<= bdb_substring_candidates not indexed\n",
495 mr = sub->sa_desc->ad_type->sat_substr;
501 if( !mr->smr_filter ) {
505 rc = (mr->smr_filter)(
506 LDAP_FILTER_SUBSTRINGS,
508 sub->sa_desc->ad_type->sat_syntax,
514 if( rc != LDAP_SUCCESS ) {
515 Debug( LDAP_DEBUG_TRACE,
516 "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n",
517 sub->sa_desc->ad_cname.bv_val, rc, 0 );
522 Debug( LDAP_DEBUG_TRACE,
523 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
524 mask, sub->sa_desc->ad_cname.bv_val, 0 );
528 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
529 rc = bdb_key_read( be, db, NULL, &keys[i], tmp );
531 if( rc == DB_NOTFOUND ) {
535 } else if( rc != LDAP_SUCCESS ) {
536 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates key read failed (%d)\n",
541 if( BDB_IDL_IS_ZERO( tmp ) ) {
542 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates NULL\n",
548 bdb_idl_intersection( ids, tmp );
550 if( BDB_IDL_IS_ZERO( ids ) )
554 ber_bvarray_free( keys );
556 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates %ld, first=%ld, last=%ld\n",
558 (long) BDB_IDL_FIRST(ids),
559 (long) BDB_IDL_LAST(ids) );