]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/filterindex.c
Fix prev commit
[openldap] / servers / slapd / back-ldbm / filterindex.c
1 /* filterindex.c - generate the list of candidate entries from a filter */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/socket.h>
22 #include <ac/string.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26
27 static ID_BLOCK *presence_candidates(
28         Operation *op,
29         AttributeDescription *desc );
30 static ID_BLOCK *equality_candidates(
31         Operation *op, AttributeAssertion *ava );
32 static ID_BLOCK *approx_candidates(
33         Operation *op, AttributeAssertion *ava );
34 static ID_BLOCK *substring_candidates(
35         Operation *op,
36         SubstringsAssertion *sub );
37 static ID_BLOCK *list_candidates(
38         Operation *op,
39         Filter *flist,
40         int ftype );
41
42 ID_BLOCK *
43 filter_candidates(
44     Operation   *op,
45     Filter      *f
46 )
47 {
48         char *sub = "SUBTREE";
49         ID_BLOCK        *result;
50
51 #ifdef NEW_LOGGING
52         LDAP_LOG( FILTER, ENTRY, "filter_candidates: enter\n", 0, 0, 0 );
53 #else
54         Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
55 #endif
56
57
58         result = NULL;
59         switch ( f->f_choice ) {
60         case SLAPD_FILTER_COMPUTED:
61                 switch( f->f_result ) {
62                 case SLAPD_COMPARE_UNDEFINED:
63                 /* This technically is not the same as FALSE, but it
64                  * certainly will produce no matches.
65                  */
66                 /* FALLTHRU */
67                 case LDAP_COMPARE_FALSE:
68                         result = NULL;
69                         break;
70                 case LDAP_COMPARE_TRUE:
71                         result = idl_allids( op->o_bd );
72                         break;
73                 }
74                 break;
75
76         case SLAPD_FILTER_DN_ONE:
77 #ifdef NEW_LOGGING
78                 LDAP_LOG( FILTER, DETAIL1, 
79                            "filter_candidates:  DN ONE (%s)\n", f->f_dn, 0, 0 );
80 #else
81                 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
82 #endif
83
84                 /* an error is treated as an empty list */
85                 if ( dn2idl( op->o_bd, f->f_dn, DN_ONE_PREFIX, &result ) != 0
86                                 && result != NULL ) {
87                         idl_free( result );
88                         result = NULL;
89                 }
90                 break;
91
92         case SLAPD_FILTER_DN_CHILDREN:
93                 sub = "CHILDREN";
94         case SLAPD_FILTER_DN_SUBTREE:
95 #ifdef NEW_LOGGING
96                 LDAP_LOG( FILTER, DETAIL1, 
97                    "filter_candidates:  DN %s (%s)\n", sub, f->f_dn, 0 );
98 #else
99                 Debug( LDAP_DEBUG_FILTER,
100                         "\tDN %s\n", sub, 0, 0 );
101 #endif
102
103                 /* an error is treated as an empty list */
104                 if ( dn2idl( op->o_bd, f->f_dn, DN_SUBTREE_PREFIX, &result ) != 0
105                                 && result != NULL ) {
106                         idl_free( result );
107                         result = NULL;
108                 }
109                 break;
110
111         case LDAP_FILTER_PRESENT:
112 #ifdef NEW_LOGGING
113                 LDAP_LOG( FILTER, DETAIL1, 
114                         "filter_candidates:  Present (%s)\n", 
115                         f->f_desc->ad_cname.bv_val, 0, 0 );
116 #else
117                 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
118 #endif
119
120                 result = presence_candidates( op, f->f_desc );
121                 break;
122
123         case LDAP_FILTER_EQUALITY:
124 #ifdef NEW_LOGGING
125                 LDAP_LOG( FILTER, DETAIL1, 
126                            "filter_candidates:  EQUALITY (%s),(%s)\n",
127                            f->f_ava->aa_desc->ad_cname.bv_val,
128                            f->f_ava->aa_value.bv_val, 0 );
129 #else
130                 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
131 #endif
132
133                 result = equality_candidates( op, f->f_ava );
134                 break;
135
136         case LDAP_FILTER_APPROX:
137 #ifdef NEW_LOGGING
138                 LDAP_LOG( FILTER, DETAIL1, 
139                            "filter_candidates:  APPROX (%s), (%s)\n",
140                            f->f_ava->aa_desc->ad_cname.bv_val,
141                            f->f_ava->aa_value.bv_val, 0 );
142 #else
143                 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
144 #endif
145
146                 result = approx_candidates( op, f->f_ava );
147                 break;
148
149         case LDAP_FILTER_SUBSTRINGS:
150 #ifdef NEW_LOGGING
151                 LDAP_LOG( FILTER, DETAIL1,
152                            "filter_candidates:  SUBSTRINGS\n", 0, 0, 0 );
153 #else
154                 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
155 #endif
156
157                 result = substring_candidates( op, f->f_sub );
158                 break;
159
160         case LDAP_FILTER_GE:
161 #ifdef NEW_LOGGING
162                 LDAP_LOG( FILTER, DETAIL1, "filter_candidates:  GE\n", 0, 0, 0 );
163 #else
164                 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
165 #endif
166
167                 result = presence_candidates( op, f->f_ava->aa_desc );
168                 break;
169
170         case LDAP_FILTER_LE:
171 #ifdef NEW_LOGGING
172                 LDAP_LOG( FILTER, DETAIL1, "filter_candidates:  LE\n", 0, 0, 0 );
173 #else
174                 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
175 #endif
176
177                 result = presence_candidates( op, f->f_ava->aa_desc );
178                 break;
179
180         case LDAP_FILTER_AND:
181 #ifdef NEW_LOGGING
182                 LDAP_LOG( FILTER, DETAIL1, "filter_candidates:  AND\n", 0, 0, 0 );
183 #else
184                 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
185 #endif
186
187                 result = list_candidates( op, f->f_and, LDAP_FILTER_AND );
188                 break;
189
190         case LDAP_FILTER_OR:
191 #ifdef NEW_LOGGING
192                 LDAP_LOG( FILTER, DETAIL1, "filter_candidates:  OR\n", 0, 0, 0 );
193 #else
194                 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
195 #endif
196
197                 result = list_candidates( op, f->f_or, LDAP_FILTER_OR );
198                 break;
199
200         case LDAP_FILTER_NOT:
201 #ifdef NEW_LOGGING
202                 LDAP_LOG( FILTER, DETAIL1, "filter_candidates:  NOT\n", 0, 0, 0 );
203 #else
204                 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
205 #endif
206
207                 /*
208                  * As candidates lists may contain entries which do
209                  * not match the assertion, negation of the inner candidate
210                  * list could result in matching entries be excluded from
211                  * the returned candidate list.
212                  */
213                 result = idl_allids( op->o_bd );
214                 break;
215         default:
216 #ifdef NEW_LOGGING
217                 LDAP_LOG( FILTER, DETAIL1, "filter_candidates:  UNKNOWN\n", 0, 0, 0 );
218 #else
219                 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN\n", 0, 0, 0 );
220 #endif
221                 /* unknown filters must not return NULL, to allow
222                  * extended filter processing to be done later.
223                  */
224                 result = idl_allids( op->o_bd );
225                 break;
226         }
227
228 #ifdef NEW_LOGGING
229         LDAP_LOG( FILTER, ENTRY, 
230                 "filter_candidates: return %ld\n", 
231                 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
232 #else
233         Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
234             result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
235 #endif
236
237         return( result );
238 }
239
240 static ID_BLOCK *
241 presence_candidates(
242     Operation *op,
243         AttributeDescription *desc
244 )
245 {
246         ID_BLOCK        *idl;
247         DBCache *db;
248         int rc;
249         char *dbname;
250         slap_mask_t mask;
251         struct berval prefix = {0, NULL};
252
253 #ifdef NEW_LOGGING
254         LDAP_LOG( FILTER, ENTRY, "presence_candidates: enter\n", 0, 0, 0 );
255 #else
256         Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
257 #endif
258
259         idl = idl_allids( op->o_bd );
260
261         if( desc == slap_schema.si_ad_objectClass ) {
262                 return idl;
263         }
264
265         rc = index_param( op->o_bd, desc, LDAP_FILTER_PRESENT,
266                 &dbname, &mask, &prefix );
267
268         if( rc != LDAP_SUCCESS ) {
269 #ifdef NEW_LOGGING
270                 LDAP_LOG( FILTER, INFO, 
271                            "presence_candidates: index_param returned %d\n", rc, 0, 0 );
272 #else
273                 Debug( LDAP_DEBUG_TRACE,
274                     "<= presence_candidates: index_param returned=%d\n",
275                         rc, 0, 0 );
276 #endif
277
278                 return idl;
279         }
280
281         if( dbname == NULL ) {
282                 /* not indexed */
283 #ifdef NEW_LOGGING
284                 LDAP_LOG( FILTER, INFO, "presence_candidates: not indexed\n", 0, 0, 0 );
285 #else
286                 Debug( LDAP_DEBUG_TRACE,
287                     "<= presense_candidates: not indexed\n",
288                         0, 0, 0 );
289 #endif
290
291                 return idl;
292         }
293
294         db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
295         
296         if ( db == NULL ) {
297 #ifdef NEW_LOGGING
298                 LDAP_LOG( FILTER, INFO, 
299                            "presence_candidates: db open failed (%s%s)\n",
300                            dbname, LDBM_SUFFIX, 0 );
301 #else
302                 Debug( LDAP_DEBUG_ANY,
303                     "<= presense_candidates db open failed (%s%s)\n",
304                         dbname, LDBM_SUFFIX, 0 );
305 #endif
306
307                 return idl;
308         }
309
310         if( prefix.bv_val != NULL ) {
311                 idl_free( idl );
312                 idl = NULL;
313
314                 rc = key_read( op->o_bd, db, &prefix, &idl );
315
316                 if( rc != LDAP_SUCCESS ) {
317 #ifdef NEW_LOGGING
318                         LDAP_LOG( FILTER, ERR, 
319                                    "presence_candidates: key read failed (%d)\n", rc, 0, 0 );
320 #else
321                         Debug( LDAP_DEBUG_TRACE,
322                                 "<= presense_candidates key read failed (%d)\n",
323                             rc, 0, 0 );
324 #endif
325
326
327                 } else if( idl == NULL ) {
328 #ifdef NEW_LOGGING
329                         LDAP_LOG( FILTER, DETAIL1, "presence_candidates: NULL\n", 0, 0, 0 );
330 #else
331                         Debug( LDAP_DEBUG_TRACE,
332                                 "<= presense_candidates NULL\n",
333                             0, 0, 0 );
334 #endif
335
336                 }
337         }
338
339         ldbm_cache_close( op->o_bd, db );
340
341 #ifdef NEW_LOGGING
342         LDAP_LOG( FILTER, ENTRY, 
343                 "presence_candidates:  return %ld\n", 
344                 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
345 #else
346         Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
347             idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
348 #endif
349
350         return( idl );
351 }
352
353 static ID_BLOCK *
354 equality_candidates(
355     Operation *op,
356         AttributeAssertion *ava
357 )
358 {
359         ID_BLOCK        *idl;
360         DBCache *db;
361         int i;
362         int rc;
363         char *dbname;
364         slap_mask_t mask;
365         struct berval prefix = {0, NULL};
366         struct berval *keys = NULL;
367         MatchingRule *mr;
368
369 #ifdef NEW_LOGGING
370         LDAP_LOG( FILTER, ENTRY, "equality_candidates: enter\n", 0, 0, 0 );
371 #else
372         Debug( LDAP_DEBUG_TRACE, "=> equality_candidates\n", 0, 0, 0 );
373 #endif
374
375
376         idl = idl_allids( op->o_bd );
377
378         rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
379                 &dbname, &mask, &prefix );
380
381         if( rc != LDAP_SUCCESS ) {
382 #ifdef NEW_LOGGING
383                 LDAP_LOG( FILTER, ERR, 
384                            "equality_candidates:  index_param returned %d\n", rc, 0, 0 );
385 #else
386                 Debug( LDAP_DEBUG_TRACE,
387                     "<= equality_candidates: index_param returned=%d\n",
388                         rc, 0, 0 );
389 #endif
390
391                 return idl;
392         }
393
394         if( dbname == NULL ) {
395                 /* not indexed */
396 #ifdef NEW_LOGGING
397                 LDAP_LOG( FILTER, ERR, "equality_candidates: not indexed\n", 0, 0, 0 );
398 #else
399                 Debug( LDAP_DEBUG_TRACE,
400                     "<= equality_candidates: not indexed\n",
401                         0, 0, 0 );
402 #endif
403
404                 return idl;
405         }
406
407         mr = ava->aa_desc->ad_type->sat_equality;
408         if( !mr ) {
409                 return idl;
410         }
411
412         if( !mr->smr_filter ) {
413                 return idl;
414         }
415
416         rc = (mr->smr_filter)(
417                 LDAP_FILTER_EQUALITY,
418                 mask,
419                 ava->aa_desc->ad_type->sat_syntax,
420                 mr,
421                 &prefix,
422                 &ava->aa_value,
423                 &keys, op->o_tmpmemctx );
424
425         if( rc != LDAP_SUCCESS ) {
426 #ifdef NEW_LOGGING
427                 LDAP_LOG( FILTER, ERR, 
428                            "equality_candidates: (%s%s) MR filter failed (%d\n",
429                            dbname, LDBM_SUFFIX, rc );
430 #else
431                 Debug( LDAP_DEBUG_TRACE,
432                     "<= equality_candidates: (%s%s) MR filter failed (%d)\n",
433                         dbname, LDBM_SUFFIX, rc );
434 #endif
435
436                 return idl;
437         }
438
439         if( keys == NULL ) {
440 #ifdef NEW_LOGGING
441                 LDAP_LOG( FILTER, ERR, 
442                    "equality_candidates: no keys (%s%s)\n", dbname, LDBM_SUFFIX, 0 );
443 #else
444                 Debug( LDAP_DEBUG_TRACE,
445                     "<= equality_candidates: no keys (%s%s)\n",
446                         dbname, LDBM_SUFFIX, 0 );
447 #endif
448
449                 return idl;
450         }
451
452         db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
453         
454         if ( db == NULL ) {
455 #ifdef NEW_LOGGING
456                 LDAP_LOG( FILTER, ERR, "equality_candidates: db open failed (%s%s)\n",
457                         dbname, LDBM_SUFFIX, 0 );
458 #else
459                 Debug( LDAP_DEBUG_ANY,
460                     "<= equality_candidates db open failed (%s%s)\n",
461                         dbname, LDBM_SUFFIX, 0 );
462 #endif
463
464                 return idl;
465         }
466
467         for ( i= 0; keys[i].bv_val != NULL; i++ ) {
468                 ID_BLOCK *save;
469                 ID_BLOCK *tmp;
470
471                 rc = key_read( op->o_bd, db, &keys[i], &tmp );
472
473                 if( rc != LDAP_SUCCESS ) {
474                         idl_free( idl );
475                         idl = NULL;
476 #ifdef NEW_LOGGING
477                         LDAP_LOG( FILTER, ERR, 
478                                    "equality_candidates: key read failed (%d)\n", rc, 0, 0 );
479 #else
480                         Debug( LDAP_DEBUG_TRACE,
481                                 "<= equality_candidates key read failed (%d)\n",
482                             rc, 0, 0 );
483 #endif
484
485                         break;
486                 }
487
488                 if( tmp == NULL ) {
489                         idl_free( idl );
490                         idl = NULL;
491 #ifdef NEW_LOGGING
492                         LDAP_LOG( FILTER, INFO, "equality_candidates NULL\n", 0, 0, 0 );
493 #else
494                         Debug( LDAP_DEBUG_TRACE,
495                                 "<= equality_candidates NULL\n",
496                             0, 0, 0 );
497 #endif
498
499                         break;
500                 }
501
502                 save = idl;
503                 idl = idl_intersection( op->o_bd, idl, tmp );
504                 idl_free( save );
505                 idl_free( tmp );
506
507                 if( idl == NULL ) break;
508         }
509
510         ber_bvarray_free_x( keys, op->o_tmpmemctx );
511
512         ldbm_cache_close( op->o_bd, db );
513
514
515 #ifdef NEW_LOGGING
516         LDAP_LOG( FILTER, ENTRY, 
517                    "equality_candidates: return %ld\n", 
518                    idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
519 #else
520         Debug( LDAP_DEBUG_TRACE, "<= equality_candidates %ld\n",
521             idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
522 #endif
523
524         return( idl );
525 }
526
527 static ID_BLOCK *
528 approx_candidates(
529     Operation *op,
530         AttributeAssertion *ava
531 )
532 {
533         ID_BLOCK *idl;
534         DBCache *db;
535         int i;
536         int rc;
537         char *dbname;
538         slap_mask_t mask;
539         struct berval prefix = {0, NULL};
540         struct berval *keys = NULL;
541         MatchingRule *mr;
542
543 #ifdef NEW_LOGGING
544         LDAP_LOG( FILTER, ENTRY, "approx_candidates: enter\n", 0, 0, 0 );
545 #else
546         Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
547 #endif
548
549
550         idl = idl_allids( op->o_bd );
551
552         rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_APPROX,
553                 &dbname, &mask, &prefix );
554
555         if( rc != LDAP_SUCCESS ) {
556 #ifdef NEW_LOGGING
557                 LDAP_LOG( FILTER, ERR, 
558                            "approx_candidates: index_param returned %d\n", rc, 0, 0 );
559 #else
560                 Debug( LDAP_DEBUG_TRACE,
561                     "<= approx_candidates: index_param returned=%d\n",
562                         rc, 0, 0 );
563 #endif
564
565                 return idl;
566         }
567
568         if( dbname == NULL ) {
569                 /* not indexed */
570 #ifdef NEW_LOGGING
571                 LDAP_LOG( FILTER, ERR, "approx_candidates: not indexed\n", 0, 0, 0 );
572 #else
573                 Debug( LDAP_DEBUG_ANY,
574                     "<= approx_candidates: not indexed\n",
575                         0, 0, 0 );
576 #endif
577
578                 return idl;
579         }
580
581         mr = ava->aa_desc->ad_type->sat_approx;
582         if( !mr ) {
583                 /* no approx matching rule, try equality matching rule */
584                 mr = ava->aa_desc->ad_type->sat_equality;
585         }
586
587         if( !mr ) {
588                 return idl;
589         }
590
591         if( !mr->smr_filter ) {
592                 return idl;
593         }
594
595         rc = (mr->smr_filter)(
596                 LDAP_FILTER_APPROX,
597                 mask,
598                 ava->aa_desc->ad_type->sat_syntax,
599                 mr,
600                 &prefix,
601                 &ava->aa_value,
602                 &keys, op->o_tmpmemctx );
603
604         if( rc != LDAP_SUCCESS ) {
605 #ifdef NEW_LOGGING
606                 LDAP_LOG( FILTER, ERR, 
607                            "approx_candidates: (%s%s) MR filter failed (%d)\n",
608                            dbname, LDBM_SUFFIX, rc );
609 #else
610                 Debug( LDAP_DEBUG_TRACE,
611                     "<= approx_candidates: (%s%s) MR filter failed (%d)\n",
612                         dbname, LDBM_SUFFIX, rc );
613 #endif
614
615                 return idl;
616         }
617
618         if( keys == NULL ) {
619 #ifdef NEW_LOGGING
620                 LDAP_LOG( FILTER, INFO, 
621                            "approx_candidates: no keys (%s%s)\n",
622                            dbname, LDBM_SUFFIX, 0 );
623 #else
624                 Debug( LDAP_DEBUG_TRACE,
625                     "<= approx_candidates: no keys (%s%s)\n",
626                         dbname, LDBM_SUFFIX, 0 );
627 #endif
628
629                 return idl;
630         }
631
632         db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
633         
634         if ( db == NULL ) {
635 #ifdef NEW_LOGGING
636                 LDAP_LOG( FILTER, ERR, 
637                         "approx_candidates db open failed (%s%s)\n", 
638                         dbname, LDBM_SUFFIX, 0 );
639 #else
640                 Debug( LDAP_DEBUG_ANY,
641                     "<= approx_candidates db open failed (%s%s)\n",
642                         dbname, LDBM_SUFFIX, 0 );
643 #endif
644
645                 return idl;
646         }
647
648         for ( i= 0; keys[i].bv_val != NULL; i++ ) {
649                 ID_BLOCK *save;
650                 ID_BLOCK *tmp;
651
652                 rc = key_read( op->o_bd, db, &keys[i], &tmp );
653
654                 if( rc != LDAP_SUCCESS ) {
655                         idl_free( idl );
656                         idl = NULL;
657 #ifdef NEW_LOGGING
658                         LDAP_LOG( FILTER, ERR, 
659                                    "approx_candidates: key read failed (%d)\n", rc, 0, 0 );
660 #else
661                         Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
662                             rc, 0, 0 );
663 #endif
664
665                         break;
666                 }
667
668                 if( tmp == NULL ) {
669                         idl_free( idl );
670                         idl = NULL;
671 #ifdef NEW_LOGGING
672                         LDAP_LOG( FILTER, INFO, "approx_candidates: NULL\n", 0, 0, 0 );
673 #else
674                         Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
675                             0, 0, 0 );
676 #endif
677
678                         break;
679                 }
680
681                 save = idl;
682                 idl = idl_intersection( op->o_bd, idl, tmp );
683                 idl_free( save );
684                 idl_free( tmp );
685
686                 if( idl == NULL ) break;
687         }
688
689         ber_bvarray_free_x( keys, op->o_tmpmemctx );
690
691         ldbm_cache_close( op->o_bd, db );
692
693 #ifdef NEW_LOGGING
694         LDAP_LOG( FILTER, ENTRY, 
695                 "approx_candidates: return %ld\n", 
696                 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
697 #else
698         Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
699             idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
700 #endif
701
702         return( idl );
703 }
704
705 static ID_BLOCK *
706 list_candidates(
707     Operation *op,
708     Filter      *flist,
709     int         ftype
710 )
711 {
712         ID_BLOCK        *idl, *tmp, *tmp2;
713         Filter  *f;
714
715 #ifdef NEW_LOGGING
716         LDAP_LOG( FILTER, ENTRY, "list_candidates: 0x%x\n", ftype, 0, 0 );
717 #else
718         Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
719 #endif
720
721
722         idl = NULL;
723         for ( f = flist; f != NULL; f = f->f_next ) {
724                 if ( (tmp = filter_candidates( op, f )) == NULL &&
725                     ftype == LDAP_FILTER_AND ) {
726 #ifdef NEW_LOGGING
727                         LDAP_LOG( FILTER, INFO, "list_candidates: NULL\n", 0, 0, 0 );
728 #else
729                         Debug( LDAP_DEBUG_TRACE,
730                                "<= list_candidates NULL\n", 0, 0, 0 );
731 #endif
732
733                         idl_free( idl );
734                         return( NULL );
735                 }
736
737                 tmp2 = idl;
738                 if ( idl == NULL ) {
739                         idl = tmp;
740                 } else if ( ftype == LDAP_FILTER_AND ) {
741                         idl = idl_intersection( op->o_bd, idl, tmp );
742                         idl_free( tmp );
743                         idl_free( tmp2 );
744                 } else {
745                         idl = idl_union( op->o_bd, idl, tmp );
746                         idl_free( tmp );
747                         idl_free( tmp2 );
748                 }
749         }
750
751 #ifdef NEW_LOGGING
752         LDAP_LOG( FILTER, ENTRY, "list_candidates: return %ld\n",
753                    idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
754 #else
755         Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
756             idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
757 #endif
758
759         return( idl );
760 }
761
762 static ID_BLOCK *
763 substring_candidates(
764     Operation *op,
765     SubstringsAssertion *sub
766 )
767 {
768         ID_BLOCK *idl;
769         DBCache *db;
770         int i;
771         int rc;
772         char *dbname;
773         slap_mask_t mask;
774         struct berval prefix = {0, NULL};
775         struct berval *keys = NULL;
776         MatchingRule *mr;
777
778 #ifdef NEW_LOGGING
779         LDAP_LOG( FILTER, ENTRY, "substrings_candidates: enter\n", 0, 0, 0 );
780 #else
781         Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
782 #endif
783
784
785         idl = idl_allids( op->o_bd );
786
787         rc = index_param( op->o_bd, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
788                 &dbname, &mask, &prefix );
789
790         if( rc != LDAP_SUCCESS ) {
791 #ifdef NEW_LOGGING
792                 LDAP_LOG( FILTER, ERR, 
793                            "substrings_candidates: index_param returned %d\n", rc, 0, 0 );
794 #else
795                 Debug( LDAP_DEBUG_TRACE,
796                     "<= substrings_candidates: index_param returned=%d\n",
797                         rc, 0, 0 );
798 #endif
799
800                 return idl;
801         }
802
803         if( dbname == NULL ) {
804                 /* not indexed */
805 #ifdef NEW_LOGGING
806                 LDAP_LOG( FILTER, ERR, "substrings_candidates: not indexed\n", 0, 0, 0);
807 #else
808                 Debug( LDAP_DEBUG_ANY,
809                     "<= substrings_candidates: not indexed\n",
810                         0, 0, 0 );
811 #endif
812
813                 return idl;
814         }
815
816         mr = sub->sa_desc->ad_type->sat_substr;
817
818         if( !mr ) {
819                 return idl;
820         }
821
822         if( !mr->smr_filter ) {
823                 return idl;
824         }
825
826         rc = (mr->smr_filter)(
827                 LDAP_FILTER_SUBSTRINGS,
828                 mask,
829                 sub->sa_desc->ad_type->sat_syntax,
830                 mr,
831                 &prefix,
832                 sub,
833                 &keys, op->o_tmpmemctx );
834
835         if( rc != LDAP_SUCCESS ) {
836 #ifdef NEW_LOGGING
837                 LDAP_LOG( FILTER, ERR, 
838                            "substrings_candidates: (%s%s) MR filter failed (%d)\n",
839                            dbname, LDBM_SUFFIX, rc );
840 #else
841                 Debug( LDAP_DEBUG_TRACE,
842                     "<= substrings_candidates: (%s%s) MR filter failed (%d)\n",
843                         dbname, LDBM_SUFFIX, rc );
844 #endif
845
846                 return idl;
847         }
848
849         if( keys == NULL ) {
850 #ifdef NEW_LOGGING
851                 LDAP_LOG( FILTER, ERR, 
852                            "substrings_candidates: (0x%04lx) no keys (%s%s)\n",
853                            mask, dbname, LDBM_SUFFIX );
854 #else
855                 Debug( LDAP_DEBUG_TRACE,
856                     "<= substrings_candidates: (0x%04lx) no keys (%s%s)\n",
857                         mask, dbname, LDBM_SUFFIX );
858 #endif
859
860                 return idl;
861         }
862
863         db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
864         
865         if ( db == NULL ) {
866 #ifdef NEW_LOGGING
867                 LDAP_LOG( FILTER, ERR, 
868                            "substrings_candidates: db open failed (%s%s)\n",
869                            dbname, LDBM_SUFFIX, 0 );
870 #else
871                 Debug( LDAP_DEBUG_ANY,
872                     "<= substrings_candidates db open failed (%s%s)\n",
873                         dbname, LDBM_SUFFIX, 0 );
874 #endif
875
876                 return idl;
877         }
878
879         for ( i= 0; keys[i].bv_val != NULL; i++ ) {
880                 ID_BLOCK *save;
881                 ID_BLOCK *tmp;
882
883                 rc = key_read( op->o_bd, db, &keys[i], &tmp );
884
885                 if( rc != LDAP_SUCCESS ) {
886                         idl_free( idl );
887                         idl = NULL;
888 #ifdef NEW_LOGGING
889                         LDAP_LOG( FILTER, ERR, 
890                                    "substrings_candidates: key read failed (%d)\n", rc, 0, 0 );
891 #else
892                         Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
893                             rc, 0, 0 );
894 #endif
895
896                         break;
897                 }
898
899                 if( tmp == NULL ) {
900                         idl_free( idl );
901                         idl = NULL;
902 #ifdef NEW_LOGGING
903                         LDAP_LOG( FILTER, INFO, "substrings_candidates: NULL\n", 0, 0, 0 );
904 #else
905                         Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
906                             0, 0, 0 );
907 #endif
908
909                         break;
910                 }
911
912                 save = idl;
913                 idl = idl_intersection( op->o_bd, idl, tmp );
914                 idl_free( save );
915                 idl_free( tmp );
916
917                 if( idl == NULL ) break;
918         }
919
920         ber_bvarray_free_x( keys, op->o_tmpmemctx );
921
922         ldbm_cache_close( op->o_bd, db );
923
924 #ifdef NEW_LOGGING
925         LDAP_LOG( FILTER, ENTRY, 
926                    "substrings_candidates: return %ld\n",
927                    idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
928 #else
929         Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld\n",
930             idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
931 #endif
932
933         return( idl );
934 }