1 /* filterindex.c - generate the list of candidate entries from a filter */
8 #include <sys/socket.h>
10 #include "back-ldbm.h"
12 extern char *first_word();
13 extern char *next_word();
14 extern char *phonetic();
15 extern IDList *index_read();
16 extern IDList *idl_intersection();
17 extern IDList *idl_union();
18 extern IDList *idl_notin();
19 extern IDList *idl_allids();
21 static IDList *ava_candidates();
22 static IDList *presence_candidates();
23 static IDList *approx_candidates();
24 static IDList *list_candidates();
25 static IDList *substring_candidates();
26 static IDList *substring_comp_candidates();
29 * test_filter - test a filter against a single entry.
30 * returns 0 filter matched
31 * -1 filter did not match
32 * >0 an ldap error code
43 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
46 switch ( f->f_choice ) {
47 case LDAP_FILTER_EQUALITY:
48 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
49 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
52 case LDAP_FILTER_SUBSTRINGS:
53 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
54 result = substring_candidates( be, f );
58 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
59 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
63 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
64 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
67 case LDAP_FILTER_PRESENT:
68 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
69 result = presence_candidates( be, f->f_type );
72 case LDAP_FILTER_APPROX:
73 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
74 result = approx_candidates( be, &f->f_ava );
78 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
79 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
83 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
84 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
88 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
89 result = idl_notin( be, idl_allids( be ), filter_candidates( be,
94 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %d\n",
95 result ? result->b_nids : 0, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
111 case LDAP_FILTER_EQUALITY:
112 idl = index_read( be, ava->ava_type, INDEX_EQUALITY,
113 ava->ava_value.bv_val );
117 idl = idl_allids( be );
121 idl = idl_allids( be );
125 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %d\n",
126 idl ? idl->b_nids : 0, 0, 0 );
138 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
140 idl = index_read( be, type, 0, "*" );
142 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %d\n",
143 idl ? idl->b_nids : 0, 0, 0 );
156 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
159 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
160 w = next_word( w ) ) {
162 if ( (tmp = index_read( be, ava->ava_type, INDEX_APPROX, c ))
166 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
175 idl = idl_intersection( be, idl, tmp );
179 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %d\n",
180 idl ? idl->b_nids : 0, 0, 0 );
191 IDList *idl, *tmp, *tmp2;
194 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
197 for ( f = flist; f != NULL; f = f->f_next ) {
198 if ( (tmp = filter_candidates( be, f )) == NULL &&
199 ftype == LDAP_FILTER_AND ) {
200 Debug( LDAP_DEBUG_TRACE,
201 "<= list_candidates NULL\n", 0, 0, 0 );
209 } else if ( ftype == LDAP_FILTER_AND ) {
210 idl = idl_intersection( be, idl, tmp );
214 idl = idl_union( be, idl, tmp );
220 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %d\n",
221 idl ? idl->b_nids : 0, 0, 0 );
226 substring_candidates(
232 IDList *idl, *tmp, *tmp2;
234 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
239 if ( f->f_sub_initial != NULL ) {
240 if ( (int) strlen( f->f_sub_initial ) < SUBLEN - 1 ) {
241 idl = idl_allids( be );
242 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
243 f->f_sub_initial, '^' )) == NULL ) {
249 if ( f->f_sub_final != NULL ) {
250 if ( (int) strlen( f->f_sub_final ) < SUBLEN - 1 ) {
251 tmp = idl_allids( be );
252 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
253 f->f_sub_final, '$' )) == NULL ) {
262 idl = idl_intersection( be, idl, tmp );
268 for ( i = 0; f->f_sub_any != NULL && f->f_sub_any[i] != NULL; i++ ) {
269 if ( (int) strlen( f->f_sub_any[i] ) < SUBLEN ) {
270 tmp = idl_allids( be );
271 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
272 f->f_sub_any[i], 0 )) == NULL ) {
281 idl = idl_intersection( be, idl, tmp );
287 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %d\n",
288 idl ? idl->b_nids : 0, 0, 0 );
293 substring_comp_candidates(
301 IDList *idl, *tmp, *tmp2;
303 char buf[SUBLEN + 1];
305 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
310 /* prepend ^ for initial substring */
311 if ( prepost == '^' ) {
313 for ( i = 0; i < SUBLEN - 1; i++ ) {
318 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
321 } else if ( prepost == '$' ) {
322 p = val + len - SUBLEN + 1;
323 for ( i = 0; i < SUBLEN - 1; i++ ) {
326 buf[SUBLEN - 1] = '$';
329 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
334 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
335 for ( i = 0; i < SUBLEN; i++ ) {
340 if ( (tmp = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
349 idl = idl_intersection( be, idl, tmp );
355 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %d\n",
356 idl ? idl->b_nids : 0, 0, 0 );