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 & 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 & SLAP_INDEX_NOSUBTYPES ) ) {
67 *atname = at->sat_cname;
68 *dbname = at->sat_cname.bv_val;
78 int bdb_index_is_indexed(
80 AttributeDescription *desc )
87 mask = index_mask( be, desc, &dbname, &prefix );
90 return LDAP_INAPPROPRIATE_MATCHING;
98 AttributeDescription *desc,
102 struct berval *prefixp )
109 mask = index_mask( be, desc, &dbname, prefixp );
112 return LDAP_INAPPROPRIATE_MATCHING;
115 rc = bdb_db_cache( be, dbname, &db );
117 if( rc != LDAP_SUCCESS ) {
122 case LDAP_FILTER_PRESENT:
123 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
128 case LDAP_FILTER_APPROX:
129 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
134 case LDAP_FILTER_EQUALITY:
135 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
140 case LDAP_FILTER_SUBSTRINGS:
141 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
150 return LDAP_INAPPROPRIATE_MATCHING;
162 struct berval *atname,
171 AttributeDescription *ad = NULL;
176 rc = bdb_db_cache( be, dbname, &db );
178 if ( rc != LDAP_SUCCESS ) {
180 LDAP_LOG(( "index", LDAP_LEVEL_ERR,
181 "bdb_index_read: Could not open DB %s\n", dbname));
183 Debug( LDAP_DEBUG_ANY,
184 "<= bdb_index_read NULL (could not open %s)\n",
190 rc = slap_bv2ad( atname, &ad, &text );
191 if( rc != LDAP_SUCCESS ) return rc;
193 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
194 rc = bdb_key_change( be, db, txn, &presence_key, id, op );
200 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
201 rc = ad->ad_type->sat_equality->smr_indexer(
202 LDAP_FILTER_EQUALITY,
204 ad->ad_type->sat_syntax,
205 ad->ad_type->sat_equality,
206 atname, vals, &keys );
208 if( rc == LDAP_SUCCESS && keys != NULL ) {
209 for( i=0; keys[i].bv_val != NULL; i++ ) {
210 rc = bdb_key_change( be, db, txn, &keys[i], id, op );
212 bvarray_free( keys );
216 bvarray_free( keys );
221 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
222 rc = ad->ad_type->sat_approx->smr_indexer(
225 ad->ad_type->sat_syntax,
226 ad->ad_type->sat_approx,
227 atname, vals, &keys );
229 if( rc == LDAP_SUCCESS && keys != NULL ) {
230 for( i=0; keys[i].bv_val != NULL; i++ ) {
231 rc = bdb_key_change( be, db, txn, &keys[i], id, op );
233 bvarray_free( keys );
237 bvarray_free( keys );
243 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
244 rc = ad->ad_type->sat_substr->smr_indexer(
245 LDAP_FILTER_SUBSTRINGS,
247 ad->ad_type->sat_syntax,
248 ad->ad_type->sat_substr,
249 atname, vals, &keys );
251 if( rc == LDAP_SUCCESS && keys != NULL ) {
252 for( i=0; keys[i].bv_val != NULL; i++ ) {
253 bdb_key_change( be, db, txn, &keys[i], id, op );
255 bvarray_free( keys );
259 bvarray_free( keys );
269 static int index_at_values(
281 slap_mask_t mask = 0;
282 slap_mask_t tmpmask = 0;
285 if( type->sat_sup ) {
287 rc = index_at_values( be, txn,
295 /* If this type has no AD, we've never used it before */
297 bdb_attr_mask( be->be_private, type->sat_ad, &mask );
301 *dbnamep = type->sat_cname.bv_val;
302 } else if ( !( tmpmask & SLAP_INDEX_AUTO_SUBTYPES ) ) {
307 rc = indexer( be, txn, *dbnamep,
318 AttributeDescription *desc;
323 desc = ad_find_lang( type, lang );
325 bdb_attr_mask( be->be_private, desc, &tmpmask );
329 dbname = desc->ad_cname.bv_val;
330 lname = desc->ad_cname;
334 if( dbname != NULL ) {
335 rc = indexer( be, txn, dbname, &lname,
340 ch_free( lname.bv_val );
351 int bdb_index_values(
354 AttributeDescription *desc,
363 rc = index_at_values( be, txn,
364 desc->ad_type, &desc->ad_lang,
382 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
383 "index_entry: %s (%s) %ld\n",
384 op == SLAP_INDEX_ADD_OP ? "add" : "del",
385 e->e_dn, (long) e->e_id ));
387 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
388 op == SLAP_INDEX_ADD_OP ? "add" : "del",
389 (long) e->e_id, e->e_dn );
392 /* add each attribute to the indexes */
393 for ( ; ap != NULL; ap = ap->a_next ) {
394 rc = bdb_index_values( be, txn,
395 ap->a_desc, ap->a_vals, e->e_id, op );
397 if( rc != LDAP_SUCCESS ) {
399 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
400 "index_entry: success\n" ));
402 Debug( LDAP_DEBUG_TRACE,
403 "<= index_entry_%s( %ld, \"%s\" ) success\n",
404 op == SLAP_INDEX_ADD_OP ? "add" : "del",
405 (long) e->e_id, e->e_dn );
412 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
413 "index_entry: success\n" ));
415 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
416 op == SLAP_INDEX_ADD_OP ? "add" : "del",
417 (long) e->e_id, e->e_dn );