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,
27 /* we do not support indexing of binary attributes */
28 if( slap_ad_is_binary( desc ) ) return 0;
30 bdb_attr_mask( be->be_private, desc->ad_cname->bv_val, &mask );
33 *atname = desc->ad_cname->bv_val;
34 *dbname = desc->ad_cname->bv_val;
38 if( slap_ad_is_lang( desc ) ) {
39 /* has language tag */
40 bdb_attr_mask( be->be_private, desc->ad_type->sat_cname, &mask );
42 if( mask & SLAP_INDEX_AUTO_LANG ) {
43 *atname = desc->ad_cname->bv_val;
44 *dbname = desc->ad_type->sat_cname;
47 if( mask & SLAP_INDEX_LANG ) {
48 *atname = desc->ad_type->sat_cname;
49 *dbname = desc->ad_type->sat_cname;
54 /* see if supertype defined mask for its subtypes */
55 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
56 bdb_attr_mask( be->be_private, at->sat_cname, &mask );
58 if( mask & SLAP_INDEX_AUTO_SUBTYPES ) {
59 *atname = desc->ad_type->sat_cname;
60 *dbname = at->sat_cname;
63 if( mask & SLAP_INDEX_SUBTYPES ) {
64 *atname = at->sat_cname;
65 *dbname = at->sat_cname;
77 AttributeDescription *desc,
81 struct berval **prefixp )
89 mask = index_mask( be, desc, &dbname, &atname );
92 return LDAP_INAPPROPRIATE_MATCHING;
95 rc = bdb_db_cache( be, dbname, &db );
97 if( rc != LDAP_SUCCESS ) {
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;
134 *prefixp = ber_bvstrdup( atname );
144 struct berval **vals,
152 AttributeDescription *ad = NULL;
153 struct berval **keys;
154 struct berval prefix;
158 rc = bdb_db_cache( be, dbname, &db );
160 if ( rc != LDAP_SUCCESS ) {
162 LDAP_LOG(( "index", LDAP_LEVEL_ERR,
163 "bdb_index_read: Could not open DB %s\n", dbname));
165 Debug( LDAP_DEBUG_ANY,
166 "<= bdb_index_read NULL (could not open %s)\n",
172 rc = slap_str2ad( atname, &ad, &text );
173 if( rc != LDAP_SUCCESS ) return rc;
175 prefix.bv_val = atname;
176 prefix.bv_len = strlen( atname );
178 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
179 rc = bdb_key_change( be, db, txn, &prefix, id, op );
185 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
186 rc = ad->ad_type->sat_equality->smr_indexer(
187 LDAP_FILTER_EQUALITY,
189 ad->ad_type->sat_syntax,
190 ad->ad_type->sat_equality,
191 &prefix, vals, &keys );
193 if( rc == LDAP_SUCCESS && keys != NULL ) {
194 for( i=0; keys[i] != NULL; i++ ) {
195 rc = bdb_key_change( be, db, txn, keys[i], id, op );
197 ber_bvecfree( keys );
201 ber_bvecfree( keys );
206 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
207 rc = ad->ad_type->sat_approx->smr_indexer(
210 ad->ad_type->sat_syntax,
211 ad->ad_type->sat_approx,
212 &prefix, vals, &keys );
214 if( rc == LDAP_SUCCESS && keys != NULL ) {
215 for( i=0; keys[i] != NULL; i++ ) {
216 rc = bdb_key_change( be, db, txn, keys[i], id, op );
218 ber_bvecfree( keys );
222 ber_bvecfree( keys );
228 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
229 rc = ad->ad_type->sat_substr->smr_indexer(
230 LDAP_FILTER_SUBSTRINGS,
232 ad->ad_type->sat_syntax,
233 ad->ad_type->sat_substr,
234 &prefix, vals, &keys );
236 if( rc == LDAP_SUCCESS && keys != NULL ) {
237 for( i=0; keys[i] != NULL; i++ ) {
238 bdb_key_change( be, db, txn, keys[i], id, op );
240 ber_bvecfree( keys );
244 ber_bvecfree( keys );
255 static int index_at_values(
260 struct berval **vals,
268 slap_mask_t tmpmask = 0;
271 if( type->sat_sup ) {
273 rc = index_at_values( be, txn,
281 bdb_attr_mask( be->be_private, type->sat_cname, &mask );
284 *dbnamep = type->sat_cname;
285 } else if ( tmpmask & SLAP_INDEX_AUTO_SUBTYPES ) {
290 rc = indexer( be, txn, *dbnamep,
300 size_t tlen = strlen( type->sat_cname );
301 size_t llen = strlen( lang );
302 char *lname = ch_malloc( tlen + llen + sizeof(";") );
304 sprintf( lname, "%s;%s", type->sat_cname, lang );
306 bdb_attr_mask( be->be_private, lname, &tmpmask );
310 } else if ( mask & SLAP_INDEX_AUTO_LANG ) {
315 if( dbname != NULL ) {
316 rc = indexer( be, txn, dbname, lname,
332 int bdb_index_values(
335 AttributeDescription *desc,
336 struct berval **vals,
344 if( slap_ad_is_binary( desc ) ) {
345 /* binary attributes have no index capabilities */
349 rc = index_at_values( be, txn,
350 desc->ad_type, desc->ad_lang,
368 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
369 "index_entry: %s (%s) %ld\n",
370 op == SLAP_INDEX_ADD_OP ? "add" : "del",
371 e->e_dn, (long) e->e_id ));
373 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
374 op == SLAP_INDEX_ADD_OP ? "add" : "del",
375 (long) e->e_id, e->e_dn );
378 /* add each attribute to the indexes */
379 for ( ; ap != NULL; ap = ap->a_next ) {
380 rc = bdb_index_values( be, txn,
381 ap->a_desc, ap->a_vals, e->e_id, op );
383 if( rc != LDAP_SUCCESS ) {
385 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
386 "index_entry: success\n" ));
388 Debug( LDAP_DEBUG_TRACE,
389 "<= index_entry_%s( %ld, \"%s\" ) success\n",
390 op == SLAP_INDEX_ADD_OP ? "add" : "del",
391 (long) e->e_id, e->e_dn );
398 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
399 "index_entry: success\n" ));
401 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
402 op == SLAP_INDEX_ADD_OP ? "add" : "del",
403 (long) e->e_id, e->e_dn );