1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2015 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,0,0,0};
29 static struct berval presence_key[2] = {BER_BVC(presence_keyval), BER_BVNULL};
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[0];
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;
167 struct mdb_attrinfo *ai,
168 AttributeDescription *ad,
169 struct berval *atname,
177 MDB_cursor *mc = ai->ai_cursor;
178 mdb_idl_keyfunc *keyfunc;
185 rc = mdb_cursor_open( txn, ai->ai_dbi, &mc );
187 if ( slapMode & SLAP_TOOL_QUICK )
191 if ( opid == SLAP_INDEX_ADD_OP ) {
192 #ifdef MDB_TOOL_IDL_CACHING
193 if (( slapMode & SLAP_TOOL_QUICK ) && slap_tool_thread_max > 2 ) {
194 AttrIxInfo *ax = (AttrIxInfo *)LDAP_SLIST_FIRST(&op->o_extra);
196 keyfunc = mdb_tool_idl_add;
197 mc = (MDB_cursor *)ax;
200 keyfunc = mdb_idl_insert_keys;
202 keyfunc = mdb_idl_delete_keys;
204 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
205 rc = keyfunc( op->o_bd, mc, presence_key, id );
212 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
213 rc = ad->ad_type->sat_equality->smr_indexer(
214 LDAP_FILTER_EQUALITY,
216 ad->ad_type->sat_syntax,
217 ad->ad_type->sat_equality,
218 atname, vals, &keys, op->o_tmpmemctx );
220 if( rc == LDAP_SUCCESS && keys != NULL ) {
221 rc = keyfunc( op->o_bd, mc, keys, id );
222 ber_bvarray_free_x( keys, op->o_tmpmemctx );
231 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
232 rc = ad->ad_type->sat_approx->smr_indexer(
235 ad->ad_type->sat_syntax,
236 ad->ad_type->sat_approx,
237 atname, vals, &keys, op->o_tmpmemctx );
239 if( rc == LDAP_SUCCESS && keys != NULL ) {
240 rc = keyfunc( op->o_bd, mc, keys, id );
241 ber_bvarray_free_x( keys, op->o_tmpmemctx );
251 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
252 rc = ad->ad_type->sat_substr->smr_indexer(
253 LDAP_FILTER_SUBSTRINGS,
255 ad->ad_type->sat_syntax,
256 ad->ad_type->sat_substr,
257 atname, vals, &keys, op->o_tmpmemctx );
259 if( rc == LDAP_SUCCESS && keys != NULL ) {
260 rc = keyfunc( op->o_bd, mc, keys, id );
261 ber_bvarray_free_x( keys, op->o_tmpmemctx );
272 if ( !(slapMode & SLAP_TOOL_QUICK))
273 mdb_cursor_close( mc );
275 /* The callers all know how to deal with these results */
278 /* Anything else is bad news */
285 static int index_at_values(
288 AttributeDescription *ad,
296 slap_mask_t mask = 0;
300 if ( opid == MDB_INDEX_UPDATE_OP )
301 ixop = SLAP_INDEX_ADD_OP;
303 if( type->sat_sup ) {
305 rc = index_at_values( op, txn, NULL,
312 /* If this type has no AD, we've never used it before */
314 ai = mdb_attr_mask( op->o_bd->be_private, type->sat_ad );
316 #ifdef LDAP_COMP_MATCH
317 /* component indexing */
319 ComponentReference *cr;
320 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
321 rc = indexer( op, txn, ai, cr->cr_ad, &type->sat_cname,
322 cr->cr_nvals, id, ixop,
328 /* If we're updating the index, just set the new bits that aren't
329 * already in the old mask.
331 if ( opid == MDB_INDEX_UPDATE_OP )
332 mask = ai->ai_newmask & ~ai->ai_indexmask;
334 /* For regular updates, if there is a newmask use it. Otherwise
335 * just use the old mask.
337 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
339 rc = indexer( op, txn, ai, ad, &type->sat_cname,
340 vals, id, ixop, mask );
348 AttributeDescription *desc;
350 desc = ad_find_tags( type, tags );
352 ai = mdb_attr_mask( op->o_bd->be_private, desc );
355 if ( opid == MDB_INDEX_UPDATE_OP )
356 mask = ai->ai_newmask & ~ai->ai_indexmask;
358 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
360 rc = indexer( op, txn, ai, desc, &desc->ad_cname,
361 vals, id, ixop, mask );
374 int mdb_index_values(
377 AttributeDescription *desc,
384 /* Never index ID 0 */
388 rc = index_at_values( op, txn, desc,
389 desc->ad_type, &desc->ad_tags,
395 /* Get the list of which indices apply to this attr */
398 struct mdb_info *mdb,
407 if( type->sat_sup ) {
409 rc = mdb_index_recset( mdb, a, type->sat_sup, tags, ir );
412 /* If this type has no AD, we've never used it before */
414 slot = mdb_attr_slot( mdb, type->sat_ad, NULL );
416 ir[slot].ir_ai = mdb->mi_attrs[slot];
417 al = ch_malloc( sizeof( AttrList ));
419 al->next = ir[slot].ir_attrs;
420 ir[slot].ir_attrs = al;
424 AttributeDescription *desc;
426 desc = ad_find_tags( type, tags );
428 slot = mdb_attr_slot( mdb, desc, NULL );
430 ir[slot].ir_ai = mdb->mi_attrs[slot];
431 al = ch_malloc( sizeof( AttrList ));
433 al->next = ir[slot].ir_attrs;
434 ir[slot].ir_attrs = al;
441 /* Apply the indices for the recset */
442 int mdb_index_recrun(
445 struct mdb_info *mdb,
454 /* Never index ID 0 */
458 for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max-1) {
460 if ( !ir->ir_ai ) continue;
461 while (( al = ir->ir_attrs )) {
462 ir->ir_attrs = al->next;
463 rc = indexer( op, txn, ir->ir_ai, ir->ir_ai->ai_desc,
464 &ir->ir_ai->ai_desc->ad_type->sat_cname,
465 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
466 ir->ir_ai->ai_indexmask );
482 Attribute *ap = e->e_attrs;
483 #if 0 /* ifdef LDAP_COMP_MATCH */
484 ComponentReference *cr_list = NULL;
485 ComponentReference *cr = NULL, *dupped_cr = NULL;
487 ComponentSyntaxInfo* csi_attr;
492 struct berval value = {0};
495 /* Never index ID 0 */
499 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
500 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
501 (long) e->e_id, e->e_dn ? e->e_dn : "" );
503 /* add each attribute to the indexes */
504 for ( ; ap != NULL; ap = ap->a_next ) {
505 #if 0 /* ifdef LDAP_COMP_MATCH */
507 /* see if attribute has components to be indexed */
508 ai = mdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
511 if ( attr_converter && cr_list ) {
512 syn = ap->a_desc->ad_type->sat_syntax;
513 ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
514 /* Memory chunk(nibble) pre-allocation for decoders */
515 mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
516 ap->a_comp_data->cd_mem_op = mem_op;
517 for( cr = cr_list ; cr ; cr = cr->cr_next ) {
518 /* count how many values in an attribute */
519 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
521 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
522 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
523 /* decoding attribute value */
524 decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
526 return LDAP_DECODING_ERROR;
527 /* extracting the referenced component */
528 dupped_cr = dup_comp_ref( op, cr );
529 csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
531 return LDAP_DECODING_ERROR;
532 cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
533 cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
535 return LDAP_INVALID_SYNTAX;
536 at = cr->cr_ad->ad_type;
537 /* encoding the value of component in GSER */
538 rc = component_encoder( mem_op, csi_attr, &value );
539 if ( rc != LDAP_SUCCESS )
540 return LDAP_ENCODING_ERROR;
541 /* Normalize the encoded component values */
542 if ( at->sat_equality && at->sat_equality->smr_normalize ) {
543 rc = at->sat_equality->smr_normalize (
544 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
545 at->sat_syntax, at->sat_equality,
546 &value, &cr->cr_nvals[i], op->o_tmpmemctx );
548 cr->cr_nvals[i] = value;
551 /* The end of BerVarray */
552 cr->cr_nvals[num_attr-1].bv_val = NULL;
553 cr->cr_nvals[num_attr-1].bv_len = 0;
555 op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
556 nibble_mem_free ( mem_op );
557 ap->a_comp_data = NULL;
560 rc = mdb_index_values( op, txn, ap->a_desc,
561 ap->a_nvals, e->e_id, opid );
563 if( rc != LDAP_SUCCESS ) {
564 Debug( LDAP_DEBUG_TRACE,
565 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
566 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
567 (long) e->e_id, e->e_dn );
572 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
573 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
574 (long) e->e_id, e->e_dn ? e->e_dn : "" );