]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/index.c
f468fbe2ab25ec2e3499a022282f14d75062e159
[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-2003 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( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
119                         goto done;
120                 }
121                 /* fall thru */
122
123         case LDAP_FILTER_EQUALITY:
124                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
125                         goto done;
126                 }
127                 break;
128
129         case LDAP_FILTER_SUBSTRINGS:
130                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
131                         goto done;
132                 }
133                 break;
134
135         default:
136                 return LDAP_OTHER;
137         }
138
139         return LDAP_INAPPROPRIATE_MATCHING;
140
141 done:
142         *dbnamep = dbname;
143         *maskp = mask;
144         return LDAP_SUCCESS;
145 }
146
147 static int indexer(
148         Operation *op,
149         char *dbname,
150         struct berval *atname,
151         BerVarray vals,
152         ID id,
153         int opid,
154         slap_mask_t mask )
155 {
156         int rc, i;
157         const char *text;
158     DBCache     *db;
159         AttributeDescription *ad = NULL;
160         struct berval *keys;
161         void *mark;
162
163         assert( mask );
164
165         rc = slap_bv2ad( atname, &ad, &text );
166
167         if( rc != LDAP_SUCCESS ) return rc;
168
169         db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
170         
171         if ( db == NULL ) {
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( INDEX, ERR, 
174                            "index_read: Could not open db %s%s\n", dbname, LDBM_SUFFIX, 0 );
175 #else
176                 Debug( LDAP_DEBUG_ANY,
177                     "<= index_read NULL (could not open %s%s)\n",
178                         dbname, LDBM_SUFFIX, 0 );
179 #endif
180
181                 return LDAP_OTHER;
182         }
183
184         mark = sl_mark( op->o_tmpmemctx );
185
186         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
187                 key_change( op->o_bd, db, atname, id, opid );
188         }
189
190         if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
191                 rc = ad->ad_type->sat_equality->smr_indexer(
192                         LDAP_FILTER_EQUALITY,
193                         mask,
194                         ad->ad_type->sat_syntax,
195                         ad->ad_type->sat_equality,
196                         atname, vals, &keys, op->o_tmpmemctx );
197
198                 if( rc == LDAP_SUCCESS && keys != NULL ) {
199                         for( i=0; keys[i].bv_val != NULL; i++ ) {
200                                 key_change( op->o_bd, db, &keys[i], id, opid );
201                         }
202                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
203                 }
204         }
205
206         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
207                 rc = ad->ad_type->sat_approx->smr_indexer(
208                         LDAP_FILTER_APPROX,
209                         mask,
210                         ad->ad_type->sat_syntax,
211                         ad->ad_type->sat_approx,
212                         atname, vals, &keys, op->o_tmpmemctx );
213
214                 if( rc == LDAP_SUCCESS && keys != NULL ) {
215                         for( i=0; keys[i].bv_val != NULL; i++ ) {
216                                 key_change( op->o_bd, db, &keys[i], id, opid );
217                         }
218                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
219                 }
220         }
221
222         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
223                 rc = ad->ad_type->sat_substr->smr_indexer(
224                         LDAP_FILTER_SUBSTRINGS,
225                         mask,
226                         ad->ad_type->sat_syntax,
227                         ad->ad_type->sat_substr,
228                         atname, vals, &keys, op->o_tmpmemctx );
229
230                 if( rc == LDAP_SUCCESS && keys != NULL ) {
231                         for( i=0; keys[i].bv_val != NULL; i++ ) {
232                                 key_change( op->o_bd, db, &keys[i], id, opid );
233                         }
234                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
235                 }
236         }
237
238         ldbm_cache_close( op->o_bd, db );
239
240         sl_release( mark, op->o_tmpmemctx );
241
242         return LDAP_SUCCESS;
243 }
244
245 static int index_at_values(
246         Operation *op,
247         AttributeType *type,
248         struct berval *tags,
249         BerVarray vals,
250         ID id,
251         int opid )
252 {
253         slap_mask_t mask = 0;
254
255         if( type->sat_sup ) {
256                 /* recurse */
257                 (void) index_at_values( op,
258                         type->sat_sup, tags,
259                         vals, id, opid );
260         }
261
262         /* If this type has no AD, we've never used it before */
263         if( type->sat_ad ) {
264                 attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
265         }
266
267         if( mask ) {
268                 indexer( op, type->sat_cname.bv_val,
269                         &type->sat_cname,
270                         vals, id, opid,
271                         mask );
272         }
273
274         if( tags->bv_len ) {
275                 AttributeDescription *desc;
276
277                 mask = 0;
278
279                 desc = ad_find_tags(type, tags);
280                 if( desc ) {
281                         attr_mask( op->o_bd->be_private, desc, &mask );
282                 }
283
284                 if( mask ) {
285                         indexer( op, desc->ad_cname.bv_val, &desc->ad_cname,
286                                 vals, id, opid,
287                                 mask );
288                 }
289         }
290
291         return LDAP_SUCCESS;
292 }
293
294 int index_values(
295         Operation *op,
296         AttributeDescription *desc,
297         BerVarray vals,
298         ID id,
299         int opid )
300 {
301         (void) index_at_values( op,
302                 desc->ad_type, &desc->ad_tags,
303                 vals, id, opid );
304
305         return LDAP_SUCCESS;
306 }
307
308 int
309 index_entry(
310         Operation *op,
311         int opid,
312         Entry *e )
313 {
314         Attribute *ap = e->e_attrs;
315 #ifdef NEW_LOGGING
316         LDAP_LOG( INDEX, ENTRY, 
317                 "index_entry: %s (%s)%ld\n", opid == SLAP_INDEX_ADD_OP ? "add" : "del",
318                 e->e_dn, e->e_id );
319 #else
320         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
321                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
322                 e->e_id, e->e_dn );
323 #endif
324
325         /* add each attribute to the indexes */
326         for ( ; ap != NULL; ap = ap->a_next ) {
327                 index_values( op, ap->a_desc,
328                         ap->a_nvals,
329                         e->e_id, opid );
330         }
331
332 #ifdef NEW_LOGGING
333         LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
334 #else
335         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
336             opid == SLAP_INDEX_ADD_OP ? "add" : "del",
337                 e->e_id, e->e_dn );
338 #endif
339
340         return LDAP_SUCCESS;
341 }
342