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 )
87 mask = index_mask( be, desc, &dbname, &atname );
90 return LDAP_INAPPROPRIATE_MATCHING;
94 case LDAP_FILTER_PRESENT:
95 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
100 case LDAP_FILTER_APPROX:
101 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
106 case LDAP_FILTER_EQUALITY:
107 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
112 case LDAP_FILTER_SUBSTRINGS:
113 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
122 return LDAP_INAPPROPRIATE_MATCHING;
126 *prefixp = ber_bvstrdup( atname );
136 struct berval **vals,
144 AttributeDescription *ad = NULL;
145 struct berval **keys;
146 struct berval prefix;
150 rc = bdb_db_cache( be, dbname, &db );
152 if ( rc != LDAP_SUCCESS ) {
154 LDAP_LOG(( "index", LDAP_LEVEL_ERR,
155 "bdb_index_read: Could not open DB %s\n", dbname));
157 Debug( LDAP_DEBUG_ANY,
158 "<= bdb_index_read NULL (could not open %s)\n",
164 rc = slap_str2ad( atname, &ad, &text );
165 if( rc != LDAP_SUCCESS ) return rc;
167 prefix.bv_val = atname;
168 prefix.bv_len = strlen( atname );
170 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
171 rc = bdb_key_change( be, db, txn, &prefix, id, op );
177 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
178 rc = ad->ad_type->sat_equality->smr_indexer(
179 LDAP_FILTER_EQUALITY,
181 ad->ad_type->sat_syntax,
182 ad->ad_type->sat_equality,
183 &prefix, vals, &keys );
185 if( rc == LDAP_SUCCESS && keys != NULL ) {
186 for( i=0; keys[i] != NULL; i++ ) {
187 rc = bdb_key_change( be, db, txn, keys[i], id, op );
189 ber_bvecfree( keys );
193 ber_bvecfree( keys );
198 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
199 rc = ad->ad_type->sat_approx->smr_indexer(
202 ad->ad_type->sat_syntax,
203 ad->ad_type->sat_approx,
204 &prefix, vals, &keys );
206 if( rc == LDAP_SUCCESS && keys != NULL ) {
207 for( i=0; keys[i] != NULL; i++ ) {
208 rc = bdb_key_change( be, db, txn, keys[i], id, op );
210 ber_bvecfree( keys );
214 ber_bvecfree( keys );
220 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
221 rc = ad->ad_type->sat_substr->smr_indexer(
222 LDAP_FILTER_SUBSTRINGS,
224 ad->ad_type->sat_syntax,
225 ad->ad_type->sat_substr,
226 &prefix, vals, &keys );
228 if( rc == LDAP_SUCCESS && keys != NULL ) {
229 for( i=0; keys[i] != NULL; i++ ) {
230 bdb_key_change( be, db, txn, keys[i], id, op );
232 ber_bvecfree( keys );
236 ber_bvecfree( keys );
247 static int index_at_values(
252 struct berval **vals,
260 slap_mask_t tmpmask = 0;
263 if( type->sat_sup ) {
265 rc = index_at_values( be, txn,
273 bdb_attr_mask( be->be_private, type->sat_cname, &mask );
276 *dbnamep = type->sat_cname;
277 } else if ( tmpmask & SLAP_INDEX_AUTO_SUBTYPES ) {
282 rc = indexer( be, txn, *dbnamep,
292 size_t tlen = strlen( type->sat_cname );
293 size_t llen = strlen( lang );
294 char *lname = ch_malloc( tlen + llen + sizeof(";") );
296 sprintf( lname, "%s;%s", type->sat_cname, lang );
298 bdb_attr_mask( be->be_private, lname, &tmpmask );
302 } else if ( mask & SLAP_INDEX_AUTO_LANG ) {
307 if( dbname != NULL ) {
308 rc = indexer( be, txn, dbname, lname,
324 int bdb_index_values(
327 AttributeDescription *desc,
328 struct berval **vals,
336 if( slap_ad_is_binary( desc ) ) {
337 /* binary attributes have no index capabilities */
341 rc = index_at_values( be, txn,
342 desc->ad_type, desc->ad_lang,
360 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
361 "index_entry: %s (%s)%ld\n",
362 op == SLAP_INDEX_ADD_OP ? "add" : "del",
365 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
366 op == SLAP_INDEX_ADD_OP ? "add" : "del",
370 /* add each attribute to the indexes */
371 for ( ; ap != NULL; ap = ap->a_next ) {
372 rc = bdb_index_values( be, txn,
373 ap->a_desc, ap->a_vals, e->e_id, op );
375 if( rc != LDAP_SUCCESS ) {
377 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
378 "index_entry: success\n" ));
380 Debug( LDAP_DEBUG_TRACE,
381 "<= index_entry_%s( %ld, \"%s\" ) success\n",
382 op == SLAP_INDEX_ADD_OP ? "add" : "del",
390 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
391 "index_entry: success\n" ));
393 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
394 op == SLAP_INDEX_ADD_OP ? "add" : "del",