]> 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 AttrInfo *index_mask(
32         Backend *be,
33         AttributeDescription *desc,
34         struct berval *atname )
35 {
36         AttributeType *at;
37         AttrInfo *ai = bdb_attr_mask( be->be_private, desc );
38
39         if( ai ) {
40                 *atname = desc->ad_cname;
41                 return ai;
42         }
43
44         /* If there is a tagging option, did we ever index the base
45          * type? If so, check for mask, otherwise it's not there.
46          */
47         if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
48                 /* has tagging option */
49                 ai = bdb_attr_mask( be->be_private, desc->ad_type->sat_ad );
50
51                 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOTAGS ) ) {
52                         *atname = desc->ad_type->sat_cname;
53                         return ai;
54                 }
55         }
56
57         /* see if supertype defined mask for its subtypes */
58         for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
59                 /* If no AD, we've never indexed this type */
60                 if ( !at->sat_ad ) continue;
61
62                 ai = bdb_attr_mask( be->be_private, at->sat_ad );
63
64                 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOSUBTYPES ) ) {
65                         *atname = at->sat_cname;
66                         return ai;
67                 }
68         }
69
70         return 0;
71 }
72
73 int bdb_index_is_indexed(
74         Backend *be,
75         AttributeDescription *desc )
76 {
77         AttrInfo *ai;
78         struct berval prefix;
79
80         ai = index_mask( be, desc, &prefix );
81
82         if( !ai )
83                 return LDAP_INAPPROPRIATE_MATCHING;
84
85         return LDAP_SUCCESS;
86 }
87
88 /* This function is only called when evaluating search filters.
89  */
90 int bdb_index_param(
91         Backend *be,
92         AttributeDescription *desc,
93         int ftype,
94         DB **dbp,
95         slap_mask_t *maskp,
96         struct berval *prefixp )
97 {
98         AttrInfo *ai;
99         int rc;
100         slap_mask_t mask;
101         DB *db;
102
103         ai = index_mask( be, desc, prefixp );
104
105         if( !ai ) {
106                 return LDAP_INAPPROPRIATE_MATCHING;
107         }
108         mask = ai->ai_indexmask;
109
110         rc = bdb_db_cache( be, prefixp->bv_val, &db );
111
112         if( rc != LDAP_SUCCESS ) {
113                 return rc;
114         }
115
116         switch( ftype ) {
117         case LDAP_FILTER_PRESENT:
118                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
119                         *prefixp = presence_key;
120                         goto done;
121                 }
122                 break;
123
124         case LDAP_FILTER_APPROX:
125                 if ( desc->ad_type->sat_approx ) {
126                         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
127                                 goto done;
128                         }
129                         break;
130                 }
131
132                 /* Use EQUALITY rule and index for approximate match */
133                 /* fall thru */
134
135         case LDAP_FILTER_EQUALITY:
136                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
137                         goto done;
138                 }
139                 break;
140
141         case LDAP_FILTER_SUBSTRINGS:
142                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
143                         goto done;
144                 }
145                 break;
146
147         default:
148                 return LDAP_OTHER;
149         }
150
151         return LDAP_INAPPROPRIATE_MATCHING;
152
153 done:
154         *dbp = db;
155         *maskp = mask;
156         return LDAP_SUCCESS;
157 }
158
159 static int indexer(
160         Operation *op,
161         DB_TXN *txn,
162         AttributeDescription *ad,
163         struct berval *atname,
164         BerVarray vals,
165         ID id,
166         int opid,
167         slap_mask_t mask )
168 {
169         int rc, i;
170         DB *db;
171         struct berval *keys;
172
173         assert( mask != 0 );
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         switch( rc ) {
258         /* The callers all know how to deal with these results */
259         case 0:
260         case DB_LOCK_DEADLOCK:
261         case DB_LOCK_NOTGRANTED:
262                 break;
263         /* Anything else is bad news */
264         default:
265                 rc = LDAP_OTHER;
266         }
267         return rc;
268 }
269
270 static int index_at_values(
271         Operation *op,
272         DB_TXN *txn,
273         AttributeDescription *ad,
274         AttributeType *type,
275         struct berval *tags,
276         BerVarray vals,
277         ID id,
278         int opid )
279 {
280         int rc;
281         slap_mask_t mask = 0;
282         int ixop = opid;
283         AttrInfo *ai = NULL;
284
285         if ( opid == BDB_INDEX_UPDATE_OP )
286                 ixop = SLAP_INDEX_ADD_OP;
287
288         if( type->sat_sup ) {
289                 /* recurse */
290                 rc = index_at_values( op, txn, NULL,
291                         type->sat_sup, tags,
292                         vals, id, opid );
293
294                 if( rc ) return rc;
295         }
296
297         /* If this type has no AD, we've never used it before */
298         if( type->sat_ad ) {
299                 ai = bdb_attr_mask( op->o_bd->be_private, type->sat_ad );
300                 if ( ai ) {
301 #ifdef LDAP_COMP_MATCH
302                         /* component indexing */
303                         if ( ai->ai_cr ) {
304                                 ComponentReference *cr;
305                                 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
306                                         rc = indexer( op, txn, cr->cr_ad, &type->sat_cname,
307                                                 cr->cr_nvals, id, ixop,
308                                                 cr->cr_indexmask );
309                                 }
310                         }
311 #endif
312                         ad = type->sat_ad;
313                         /* If we're updating the index, just set the new bits that aren't
314                          * already in the old mask.
315                          */
316                         if ( opid == BDB_INDEX_UPDATE_OP )
317                                 mask = ai->ai_newmask & ~ai->ai_indexmask;
318                         else
319                         /* For regular updates, if there is a newmask use it. Otherwise
320                          * just use the old mask.
321                          */
322                                 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
323                         if( mask ) {
324                                 rc = indexer( op, txn, ad, &type->sat_cname,
325                                         vals, id, ixop, mask );
326
327                                 if( rc ) return rc;
328                         }
329                 }
330         }
331
332         if( tags->bv_len ) {
333                 AttributeDescription *desc;
334
335                 desc = ad_find_tags( type, tags );
336                 if( desc ) {
337                         ai = bdb_attr_mask( op->o_bd->be_private, desc );
338
339                         if( ai ) {
340                                 if ( opid == BDB_INDEX_UPDATE_OP )
341                                         mask = ai->ai_newmask & ~ai->ai_indexmask;
342                                 else
343                                         mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
344                                 if ( mask ) {
345                                         rc = indexer( op, txn, desc, &desc->ad_cname,
346                                                 vals, id, ixop, mask );
347
348                                         if( rc ) {
349                                                 return rc;
350                                         }
351                                 }
352                         }
353                 }
354         }
355
356         return LDAP_SUCCESS;
357 }
358
359 int bdb_index_values(
360         Operation *op,
361         DB_TXN *txn,
362         AttributeDescription *desc,
363         BerVarray vals,
364         ID id,
365         int opid )
366 {
367         int rc;
368
369         /* Never index ID 0 */
370         if ( id == 0 )
371                 return 0;
372
373         rc = index_at_values( op, txn, desc,
374                 desc->ad_type, &desc->ad_tags,
375                 vals, id, opid );
376
377         return rc;
378 }
379
380 /* Get the list of which indices apply to this attr */
381 int
382 bdb_index_recset(
383         struct bdb_info *bdb,
384         Attribute *a,
385         AttributeType *type,
386         struct berval *tags,
387         IndexRec *ir )
388 {
389         int rc, slot;
390         AttrList *al;
391
392         if( type->sat_sup ) {
393                 /* recurse */
394                 rc = bdb_index_recset( bdb, a, type->sat_sup, tags, ir );
395                 if( rc ) return rc;
396         }
397         /* If this type has no AD, we've never used it before */
398         if( type->sat_ad ) {
399                 slot = bdb_attr_slot( bdb, type->sat_ad, NULL );
400                 if ( slot >= 0 ) {
401                         ir[slot].ai = bdb->bi_attrs[slot];
402                         al = ch_malloc( sizeof( AttrList ));
403                         al->attr = a;
404                         al->next = ir[slot].attrs;
405                         ir[slot].attrs = al;
406                 }
407         }
408         if( tags->bv_len ) {
409                 AttributeDescription *desc;
410
411                 desc = ad_find_tags( type, tags );
412                 if( desc ) {
413                         slot = bdb_attr_slot( bdb, desc, NULL );
414                         if ( slot >= 0 ) {
415                                 ir[slot].ai = bdb->bi_attrs[slot];
416                                 al = ch_malloc( sizeof( AttrList ));
417                                 al->attr = a;
418                                 al->next = ir[slot].attrs;
419                                 ir[slot].attrs = al;
420                         }
421                 }
422         }
423         return LDAP_SUCCESS;
424 }
425
426 /* Apply the indices for the recset */
427 int bdb_index_recrun(
428         Operation *op,
429         struct bdb_info *bdb,
430         IndexRec *ir0,
431         ID id,
432         int base )
433 {
434         IndexRec *ir;
435         AttrList *al;
436         int i, rc = 0;
437
438         for (i=base; i<bdb->bi_nattrs; i+=slap_tool_thread_max) {
439                 ir = ir0 + i;
440                 if ( !ir->ai ) continue;
441                 while (( al = ir->attrs )) {
442                         ir->attrs = al->next;
443                         rc = indexer( op, NULL, ir->ai->ai_desc,
444                                 &ir->ai->ai_desc->ad_type->sat_cname,
445                                 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
446                                 ir->ai->ai_indexmask );
447                         free( al );
448                         if ( rc ) break;
449                 }
450         }
451         return rc;
452 }
453
454 int
455 bdb_index_entry(
456         Operation *op,
457         DB_TXN *txn,
458         int opid,
459         Entry   *e )
460 {
461         int rc;
462         Attribute *ap = e->e_attrs;
463 #ifdef LDAP_COMP_MATCH
464         ComponentReference *cr_list = NULL;
465         ComponentReference *cr = NULL, *dupped_cr = NULL;
466         void* decoded_comp;
467         ComponentSyntaxInfo* csi_attr;
468         Syntax* syn;
469         AttributeType* at;
470         int i, num_attr;
471         void* mem_op;
472         struct berval value = {0};
473 #endif
474
475         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
476                 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
477                 (long) e->e_id, e->e_dn );
478
479         /* add each attribute to the indexes */
480         for ( ; ap != NULL; ap = ap->a_next ) {
481 #ifdef LDAP_COMP_MATCH
482                 AttrInfo *ai;
483                 /* see if attribute has components to be indexed */
484                 ai = bdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
485                 if ( ai ) cr_list = ai->ai_cr;
486                 else cr_list = NULL;
487                 if ( attr_converter && cr_list ) {
488                         syn = ap->a_desc->ad_type->sat_syntax;
489                         ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
490                         /* Memory chunk(nibble) pre-allocation for decoders */
491                         mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
492                         ap->a_comp_data->cd_mem_op = mem_op;
493                         for( cr = cr_list ; cr ; cr = cr->cr_next ) {
494                                 /* count how many values in an attribute */
495                                 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
496                                 num_attr++;
497                                 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
498                                 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
499                                         /* decoding attribute value */
500                                         decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
501                                         if ( !decoded_comp )
502                                                 return LDAP_DECODING_ERROR;
503                                         /* extracting the referenced component */
504                                         dupped_cr = dup_comp_ref( op, cr );
505                                         csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
506                                         if ( !csi_attr )
507                                                 return LDAP_DECODING_ERROR;
508                                         cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
509                                         cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
510                                         if ( !cr->cr_ad )
511                                                 return LDAP_INVALID_SYNTAX;
512                                         at = cr->cr_ad->ad_type;
513                                         /* encoding the value of component in GSER */
514                                         rc = component_encoder( mem_op, csi_attr, &value );
515                                         if ( rc != LDAP_SUCCESS )
516                                                 return LDAP_ENCODING_ERROR;
517                                         /* Normalize the encoded component values */
518                                         if ( at->sat_equality && at->sat_equality->smr_normalize ) {
519                                                 rc = at->sat_equality->smr_normalize (
520                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
521                                                         at->sat_syntax, at->sat_equality,
522                                                         &value, &cr->cr_nvals[i], op->o_tmpmemctx );
523                                         } else {
524                                                 cr->cr_nvals[i] = value;
525                                         }
526                                 }
527                                 /* The end of BerVarray */
528                                 cr->cr_nvals[num_attr-1].bv_val = NULL;
529                                 cr->cr_nvals[num_attr-1].bv_len = 0;
530                         }
531                         op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
532                         nibble_mem_free ( mem_op );
533                         ap->a_comp_data = NULL;
534                 }
535 #endif
536                 rc = bdb_index_values( op, txn, ap->a_desc,
537                         ap->a_nvals, e->e_id, opid );
538
539                 if( rc != LDAP_SUCCESS ) {
540                         Debug( LDAP_DEBUG_TRACE,
541                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
542                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
543                                 (long) e->e_id, e->e_dn );
544                         return rc;
545                 }
546         }
547
548         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
549                 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
550                 (long) e->e_id, e->e_dn );
551
552         return LDAP_SUCCESS;
553 }