]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/index.c
Hide log schema
[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-2007 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, type = 0;
101         DB *db;
102
103         ai = index_mask( be, desc, prefixp );
104
105         if ( !ai ) {
106 #ifdef BDB_MONITOR_IDX
107                 switch ( ftype ) {
108                 case LDAP_FILTER_PRESENT:
109                         type = SLAP_INDEX_PRESENT;
110                         break;
111                 case LDAP_FILTER_APPROX:
112                         type = SLAP_INDEX_APPROX;
113                         break;
114                 case LDAP_FILTER_EQUALITY:
115                         type = SLAP_INDEX_EQUALITY;
116                         break;
117                 case LDAP_FILTER_SUBSTRINGS:
118                         type = SLAP_INDEX_SUBSTR;
119                         break;
120                 default:
121                         return LDAP_INAPPROPRIATE_MATCHING;
122                 }
123                 bdb_monitor_idx_add( be->be_private, desc, type );
124 #endif /* BDB_MONITOR_IDX */
125
126                 return LDAP_INAPPROPRIATE_MATCHING;
127         }
128         mask = ai->ai_indexmask;
129
130         rc = bdb_db_cache( be, prefixp->bv_val, &db );
131
132         if( rc != LDAP_SUCCESS ) {
133                 return rc;
134         }
135
136         switch( ftype ) {
137         case LDAP_FILTER_PRESENT:
138                 type = SLAP_INDEX_PRESENT;
139                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
140                         *prefixp = presence_key;
141                         goto done;
142                 }
143                 break;
144
145         case LDAP_FILTER_APPROX:
146                 type = SLAP_INDEX_APPROX;
147                 if ( desc->ad_type->sat_approx ) {
148                         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
149                                 goto done;
150                         }
151                         break;
152                 }
153
154                 /* Use EQUALITY rule and index for approximate match */
155                 /* fall thru */
156
157         case LDAP_FILTER_EQUALITY:
158                 type = SLAP_INDEX_EQUALITY;
159                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
160                         goto done;
161                 }
162                 break;
163
164         case LDAP_FILTER_SUBSTRINGS:
165                 type = SLAP_INDEX_SUBSTR;
166                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
167                         goto done;
168                 }
169                 break;
170
171         default:
172                 return LDAP_OTHER;
173         }
174
175 #ifdef BDB_MONITOR_IDX
176         bdb_monitor_idx_add( be->be_private, desc, type );
177 #endif /* BDB_MONITOR_IDX */
178
179         return LDAP_INAPPROPRIATE_MATCHING;
180
181 done:
182         *dbp = db;
183         *maskp = mask;
184         return LDAP_SUCCESS;
185 }
186
187 static int indexer(
188         Operation *op,
189         DB_TXN *txn,
190         AttributeDescription *ad,
191         struct berval *atname,
192         BerVarray vals,
193         ID id,
194         int opid,
195         slap_mask_t mask )
196 {
197         int rc, i;
198         DB *db;
199         struct berval *keys;
200
201         assert( mask != 0 );
202
203         rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
204         
205         if ( rc != LDAP_SUCCESS ) {
206                 Debug( LDAP_DEBUG_ANY,
207                         "bdb_index_read: Could not open DB %s\n",
208                         atname->bv_val, 0, 0 );
209                 return LDAP_OTHER;
210         }
211
212         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
213                 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
214                 if( rc ) {
215                         goto done;
216                 }
217         }
218
219         if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
220                 rc = ad->ad_type->sat_equality->smr_indexer(
221                         LDAP_FILTER_EQUALITY,
222                         mask,
223                         ad->ad_type->sat_syntax,
224                         ad->ad_type->sat_equality,
225                         atname, vals, &keys, op->o_tmpmemctx );
226
227                 if( rc == LDAP_SUCCESS && keys != NULL ) {
228                         for( i=0; keys[i].bv_val != NULL; i++ ) {
229                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
230                                 if( rc ) {
231                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
232                                         goto done;
233                                 }
234                         }
235                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
236                 }
237                 rc = LDAP_SUCCESS;
238         }
239
240         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
241                 rc = ad->ad_type->sat_approx->smr_indexer(
242                         LDAP_FILTER_APPROX,
243                         mask,
244                         ad->ad_type->sat_syntax,
245                         ad->ad_type->sat_approx,
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         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
263                 rc = ad->ad_type->sat_substr->smr_indexer(
264                         LDAP_FILTER_SUBSTRINGS,
265                         mask,
266                         ad->ad_type->sat_syntax,
267                         ad->ad_type->sat_substr,
268                         atname, vals, &keys, op->o_tmpmemctx );
269
270                 if( rc == LDAP_SUCCESS && keys != NULL ) {
271                         for( i=0; keys[i].bv_val != NULL; i++ ) {
272                                 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
273                                 if( rc ) {
274                                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
275                                         goto done;
276                                 }
277                         }
278                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
279                 }
280
281                 rc = LDAP_SUCCESS;
282         }
283
284 done:
285         switch( rc ) {
286         /* The callers all know how to deal with these results */
287         case 0:
288         case DB_LOCK_DEADLOCK:
289         case DB_LOCK_NOTGRANTED:
290                 break;
291         /* Anything else is bad news */
292         default:
293                 rc = LDAP_OTHER;
294         }
295         return rc;
296 }
297
298 static int index_at_values(
299         Operation *op,
300         DB_TXN *txn,
301         AttributeDescription *ad,
302         AttributeType *type,
303         struct berval *tags,
304         BerVarray vals,
305         ID id,
306         int opid )
307 {
308         int rc;
309         slap_mask_t mask = 0;
310         int ixop = opid;
311         AttrInfo *ai = NULL;
312
313         if ( opid == BDB_INDEX_UPDATE_OP )
314                 ixop = SLAP_INDEX_ADD_OP;
315
316         if( type->sat_sup ) {
317                 /* recurse */
318                 rc = index_at_values( op, txn, NULL,
319                         type->sat_sup, tags,
320                         vals, id, opid );
321
322                 if( rc ) return rc;
323         }
324
325         /* If this type has no AD, we've never used it before */
326         if( type->sat_ad ) {
327                 ai = bdb_attr_mask( op->o_bd->be_private, type->sat_ad );
328                 if ( ai ) {
329 #ifdef LDAP_COMP_MATCH
330                         /* component indexing */
331                         if ( ai->ai_cr ) {
332                                 ComponentReference *cr;
333                                 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
334                                         rc = indexer( op, txn, cr->cr_ad, &type->sat_cname,
335                                                 cr->cr_nvals, id, ixop,
336                                                 cr->cr_indexmask );
337                                 }
338                         }
339 #endif
340                         ad = type->sat_ad;
341                         /* If we're updating the index, just set the new bits that aren't
342                          * already in the old mask.
343                          */
344                         if ( opid == BDB_INDEX_UPDATE_OP )
345                                 mask = ai->ai_newmask & ~ai->ai_indexmask;
346                         else
347                         /* For regular updates, if there is a newmask use it. Otherwise
348                          * just use the old mask.
349                          */
350                                 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
351                         if( mask ) {
352                                 rc = indexer( op, txn, ad, &type->sat_cname,
353                                         vals, id, ixop, mask );
354
355                                 if( rc ) return rc;
356                         }
357                 }
358         }
359
360         if( tags->bv_len ) {
361                 AttributeDescription *desc;
362
363                 desc = ad_find_tags( type, tags );
364                 if( desc ) {
365                         ai = bdb_attr_mask( op->o_bd->be_private, desc );
366
367                         if( ai ) {
368                                 if ( opid == BDB_INDEX_UPDATE_OP )
369                                         mask = ai->ai_newmask & ~ai->ai_indexmask;
370                                 else
371                                         mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
372                                 if ( mask ) {
373                                         rc = indexer( op, txn, desc, &desc->ad_cname,
374                                                 vals, id, ixop, mask );
375
376                                         if( rc ) {
377                                                 return rc;
378                                         }
379                                 }
380                         }
381                 }
382         }
383
384         return LDAP_SUCCESS;
385 }
386
387 int bdb_index_values(
388         Operation *op,
389         DB_TXN *txn,
390         AttributeDescription *desc,
391         BerVarray vals,
392         ID id,
393         int opid )
394 {
395         int rc;
396
397         /* Never index ID 0 */
398         if ( id == 0 )
399                 return 0;
400
401         rc = index_at_values( op, txn, desc,
402                 desc->ad_type, &desc->ad_tags,
403                 vals, id, opid );
404
405         return rc;
406 }
407
408 /* Get the list of which indices apply to this attr */
409 int
410 bdb_index_recset(
411         struct bdb_info *bdb,
412         Attribute *a,
413         AttributeType *type,
414         struct berval *tags,
415         IndexRec *ir )
416 {
417         int rc, slot;
418         AttrList *al;
419
420         if( type->sat_sup ) {
421                 /* recurse */
422                 rc = bdb_index_recset( bdb, a, type->sat_sup, tags, ir );
423                 if( rc ) return rc;
424         }
425         /* If this type has no AD, we've never used it before */
426         if( type->sat_ad ) {
427                 slot = bdb_attr_slot( bdb, type->sat_ad, NULL );
428                 if ( slot >= 0 ) {
429                         ir[slot].ai = bdb->bi_attrs[slot];
430                         al = ch_malloc( sizeof( AttrList ));
431                         al->attr = a;
432                         al->next = ir[slot].attrs;
433                         ir[slot].attrs = al;
434                 }
435         }
436         if( tags->bv_len ) {
437                 AttributeDescription *desc;
438
439                 desc = ad_find_tags( type, tags );
440                 if( desc ) {
441                         slot = bdb_attr_slot( bdb, desc, NULL );
442                         if ( slot >= 0 ) {
443                                 ir[slot].ai = bdb->bi_attrs[slot];
444                                 al = ch_malloc( sizeof( AttrList ));
445                                 al->attr = a;
446                                 al->next = ir[slot].attrs;
447                                 ir[slot].attrs = al;
448                         }
449                 }
450         }
451         return LDAP_SUCCESS;
452 }
453
454 /* Apply the indices for the recset */
455 int bdb_index_recrun(
456         Operation *op,
457         struct bdb_info *bdb,
458         IndexRec *ir0,
459         ID id,
460         int base )
461 {
462         IndexRec *ir;
463         AttrList *al;
464         int i, rc = 0;
465
466         /* Never index ID 0 */
467         if ( id == 0 )
468                 return 0;
469
470         for (i=base; i<bdb->bi_nattrs; i+=slap_tool_thread_max) {
471                 ir = ir0 + i;
472                 if ( !ir->ai ) continue;
473                 while (( al = ir->attrs )) {
474                         ir->attrs = al->next;
475                         rc = indexer( op, NULL, ir->ai->ai_desc,
476                                 &ir->ai->ai_desc->ad_type->sat_cname,
477                                 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
478                                 ir->ai->ai_indexmask );
479                         free( al );
480                         if ( rc ) break;
481                 }
482         }
483         return rc;
484 }
485
486 int
487 bdb_index_entry(
488         Operation *op,
489         DB_TXN *txn,
490         int opid,
491         Entry   *e )
492 {
493         int rc;
494         Attribute *ap = e->e_attrs;
495 #ifdef LDAP_COMP_MATCH
496         ComponentReference *cr_list = NULL;
497         ComponentReference *cr = NULL, *dupped_cr = NULL;
498         void* decoded_comp;
499         ComponentSyntaxInfo* csi_attr;
500         Syntax* syn;
501         AttributeType* at;
502         int i, num_attr;
503         void* mem_op;
504         struct berval value = {0};
505 #endif
506
507         /* Never index ID 0 */
508         if ( e->e_id == 0 )
509                 return 0;
510
511         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
512                 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
513                 (long) e->e_id, e->e_dn );
514
515         /* add each attribute to the indexes */
516         for ( ; ap != NULL; ap = ap->a_next ) {
517 #ifdef LDAP_COMP_MATCH
518                 AttrInfo *ai;
519                 /* see if attribute has components to be indexed */
520                 ai = bdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
521                 if ( !ai ) continue;
522                 cr_list = ai->ai_cr;
523                 if ( attr_converter && cr_list ) {
524                         syn = ap->a_desc->ad_type->sat_syntax;
525                         ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
526                         /* Memory chunk(nibble) pre-allocation for decoders */
527                         mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
528                         ap->a_comp_data->cd_mem_op = mem_op;
529                         for( cr = cr_list ; cr ; cr = cr->cr_next ) {
530                                 /* count how many values in an attribute */
531                                 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
532                                 num_attr++;
533                                 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
534                                 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
535                                         /* decoding attribute value */
536                                         decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
537                                         if ( !decoded_comp )
538                                                 return LDAP_DECODING_ERROR;
539                                         /* extracting the referenced component */
540                                         dupped_cr = dup_comp_ref( op, cr );
541                                         csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
542                                         if ( !csi_attr )
543                                                 return LDAP_DECODING_ERROR;
544                                         cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
545                                         cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
546                                         if ( !cr->cr_ad )
547                                                 return LDAP_INVALID_SYNTAX;
548                                         at = cr->cr_ad->ad_type;
549                                         /* encoding the value of component in GSER */
550                                         rc = component_encoder( mem_op, csi_attr, &value );
551                                         if ( rc != LDAP_SUCCESS )
552                                                 return LDAP_ENCODING_ERROR;
553                                         /* Normalize the encoded component values */
554                                         if ( at->sat_equality && at->sat_equality->smr_normalize ) {
555                                                 rc = at->sat_equality->smr_normalize (
556                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
557                                                         at->sat_syntax, at->sat_equality,
558                                                         &value, &cr->cr_nvals[i], op->o_tmpmemctx );
559                                         } else {
560                                                 cr->cr_nvals[i] = value;
561                                         }
562                                 }
563                                 /* The end of BerVarray */
564                                 cr->cr_nvals[num_attr-1].bv_val = NULL;
565                                 cr->cr_nvals[num_attr-1].bv_len = 0;
566                         }
567                         op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
568                         nibble_mem_free ( mem_op );
569                         ap->a_comp_data = NULL;
570                 }
571 #endif
572                 rc = bdb_index_values( op, txn, ap->a_desc,
573                         ap->a_nvals, e->e_id, opid );
574
575                 if( rc != LDAP_SUCCESS ) {
576                         Debug( LDAP_DEBUG_TRACE,
577                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
578                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
579                                 (long) e->e_id, e->e_dn );
580                         return rc;
581                 }
582         }
583
584         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
585                 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
586                 (long) e->e_id, e->e_dn );
587
588         return LDAP_SUCCESS;
589 }