1 /* index.c - routines for dealing with attribute indexes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-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>
25 #include "back-ldbm.h"
27 static slap_mask_t index_mask(
29 AttributeDescription *desc,
31 struct berval *atname )
36 attr_mask( be->be_private, desc, &mask );
39 *atname = desc->ad_cname;
40 *dbname = desc->ad_cname.bv_val;
44 /* If there is a tagging option, did we ever index the base
45 * type? If so, check for mask, otherwise it's not there.
47 if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
48 /* has tagging option */
49 attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
51 if( mask && ( mask ^ SLAP_INDEX_NOTAGS ) ) {
52 *atname = desc->ad_type->sat_cname;
53 *dbname = desc->ad_type->sat_cname.bv_val;
58 /* see if supertype defined mask for its subtypes */
59 for( at = desc->ad_type->sat_sup; at != NULL ; at = at->sat_sup ) {
60 /* If no AD, we've never indexed this type */
64 attr_mask( be->be_private, at->sat_ad, &mask );
66 if( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
67 *atname = at->sat_cname;
68 *dbname = at->sat_cname.bv_val;
78 AttributeDescription *desc )
84 mask = index_mask( be, desc, &dbname, &prefix );
87 return LDAP_INAPPROPRIATE_MATCHING;
95 AttributeDescription *desc,
99 struct berval *prefixp )
104 mask = index_mask( be, desc, &dbname, prefixp );
107 return LDAP_INAPPROPRIATE_MATCHING;
111 case LDAP_FILTER_PRESENT:
112 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
117 case LDAP_FILTER_APPROX:
118 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
123 case LDAP_FILTER_EQUALITY:
124 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
129 case LDAP_FILTER_SUBSTRINGS:
130 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
139 return LDAP_INAPPROPRIATE_MATCHING;
150 struct berval *atname,
159 AttributeDescription *ad = NULL;
165 rc = slap_bv2ad( atname, &ad, &text );
167 if( rc != LDAP_SUCCESS ) return rc;
169 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
173 LDAP_LOG( INDEX, ERR,
174 "index_read: Could not open db %s%s\n", dbname, LDBM_SUFFIX, 0 );
176 Debug( LDAP_DEBUG_ANY,
177 "<= index_read NULL (could not open %s%s)\n",
178 dbname, LDBM_SUFFIX, 0 );
184 mark = sl_mark( op->o_tmpmemctx );
186 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
187 key_change( op->o_bd, db, atname, id, opid );
190 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
191 rc = ad->ad_type->sat_equality->smr_indexer(
192 LDAP_FILTER_EQUALITY,
194 ad->ad_type->sat_syntax,
195 ad->ad_type->sat_equality,
196 atname, vals, &keys, op->o_tmpmemctx );
198 if( rc == LDAP_SUCCESS && keys != NULL ) {
199 for( i=0; keys[i].bv_val != NULL; i++ ) {
200 key_change( op->o_bd, db, &keys[i], id, opid );
202 ber_bvarray_free_x( keys, op->o_tmpmemctx );
206 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
207 rc = ad->ad_type->sat_approx->smr_indexer(
210 ad->ad_type->sat_syntax,
211 ad->ad_type->sat_approx,
212 atname, vals, &keys, op->o_tmpmemctx );
214 if( rc == LDAP_SUCCESS && keys != NULL ) {
215 for( i=0; keys[i].bv_val != NULL; i++ ) {
216 key_change( op->o_bd, db, &keys[i], id, opid );
218 ber_bvarray_free_x( keys, op->o_tmpmemctx );
222 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
223 rc = ad->ad_type->sat_substr->smr_indexer(
224 LDAP_FILTER_SUBSTRINGS,
226 ad->ad_type->sat_syntax,
227 ad->ad_type->sat_substr,
228 atname, vals, &keys, op->o_tmpmemctx );
230 if( rc == LDAP_SUCCESS && keys != NULL ) {
231 for( i=0; keys[i].bv_val != NULL; i++ ) {
232 key_change( op->o_bd, db, &keys[i], id, opid );
234 ber_bvarray_free_x( keys, op->o_tmpmemctx );
238 ldbm_cache_close( op->o_bd, db );
240 sl_release( mark, op->o_tmpmemctx );
245 static int index_at_values(
253 slap_mask_t mask = 0;
255 if( type->sat_sup ) {
257 (void) index_at_values( op,
262 /* If this type has no AD, we've never used it before */
264 attr_mask( op->o_bd->be_private, type->sat_ad, &mask );
268 indexer( op, type->sat_cname.bv_val,
275 AttributeDescription *desc;
279 desc = ad_find_tags(type, tags);
281 attr_mask( op->o_bd->be_private, desc, &mask );
285 indexer( op, desc->ad_cname.bv_val, &desc->ad_cname,
296 AttributeDescription *desc,
301 (void) index_at_values( op,
302 desc->ad_type, &desc->ad_tags,
314 Attribute *ap = e->e_attrs;
316 LDAP_LOG( INDEX, ENTRY,
317 "index_entry: %s (%s)%ld\n", opid == SLAP_INDEX_ADD_OP ? "add" : "del",
320 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
321 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
325 /* add each attribute to the indexes */
326 for ( ; ap != NULL; ap = ap->a_next ) {
327 index_values( op, ap->a_desc,
333 LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
335 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
336 opid == SLAP_INDEX_ADD_OP ? "add" : "del",