]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/index.c
db mismatch (including patch mismatch) should be an error
[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         DB *db;
170         struct berval *keys;
171
172         assert( mask );
173
174         rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
175         
176         if ( rc != LDAP_SUCCESS ) {
177                 Debug( LDAP_DEBUG_ANY,
178                         "bdb_index_read: Could not open DB %s\n",
179                         atname->bv_val, 0, 0 );
180                 return LDAP_OTHER;
181         }
182
183         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
184                 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
185                 if( rc ) {
186                         goto done;
187                 }
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                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
201                                 if( rc ) {
202                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
203                                         goto done;
204                                 }
205                         }
206                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
207                 }
208                 rc = LDAP_SUCCESS;
209         }
210
211         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
212                 rc = ad->ad_type->sat_approx->smr_indexer(
213                         LDAP_FILTER_APPROX,
214                         mask,
215                         ad->ad_type->sat_syntax,
216                         ad->ad_type->sat_approx,
217                         atname, vals, &keys, op->o_tmpmemctx );
218
219                 if( rc == LDAP_SUCCESS && keys != NULL ) {
220                         for( i=0; keys[i].bv_val != NULL; i++ ) {
221                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
222                                 if( rc ) {
223                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
224                                         goto done;
225                                 }
226                         }
227                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
228                 }
229
230                 rc = LDAP_SUCCESS;
231         }
232
233         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
234                 rc = ad->ad_type->sat_substr->smr_indexer(
235                         LDAP_FILTER_SUBSTRINGS,
236                         mask,
237                         ad->ad_type->sat_syntax,
238                         ad->ad_type->sat_substr,
239                         atname, vals, &keys, op->o_tmpmemctx );
240
241                 if( rc == LDAP_SUCCESS && keys != NULL ) {
242                         for( i=0; keys[i].bv_val != NULL; i++ ) {
243                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
244                                 if( rc ) {
245                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
246                                         goto done;
247                                 }
248                         }
249                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
250                 }
251
252                 rc = LDAP_SUCCESS;
253         }
254
255 done:
256         return rc;
257 }
258
259 static int index_at_values(
260         Operation *op,
261         DB_TXN *txn,
262         AttributeDescription *ad,
263         AttributeType *type,
264         struct berval *tags,
265         BerVarray vals,
266         ID id,
267         int opid )
268 {
269         int rc;
270         slap_mask_t mask = 0;
271
272         if( type->sat_sup ) {
273                 /* recurse */
274                 rc = index_at_values( op, txn, NULL,
275                         type->sat_sup, tags,
276                         vals, id, opid );
277
278                 if( rc ) return rc;
279         }
280
281         /* If this type has no AD, we've never used it before */
282         if( type->sat_ad ) {
283 #ifdef LDAP_COMP_MATCH
284                 /* component indexing */
285                 ComponentReference* cr_list, *cr;
286
287                 bdb_attr_mask_cr( op->o_bd->be_private, type->sat_ad, &mask, &cr_list );
288                 if ( cr_list ) {
289                         for( cr = cr_list ; cr ; cr = cr->cr_next ) {
290                                 rc = indexer( op, txn, cr->cr_ad, &type->sat_cname,
291                                         cr->cr_nvals, id, opid,
292                                         cr->cr_indexmask );
293                         }
294                 }
295 #else
296                 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
297 #endif
298                 ad = type->sat_ad;
299         }
300
301         if( mask ) {
302                 rc = indexer( op, txn, ad, &type->sat_cname,
303                         vals, id, opid,
304                         mask );
305
306                 if( rc ) return rc;
307         }
308
309         if( tags->bv_len ) {
310                 AttributeDescription *desc;
311
312                 mask = 0;
313
314                 desc = ad_find_tags( type, tags );
315                 if( desc ) {
316                         bdb_attr_mask( op->o_bd->be_private, desc, &mask );
317                 }
318
319                 if( mask ) {
320                         rc = indexer( op, txn, desc, &desc->ad_cname,
321                                 vals, id, opid,
322                                 mask );
323
324                         if( rc ) {
325                                 return rc;
326                         }
327                 }
328         }
329
330         return LDAP_SUCCESS;
331 }
332
333 int bdb_index_values(
334         Operation *op,
335         DB_TXN *txn,
336         AttributeDescription *desc,
337         BerVarray vals,
338         ID id,
339         int opid )
340 {
341         int rc;
342
343         rc = index_at_values( op, txn, desc,
344                 desc->ad_type, &desc->ad_tags,
345                 vals, id, opid );
346
347         return rc;
348 }
349
350 int
351 bdb_index_entry(
352         Operation *op,
353         DB_TXN *txn,
354         int opid,
355         Entry   *e )
356 {
357         int rc;
358         Attribute *ap = e->e_attrs;
359 #ifdef LDAP_COMP_MATCH
360         ComponentReference *cr_list = NULL;
361         ComponentReference *cr = NULL, *dupped_cr = NULL;
362         void* decoded_comp, *extracted_comp;
363         ComponentSyntaxInfo* csi_attr;
364         Syntax* syn;
365         AttributeType* at;
366         int i, num_attr;
367         void* mem_op;
368         struct berval value = {0};
369 #endif
370
371         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
372                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
373                 (long) e->e_id, e->e_dn );
374
375         /* add each attribute to the indexes */
376         for ( ; ap != NULL; ap = ap->a_next ) {
377 #ifdef LDAP_COMP_MATCH
378                 /* see if attribute has components to be indexed */
379                 bdb_attr_comp_ref( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad, &cr_list );
380                 if ( attr_converter && cr_list ) {
381                         syn = ap->a_desc->ad_type->sat_syntax;
382                         ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
383                         /* Memory chunk(nibble) pre-allocation for decoders */
384                         mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
385                         ap->a_comp_data->cd_mem_op = mem_op;
386                         for( cr = cr_list ; cr ; cr = cr->cr_next ) {
387                                 /* count how many values in an attribute */
388                                 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
389                                 num_attr++;
390                                 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
391                                 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
392                                         /* decoding attribute value */
393                                         decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
394                                         if ( !decoded_comp )
395                                                 return LDAP_DECODING_ERROR;
396                                         /* extracting the referenced component */
397                                         dupped_cr = dup_comp_ref( op, cr );
398                                         csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
399                                         if ( !csi_attr )
400                                                 return LDAP_DECODING_ERROR;
401                                         cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
402                                         cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
403                                         if ( !cr->cr_ad )
404                                                 return LDAP_INVALID_SYNTAX;
405                                         at = cr->cr_ad->ad_type;
406                                         /* encoding the value of component in GSER */
407                                         rc = component_encoder( mem_op, csi_attr, &value );
408                                         if ( rc != LDAP_SUCCESS )
409                                                 return LDAP_ENCODING_ERROR;
410                                         /* Normalize the encoded component values */
411                                         if ( at->sat_equality && at->sat_equality->smr_normalize ) {
412                                                 rc = at->sat_equality->smr_normalize (
413                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
414                                                         at->sat_syntax, at->sat_equality,
415                                                         &value, &cr->cr_nvals[i], op->o_tmpmemctx );
416                                         } else {
417                                                 cr->cr_nvals[i] = value;
418                                         }
419                                 }
420                                 /* The end of BerVarray */
421                                 cr->cr_nvals[num_attr-1].bv_val = NULL;
422                                 cr->cr_nvals[num_attr-1].bv_len = 0;
423                         }
424                         op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
425                         nibble_mem_free ( mem_op );
426                         ap->a_comp_data = NULL;
427                 }
428 #endif
429                 rc = bdb_index_values( op, txn, ap->a_desc,
430                         ap->a_nvals, e->e_id, opid );
431
432                 if( rc != LDAP_SUCCESS ) {
433                         Debug( LDAP_DEBUG_TRACE,
434                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
435                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
436                                 (long) e->e_id, e->e_dn );
437                         return rc;
438                 }
439         }
440
441         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
442                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
443                 (long) e->e_id, e->e_dn );
444
445         return LDAP_SUCCESS;
446 }