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,
158 struct berval *keys, *xkeys = NULL;
163 rc = bdb_db_cache( op->o_bd, txn, atname->bv_val, &db );
165 if ( rc != LDAP_SUCCESS ) {
167 LDAP_LOG( INDEX, ERR,
168 "bdb_index_read: Could not open DB %s\n",
169 atname->bv_val, 0, 0 );
171 Debug( LDAP_DEBUG_ANY,
172 "bdb_index_read: Could not open DB %s\n",
173 atname->bv_val, 0, 0 );
178 mark = sl_mark(op->o_tmpmemctx);
180 /* For a delete op, make sure we're deleting the entire
181 * attribute (xvals == NULL) before changing the presence
182 * index. xvals is only non-NULL when deleting part of an attribute.
184 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) && xvals == NULL ) {
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 /* xvals is only provided on deletes. Generate the keys for
201 * xvals, representing all of the keys that will exist in
202 * the index when we're done. If we find a delete key that
203 * is in the xkeys, nullify the delete on that key.
206 rc = ad->ad_type->sat_equality->smr_indexer(
207 LDAP_FILTER_EQUALITY, mask,
208 ad->ad_type->sat_syntax,
209 ad->ad_type->sat_equality,
210 atname, xvals, &xkeys,
213 for( i=0; keys[i].bv_val; i++ ) {
214 for( j=0; xkeys[j].bv_val != NULL; j++ ) {
215 if( bvmatch( &keys[i], &xkeys[j] ) ) {
221 for( i=0; keys[i].bv_val != NULL; i++ ) {
222 /* ignore nullified keys */
223 if( keys[i].bv_len == 0 ) continue;
224 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
228 ber_bvarray_free_x( xkeys, op->o_tmpmemctx );
231 ber_bvarray_free_x( keys, op->o_tmpmemctx );
237 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
238 rc = ad->ad_type->sat_approx->smr_indexer(
241 ad->ad_type->sat_syntax,
242 ad->ad_type->sat_approx,
243 atname, vals, &keys, op->o_tmpmemctx );
245 if( rc == LDAP_SUCCESS && keys != NULL ) {
246 /* nullify duplicate keys */
247 for( i=0; keys[i].bv_val; i++ ) {
248 if( !keys[i].bv_len ) continue;
249 for( j=i+1; keys[j].bv_val; j++ ) {
250 if( bvmatch( &keys[i], &keys[j] ) ) {
257 rc = ad->ad_type->sat_equality->smr_indexer(
258 LDAP_FILTER_APPROX, mask,
259 ad->ad_type->sat_syntax,
260 ad->ad_type->sat_approx,
261 atname, xvals, &xkeys,
264 for( i=0; keys[i].bv_val; i++ ) {
265 for( j=0; xkeys[j].bv_val != NULL; j++ ) {
266 if( bvmatch( &keys[i], &xkeys[j] ) ) {
272 for( i=0; keys[i].bv_val != NULL; i++ ) {
273 /* ignore nullified keys */
274 if( keys[i].bv_len == 0 ) continue;
275 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
279 ber_bvarray_free_x( xkeys, op->o_tmpmemctx );
282 ber_bvarray_free_x( keys, op->o_tmpmemctx );
289 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
290 rc = ad->ad_type->sat_substr->smr_indexer(
291 LDAP_FILTER_SUBSTRINGS,
293 ad->ad_type->sat_syntax,
294 ad->ad_type->sat_substr,
295 atname, vals, &keys, op->o_tmpmemctx );
297 if( rc == LDAP_SUCCESS && keys != NULL ) {
298 /* nullify duplicate keys */
299 for( i=0; keys[i].bv_val; i++ ) {
300 if( !keys[i].bv_len ) continue;
301 for( j=i+1; keys[j].bv_val; j++ ) {
302 if( bvmatch( &keys[i], &keys[j] ) ) {
309 rc = ad->ad_type->sat_equality->smr_indexer(
310 LDAP_FILTER_SUBSTRINGS, mask,
311 ad->ad_type->sat_syntax,
312 ad->ad_type->sat_substr,
313 atname, xvals, &xkeys,
316 for( i=0; keys[i].bv_val; i++ ) {
317 for( j=0; xkeys[j].bv_val != NULL; j++ ) {
318 if( bvmatch( &keys[i], &xkeys[j] ) ) {
324 for( i=0; keys[i].bv_val != NULL; i++ ) {
325 /* ignore nullified keys */
326 if ( keys[i].bv_len == 0 ) continue;
327 bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
331 ber_bvarray_free_x( xkeys, op->o_tmpmemctx );
334 ber_bvarray_free_x( keys, op->o_tmpmemctx );
342 sl_release( mark, op->o_tmpmemctx );
346 static int index_at_values(
349 AttributeDescription *ad,
358 slap_mask_t mask = 0;
360 if( type->sat_sup ) {
362 rc = index_at_values( op, txn, NULL,
364 vals, xvals, id, opid );
369 /* If this type has no AD, we've never used it before */
371 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
376 rc = indexer( op, txn, ad, &type->sat_cname,
377 vals, xvals, id, opid,
384 AttributeDescription *desc;
388 desc = ad_find_tags( type, tags );
390 bdb_attr_mask( op->o_bd->be_private, desc, &mask );
394 rc = indexer( op, txn, desc, &desc->ad_cname,
395 vals, xvals, id, opid,
407 int bdb_index_values(
410 AttributeDescription *desc,
418 rc = index_at_values( op, txn, desc,
419 desc->ad_type, &desc->ad_tags,
420 vals, xvals, id, opid );
433 Attribute *ap = e->e_attrs;
436 LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
437 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
439 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
440 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
441 (long) e->e_id, e->e_dn );
444 /* add each attribute to the indexes */
445 for ( ; ap != NULL; ap = ap->a_next ) {
446 rc = bdb_index_values( op, txn, ap->a_desc,
447 ap->a_nvals, NULL, e->e_id, opid );
449 if( rc != LDAP_SUCCESS ) {
451 LDAP_LOG( INDEX, ENTRY,
452 "index_entry: failure (%d)\n", rc, 0, 0 );
454 Debug( LDAP_DEBUG_TRACE,
455 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
456 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
457 (long) e->e_id, e->e_dn );
464 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
466 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
467 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
468 (long) e->e_id, e->e_dn );