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 )
84 slap_mask_t mask, type = 0;
86 ai = mdb_index_mask( be, desc, prefixp );
89 #ifdef MDB_MONITOR_IDX
91 case LDAP_FILTER_PRESENT:
92 type = SLAP_INDEX_PRESENT;
94 case LDAP_FILTER_APPROX:
95 type = SLAP_INDEX_APPROX;
97 case LDAP_FILTER_EQUALITY:
98 type = SLAP_INDEX_EQUALITY;
100 case LDAP_FILTER_SUBSTRINGS:
101 type = SLAP_INDEX_SUBSTR;
104 return LDAP_INAPPROPRIATE_MATCHING;
106 mdb_monitor_idx_add( be->be_private, desc, type );
107 #endif /* MDB_MONITOR_IDX */
109 return LDAP_INAPPROPRIATE_MATCHING;
111 mask = ai->ai_indexmask;
114 case LDAP_FILTER_PRESENT:
115 type = SLAP_INDEX_PRESENT;
116 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
117 *prefixp = presence_key;
122 case LDAP_FILTER_APPROX:
123 type = SLAP_INDEX_APPROX;
124 if ( desc->ad_type->sat_approx ) {
125 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
131 /* Use EQUALITY rule and index for approximate match */
134 case LDAP_FILTER_EQUALITY:
135 type = SLAP_INDEX_EQUALITY;
136 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
141 case LDAP_FILTER_SUBSTRINGS:
142 type = SLAP_INDEX_SUBSTR;
143 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
152 #ifdef MDB_MONITOR_IDX
153 mdb_monitor_idx_add( be->be_private, desc, type );
154 #endif /* MDB_MONITOR_IDX */
156 return LDAP_INAPPROPRIATE_MATCHING;
168 AttributeDescription *ad,
169 struct berval *atname,
180 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
181 rc = mdb_key_change( op->o_bd, txn, dbi, &presence_key, id, opid );
187 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
188 rc = ad->ad_type->sat_equality->smr_indexer(
189 LDAP_FILTER_EQUALITY,
191 ad->ad_type->sat_syntax,
192 ad->ad_type->sat_equality,
193 atname, vals, &keys, op->o_tmpmemctx );
195 if( rc == LDAP_SUCCESS && keys != NULL ) {
196 for( i=0; keys[i].bv_val != NULL; i++ ) {
197 rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
199 ber_bvarray_free_x( keys, op->o_tmpmemctx );
203 ber_bvarray_free_x( keys, op->o_tmpmemctx );
208 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
209 rc = ad->ad_type->sat_approx->smr_indexer(
212 ad->ad_type->sat_syntax,
213 ad->ad_type->sat_approx,
214 atname, vals, &keys, op->o_tmpmemctx );
216 if( rc == LDAP_SUCCESS && keys != NULL ) {
217 for( i=0; keys[i].bv_val != NULL; i++ ) {
218 rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
220 ber_bvarray_free_x( keys, op->o_tmpmemctx );
224 ber_bvarray_free_x( keys, op->o_tmpmemctx );
230 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
231 rc = ad->ad_type->sat_substr->smr_indexer(
232 LDAP_FILTER_SUBSTRINGS,
234 ad->ad_type->sat_syntax,
235 ad->ad_type->sat_substr,
236 atname, vals, &keys, op->o_tmpmemctx );
238 if( rc == LDAP_SUCCESS && keys != NULL ) {
239 for( i=0; keys[i].bv_val != NULL; i++ ) {
240 rc = mdb_key_change( op->o_bd, txn, dbi, &keys[i], id, opid );
242 ber_bvarray_free_x( keys, op->o_tmpmemctx );
246 ber_bvarray_free_x( keys, op->o_tmpmemctx );
254 /* The callers all know how to deal with these results */
257 /* Anything else is bad news */
264 static int index_at_values(
267 AttributeDescription *ad,
275 slap_mask_t mask = 0;
279 if ( opid == MDB_INDEX_UPDATE_OP )
280 ixop = SLAP_INDEX_ADD_OP;
282 if( type->sat_sup ) {
284 rc = index_at_values( op, txn, NULL,
291 /* If this type has no AD, we've never used it before */
293 ai = mdb_attr_mask( op->o_bd->be_private, type->sat_ad );
295 #ifdef LDAP_COMP_MATCH
296 /* component indexing */
298 ComponentReference *cr;
299 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
300 rc = indexer( op, txn, ai->ai_dbi, cr->cr_ad, &type->sat_cname,
301 cr->cr_nvals, id, ixop,
307 /* If we're updating the index, just set the new bits that aren't
308 * already in the old mask.
310 if ( opid == MDB_INDEX_UPDATE_OP )
311 mask = ai->ai_newmask & ~ai->ai_indexmask;
313 /* For regular updates, if there is a newmask use it. Otherwise
314 * just use the old mask.
316 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
318 rc = indexer( op, txn, ai->ai_dbi, ad, &type->sat_cname,
319 vals, id, ixop, mask );
327 AttributeDescription *desc;
329 desc = ad_find_tags( type, tags );
331 ai = mdb_attr_mask( op->o_bd->be_private, desc );
334 if ( opid == MDB_INDEX_UPDATE_OP )
335 mask = ai->ai_newmask & ~ai->ai_indexmask;
337 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
339 rc = indexer( op, txn, ai->ai_dbi, desc, &desc->ad_cname,
340 vals, id, ixop, mask );
353 int mdb_index_values(
356 AttributeDescription *desc,
363 /* Never index ID 0 */
367 rc = index_at_values( op, txn, desc,
368 desc->ad_type, &desc->ad_tags,
374 /* Get the list of which indices apply to this attr */
377 struct mdb_info *mdb,
386 if( type->sat_sup ) {
388 rc = mdb_index_recset( mdb, a, type->sat_sup, tags, ir );
391 /* If this type has no AD, we've never used it before */
393 slot = mdb_attr_slot( mdb, type->sat_ad, NULL );
395 ir[slot].ai = mdb->mi_attrs[slot];
396 al = ch_malloc( sizeof( AttrList ));
398 al->next = ir[slot].attrs;
403 AttributeDescription *desc;
405 desc = ad_find_tags( type, tags );
407 slot = mdb_attr_slot( mdb, desc, NULL );
409 ir[slot].ai = mdb->mi_attrs[slot];
410 al = ch_malloc( sizeof( AttrList ));
412 al->next = ir[slot].attrs;
420 /* Apply the indices for the recset */
421 int mdb_index_recrun(
423 struct mdb_info *mdb,
432 /* Never index ID 0 */
436 for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max) {
438 if ( !ir->ai ) continue;
439 while (( al = ir->attrs )) {
440 ir->attrs = al->next;
441 rc = indexer( op, NULL, ir->ai->ai_dbi, ir->ai->ai_desc,
442 &ir->ai->ai_desc->ad_type->sat_cname,
443 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
444 ir->ai->ai_indexmask );
460 Attribute *ap = e->e_attrs;
461 #if 0 /* ifdef LDAP_COMP_MATCH */
462 ComponentReference *cr_list = NULL;
463 ComponentReference *cr = NULL, *dupped_cr = NULL;
465 ComponentSyntaxInfo* csi_attr;
470 struct berval value = {0};
473 /* Never index ID 0 */
477 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
478 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
479 (long) e->e_id, e->e_dn );
481 /* add each attribute to the indexes */
482 for ( ; ap != NULL; ap = ap->a_next ) {
483 #if 0 /* ifdef LDAP_COMP_MATCH */
485 /* see if attribute has components to be indexed */
486 ai = mdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
489 if ( attr_converter && cr_list ) {
490 syn = ap->a_desc->ad_type->sat_syntax;
491 ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
492 /* Memory chunk(nibble) pre-allocation for decoders */
493 mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
494 ap->a_comp_data->cd_mem_op = mem_op;
495 for( cr = cr_list ; cr ; cr = cr->cr_next ) {
496 /* count how many values in an attribute */
497 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
499 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
500 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
501 /* decoding attribute value */
502 decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
504 return LDAP_DECODING_ERROR;
505 /* extracting the referenced component */
506 dupped_cr = dup_comp_ref( op, cr );
507 csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
509 return LDAP_DECODING_ERROR;
510 cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
511 cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
513 return LDAP_INVALID_SYNTAX;
514 at = cr->cr_ad->ad_type;
515 /* encoding the value of component in GSER */
516 rc = component_encoder( mem_op, csi_attr, &value );
517 if ( rc != LDAP_SUCCESS )
518 return LDAP_ENCODING_ERROR;
519 /* Normalize the encoded component values */
520 if ( at->sat_equality && at->sat_equality->smr_normalize ) {
521 rc = at->sat_equality->smr_normalize (
522 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
523 at->sat_syntax, at->sat_equality,
524 &value, &cr->cr_nvals[i], op->o_tmpmemctx );
526 cr->cr_nvals[i] = value;
529 /* The end of BerVarray */
530 cr->cr_nvals[num_attr-1].bv_val = NULL;
531 cr->cr_nvals[num_attr-1].bv_len = 0;
533 op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
534 nibble_mem_free ( mem_op );
535 ap->a_comp_data = NULL;
538 rc = mdb_index_values( op, txn, ap->a_desc,
539 ap->a_nvals, e->e_id, opid );
541 if( rc != LDAP_SUCCESS ) {
542 Debug( LDAP_DEBUG_TRACE,
543 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
544 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
545 (long) e->e_id, e->e_dn );
550 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
551 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
552 (long) e->e_id, e->e_dn );