1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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 slap_mask_t index_mask(
33 AttributeDescription *desc,
34 struct berval *atname )
39 bdb_attr_mask( be->be_private, desc, &mask );
42 *atname = desc->ad_cname;
46 /* If there is a tagging option, did we ever index the base
47 * type? If so, check for mask, otherwise it's not there.
49 if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
50 /* has tagging option */
51 bdb_attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
53 if ( mask && ( mask ^ SLAP_INDEX_NOTAGS ) ) {
54 *atname = desc->ad_type->sat_cname;
59 /* see if supertype defined mask for its subtypes */
60 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
61 /* If no AD, we've never indexed this type */
62 if ( !at->sat_ad ) continue;
64 bdb_attr_mask( be->be_private, at->sat_ad, &mask );
66 if ( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
67 *atname = at->sat_cname;
75 int bdb_index_is_indexed(
77 AttributeDescription *desc )
82 mask = index_mask( be, desc, &prefix );
85 return LDAP_INAPPROPRIATE_MATCHING;
93 AttributeDescription *desc,
97 struct berval *prefixp )
103 mask = index_mask( be, desc, prefixp );
106 return LDAP_INAPPROPRIATE_MATCHING;
109 rc = bdb_db_cache( be, prefixp->bv_val, &db );
111 if( rc != LDAP_SUCCESS ) {
116 case LDAP_FILTER_PRESENT:
117 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
118 *prefixp = presence_key;
123 case LDAP_FILTER_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 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
140 case LDAP_FILTER_SUBSTRINGS:
141 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
150 return LDAP_INAPPROPRIATE_MATCHING;
161 AttributeDescription *ad,
162 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 );
260 static int index_at_values(
263 AttributeDescription *ad,
271 slap_mask_t mask = 0;
272 #ifdef LDAP_COMP_MATCH
273 ComponentReference* cr_list, *cr;
274 AttributeDescription *comp_ad;
277 if( type->sat_sup ) {
279 rc = index_at_values( op, txn, NULL,
286 #ifdef LDAP_COMP_MATCH
287 /* component indexing */
288 bdb_attr_comp_ref ( op->o_bd->be_private, type->sat_ad, &cr_list );
290 for( cr = cr_list ; cr ; cr = cr->cr_next ) {
291 rc = indexer( op, txn, cr->cr_ad, &type->sat_cname,
292 cr->cr_nvals, id, opid,
297 /* If this type has no AD, we've never used it before */
299 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
304 rc = indexer( op, txn, ad, &type->sat_cname,
312 AttributeDescription *desc;
316 desc = ad_find_tags( type, tags );
318 bdb_attr_mask( op->o_bd->be_private, desc, &mask );
322 rc = indexer( op, txn, desc, &desc->ad_cname,
335 int bdb_index_values(
338 AttributeDescription *desc,
345 rc = index_at_values( op, txn, desc,
346 desc->ad_type, &desc->ad_tags,
360 Attribute *ap = e->e_attrs;
361 #ifdef LDAP_COMP_MATCH
362 ComponentReference *cr_list = NULL;
363 ComponentReference *cr = NULL, *dupped_cr = NULL;
364 void* decoded_comp, *extracted_comp;
365 ComponentSyntaxInfo* csi_attr;
370 struct berval value = {0};
373 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
374 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
375 (long) e->e_id, e->e_dn );
377 /* add each attribute to the indexes */
378 for ( ; ap != NULL; ap = ap->a_next ) {
379 #ifdef LDAP_COMP_MATCH
380 /* see if attribute has components to be indexed */
381 bdb_attr_comp_ref( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad, &cr_list );
382 if ( attr_converter && cr_list ) {
383 syn = ap->a_desc->ad_type->sat_syntax;
384 ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
385 /* Memory chunk(nibble) pre-allocation for decoders */
386 mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
387 ap->a_comp_data->cd_mem_op = mem_op;
388 for( cr = cr_list ; cr ; cr = cr->cr_next ) {
389 /* count how many values in an attribute */
390 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
392 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
393 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
394 /* decoding attribute value */
395 decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
397 return LDAP_DECODING_ERROR;
398 /* extracting the referenced component */
399 dupped_cr = dup_comp_ref( op, cr );
400 csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
402 return LDAP_DECODING_ERROR;
403 cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
404 cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
406 return LDAP_INVALID_SYNTAX;
407 at = cr->cr_ad->ad_type;
408 /* encoding the value of component in GSER */
409 rc = component_encoder( mem_op, csi_attr, &value );
410 if ( rc != LDAP_SUCCESS )
411 return LDAP_ENCODING_ERROR;
412 /* Normalize the encoded component values */
413 if ( at->sat_equality && at->sat_equality->smr_normalize ) {
414 rc = at->sat_equality->smr_normalize (
415 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
416 at->sat_syntax, at->sat_equality,
417 &value, &cr->cr_nvals[i], op->o_tmpmemctx );
419 cr->cr_nvals[i] = value;
422 /* The end of BerVarray */
423 cr->cr_nvals[num_attr-1].bv_val = NULL;
424 cr->cr_nvals[num_attr-1].bv_len = 0;
426 op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
427 nibble_mem_free ( mem_op );
428 ap->a_comp_data = NULL;
431 rc = bdb_index_values( op, txn, ap->a_desc,
432 ap->a_nvals, e->e_id, opid );
434 if( rc != LDAP_SUCCESS ) {
435 Debug( LDAP_DEBUG_TRACE,
436 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
437 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
438 (long) e->e_id, e->e_dn );
443 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
444 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
445 (long) e->e_id, e->e_dn );