1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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,
170 rc = bdb_db_cache( op->o_bd, atname->bv_val, &db );
172 if ( rc != LDAP_SUCCESS ) {
174 LDAP_LOG( INDEX, ERR,
175 "bdb_index_read: Could not open DB %s\n",
176 atname->bv_val, 0, 0 );
178 Debug( LDAP_DEBUG_ANY,
179 "bdb_index_read: Could not open DB %s\n",
180 atname->bv_val, 0, 0 );
185 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
186 rc = bdb_key_change( op->o_bd, db, txn, &presence_key, id, opid );
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, op->o_tmpmemctx );
200 if( rc == LDAP_SUCCESS && keys != NULL ) {
201 for( i=0; keys[i].bv_val != NULL; i++ ) {
202 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
204 ber_bvarray_free_x( keys, op->o_tmpmemctx );
208 ber_bvarray_free_x( keys, op->o_tmpmemctx );
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, op->o_tmpmemctx );
221 if( rc == LDAP_SUCCESS && keys != NULL ) {
222 for( i=0; keys[i].bv_val != NULL; i++ ) {
223 rc = bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
225 ber_bvarray_free_x( keys, op->o_tmpmemctx );
229 ber_bvarray_free_x( keys, op->o_tmpmemctx );
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, op->o_tmpmemctx );
243 if( rc == LDAP_SUCCESS && keys != NULL ) {
244 for( i=0; keys[i].bv_val != NULL; i++ ) {
245 bdb_key_change( op->o_bd, db, txn, &keys[i], id, opid );
247 ber_bvarray_free_x( keys, op->o_tmpmemctx );
251 ber_bvarray_free_x( keys, op->o_tmpmemctx );
261 static int index_at_values(
264 AttributeDescription *ad,
272 slap_mask_t mask = 0;
274 if( type->sat_sup ) {
276 rc = index_at_values( op, txn, NULL,
283 /* If this type has no AD, we've never used it before */
285 bdb_attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
290 rc = indexer( op, txn, ad, &type->sat_cname,
298 AttributeDescription *desc;
302 desc = ad_find_tags( type, tags );
304 bdb_attr_mask( op->o_bd->be_private, desc, &mask );
308 rc = indexer( op, txn, desc, &desc->ad_cname,
321 int bdb_index_values(
324 AttributeDescription *desc,
331 rc = index_at_values( op, txn, desc,
332 desc->ad_type, &desc->ad_tags,
346 Attribute *ap = e->e_attrs;
349 LDAP_LOG( INDEX, ENTRY, "index_entry: %s (%s) %ld\n",
350 opid == SLAP_INDEX_ADD_OP ? "add" : "del", e->e_dn, (long) e->e_id );
352 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
353 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
354 (long) e->e_id, e->e_dn );
357 /* add each attribute to the indexes */
358 for ( ; ap != NULL; ap = ap->a_next ) {
359 rc = bdb_index_values( op, txn, ap->a_desc,
360 ap->a_nvals, e->e_id, opid );
362 if( rc != LDAP_SUCCESS ) {
364 LDAP_LOG( INDEX, ENTRY,
365 "index_entry: failure (%d)\n", rc, 0, 0 );
367 Debug( LDAP_DEBUG_TRACE,
368 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
369 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
370 (long) e->e_id, e->e_dn );
377 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
379 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
380 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
381 (long) e->e_id, e->e_dn );