]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/index.c
Sync with HEAD
[openldap] / servers / slapd / back-bdb / 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 2000-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-bdb.h"
26 #include "lutil_hash.h"
27
28 static char presence_keyval[LUTIL_HASH_BYTES] = {0,0,0,1};
29 static struct berval presence_key = {LUTIL_HASH_BYTES, presence_keyval};
30
31 static slap_mask_t index_mask(
32         Backend *be,
33         AttributeDescription *desc,
34         struct berval *atname )
35 {
36         AttributeType *at;
37         slap_mask_t mask = 0;
38
39         bdb_attr_mask( be->be_private, desc, &mask );
40
41         if( mask ) {
42                 *atname = desc->ad_cname;
43                 return mask;
44         }
45
46         /* If there is a tagging option, did we ever index the base
47          * type? If so, check for mask, otherwise it's not there.
48          */
49         if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
50                 /* has tagging option */
51                 bdb_attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
52
53                 if ( mask && ( mask ^ SLAP_INDEX_NOTAGS ) ) {
54                         *atname = desc->ad_type->sat_cname;
55                         return mask;
56                 }
57         }
58
59         /* see if supertype defined mask for its subtypes */
60         for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
61                 /* If no AD, we've never indexed this type */
62                 if ( !at->sat_ad ) continue;
63
64                 bdb_attr_mask( be->be_private, at->sat_ad, &mask );
65
66                 if ( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
67                         *atname = at->sat_cname;
68                         return mask;
69                 }
70         }
71
72         return 0;
73 }
74
75 int bdb_index_is_indexed(
76         Backend *be,
77         AttributeDescription *desc )
78 {
79         slap_mask_t mask;
80         struct berval prefix;
81
82         mask = index_mask( be, desc, &prefix );
83
84         if( mask == 0 ) {
85                 return LDAP_INAPPROPRIATE_MATCHING;
86         }
87
88         return LDAP_SUCCESS;
89 }
90
91 int bdb_index_param(
92         Backend *be,
93         AttributeDescription *desc,
94         int ftype,
95         DB **dbp,
96         slap_mask_t *maskp,
97         struct berval *prefixp )
98 {
99         int rc;
100         slap_mask_t mask;
101         DB *db;
102
103         mask = index_mask( be, desc, prefixp );
104
105         if( mask == 0 ) {
106                 return LDAP_INAPPROPRIATE_MATCHING;
107         }
108
109         rc = bdb_db_cache( be, prefixp->bv_val, &db );
110
111         if( rc != LDAP_SUCCESS ) {
112                 return rc;
113         }
114
115         switch( ftype ) {
116         case LDAP_FILTER_PRESENT:
117                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
118                         *prefixp = presence_key;
119                         goto done;
120                 }
121                 break;
122
123         case LDAP_FILTER_APPROX:
124                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
125                         goto done;
126                 }
127                 /* fall thru */
128
129         case LDAP_FILTER_EQUALITY:
130                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
131                         goto done;
132                 }
133                 break;
134
135         case LDAP_FILTER_SUBSTRINGS:
136                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
137                         goto done;
138                 }
139                 break;
140
141         default:
142                 return LDAP_OTHER;
143         }
144
145         return LDAP_INAPPROPRIATE_MATCHING;
146
147 done:
148         *dbp = db;
149         *maskp = mask;
150         return LDAP_SUCCESS;
151 }
152
153 static int indexer(
154         Operation *op,
155         DB_TXN *txn,
156         AttributeDescription *ad,
157         struct berval *atname,
158         BerVarray vals,
159         ID id,
160         int opid,
161         slap_mask_t mask )
162 {
163         int rc, i;
164         const char *text;
165         DB *db;
166         struct berval *keys;
167         void *mark;
168
169         assert( mask );
170
171         rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
172         
173         if ( rc != LDAP_SUCCESS ) {
174 #ifdef NEW_LOGGING
175                 LDAP_LOG( INDEX, ERR, 
176                         "bdb_index_read: Could not open DB %s\n",
177                         atname->bv_val, 0, 0 );
178 #else
179                 Debug( LDAP_DEBUG_ANY,
180                         "bdb_index_read: Could not open DB %s\n",
181                         atname->bv_val, 0, 0 );
182 #endif
183                 return LDAP_OTHER;
184         }
185
186 #if 0   /* No longer needed, our frees are in order so nothing accumulates */
187         mark = sl_mark(op->o_tmpmemctx);
188 #endif
189
190         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
191                 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
192                 if( rc ) {
193                         goto done;
194                 }
195         }
196
197         if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
198                 rc = ad->ad_type->sat_equality->smr_indexer(
199                         LDAP_FILTER_EQUALITY,
200                         mask,
201                         ad->ad_type->sat_syntax,
202                         ad->ad_type->sat_equality,
203                         atname, vals, &keys, op->o_tmpmemctx );
204
205                 if( rc == LDAP_SUCCESS && keys != NULL ) {
206                         for( i=0; keys[i].bv_val != NULL; i++ ) {
207                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
208                                 if( rc ) {
209                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
210                                         goto done;
211                                 }
212                         }
213                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
214                 }
215                 rc = LDAP_SUCCESS;
216         }
217
218         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
219                 rc = ad->ad_type->sat_approx->smr_indexer(
220                         LDAP_FILTER_APPROX,
221                         mask,
222                         ad->ad_type->sat_syntax,
223                         ad->ad_type->sat_approx,
224                         atname, vals, &keys, op->o_tmpmemctx );
225
226                 if( rc == LDAP_SUCCESS && keys != NULL ) {
227                         for( i=0; keys[i].bv_val != NULL; i++ ) {
228                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
229                                 if( rc ) {
230                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
231                                         goto done;
232                                 }
233                         }
234                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
235                 }
236
237                 rc = LDAP_SUCCESS;
238         }
239
240         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
241                 rc = ad->ad_type->sat_substr->smr_indexer(
242                         LDAP_FILTER_SUBSTRINGS,
243                         mask,
244                         ad->ad_type->sat_syntax,
245                         ad->ad_type->sat_substr,
246                         atname, vals, &keys, op->o_tmpmemctx );
247
248                 if( rc == LDAP_SUCCESS && keys != NULL ) {
249                         for( i=0; keys[i].bv_val != NULL; i++ ) {
250                                 bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
251                                 if( rc ) {
252                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
253                                         goto done;
254                                 }
255                         }
256                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
257                 }
258
259                 rc = LDAP_SUCCESS;
260         }
261
262 done:
263 #if 0
264         sl_release( mark, op->o_tmpmemctx );
265 #endif
266         return rc;
267 }
268
269 static int index_at_values(
270         Operation *op,
271         DB_TXN *txn,
272         AttributeDescription *ad,
273         AttributeType *type,
274         struct berval *tags,
275         BerVarray vals,
276         ID id,
277         int opid )
278 {
279         int rc;
280         slap_mask_t mask = 0;
281
282         if( type->sat_sup ) {
283                 /* recurse */
284                 rc = index_at_values( op, txn, NULL,
285                         type->sat_sup, tags,
286                         vals, id, opid );
287
288                 if( rc ) return rc;
289         }
290
291         /* If this type has no AD, we've never used it before */
292         if( type->sat_ad ) {
293                 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
294                 ad = type->sat_ad;
295         }
296
297         if( mask ) {
298                 rc = indexer( op, txn, ad, &type->sat_cname,
299                         vals, id, opid,
300                         mask );
301
302                 if( rc ) return rc;
303         }
304
305         if( tags->bv_len ) {
306                 AttributeDescription *desc;
307
308                 mask = 0;
309
310                 desc = ad_find_tags( type, tags );
311                 if( desc ) {
312                         bdb_attr_mask( op->o_bd->be_private, desc, &mask );
313                 }
314
315                 if( mask ) {
316                         rc = indexer( op, txn, desc, &desc->ad_cname,
317                                 vals, id, opid,
318                                 mask );
319
320                         if( rc ) {
321                                 return rc;
322                         }
323                 }
324         }
325
326         return LDAP_SUCCESS;
327 }
328
329 int bdb_index_values(
330         Operation *op,
331         DB_TXN *txn,
332         AttributeDescription *desc,
333         BerVarray vals,
334         ID id,
335         int opid )
336 {
337         int rc;
338
339         rc = index_at_values( op, txn, desc,
340                 desc->ad_type, &desc->ad_tags,
341                 vals, id, opid );
342
343         return rc;
344 }
345
346 int
347 bdb_index_entry(
348         Operation *op,
349         DB_TXN *txn,
350         int opid,
351         Entry   *e )
352 {
353         int rc;
354         Attribute *ap = e->e_attrs;
355
356 #ifdef NEW_LOGGING
357         LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
358                 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
359 #else
360         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
361                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
362                 (long) e->e_id, e->e_dn );
363 #endif
364
365         /* add each attribute to the indexes */
366         for ( ; ap != NULL; ap = ap->a_next ) {
367                 rc = bdb_index_values( op, txn, ap->a_desc,
368                         ap->a_nvals, e->e_id, opid );
369
370                 if( rc != LDAP_SUCCESS ) {
371 #ifdef NEW_LOGGING
372                         LDAP_LOG( INDEX, ENTRY, 
373                                 "index_entry: failure (%d)\n", rc, 0, 0 );
374 #else
375                         Debug( LDAP_DEBUG_TRACE,
376                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
377                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
378                                 (long) e->e_id, e->e_dn );
379 #endif
380                         return rc;
381                 }
382         }
383
384 #ifdef NEW_LOGGING
385         LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0  );
386 #else
387         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
388                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
389                 (long) e->e_id, e->e_dn );
390 #endif
391
392         return LDAP_SUCCESS;
393 }