]> 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-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/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
168         assert( mask );
169
170         rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
171         
172         if ( rc != LDAP_SUCCESS ) {
173 #ifdef NEW_LOGGING
174                 LDAP_LOG( INDEX, ERR, 
175                         "bdb_index_read: Could not open DB %s\n",
176                         atname->bv_val, 0, 0 );
177 #else
178                 Debug( LDAP_DEBUG_ANY,
179                         "bdb_index_read: Could not open DB %s\n",
180                         atname->bv_val, 0, 0 );
181 #endif
182                 return LDAP_OTHER;
183         }
184
185         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
186                 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
187                 if( rc ) {
188                         goto done;
189                 }
190         }
191
192         if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
193                 rc = ad->ad_type->sat_equality->smr_indexer(
194                         LDAP_FILTER_EQUALITY,
195                         mask,
196                         ad->ad_type->sat_syntax,
197                         ad->ad_type->sat_equality,
198                         atname, vals, &keys, op->o_tmpmemctx );
199
200                 if( rc == LDAP_SUCCESS && keys != NULL ) {
201                         for( i=0; keys[i].bv_val != NULL; i++ ) {
202                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
203                                 if( rc ) {
204                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
205                                         goto done;
206                                 }
207                         }
208                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
209                 }
210                 rc = LDAP_SUCCESS;
211         }
212
213         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
214                 rc = ad->ad_type->sat_approx->smr_indexer(
215                         LDAP_FILTER_APPROX,
216                         mask,
217                         ad->ad_type->sat_syntax,
218                         ad->ad_type->sat_approx,
219                         atname, vals, &keys, op->o_tmpmemctx );
220
221                 if( rc == LDAP_SUCCESS && keys != NULL ) {
222                         for( i=0; keys[i].bv_val != NULL; i++ ) {
223                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
224                                 if( rc ) {
225                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
226                                         goto done;
227                                 }
228                         }
229                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
230                 }
231
232                 rc = LDAP_SUCCESS;
233         }
234
235         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
236                 rc = ad->ad_type->sat_substr->smr_indexer(
237                         LDAP_FILTER_SUBSTRINGS,
238                         mask,
239                         ad->ad_type->sat_syntax,
240                         ad->ad_type->sat_substr,
241                         atname, vals, &keys, op->o_tmpmemctx );
242
243                 if( rc == LDAP_SUCCESS && keys != NULL ) {
244                         for( i=0; keys[i].bv_val != NULL; i++ ) {
245                                 bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
246                                 if( rc ) {
247                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
248                                         goto done;
249                                 }
250                         }
251                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
252                 }
253
254                 rc = LDAP_SUCCESS;
255         }
256
257 done:
258         return rc;
259 }
260
261 static int index_at_values(
262         Operation *op,
263         DB_TXN *txn,
264         AttributeDescription *ad,
265         AttributeType *type,
266         struct berval *tags,
267         BerVarray vals,
268         ID id,
269         int opid )
270 {
271         int rc;
272         slap_mask_t mask = 0;
273
274         if( type->sat_sup ) {
275                 /* recurse */
276                 rc = index_at_values( op, txn, NULL,
277                         type->sat_sup, tags,
278                         vals, id, opid );
279
280                 if( rc ) return rc;
281         }
282
283         /* If this type has no AD, we've never used it before */
284         if( type->sat_ad ) {
285                 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
286                 ad = type->sat_ad;
287         }
288
289         if( mask ) {
290                 rc = indexer( op, txn, ad, &type->sat_cname,
291                         vals, id, opid,
292                         mask );
293
294                 if( rc ) return rc;
295         }
296
297         if( tags->bv_len ) {
298                 AttributeDescription *desc;
299
300                 mask = 0;
301
302                 desc = ad_find_tags( type, tags );
303                 if( desc ) {
304                         bdb_attr_mask( op->o_bd->be_private, desc, &mask );
305                 }
306
307                 if( mask ) {
308                         rc = indexer( op, txn, desc, &desc->ad_cname,
309                                 vals, id, opid,
310                                 mask );
311
312                         if( rc ) {
313                                 return rc;
314                         }
315                 }
316         }
317
318         return LDAP_SUCCESS;
319 }
320
321 int bdb_index_values(
322         Operation *op,
323         DB_TXN *txn,
324         AttributeDescription *desc,
325         BerVarray vals,
326         ID id,
327         int opid )
328 {
329         int rc;
330
331         rc = index_at_values( op, txn, desc,
332                 desc->ad_type, &desc->ad_tags,
333                 vals, id, opid );
334
335         return rc;
336 }
337
338 int
339 bdb_index_entry(
340         Operation *op,
341         DB_TXN *txn,
342         int opid,
343         Entry   *e )
344 {
345         int rc;
346         Attribute *ap = e->e_attrs;
347
348 #ifdef NEW_LOGGING
349         LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
350                 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
351 #else
352         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
353                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
354                 (long) e->e_id, e->e_dn );
355 #endif
356
357         /* add each attribute to the indexes */
358         for ( ; ap != NULL; ap = ap->a_next ) {
359                 rc = bdb_index_values( op, txn, ap->a_desc,
360                         ap->a_nvals, e->e_id, opid );
361
362                 if( rc != LDAP_SUCCESS ) {
363 #ifdef NEW_LOGGING
364                         LDAP_LOG( INDEX, ENTRY, 
365                                 "index_entry: failure (%d)\n", rc, 0, 0 );
366 #else
367                         Debug( LDAP_DEBUG_TRACE,
368                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
369                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
370                                 (long) e->e_id, e->e_dn );
371 #endif
372                         return rc;
373                 }
374         }
375
376 #ifdef NEW_LOGGING
377         LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0  );
378 #else
379         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
380                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
381                 (long) e->e_id, e->e_dn );
382 #endif
383
384         return LDAP_SUCCESS;
385 }