]> 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-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                 Debug( LDAP_DEBUG_ANY,
179                         "bdb_index_read: Could not open DB %s\n",
180                         atname->bv_val, 0, 0 );
181                 return LDAP_OTHER;
182         }
183
184         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
185                 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
186                 if( rc ) {
187                         goto done;
188                 }
189         }
190
191         if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
192                 rc = ad->ad_type->sat_equality->smr_indexer(
193                         LDAP_FILTER_EQUALITY,
194                         mask,
195                         ad->ad_type->sat_syntax,
196                         ad->ad_type->sat_equality,
197                         atname, vals, &keys, op->o_tmpmemctx );
198
199                 if( rc == LDAP_SUCCESS && keys != NULL ) {
200                         for( i=0; keys[i].bv_val != NULL; i++ ) {
201                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
202                                 if( rc ) {
203                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
204                                         goto done;
205                                 }
206                         }
207                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
208                 }
209                 rc = LDAP_SUCCESS;
210         }
211
212         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
213                 rc = ad->ad_type->sat_approx->smr_indexer(
214                         LDAP_FILTER_APPROX,
215                         mask,
216                         ad->ad_type->sat_syntax,
217                         ad->ad_type->sat_approx,
218                         atname, vals, &keys, op->o_tmpmemctx );
219
220                 if( rc == LDAP_SUCCESS && keys != NULL ) {
221                         for( i=0; keys[i].bv_val != NULL; i++ ) {
222                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
223                                 if( rc ) {
224                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
225                                         goto done;
226                                 }
227                         }
228                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
229                 }
230
231                 rc = LDAP_SUCCESS;
232         }
233
234         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
235                 rc = ad->ad_type->sat_substr->smr_indexer(
236                         LDAP_FILTER_SUBSTRINGS,
237                         mask,
238                         ad->ad_type->sat_syntax,
239                         ad->ad_type->sat_substr,
240                         atname, vals, &keys, op->o_tmpmemctx );
241
242                 if( rc == LDAP_SUCCESS && keys != NULL ) {
243                         for( i=0; keys[i].bv_val != NULL; i++ ) {
244                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
245                                 if( rc ) {
246                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
247                                         goto done;
248                                 }
249                         }
250                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
251                 }
252
253                 rc = LDAP_SUCCESS;
254         }
255
256 done:
257         return rc;
258 }
259
260 static int index_at_values(
261         Operation *op,
262         DB_TXN *txn,
263         AttributeDescription *ad,
264         AttributeType *type,
265         struct berval *tags,
266         BerVarray vals,
267         ID id,
268         int opid )
269 {
270         int rc;
271         slap_mask_t mask = 0;
272 #ifdef LDAP_COMP_MATCH
273         ComponentReference* cr_list, *cr;
274         AttributeDescription *comp_ad;
275 #endif
276
277         if( type->sat_sup ) {
278                 /* recurse */
279                 rc = index_at_values( op, txn, NULL,
280                         type->sat_sup, tags,
281                         vals, id, opid );
282
283                 if( rc ) return rc;
284         }
285
286         /* If this type has no AD, we've never used it before */
287         if( type->sat_ad ) {
288 #ifdef LDAP_COMP_MATCH
289                 /* component indexing */
290                 bdb_attr_mask_cr( op->o_bd->be_private, type->sat_ad, &mask, &cr_list );
291                 if ( cr_list ) {
292                         for( cr = cr_list ; cr ; cr = cr->cr_next ) {
293                                 rc = indexer( op, txn, cr->cr_ad, &type->sat_cname,
294                                         cr->cr_nvals, id, opid,
295                                         cr->cr_indexmask );
296                         }
297                 }
298 #else
299                 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
300 #endif
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 #ifdef LDAP_COMP_MATCH
363         ComponentReference *cr_list = NULL;
364         ComponentReference *cr = NULL, *dupped_cr = NULL;
365         void* decoded_comp, *extracted_comp;
366         ComponentSyntaxInfo* csi_attr;
367         Syntax* syn;
368         AttributeType* at;
369         int i, num_attr;
370         void* mem_op;
371         struct berval value = {0};
372 #endif
373
374         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
375                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
376                 (long) e->e_id, e->e_dn );
377
378         /* add each attribute to the indexes */
379         for ( ; ap != NULL; ap = ap->a_next ) {
380 #ifdef LDAP_COMP_MATCH
381                 /* see if attribute has components to be indexed */
382                 bdb_attr_comp_ref( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad, &cr_list );
383                 if ( attr_converter && cr_list ) {
384                         syn = ap->a_desc->ad_type->sat_syntax;
385                         ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
386                         /* Memory chunk(nibble) pre-allocation for decoders */
387                         mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
388                         ap->a_comp_data->cd_mem_op = mem_op;
389                         for( cr = cr_list ; cr ; cr = cr->cr_next ) {
390                                 /* count how many values in an attribute */
391                                 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
392                                 num_attr++;
393                                 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
394                                 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
395                                         /* decoding attribute value */
396                                         decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
397                                         if ( !decoded_comp )
398                                                 return LDAP_DECODING_ERROR;
399                                         /* extracting the referenced component */
400                                         dupped_cr = dup_comp_ref( op, cr );
401                                         csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
402                                         if ( !csi_attr )
403                                                 return LDAP_DECODING_ERROR;
404                                         cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
405                                         cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
406                                         if ( !cr->cr_ad )
407                                                 return LDAP_INVALID_SYNTAX;
408                                         at = cr->cr_ad->ad_type;
409                                         /* encoding the value of component in GSER */
410                                         rc = component_encoder( mem_op, csi_attr, &value );
411                                         if ( rc != LDAP_SUCCESS )
412                                                 return LDAP_ENCODING_ERROR;
413                                         /* Normalize the encoded component values */
414                                         if ( at->sat_equality && at->sat_equality->smr_normalize ) {
415                                                 rc = at->sat_equality->smr_normalize (
416                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
417                                                         at->sat_syntax, at->sat_equality,
418                                                         &value, &cr->cr_nvals[i], op->o_tmpmemctx );
419                                         } else {
420                                                 cr->cr_nvals[i] = value;
421                                         }
422                                 }
423                                 /* The end of BerVarray */
424                                 cr->cr_nvals[num_attr-1].bv_val = NULL;
425                                 cr->cr_nvals[num_attr-1].bv_len = 0;
426                         }
427                         op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
428                         nibble_mem_free ( mem_op );
429                         ap->a_comp_data = NULL;
430                 }
431 #endif
432                 rc = bdb_index_values( op, txn, ap->a_desc,
433                         ap->a_nvals, e->e_id, opid );
434
435                 if( rc != LDAP_SUCCESS ) {
436                         Debug( LDAP_DEBUG_TRACE,
437                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
438                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
439                                 (long) e->e_id, e->e_dn );
440                         return rc;
441                 }
442         }
443
444         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
445                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
446                 (long) e->e_id, e->e_dn );
447
448         return LDAP_SUCCESS;
449 }