1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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[] = {0,0};
29 static struct berval presence_key = BER_BVC(presence_keyval);
31 AttrInfo *mdb_index_mask(
33 AttributeDescription *desc,
34 struct berval *atname )
37 AttrInfo *ai = mdb_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 = mdb_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 = mdb_attr_mask( be->be_private, at->sat_ad );
64 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOSUBTYPES ) ) {
65 *atname = at->sat_cname;
73 /* This function is only called when evaluating search filters.
77 AttributeDescription *desc,
81 struct berval *prefixp )
85 slap_mask_t mask, type = 0;
88 ai = mdb_index_mask( be, desc, prefixp );
91 #ifdef MDB_MONITOR_IDX
93 case LDAP_FILTER_PRESENT:
94 type = SLAP_INDEX_PRESENT;
96 case LDAP_FILTER_APPROX:
97 type = SLAP_INDEX_APPROX;
99 case LDAP_FILTER_EQUALITY:
100 type = SLAP_INDEX_EQUALITY;
102 case LDAP_FILTER_SUBSTRINGS:
103 type = SLAP_INDEX_SUBSTR;
106 return LDAP_INAPPROPRIATE_MATCHING;
108 mdb_monitor_idx_add( be->be_private, desc, type );
109 #endif /* MDB_MONITOR_IDX */
111 return LDAP_INAPPROPRIATE_MATCHING;
113 mask = ai->ai_indexmask;
115 rc = mdb_db_cache( be, prefixp, &dbi );
117 if( rc != LDAP_SUCCESS ) {
122 case LDAP_FILTER_PRESENT:
123 type = SLAP_INDEX_PRESENT;
124 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
125 *prefixp = presence_key;
130 case LDAP_FILTER_APPROX:
131 type = SLAP_INDEX_APPROX;
132 if ( desc->ad_type->sat_approx ) {
133 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
139 /* Use EQUALITY rule and index for approximate match */
142 case LDAP_FILTER_EQUALITY:
143 type = SLAP_INDEX_EQUALITY;
144 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
149 case LDAP_FILTER_SUBSTRINGS:
150 type = SLAP_INDEX_SUBSTR;
151 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
160 #ifdef MDB_MONITOR_IDX
161 mdb_monitor_idx_add( be->be_private, desc, type );
162 #endif /* MDB_MONITOR_IDX */
164 return LDAP_INAPPROPRIATE_MATCHING;
175 AttributeDescription *ad,
176 struct berval *atname,
188 rc = mdb_db_cache( op->o_bd, atname, &dbi );
190 if ( rc != LDAP_SUCCESS ) {
191 Debug( LDAP_DEBUG_ANY,
192 "mdb_index_read: Could not open DB %s\n",
193 atname->bv_val, 0, 0 );
197 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
198 rc = mdb_key_change( op->o_bd, txn, dbi, &presence_key, id, opid );
204 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
205 rc = ad->ad_type->sat_equality->smr_indexer(
206 LDAP_FILTER_EQUALITY,
208 ad->ad_type->sat_syntax,
209 ad->ad_type->sat_equality,
210 atname, vals, &keys, op->o_tmpmemctx );
212 if( rc == LDAP_SUCCESS && keys != NULL ) {
213 for( i=0; keys[i].bv_val != NULL; i++ ) {
214 rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
216 ber_bvarray_free_x( keys, op->o_tmpmemctx );
220 ber_bvarray_free_x( keys, op->o_tmpmemctx );
225 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
226 rc = ad->ad_type->sat_approx->smr_indexer(
229 ad->ad_type->sat_syntax,
230 ad->ad_type->sat_approx,
231 atname, vals, &keys, op->o_tmpmemctx );
233 if( rc == LDAP_SUCCESS && keys != NULL ) {
234 for( i=0; keys[i].bv_val != NULL; i++ ) {
235 rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
237 ber_bvarray_free_x( keys, op->o_tmpmemctx );
241 ber_bvarray_free_x( keys, op->o_tmpmemctx );
247 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
248 rc = ad->ad_type->sat_substr->smr_indexer(
249 LDAP_FILTER_SUBSTRINGS,
251 ad->ad_type->sat_syntax,
252 ad->ad_type->sat_substr,
253 atname, vals, &keys, op->o_tmpmemctx );
255 if( rc == LDAP_SUCCESS && keys != NULL ) {
256 for( i=0; keys[i].bv_val != NULL; i++ ) {
257 rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
259 ber_bvarray_free_x( keys, op->o_tmpmemctx );
263 ber_bvarray_free_x( keys, op->o_tmpmemctx );
271 /* The callers all know how to deal with these results */
274 /* Anything else is bad news */
281 static int index_at_values(
284 AttributeDescription *ad,
292 slap_mask_t mask = 0;
296 if ( opid == MDB_INDEX_UPDATE_OP )
297 ixop = SLAP_INDEX_ADD_OP;
299 if( type->sat_sup ) {
301 rc = index_at_values( op, txn, NULL,
308 /* If this type has no AD, we've never used it before */
310 ai = mdb_attr_mask( op->o_bd->be_private, type->sat_ad );
312 #ifdef LDAP_COMP_MATCH
313 /* component indexing */
315 ComponentReference *cr;
316 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
317 rc = indexer( op, txn, cr->cr_ad, &type->sat_cname,
318 cr->cr_nvals, id, ixop,
324 /* If we're updating the index, just set the new bits that aren't
325 * already in the old mask.
327 if ( opid == MDB_INDEX_UPDATE_OP )
328 mask = ai->ai_newmask & ~ai->ai_indexmask;
330 /* For regular updates, if there is a newmask use it. Otherwise
331 * just use the old mask.
333 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
335 rc = indexer( op, txn, ad, &type->sat_cname,
336 vals, id, ixop, mask );
344 AttributeDescription *desc;
346 desc = ad_find_tags( type, tags );
348 ai = mdb_attr_mask( op->o_bd->be_private, desc );
351 if ( opid == MDB_INDEX_UPDATE_OP )
352 mask = ai->ai_newmask & ~ai->ai_indexmask;
354 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
356 rc = indexer( op, txn, desc, &desc->ad_cname,
357 vals, id, ixop, mask );
370 int mdb_index_values(
373 AttributeDescription *desc,
380 /* Never index ID 0 */
384 rc = index_at_values( op, txn, desc,
385 desc->ad_type, &desc->ad_tags,
391 /* Get the list of which indices apply to this attr */
394 struct mdb_info *mdb,
403 if( type->sat_sup ) {
405 rc = mdb_index_recset( mdb, a, type->sat_sup, tags, ir );
408 /* If this type has no AD, we've never used it before */
410 slot = mdb_attr_slot( mdb, type->sat_ad, NULL );
412 ir[slot].ai = mdb->mi_attrs[slot];
413 al = ch_malloc( sizeof( AttrList ));
415 al->next = ir[slot].attrs;
420 AttributeDescription *desc;
422 desc = ad_find_tags( type, tags );
424 slot = mdb_attr_slot( mdb, desc, NULL );
426 ir[slot].ai = mdb->mi_attrs[slot];
427 al = ch_malloc( sizeof( AttrList ));
429 al->next = ir[slot].attrs;
437 /* Apply the indices for the recset */
438 int mdb_index_recrun(
440 struct mdb_info *mdb,
449 /* Never index ID 0 */
453 for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max) {
455 if ( !ir->ai ) continue;
456 while (( al = ir->attrs )) {
457 ir->attrs = al->next;
458 rc = indexer( op, NULL, ir->ai->ai_desc,
459 &ir->ai->ai_desc->ad_type->sat_cname,
460 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
461 ir->ai->ai_indexmask );
477 Attribute *ap = e->e_attrs;
478 #if 0 /* ifdef LDAP_COMP_MATCH */
479 ComponentReference *cr_list = NULL;
480 ComponentReference *cr = NULL, *dupped_cr = NULL;
482 ComponentSyntaxInfo* csi_attr;
487 struct berval value = {0};
490 /* Never index ID 0 */
494 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
495 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
496 (long) e->e_id, e->e_dn );
498 /* add each attribute to the indexes */
499 for ( ; ap != NULL; ap = ap->a_next ) {
500 #if 0 /* ifdef LDAP_COMP_MATCH */
502 /* see if attribute has components to be indexed */
503 ai = mdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
506 if ( attr_converter && cr_list ) {
507 syn = ap->a_desc->ad_type->sat_syntax;
508 ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
509 /* Memory chunk(nibble) pre-allocation for decoders */
510 mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
511 ap->a_comp_data->cd_mem_op = mem_op;
512 for( cr = cr_list ; cr ; cr = cr->cr_next ) {
513 /* count how many values in an attribute */
514 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
516 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
517 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
518 /* decoding attribute value */
519 decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
521 return LDAP_DECODING_ERROR;
522 /* extracting the referenced component */
523 dupped_cr = dup_comp_ref( op, cr );
524 csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
526 return LDAP_DECODING_ERROR;
527 cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
528 cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
530 return LDAP_INVALID_SYNTAX;
531 at = cr->cr_ad->ad_type;
532 /* encoding the value of component in GSER */
533 rc = component_encoder( mem_op, csi_attr, &value );
534 if ( rc != LDAP_SUCCESS )
535 return LDAP_ENCODING_ERROR;
536 /* Normalize the encoded component values */
537 if ( at->sat_equality && at->sat_equality->smr_normalize ) {
538 rc = at->sat_equality->smr_normalize (
539 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
540 at->sat_syntax, at->sat_equality,
541 &value, &cr->cr_nvals[i], op->o_tmpmemctx );
543 cr->cr_nvals[i] = value;
546 /* The end of BerVarray */
547 cr->cr_nvals[num_attr-1].bv_val = NULL;
548 cr->cr_nvals[num_attr-1].bv_len = 0;
550 op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
551 nibble_mem_free ( mem_op );
552 ap->a_comp_data = NULL;
555 rc = mdb_index_values( op, txn, ap->a_desc,
556 ap->a_nvals, e->e_id, opid );
558 if( rc != LDAP_SUCCESS ) {
559 Debug( LDAP_DEBUG_TRACE,
560 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
561 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
562 (long) e->e_id, e->e_dn );
567 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
568 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
569 (long) e->e_id, e->e_dn );