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 );
74 case LDAP_FILTER_EQUALITY:
75 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
76 rc = equality_candidates( be, range, f->f_ava, ids );
79 case LDAP_FILTER_APPROX:
80 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
81 rc = approx_candidates( be, range, f->f_ava, ids );
84 case LDAP_FILTER_SUBSTRINGS:
85 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
86 rc = substring_candidates( be, range, f->f_sub, ids );
90 /* no GE index, use pres */
91 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
92 rc = presence_candidates( be, range, f->f_desc, ids );
96 /* no LE index, use pres */
97 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
98 rc = presence_candidates( be, range, f->f_desc, ids );
102 case LDAP_FILTER_NOT:
103 /* no indexing to support NOT filters */
104 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
107 case LDAP_FILTER_AND:
108 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
109 rc = list_candidates( be, range,
110 f->f_and, LDAP_FILTER_AND, ids );
114 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
115 rc = list_candidates( be, range,
116 f->f_or, LDAP_FILTER_OR, ids );
120 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %d\n",
125 BDB_IDL_CPY( ids, range );
128 Debug( LDAP_DEBUG_FILTER,
129 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
131 (long) BDB_IDL_FIRST( ids ),
132 (long) BDB_IDL_LAST( ids ) );
148 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
150 if( ftype == LDAP_FILTER_AND ) {
151 BDB_IDL_CPY( ids, range );
156 for ( f = flist; f != NULL; f = f->f_next ) {
157 ID tmp[BDB_IDL_UM_SIZE];
158 ID result[BDB_IDL_UM_SIZE];
159 rc = bdb_filter_candidates( be, range, f, tmp );
162 /* Error: treat as undefined */
163 if( ftype == LDAP_FILTER_AND ) {
166 BDB_IDL_CPY( ids, range );
171 if ( ftype == LDAP_FILTER_AND ) {
172 bdb_idl_intersection( tmp, ids, result );
173 if( BDB_IDL_IS_ZERO( result ) ) {
178 bdb_idl_union( tmp, ids, result );
179 if( BDB_IDL_IS_ALL( range, result ) ) {
180 BDB_IDL_CPY( ids, range );
185 BDB_IDL_CPY( ids, result );
188 Debug( LDAP_DEBUG_FILTER,
189 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
191 (long) BDB_IDL_FIRST(ids),
192 (long) BDB_IDL_LAST(ids) );
200 AttributeDescription *desc,
206 struct berval *prefix;
208 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
211 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
212 &db, &mask, &prefix );
214 if( rc != LDAP_SUCCESS ) {
215 Debug( LDAP_DEBUG_TRACE,
216 "<= bdb_presence_candidates: index_param returned=%d\n",
223 Debug( LDAP_DEBUG_TRACE,
224 "<= bdb_presense_candidates: not indexed\n",
230 if( prefix == NULL ) {
231 Debug( LDAP_DEBUG_TRACE,
232 "<= bdb_presense_candidates: no prefix\n",
238 rc = bdb_key_read( be, db, prefix, ids );
240 if( rc == DB_NOTFOUND ) {
243 } else if( rc != LDAP_SUCCESS ) {
244 Debug( LDAP_DEBUG_TRACE,
245 "<= bdb_presense_candidates: key read failed (%d)\n",
250 Debug(LDAP_DEBUG_TRACE,
251 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
253 (long) BDB_IDL_FIRST(ids),
254 (long) BDB_IDL_LAST(ids) );
257 ber_bvfree( prefix );
265 AttributeAssertion *ava,
273 struct berval *prefix;
274 struct berval **keys = NULL;
277 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
279 BDB_IDL_RANGE_CPY( range, ids );
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 "<= equality_candidates: index_param failed (%d)\n",
291 mr = ava->aa_desc->ad_type->sat_equality;
293 ber_bvfree( prefix );
297 if( !mr->smr_filter ) {
298 ber_bvfree( prefix );
302 rc = (mr->smr_filter)(
303 LDAP_FILTER_EQUALITY,
305 ava->aa_desc->ad_type->sat_syntax,
311 ber_bvfree( prefix );
313 if( rc != LDAP_SUCCESS ) {
314 Debug( LDAP_DEBUG_TRACE,
315 "<= bdb_equality_candidates: MR filter failed (%d)\n",
321 Debug( LDAP_DEBUG_TRACE,
322 "<= bdb_equality_candidates: no keys\n",
327 for ( i= 0; keys[i] != NULL; i++ ) {
328 ID save[BDB_IDL_UM_SIZE];
329 ID tmp[BDB_IDL_UM_SIZE];
331 rc = bdb_key_read( be, db, keys[i], tmp );
333 if( rc != LDAP_SUCCESS ) {
334 Debug( LDAP_DEBUG_TRACE,
335 "<= bdb_equality_candidates key read failed (%d)\n",
341 Debug( LDAP_DEBUG_TRACE,
342 "<= bdb_equality_candidates NULL\n",
348 idl = idl_intersection( be, idl, tmp );
352 if( idl == NULL ) break;
355 ber_bvecfree( keys );
357 Debug( LDAP_DEBUG_TRACE,
358 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
360 (long) BDB_IDL_FIRST(ids),
361 (long) BDB_IDL_LAST(ids) );
370 AttributeAssertion *ava,
379 struct berval *prefix;
380 struct berval **keys = NULL;
383 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
385 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
386 &db, &mask, &prefix );
388 if( rc != LDAP_SUCCESS ) {
389 Debug( LDAP_DEBUG_ANY,
390 "<= approx_candidates: index_param failed (%d)\n",
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;
402 ber_bvfree( prefix );
406 if( !mr->smr_filter ) {
407 ber_bvfree( prefix );
411 rc = (mr->smr_filter)(
414 ava->aa_desc->ad_type->sat_syntax,
420 ber_bvfree( prefix );
422 if( rc != LDAP_SUCCESS ) {
423 Debug( LDAP_DEBUG_TRACE,
424 "<= approx_candidates: (%s) MR filter failed (%d)\n",
430 Debug( LDAP_DEBUG_TRACE,
431 "<= approx_candidates: no keys (%s)\n",
437 Debug( LDAP_DEBUG_ANY,
438 "<= approx_candidates db open failed (%s)\n",
443 for ( i= 0; keys[i] != NULL; i++ ) {
447 rc = bdb_key_read( be, db, keys[i], tmp );
449 if( rc != LDAP_SUCCESS ) {
452 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
460 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
466 idl = idl_intersection( be, idl, tmp );
469 if( idl == NULL ) break;
472 ber_bvecfree( keys );
474 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld, first=%ld, last=%ld\n",
476 (long) BDB_IDL_FIRST(ids),
477 (long) BDB_IDL_LAST(ids) );
483 substring_candidates(
486 SubstringsAssertion *sub,
494 struct berval *prefix;
495 struct berval **keys = NULL;
498 Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
500 idl = idl_allids( be );
502 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
503 &db, &mask, &prefix );
505 if( rc != LDAP_SUCCESS ) {
506 Debug( LDAP_DEBUG_ANY,
507 "<= substrings_candidates: index_param failed (%d)\n",
512 if( dbname == NULL ) {
514 Debug( LDAP_DEBUG_ANY,
515 "<= substrings_candidates: not indexed\n",
517 ber_bvfree( prefix );
521 mr = sub->sa_desc->ad_type->sat_substr;
524 ber_bvfree( prefix );
528 if( !mr->smr_filter ) {
529 ber_bvfree( prefix );
533 rc = (mr->smr_filter)(
534 LDAP_FILTER_SUBSTRINGS,
536 sub->sa_desc->ad_type->sat_syntax,
542 ber_bvfree( prefix );
544 if( rc != LDAP_SUCCESS ) {
545 Debug( LDAP_DEBUG_TRACE,
546 "<= substrings_candidates: (%s) MR filter failed (%d)\n",
552 Debug( LDAP_DEBUG_TRACE,
553 "<= substrings_candidates: (0x%04lx) no keys (%s)\n",
558 rc = bdb_db_cache( be, dbname, &db );
561 Debug( LDAP_DEBUG_ANY,
562 "<= substrings_candidates db open failed (%s)\n",
567 for ( i= 0; keys[i] != NULL; i++ ) {
571 rc = bdb_key_read( be, db, keys[i], &tmp );
573 if( rc != LDAP_SUCCESS ) {
576 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
584 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
590 idl = idl_intersection( be, idl, tmp );
593 if( idl == NULL ) break;
596 ber_bvecfree( keys );
598 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld, first=%ld, last=%ld\n",
600 (long) BDB_IDL_FIRST(ids),
601 (long) BDB_IDL_LAST(ids) );