1 /* index.c - routines for dealing with attribute indexes */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
18 static slap_mask_t index_mask(
20 AttributeDescription *desc,
22 struct berval *atname )
27 bdb_attr_mask( be->be_private, desc, &mask );
30 *atname = desc->ad_cname;
31 *dbname = desc->ad_cname.bv_val;
35 /* If there is a language tag, did we ever index the base
36 * type? If so, check for mask, otherwise it's not there.
38 if( slap_ad_is_lang( desc ) && desc != desc->ad_type->sat_ad ) {
39 /* has language tag */
40 bdb_attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
42 if( mask & SLAP_INDEX_AUTO_LANG ) {
43 *atname = desc->ad_cname;
44 *dbname = desc->ad_type->sat_cname.bv_val;
47 if( mask & SLAP_INDEX_LANG ) {
48 *atname = desc->ad_type->sat_cname;
49 *dbname = desc->ad_type->sat_cname.bv_val;
54 /* see if supertype defined mask for its subtypes */
55 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
56 /* If no AD, we've never indexed this type */
60 bdb_attr_mask( be->be_private, at->sat_ad, &mask );
62 if( mask & SLAP_INDEX_AUTO_SUBTYPES ) {
63 *atname = desc->ad_type->sat_cname;
64 *dbname = at->sat_cname.bv_val;
67 if( mask & SLAP_INDEX_SUBTYPES ) {
68 *atname = at->sat_cname;
69 *dbname = at->sat_cname.bv_val;
81 AttributeDescription *desc,
85 struct berval *prefixp )
92 mask = index_mask( be, desc, &dbname, prefixp );
95 return LDAP_INAPPROPRIATE_MATCHING;
98 rc = bdb_db_cache( be, dbname, &db );
100 if( rc != LDAP_SUCCESS ) {
105 case LDAP_FILTER_PRESENT:
106 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
111 case LDAP_FILTER_APPROX:
112 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
117 case LDAP_FILTER_EQUALITY:
118 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
123 case LDAP_FILTER_SUBSTRINGS:
124 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
133 return LDAP_INAPPROPRIATE_MATCHING;
145 struct berval *atname,
146 struct berval **vals,
154 AttributeDescription *ad = NULL;
155 struct berval **keys;
159 rc = bdb_db_cache( be, dbname, &db );
161 if ( rc != LDAP_SUCCESS ) {
163 LDAP_LOG(( "index", LDAP_LEVEL_ERR,
164 "bdb_index_read: Could not open DB %s\n", dbname));
166 Debug( LDAP_DEBUG_ANY,
167 "<= bdb_index_read NULL (could not open %s)\n",
173 rc = slap_bv2ad( atname, &ad, &text );
174 if( rc != LDAP_SUCCESS ) return rc;
176 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
177 rc = bdb_key_change( be, db, txn, atname, id, op );
183 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
184 rc = ad->ad_type->sat_equality->smr_indexer(
185 LDAP_FILTER_EQUALITY,
187 ad->ad_type->sat_syntax,
188 ad->ad_type->sat_equality,
189 atname, vals, &keys );
191 if( rc == LDAP_SUCCESS && keys != NULL ) {
192 for( i=0; keys[i] != NULL; i++ ) {
193 rc = bdb_key_change( be, db, txn, keys[i], id, op );
195 ber_bvecfree( keys );
199 ber_bvecfree( keys );
204 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
205 rc = ad->ad_type->sat_approx->smr_indexer(
208 ad->ad_type->sat_syntax,
209 ad->ad_type->sat_approx,
210 atname, vals, &keys );
212 if( rc == LDAP_SUCCESS && keys != NULL ) {
213 for( i=0; keys[i] != NULL; i++ ) {
214 rc = bdb_key_change( be, db, txn, keys[i], id, op );
216 ber_bvecfree( keys );
220 ber_bvecfree( keys );
226 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
227 rc = ad->ad_type->sat_substr->smr_indexer(
228 LDAP_FILTER_SUBSTRINGS,
230 ad->ad_type->sat_syntax,
231 ad->ad_type->sat_substr,
232 atname, vals, &keys );
234 if( rc == LDAP_SUCCESS && keys != NULL ) {
235 for( i=0; keys[i] != NULL; i++ ) {
236 bdb_key_change( be, db, txn, keys[i], id, op );
238 ber_bvecfree( keys );
242 ber_bvecfree( keys );
252 static int index_at_values(
257 struct berval **vals,
264 slap_mask_t mask = 0;
265 slap_mask_t tmpmask = 0;
268 if( type->sat_sup ) {
270 rc = index_at_values( be, txn,
278 /* If this type has no AD, we've never used it before */
280 bdb_attr_mask( be->be_private, type->sat_ad, &mask );
283 *dbnamep = type->sat_cname.bv_val;
284 } else if ( tmpmask & SLAP_INDEX_AUTO_SUBTYPES ) {
289 rc = indexer( be, txn, *dbnamep,
300 AttributeDescription *desc;
305 desc = ad_find_lang( type, lang );
307 bdb_attr_mask( be->be_private, desc, &tmpmask );
310 dbname = desc->ad_cname.bv_val;
311 lname = desc->ad_cname;
313 } else if ( mask & SLAP_INDEX_AUTO_LANG ) {
315 lname.bv_len = type->sat_cname.bv_len+lang->bv_len + 1;
316 lname.bv_val = ch_malloc( lname.bv_len + 1 );
317 strcpy(lname.bv_val, type->sat_cname.bv_val);
318 lname.bv_val[type->sat_cname.bv_len] = ';';
319 strcpy(lname.bv_val+type->sat_cname.bv_len+1,
323 if( dbname != NULL ) {
324 rc = indexer( be, txn, dbname, &lname,
329 ch_free( lname.bv_val );
339 int bdb_index_values(
342 AttributeDescription *desc,
343 struct berval **vals,
351 rc = index_at_values( be, txn,
352 desc->ad_type, &desc->ad_lang,
370 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
371 "index_entry: %s (%s) %ld\n",
372 op == SLAP_INDEX_ADD_OP ? "add" : "del",
373 e->e_dn, (long) e->e_id ));
375 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
376 op == SLAP_INDEX_ADD_OP ? "add" : "del",
377 (long) e->e_id, e->e_dn );
380 /* add each attribute to the indexes */
381 for ( ; ap != NULL; ap = ap->a_next ) {
382 rc = bdb_index_values( be, txn,
383 ap->a_desc, ap->a_vals, e->e_id, op );
385 if( rc != LDAP_SUCCESS ) {
387 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
388 "index_entry: success\n" ));
390 Debug( LDAP_DEBUG_TRACE,
391 "<= index_entry_%s( %ld, \"%s\" ) success\n",
392 op == SLAP_INDEX_ADD_OP ? "add" : "del",
393 (long) e->e_id, e->e_dn );
400 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
401 "index_entry: success\n" ));
403 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
404 op == SLAP_INDEX_ADD_OP ? "add" : "del",
405 (long) e->e_id, e->e_dn );