1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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 ) {
179 LDAP_LOG( INDEX, ERR,
180 "bdb_index_read: Could not open DB %s\n",
181 atname->bv_val, 0, 0 );
183 Debug( LDAP_DEBUG_ANY,
184 "bdb_index_read: Could not open DB %s\n",
185 atname->bv_val, 0, 0 );
190 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
191 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
197 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
198 rc = ad->ad_type->sat_equality->smr_indexer(
199 LDAP_FILTER_EQUALITY,
201 ad->ad_type->sat_syntax,
202 ad->ad_type->sat_equality,
203 atname, vals, &keys, op->o_tmpmemctx );
205 if( rc == LDAP_SUCCESS && keys != NULL ) {
206 for( i=0; keys[i].bv_val != NULL; i++ ) {
207 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
209 ber_bvarray_free_x( keys, op->o_tmpmemctx );
213 ber_bvarray_free_x( keys, op->o_tmpmemctx );
218 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
219 rc = ad->ad_type->sat_approx->smr_indexer(
222 ad->ad_type->sat_syntax,
223 ad->ad_type->sat_approx,
224 atname, vals, &keys, op->o_tmpmemctx );
226 if( rc == LDAP_SUCCESS && keys != NULL ) {
227 for( i=0; keys[i].bv_val != NULL; i++ ) {
228 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
230 ber_bvarray_free_x( keys, op->o_tmpmemctx );
234 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 for( i=0; keys[i].bv_val != NULL; i++ ) {
250 bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
252 ber_bvarray_free_x( keys, op->o_tmpmemctx );
256 ber_bvarray_free_x( keys, op->o_tmpmemctx );
266 static int index_at_values(
269 AttributeDescription *ad,
277 slap_mask_t mask = 0;
279 if( type->sat_sup ) {
281 rc = index_at_values( op, txn, NULL,
288 /* If this type has no AD, we've never used it before */
290 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
295 rc = indexer( op, txn, ad, &type->sat_cname,
303 AttributeDescription *desc;
307 desc = ad_find_tags( type, tags );
309 bdb_attr_mask( op->o_bd->be_private, desc, &mask );
313 rc = indexer( op, txn, desc, &desc->ad_cname,
326 int bdb_index_values(
329 AttributeDescription *desc,
336 rc = index_at_values( op, txn, desc,
337 desc->ad_type, &desc->ad_tags,
351 Attribute *ap = e->e_attrs;
354 LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
355 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
357 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
358 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
359 (long) e->e_id, e->e_dn );
362 /* add each attribute to the indexes */
363 for ( ; ap != NULL; ap = ap->a_next ) {
364 rc = bdb_index_values( op, txn, ap->a_desc,
365 ap->a_nvals, e->e_id, opid );
367 if( rc != LDAP_SUCCESS ) {
369 LDAP_LOG( INDEX, ENTRY,
370 "index_entry: failure (%d)\n", rc, 0, 0 );
372 Debug( LDAP_DEBUG_TRACE,
373 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
374 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
375 (long) e->e_id, e->e_dn );
382 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
384 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
385 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
386 (long) e->e_id, e->e_dn );