1 /* filterindex.c - generate the list of candidate entries from a filter */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 #ifdef BDB_FILTER_INDICES
18 static int presence_candidates(
21 AttributeDescription *desc,
23 static int equality_candidates(
26 AttributeAssertion *ava,
28 static int approx_candidates(
31 AttributeAssertion *ava,
33 static int substring_candidates(
36 SubstringsAssertion *sub,
39 static int list_candidates(
48 bdb_filter_candidates(
55 Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
57 switch ( f->f_choice ) {
58 case SLAPD_FILTER_DN_ONE:
59 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
60 rc = bdb_dn2idl( be, f->f_dn, DN_ONE_PREFIX, ids );
63 case SLAPD_FILTER_DN_SUBTREE:
64 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
65 rc = bdb_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, ids );
68 case LDAP_FILTER_PRESENT:
69 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
70 rc = presence_candidates( be, range, f->f_desc, ids );
73 case LDAP_FILTER_EQUALITY:
74 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
75 rc = equality_candidates( be, range, f->f_ava, ids );
78 case LDAP_FILTER_APPROX:
79 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
80 rc = approx_candidates( be, range, f->f_ava, ids );
83 case LDAP_FILTER_SUBSTRINGS:
84 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
85 rc = substring_candidates( be, range, f->f_sub, ids );
89 /* no GE index, use pres */
90 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
91 rc = presence_candidates( be, range, f->f_desc, ids );
95 /* no LE index, use pres */
96 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
97 rc = presence_candidates( be, range, f->f_desc, ids );
100 case LDAP_FILTER_NOT:
101 /* no indexing to support NOT filters */
102 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
105 case LDAP_FILTER_AND:
106 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
107 rc = list_candidates( be, range,
108 f->f_and, LDAP_FILTER_AND, ids );
112 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
113 rc = list_candidates( be, range,
114 f->f_or, LDAP_FILTER_OR, ids );
118 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %d\n",
123 BDB_IDL_CPY( ids, range );
126 Debug( LDAP_DEBUG_FILTER,
127 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
129 (long) BDB_IDL_FIRST( ids ),
130 (long) BDB_IDL_LAST( ids ) );
146 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
148 if( ftype == LDAP_FILTER_AND ) {
149 BDB_IDL_CPY( ids, range );
154 for ( f = flist; f != NULL; f = f->f_next ) {
155 ID tmp[BDB_IDL_UM_SIZE];
156 ID result[BDB_IDL_UM_SIZE];
157 rc = bdb_filter_candidates( be, range, f, tmp );
160 /* Error: treat as undefined */
161 if( ftype == LDAP_FILTER_AND ) {
164 BDB_IDL_CPY( ids, range );
169 if ( ftype == LDAP_FILTER_AND ) {
170 bdb_idl_intersection( tmp, ids, result );
171 if( BDB_IDL_IS_ZERO( result ) ) {
176 bdb_idl_union( tmp, ids, result );
177 if( BDB_IDL_IS_ALL( range, result ) ) {
178 BDB_IDL_CPY( ids, range );
183 BDB_IDL_CPY( ids, result );
186 Debug( LDAP_DEBUG_FILTER,
187 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
189 (long) BDB_IDL_FIRST(ids),
190 (long) BDB_IDL_LAST(ids) );
198 AttributeDescription *desc,
204 struct berval prefix = {0};
206 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
209 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
210 &db, &mask, &prefix );
212 if( rc != LDAP_SUCCESS ) {
213 Debug( LDAP_DEBUG_TRACE,
214 "<= bdb_presence_candidates: index_param returned=%d\n",
221 Debug( LDAP_DEBUG_TRACE,
222 "<= bdb_presence_candidates: not indexed\n",
228 if( prefix.bv_val == NULL ) {
229 Debug( LDAP_DEBUG_TRACE,
230 "<= bdb_presence_candidates: no prefix\n",
236 rc = bdb_key_read( be, db, NULL, &prefix, ids );
238 if( rc == DB_NOTFOUND ) {
241 } else if( rc != LDAP_SUCCESS ) {
242 Debug( LDAP_DEBUG_TRACE,
243 "<= bdb_presense_candidates: key read failed (%d)\n",
248 Debug(LDAP_DEBUG_TRACE,
249 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
251 (long) BDB_IDL_FIRST(ids),
252 (long) BDB_IDL_LAST(ids) );
262 AttributeAssertion *ava,
269 struct berval prefix = {0};
270 struct berval **keys = NULL;
273 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
275 BDB_IDL_RANGE_CPY( range, ids );
277 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
278 &db, &mask, &prefix );
280 if( rc != LDAP_SUCCESS ) {
281 Debug( LDAP_DEBUG_ANY,
282 "<= bdb_equality_candidates: index_param failed (%d)\n",
288 Debug( LDAP_DEBUG_ANY,
289 "<= bdb_equality_candidates: not indexed\n", 0, 0, 0 );
293 mr = ava->aa_desc->ad_type->sat_equality;
298 if( !mr->smr_filter ) {
302 rc = (mr->smr_filter)(
303 LDAP_FILTER_EQUALITY,
305 ava->aa_desc->ad_type->sat_syntax,
311 if( rc != LDAP_SUCCESS ) {
312 Debug( LDAP_DEBUG_TRACE,
313 "<= bdb_equality_candidates: MR filter failed (%d)\n",
319 Debug( LDAP_DEBUG_TRACE,
320 "<= bdb_equality_candidates: no keys\n",
325 for ( i= 0; keys[i] != NULL; i++ ) {
326 ID save[BDB_IDL_UM_SIZE];
327 ID tmp[BDB_IDL_UM_SIZE];
329 rc = bdb_key_read( be, db, NULL, keys[i], tmp );
331 if( rc != LDAP_SUCCESS ) {
332 Debug( LDAP_DEBUG_TRACE,
333 "<= bdb_equality_candidates key read failed (%d)\n",
339 Debug( LDAP_DEBUG_TRACE,
340 "<= bdb_equality_candidates NULL\n",
345 memcpy(save, ids, sizeof(save));
346 bdb_idl_intersection( save, tmp, ids );
348 if( ids[0] == 0 ) break;
351 ber_bvecfree( keys );
353 Debug( LDAP_DEBUG_TRACE,
354 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
356 (long) BDB_IDL_FIRST(ids),
357 (long) BDB_IDL_LAST(ids) );
366 AttributeAssertion *ava,
373 struct berval prefix = {0};
374 struct berval **keys = NULL;
377 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates\n", 0, 0, 0 );
379 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
380 &db, &mask, &prefix );
382 if( rc != LDAP_SUCCESS ) {
383 Debug( LDAP_DEBUG_ANY,
384 "<= bdb_approx_candidates: index_param failed (%d)\n",
390 Debug( LDAP_DEBUG_ANY,
391 "<= bdb_approx_candidates: not indexed\n", 0, 0, 0 );
395 mr = ava->aa_desc->ad_type->sat_approx;
397 /* no approx matching rule, try equality matching rule */
398 mr = ava->aa_desc->ad_type->sat_equality;
405 if( !mr->smr_filter ) {
409 rc = (mr->smr_filter)(
412 ava->aa_desc->ad_type->sat_syntax,
418 if( rc != LDAP_SUCCESS ) {
419 Debug( LDAP_DEBUG_TRACE,
420 "<= bdb_approx_candidates: (%s) MR filter failed (%d)\n",
421 prefix.bv_val, rc, 0 );
426 Debug( LDAP_DEBUG_TRACE,
427 "<= bdb_approx_candidates: no keys (%s)\n",
428 prefix.bv_val, 0, 0 );
432 for ( i= 0; keys[i] != NULL; i++ ) {
433 ID save[BDB_IDL_UM_SIZE];
434 ID tmp[BDB_IDL_UM_SIZE];
436 rc = bdb_key_read( be, db, NULL, keys[i], tmp );
438 if( rc != LDAP_SUCCESS ) {
439 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates key read failed (%d)\n",
445 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates NULL\n",
450 memcpy(save, ids, sizeof(save));
451 bdb_idl_intersection( save, tmp, ids );
453 if( ids[0] == 0 ) break;
456 ber_bvecfree( keys );
458 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
460 (long) BDB_IDL_FIRST(ids),
461 (long) BDB_IDL_LAST(ids) );
467 substring_candidates(
470 SubstringsAssertion *sub,
477 struct berval prefix = {0};
478 struct berval **keys = NULL;
481 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates\n", 0, 0, 0 );
483 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
484 &db, &mask, &prefix );
486 if( rc != LDAP_SUCCESS ) {
487 Debug( LDAP_DEBUG_ANY,
488 "<= bdb_substring_candidates: index_param failed (%d)\n",
494 Debug( LDAP_DEBUG_ANY,
495 "<= bdb_substring_candidates not indexed\n",
500 mr = sub->sa_desc->ad_type->sat_substr;
506 if( !mr->smr_filter ) {
510 rc = (mr->smr_filter)(
511 LDAP_FILTER_SUBSTRINGS,
513 sub->sa_desc->ad_type->sat_syntax,
519 if( rc != LDAP_SUCCESS ) {
520 Debug( LDAP_DEBUG_TRACE,
521 "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n",
522 sub->sa_desc->ad_cname.bv_val, rc, 0 );
527 Debug( LDAP_DEBUG_TRACE,
528 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
529 mask, sub->sa_desc->ad_cname.bv_val, 0 );
533 for ( i= 0; keys[i] != NULL; i++ ) {
534 ID save[BDB_IDL_UM_SIZE];
535 ID tmp[BDB_IDL_UM_SIZE];
537 rc = bdb_key_read( be, db, NULL, keys[i], tmp );
539 if( rc != LDAP_SUCCESS ) {
540 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates key read failed (%d)\n",
546 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates NULL\n",
551 memcpy(save, ids, sizeof(save));
552 bdb_idl_intersection( save, tmp, ids );
554 if( ids[0] == 0 ) break;
557 ber_bvecfree( keys );
559 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates %ld, first=%ld, last=%ld\n",
561 (long) BDB_IDL_FIRST(ids),
562 (long) BDB_IDL_LAST(ids) );