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,
24 struct berval *val, int prepost );
27 * test_filter - test a filter against a single entry.
28 * returns 0 filter matched
29 * -1 filter did not match
30 * >0 an ldap error code
39 ID_BLOCK *result, *tmp1, *tmp2;
41 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
44 switch ( f->f_choice ) {
45 case SLAPD_FILTER_DN_ONE:
46 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
47 result = dn2idl( be, f->f_dn, DN_ONE_PREFIX );
50 case SLAPD_FILTER_DN_SUBTREE:
51 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
52 result = dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX );
55 case LDAP_FILTER_EQUALITY:
56 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
57 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
60 case LDAP_FILTER_SUBSTRINGS:
61 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
62 result = substring_candidates( be, f );
66 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
67 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
71 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
72 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
75 case LDAP_FILTER_PRESENT:
76 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
77 result = presence_candidates( be, f->f_type );
80 case LDAP_FILTER_APPROX:
81 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
82 result = approx_candidates( be, &f->f_ava );
86 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
87 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
91 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
92 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
96 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
97 tmp1 = idl_allids( be );
98 tmp2 = filter_candidates( be, f->f_not );
99 result = idl_notin( be, tmp1, tmp2 );
105 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
106 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
119 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
122 case LDAP_FILTER_EQUALITY:
123 idl = index_read( be, ava->ava_type, SLAP_INDEX_EQUALITY,
124 ava->ava_value.bv_val );
128 idl = idl_allids( be );
132 idl = idl_allids( be );
136 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %ld\n",
137 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
149 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
151 idl = index_read( be, type, SLAP_INDEX_PRESENCE, "*" );
153 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
154 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
167 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
170 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
171 w = next_word( w ) ) {
173 if ( (tmp = index_read( be, ava->ava_type, SLAP_INDEX_APPROX, c ))
177 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
186 idl = idl_intersection( be, idl, tmp );
190 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
191 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
202 ID_BLOCK *idl, *tmp, *tmp2;
205 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
208 for ( f = flist; f != NULL; f = f->f_next ) {
209 if ( (tmp = filter_candidates( be, f )) == NULL &&
210 ftype == LDAP_FILTER_AND ) {
211 Debug( LDAP_DEBUG_TRACE,
212 "<= list_candidates NULL\n", 0, 0, 0 );
220 } else if ( ftype == LDAP_FILTER_AND ) {
221 idl = idl_intersection( be, idl, tmp );
225 idl = idl_union( be, idl, tmp );
231 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
232 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
237 substring_candidates(
243 ID_BLOCK *idl, *tmp, *tmp2;
245 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
250 if ( f->f_sub_initial != NULL ) {
251 if ( f->f_sub_initial->bv_len < SUBLEN - 1 ) {
252 idl = idl_allids( be );
253 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
254 f->f_sub_initial, '^' )) == NULL ) {
260 if ( f->f_sub_final != NULL ) {
261 if ( f->f_sub_final->bv_len < SUBLEN - 1 ) {
262 tmp = idl_allids( be );
263 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
264 f->f_sub_final, '$' )) == NULL ) {
273 idl = idl_intersection( be, idl, tmp );
279 if( f->f_sub_any != NULL ) {
280 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
281 if ( f->f_sub_any[i]->bv_len < SUBLEN ) {
282 tmp = idl_allids( be );
283 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
284 f->f_sub_any[i], 0 )) == NULL ) {
293 idl = idl_intersection( be, idl, tmp );
300 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %ld\n",
301 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
306 substring_comp_candidates(
314 ID_BLOCK *idl, *tmp, *tmp2;
316 char buf[SUBLEN + 1];
319 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
325 /* prepend ^ for initial substring */
326 if ( prepost == '^' ) {
328 for ( i = 0; i < SUBLEN - 1; i++ ) {
333 if ( (idl = index_read( be, type, SLAP_INDEX_SUB, buf )) == NULL ) {
336 } else if ( prepost == '$' ) {
337 p = val + len - SUBLEN + 1;
338 for ( i = 0; i < SUBLEN - 1; i++ ) {
341 buf[SUBLEN - 1] = '$';
344 if ( (idl = index_read( be, type, SLAP_INDEX_SUB, buf )) == NULL ) {
349 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
350 for ( i = 0; i < SUBLEN; i++ ) {
355 if ( (tmp = index_read( be, type, SLAP_INDEX_SUB, buf )) == NULL ) {
364 idl = idl_intersection( be, idl, tmp );
369 /* break if no candidates */
374 /* if we're down to two (or less) matches, stop searching */
375 if( ID_BLOCK_NIDS(idl) < 3 ) {
376 Debug( LDAP_DEBUG_TRACE, "substring_comp_candiates: "
377 "down to a %ld matches, stopped search\n",
378 (long) ID_BLOCK_NIDS(idl), 0, 0 );
383 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %ld\n",
384 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );