]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/filterindex.c
Sync with HEAD for OL 2.4.5
[openldap] / servers / slapd / back-bdb / 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 2000-2007 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 #include <ac/string.h>
21
22 #include "back-bdb.h"
23 #include "idl.h"
24 #ifdef LDAP_COMP_MATCH
25 #include <component.h>
26 #endif
27
28 static int presence_candidates(
29         Operation *op,
30         BDB_LOCKER locker,
31         AttributeDescription *desc,
32         ID *ids );
33
34 static int equality_candidates(
35         Operation *op,
36         BDB_LOCKER locker,
37         AttributeAssertion *ava,
38         ID *ids,
39         ID *tmp );
40 static int inequality_candidates(
41         Operation *op,
42         BDB_LOCKER locker,
43         AttributeAssertion *ava,
44         ID *ids,
45         ID *tmp,
46         int gtorlt );
47 static int approx_candidates(
48         Operation *op,
49         BDB_LOCKER locker,
50         AttributeAssertion *ava,
51         ID *ids,
52         ID *tmp );
53 static int substring_candidates(
54         Operation *op,
55         BDB_LOCKER locker,
56         SubstringsAssertion *sub,
57         ID *ids,
58         ID *tmp );
59
60 static int list_candidates(
61         Operation *op,
62         BDB_LOCKER locker,
63         Filter *flist,
64         int ftype,
65         ID *ids,
66         ID *tmp,
67         ID *stack );
68
69 static int
70 ext_candidates(
71         Operation *op,
72                 BDB_LOCKER locker,
73         MatchingRuleAssertion *mra,
74         ID *ids,
75         ID *tmp,
76         ID *stack);
77
78 #ifdef LDAP_COMP_MATCH
79 static int
80 comp_candidates (
81         Operation *op,
82         BDB_LOCKER locker,
83         MatchingRuleAssertion *mra,
84         ComponentFilter *f,
85         ID *ids,
86         ID *tmp,
87         ID *stack);
88
89 static int
90 ava_comp_candidates (
91                 Operation *op,
92                 BDB_LOCKER locker,
93                 AttributeAssertion *ava,
94                 AttributeAliasing *aa,
95                 ID *ids,
96                 ID *tmp,
97                 ID *stack);
98 #endif
99
100 int
101 bdb_filter_candidates(
102         Operation *op,
103         BDB_LOCKER locker,
104         Filter  *f,
105         ID *ids,
106         ID *tmp,
107         ID *stack )
108 {
109         int rc = 0;
110 #ifdef LDAP_COMP_MATCH
111         AttributeAliasing *aa;
112 #endif
113         Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
114
115         if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) {
116                 BDB_IDL_ZERO( ids );
117                 goto out;
118         }
119
120         switch ( f->f_choice ) {
121         case SLAPD_FILTER_COMPUTED:
122                 switch( f->f_result ) {
123                 case SLAPD_COMPARE_UNDEFINED:
124                 /* This technically is not the same as FALSE, but it
125                  * certainly will produce no matches.
126                  */
127                 /* FALL THRU */
128                 case LDAP_COMPARE_FALSE:
129                         BDB_IDL_ZERO( ids );
130                         break;
131                 case LDAP_COMPARE_TRUE: {
132                         struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
133                         BDB_IDL_ALL( bdb, ids );
134                         } break;
135                 case LDAP_SUCCESS:
136                         /* this is a pre-computed scope, leave it alone */
137                         break;
138                 }
139                 break;
140         case LDAP_FILTER_PRESENT:
141                 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
142                 rc = presence_candidates( op, locker, f->f_desc, ids );
143                 break;
144
145         case LDAP_FILTER_EQUALITY:
146                 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
147 #ifdef LDAP_COMP_MATCH
148                 if ( is_aliased_attribute && ( aa = is_aliased_attribute ( f->f_ava->aa_desc ) ) ) {
149                         rc = ava_comp_candidates ( op, locker, f->f_ava, aa, ids, tmp, stack );
150                 }
151                 else
152 #endif
153                 {
154                         rc = equality_candidates( op, locker, f->f_ava, ids, tmp );
155                 }
156                 break;
157
158         case LDAP_FILTER_APPROX:
159                 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
160                 rc = approx_candidates( op, locker, f->f_ava, ids, tmp );
161                 break;
162
163         case LDAP_FILTER_SUBSTRINGS:
164                 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
165                 rc = substring_candidates( op, locker, f->f_sub, ids, tmp );
166                 break;
167
168         case LDAP_FILTER_GE:
169                 /* if no GE index, use pres */
170                 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
171                 if( f->f_ava->aa_desc->ad_type->sat_ordering &&
172                         ( f->f_ava->aa_desc->ad_type->sat_ordering->smr_usage & SLAP_MR_ORDERED_INDEX ) )
173                         rc = inequality_candidates( op, locker, f->f_ava, ids, tmp, LDAP_FILTER_GE );
174                 else
175                         rc = presence_candidates( op, locker, f->f_ava->aa_desc, ids );
176                 break;
177
178         case LDAP_FILTER_LE:
179                 /* if no LE index, use pres */
180                 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
181                 if( f->f_ava->aa_desc->ad_type->sat_ordering &&
182                         ( f->f_ava->aa_desc->ad_type->sat_ordering->smr_usage & SLAP_MR_ORDERED_INDEX ) )
183                         rc = inequality_candidates( op, locker, f->f_ava, ids, tmp, LDAP_FILTER_LE );
184                 else
185                         rc = presence_candidates( op, locker, f->f_ava->aa_desc, ids );
186                 break;
187
188         case LDAP_FILTER_NOT:
189                 /* no indexing to support NOT filters */
190                 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
191                 { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
192                 BDB_IDL_ALL( bdb, ids );
193                 }
194                 break;
195
196         case LDAP_FILTER_AND:
197                 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
198                 rc = list_candidates( op, locker, 
199                         f->f_and, LDAP_FILTER_AND, ids, tmp, stack );
200                 break;
201
202         case LDAP_FILTER_OR:
203                 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
204                 rc = list_candidates( op, locker,
205                         f->f_or, LDAP_FILTER_OR, ids, tmp, stack );
206                 break;
207         case LDAP_FILTER_EXT:
208                 Debug( LDAP_DEBUG_FILTER, "\tEXT\n", 0, 0, 0 );
209                 rc = ext_candidates( op, locker, f->f_mra, ids, tmp, stack );
210                 break;
211         default:
212                 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %lu\n",
213                         (unsigned long) f->f_choice, 0, 0 );
214                 /* Must not return NULL, otherwise extended filters break */
215                 { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
216                 BDB_IDL_ALL( bdb, ids );
217                 }
218         }
219
220 out:
221         Debug( LDAP_DEBUG_FILTER,
222                 "<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
223                 (long) ids[0],
224                 (long) BDB_IDL_FIRST( ids ),
225                 (long) BDB_IDL_LAST( ids ) );
226
227         return rc;
228 }
229
230 #ifdef LDAP_COMP_MATCH
231 static int
232 comp_list_candidates(
233         Operation *op,
234         BDB_LOCKER locker,
235         MatchingRuleAssertion* mra,
236         ComponentFilter *flist,
237         int     ftype,
238         ID *ids,
239         ID *tmp,
240         ID *save )
241 {
242         int rc = 0;
243         ComponentFilter *f;
244
245         Debug( LDAP_DEBUG_FILTER, "=> comp_list_candidates 0x%x\n", ftype, 0, 0 );
246         for ( f = flist; f != NULL; f = f->cf_next ) {
247                 /* ignore precomputed scopes */
248                 if ( f->cf_choice == SLAPD_FILTER_COMPUTED &&
249                      f->cf_result == LDAP_SUCCESS ) {
250                         continue;
251                 }
252                 BDB_IDL_ZERO( save );
253                 rc = comp_candidates( op, locker, mra, f, save, tmp, save+BDB_IDL_UM_SIZE );
254
255                 if ( rc != 0 ) {
256                         if ( ftype == LDAP_COMP_FILTER_AND ) {
257                                 rc = 0;
258                                 continue;
259                         }
260                         break;
261                 }
262                 
263                 if ( ftype == LDAP_COMP_FILTER_AND ) {
264                         if ( f == flist ) {
265                                 BDB_IDL_CPY( ids, save );
266                         } else {
267                                 bdb_idl_intersection( ids, save );
268                         }
269                         if( BDB_IDL_IS_ZERO( ids ) )
270                                 break;
271                 } else {
272                         if ( f == flist ) {
273                                 BDB_IDL_CPY( ids, save );
274                         } else {
275                                 bdb_idl_union( ids, save );
276                         }
277                 }
278         }
279
280         if( rc == LDAP_SUCCESS ) {
281                 Debug( LDAP_DEBUG_FILTER,
282                         "<= comp_list_candidates: id=%ld first=%ld last=%ld\n",
283                         (long) ids[0],
284                         (long) BDB_IDL_FIRST(ids),
285                         (long) BDB_IDL_LAST(ids) );
286
287         } else {
288                 Debug( LDAP_DEBUG_FILTER,
289                         "<= comp_list_candidates: undefined rc=%d\n",
290                         rc, 0, 0 );
291         }
292
293         return rc;
294 }
295
296 static int
297 comp_equality_candidates (
298         Operation *op,
299         BDB_LOCKER locker,
300         MatchingRuleAssertion *mra,
301         ComponentAssertion *ca,
302         ID *ids,
303         ID *tmp,
304         ID *stack)
305 {
306        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
307         DB      *db;
308         int i;
309         int rc;
310         slap_mask_t mask;
311         struct berval prefix = {0, NULL};
312         struct berval *keys = NULL;
313         MatchingRule *mr = mra->ma_rule;
314         Syntax *sat_syntax;
315         ComponentReference* cr_list, *cr;
316         AttrInfo *ai;
317
318         BDB_IDL_ALL( bdb, ids );
319
320         if ( !ca->ca_comp_ref )
321                 return 0;
322
323         ai = bdb_attr_mask( op->o_bd->be_private, mra->ma_desc );
324         if( ai ) {
325                 cr_list = ai->ai_cr;
326         }
327         else {
328                 return 0;
329         }
330         /* find a component reference to be indexed */
331         sat_syntax = ca->ca_ma_rule->smr_syntax;
332         for ( cr = cr_list ; cr ; cr = cr->cr_next ) {
333                 if ( cr->cr_string.bv_len == ca->ca_comp_ref->cr_string.bv_len &&
334                         strncmp( cr->cr_string.bv_val, ca->ca_comp_ref->cr_string.bv_val,cr->cr_string.bv_len ) == 0 )
335                         break;
336         }
337         
338         if ( !cr )
339                 return 0;
340
341         rc = bdb_index_param( op->o_bd, mra->ma_desc, LDAP_FILTER_EQUALITY,
342                 &db, &mask, &prefix );
343
344         if( rc != LDAP_SUCCESS ) {
345                 return 0;
346         }
347
348         if( !mr ) {
349                 return 0;
350         }
351
352         if( !mr->smr_filter ) {
353                 return 0;
354         }
355
356         rc = (ca->ca_ma_rule->smr_filter)(
357                 LDAP_FILTER_EQUALITY,
358                 cr->cr_indexmask,
359                 sat_syntax,
360                 ca->ca_ma_rule,
361                 &prefix,
362                 &ca->ca_ma_value,
363                 &keys, op->o_tmpmemctx );
364
365         if( rc != LDAP_SUCCESS ) {
366                 return 0;
367         }
368
369         if( keys == NULL ) {
370                 return 0;
371         }
372         for ( i= 0; keys[i].bv_val != NULL; i++ ) {
373                 rc = bdb_key_read( op->o_bd, db, locker, &keys[i], tmp, NULL, 0 );
374
375                 if( rc == DB_NOTFOUND ) {
376                         BDB_IDL_ZERO( ids );
377                         rc = 0;
378                         break;
379                 } else if( rc != LDAP_SUCCESS ) {
380                         break;
381                 }
382
383                 if( BDB_IDL_IS_ZERO( tmp ) ) {
384                         BDB_IDL_ZERO( ids );
385                         break;
386                 }
387
388                 if ( i == 0 ) {
389                         BDB_IDL_CPY( ids, tmp );
390                 } else {
391                         bdb_idl_intersection( ids, tmp );
392                 }
393
394                 if( BDB_IDL_IS_ZERO( ids ) )
395                         break;
396         }
397         ber_bvarray_free_x( keys, op->o_tmpmemctx );
398
399         Debug( LDAP_DEBUG_TRACE,
400                 "<= comp_equality_candidates: id=%ld, first=%ld, last=%ld\n",
401                 (long) ids[0],
402                 (long) BDB_IDL_FIRST(ids),
403                 (long) BDB_IDL_LAST(ids) );
404         return( rc );
405 }
406
407 static int
408 ava_comp_candidates (
409         Operation *op,
410         BDB_LOCKER locker,
411         AttributeAssertion *ava,
412         AttributeAliasing *aa,
413         ID *ids,
414         ID *tmp,
415         ID *stack )
416 {
417         MatchingRuleAssertion mra;
418         
419         mra.ma_rule = ava->aa_desc->ad_type->sat_equality;
420         if ( !mra.ma_rule ) {
421                 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
422                 BDB_IDL_ALL( bdb, ids );
423                 return 0;
424         }
425         mra.ma_desc = aa->aa_aliased_ad;
426         mra.ma_rule = ava->aa_desc->ad_type->sat_equality;
427         
428         return comp_candidates ( op, locker, &mra, ava->aa_cf, ids, tmp, stack );
429 }
430
431 static int
432 comp_candidates (
433         Operation *op,
434         BDB_LOCKER locker,
435         MatchingRuleAssertion *mra,
436         ComponentFilter *f,
437         ID *ids,
438         ID *tmp,
439         ID *stack)
440 {
441         int     rc;
442
443         if ( !f ) return LDAP_PROTOCOL_ERROR;
444
445         Debug( LDAP_DEBUG_FILTER, "comp_candidates\n", 0, 0, 0 );
446         switch ( f->cf_choice ) {
447         case SLAPD_FILTER_COMPUTED:
448                 rc = f->cf_result;
449                 break;
450         case LDAP_COMP_FILTER_AND:
451                 rc = comp_list_candidates( op, locker, mra, f->cf_and, LDAP_COMP_FILTER_AND, ids, tmp, stack );
452                 break;
453         case LDAP_COMP_FILTER_OR:
454                 rc = comp_list_candidates( op, locker, mra, f->cf_or, LDAP_COMP_FILTER_OR, ids, tmp, stack );
455                 break;
456         case LDAP_COMP_FILTER_NOT:
457                 /* No component indexing supported for NOT filter */
458                 Debug( LDAP_DEBUG_FILTER, "\tComponent NOT\n", 0, 0, 0 );
459                 {
460                         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
461                         BDB_IDL_ALL( bdb, ids );
462                 }
463                 rc = LDAP_PROTOCOL_ERROR;
464                 break;
465         case LDAP_COMP_FILTER_ITEM:
466                 rc = comp_equality_candidates( op, locker, mra, f->cf_ca, ids, tmp, stack );
467                 break;
468         default:
469                 {
470                         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
471                         BDB_IDL_ALL( bdb, ids );
472                 }
473                 rc = LDAP_PROTOCOL_ERROR;
474         }
475
476         return( rc );
477 }
478 #endif
479
480 static int
481 ext_candidates(
482         Operation *op,
483                 BDB_LOCKER locker,
484         MatchingRuleAssertion *mra,
485         ID *ids,
486         ID *tmp,
487         ID *stack)
488 {
489         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
490
491 #ifdef LDAP_COMP_MATCH
492         /*
493          * Currently Only Component Indexing for componentFilterMatch is supported
494          * Indexing for an extensible filter is not supported yet
495          */
496         if ( mra->ma_cf ) {
497                 return comp_candidates ( op, locker, mra, mra->ma_cf, ids, tmp, stack);
498         }
499 #endif
500         if ( mra->ma_desc == slap_schema.si_ad_entryDN ) {
501                 int rc;
502                 EntryInfo *ei;
503
504                 BDB_IDL_ZERO( ids );
505                 if ( mra->ma_rule == slap_schema.si_mr_distinguishedNameMatch ) {
506                         ei = NULL;
507                         rc = bdb_cache_find_ndn( op, NULL, &mra->ma_value, &ei );
508                         if ( rc == LDAP_SUCCESS )
509                                 bdb_idl_insert( ids, ei->bei_id );
510                         if ( ei )
511                                 bdb_cache_entryinfo_unlock( ei );
512                         return 0;
513                 } else if ( mra->ma_rule && mra->ma_rule->smr_match ==
514                         dnRelativeMatch && dnIsSuffix( &mra->ma_value,
515                                 op->o_bd->be_nsuffix )) {
516                         int scope;
517                         if ( mra->ma_rule == slap_schema.si_mr_dnSuperiorMatch ) {
518                                 struct berval pdn;
519                                 ei = NULL;
520                                 dnParent( &mra->ma_value, &pdn );
521                                 bdb_cache_find_ndn( op, NULL, &pdn, &ei );
522                                 if ( ei ) {
523                                         bdb_cache_entryinfo_unlock( ei );
524                                         while ( ei && ei->bei_id ) {
525                                                 bdb_idl_insert( ids, ei->bei_id );
526                                                 ei = ei->bei_parent;
527                                         }
528                                 }
529                                 return 0;
530                         }
531                         if ( mra->ma_rule == slap_schema.si_mr_dnSubtreeMatch )
532                                 scope = LDAP_SCOPE_SUBTREE;
533                         else if ( mra->ma_rule == slap_schema.si_mr_dnOneLevelMatch )
534                                 scope = LDAP_SCOPE_ONELEVEL;
535                         else if ( mra->ma_rule == slap_schema.si_mr_dnSubordinateMatch )
536                                 scope = LDAP_SCOPE_SUBORDINATE;
537                         else
538                                 scope = LDAP_SCOPE_BASE;
539                         if ( scope > LDAP_SCOPE_BASE ) {
540                                 ei = NULL;
541                                 rc = bdb_cache_find_ndn( op, NULL, &mra->ma_value, &ei );
542                                 if ( ei )
543                                         bdb_cache_entryinfo_unlock( ei );
544                                 if ( rc == LDAP_SUCCESS ) {
545                                         int sc = op->ors_scope;
546                                         op->ors_scope = scope;
547                                         rc = bdb_dn2idl( op, locker, &mra->ma_value, ei, ids,
548                                                 stack );
549                                 }
550                                 return 0;
551                         }
552                 }
553         }
554
555         BDB_IDL_ALL( bdb, ids );
556         return 0;
557 }
558
559 static int
560 list_candidates(
561         Operation *op,
562         BDB_LOCKER locker,
563         Filter  *flist,
564         int             ftype,
565         ID *ids,
566         ID *tmp,
567         ID *save )
568 {
569         int rc = 0;
570         Filter  *f;
571
572         Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
573         for ( f = flist; f != NULL; f = f->f_next ) {
574                 /* ignore precomputed scopes */
575                 if ( f->f_choice == SLAPD_FILTER_COMPUTED &&
576                      f->f_result == LDAP_SUCCESS ) {
577                         continue;
578                 }
579                 BDB_IDL_ZERO( save );
580                 rc = bdb_filter_candidates( op, locker, f, save, tmp,
581                         save+BDB_IDL_UM_SIZE );
582
583                 if ( rc != 0 ) {
584                         if ( ftype == LDAP_FILTER_AND ) {
585                                 rc = 0;
586                                 continue;
587                         }
588                         break;
589                 }
590
591                 
592                 if ( ftype == LDAP_FILTER_AND ) {
593                         if ( f == flist ) {
594                                 BDB_IDL_CPY( ids, save );
595                         } else {
596                                 bdb_idl_intersection( ids, save );
597                         }
598                         if( BDB_IDL_IS_ZERO( ids ) )
599                                 break;
600                 } else {
601                         if ( f == flist ) {
602                                 BDB_IDL_CPY( ids, save );
603                         } else {
604                                 bdb_idl_union( ids, save );
605                         }
606                 }
607         }
608
609         if( rc == LDAP_SUCCESS ) {
610                 Debug( LDAP_DEBUG_FILTER,
611                         "<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
612                         (long) ids[0],
613                         (long) BDB_IDL_FIRST(ids),
614                         (long) BDB_IDL_LAST(ids) );
615
616         } else {
617                 Debug( LDAP_DEBUG_FILTER,
618                         "<= bdb_list_candidates: undefined rc=%d\n",
619                         rc, 0, 0 );
620         }
621
622         return rc;
623 }
624
625 static int
626 presence_candidates(
627         Operation *op,
628         BDB_LOCKER locker,
629         AttributeDescription *desc,
630         ID *ids )
631 {
632         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
633         DB *db;
634         int rc;
635         slap_mask_t mask;
636         struct berval prefix = {0, NULL};
637
638         Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates (%s)\n",
639                         desc->ad_cname.bv_val, 0, 0 );
640
641         BDB_IDL_ALL( bdb, ids );
642
643         if( desc == slap_schema.si_ad_objectClass ) {
644                 return 0;
645         }
646
647         rc = bdb_index_param( op->o_bd, desc, LDAP_FILTER_PRESENT,
648                 &db, &mask, &prefix );
649
650         if( rc == LDAP_INAPPROPRIATE_MATCHING ) {
651                 /* not indexed */
652                 Debug( LDAP_DEBUG_TRACE,
653                         "<= bdb_presence_candidates: (%s) not indexed\n",
654                         desc->ad_cname.bv_val, 0, 0 );
655                 return 0;
656         }
657
658         if( rc != LDAP_SUCCESS ) {
659                 Debug( LDAP_DEBUG_TRACE,
660                         "<= bdb_presence_candidates: (%s) index_param "
661                         "returned=%d\n",
662                         desc->ad_cname.bv_val, rc, 0 );
663                 return 0;
664         }
665
666         if( prefix.bv_val == NULL ) {
667                 Debug( LDAP_DEBUG_TRACE,
668                         "<= bdb_presence_candidates: (%s) no prefix\n",
669                         desc->ad_cname.bv_val, 0, 0 );
670                 return -1;
671         }
672
673         rc = bdb_key_read( op->o_bd, db, locker, &prefix, ids, NULL, 0 );
674
675         if( rc == DB_NOTFOUND ) {
676                 BDB_IDL_ZERO( ids );
677                 rc = 0;
678         } else if( rc != LDAP_SUCCESS ) {
679                 Debug( LDAP_DEBUG_TRACE,
680                         "<= bdb_presense_candidates: (%s) "
681                         "key read failed (%d)\n",
682                         desc->ad_cname.bv_val, rc, 0 );
683                 goto done;
684         }
685
686         Debug(LDAP_DEBUG_TRACE,
687                 "<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
688                 (long) ids[0],
689                 (long) BDB_IDL_FIRST(ids),
690                 (long) BDB_IDL_LAST(ids) );
691
692 done:
693         return rc;
694 }
695
696 static int
697 equality_candidates(
698         Operation *op,
699         BDB_LOCKER locker,
700         AttributeAssertion *ava,
701         ID *ids,
702         ID *tmp )
703 {
704         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
705         DB      *db;
706         int i;
707         int rc;
708         slap_mask_t mask;
709         struct berval prefix = {0, NULL};
710         struct berval *keys = NULL;
711         MatchingRule *mr;
712
713         Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates (%s)\n",
714                         ava->aa_desc->ad_cname.bv_val, 0, 0 );
715
716         BDB_IDL_ALL( bdb, ids );
717
718         rc = bdb_index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
719                 &db, &mask, &prefix );
720
721         if ( rc == LDAP_INAPPROPRIATE_MATCHING ) {
722                 Debug( LDAP_DEBUG_ANY,
723                         "<= bdb_equality_candidates: (%s) not indexed\n", 
724                         ava->aa_desc->ad_cname.bv_val, 0, 0 );
725                 return 0;
726         }
727
728         if( rc != LDAP_SUCCESS ) {
729                 Debug( LDAP_DEBUG_ANY,
730                         "<= bdb_equality_candidates: (%s) "
731                         "index_param failed (%d)\n",
732                         ava->aa_desc->ad_cname.bv_val, rc, 0 );
733                 return 0;
734         }
735
736         mr = ava->aa_desc->ad_type->sat_equality;
737         if( !mr ) {
738                 return 0;
739         }
740
741         if( !mr->smr_filter ) {
742                 return 0;
743         }
744
745         rc = (mr->smr_filter)(
746                 LDAP_FILTER_EQUALITY,
747                 mask,
748                 ava->aa_desc->ad_type->sat_syntax,
749                 mr,
750                 &prefix,
751                 &ava->aa_value,
752                 &keys, op->o_tmpmemctx );
753
754         if( rc != LDAP_SUCCESS ) {
755                 Debug( LDAP_DEBUG_TRACE,
756                         "<= bdb_equality_candidates: (%s, %s) "
757                         "MR filter failed (%d)\n",
758                         prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
759                 return 0;
760         }
761
762         if( keys == NULL ) {
763                 Debug( LDAP_DEBUG_TRACE,
764                         "<= bdb_equality_candidates: (%s) no keys\n",
765                         ava->aa_desc->ad_cname.bv_val, 0, 0 );
766                 return 0;
767         }
768
769         for ( i= 0; keys[i].bv_val != NULL; i++ ) {
770                 rc = bdb_key_read( op->o_bd, db, locker, &keys[i], tmp, NULL, 0 );
771
772                 if( rc == DB_NOTFOUND ) {
773                         BDB_IDL_ZERO( ids );
774                         rc = 0;
775                         break;
776                 } else if( rc != LDAP_SUCCESS ) {
777                         Debug( LDAP_DEBUG_TRACE,
778                                 "<= bdb_equality_candidates: (%s) "
779                                 "key read failed (%d)\n",
780                                 ava->aa_desc->ad_cname.bv_val, rc, 0 );
781                         break;
782                 }
783
784                 if( BDB_IDL_IS_ZERO( tmp ) ) {
785                         Debug( LDAP_DEBUG_TRACE,
786                                 "<= bdb_equality_candidates: (%s) NULL\n", 
787                                 ava->aa_desc->ad_cname.bv_val, 0, 0 );
788                         BDB_IDL_ZERO( ids );
789                         break;
790                 }
791
792                 if ( i == 0 ) {
793                         BDB_IDL_CPY( ids, tmp );
794                 } else {
795                         bdb_idl_intersection( ids, tmp );
796                 }
797
798                 if( BDB_IDL_IS_ZERO( ids ) )
799                         break;
800         }
801
802         ber_bvarray_free_x( keys, op->o_tmpmemctx );
803
804         Debug( LDAP_DEBUG_TRACE,
805                 "<= bdb_equality_candidates: id=%ld, first=%ld, last=%ld\n",
806                 (long) ids[0],
807                 (long) BDB_IDL_FIRST(ids),
808                 (long) BDB_IDL_LAST(ids) );
809         return( rc );
810 }
811
812
813 static int
814 approx_candidates(
815         Operation *op,
816         BDB_LOCKER locker,
817         AttributeAssertion *ava,
818         ID *ids,
819         ID *tmp )
820 {
821         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
822         DB      *db;
823         int i;
824         int rc;
825         slap_mask_t mask;
826         struct berval prefix = {0, NULL};
827         struct berval *keys = NULL;
828         MatchingRule *mr;
829
830         Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates (%s)\n",
831                         ava->aa_desc->ad_cname.bv_val, 0, 0 );
832
833         BDB_IDL_ALL( bdb, ids );
834
835         rc = bdb_index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_APPROX,
836                 &db, &mask, &prefix );
837
838         if ( rc == LDAP_INAPPROPRIATE_MATCHING ) {
839                 Debug( LDAP_DEBUG_ANY,
840                         "<= bdb_approx_candidates: (%s) not indexed\n",
841                         ava->aa_desc->ad_cname.bv_val, 0, 0 );
842                 return 0;
843         }
844
845         if( rc != LDAP_SUCCESS ) {
846                 Debug( LDAP_DEBUG_ANY,
847                         "<= bdb_approx_candidates: (%s) "
848                         "index_param failed (%d)\n",
849                         ava->aa_desc->ad_cname.bv_val, rc, 0 );
850                 return 0;
851         }
852
853         mr = ava->aa_desc->ad_type->sat_approx;
854         if( !mr ) {
855                 /* no approx matching rule, try equality matching rule */
856                 mr = ava->aa_desc->ad_type->sat_equality;
857         }
858
859         if( !mr ) {
860                 return 0;
861         }
862
863         if( !mr->smr_filter ) {
864                 return 0;
865         }
866
867         rc = (mr->smr_filter)(
868                 LDAP_FILTER_APPROX,
869                 mask,
870                 ava->aa_desc->ad_type->sat_syntax,
871                 mr,
872                 &prefix,
873                 &ava->aa_value,
874                 &keys, op->o_tmpmemctx );
875
876         if( rc != LDAP_SUCCESS ) {
877                 Debug( LDAP_DEBUG_TRACE,
878                         "<= bdb_approx_candidates: (%s, %s) "
879                         "MR filter failed (%d)\n",
880                         prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
881                 return 0;
882         }
883
884         if( keys == NULL ) {
885                 Debug( LDAP_DEBUG_TRACE,
886                         "<= bdb_approx_candidates: (%s) no keys (%s)\n",
887                         prefix.bv_val, ava->aa_desc->ad_cname.bv_val, 0 );
888                 return 0;
889         }
890
891         for ( i= 0; keys[i].bv_val != NULL; i++ ) {
892                 rc = bdb_key_read( op->o_bd, db, locker, &keys[i], tmp, NULL, 0 );
893
894                 if( rc == DB_NOTFOUND ) {
895                         BDB_IDL_ZERO( ids );
896                         rc = 0;
897                         break;
898                 } else if( rc != LDAP_SUCCESS ) {
899                         Debug( LDAP_DEBUG_TRACE,
900                                 "<= bdb_approx_candidates: (%s) "
901                                 "key read failed (%d)\n",
902                                 ava->aa_desc->ad_cname.bv_val, rc, 0 );
903                         break;
904                 }
905
906                 if( BDB_IDL_IS_ZERO( tmp ) ) {
907                         Debug( LDAP_DEBUG_TRACE,
908                                 "<= bdb_approx_candidates: (%s) NULL\n",
909                                 ava->aa_desc->ad_cname.bv_val, 0, 0 );
910                         BDB_IDL_ZERO( ids );
911                         break;
912                 }
913
914                 if ( i == 0 ) {
915                         BDB_IDL_CPY( ids, tmp );
916                 } else {
917                         bdb_idl_intersection( ids, tmp );
918                 }
919
920                 if( BDB_IDL_IS_ZERO( ids ) )
921                         break;
922         }
923
924         ber_bvarray_free_x( keys, op->o_tmpmemctx );
925
926         Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
927                 (long) ids[0],
928                 (long) BDB_IDL_FIRST(ids),
929                 (long) BDB_IDL_LAST(ids) );
930         return( rc );
931 }
932
933 static int
934 substring_candidates(
935         Operation *op,
936         BDB_LOCKER locker,
937         SubstringsAssertion     *sub,
938         ID *ids,
939         ID *tmp )
940 {
941         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
942         DB      *db;
943         int i;
944         int rc;
945         slap_mask_t mask;
946         struct berval prefix = {0, NULL};
947         struct berval *keys = NULL;
948         MatchingRule *mr;
949
950         Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates (%s)\n",
951                         sub->sa_desc->ad_cname.bv_val, 0, 0 );
952
953         BDB_IDL_ALL( bdb, ids );
954
955         rc = bdb_index_param( op->o_bd, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
956                 &db, &mask, &prefix );
957
958         if ( rc == LDAP_INAPPROPRIATE_MATCHING ) {
959                 Debug( LDAP_DEBUG_ANY,
960                         "<= bdb_substring_candidates: (%s) not indexed\n",
961                         sub->sa_desc->ad_cname.bv_val, 0, 0 );
962                 return 0;
963         }
964
965         if( rc != LDAP_SUCCESS ) {
966                 Debug( LDAP_DEBUG_ANY,
967                         "<= bdb_substring_candidates: (%s) "
968                         "index_param failed (%d)\n",
969                         sub->sa_desc->ad_cname.bv_val, rc, 0 );
970                 return 0;
971         }
972
973         mr = sub->sa_desc->ad_type->sat_substr;
974
975         if( !mr ) {
976                 return 0;
977         }
978
979         if( !mr->smr_filter ) {
980                 return 0;
981         }
982
983         rc = (mr->smr_filter)(
984                 LDAP_FILTER_SUBSTRINGS,
985                 mask,
986                 sub->sa_desc->ad_type->sat_syntax,
987                 mr,
988                 &prefix,
989                 sub,
990                 &keys, op->o_tmpmemctx );
991
992         if( rc != LDAP_SUCCESS ) {
993                 Debug( LDAP_DEBUG_TRACE,
994                         "<= bdb_substring_candidates: (%s) "
995                         "MR filter failed (%d)\n",
996                         sub->sa_desc->ad_cname.bv_val, rc, 0 );
997                 return 0;
998         }
999
1000         if( keys == NULL ) {
1001                 Debug( LDAP_DEBUG_TRACE,
1002                         "<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
1003                         mask, sub->sa_desc->ad_cname.bv_val, 0 );
1004                 return 0;
1005         }
1006
1007         for ( i= 0; keys[i].bv_val != NULL; i++ ) {
1008                 rc = bdb_key_read( op->o_bd, db, locker, &keys[i], tmp, NULL, 0 );
1009
1010                 if( rc == DB_NOTFOUND ) {
1011                         BDB_IDL_ZERO( ids );
1012                         rc = 0;
1013                         break;
1014                 } else if( rc != LDAP_SUCCESS ) {
1015                         Debug( LDAP_DEBUG_TRACE,
1016                                 "<= bdb_substring_candidates: (%s) "
1017                                 "key read failed (%d)\n",
1018                                 sub->sa_desc->ad_cname.bv_val, rc, 0 );
1019                         break;
1020                 }
1021
1022                 if( BDB_IDL_IS_ZERO( tmp ) ) {
1023                         Debug( LDAP_DEBUG_TRACE,
1024                                 "<= bdb_substring_candidates: (%s) NULL\n",
1025                                 sub->sa_desc->ad_cname.bv_val, 0, 0 );
1026                         BDB_IDL_ZERO( ids );
1027                         break;
1028                 }
1029
1030                 if ( i == 0 ) {
1031                         BDB_IDL_CPY( ids, tmp );
1032                 } else {
1033                         bdb_idl_intersection( ids, tmp );
1034                 }
1035
1036                 if( BDB_IDL_IS_ZERO( ids ) )
1037                         break;
1038         }
1039
1040         ber_bvarray_free_x( keys, op->o_tmpmemctx );
1041
1042         Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates: %ld, first=%ld, last=%ld\n",
1043                 (long) ids[0],
1044                 (long) BDB_IDL_FIRST(ids),
1045                 (long) BDB_IDL_LAST(ids) );
1046         return( rc );
1047 }
1048
1049 static int
1050 inequality_candidates(
1051         Operation *op,
1052         BDB_LOCKER locker,
1053         AttributeAssertion *ava,
1054         ID *ids,
1055         ID *tmp,
1056         int gtorlt )
1057 {
1058         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1059         DB      *db;
1060         int rc;
1061         slap_mask_t mask;
1062         struct berval prefix = {0, NULL};
1063         struct berval *keys = NULL;
1064         MatchingRule *mr;
1065         DBC * cursor = NULL;
1066
1067         Debug( LDAP_DEBUG_TRACE, "=> bdb_inequality_candidates (%s)\n",
1068                         ava->aa_desc->ad_cname.bv_val, 0, 0 );
1069
1070         BDB_IDL_ALL( bdb, ids );
1071
1072         rc = bdb_index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
1073                 &db, &mask, &prefix );
1074
1075         if ( rc == LDAP_INAPPROPRIATE_MATCHING ) {
1076                 Debug( LDAP_DEBUG_ANY,
1077                         "<= bdb_inequality_candidates: (%s) not indexed\n", 
1078                         ava->aa_desc->ad_cname.bv_val, 0, 0 );
1079                 return 0;
1080         }
1081
1082         if( rc != LDAP_SUCCESS ) {
1083                 Debug( LDAP_DEBUG_ANY,
1084                         "<= bdb_inequality_candidates: (%s) "
1085                         "index_param failed (%d)\n",
1086                         ava->aa_desc->ad_cname.bv_val, rc, 0 );
1087                 return 0;
1088         }
1089
1090         mr = ava->aa_desc->ad_type->sat_equality;
1091         if( !mr ) {
1092                 return 0;
1093         }
1094
1095         if( !mr->smr_filter ) {
1096                 return 0;
1097         }
1098
1099         rc = (mr->smr_filter)(
1100                 LDAP_FILTER_EQUALITY,
1101                 mask,
1102                 ava->aa_desc->ad_type->sat_syntax,
1103                 mr,
1104                 &prefix,
1105                 &ava->aa_value,
1106                 &keys, op->o_tmpmemctx );
1107
1108         if( rc != LDAP_SUCCESS ) {
1109                 Debug( LDAP_DEBUG_TRACE,
1110                         "<= bdb_inequality_candidates: (%s, %s) "
1111                         "MR filter failed (%d)\n",
1112                         prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
1113                 return 0;
1114         }
1115
1116         if( keys == NULL ) {
1117                 Debug( LDAP_DEBUG_TRACE,
1118                         "<= bdb_inequality_candidates: (%s) no keys\n",
1119                         ava->aa_desc->ad_cname.bv_val, 0, 0 );
1120                 return 0;
1121         }
1122
1123         BDB_IDL_ZERO( ids );
1124         while(1) {
1125                 rc = bdb_key_read( op->o_bd, db, locker, &keys[0], tmp, &cursor, gtorlt );
1126
1127                 if( rc == DB_NOTFOUND ) {
1128                         rc = 0;
1129                         break;
1130                 } else if( rc != LDAP_SUCCESS ) {
1131                         Debug( LDAP_DEBUG_TRACE,
1132                                "<= bdb_inequality_candidates: (%s) "
1133                                "key read failed (%d)\n",
1134                                ava->aa_desc->ad_cname.bv_val, rc, 0 );
1135                         break;
1136                 }
1137
1138                 if( BDB_IDL_IS_ZERO( tmp ) ) {
1139                         Debug( LDAP_DEBUG_TRACE,
1140                                "<= bdb_inequality_candidates: (%s) NULL\n", 
1141                                ava->aa_desc->ad_cname.bv_val, 0, 0 );
1142                         break;
1143                 }
1144
1145                 bdb_idl_union( ids, tmp );
1146
1147                 if( op->ors_limit && op->ors_limit->lms_s_unchecked != -1 &&
1148                         BDB_IDL_N( ids ) >= (unsigned) op->ors_limit->lms_s_unchecked ) {
1149                         cursor->c_close( cursor );
1150                         break;
1151                 }
1152         }
1153         ber_bvarray_free_x( keys, op->o_tmpmemctx );
1154
1155         Debug( LDAP_DEBUG_TRACE,
1156                 "<= bdb_inequality_candidates: id=%ld, first=%ld, last=%ld\n",
1157                 (long) ids[0],
1158                 (long) BDB_IDL_FIRST(ids),
1159                 (long) BDB_IDL_LAST(ids) );
1160         return( rc );
1161 }