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 );
91 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
96 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
100 case LDAP_FILTER_NOT: {
101 ID tmp[BDB_IDL_UM_SIZE];
103 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
104 rc = bdb_filter_candidates( be, range, f->f_not, tmp );
106 rc = bdb_idl_notin( range, tmp, ids );
110 case LDAP_FILTER_AND:
111 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
112 rc = list_candidates( be, range,
113 f->f_and, LDAP_FILTER_AND, ids );
117 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
118 rc = list_candidates( be, range,
119 f->f_or, LDAP_FILTER_OR, ids );
123 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %d\n",
128 BDB_IDL_CPY( ids, range );
131 Debug( LDAP_DEBUG_FILTER,
132 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
134 (long) BDB_IDL_FIRST( ids ),
135 (long) BDB_IDL_LAST( ids ) );
151 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
153 if( ftype == LDAP_FILTER_AND ) {
154 BDB_IDL_CPY( ids, range );
159 for ( f = flist; f != NULL; f = f->f_next ) {
160 ID tmp[BDB_IDL_UM_SIZE];
161 ID result[BDB_IDL_UM_SIZE];
162 rc = bdb_filter_candidates( be, range, f, tmp );
165 /* Error: treat as undefined */
166 if( ftype == LDAP_FILTER_AND ) {
169 BDB_IDL_CPY( ids, range );
174 if ( ftype == LDAP_FILTER_AND ) {
175 bdb_idl_intersection( tmp, ids, result );
176 if( BDB_IDL_IS_ZERO( result ) ) {
181 bdb_idl_union( tmp, ids, result );
182 if( BDB_IDL_IS_ALL( range, result ) ) {
183 BDB_IDL_CPY( ids, range );
188 BDB_IDL_CPY( ids, result );
191 Debug( LDAP_DEBUG_FILTER,
192 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
194 (long) BDB_IDL_FIRST(ids),
195 (long) BDB_IDL_LAST(ids) );
203 AttributeDescription *desc,
209 struct berval *prefix;
211 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
214 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
215 &db, &mask, &prefix );
217 if( rc != LDAP_SUCCESS ) {
218 Debug( LDAP_DEBUG_TRACE,
219 "<= bdb_presence_candidates: index_param returned=%d\n",
226 Debug( LDAP_DEBUG_TRACE,
227 "<= bdb_presense_candidates: not indexed\n",
233 if( prefix == NULL ) {
234 Debug( LDAP_DEBUG_TRACE,
235 "<= bdb_presense_candidates: no prefix\n",
241 rc = bdb_key_read( be, db, prefix, ids );
243 if( rc == DB_NOTFOUND ) {
246 } else if( rc != LDAP_SUCCESS ) {
247 Debug( LDAP_DEBUG_TRACE,
248 "<= bdb_presense_candidates: key read failed (%d)\n",
253 Debug(LDAP_DEBUG_TRACE,
254 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
256 (long) BDB_IDL_FIRST(ids),
257 (long) BDB_IDL_LAST(ids) );
260 ber_bvfree( prefix );
268 AttributeAssertion *ava,
276 struct berval *prefix;
277 struct berval **keys = NULL;
280 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
282 BDB_IDL_RANGE_CPY( range, ids );
284 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
285 &db, &mask, &prefix );
287 if( rc != LDAP_SUCCESS ) {
288 Debug( LDAP_DEBUG_ANY,
289 "<= equality_candidates: index_param failed (%d)\n",
294 mr = ava->aa_desc->ad_type->sat_equality;
296 ber_bvfree( prefix );
300 if( !mr->smr_filter ) {
301 ber_bvfree( prefix );
305 rc = (mr->smr_filter)(
306 LDAP_FILTER_EQUALITY,
308 ava->aa_desc->ad_type->sat_syntax,
314 ber_bvfree( prefix );
316 if( rc != LDAP_SUCCESS ) {
317 Debug( LDAP_DEBUG_TRACE,
318 "<= bdb_equality_candidates: MR filter failed (%d)\n",
324 Debug( LDAP_DEBUG_TRACE,
325 "<= bdb_equality_candidates: no keys\n",
330 for ( i= 0; keys[i] != NULL; i++ ) {
331 ID save[BDB_IDL_UM_SIZE];
332 ID tmp[BDB_IDL_UM_SIZE];
334 rc = bdb_key_read( be, db, keys[i], tmp );
336 if( rc != LDAP_SUCCESS ) {
337 Debug( LDAP_DEBUG_TRACE,
338 "<= bdb_equality_candidates key read failed (%d)\n",
344 Debug( LDAP_DEBUG_TRACE,
345 "<= bdb_equality_candidates NULL\n",
351 idl = idl_intersection( be, idl, tmp );
355 if( idl == NULL ) break;
358 ber_bvecfree( keys );
360 Debug( LDAP_DEBUG_TRACE,
361 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
363 (long) BDB_IDL_FIRST(ids),
364 (long) BDB_IDL_LAST(ids) );
373 AttributeAssertion *ava,
382 struct berval *prefix;
383 struct berval **keys = NULL;
386 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
388 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
389 &db, &mask, &prefix );
391 if( rc != LDAP_SUCCESS ) {
392 Debug( LDAP_DEBUG_ANY,
393 "<= approx_candidates: index_param failed (%d)\n",
398 mr = ava->aa_desc->ad_type->sat_approx;
400 /* no approx matching rule, try equality matching rule */
401 mr = ava->aa_desc->ad_type->sat_equality;
405 ber_bvfree( prefix );
409 if( !mr->smr_filter ) {
410 ber_bvfree( prefix );
414 rc = (mr->smr_filter)(
417 ava->aa_desc->ad_type->sat_syntax,
423 ber_bvfree( prefix );
425 if( rc != LDAP_SUCCESS ) {
426 Debug( LDAP_DEBUG_TRACE,
427 "<= approx_candidates: (%s) MR filter failed (%d)\n",
433 Debug( LDAP_DEBUG_TRACE,
434 "<= approx_candidates: no keys (%s)\n",
440 Debug( LDAP_DEBUG_ANY,
441 "<= approx_candidates db open failed (%s)\n",
446 for ( i= 0; keys[i] != NULL; i++ ) {
450 rc = bdb_key_read( be, db, keys[i], tmp );
452 if( rc != LDAP_SUCCESS ) {
455 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
463 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
469 idl = idl_intersection( be, idl, tmp );
472 if( idl == NULL ) break;
475 ber_bvecfree( keys );
477 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld, first=%ld, last=%ld\n",
479 (long) BDB_IDL_FIRST(ids),
480 (long) BDB_IDL_LAST(ids) );
486 substring_candidates(
489 SubstringsAssertion *sub,
497 struct berval *prefix;
498 struct berval **keys = NULL;
501 Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
503 idl = idl_allids( be );
505 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
506 &db, &mask, &prefix );
508 if( rc != LDAP_SUCCESS ) {
509 Debug( LDAP_DEBUG_ANY,
510 "<= substrings_candidates: index_param failed (%d)\n",
515 if( dbname == NULL ) {
517 Debug( LDAP_DEBUG_ANY,
518 "<= substrings_candidates: not indexed\n",
520 ber_bvfree( prefix );
524 mr = sub->sa_desc->ad_type->sat_substr;
527 ber_bvfree( prefix );
531 if( !mr->smr_filter ) {
532 ber_bvfree( prefix );
536 rc = (mr->smr_filter)(
537 LDAP_FILTER_SUBSTRINGS,
539 sub->sa_desc->ad_type->sat_syntax,
545 ber_bvfree( prefix );
547 if( rc != LDAP_SUCCESS ) {
548 Debug( LDAP_DEBUG_TRACE,
549 "<= substrings_candidates: (%s) MR filter failed (%d)\n",
555 Debug( LDAP_DEBUG_TRACE,
556 "<= substrings_candidates: (0x%04lx) no keys (%s)\n",
561 rc = bdb_db_cache( be, dbname, &db );
564 Debug( LDAP_DEBUG_ANY,
565 "<= substrings_candidates db open failed (%s)\n",
570 for ( i= 0; keys[i] != NULL; i++ ) {
574 rc = bdb_key_read( be, db, keys[i], &tmp );
576 if( rc != LDAP_SUCCESS ) {
579 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
587 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
593 idl = idl_intersection( be, idl, tmp );
596 if( idl == NULL ) break;
599 ber_bvecfree( keys );
601 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld, first=%ld, last=%ld\n",
603 (long) BDB_IDL_FIRST(ids),
604 (long) BDB_IDL_LAST(ids) );