1 /* filterindex.c - generate the list of candidate entries from a filter */
11 #include "back-ldbm.h"
13 extern char *first_word();
14 extern char *next_word();
15 extern char *phonetic();
16 extern IDList *index_read();
17 extern IDList *idl_intersection();
18 extern IDList *idl_union();
19 extern IDList *idl_notin();
20 extern IDList *idl_allids();
22 static IDList *ava_candidates();
23 static IDList *presence_candidates();
24 static IDList *approx_candidates();
25 static IDList *list_candidates();
26 static IDList *substring_candidates();
27 static IDList *substring_comp_candidates();
30 * test_filter - test a filter against a single entry.
31 * returns 0 filter matched
32 * -1 filter did not match
33 * >0 an ldap error code
44 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
47 switch ( f->f_choice ) {
48 case LDAP_FILTER_EQUALITY:
49 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
50 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
53 case LDAP_FILTER_SUBSTRINGS:
54 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
55 result = substring_candidates( be, f );
59 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
60 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
64 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
65 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
68 case LDAP_FILTER_PRESENT:
69 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
70 result = presence_candidates( be, f->f_type );
73 case LDAP_FILTER_APPROX:
74 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
75 result = approx_candidates( be, &f->f_ava );
79 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
80 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
84 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
85 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
89 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
90 result = idl_notin( be, idl_allids( be ), filter_candidates( be,
95 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %d\n",
96 result ? result->b_nids : 0, 0, 0 );
109 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
112 case LDAP_FILTER_EQUALITY:
113 idl = index_read( be, ava->ava_type, INDEX_EQUALITY,
114 ava->ava_value.bv_val );
118 idl = idl_allids( be );
122 idl = idl_allids( be );
126 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %d\n",
127 idl ? idl->b_nids : 0, 0, 0 );
139 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
141 idl = index_read( be, type, 0, "*" );
143 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %d\n",
144 idl ? idl->b_nids : 0, 0, 0 );
157 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
160 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
161 w = next_word( w ) ) {
163 if ( (tmp = index_read( be, ava->ava_type, INDEX_APPROX, c ))
167 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
176 idl = idl_intersection( be, idl, tmp );
180 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %d\n",
181 idl ? idl->b_nids : 0, 0, 0 );
192 IDList *idl, *tmp, *tmp2;
195 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
198 for ( f = flist; f != NULL; f = f->f_next ) {
199 if ( (tmp = filter_candidates( be, f )) == NULL &&
200 ftype == LDAP_FILTER_AND ) {
201 Debug( LDAP_DEBUG_TRACE,
202 "<= list_candidates NULL\n", 0, 0, 0 );
210 } else if ( ftype == LDAP_FILTER_AND ) {
211 idl = idl_intersection( be, idl, tmp );
215 idl = idl_union( be, idl, tmp );
221 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %d\n",
222 idl ? idl->b_nids : 0, 0, 0 );
227 substring_candidates(
233 IDList *idl, *tmp, *tmp2;
235 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
240 if ( f->f_sub_initial != NULL ) {
241 if ( (int) strlen( f->f_sub_initial ) < SUBLEN - 1 ) {
242 idl = idl_allids( be );
243 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
244 f->f_sub_initial, '^' )) == NULL ) {
250 if ( f->f_sub_final != NULL ) {
251 if ( (int) strlen( f->f_sub_final ) < SUBLEN - 1 ) {
252 tmp = idl_allids( be );
253 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
254 f->f_sub_final, '$' )) == NULL ) {
263 idl = idl_intersection( be, idl, tmp );
269 for ( i = 0; f->f_sub_any != NULL && f->f_sub_any[i] != NULL; i++ ) {
270 if ( (int) strlen( f->f_sub_any[i] ) < SUBLEN ) {
271 tmp = idl_allids( be );
272 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
273 f->f_sub_any[i], 0 )) == NULL ) {
282 idl = idl_intersection( be, idl, tmp );
288 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %d\n",
289 idl ? idl->b_nids : 0, 0, 0 );
294 substring_comp_candidates(
302 IDList *idl, *tmp, *tmp2;
304 char buf[SUBLEN + 1];
306 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
311 /* prepend ^ for initial substring */
312 if ( prepost == '^' ) {
314 for ( i = 0; i < SUBLEN - 1; i++ ) {
319 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
322 } else if ( prepost == '$' ) {
323 p = val + len - SUBLEN + 1;
324 for ( i = 0; i < SUBLEN - 1; i++ ) {
327 buf[SUBLEN - 1] = '$';
330 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
335 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
336 for ( i = 0; i < SUBLEN; i++ ) {
341 if ( (tmp = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
350 idl = idl_intersection( be, idl, tmp );
356 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %d\n",
357 idl ? idl->b_nids : 0, 0, 0 );