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 struct berval *atname,
156 AttributeDescription *ad = NULL;
161 rc = bdb_db_cache( be, txn, atname->bv_val, &db );
163 if ( rc != LDAP_SUCCESS ) {
165 LDAP_LOG( INDEX, ERR,
166 "bdb_index_read: Could not open DB %s\n",
167 atname->bv_val, 0, 0 );
169 Debug( LDAP_DEBUG_ANY,
170 "bdb_index_read: Could not open DB %s\n",
171 atname->bv_val, 0, 0 );
176 rc = slap_bv2ad( atname, &ad, &text );
177 if( rc != LDAP_SUCCESS ) return rc;
179 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
180 rc = bdb_key_change( be, db, txn, &presence_key, id, op );
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 );
194 if( rc == LDAP_SUCCESS && keys != NULL ) {
195 for( i=0; keys[i].bv_val != NULL; i++ ) {
196 rc = bdb_key_change( be, db, txn, &keys[i], id, op );
198 ber_bvarray_free( keys );
202 ber_bvarray_free( keys );
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 );
215 if( rc == LDAP_SUCCESS && keys != NULL ) {
216 for( i=0; keys[i].bv_val != NULL; i++ ) {
217 rc = bdb_key_change( be, db, txn, &keys[i], id, op );
219 ber_bvarray_free( keys );
223 ber_bvarray_free( keys );
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 );
237 if( rc == LDAP_SUCCESS && keys != NULL ) {
238 for( i=0; keys[i].bv_val != NULL; i++ ) {
239 bdb_key_change( be, db, txn, &keys[i], id, op );
241 ber_bvarray_free( keys );
245 ber_bvarray_free( keys );
255 static int index_at_values(
265 slap_mask_t mask = 0;
267 if( type->sat_sup ) {
269 rc = index_at_values( be, txn,
276 /* If this type has no AD, we've never used it before */
278 bdb_attr_mask( be->be_private, type->sat_ad, &mask );
282 rc = indexer( be, txn, &type->sat_cname,
290 AttributeDescription *desc;
294 desc = ad_find_tags( type, tags );
296 bdb_attr_mask( be->be_private, desc, &mask );
300 rc = indexer( be, txn, &desc->ad_cname,
313 int bdb_index_values(
316 AttributeDescription *desc,
323 rc = index_at_values( be, txn,
324 desc->ad_type, &desc->ad_tags,
338 Attribute *ap = e->e_attrs;
341 LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
342 op == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
344 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
345 op == SLAP_INDEX_ADD_OP ? "add" : "del",
346 (long) e->e_id, e->e_dn );
349 /* add each attribute to the indexes */
350 for ( ; ap != NULL; ap = ap->a_next ) {
351 rc = bdb_index_values( be, txn, ap->a_desc,
352 ap->a_nvals, e->e_id, op );
354 if( rc != LDAP_SUCCESS ) {
356 LDAP_LOG( INDEX, ENTRY,
357 "index_entry: failure (%d)\n", rc, 0, 0 );
359 Debug( LDAP_DEBUG_TRACE,
360 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
361 op == SLAP_INDEX_ADD_OP ? "add" : "del",
362 (long) e->e_id, e->e_dn );
369 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
371 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
372 op == SLAP_INDEX_ADD_OP ? "add" : "del",
373 (long) e->e_id, e->e_dn );