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>
16 #include "back-ldbm.h"
18 static slap_mask_t index_mask(
20 AttributeDescription *desc,
22 struct berval *atname )
27 attr_mask( be->be_private, desc, &mask );
30 *atname = desc->ad_cname;
31 *dbname = desc->ad_cname.bv_val;
35 /* If there is a tagging option, did we ever index the base
36 * type? If so, check for mask, otherwise it's not there.
38 if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
39 /* has tagging option */
40 attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
42 if( mask && ( mask ^ SLAP_INDEX_NOTAGS ) ) {
43 *atname = desc->ad_type->sat_cname;
44 *dbname = desc->ad_type->sat_cname.bv_val;
49 /* see if supertype defined mask for its subtypes */
50 for( at = desc->ad_type->sat_sup; at != NULL ; at = at->sat_sup ) {
51 /* If no AD, we've never indexed this type */
55 attr_mask( be->be_private, at->sat_ad, &mask );
57 if( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
58 *atname = at->sat_cname;
59 *dbname = at->sat_cname.bv_val;
69 AttributeDescription *desc )
75 mask = index_mask( be, desc, &dbname, &prefix );
78 return LDAP_INAPPROPRIATE_MATCHING;
86 AttributeDescription *desc,
90 struct berval *prefixp )
95 mask = index_mask( be, desc, &dbname, prefixp );
98 return LDAP_INAPPROPRIATE_MATCHING;
102 case LDAP_FILTER_PRESENT:
103 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
108 case LDAP_FILTER_APPROX:
109 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
114 case LDAP_FILTER_EQUALITY:
115 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
120 case LDAP_FILTER_SUBSTRINGS:
121 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
130 return LDAP_INAPPROPRIATE_MATCHING;
141 struct berval *atname,
150 AttributeDescription *ad = NULL;
155 rc = slap_bv2ad( atname, &ad, &text );
157 if( rc != LDAP_SUCCESS ) return rc;
159 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
163 LDAP_LOG( INDEX, ERR,
164 "index_read: Could not open db %s%s\n", dbname, LDBM_SUFFIX, 0 );
166 Debug( LDAP_DEBUG_ANY,
167 "<= index_read NULL (could not open %s%s)\n",
168 dbname, LDBM_SUFFIX, 0 );
174 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
175 key_change( be, db, atname, id, op );
178 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
179 rc = ad->ad_type->sat_equality->smr_indexer(
180 LDAP_FILTER_EQUALITY,
182 ad->ad_type->sat_syntax,
183 ad->ad_type->sat_equality,
184 atname, vals, &keys );
186 if( rc == LDAP_SUCCESS && keys != NULL ) {
187 for( i=0; keys[i].bv_val != NULL; i++ ) {
188 key_change( be, db, &keys[i], id, op );
190 ber_bvarray_free( keys );
194 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
195 rc = ad->ad_type->sat_approx->smr_indexer(
198 ad->ad_type->sat_syntax,
199 ad->ad_type->sat_approx,
200 atname, vals, &keys );
202 if( rc == LDAP_SUCCESS && keys != NULL ) {
203 for( i=0; keys[i].bv_val != NULL; i++ ) {
204 key_change( be, db, &keys[i], id, op );
206 ber_bvarray_free( keys );
210 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
211 rc = ad->ad_type->sat_substr->smr_indexer(
212 LDAP_FILTER_SUBSTRINGS,
214 ad->ad_type->sat_syntax,
215 ad->ad_type->sat_substr,
216 atname, vals, &keys );
218 if( rc == LDAP_SUCCESS && keys != NULL ) {
219 for( i=0; keys[i].bv_val != NULL; i++ ) {
220 key_change( be, db, &keys[i], id, op );
222 ber_bvarray_free( keys );
226 ldbm_cache_close( be, db );
230 static int index_at_values(
238 slap_mask_t mask = 0;
240 if( type->sat_sup ) {
242 (void) index_at_values( be,
247 /* If this type has no AD, we've never used it before */
249 attr_mask( be->be_private, type->sat_ad, &mask );
253 indexer( be, type->sat_cname.bv_val,
260 AttributeDescription *desc;
264 desc = ad_find_tags(type, tags);
266 attr_mask( be->be_private, desc, &mask );
270 indexer( be, desc->ad_cname.bv_val, &desc->ad_cname,
281 AttributeDescription *desc,
286 (void) index_at_values( be,
287 desc->ad_type, &desc->ad_tags,
301 LDAP_LOG( INDEX, ENTRY,
302 "index_entry: %s (%s)%ld\n", op == SLAP_INDEX_ADD_OP ? "add" : "del",
305 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
306 op == SLAP_INDEX_ADD_OP ? "add" : "del",
310 /* add each attribute to the indexes */
311 for ( ; ap != NULL; ap = ap->a_next ) {
312 index_values( be, ap->a_desc, ap->a_vals, e->e_id, op );
316 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
318 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
319 op == SLAP_INDEX_ADD_OP ? "add" : "del",