]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/index.c
7a7b837ff3cbe132120bbae7a656887f9d9e4759
[openldap] / servers / slapd / back-ldbm / index.c
1 /* index.c - routines for dealing with attribute indexes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 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/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26
27 static slap_mask_t index_mask(
28         Backend *be,
29         AttributeDescription *desc,
30         char **dbname,
31         struct berval *atname )
32 {
33         AttributeType *at;
34         slap_mask_t mask = 0;
35
36         attr_mask( be->be_private, desc, &mask );
37
38         if( mask ) {
39                 *atname = desc->ad_cname;
40                 *dbname = desc->ad_cname.bv_val;
41                 return mask;
42         }
43
44         /* If there is a tagging option, did we ever index the base
45          * type? If so, check for mask, otherwise it's not there.
46          */
47         if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
48                 /* has tagging option */
49                 attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
50
51                 if( mask && ( mask ^ SLAP_INDEX_NOTAGS ) ) {
52                         *atname = desc->ad_type->sat_cname;
53                         *dbname = desc->ad_type->sat_cname.bv_val;
54                         return mask;
55                 }
56         }
57
58         /* see if supertype defined mask for its subtypes */
59         for( at = desc->ad_type->sat_sup; at != NULL ; at = at->sat_sup ) {
60                 /* If no AD, we've never indexed this type */
61                 if (!at->sat_ad)
62                         continue;
63                 
64                 attr_mask( be->be_private, at->sat_ad, &mask );
65
66                 if( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
67                         *atname = at->sat_cname;
68                         *dbname = at->sat_cname.bv_val;
69                         return mask;
70                 }
71         }
72
73         return 0;
74 }
75
76 int index_is_indexed(
77         Backend *be,
78         AttributeDescription *desc )
79 {
80         slap_mask_t mask;
81         char *dbname;
82         struct berval prefix;
83
84         mask = index_mask( be, desc, &dbname, &prefix );
85
86         if( mask == 0 ) {
87                 return LDAP_INAPPROPRIATE_MATCHING;
88         }
89
90         return LDAP_SUCCESS;
91 }
92
93 int index_param(
94         Backend *be,
95         AttributeDescription *desc,
96         int ftype,
97         char **dbnamep,
98         slap_mask_t *maskp,
99         struct berval *prefixp )
100 {
101         slap_mask_t mask;
102         char *dbname;
103
104         mask = index_mask( be, desc, &dbname, prefixp );
105
106         if( mask == 0 ) {
107                 return LDAP_INAPPROPRIATE_MATCHING;
108         }
109
110         switch( ftype ) {
111         case LDAP_FILTER_PRESENT:
112                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
113                         goto done;
114                 }
115                 break;
116
117         case LDAP_FILTER_APPROX:
118                 if ( desc->ad_type->sat_approx ) {
119                         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
120                                 goto done;
121                         }
122                         break;
123                 }
124
125                 /* Use EQUALITY rule and index for approximate match */
126                 /* fall thru */
127
128         case LDAP_FILTER_EQUALITY:
129                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
130                         goto done;
131                 }
132                 break;
133
134         case LDAP_FILTER_SUBSTRINGS:
135                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
136                         goto done;
137                 }
138                 break;
139
140         default:
141                 return LDAP_OTHER;
142         }
143
144         return LDAP_INAPPROPRIATE_MATCHING;
145
146 done:
147         *dbnamep = dbname;
148         *maskp = mask;
149         return LDAP_SUCCESS;
150 }
151
152 static int indexer(
153         Operation *op,
154         char *dbname,
155         struct berval *atname,
156         BerVarray vals,
157         ID id,
158         int opid,
159         slap_mask_t mask )
160 {
161         int rc, i;
162         const char *text;
163         DBCache *db;
164         AttributeDescription *ad = NULL;
165         struct berval *keys;
166
167         assert( mask );
168
169         rc = slap_bv2ad( atname, &ad, &text );
170
171         if( rc != LDAP_SUCCESS ) return rc;
172
173         db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
174         
175         if ( db == NULL ) {
176                 Debug( LDAP_DEBUG_ANY,
177                     "<= index_read NULL (could not open %s%s)\n",
178                         dbname, LDBM_SUFFIX, 0 );
179
180                 return LDAP_OTHER;
181         }
182
183         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
184                 key_change( op->o_bd, db, atname, id, opid );
185         }
186
187         if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
188                 rc = ad->ad_type->sat_equality->smr_indexer(
189                         LDAP_FILTER_EQUALITY,
190                         mask,
191                         ad->ad_type->sat_syntax,
192                         ad->ad_type->sat_equality,
193                         atname, vals, &keys, op->o_tmpmemctx );
194
195                 if( rc == LDAP_SUCCESS && keys != NULL ) {
196                         for( i=0; keys[i].bv_val != NULL; i++ ) {
197                                 key_change( op->o_bd, db, &keys[i], id, opid );
198                         }
199                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
200                 }
201         }
202
203         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
204                 rc = ad->ad_type->sat_approx->smr_indexer(
205                         LDAP_FILTER_APPROX,
206                         mask,
207                         ad->ad_type->sat_syntax,
208                         ad->ad_type->sat_approx,
209                         atname, vals, &keys, op->o_tmpmemctx );
210
211                 if( rc == LDAP_SUCCESS && keys != NULL ) {
212                         for( i=0; keys[i].bv_val != NULL; i++ ) {
213                                 key_change( op->o_bd, db, &keys[i], id, opid );
214                         }
215                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
216                 }
217         }
218
219         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
220                 rc = ad->ad_type->sat_substr->smr_indexer(
221                         LDAP_FILTER_SUBSTRINGS,
222                         mask,
223                         ad->ad_type->sat_syntax,
224                         ad->ad_type->sat_substr,
225                         atname, vals, &keys, op->o_tmpmemctx );
226
227                 if( rc == LDAP_SUCCESS && keys != NULL ) {
228                         for( i=0; keys[i].bv_val != NULL; i++ ) {
229                                 key_change( op->o_bd, db, &keys[i], id, opid );
230                         }
231                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
232                 }
233         }
234
235         ldbm_cache_close( op->o_bd, db );
236
237         return LDAP_SUCCESS;
238 }
239
240 static int index_at_values(
241         Operation *op,
242         AttributeType *type,
243         struct berval *tags,
244         BerVarray vals,
245         ID id,
246         int opid )
247 {
248         slap_mask_t mask = 0;
249
250         if( type->sat_sup ) {
251                 /* recurse */
252                 (void) index_at_values( op,
253                         type->sat_sup, tags,
254                         vals, id, opid );
255         }
256
257         /* If this type has no AD, we've never used it before */
258         if( type->sat_ad ) {
259                 attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
260         }
261
262         if( mask ) {
263                 indexer( op, type->sat_cname.bv_val,
264                         &type->sat_cname,
265                         vals, id, opid,
266                         mask );
267         }
268
269         if( tags->bv_len ) {
270                 AttributeDescription *desc;
271
272                 mask = 0;
273
274                 desc = ad_find_tags(type, tags);
275                 if( desc ) {
276                         attr_mask( op->o_bd->be_private, desc, &mask );
277                 }
278
279                 if( mask ) {
280                         indexer( op, desc->ad_cname.bv_val, &desc->ad_cname,
281                                 vals, id, opid,
282                                 mask );
283                 }
284         }
285
286         return LDAP_SUCCESS;
287 }
288
289 int index_values(
290         Operation *op,
291         AttributeDescription *desc,
292         BerVarray vals,
293         ID id,
294         int opid )
295 {
296         (void) index_at_values( op,
297                 desc->ad_type, &desc->ad_tags,
298                 vals, id, opid );
299
300         return LDAP_SUCCESS;
301 }
302
303 int
304 index_entry(
305         Operation *op,
306         int opid,
307         Entry *e )
308 {
309         Attribute *ap = e->e_attrs;
310         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
311                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
312                 e->e_id, e->e_dn );
313
314         /* add each attribute to the indexes */
315         for ( ; ap != NULL; ap = ap->a_next ) {
316                 index_values( op, ap->a_desc,
317                         ap->a_nvals,
318                         e->e_id, opid );
319         }
320
321         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
322             opid == SLAP_INDEX_ADD_OP ? "add" : "del",
323                 e->e_id, e->e_dn );
324
325         return LDAP_SUCCESS;
326 }
327