1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 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[LUTIL_HASH_BYTES] = {0,0,0,1};
29 static struct berval presence_key = {LUTIL_HASH_BYTES, presence_keyval};
31 static AttrInfo *index_mask(
33 AttributeDescription *desc,
34 struct berval *atname )
37 AttrInfo *ai = bdb_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 = bdb_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 = bdb_attr_mask( be->be_private, at->sat_ad );
64 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOSUBTYPES ) ) {
65 *atname = at->sat_cname;
73 int bdb_index_is_indexed(
75 AttributeDescription *desc )
80 ai = index_mask( be, desc, &prefix );
83 return LDAP_INAPPROPRIATE_MATCHING;
88 /* This function is only called when evaluating search filters.
92 AttributeDescription *desc,
96 struct berval *prefixp )
103 ai = index_mask( be, desc, prefixp );
106 return LDAP_INAPPROPRIATE_MATCHING;
108 mask = ai->ai_indexmask;
110 rc = bdb_db_cache( be, prefixp->bv_val, &db );
112 if( rc != LDAP_SUCCESS ) {
117 case LDAP_FILTER_PRESENT:
118 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
119 *prefixp = presence_key;
124 case LDAP_FILTER_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 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
141 case LDAP_FILTER_SUBSTRINGS:
142 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
151 return LDAP_INAPPROPRIATE_MATCHING;
162 AttributeDescription *ad,
163 struct berval *atname,
175 rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
177 if ( rc != LDAP_SUCCESS ) {
178 Debug( LDAP_DEBUG_ANY,
179 "bdb_index_read: Could not open DB %s\n",
180 atname->bv_val, 0, 0 );
184 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
185 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
191 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
192 rc = ad->ad_type->sat_equality->smr_indexer(
193 LDAP_FILTER_EQUALITY,
195 ad->ad_type->sat_syntax,
196 ad->ad_type->sat_equality,
197 atname, vals, &keys, op->o_tmpmemctx );
199 if( rc == LDAP_SUCCESS && keys != NULL ) {
200 for( i=0; keys[i].bv_val != NULL; i++ ) {
201 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
203 ber_bvarray_free_x( keys, op->o_tmpmemctx );
207 ber_bvarray_free_x( keys, op->o_tmpmemctx );
212 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
213 rc = ad->ad_type->sat_approx->smr_indexer(
216 ad->ad_type->sat_syntax,
217 ad->ad_type->sat_approx,
218 atname, vals, &keys, op->o_tmpmemctx );
220 if( rc == LDAP_SUCCESS && keys != NULL ) {
221 for( i=0; keys[i].bv_val != NULL; i++ ) {
222 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
224 ber_bvarray_free_x( keys, op->o_tmpmemctx );
228 ber_bvarray_free_x( keys, op->o_tmpmemctx );
234 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
235 rc = ad->ad_type->sat_substr->smr_indexer(
236 LDAP_FILTER_SUBSTRINGS,
238 ad->ad_type->sat_syntax,
239 ad->ad_type->sat_substr,
240 atname, vals, &keys, op->o_tmpmemctx );
242 if( rc == LDAP_SUCCESS && keys != NULL ) {
243 for( i=0; keys[i].bv_val != NULL; i++ ) {
244 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
246 ber_bvarray_free_x( keys, op->o_tmpmemctx );
250 ber_bvarray_free_x( keys, op->o_tmpmemctx );
258 /* The callers all know how to deal with these results */
260 case DB_LOCK_DEADLOCK:
261 case DB_LOCK_NOTGRANTED:
263 /* Anything else is bad news */
270 static int index_at_values(
273 AttributeDescription *ad,
281 slap_mask_t mask = 0;
285 if ( opid == BDB_INDEX_UPDATE_OP )
286 ixop = SLAP_INDEX_ADD_OP;
288 if( type->sat_sup ) {
290 rc = index_at_values( op, txn, NULL,
297 /* If this type has no AD, we've never used it before */
299 ai = bdb_attr_mask( op->o_bd->be_private, type->sat_ad );
301 #ifdef LDAP_COMP_MATCH
302 /* component indexing */
304 ComponentReference *cr;
305 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
306 rc = indexer( op, txn, cr->cr_ad, &type->sat_cname,
307 cr->cr_nvals, id, ixop,
313 /* If we're updating the index, just set the new bits that aren't
314 * already in the old mask.
316 if ( opid == BDB_INDEX_UPDATE_OP )
317 mask = ai->ai_newmask & ~ai->ai_indexmask;
319 /* For regular updates, if there is a newmask use it. Otherwise
320 * just use the old mask.
322 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
324 rc = indexer( op, txn, ad, &type->sat_cname,
325 vals, id, ixop, mask );
333 AttributeDescription *desc;
335 desc = ad_find_tags( type, tags );
337 ai = bdb_attr_mask( op->o_bd->be_private, desc );
340 if ( opid == BDB_INDEX_UPDATE_OP )
341 mask = ai->ai_newmask & ~ai->ai_indexmask;
343 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
345 rc = indexer( op, txn, desc, &desc->ad_cname,
346 vals, id, ixop, mask );
359 int bdb_index_values(
362 AttributeDescription *desc,
369 /* Never index ID 0 */
373 rc = index_at_values( op, txn, desc,
374 desc->ad_type, &desc->ad_tags,
380 /* Get the list of which indices apply to this attr */
383 struct bdb_info *bdb,
392 if( type->sat_sup ) {
394 rc = bdb_index_recset( bdb, a, type->sat_sup, tags, ir );
397 /* If this type has no AD, we've never used it before */
399 slot = bdb_attr_slot( bdb, type->sat_ad, NULL );
401 ir[slot].ai = bdb->bi_attrs[slot];
402 al = ch_malloc( sizeof( AttrList ));
404 al->next = ir[slot].attrs;
409 AttributeDescription *desc;
411 desc = ad_find_tags( type, tags );
413 slot = bdb_attr_slot( bdb, desc, NULL );
415 ir[slot].ai = bdb->bi_attrs[slot];
416 al = ch_malloc( sizeof( AttrList ));
418 al->next = ir[slot].attrs;
426 /* Apply the indices for the recset */
427 int bdb_index_recrun(
429 struct bdb_info *bdb,
438 /* Never index ID 0 */
442 for (i=base; i<bdb->bi_nattrs; i+=slap_tool_thread_max) {
444 if ( !ir->ai ) continue;
445 while (( al = ir->attrs )) {
446 ir->attrs = al->next;
447 rc = indexer( op, NULL, ir->ai->ai_desc,
448 &ir->ai->ai_desc->ad_type->sat_cname,
449 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
450 ir->ai->ai_indexmask );
466 Attribute *ap = e->e_attrs;
467 #ifdef LDAP_COMP_MATCH
468 ComponentReference *cr_list = NULL;
469 ComponentReference *cr = NULL, *dupped_cr = NULL;
471 ComponentSyntaxInfo* csi_attr;
476 struct berval value = {0};
479 /* Never index ID 0 */
483 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
484 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
485 (long) e->e_id, e->e_dn );
487 /* add each attribute to the indexes */
488 for ( ; ap != NULL; ap = ap->a_next ) {
489 #ifdef LDAP_COMP_MATCH
491 /* see if attribute has components to be indexed */
492 ai = bdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
495 if ( attr_converter && cr_list ) {
496 syn = ap->a_desc->ad_type->sat_syntax;
497 ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
498 /* Memory chunk(nibble) pre-allocation for decoders */
499 mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
500 ap->a_comp_data->cd_mem_op = mem_op;
501 for( cr = cr_list ; cr ; cr = cr->cr_next ) {
502 /* count how many values in an attribute */
503 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
505 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
506 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
507 /* decoding attribute value */
508 decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
510 return LDAP_DECODING_ERROR;
511 /* extracting the referenced component */
512 dupped_cr = dup_comp_ref( op, cr );
513 csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
515 return LDAP_DECODING_ERROR;
516 cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
517 cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
519 return LDAP_INVALID_SYNTAX;
520 at = cr->cr_ad->ad_type;
521 /* encoding the value of component in GSER */
522 rc = component_encoder( mem_op, csi_attr, &value );
523 if ( rc != LDAP_SUCCESS )
524 return LDAP_ENCODING_ERROR;
525 /* Normalize the encoded component values */
526 if ( at->sat_equality && at->sat_equality->smr_normalize ) {
527 rc = at->sat_equality->smr_normalize (
528 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
529 at->sat_syntax, at->sat_equality,
530 &value, &cr->cr_nvals[i], op->o_tmpmemctx );
532 cr->cr_nvals[i] = value;
535 /* The end of BerVarray */
536 cr->cr_nvals[num_attr-1].bv_val = NULL;
537 cr->cr_nvals[num_attr-1].bv_len = 0;
539 op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
540 nibble_mem_free ( mem_op );
541 ap->a_comp_data = NULL;
544 rc = bdb_index_values( op, txn, ap->a_desc,
545 ap->a_nvals, e->e_id, opid );
547 if( rc != LDAP_SUCCESS ) {
548 Debug( LDAP_DEBUG_TRACE,
549 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
550 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
551 (long) e->e_id, e->e_dn );
556 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
557 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
558 (long) e->e_id, e->e_dn );