1 /* index.c - routines for dealing with attribute indexes */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
17 #include "lutil_hash.h"
19 static char presence_keyval[LUTIL_HASH_BYTES] = {0,0,0,1};
20 static struct berval presence_key = {LUTIL_HASH_BYTES, presence_keyval};
22 static slap_mask_t index_mask(
24 AttributeDescription *desc,
26 struct berval *atname )
31 bdb_attr_mask( be->be_private, desc, &mask );
34 *atname = desc->ad_cname;
35 *dbname = desc->ad_cname.bv_val;
39 /* If there is a language tag, did we ever index the base
40 * type? If so, check for mask, otherwise it's not there.
42 if( slap_ad_is_lang( desc ) && desc != desc->ad_type->sat_ad ) {
43 /* has language tag */
44 bdb_attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
46 if ( mask && ( mask ^ SLAP_INDEX_NOLANG ) ) {
47 *atname = desc->ad_type->sat_cname;
48 *dbname = desc->ad_type->sat_cname.bv_val;
53 /* see if supertype defined mask for its subtypes */
54 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
55 /* If no AD, we've never indexed this type */
56 if ( !at->sat_ad ) continue;
58 bdb_attr_mask( be->be_private, at->sat_ad, &mask );
60 if ( mask & SLAP_INDEX_AUTO_SUBTYPES ) {
61 *atname = desc->ad_type->sat_cname;
62 *dbname = at->sat_cname.bv_val;
66 if ( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
67 *atname = at->sat_cname;
68 *dbname = at->sat_cname.bv_val;
76 int bdb_index_is_indexed(
78 AttributeDescription *desc )
85 mask = index_mask( be, desc, &dbname, &prefix );
88 return LDAP_INAPPROPRIATE_MATCHING;
96 AttributeDescription *desc,
100 struct berval *prefixp )
107 mask = index_mask( be, desc, &dbname, prefixp );
110 return LDAP_INAPPROPRIATE_MATCHING;
113 rc = bdb_db_cache( be, dbname, &db );
115 if( rc != LDAP_SUCCESS ) {
120 case LDAP_FILTER_PRESENT:
121 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
126 case LDAP_FILTER_APPROX:
127 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
132 case LDAP_FILTER_EQUALITY:
133 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
138 case LDAP_FILTER_SUBSTRINGS:
139 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
148 return LDAP_INAPPROPRIATE_MATCHING;
160 struct berval *atname,
169 AttributeDescription *ad = NULL;
174 rc = bdb_db_cache( be, dbname, &db );
176 if ( rc != LDAP_SUCCESS ) {
178 LDAP_LOG(( "index", LDAP_LEVEL_ERR,
179 "bdb_index_read: Could not open DB %s\n", dbname));
181 Debug( LDAP_DEBUG_ANY,
182 "<= bdb_index_read NULL (could not open %s)\n",
188 rc = slap_bv2ad( atname, &ad, &text );
189 if( rc != LDAP_SUCCESS ) return rc;
191 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
192 rc = bdb_key_change( be, db, txn, &presence_key, id, op );
198 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
199 rc = ad->ad_type->sat_equality->smr_indexer(
200 LDAP_FILTER_EQUALITY,
202 ad->ad_type->sat_syntax,
203 ad->ad_type->sat_equality,
204 atname, vals, &keys );
206 if( rc == LDAP_SUCCESS && keys != NULL ) {
207 for( i=0; keys[i].bv_val != NULL; i++ ) {
208 rc = bdb_key_change( be, db, txn, &keys[i], id, op );
210 ber_bvarray_free( keys );
214 ber_bvarray_free( keys );
219 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
220 rc = ad->ad_type->sat_approx->smr_indexer(
223 ad->ad_type->sat_syntax,
224 ad->ad_type->sat_approx,
225 atname, vals, &keys );
227 if( rc == LDAP_SUCCESS && keys != NULL ) {
228 for( i=0; keys[i].bv_val != NULL; i++ ) {
229 rc = bdb_key_change( be, db, txn, &keys[i], id, op );
231 ber_bvarray_free( keys );
235 ber_bvarray_free( keys );
241 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
242 rc = ad->ad_type->sat_substr->smr_indexer(
243 LDAP_FILTER_SUBSTRINGS,
245 ad->ad_type->sat_syntax,
246 ad->ad_type->sat_substr,
247 atname, vals, &keys );
249 if( rc == LDAP_SUCCESS && keys != NULL ) {
250 for( i=0; keys[i].bv_val != NULL; i++ ) {
251 bdb_key_change( be, db, txn, &keys[i], id, op );
253 ber_bvarray_free( keys );
257 ber_bvarray_free( keys );
267 static int index_at_values(
279 slap_mask_t mask = 0;
280 slap_mask_t tmpmask = 0;
283 if( type->sat_sup ) {
285 rc = index_at_values( be, txn,
293 /* If this type has no AD, we've never used it before */
295 bdb_attr_mask( be->be_private, type->sat_ad, &mask );
299 *dbnamep = type->sat_cname.bv_val;
300 } else if ( tmpmask & SLAP_INDEX_AUTO_SUBTYPES ) {
306 rc = indexer( be, txn, *dbnamep,
317 AttributeDescription *desc;
322 desc = ad_find_lang( type, lang );
324 bdb_attr_mask( be->be_private, desc, &tmpmask );
328 dbname = desc->ad_cname.bv_val;
329 lname = desc->ad_cname;
333 if( dbname != NULL ) {
334 rc = indexer( be, txn, dbname, &lname,
339 ch_free( lname.bv_val );
350 int bdb_index_values(
353 AttributeDescription *desc,
362 rc = index_at_values( be, txn,
363 desc->ad_type, &desc->ad_lang,
381 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
382 "index_entry: %s (%s) %ld\n",
383 op == SLAP_INDEX_ADD_OP ? "add" : "del",
384 e->e_dn, (long) e->e_id ));
386 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
387 op == SLAP_INDEX_ADD_OP ? "add" : "del",
388 (long) e->e_id, e->e_dn );
391 /* add each attribute to the indexes */
392 for ( ; ap != NULL; ap = ap->a_next ) {
393 rc = bdb_index_values( be, txn,
394 ap->a_desc, ap->a_vals, e->e_id, op );
396 if( rc != LDAP_SUCCESS ) {
398 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
399 "index_entry: success\n" ));
401 Debug( LDAP_DEBUG_TRACE,
402 "<= index_entry_%s( %ld, \"%s\" ) success\n",
403 op == SLAP_INDEX_ADD_OP ? "add" : "del",
404 (long) e->e_id, e->e_dn );
411 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
412 "index_entry: success\n" ));
414 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
415 op == SLAP_INDEX_ADD_OP ? "add" : "del",
416 (long) e->e_id, e->e_dn );