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
12 #include <ac/socket.h>
13 #include <ac/string.h>
16 #include "back-ldbm.h"
18 static ID_BLOCK *presence_candidates(
20 AttributeDescription *desc );
21 static ID_BLOCK *equality_candidates(
22 Backend *be, AttributeAssertion *ava );
23 static ID_BLOCK *approx_candidates(
24 Backend *be, AttributeAssertion *ava );
25 static ID_BLOCK *substring_candidates(
28 static ID_BLOCK *list_candidates(
39 ID_BLOCK *result, *tmp1, *tmp2;
41 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
44 switch ( f->f_choice ) {
45 case SLAPD_FILTER_DN_ONE:
46 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
47 result = dn2idl( be, f->f_dn, DN_ONE_PREFIX );
50 case SLAPD_FILTER_DN_SUBTREE:
51 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
52 result = dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX );
55 case LDAP_FILTER_PRESENT:
56 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
57 result = presence_candidates( be, f->f_desc );
60 case LDAP_FILTER_EQUALITY:
61 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
62 result = equality_candidates( be, f->f_ava );
65 case LDAP_FILTER_APPROX:
66 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
67 result = approx_candidates( be, f->f_ava );
70 case LDAP_FILTER_SUBSTRINGS:
71 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
72 result = substring_candidates( be, f );
76 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
77 result = idl_allids( be );
81 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
82 result = idl_allids( be );
86 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
87 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
91 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
92 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
96 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
97 tmp1 = idl_allids( be );
98 tmp2 = filter_candidates( be, f->f_not );
99 result = idl_notin( be, tmp1, tmp2 );
105 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
106 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
113 AttributeDescription *desc
121 struct berval *prefix;
123 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
125 idl = idl_allids( be );
127 rc = index_param( be, desc, LDAP_FILTER_PRESENT,
128 &dbname, &mask, &prefix );
130 if( rc != LDAP_SUCCESS ) {
134 if( dbname == NULL ) {
139 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_READER );
142 Debug( LDAP_DEBUG_ANY,
143 "<= presense_candidates db open failed (%s%s)\n",
144 dbname, LDBM_SUFFIX, 0 );
148 if( prefix != NULL ) {
152 rc = key_read( be, db, prefix, &idl );
154 if( rc != LDAP_SUCCESS ) {
155 Debug( LDAP_DEBUG_TRACE, "<= presense_candidates key read failed (%d)\n",
158 } else if( idl == NULL ) {
159 Debug( LDAP_DEBUG_TRACE, "<= presense_candidates NULL\n",
164 ldbm_cache_close( be, db );
167 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
168 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
175 AttributeAssertion *ava
184 struct berval *prefix;
185 struct berval **keys = NULL;
188 Debug( LDAP_DEBUG_TRACE, "=> equality_candidates\n", 0, 0, 0 );
190 idl = idl_allids( be );
192 rc = index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
193 &dbname, &mask, &prefix );
195 if( rc != LDAP_SUCCESS ) {
199 if( dbname == NULL ) {
204 mr = ava->aa_desc->ad_type->sat_equality;
206 /* return LDAP_INAPPROPRIATE_MATCHING; */
210 if( !mr->smr_filter ) {
214 rc = (mr->smr_filter)(
215 LDAP_FILTER_EQUALITY,
216 ava->aa_desc->ad_type->sat_syntax,
222 if( rc != LDAP_SUCCESS ) {
226 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_READER );
229 Debug( LDAP_DEBUG_ANY,
230 "<= equality_candidates db open failed (%s%s)\n",
231 dbname, LDBM_SUFFIX, 0 );
235 for ( i= 0; keys[i] != NULL; i++ ) {
239 rc = key_read( be, db, keys[i], &tmp );
241 if( rc != LDAP_SUCCESS ) {
244 Debug( LDAP_DEBUG_TRACE, "<= equality_candidates key read failed (%d)\n",
252 Debug( LDAP_DEBUG_TRACE, "<= equality_candidates NULL\n",
258 idl = idl_intersection( be, idl, tmp );
261 if( idl == NULL ) break;
264 ber_bvecfree( keys );
266 ldbm_cache_close( be, db );
269 Debug( LDAP_DEBUG_TRACE, "<= equality_candidates %ld\n",
270 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
277 AttributeAssertion *ava
286 struct berval *prefix;
287 struct berval **keys = NULL;
290 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
292 idl = idl_allids( be );
294 rc = index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
295 &dbname, &mask, &prefix );
297 if( rc != LDAP_SUCCESS ) {
301 if( dbname == NULL ) {
306 mr = ava->aa_desc->ad_type->sat_approx;
308 /* no approx matching rule, try equality matching rule */
309 mr = ava->aa_desc->ad_type->sat_equality;
313 /* return LDAP_INAPPROPRIATE_MATCHING; */
317 if( !mr->smr_filter ) {
321 rc = (mr->smr_filter)(
322 LDAP_FILTER_EQUALITY,
323 ava->aa_desc->ad_type->sat_syntax,
329 if( rc != LDAP_SUCCESS ) {
333 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_READER );
336 Debug( LDAP_DEBUG_ANY,
337 "<= approx_candidates db open failed (%s%s)\n",
338 dbname, LDBM_SUFFIX, 0 );
342 for ( i= 0; keys[i] != NULL; i++ ) {
346 rc = key_read( be, db, keys[i], &tmp );
348 if( rc != LDAP_SUCCESS ) {
351 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
359 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
365 idl = idl_intersection( be, idl, tmp );
368 if( idl == NULL ) break;
371 ber_bvecfree( keys );
373 ldbm_cache_close( be, db );
375 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
376 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
387 ID_BLOCK *idl, *tmp, *tmp2;
390 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
393 for ( f = flist; f != NULL; f = f->f_next ) {
394 if ( (tmp = filter_candidates( be, f )) == NULL &&
395 ftype == LDAP_FILTER_AND ) {
396 Debug( LDAP_DEBUG_TRACE,
397 "<= list_candidates NULL\n", 0, 0, 0 );
405 } else if ( ftype == LDAP_FILTER_AND ) {
406 idl = idl_intersection( be, idl, tmp );
410 idl = idl_union( be, idl, tmp );
416 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
417 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
422 substring_candidates(
429 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
431 idl = idl_allids( be );
432 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %ld\n",
433 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );