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 static ID_BLOCK *ava_candidates( Backend *be, Ava *ava, int type );
19 static ID_BLOCK *presence_candidates( Backend *be, char *type );
20 static ID_BLOCK *approx_candidates( Backend *be, Ava *ava );
21 static ID_BLOCK *list_candidates( Backend *be, Filter *flist, int ftype );
22 static ID_BLOCK *substring_candidates( Backend *be, Filter *f );
23 static ID_BLOCK *substring_comp_candidates( Backend *be, char *type, char *val, int prepost );
26 * test_filter - test a filter against a single entry.
27 * returns 0 filter matched
28 * -1 filter did not match
29 * >0 an ldap error code
38 ID_BLOCK *result, *tmp1, *tmp2;
40 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
43 switch ( f->f_choice ) {
44 case SLAPD_FILTER_DN_ONE:
45 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
46 result = dn2idl( be, f->f_dn, DN_ONE_PREFIX );
49 case SLAPD_FILTER_DN_SUBTREE:
50 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
51 result = dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX );
54 case LDAP_FILTER_EQUALITY:
55 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
56 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
59 case LDAP_FILTER_SUBSTRINGS:
60 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
61 result = substring_candidates( be, f );
65 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
66 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
70 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
71 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
74 case LDAP_FILTER_PRESENT:
75 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
76 result = presence_candidates( be, f->f_type );
79 case LDAP_FILTER_APPROX:
80 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
81 result = approx_candidates( be, &f->f_ava );
85 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
86 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
90 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
91 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
95 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
96 tmp1 = idl_allids( be );
97 tmp2 = filter_candidates( be, f->f_not );
98 result = idl_notin( be, tmp1, tmp2 );
104 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
105 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
118 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
121 case LDAP_FILTER_EQUALITY:
122 idl = index_read( be, ava->ava_type, INDEX_EQUALITY,
123 ava->ava_value.bv_val );
127 idl = idl_allids( be );
131 idl = idl_allids( be );
135 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %ld\n",
136 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
148 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
150 idl = index_read( be, type, 0, "*" );
152 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
153 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
166 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
169 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
170 w = next_word( w ) ) {
172 if ( (tmp = index_read( be, ava->ava_type, INDEX_APPROX, c ))
176 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
185 idl = idl_intersection( be, idl, tmp );
189 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
190 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
201 ID_BLOCK *idl, *tmp, *tmp2;
204 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
207 for ( f = flist; f != NULL; f = f->f_next ) {
208 if ( (tmp = filter_candidates( be, f )) == NULL &&
209 ftype == LDAP_FILTER_AND ) {
210 Debug( LDAP_DEBUG_TRACE,
211 "<= list_candidates NULL\n", 0, 0, 0 );
219 } else if ( ftype == LDAP_FILTER_AND ) {
220 idl = idl_intersection( be, idl, tmp );
224 idl = idl_union( be, idl, tmp );
230 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
231 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
236 substring_candidates(
242 ID_BLOCK *idl, *tmp, *tmp2;
244 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
249 if ( f->f_sub_initial != NULL ) {
250 if ( (int) strlen( f->f_sub_initial ) < SUBLEN - 1 ) {
251 idl = idl_allids( be );
252 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
253 f->f_sub_initial, '^' )) == NULL ) {
259 if ( f->f_sub_final != NULL ) {
260 if ( (int) strlen( f->f_sub_final ) < SUBLEN - 1 ) {
261 tmp = idl_allids( be );
262 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
263 f->f_sub_final, '$' )) == NULL ) {
272 idl = idl_intersection( be, idl, tmp );
278 for ( i = 0; f->f_sub_any != NULL && f->f_sub_any[i] != NULL; i++ ) {
279 if ( (int) strlen( f->f_sub_any[i] ) < SUBLEN ) {
280 tmp = idl_allids( be );
281 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
282 f->f_sub_any[i], 0 )) == NULL ) {
291 idl = idl_intersection( be, idl, tmp );
297 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %ld\n",
298 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
303 substring_comp_candidates(
311 ID_BLOCK *idl, *tmp, *tmp2;
313 char buf[SUBLEN + 1];
315 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
320 /* prepend ^ for initial substring */
321 if ( prepost == '^' ) {
323 for ( i = 0; i < SUBLEN - 1; i++ ) {
328 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
331 } else if ( prepost == '$' ) {
332 p = val + len - SUBLEN + 1;
333 for ( i = 0; i < SUBLEN - 1; i++ ) {
336 buf[SUBLEN - 1] = '$';
339 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
344 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
345 for ( i = 0; i < SUBLEN; i++ ) {
350 if ( (tmp = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
359 idl = idl_intersection( be, idl, tmp );
364 /* break if no candidates */
370 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %ld\n",
371 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );