1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
26 #include "lutil_hash.h"
28 static char presence_keyval[LUTIL_HASH_BYTES] = {0,0,0,1};
29 static struct berval presence_key = {LUTIL_HASH_BYTES, presence_keyval};
31 static slap_mask_t index_mask(
33 AttributeDescription *desc,
34 struct berval *atname )
39 bdb_attr_mask( be->be_private, desc, &mask );
42 *atname = desc->ad_cname;
46 /* If there is a tagging option, did we ever index the base
47 * type? If so, check for mask, otherwise it's not there.
49 if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
50 /* has tagging option */
51 bdb_attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
53 if ( mask && ( mask ^ SLAP_INDEX_NOTAGS ) ) {
54 *atname = desc->ad_type->sat_cname;
59 /* see if supertype defined mask for its subtypes */
60 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
61 /* If no AD, we've never indexed this type */
62 if ( !at->sat_ad ) continue;
64 bdb_attr_mask( be->be_private, at->sat_ad, &mask );
66 if ( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
67 *atname = at->sat_cname;
75 int bdb_index_is_indexed(
77 AttributeDescription *desc )
82 mask = index_mask( be, desc, &prefix );
85 return LDAP_INAPPROPRIATE_MATCHING;
93 AttributeDescription *desc,
97 struct berval *prefixp )
103 mask = index_mask( be, desc, prefixp );
106 return LDAP_INAPPROPRIATE_MATCHING;
109 rc = bdb_db_cache( be, prefixp->bv_val, &db );
111 if( rc != LDAP_SUCCESS ) {
116 case LDAP_FILTER_PRESENT:
117 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
118 *prefixp = presence_key;
123 case LDAP_FILTER_APPROX:
124 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
129 case LDAP_FILTER_EQUALITY:
130 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
135 case LDAP_FILTER_SUBSTRINGS:
136 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
145 return LDAP_INAPPROPRIATE_MATCHING;
156 AttributeDescription *ad,
157 struct berval *atname,
171 rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
173 if ( rc != LDAP_SUCCESS ) {
175 LDAP_LOG( INDEX, ERR,
176 "bdb_index_read: Could not open DB %s\n",
177 atname->bv_val, 0, 0 );
179 Debug( LDAP_DEBUG_ANY,
180 "bdb_index_read: Could not open DB %s\n",
181 atname->bv_val, 0, 0 );
186 #if 0 /* No longer needed, our frees are in order so nothing accumulates */
187 mark = sl_mark(op->o_tmpmemctx);
190 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
191 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
197 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
198 rc = ad->ad_type->sat_equality->smr_indexer(
199 LDAP_FILTER_EQUALITY,
201 ad->ad_type->sat_syntax,
202 ad->ad_type->sat_equality,
203 atname, vals, &keys, op->o_tmpmemctx );
205 if( rc == LDAP_SUCCESS && keys != NULL ) {
206 for( i=0; keys[i].bv_val != NULL; i++ ) {
207 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
209 ber_bvarray_free_x( keys, op->o_tmpmemctx );
213 ber_bvarray_free_x( keys, op->o_tmpmemctx );
218 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
219 rc = ad->ad_type->sat_approx->smr_indexer(
222 ad->ad_type->sat_syntax,
223 ad->ad_type->sat_approx,
224 atname, vals, &keys, op->o_tmpmemctx );
226 if( rc == LDAP_SUCCESS && keys != NULL ) {
227 for( i=0; keys[i].bv_val != NULL; i++ ) {
228 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
230 ber_bvarray_free_x( keys, op->o_tmpmemctx );
234 ber_bvarray_free_x( keys, op->o_tmpmemctx );
240 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
241 rc = ad->ad_type->sat_substr->smr_indexer(
242 LDAP_FILTER_SUBSTRINGS,
244 ad->ad_type->sat_syntax,
245 ad->ad_type->sat_substr,
246 atname, vals, &keys, op->o_tmpmemctx );
248 if( rc == LDAP_SUCCESS && keys != NULL ) {
249 for( i=0; keys[i].bv_val != NULL; i++ ) {
250 bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
252 ber_bvarray_free_x( keys, op->o_tmpmemctx );
256 ber_bvarray_free_x( keys, op->o_tmpmemctx );
264 sl_release( mark, op->o_tmpmemctx );
269 static int index_at_values(
272 AttributeDescription *ad,
280 slap_mask_t mask = 0;
282 if( type->sat_sup ) {
284 rc = index_at_values( op, txn, NULL,
291 /* If this type has no AD, we've never used it before */
293 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
298 rc = indexer( op, txn, ad, &type->sat_cname,
306 AttributeDescription *desc;
310 desc = ad_find_tags( type, tags );
312 bdb_attr_mask( op->o_bd->be_private, desc, &mask );
316 rc = indexer( op, txn, desc, &desc->ad_cname,
329 int bdb_index_values(
332 AttributeDescription *desc,
339 rc = index_at_values( op, txn, desc,
340 desc->ad_type, &desc->ad_tags,
354 Attribute *ap = e->e_attrs;
357 LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
358 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
360 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
361 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
362 (long) e->e_id, e->e_dn );
365 /* add each attribute to the indexes */
366 for ( ; ap != NULL; ap = ap->a_next ) {
367 rc = bdb_index_values( op, txn, ap->a_desc,
368 ap->a_nvals, e->e_id, opid );
370 if( rc != LDAP_SUCCESS ) {
372 LDAP_LOG( INDEX, ENTRY,
373 "index_entry: failure (%d)\n", rc, 0, 0 );
375 Debug( LDAP_DEBUG_TRACE,
376 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
377 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
378 (long) e->e_id, e->e_dn );
385 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
387 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
388 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
389 (long) e->e_id, e->e_dn );