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, 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, 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 #if 0 /* No longer needed, our frees are in order so nothing accumulates */
178 mark = sl_mark(op->o_tmpmemctx);
181 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
182 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
188 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
189 rc = ad->ad_type->sat_equality->smr_indexer(
190 LDAP_FILTER_EQUALITY,
192 ad->ad_type->sat_syntax,
193 ad->ad_type->sat_equality,
194 atname, vals, &keys, op->o_tmpmemctx );
196 if( rc == LDAP_SUCCESS && keys != NULL ) {
197 for( i=0; keys[i].bv_val != NULL; i++ ) {
198 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
200 ber_bvarray_free_x( keys, op->o_tmpmemctx );
204 ber_bvarray_free_x( keys, op->o_tmpmemctx );
209 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
210 rc = ad->ad_type->sat_approx->smr_indexer(
213 ad->ad_type->sat_syntax,
214 ad->ad_type->sat_approx,
215 atname, vals, &keys, op->o_tmpmemctx );
217 if( rc == LDAP_SUCCESS && keys != NULL ) {
218 for( i=0; keys[i].bv_val != NULL; i++ ) {
219 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
221 ber_bvarray_free_x( keys, op->o_tmpmemctx );
225 ber_bvarray_free_x( keys, op->o_tmpmemctx );
231 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
232 rc = ad->ad_type->sat_substr->smr_indexer(
233 LDAP_FILTER_SUBSTRINGS,
235 ad->ad_type->sat_syntax,
236 ad->ad_type->sat_substr,
237 atname, vals, &keys, op->o_tmpmemctx );
239 if( rc == LDAP_SUCCESS && keys != NULL ) {
240 for( i=0; keys[i].bv_val != NULL; i++ ) {
241 bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
243 ber_bvarray_free_x( keys, op->o_tmpmemctx );
247 ber_bvarray_free_x( keys, op->o_tmpmemctx );
255 sl_release( mark, op->o_tmpmemctx );
260 static int index_at_values(
263 AttributeDescription *ad,
271 slap_mask_t mask = 0;
273 if( type->sat_sup ) {
275 rc = index_at_values( op, txn, NULL,
282 /* If this type has no AD, we've never used it before */
284 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
289 rc = indexer( op, txn, ad, &type->sat_cname,
297 AttributeDescription *desc;
301 desc = ad_find_tags( type, tags );
303 bdb_attr_mask( op->o_bd->be_private, desc, &mask );
307 rc = indexer( op, txn, desc, &desc->ad_cname,
320 int bdb_index_values(
323 AttributeDescription *desc,
330 rc = index_at_values( op, txn, desc,
331 desc->ad_type, &desc->ad_tags,
345 Attribute *ap = e->e_attrs;
348 LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
349 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
351 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
352 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
353 (long) e->e_id, e->e_dn );
356 /* add each attribute to the indexes */
357 for ( ; ap != NULL; ap = ap->a_next ) {
358 rc = bdb_index_values( op, txn, ap->a_desc,
359 ap->a_nvals, e->e_id, opid );
361 if( rc != LDAP_SUCCESS ) {
363 LDAP_LOG( INDEX, ENTRY,
364 "index_entry: failure (%d)\n", rc, 0, 0 );
366 Debug( LDAP_DEBUG_TRACE,
367 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
368 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
369 (long) e->e_id, e->e_dn );
376 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
378 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
379 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
380 (long) e->e_id, e->e_dn );