1 /* filterindex.c - generate the list of candidate entries from a filter */
11 #include "back-ldbm.h"
13 static IDList *ava_candidates( Backend *be, Ava *ava, int type );
14 static IDList *presence_candidates( Backend *be, char *type );
15 static IDList *approx_candidates( Backend *be, Ava *ava );
16 static IDList *list_candidates( Backend *be, Filter *flist, int ftype );
17 static IDList *substring_candidates( Backend *be, Filter *f );
18 static IDList *substring_comp_candidates( Backend *be, char *type, char *val, int prepost );
21 * test_filter - test a filter against a single entry.
22 * returns 0 filter matched
23 * -1 filter did not match
24 * >0 an ldap error code
35 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
38 switch ( f->f_choice ) {
39 case LDAP_FILTER_EQUALITY:
40 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
41 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
44 case LDAP_FILTER_SUBSTRINGS:
45 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
46 result = substring_candidates( be, f );
50 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
51 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
55 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
56 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
59 case LDAP_FILTER_PRESENT:
60 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
61 result = presence_candidates( be, f->f_type );
64 case LDAP_FILTER_APPROX:
65 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
66 result = approx_candidates( be, &f->f_ava );
70 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
71 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
75 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
76 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
80 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
81 result = idl_notin( be, idl_allids( be ), filter_candidates( be,
86 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %lu\n",
87 result ? result->b_nids : 0, 0, 0 );
100 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
103 case LDAP_FILTER_EQUALITY:
104 idl = index_read( be, ava->ava_type, INDEX_EQUALITY,
105 ava->ava_value.bv_val );
109 idl = idl_allids( be );
113 idl = idl_allids( be );
117 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %lu\n",
118 idl ? idl->b_nids : 0, 0, 0 );
130 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
132 idl = index_read( be, type, 0, "*" );
134 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %lu\n",
135 idl ? idl->b_nids : 0, 0, 0 );
148 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
151 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
152 w = next_word( w ) ) {
154 if ( (tmp = index_read( be, ava->ava_type, INDEX_APPROX, c ))
158 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
167 idl = idl_intersection( be, idl, tmp );
171 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %lu\n",
172 idl ? idl->b_nids : 0, 0, 0 );
183 IDList *idl, *tmp, *tmp2;
186 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
189 for ( f = flist; f != NULL; f = f->f_next ) {
190 if ( (tmp = filter_candidates( be, f )) == NULL &&
191 ftype == LDAP_FILTER_AND ) {
192 Debug( LDAP_DEBUG_TRACE,
193 "<= list_candidates NULL\n", 0, 0, 0 );
201 } else if ( ftype == LDAP_FILTER_AND ) {
202 idl = idl_intersection( be, idl, tmp );
206 idl = idl_union( be, idl, tmp );
212 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %lu\n",
213 idl ? idl->b_nids : 0, 0, 0 );
218 substring_candidates(
224 IDList *idl, *tmp, *tmp2;
226 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
231 if ( f->f_sub_initial != NULL ) {
232 if ( (int) strlen( f->f_sub_initial ) < SUBLEN - 1 ) {
233 idl = idl_allids( be );
234 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
235 f->f_sub_initial, '^' )) == NULL ) {
241 if ( f->f_sub_final != NULL ) {
242 if ( (int) strlen( f->f_sub_final ) < SUBLEN - 1 ) {
243 tmp = idl_allids( be );
244 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
245 f->f_sub_final, '$' )) == NULL ) {
254 idl = idl_intersection( be, idl, tmp );
260 for ( i = 0; f->f_sub_any != NULL && f->f_sub_any[i] != NULL; i++ ) {
261 if ( (int) strlen( f->f_sub_any[i] ) < SUBLEN ) {
262 tmp = idl_allids( be );
263 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
264 f->f_sub_any[i], 0 )) == NULL ) {
273 idl = idl_intersection( be, idl, tmp );
279 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %lu\n",
280 idl ? idl->b_nids : 0, 0, 0 );
285 substring_comp_candidates(
293 IDList *idl, *tmp, *tmp2;
295 char buf[SUBLEN + 1];
297 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
302 /* prepend ^ for initial substring */
303 if ( prepost == '^' ) {
305 for ( i = 0; i < SUBLEN - 1; i++ ) {
310 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
313 } else if ( prepost == '$' ) {
314 p = val + len - SUBLEN + 1;
315 for ( i = 0; i < SUBLEN - 1; i++ ) {
318 buf[SUBLEN - 1] = '$';
321 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
326 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
327 for ( i = 0; i < SUBLEN; i++ ) {
332 if ( (tmp = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
341 idl = idl_intersection( be, idl, tmp );
347 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %lu\n",
348 idl ? idl->b_nids : 0, 0, 0 );