]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/index.c
Import ITS#3731 fix from 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-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-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 ( desc->ad_type->sat_approx ) {
125                         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
126                                 goto done;
127                         }
128                         break;
129                 }
130
131                 /* Use EQUALITY rule and index for approximate match */
132                 /* fall thru */
133
134         case LDAP_FILTER_EQUALITY:
135                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
136                         goto done;
137                 }
138                 break;
139
140         case LDAP_FILTER_SUBSTRINGS:
141                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
142                         goto done;
143                 }
144                 break;
145
146         default:
147                 return LDAP_OTHER;
148         }
149
150         return LDAP_INAPPROPRIATE_MATCHING;
151
152 done:
153         *dbp = db;
154         *maskp = mask;
155         return LDAP_SUCCESS;
156 }
157
158 static int indexer(
159         Operation *op,
160         DB_TXN *txn,
161         AttributeDescription *ad,
162         struct berval *atname,
163         BerVarray vals,
164         ID id,
165         int opid,
166         slap_mask_t mask )
167 {
168         int rc, i;
169         const char *text;
170         DB *db;
171         struct berval *keys;
172
173         assert( mask );
174
175         rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
176         
177         if ( rc != LDAP_SUCCESS ) {
178 #ifdef NEW_LOGGING
179                 LDAP_LOG( INDEX, ERR, 
180                         "bdb_index_read: Could not open DB %s\n",
181                         atname->bv_val, 0, 0 );
182 #else
183                 Debug( LDAP_DEBUG_ANY,
184                         "bdb_index_read: Could not open DB %s\n",
185                         atname->bv_val, 0, 0 );
186 #endif
187                 return LDAP_OTHER;
188         }
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                                 rc = 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         switch( rc ) {
264         /* The callers all know how to deal with these results */
265         case 0:
266         case DB_LOCK_DEADLOCK:
267         case DB_LOCK_NOTGRANTED:
268                 break;
269         /* Anything else is bad news */
270         default:
271                 rc = LDAP_OTHER;
272         }
273         return rc;
274 }
275
276 static int index_at_values(
277         Operation *op,
278         DB_TXN *txn,
279         AttributeDescription *ad,
280         AttributeType *type,
281         struct berval *tags,
282         BerVarray vals,
283         ID id,
284         int opid )
285 {
286         int rc;
287         slap_mask_t mask = 0;
288
289         if( type->sat_sup ) {
290                 /* recurse */
291                 rc = index_at_values( op, txn, NULL,
292                         type->sat_sup, tags,
293                         vals, id, opid );
294
295                 if( rc ) return rc;
296         }
297
298         /* If this type has no AD, we've never used it before */
299         if( type->sat_ad ) {
300                 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
301                 ad = type->sat_ad;
302         }
303
304         if( mask ) {
305                 rc = indexer( op, txn, ad, &type->sat_cname,
306                         vals, id, opid,
307                         mask );
308
309                 if( rc ) return rc;
310         }
311
312         if( tags->bv_len ) {
313                 AttributeDescription *desc;
314
315                 mask = 0;
316
317                 desc = ad_find_tags( type, tags );
318                 if( desc ) {
319                         bdb_attr_mask( op->o_bd->be_private, desc, &mask );
320                 }
321
322                 if( mask ) {
323                         rc = indexer( op, txn, desc, &desc->ad_cname,
324                                 vals, id, opid,
325                                 mask );
326
327                         if( rc ) {
328                                 return rc;
329                         }
330                 }
331         }
332
333         return LDAP_SUCCESS;
334 }
335
336 int bdb_index_values(
337         Operation *op,
338         DB_TXN *txn,
339         AttributeDescription *desc,
340         BerVarray vals,
341         ID id,
342         int opid )
343 {
344         int rc;
345
346         rc = index_at_values( op, txn, desc,
347                 desc->ad_type, &desc->ad_tags,
348                 vals, id, opid );
349
350         return rc;
351 }
352
353 int
354 bdb_index_entry(
355         Operation *op,
356         DB_TXN *txn,
357         int opid,
358         Entry   *e )
359 {
360         int rc;
361         Attribute *ap = e->e_attrs;
362
363 #ifdef NEW_LOGGING
364         LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
365                 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
366 #else
367         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
368                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
369                 (long) e->e_id, e->e_dn );
370 #endif
371
372         /* add each attribute to the indexes */
373         for ( ; ap != NULL; ap = ap->a_next ) {
374                 rc = bdb_index_values( op, txn, ap->a_desc,
375                         ap->a_nvals, e->e_id, opid );
376
377                 if( rc != LDAP_SUCCESS ) {
378 #ifdef NEW_LOGGING
379                         LDAP_LOG( INDEX, ENTRY, 
380                                 "index_entry: failure (%d)\n", rc, 0, 0 );
381 #else
382                         Debug( LDAP_DEBUG_TRACE,
383                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
384                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
385                                 (long) e->e_id, e->e_dn );
386 #endif
387                         return rc;
388                 }
389         }
390
391 #ifdef NEW_LOGGING
392         LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0  );
393 #else
394         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
395                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
396                 (long) e->e_id, e->e_dn );
397 #endif
398
399         return LDAP_SUCCESS;
400 }