1 /* filterindex.c - generate the list of candidate entries from a filter */
4 * Copyright 1998-1999 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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
28 static ID_BLOCK *ava_candidates( Backend *be, Ava *ava, int type );
29 static ID_BLOCK *presence_candidates( Backend *be, char *type );
30 static ID_BLOCK *approx_candidates( Backend *be, Ava *ava );
31 static ID_BLOCK *list_candidates( Backend *be, Filter *flist, int ftype );
32 static ID_BLOCK *substring_candidates( Backend *be, Filter *f );
33 static ID_BLOCK *substring_comp_candidates( Backend *be, char *type,
34 struct berval *val, int prepost );
42 ID_BLOCK *result, *tmp1, *tmp2;
44 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
47 switch ( f->f_choice ) {
48 case SLAPD_FILTER_DN_ONE:
49 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
50 result = dn2idl( be, f->f_dn, DN_ONE_PREFIX );
53 case SLAPD_FILTER_DN_SUBTREE:
54 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
55 result = dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX );
58 case LDAP_FILTER_EQUALITY:
59 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
60 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
63 case LDAP_FILTER_SUBSTRINGS:
64 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
65 result = substring_candidates( be, f );
69 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
70 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
74 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
75 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
78 case LDAP_FILTER_PRESENT:
79 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
80 result = presence_candidates( be, f->f_type );
83 case LDAP_FILTER_APPROX:
84 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
85 result = approx_candidates( be, &f->f_ava );
89 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
90 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
94 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
95 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
99 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
100 tmp1 = idl_allids( be );
101 tmp2 = filter_candidates( be, f->f_not );
102 result = idl_notin( be, tmp1, tmp2 );
108 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
109 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
122 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
125 case LDAP_FILTER_EQUALITY:
126 idl = index_read( be, ava->ava_type, SLAP_INDEX_EQUALITY,
127 ava->ava_value.bv_val );
131 idl = idl_allids( be );
135 idl = idl_allids( be );
139 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %ld\n",
140 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
152 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
154 idl = index_read( be, type, SLAP_INDEX_PRESENCE, "*" );
156 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
157 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
170 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
173 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
174 w = next_word( w ) ) {
176 if ( (tmp = index_read( be, ava->ava_type, SLAP_INDEX_APPROX, c ))
180 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
189 idl = idl_intersection( be, idl, tmp );
193 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
194 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
205 ID_BLOCK *idl, *tmp, *tmp2;
208 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
211 for ( f = flist; f != NULL; f = f->f_next ) {
212 if ( (tmp = filter_candidates( be, f )) == NULL &&
213 ftype == LDAP_FILTER_AND ) {
214 Debug( LDAP_DEBUG_TRACE,
215 "<= list_candidates NULL\n", 0, 0, 0 );
223 } else if ( ftype == LDAP_FILTER_AND ) {
224 idl = idl_intersection( be, idl, tmp );
228 idl = idl_union( be, idl, tmp );
234 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
235 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
240 substring_candidates(
246 ID_BLOCK *idl, *tmp, *tmp2;
248 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
253 if ( f->f_sub_initial != NULL ) {
254 if ( f->f_sub_initial->bv_len < SUBLEN - 1 ) {
255 idl = idl_allids( be );
256 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
257 f->f_sub_initial, '^' )) == NULL ) {
263 if ( f->f_sub_final != NULL ) {
264 if ( f->f_sub_final->bv_len < SUBLEN - 1 ) {
265 tmp = idl_allids( be );
266 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
267 f->f_sub_final, '$' )) == NULL ) {
276 idl = idl_intersection( be, idl, tmp );
282 if( f->f_sub_any != NULL ) {
283 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
284 if ( f->f_sub_any[i]->bv_len < SUBLEN ) {
285 tmp = idl_allids( be );
286 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
287 f->f_sub_any[i], 0 )) == NULL ) {
296 idl = idl_intersection( be, idl, tmp );
303 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %ld\n",
304 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
309 substring_comp_candidates(
317 ID_BLOCK *idl, *tmp, *tmp2;
319 char buf[SUBLEN + 1];
322 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
328 /* prepend ^ for initial substring */
329 if ( prepost == '^' ) {
331 for ( i = 0; i < SUBLEN - 1; i++ ) {
336 if ( (idl = index_read( be, type, SLAP_INDEX_SUB, buf )) == NULL ) {
339 } else if ( prepost == '$' ) {
340 p = val + len - SUBLEN + 1;
341 for ( i = 0; i < SUBLEN - 1; i++ ) {
344 buf[SUBLEN - 1] = '$';
347 if ( (idl = index_read( be, type, SLAP_INDEX_SUB, buf )) == NULL ) {
352 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
353 for ( i = 0; i < SUBLEN; i++ ) {
358 if ( (tmp = index_read( be, type, SLAP_INDEX_SUB, buf )) == NULL ) {
367 idl = idl_intersection( be, idl, tmp );
372 /* break if no candidates */
377 /* if we're down to two (or less) matches, stop searching */
378 if( ID_BLOCK_NIDS(idl) < 3 ) {
379 Debug( LDAP_DEBUG_TRACE, "substring_comp_candiates: "
380 "down to a %ld matches, stopped search\n",
381 (long) ID_BLOCK_NIDS(idl), 0, 0 );
386 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %ld\n",
387 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );