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>
16 #include "back-ldbm.h"
18 static slap_mask_t index_mask(
20 AttributeDescription *desc,
27 attr_mask( be->be_private, desc->ad_cname.bv_val, &mask );
30 *atname = desc->ad_cname.bv_val;
31 *dbname = desc->ad_cname.bv_val;
35 if( slap_ad_is_lang( desc ) ) {
36 /* has language tag */
37 attr_mask( be->be_private, desc->ad_type->sat_cname, &mask );
39 if( mask & SLAP_INDEX_AUTO_LANG ) {
40 *atname = desc->ad_cname.bv_val;
41 *dbname = desc->ad_type->sat_cname;
44 if( mask & SLAP_INDEX_LANG ) {
45 *atname = desc->ad_type->sat_cname;
46 *dbname = desc->ad_type->sat_cname;
51 /* see if supertype defined mask for its subtypes */
52 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
53 attr_mask( be->be_private, at->sat_cname, &mask );
55 if( mask & SLAP_INDEX_AUTO_SUBTYPES ) {
56 *atname = desc->ad_type->sat_cname;
57 *dbname = at->sat_cname;
60 if( mask & SLAP_INDEX_SUBTYPES ) {
61 *atname = at->sat_cname;
62 *dbname = at->sat_cname;
74 AttributeDescription *desc,
78 struct berval **prefixp )
84 mask = index_mask( be, desc, &dbname, &atname );
87 return LDAP_INAPPROPRIATE_MATCHING;
91 case LDAP_FILTER_PRESENT:
92 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
97 case LDAP_FILTER_APPROX:
98 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
103 case LDAP_FILTER_EQUALITY:
104 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
109 case LDAP_FILTER_SUBSTRINGS:
110 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
119 return LDAP_INAPPROPRIATE_MATCHING;
123 *prefixp = ber_bvstrdup( atname );
132 struct berval **vals,
140 AttributeDescription *ad = NULL;
141 struct berval **keys;
142 struct berval prefix;
146 rc = slap_str2ad( atname, &ad, &text );
148 if( rc != LDAP_SUCCESS ) return rc;
150 prefix.bv_val = atname;
151 prefix.bv_len = strlen( atname );
153 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
157 LDAP_LOG(( "index", LDAP_LEVEL_ERR,
158 "index_read: Could not open db %s%s\n",
159 dbname, LDBM_SUFFIX ));
161 Debug( LDAP_DEBUG_ANY,
162 "<= index_read NULL (could not open %s%s)\n",
163 dbname, LDBM_SUFFIX, 0 );
169 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
170 key_change( be, db, &prefix, id, op );
173 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
174 rc = ad->ad_type->sat_equality->smr_indexer(
175 LDAP_FILTER_EQUALITY,
177 ad->ad_type->sat_syntax,
178 ad->ad_type->sat_equality,
179 &prefix, vals, &keys );
181 if( rc == LDAP_SUCCESS && keys != NULL ) {
182 for( i=0; keys[i] != NULL; i++ ) {
183 key_change( be, db, keys[i], id, op );
185 ber_bvecfree( keys );
189 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
190 rc = ad->ad_type->sat_approx->smr_indexer(
193 ad->ad_type->sat_syntax,
194 ad->ad_type->sat_approx,
195 &prefix, vals, &keys );
197 if( rc == LDAP_SUCCESS && keys != NULL ) {
198 for( i=0; keys[i] != NULL; i++ ) {
199 key_change( be, db, keys[i], id, op );
201 ber_bvecfree( keys );
205 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
206 rc = ad->ad_type->sat_substr->smr_indexer(
207 LDAP_FILTER_SUBSTRINGS,
209 ad->ad_type->sat_syntax,
210 ad->ad_type->sat_substr,
211 &prefix, vals, &keys );
213 if( rc == LDAP_SUCCESS && keys != NULL ) {
214 for( i=0; keys[i] != NULL; i++ ) {
215 key_change( be, db, keys[i], id, op );
217 ber_bvecfree( keys );
221 ldbm_cache_close( be, db );
225 static int index_at_values(
229 struct berval **vals,
236 slap_mask_t tmpmask = 0;
239 if( type->sat_sup ) {
241 (void) index_at_values( be,
247 attr_mask( be->be_private, type->sat_cname, &mask );
250 *dbnamep = type->sat_cname;
251 } else if ( tmpmask & SLAP_INDEX_AUTO_SUBTYPES ) {
256 indexer( be, *dbnamep,
264 size_t tlen = strlen( type->sat_cname );
265 size_t llen = lang->bv_len;
266 char *lname = ch_malloc( tlen + llen + sizeof(";") );
268 sprintf( lname, "%s;%s", type->sat_cname, lang->bv_val );
270 attr_mask( be->be_private, lname, &tmpmask );
274 } else if ( mask & SLAP_INDEX_AUTO_LANG ) {
279 if( dbname != NULL ) {
280 indexer( be, dbname, lname,
293 AttributeDescription *desc,
294 struct berval **vals,
301 (void) index_at_values( be,
302 desc->ad_type, &desc->ad_lang,
319 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
320 "index_entry: %s (%s)%ld\n",
321 op == SLAP_INDEX_ADD_OP ? "add" : "del",
324 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
325 op == SLAP_INDEX_ADD_OP ? "add" : "del",
330 /* add each attribute to the indexes */
331 for ( ; ap != NULL; ap = ap->a_next ) {
332 index_values( be, ap->a_desc, ap->a_vals, e->e_id, op );
336 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
337 "index_entry: success\n" ));
339 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
340 op == SLAP_INDEX_ADD_OP ? "add" : "del",