1 /* filterindex.c - generate the list of candidate entries from a filter */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
12 #include <ac/string.h>
15 #include "back-ldbm.h"
17 static ID_BLOCK *ava_candidates( Backend *be, Ava *ava, int type );
18 static ID_BLOCK *presence_candidates( Backend *be, char *type );
19 static ID_BLOCK *approx_candidates( Backend *be, Ava *ava );
20 static ID_BLOCK *list_candidates( Backend *be, Filter *flist, int ftype );
21 static ID_BLOCK *substring_candidates( Backend *be, Filter *f );
22 static ID_BLOCK *substring_comp_candidates( Backend *be, char *type, char *val, int prepost );
25 * test_filter - test a filter against a single entry.
26 * returns 0 filter matched
27 * -1 filter did not match
28 * >0 an ldap error code
37 ID_BLOCK *result, *tmp1, *tmp2;
39 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
42 switch ( f->f_choice ) {
43 case SLAPD_FILTER_DN_ONE:
44 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
45 result = dn2idl( be, f->f_dn, DN_ONE_PREFIX );
48 case SLAPD_FILTER_DN_SUBTREE:
49 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
50 result = dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX );
53 case LDAP_FILTER_EQUALITY:
54 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
55 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
58 case LDAP_FILTER_SUBSTRINGS:
59 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
60 result = substring_candidates( be, f );
64 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
65 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
69 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
70 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
73 case LDAP_FILTER_PRESENT:
74 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
75 result = presence_candidates( be, f->f_type );
78 case LDAP_FILTER_APPROX:
79 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
80 result = approx_candidates( be, &f->f_ava );
84 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
85 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
89 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
90 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
94 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
95 tmp1 = idl_allids( be );
96 tmp2 = filter_candidates( be, f->f_not );
97 result = idl_notin( be, tmp1, tmp2 );
103 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
104 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
117 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
120 case LDAP_FILTER_EQUALITY:
121 idl = index_read( be, ava->ava_type, INDEX_EQUALITY,
122 ava->ava_value.bv_val );
126 idl = idl_allids( be );
130 idl = idl_allids( be );
134 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %ld\n",
135 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
147 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
149 idl = index_read( be, type, 0, "*" );
151 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
152 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
165 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
168 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
169 w = next_word( w ) ) {
171 if ( (tmp = index_read( be, ava->ava_type, INDEX_APPROX, c ))
175 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
184 idl = idl_intersection( be, idl, tmp );
188 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
189 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
200 ID_BLOCK *idl, *tmp, *tmp2;
203 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
206 for ( f = flist; f != NULL; f = f->f_next ) {
207 if ( (tmp = filter_candidates( be, f )) == NULL &&
208 ftype == LDAP_FILTER_AND ) {
209 Debug( LDAP_DEBUG_TRACE,
210 "<= list_candidates NULL\n", 0, 0, 0 );
218 } else if ( ftype == LDAP_FILTER_AND ) {
219 idl = idl_intersection( be, idl, tmp );
223 idl = idl_union( be, idl, tmp );
229 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
230 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
235 substring_candidates(
241 ID_BLOCK *idl, *tmp, *tmp2;
243 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
248 if ( f->f_sub_initial != NULL ) {
249 if ( (int) strlen( f->f_sub_initial ) < SUBLEN - 1 ) {
250 idl = idl_allids( be );
251 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
252 f->f_sub_initial, '^' )) == NULL ) {
258 if ( f->f_sub_final != NULL ) {
259 if ( (int) strlen( f->f_sub_final ) < SUBLEN - 1 ) {
260 tmp = idl_allids( be );
261 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
262 f->f_sub_final, '$' )) == NULL ) {
271 idl = idl_intersection( be, idl, tmp );
277 for ( i = 0; f->f_sub_any != NULL && f->f_sub_any[i] != NULL; i++ ) {
278 if ( (int) strlen( f->f_sub_any[i] ) < SUBLEN ) {
279 tmp = idl_allids( be );
280 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
281 f->f_sub_any[i], 0 )) == NULL ) {
290 idl = idl_intersection( be, idl, tmp );
296 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %ld\n",
297 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
302 substring_comp_candidates(
310 ID_BLOCK *idl, *tmp, *tmp2;
312 char buf[SUBLEN + 1];
314 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
319 /* prepend ^ for initial substring */
320 if ( prepost == '^' ) {
322 for ( i = 0; i < SUBLEN - 1; i++ ) {
327 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
330 } else if ( prepost == '$' ) {
331 p = val + len - SUBLEN + 1;
332 for ( i = 0; i < SUBLEN - 1; i++ ) {
335 buf[SUBLEN - 1] = '$';
338 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
343 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
344 for ( i = 0; i < SUBLEN; i++ ) {
349 if ( (tmp = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
358 idl = idl_intersection( be, idl, tmp );
363 /* break if no candidates */
369 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %ld\n",
370 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );