1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
26 #include "lutil_hash.h"
28 static char presence_keyval[LUTIL_HASH_BYTES] = {0,0,0,1};
29 static struct berval presence_key = {LUTIL_HASH_BYTES, presence_keyval};
31 static AttrInfo *index_mask(
33 AttributeDescription *desc,
34 struct berval *atname )
37 AttrInfo *ai = bdb_attr_mask( be->be_private, desc );
40 *atname = desc->ad_cname;
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.
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 );
51 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOTAGS ) ) {
52 *atname = desc->ad_type->sat_cname;
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;
62 ai = bdb_attr_mask( be->be_private, at->sat_ad );
64 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOSUBTYPES ) ) {
65 *atname = at->sat_cname;
73 int bdb_index_is_indexed(
75 AttributeDescription *desc )
80 ai = index_mask( be, desc, &prefix );
83 return LDAP_INAPPROPRIATE_MATCHING;
88 /* This function is only called when evaluating search filters.
92 AttributeDescription *desc,
96 struct berval *prefixp )
100 slap_mask_t mask, type = 0;
103 ai = index_mask( be, desc, prefixp );
106 #ifdef BDB_MONITOR_IDX
108 case LDAP_FILTER_PRESENT:
109 type = SLAP_INDEX_PRESENT;
111 case LDAP_FILTER_APPROX:
112 type = SLAP_INDEX_APPROX;
114 case LDAP_FILTER_EQUALITY:
115 type = SLAP_INDEX_EQUALITY;
117 case LDAP_FILTER_SUBSTRINGS:
118 type = SLAP_INDEX_SUBSTR;
121 return LDAP_INAPPROPRIATE_MATCHING;
123 bdb_monitor_idx_add( be->be_private, desc, type );
124 #endif /* BDB_MONITOR_IDX */
126 return LDAP_INAPPROPRIATE_MATCHING;
128 mask = ai->ai_indexmask;
130 rc = bdb_db_cache( be, prefixp->bv_val, &db );
132 if( rc != LDAP_SUCCESS ) {
137 case LDAP_FILTER_PRESENT:
138 type = SLAP_INDEX_PRESENT;
139 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
140 *prefixp = presence_key;
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 ) ) {
154 /* Use EQUALITY rule and index for approximate match */
157 case LDAP_FILTER_EQUALITY:
158 type = SLAP_INDEX_EQUALITY;
159 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
164 case LDAP_FILTER_SUBSTRINGS:
165 type = SLAP_INDEX_SUBSTR;
166 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
175 #ifdef BDB_MONITOR_IDX
176 bdb_monitor_idx_add( be->be_private, desc, type );
177 #endif /* BDB_MONITOR_IDX */
179 return LDAP_INAPPROPRIATE_MATCHING;
190 AttributeDescription *ad,
191 struct berval *atname,
203 rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
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 );
212 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
213 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
219 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
220 rc = ad->ad_type->sat_equality->smr_indexer(
221 LDAP_FILTER_EQUALITY,
223 ad->ad_type->sat_syntax,
224 ad->ad_type->sat_equality,
225 atname, vals, &keys, op->o_tmpmemctx );
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 );
231 ber_bvarray_free_x( keys, op->o_tmpmemctx );
235 ber_bvarray_free_x( keys, op->o_tmpmemctx );
240 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
241 rc = ad->ad_type->sat_approx->smr_indexer(
244 ad->ad_type->sat_syntax,
245 ad->ad_type->sat_approx,
246 atname, vals, &keys, op->o_tmpmemctx );
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 );
252 ber_bvarray_free_x( keys, op->o_tmpmemctx );
256 ber_bvarray_free_x( keys, op->o_tmpmemctx );
262 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
263 rc = ad->ad_type->sat_substr->smr_indexer(
264 LDAP_FILTER_SUBSTRINGS,
266 ad->ad_type->sat_syntax,
267 ad->ad_type->sat_substr,
268 atname, vals, &keys, op->o_tmpmemctx );
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 );
274 ber_bvarray_free_x( keys, op->o_tmpmemctx );
278 ber_bvarray_free_x( keys, op->o_tmpmemctx );
286 /* The callers all know how to deal with these results */
288 case DB_LOCK_DEADLOCK:
289 case DB_LOCK_NOTGRANTED:
291 /* Anything else is bad news */
298 static int index_at_values(
301 AttributeDescription *ad,
309 slap_mask_t mask = 0;
313 if ( opid == BDB_INDEX_UPDATE_OP )
314 ixop = SLAP_INDEX_ADD_OP;
316 if( type->sat_sup ) {
318 rc = index_at_values( op, txn, NULL,
325 /* If this type has no AD, we've never used it before */
327 ai = bdb_attr_mask( op->o_bd->be_private, type->sat_ad );
329 #ifdef LDAP_COMP_MATCH
330 /* component indexing */
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,
341 /* If we're updating the index, just set the new bits that aren't
342 * already in the old mask.
344 if ( opid == BDB_INDEX_UPDATE_OP )
345 mask = ai->ai_newmask & ~ai->ai_indexmask;
347 /* For regular updates, if there is a newmask use it. Otherwise
348 * just use the old mask.
350 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
352 rc = indexer( op, txn, ad, &type->sat_cname,
353 vals, id, ixop, mask );
361 AttributeDescription *desc;
363 desc = ad_find_tags( type, tags );
365 ai = bdb_attr_mask( op->o_bd->be_private, desc );
368 if ( opid == BDB_INDEX_UPDATE_OP )
369 mask = ai->ai_newmask & ~ai->ai_indexmask;
371 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
373 rc = indexer( op, txn, desc, &desc->ad_cname,
374 vals, id, ixop, mask );
387 int bdb_index_values(
390 AttributeDescription *desc,
397 /* Never index ID 0 */
401 rc = index_at_values( op, txn, desc,
402 desc->ad_type, &desc->ad_tags,
408 /* Get the list of which indices apply to this attr */
411 struct bdb_info *bdb,
420 if( type->sat_sup ) {
422 rc = bdb_index_recset( bdb, a, type->sat_sup, tags, ir );
425 /* If this type has no AD, we've never used it before */
427 slot = bdb_attr_slot( bdb, type->sat_ad, NULL );
429 ir[slot].ai = bdb->bi_attrs[slot];
430 al = ch_malloc( sizeof( AttrList ));
432 al->next = ir[slot].attrs;
437 AttributeDescription *desc;
439 desc = ad_find_tags( type, tags );
441 slot = bdb_attr_slot( bdb, desc, NULL );
443 ir[slot].ai = bdb->bi_attrs[slot];
444 al = ch_malloc( sizeof( AttrList ));
446 al->next = ir[slot].attrs;
454 /* Apply the indices for the recset */
455 int bdb_index_recrun(
457 struct bdb_info *bdb,
466 /* Never index ID 0 */
470 for (i=base; i<bdb->bi_nattrs; i+=slap_tool_thread_max) {
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 );
494 Attribute *ap = e->e_attrs;
495 #ifdef LDAP_COMP_MATCH
496 ComponentReference *cr_list = NULL;
497 ComponentReference *cr = NULL, *dupped_cr = NULL;
499 ComponentSyntaxInfo* csi_attr;
504 struct berval value = {0};
507 /* Never index ID 0 */
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 );
515 /* add each attribute to the indexes */
516 for ( ; ap != NULL; ap = ap->a_next ) {
517 #ifdef LDAP_COMP_MATCH
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 );
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++ );
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] );
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 );
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 );
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 );
560 cr->cr_nvals[i] = value;
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;
567 op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
568 nibble_mem_free ( mem_op );
569 ap->a_comp_data = NULL;
572 rc = bdb_index_values( op, txn, ap->a_desc,
573 ap->a_nvals, e->e_id, opid );
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 );
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 );