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[2] = {BER_BVC(presence_keyval), BER_BVNULL};
30 static MDB_val presence_mdbkey[2] = {{1, presence_keyval}, {0, NULL}};
32 AttrInfo *mdb_index_mask(
34 AttributeDescription *desc,
35 struct berval *atname )
38 AttrInfo *ai = mdb_attr_mask( be->be_private, desc );
41 *atname = desc->ad_cname;
45 /* If there is a tagging option, did we ever index the base
46 * type? If so, check for mask, otherwise it's not there.
48 if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
49 /* has tagging option */
50 ai = mdb_attr_mask( be->be_private, desc->ad_type->sat_ad );
52 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOTAGS ) ) {
53 *atname = desc->ad_type->sat_cname;
58 /* see if supertype defined mask for its subtypes */
59 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
60 /* If no AD, we've never indexed this type */
61 if ( !at->sat_ad ) continue;
63 ai = mdb_attr_mask( be->be_private, at->sat_ad );
65 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOSUBTYPES ) ) {
66 *atname = at->sat_cname;
74 /* This function is only called when evaluating search filters.
78 AttributeDescription *desc,
82 struct berval *prefixp )
85 slap_mask_t mask, type = 0;
87 ai = mdb_index_mask( be, desc, prefixp );
90 #ifdef MDB_MONITOR_IDX
92 case LDAP_FILTER_PRESENT:
93 type = SLAP_INDEX_PRESENT;
95 case LDAP_FILTER_APPROX:
96 type = SLAP_INDEX_APPROX;
98 case LDAP_FILTER_EQUALITY:
99 type = SLAP_INDEX_EQUALITY;
101 case LDAP_FILTER_SUBSTRINGS:
102 type = SLAP_INDEX_SUBSTR;
105 return LDAP_INAPPROPRIATE_MATCHING;
107 mdb_monitor_idx_add( be->be_private, desc, type );
108 #endif /* MDB_MONITOR_IDX */
110 return LDAP_INAPPROPRIATE_MATCHING;
112 mask = ai->ai_indexmask;
115 case LDAP_FILTER_PRESENT:
116 type = SLAP_INDEX_PRESENT;
117 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
118 *prefixp = presence_key[0];
123 case LDAP_FILTER_APPROX:
124 type = SLAP_INDEX_APPROX;
125 if ( desc->ad_type->sat_approx ) {
126 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
132 /* Use EQUALITY rule and index for approximate match */
135 case LDAP_FILTER_EQUALITY:
136 type = SLAP_INDEX_EQUALITY;
137 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
142 case LDAP_FILTER_SUBSTRINGS:
143 type = SLAP_INDEX_SUBSTR;
144 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
153 #ifdef MDB_MONITOR_IDX
154 mdb_monitor_idx_add( be->be_private, desc, type );
155 #endif /* MDB_MONITOR_IDX */
157 return LDAP_INAPPROPRIATE_MATCHING;
169 AttributeDescription *ad,
170 struct berval *atname,
179 mdb_idl_keyfunc *keyfunc;
185 rc = mdb_cursor_open( txn, dbi, &mc );
188 if ( opid == SLAP_INDEX_ADD_OP )
189 keyfunc = mdb_idl_insert_keys;
191 keyfunc = mdb_idl_delete_keys;
193 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
194 rc = keyfunc( mc, presence_mdbkey, id );
201 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
202 rc = ad->ad_type->sat_equality->smr_indexer(
203 LDAP_FILTER_EQUALITY,
205 ad->ad_type->sat_syntax,
206 ad->ad_type->sat_equality,
207 atname, vals, &keys, op->o_tmpmemctx );
209 if( rc == LDAP_SUCCESS && keys != NULL ) {
210 rc = keyfunc( mc, (MDB_val *)keys, id );
211 ber_bvarray_free_x( keys, op->o_tmpmemctx );
220 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
221 rc = ad->ad_type->sat_approx->smr_indexer(
224 ad->ad_type->sat_syntax,
225 ad->ad_type->sat_approx,
226 atname, vals, &keys, op->o_tmpmemctx );
228 if( rc == LDAP_SUCCESS && keys != NULL ) {
229 rc = keyfunc( mc, (MDB_val *)keys, id );
230 ber_bvarray_free_x( keys, op->o_tmpmemctx );
240 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
241 rc = ad->ad_type->sat_substr->smr_indexer(
242 LDAP_FILTER_SUBSTRINGS,
244 ad->ad_type->sat_syntax,
245 ad->ad_type->sat_substr,
246 atname, vals, &keys, op->o_tmpmemctx );
248 if( rc == LDAP_SUCCESS && keys != NULL ) {
249 rc = keyfunc( mc, (MDB_val *)keys, id );
250 ber_bvarray_free_x( keys, op->o_tmpmemctx );
261 mdb_cursor_close( mc );
263 /* The callers all know how to deal with these results */
266 /* Anything else is bad news */
273 static int index_at_values(
276 AttributeDescription *ad,
284 slap_mask_t mask = 0;
288 if ( opid == MDB_INDEX_UPDATE_OP )
289 ixop = SLAP_INDEX_ADD_OP;
291 if( type->sat_sup ) {
293 rc = index_at_values( op, txn, NULL,
300 /* If this type has no AD, we've never used it before */
302 ai = mdb_attr_mask( op->o_bd->be_private, type->sat_ad );
304 #ifdef LDAP_COMP_MATCH
305 /* component indexing */
307 ComponentReference *cr;
308 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
309 rc = indexer( op, txn, ai->ai_dbi, cr->cr_ad, &type->sat_cname,
310 cr->cr_nvals, id, ixop,
316 /* If we're updating the index, just set the new bits that aren't
317 * already in the old mask.
319 if ( opid == MDB_INDEX_UPDATE_OP )
320 mask = ai->ai_newmask & ~ai->ai_indexmask;
322 /* For regular updates, if there is a newmask use it. Otherwise
323 * just use the old mask.
325 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
327 rc = indexer( op, txn, ai->ai_dbi, ad, &type->sat_cname,
328 vals, id, ixop, mask );
336 AttributeDescription *desc;
338 desc = ad_find_tags( type, tags );
340 ai = mdb_attr_mask( op->o_bd->be_private, desc );
343 if ( opid == MDB_INDEX_UPDATE_OP )
344 mask = ai->ai_newmask & ~ai->ai_indexmask;
346 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
348 rc = indexer( op, txn, ai->ai_dbi, desc, &desc->ad_cname,
349 vals, id, ixop, mask );
362 int mdb_index_values(
365 AttributeDescription *desc,
372 /* Never index ID 0 */
376 rc = index_at_values( op, txn, desc,
377 desc->ad_type, &desc->ad_tags,
383 /* Get the list of which indices apply to this attr */
386 struct mdb_info *mdb,
395 if( type->sat_sup ) {
397 rc = mdb_index_recset( mdb, a, type->sat_sup, tags, ir );
400 /* If this type has no AD, we've never used it before */
402 slot = mdb_attr_slot( mdb, type->sat_ad, NULL );
404 ir[slot].ai = mdb->mi_attrs[slot];
405 al = ch_malloc( sizeof( AttrList ));
407 al->next = ir[slot].attrs;
412 AttributeDescription *desc;
414 desc = ad_find_tags( type, tags );
416 slot = mdb_attr_slot( mdb, desc, NULL );
418 ir[slot].ai = mdb->mi_attrs[slot];
419 al = ch_malloc( sizeof( AttrList ));
421 al->next = ir[slot].attrs;
429 /* Apply the indices for the recset */
430 int mdb_index_recrun(
432 struct mdb_info *mdb,
441 /* Never index ID 0 */
445 for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max) {
447 if ( !ir->ai ) continue;
448 while (( al = ir->attrs )) {
449 ir->attrs = al->next;
450 rc = indexer( op, NULL, ir->ai->ai_dbi, ir->ai->ai_desc,
451 &ir->ai->ai_desc->ad_type->sat_cname,
452 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
453 ir->ai->ai_indexmask );
469 Attribute *ap = e->e_attrs;
470 #if 0 /* ifdef LDAP_COMP_MATCH */
471 ComponentReference *cr_list = NULL;
472 ComponentReference *cr = NULL, *dupped_cr = NULL;
474 ComponentSyntaxInfo* csi_attr;
479 struct berval value = {0};
482 /* Never index ID 0 */
486 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
487 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
488 (long) e->e_id, e->e_dn );
490 /* add each attribute to the indexes */
491 for ( ; ap != NULL; ap = ap->a_next ) {
492 #if 0 /* ifdef LDAP_COMP_MATCH */
494 /* see if attribute has components to be indexed */
495 ai = mdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
498 if ( attr_converter && cr_list ) {
499 syn = ap->a_desc->ad_type->sat_syntax;
500 ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
501 /* Memory chunk(nibble) pre-allocation for decoders */
502 mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
503 ap->a_comp_data->cd_mem_op = mem_op;
504 for( cr = cr_list ; cr ; cr = cr->cr_next ) {
505 /* count how many values in an attribute */
506 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
508 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
509 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
510 /* decoding attribute value */
511 decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
513 return LDAP_DECODING_ERROR;
514 /* extracting the referenced component */
515 dupped_cr = dup_comp_ref( op, cr );
516 csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
518 return LDAP_DECODING_ERROR;
519 cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
520 cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
522 return LDAP_INVALID_SYNTAX;
523 at = cr->cr_ad->ad_type;
524 /* encoding the value of component in GSER */
525 rc = component_encoder( mem_op, csi_attr, &value );
526 if ( rc != LDAP_SUCCESS )
527 return LDAP_ENCODING_ERROR;
528 /* Normalize the encoded component values */
529 if ( at->sat_equality && at->sat_equality->smr_normalize ) {
530 rc = at->sat_equality->smr_normalize (
531 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
532 at->sat_syntax, at->sat_equality,
533 &value, &cr->cr_nvals[i], op->o_tmpmemctx );
535 cr->cr_nvals[i] = value;
538 /* The end of BerVarray */
539 cr->cr_nvals[num_attr-1].bv_val = NULL;
540 cr->cr_nvals[num_attr-1].bv_len = 0;
542 op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
543 nibble_mem_free ( mem_op );
544 ap->a_comp_data = NULL;
547 rc = mdb_index_values( op, txn, ap->a_desc,
548 ap->a_nvals, e->e_id, opid );
550 if( rc != LDAP_SUCCESS ) {
551 Debug( LDAP_DEBUG_TRACE,
552 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
553 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
554 (long) e->e_id, e->e_dn );
559 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
560 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
561 (long) e->e_id, e->e_dn );