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(
20 AttributeDescription *desc,
23 static int equality_candidates(
25 AttributeAssertion *ava,
28 static int approx_candidates(
30 AttributeAssertion *ava,
33 static int substring_candidates(
35 SubstringsAssertion *sub,
39 static int list_candidates(
47 bdb_filter_candidates(
54 Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
56 switch ( f->f_choice ) {
57 case SLAPD_FILTER_DN_ONE:
58 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
59 rc = bdb_dn2idl( be, f->f_dn, DN_ONE_PREFIX, ids );
62 case SLAPD_FILTER_DN_SUBTREE:
63 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
64 rc = bdb_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, ids );
67 case LDAP_FILTER_PRESENT:
68 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
69 rc = presence_candidates( be, f->f_desc, ids );
72 case LDAP_FILTER_EQUALITY:
73 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
74 rc = equality_candidates( be, f->f_ava, ids, tmp );
77 case LDAP_FILTER_APPROX:
78 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
79 rc = approx_candidates( be, f->f_ava, ids, tmp );
82 case LDAP_FILTER_SUBSTRINGS:
83 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
84 rc = substring_candidates( be, f->f_sub, ids, tmp );
88 /* no GE index, use pres */
89 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
90 rc = presence_candidates( be, f->f_desc, ids );
94 /* no LE index, use pres */
95 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
96 rc = presence_candidates( be, f->f_desc, ids );
100 /* no indexing to support NOT filters */
101 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
104 case LDAP_FILTER_AND:
105 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
106 rc = list_candidates( be,
107 f->f_and, LDAP_FILTER_AND, ids, tmp );
111 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
112 rc = list_candidates( be,
113 f->f_or, LDAP_FILTER_OR, ids, tmp );
117 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %d\n",
121 Debug( LDAP_DEBUG_FILTER,
122 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
124 (long) BDB_IDL_FIRST( ids ),
125 (long) BDB_IDL_LAST( ids ) );
138 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
142 /* Systems that can't increase thread stack size will die with these
143 * structures allocated on the stack. */
144 #if !defined(LDAP_PVT_THREAD_STACK_SIZE) || (LDAP_PVT_THREAD_STACK_SIZE == 0)
145 ID *save = ch_malloc(BDB_IDL_UM_SIZEOF);
147 ID save[BDB_IDL_UM_SIZE];
150 Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
152 if ( ftype == LDAP_FILTER_OR ) {
153 BDB_IDL_ALL( bdb, save );
156 BDB_IDL_CPY( save, ids );
159 for ( f = flist; f != NULL; f = f->f_next ) {
160 rc = bdb_filter_candidates( be, f, save, tmp );
163 /* Error: treat as undefined */
167 if ( ftype == LDAP_FILTER_AND ) {
168 bdb_idl_intersection( ids, save );
169 if( BDB_IDL_IS_ZERO( ids ) )
172 bdb_idl_union( ids, save );
173 BDB_IDL_ALL( bdb, save );
176 #if !defined(LDAP_PVT_THREAD_STACK_SIZE) || (LDAP_PVT_THREAD_STACK_SIZE == 0)
180 Debug( LDAP_DEBUG_FILTER,
181 "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
183 (long) BDB_IDL_FIRST(ids),
184 (long) BDB_IDL_LAST(ids) );
191 AttributeDescription *desc,
197 struct berval prefix = {0};
199 Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates\n", 0, 0, 0 );
201 rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
202 &db, &mask, &prefix );
204 if( rc != LDAP_SUCCESS ) {
205 Debug( LDAP_DEBUG_TRACE,
206 "<= bdb_presence_candidates: index_param returned=%d\n",
213 Debug( LDAP_DEBUG_TRACE,
214 "<= bdb_presence_candidates: not indexed\n",
219 if( prefix.bv_val == NULL ) {
220 Debug( LDAP_DEBUG_TRACE,
221 "<= bdb_presence_candidates: no prefix\n",
226 rc = bdb_key_read( be, db, NULL, &prefix, ids );
228 if( rc == DB_NOTFOUND ) {
231 } else if( rc != LDAP_SUCCESS ) {
232 Debug( LDAP_DEBUG_TRACE,
233 "<= bdb_presense_candidates: key read failed (%d)\n",
238 Debug(LDAP_DEBUG_TRACE,
239 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
241 (long) BDB_IDL_FIRST(ids),
242 (long) BDB_IDL_LAST(ids) );
251 AttributeAssertion *ava,
259 struct berval prefix = {0};
260 struct berval **keys = NULL;
263 Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates\n", 0, 0, 0 );
265 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
266 &db, &mask, &prefix );
268 if( rc != LDAP_SUCCESS ) {
269 Debug( LDAP_DEBUG_ANY,
270 "<= bdb_equality_candidates: index_param failed (%d)\n",
276 Debug( LDAP_DEBUG_ANY,
277 "<= bdb_equality_candidates: not indexed\n", 0, 0, 0 );
281 mr = ava->aa_desc->ad_type->sat_equality;
286 if( !mr->smr_filter ) {
290 rc = (mr->smr_filter)(
291 LDAP_FILTER_EQUALITY,
293 ava->aa_desc->ad_type->sat_syntax,
299 if( rc != LDAP_SUCCESS ) {
300 Debug( LDAP_DEBUG_TRACE,
301 "<= bdb_equality_candidates: MR filter failed (%d)\n",
307 Debug( LDAP_DEBUG_TRACE,
308 "<= bdb_equality_candidates: no keys\n",
313 for ( i= 0; keys[i] != NULL; i++ ) {
314 rc = bdb_key_read( be, db, NULL, keys[i], tmp );
316 if( rc != LDAP_SUCCESS ) {
317 Debug( LDAP_DEBUG_TRACE,
318 "<= bdb_equality_candidates key read failed (%d)\n",
323 if( BDB_IDL_IS_ZERO( tmp ) ) {
324 Debug( LDAP_DEBUG_TRACE,
325 "<= bdb_equality_candidates NULL\n",
331 bdb_idl_intersection( ids, tmp );
333 if( BDB_IDL_IS_ZERO( ids ) )
337 ber_bvecfree( keys );
339 Debug( LDAP_DEBUG_TRACE,
340 "<= bdb_equality_candidates id=%ld, first=%ld, last=%ld\n",
342 (long) BDB_IDL_FIRST(ids),
343 (long) BDB_IDL_LAST(ids) );
351 AttributeAssertion *ava,
359 struct berval prefix = {0};
360 struct berval **keys = NULL;
363 Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates\n", 0, 0, 0 );
365 rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
366 &db, &mask, &prefix );
368 if( rc != LDAP_SUCCESS ) {
369 Debug( LDAP_DEBUG_ANY,
370 "<= bdb_approx_candidates: index_param failed (%d)\n",
376 Debug( LDAP_DEBUG_ANY,
377 "<= bdb_approx_candidates: not indexed\n", 0, 0, 0 );
381 mr = ava->aa_desc->ad_type->sat_approx;
383 /* no approx matching rule, try equality matching rule */
384 mr = ava->aa_desc->ad_type->sat_equality;
391 if( !mr->smr_filter ) {
395 rc = (mr->smr_filter)(
398 ava->aa_desc->ad_type->sat_syntax,
404 if( rc != LDAP_SUCCESS ) {
405 Debug( LDAP_DEBUG_TRACE,
406 "<= bdb_approx_candidates: (%s) MR filter failed (%d)\n",
407 prefix.bv_val, rc, 0 );
412 Debug( LDAP_DEBUG_TRACE,
413 "<= bdb_approx_candidates: no keys (%s)\n",
414 prefix.bv_val, 0, 0 );
418 for ( i= 0; keys[i] != NULL; i++ ) {
419 rc = bdb_key_read( be, db, NULL, keys[i], tmp );
421 if( rc != LDAP_SUCCESS ) {
422 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates key read failed (%d)\n",
427 if( BDB_IDL_IS_ZERO( tmp ) ) {
428 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates NULL\n",
434 bdb_idl_intersection( ids, tmp );
436 if( BDB_IDL_IS_ZERO( ids ) )
440 ber_bvecfree( keys );
442 Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
444 (long) BDB_IDL_FIRST(ids),
445 (long) BDB_IDL_LAST(ids) );
451 substring_candidates(
453 SubstringsAssertion *sub,
461 struct berval prefix = {0};
462 struct berval **keys = NULL;
465 Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates\n", 0, 0, 0 );
467 rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
468 &db, &mask, &prefix );
470 if( rc != LDAP_SUCCESS ) {
471 Debug( LDAP_DEBUG_ANY,
472 "<= bdb_substring_candidates: index_param failed (%d)\n",
478 Debug( LDAP_DEBUG_ANY,
479 "<= bdb_substring_candidates not indexed\n",
484 mr = sub->sa_desc->ad_type->sat_substr;
490 if( !mr->smr_filter ) {
494 rc = (mr->smr_filter)(
495 LDAP_FILTER_SUBSTRINGS,
497 sub->sa_desc->ad_type->sat_syntax,
503 if( rc != LDAP_SUCCESS ) {
504 Debug( LDAP_DEBUG_TRACE,
505 "<= bdb_substring_candidates: (%s) MR filter failed (%d)\n",
506 sub->sa_desc->ad_cname.bv_val, rc, 0 );
511 Debug( LDAP_DEBUG_TRACE,
512 "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
513 mask, sub->sa_desc->ad_cname.bv_val, 0 );
517 for ( i= 0; keys[i] != NULL; i++ ) {
518 rc = bdb_key_read( be, db, NULL, keys[i], tmp );
520 if( rc != LDAP_SUCCESS ) {
521 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates key read failed (%d)\n",
526 if( BDB_IDL_IS_ZERO( tmp ) ) {
527 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates NULL\n",
533 bdb_idl_intersection( ids, tmp );
535 if( BDB_IDL_IS_ZERO( ids ) )
539 ber_bvecfree( keys );
541 Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates %ld, first=%ld, last=%ld\n",
543 (long) BDB_IDL_FIRST(ids),
544 (long) BDB_IDL_LAST(ids) );