1 /* filterindex.c - generate the list of candidate entries from a filter */
12 #include "back-bdb2.h"
14 static ID_BLOCK *ava_candidates( BackendDB *be, Ava *ava, int type );
15 static ID_BLOCK *presence_candidates( BackendDB *be, char *type );
16 static ID_BLOCK *approx_candidates( BackendDB *be, Ava *ava );
17 static ID_BLOCK *list_candidates( BackendDB *be, Filter *flist, int ftype );
18 static ID_BLOCK *substring_candidates( BackendDB *be, Filter *f );
19 static ID_BLOCK *substring_comp_candidates( BackendDB *be, char *type, char *val, int prepost );
22 bdb2i_filter_candidates(
27 ID_BLOCK *result, *tmp1, *tmp2;
29 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_filter_candidates\n", 0, 0, 0 );
32 switch ( f->f_choice ) {
33 case SLAPD_FILTER_DN_ONE:
34 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
35 result = bdb2i_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX );
38 case SLAPD_FILTER_DN_SUBTREE:
39 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
40 result = bdb2i_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX );
43 case LDAP_FILTER_EQUALITY:
44 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
45 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
48 case LDAP_FILTER_SUBSTRINGS:
49 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
50 result = substring_candidates( be, f );
54 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
55 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
59 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
60 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
63 case LDAP_FILTER_PRESENT:
64 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
65 result = presence_candidates( be, f->f_type );
68 case LDAP_FILTER_APPROX:
69 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
70 result = approx_candidates( be, &f->f_ava );
74 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
75 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
79 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
80 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
84 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
85 tmp1 = bdb2i_idl_allids( be );
86 tmp2 = bdb2i_filter_candidates( be, f->f_not );
87 result = bdb2i_idl_notin( be, tmp1, tmp2 );
88 bdb2i_idl_free( tmp2 );
89 bdb2i_idl_free( tmp1 );
93 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_filter_candidates %ld\n",
94 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
107 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
110 case LDAP_FILTER_EQUALITY:
111 idl = bdb2i_index_read( be, ava->ava_type, INDEX_EQUALITY,
112 ava->ava_value.bv_val );
116 idl = bdb2i_idl_allids( be );
120 idl = bdb2i_idl_allids( be );
124 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %ld\n",
125 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
137 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
139 idl = bdb2i_index_read( be, type, INDEX_PRESENCE, "*" );
141 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
142 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
155 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
158 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
159 w = next_word( w ) ) {
161 if ( (tmp = bdb2i_index_read( be, ava->ava_type, INDEX_APPROX, c ))
164 bdb2i_idl_free( idl );
165 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
174 idl = bdb2i_idl_intersection( be, idl, tmp );
178 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
179 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
190 ID_BLOCK *idl, *tmp, *tmp2;
193 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
196 for ( f = flist; f != NULL; f = f->f_next ) {
197 if ( (tmp = bdb2i_filter_candidates( be, f )) == NULL &&
198 ftype == LDAP_FILTER_AND ) {
199 Debug( LDAP_DEBUG_TRACE,
200 "<= list_candidates NULL\n", 0, 0, 0 );
201 bdb2i_idl_free( idl );
208 } else if ( ftype == LDAP_FILTER_AND ) {
209 idl = bdb2i_idl_intersection( be, idl, tmp );
210 bdb2i_idl_free( tmp );
211 bdb2i_idl_free( tmp2 );
213 idl = bdb2i_idl_union( be, idl, tmp );
214 bdb2i_idl_free( tmp );
215 bdb2i_idl_free( tmp2 );
219 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
220 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
225 substring_candidates(
231 ID_BLOCK *idl, *tmp, *tmp2;
233 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
238 if ( f->f_sub_initial != NULL ) {
239 if ( (int) strlen( f->f_sub_initial ) < SUBLEN - 1 ) {
240 idl = bdb2i_idl_allids( be );
241 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
242 f->f_sub_initial, '^' )) == NULL ) {
248 if ( f->f_sub_final != NULL ) {
249 if ( (int) strlen( f->f_sub_final ) < SUBLEN - 1 ) {
250 tmp = bdb2i_idl_allids( be );
251 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
252 f->f_sub_final, '$' )) == NULL ) {
253 bdb2i_idl_free( idl );
261 idl = bdb2i_idl_intersection( be, idl, tmp );
262 bdb2i_idl_free( tmp );
263 bdb2i_idl_free( tmp2 );
267 for ( i = 0; f->f_sub_any != NULL && f->f_sub_any[i] != NULL; i++ ) {
268 if ( (int) strlen( f->f_sub_any[i] ) < SUBLEN ) {
269 tmp = bdb2i_idl_allids( be );
270 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
271 f->f_sub_any[i], 0 )) == NULL ) {
272 bdb2i_idl_free( idl );
280 idl = bdb2i_idl_intersection( be, idl, tmp );
281 bdb2i_idl_free( tmp );
282 bdb2i_idl_free( tmp2 );
286 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %ld\n",
287 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
292 substring_comp_candidates(
300 ID_BLOCK *idl, *tmp, *tmp2;
302 char buf[SUBLEN + 1];
304 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
309 /* prepend ^ for initial substring */
310 if ( prepost == '^' ) {
312 for ( i = 0; i < SUBLEN - 1; i++ ) {
317 if ( (idl = bdb2i_index_read( be, type, INDEX_SUB, buf )) == NULL ) {
320 } else if ( prepost == '$' ) {
321 p = val + len - SUBLEN + 1;
322 for ( i = 0; i < SUBLEN - 1; i++ ) {
325 buf[SUBLEN - 1] = '$';
328 if ( (idl = bdb2i_index_read( be, type, INDEX_SUB, buf )) == NULL ) {
333 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
334 for ( i = 0; i < SUBLEN; i++ ) {
339 if ( (tmp = bdb2i_index_read( be, type, INDEX_SUB, buf )) == NULL ) {
340 bdb2i_idl_free( idl );
348 idl = bdb2i_idl_intersection( be, idl, tmp );
349 bdb2i_idl_free( tmp );
350 bdb2i_idl_free( tmp2 );
353 /* break if no candidates */
358 /* if we're down to two (or less) matches, stop searching */
359 if( ID_BLOCK_NIDS(idl) < 3 ) {
360 Debug( LDAP_DEBUG_TRACE, "substring_comp_candidates: "
361 "down to a %ld matches, stopped search\n",
362 (long) ID_BLOCK_NIDS(idl), 0, 0 );
367 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %ld\n",
368 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );