1 /* index.c - routines for dealing with attribute indexes */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
17 #include "lutil_hash.h"
19 static char presence_keyval[LUTIL_HASH_BYTES] = {0,0,0,1};
20 static struct berval presence_key = {LUTIL_HASH_BYTES, presence_keyval};
22 static slap_mask_t index_mask(
24 AttributeDescription *desc,
25 struct berval *atname )
30 bdb_attr_mask( be->be_private, desc, &mask );
33 *atname = desc->ad_cname;
37 /* If there is a tagging option, did we ever index the base
38 * type? If so, check for mask, otherwise it's not there.
40 if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
41 /* has tagging option */
42 bdb_attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
44 if ( mask && ( mask ^ SLAP_INDEX_NOTAGS ) ) {
45 *atname = desc->ad_type->sat_cname;
50 /* see if supertype defined mask for its subtypes */
51 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
52 /* If no AD, we've never indexed this type */
53 if ( !at->sat_ad ) continue;
55 bdb_attr_mask( be->be_private, at->sat_ad, &mask );
57 if ( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
58 *atname = at->sat_cname;
66 int bdb_index_is_indexed(
68 AttributeDescription *desc )
73 mask = index_mask( be, desc, &prefix );
76 return LDAP_INAPPROPRIATE_MATCHING;
84 AttributeDescription *desc,
88 struct berval *prefixp )
94 mask = index_mask( be, desc, prefixp );
97 return LDAP_INAPPROPRIATE_MATCHING;
100 rc = bdb_db_cache( be, NULL, prefixp->bv_val, &db );
102 if( rc != LDAP_SUCCESS ) {
107 case LDAP_FILTER_PRESENT:
108 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
109 *prefixp = presence_key;
114 case LDAP_FILTER_APPROX:
115 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
120 case LDAP_FILTER_EQUALITY:
121 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
126 case LDAP_FILTER_SUBSTRINGS:
127 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
136 return LDAP_INAPPROPRIATE_MATCHING;
147 AttributeDescription *ad,
148 struct berval *atname,
162 rc = bdb_db_cache( op->o_bd, txn, atname->bv_val, &db );
164 if ( rc != LDAP_SUCCESS ) {
166 LDAP_LOG( INDEX, ERR,
167 "bdb_index_read: Could not open DB %s\n",
168 atname->bv_val, 0, 0 );
170 Debug( LDAP_DEBUG_ANY,
171 "bdb_index_read: Could not open DB %s\n",
172 atname->bv_val, 0, 0 );
177 mark = sl_mark(op->o_tmpmemctx);
179 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
180 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
186 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
187 rc = ad->ad_type->sat_equality->smr_indexer(
188 LDAP_FILTER_EQUALITY,
190 ad->ad_type->sat_syntax,
191 ad->ad_type->sat_equality,
192 atname, vals, &keys, op->o_tmpmemctx );
194 if( rc == LDAP_SUCCESS && keys != NULL ) {
195 for( i=0; keys[i].bv_val != NULL; i++ ) {
196 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
198 ber_bvarray_free_x( keys, op->o_tmpmemctx );
202 ber_bvarray_free_x( keys, op->o_tmpmemctx );
207 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
208 rc = ad->ad_type->sat_approx->smr_indexer(
211 ad->ad_type->sat_syntax,
212 ad->ad_type->sat_approx,
213 atname, vals, &keys, op->o_tmpmemctx );
215 if( rc == LDAP_SUCCESS && keys != NULL ) {
216 for( i=0; keys[i].bv_val != NULL; i++ ) {
217 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
219 ber_bvarray_free_x( keys, op->o_tmpmemctx );
223 ber_bvarray_free_x( keys, op->o_tmpmemctx );
229 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
230 rc = ad->ad_type->sat_substr->smr_indexer(
231 LDAP_FILTER_SUBSTRINGS,
233 ad->ad_type->sat_syntax,
234 ad->ad_type->sat_substr,
235 atname, vals, &keys, op->o_tmpmemctx );
237 if( rc == LDAP_SUCCESS && keys != NULL ) {
238 for( i=0; keys[i].bv_val != NULL; i++ ) {
239 bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
241 ber_bvarray_free_x( keys, op->o_tmpmemctx );
245 ber_bvarray_free_x( keys, op->o_tmpmemctx );
252 sl_release( mark, op->o_tmpmemctx );
256 static int index_at_values(
259 AttributeDescription *ad,
267 slap_mask_t mask = 0;
269 if( type->sat_sup ) {
271 rc = index_at_values( op, txn, NULL,
278 /* If this type has no AD, we've never used it before */
280 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
285 rc = indexer( op, txn, ad, &type->sat_cname,
293 AttributeDescription *desc;
297 desc = ad_find_tags( type, tags );
299 bdb_attr_mask( op->o_bd->be_private, desc, &mask );
303 rc = indexer( op, txn, desc, &desc->ad_cname,
316 int bdb_index_values(
319 AttributeDescription *desc,
326 rc = index_at_values( op, txn, desc,
327 desc->ad_type, &desc->ad_tags,
341 Attribute *ap = e->e_attrs;
344 LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
345 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
347 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
348 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
349 (long) e->e_id, e->e_dn );
352 /* add each attribute to the indexes */
353 for ( ; ap != NULL; ap = ap->a_next ) {
354 rc = bdb_index_values( op, txn, ap->a_desc,
355 ap->a_nvals, e->e_id, opid );
357 if( rc != LDAP_SUCCESS ) {
359 LDAP_LOG( INDEX, ENTRY,
360 "index_entry: failure (%d)\n", rc, 0, 0 );
362 Debug( LDAP_DEBUG_TRACE,
363 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
364 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
365 (long) e->e_id, e->e_dn );
372 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
374 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
375 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
376 (long) e->e_id, e->e_dn );