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};
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;
168 AttributeDescription *ad,
169 struct berval *atname,
178 mdb_idl_keyfunc *keyfunc;
184 rc = mdb_cursor_open( txn, dbi, &mc );
187 if ( opid == SLAP_INDEX_ADD_OP )
188 keyfunc = mdb_idl_insert_keys;
190 keyfunc = mdb_idl_delete_keys;
192 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
193 rc = keyfunc( mc, (MDB_val *)presence_key, id );
200 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
201 rc = ad->ad_type->sat_equality->smr_indexer(
202 LDAP_FILTER_EQUALITY,
204 ad->ad_type->sat_syntax,
205 ad->ad_type->sat_equality,
206 atname, vals, &keys, op->o_tmpmemctx );
208 if( rc == LDAP_SUCCESS && keys != NULL ) {
209 rc = keyfunc( mc, (MDB_val *)keys, id );
210 ber_bvarray_free_x( keys, op->o_tmpmemctx );
219 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
220 rc = ad->ad_type->sat_approx->smr_indexer(
223 ad->ad_type->sat_syntax,
224 ad->ad_type->sat_approx,
225 atname, vals, &keys, op->o_tmpmemctx );
227 if( rc == LDAP_SUCCESS && keys != NULL ) {
228 rc = keyfunc( mc, (MDB_val *)keys, id );
229 ber_bvarray_free_x( keys, op->o_tmpmemctx );
239 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
240 rc = ad->ad_type->sat_substr->smr_indexer(
241 LDAP_FILTER_SUBSTRINGS,
243 ad->ad_type->sat_syntax,
244 ad->ad_type->sat_substr,
245 atname, vals, &keys, op->o_tmpmemctx );
247 if( rc == LDAP_SUCCESS && keys != NULL ) {
248 rc = keyfunc( mc, (MDB_val *)keys, id );
249 ber_bvarray_free_x( keys, op->o_tmpmemctx );
260 mdb_cursor_close( mc );
262 /* The callers all know how to deal with these results */
265 /* Anything else is bad news */
272 static int index_at_values(
275 AttributeDescription *ad,
283 slap_mask_t mask = 0;
287 if ( opid == MDB_INDEX_UPDATE_OP )
288 ixop = SLAP_INDEX_ADD_OP;
290 if( type->sat_sup ) {
292 rc = index_at_values( op, txn, NULL,
299 /* If this type has no AD, we've never used it before */
301 ai = mdb_attr_mask( op->o_bd->be_private, type->sat_ad );
303 #ifdef LDAP_COMP_MATCH
304 /* component indexing */
306 ComponentReference *cr;
307 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
308 rc = indexer( op, txn, ai->ai_dbi, cr->cr_ad, &type->sat_cname,
309 cr->cr_nvals, id, ixop,
315 /* If we're updating the index, just set the new bits that aren't
316 * already in the old mask.
318 if ( opid == MDB_INDEX_UPDATE_OP )
319 mask = ai->ai_newmask & ~ai->ai_indexmask;
321 /* For regular updates, if there is a newmask use it. Otherwise
322 * just use the old mask.
324 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
326 rc = indexer( op, txn, ai->ai_dbi, ad, &type->sat_cname,
327 vals, id, ixop, mask );
335 AttributeDescription *desc;
337 desc = ad_find_tags( type, tags );
339 ai = mdb_attr_mask( op->o_bd->be_private, desc );
342 if ( opid == MDB_INDEX_UPDATE_OP )
343 mask = ai->ai_newmask & ~ai->ai_indexmask;
345 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
347 rc = indexer( op, txn, ai->ai_dbi, desc, &desc->ad_cname,
348 vals, id, ixop, mask );
361 int mdb_index_values(
364 AttributeDescription *desc,
371 /* Never index ID 0 */
375 rc = index_at_values( op, txn, desc,
376 desc->ad_type, &desc->ad_tags,
382 /* Get the list of which indices apply to this attr */
385 struct mdb_info *mdb,
394 if( type->sat_sup ) {
396 rc = mdb_index_recset( mdb, a, type->sat_sup, tags, ir );
399 /* If this type has no AD, we've never used it before */
401 slot = mdb_attr_slot( mdb, type->sat_ad, NULL );
403 ir[slot].ai = mdb->mi_attrs[slot];
404 al = ch_malloc( sizeof( AttrList ));
406 al->next = ir[slot].attrs;
411 AttributeDescription *desc;
413 desc = ad_find_tags( type, tags );
415 slot = mdb_attr_slot( mdb, desc, NULL );
417 ir[slot].ai = mdb->mi_attrs[slot];
418 al = ch_malloc( sizeof( AttrList ));
420 al->next = ir[slot].attrs;
428 /* Apply the indices for the recset */
429 int mdb_index_recrun(
431 struct mdb_info *mdb,
440 /* Never index ID 0 */
444 for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max) {
446 if ( !ir->ai ) continue;
447 while (( al = ir->attrs )) {
448 ir->attrs = al->next;
449 rc = indexer( op, NULL, ir->ai->ai_dbi, ir->ai->ai_desc,
450 &ir->ai->ai_desc->ad_type->sat_cname,
451 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
452 ir->ai->ai_indexmask );
468 Attribute *ap = e->e_attrs;
469 #if 0 /* ifdef LDAP_COMP_MATCH */
470 ComponentReference *cr_list = NULL;
471 ComponentReference *cr = NULL, *dupped_cr = NULL;
473 ComponentSyntaxInfo* csi_attr;
478 struct berval value = {0};
481 /* Never index ID 0 */
485 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
486 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
487 (long) e->e_id, e->e_dn );
489 /* add each attribute to the indexes */
490 for ( ; ap != NULL; ap = ap->a_next ) {
491 #if 0 /* ifdef LDAP_COMP_MATCH */
493 /* see if attribute has components to be indexed */
494 ai = mdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
497 if ( attr_converter && cr_list ) {
498 syn = ap->a_desc->ad_type->sat_syntax;
499 ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
500 /* Memory chunk(nibble) pre-allocation for decoders */
501 mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
502 ap->a_comp_data->cd_mem_op = mem_op;
503 for( cr = cr_list ; cr ; cr = cr->cr_next ) {
504 /* count how many values in an attribute */
505 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
507 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
508 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
509 /* decoding attribute value */
510 decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
512 return LDAP_DECODING_ERROR;
513 /* extracting the referenced component */
514 dupped_cr = dup_comp_ref( op, cr );
515 csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
517 return LDAP_DECODING_ERROR;
518 cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
519 cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
521 return LDAP_INVALID_SYNTAX;
522 at = cr->cr_ad->ad_type;
523 /* encoding the value of component in GSER */
524 rc = component_encoder( mem_op, csi_attr, &value );
525 if ( rc != LDAP_SUCCESS )
526 return LDAP_ENCODING_ERROR;
527 /* Normalize the encoded component values */
528 if ( at->sat_equality && at->sat_equality->smr_normalize ) {
529 rc = at->sat_equality->smr_normalize (
530 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
531 at->sat_syntax, at->sat_equality,
532 &value, &cr->cr_nvals[i], op->o_tmpmemctx );
534 cr->cr_nvals[i] = value;
537 /* The end of BerVarray */
538 cr->cr_nvals[num_attr-1].bv_val = NULL;
539 cr->cr_nvals[num_attr-1].bv_len = 0;
541 op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
542 nibble_mem_free ( mem_op );
543 ap->a_comp_data = NULL;
546 rc = mdb_index_values( op, txn, ap->a_desc,
547 ap->a_nvals, e->e_id, opid );
549 if( rc != LDAP_SUCCESS ) {
550 Debug( LDAP_DEBUG_TRACE,
551 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
552 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
553 (long) e->e_id, e->e_dn );
558 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
559 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
560 (long) e->e_id, e->e_dn );