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 && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
61 *atname = desc->ad_type->sat_cname;
62 *dbname = at->sat_cname.bv_val;
70 int bdb_index_is_indexed(
72 AttributeDescription *desc )
79 mask = index_mask( be, desc, &dbname, &prefix );
82 return LDAP_INAPPROPRIATE_MATCHING;
90 AttributeDescription *desc,
94 struct berval *prefixp )
101 mask = index_mask( be, desc, &dbname, prefixp );
104 return LDAP_INAPPROPRIATE_MATCHING;
107 rc = bdb_db_cache( be, dbname, &db );
109 if( rc != LDAP_SUCCESS ) {
114 case LDAP_FILTER_PRESENT:
115 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
120 case LDAP_FILTER_APPROX:
121 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
126 case LDAP_FILTER_EQUALITY:
127 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
132 case LDAP_FILTER_SUBSTRINGS:
133 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
142 return LDAP_INAPPROPRIATE_MATCHING;
154 struct berval *atname,
163 AttributeDescription *ad = NULL;
168 rc = bdb_db_cache( be, dbname, &db );
170 if ( rc != LDAP_SUCCESS ) {
172 LDAP_LOG(( "index", LDAP_LEVEL_ERR,
173 "bdb_index_read: Could not open DB %s\n", dbname));
175 Debug( LDAP_DEBUG_ANY,
176 "<= bdb_index_read NULL (could not open %s)\n",
182 rc = slap_bv2ad( atname, &ad, &text );
183 if( rc != LDAP_SUCCESS ) return rc;
185 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
186 rc = bdb_key_change( be, db, txn, &presence_key, id, op );
192 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
193 rc = ad->ad_type->sat_equality->smr_indexer(
194 LDAP_FILTER_EQUALITY,
196 ad->ad_type->sat_syntax,
197 ad->ad_type->sat_equality,
198 atname, vals, &keys );
200 if( rc == LDAP_SUCCESS && keys != NULL ) {
201 for( i=0; keys[i].bv_val != NULL; i++ ) {
202 rc = bdb_key_change( be, db, txn, &keys[i], id, op );
204 ber_bvarray_free( keys );
208 ber_bvarray_free( keys );
213 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
214 rc = ad->ad_type->sat_approx->smr_indexer(
217 ad->ad_type->sat_syntax,
218 ad->ad_type->sat_approx,
219 atname, vals, &keys );
221 if( rc == LDAP_SUCCESS && keys != NULL ) {
222 for( i=0; keys[i].bv_val != NULL; i++ ) {
223 rc = bdb_key_change( be, db, txn, &keys[i], id, op );
225 ber_bvarray_free( keys );
229 ber_bvarray_free( keys );
235 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
236 rc = ad->ad_type->sat_substr->smr_indexer(
237 LDAP_FILTER_SUBSTRINGS,
239 ad->ad_type->sat_syntax,
240 ad->ad_type->sat_substr,
241 atname, vals, &keys );
243 if( rc == LDAP_SUCCESS && keys != NULL ) {
244 for( i=0; keys[i].bv_val != NULL; i++ ) {
245 bdb_key_change( be, db, txn, &keys[i], id, op );
247 ber_bvarray_free( keys );
251 ber_bvarray_free( keys );
261 static int index_at_values(
273 slap_mask_t mask = 0;
274 slap_mask_t tmpmask = 0;
277 if( type->sat_sup ) {
279 rc = index_at_values( be, txn,
287 /* If this type has no AD, we've never used it before */
289 bdb_attr_mask( be->be_private, type->sat_ad, &mask );
293 *dbnamep = type->sat_cname.bv_val;
294 } else if ( tmpmask ^ SLAP_INDEX_NOSUBTYPES ) {
299 rc = indexer( be, txn, *dbnamep,
311 AttributeDescription *desc;
316 desc = ad_find_lang( type, lang );
318 bdb_attr_mask( be->be_private, desc, &tmpmask );
322 dbname = desc->ad_cname.bv_val;
323 lname = desc->ad_cname;
327 if( dbname != NULL ) {
328 rc = indexer( be, txn, dbname, &lname,
333 ch_free( lname.bv_val );
344 int bdb_index_values(
347 AttributeDescription *desc,
356 rc = index_at_values( be, txn,
357 desc->ad_type, &desc->ad_lang,
375 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
376 "index_entry: %s (%s) %ld\n",
377 op == SLAP_INDEX_ADD_OP ? "add" : "del",
378 e->e_dn, (long) e->e_id ));
380 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
381 op == SLAP_INDEX_ADD_OP ? "add" : "del",
382 (long) e->e_id, e->e_dn );
385 /* add each attribute to the indexes */
386 for ( ; ap != NULL; ap = ap->a_next ) {
387 rc = bdb_index_values( be, txn,
388 ap->a_desc, ap->a_vals, e->e_id, op );
390 if( rc != LDAP_SUCCESS ) {
392 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
393 "index_entry: success\n" ));
395 Debug( LDAP_DEBUG_TRACE,
396 "<= index_entry_%s( %ld, \"%s\" ) success\n",
397 op == SLAP_INDEX_ADD_OP ? "add" : "del",
398 (long) e->e_id, e->e_dn );
405 LDAP_LOG(( "index", LDAP_LEVEL_ENTRY,
406 "index_entry: success\n" ));
408 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
409 op == SLAP_INDEX_ADD_OP ? "add" : "del",
410 (long) e->e_id, e->e_dn );