1 /* index.c - routines for dealing with attribute indexes */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/index.c,v 1.61.2.7 2008/02/11 23:26:45 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 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
[] = {0,0};
29 static struct berval presence_key
= BER_BVC(presence_keyval
);
31 AttrInfo
*bdb_index_mask(
33 AttributeDescription
*desc
,
34 struct berval
*atname
)
37 AttrInfo
*ai
= bdb_attr_mask( be
->be_private
, desc
);
40 *atname
= desc
->ad_cname
;
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 ai
= bdb_attr_mask( be
->be_private
, desc
->ad_type
->sat_ad
);
51 if ( ai
&& !( ai
->ai_indexmask
& SLAP_INDEX_NOTAGS
) ) {
52 *atname
= desc
->ad_type
->sat_cname
;
57 /* see if supertype defined mask for its subtypes */
58 for( at
= desc
->ad_type
; at
!= NULL
; at
= at
->sat_sup
) {
59 /* If no AD, we've never indexed this type */
60 if ( !at
->sat_ad
) continue;
62 ai
= bdb_attr_mask( be
->be_private
, at
->sat_ad
);
64 if ( ai
&& !( ai
->ai_indexmask
& SLAP_INDEX_NOSUBTYPES
) ) {
65 *atname
= at
->sat_cname
;
73 /* This function is only called when evaluating search filters.
77 AttributeDescription
*desc
,
81 struct berval
*prefixp
)
85 slap_mask_t mask
, type
= 0;
88 ai
= bdb_index_mask( be
, desc
, prefixp
);
91 #ifdef BDB_MONITOR_IDX
93 case LDAP_FILTER_PRESENT
:
94 type
= SLAP_INDEX_PRESENT
;
96 case LDAP_FILTER_APPROX
:
97 type
= SLAP_INDEX_APPROX
;
99 case LDAP_FILTER_EQUALITY
:
100 type
= SLAP_INDEX_EQUALITY
;
102 case LDAP_FILTER_SUBSTRINGS
:
103 type
= SLAP_INDEX_SUBSTR
;
106 return LDAP_INAPPROPRIATE_MATCHING
;
108 bdb_monitor_idx_add( be
->be_private
, desc
, type
);
109 #endif /* BDB_MONITOR_IDX */
111 return LDAP_INAPPROPRIATE_MATCHING
;
113 mask
= ai
->ai_indexmask
;
115 rc
= bdb_db_cache( be
, prefixp
, &db
);
117 if( rc
!= LDAP_SUCCESS
) {
122 case LDAP_FILTER_PRESENT
:
123 type
= SLAP_INDEX_PRESENT
;
124 if( IS_SLAP_INDEX( mask
, SLAP_INDEX_PRESENT
) ) {
125 *prefixp
= presence_key
;
130 case LDAP_FILTER_APPROX
:
131 type
= SLAP_INDEX_APPROX
;
132 if ( desc
->ad_type
->sat_approx
) {
133 if( IS_SLAP_INDEX( mask
, SLAP_INDEX_APPROX
) ) {
139 /* Use EQUALITY rule and index for approximate match */
142 case LDAP_FILTER_EQUALITY
:
143 type
= SLAP_INDEX_EQUALITY
;
144 if( IS_SLAP_INDEX( mask
, SLAP_INDEX_EQUALITY
) ) {
149 case LDAP_FILTER_SUBSTRINGS
:
150 type
= SLAP_INDEX_SUBSTR
;
151 if( IS_SLAP_INDEX( mask
, SLAP_INDEX_SUBSTR
) ) {
160 #ifdef BDB_MONITOR_IDX
161 bdb_monitor_idx_add( be
->be_private
, desc
, type
);
162 #endif /* BDB_MONITOR_IDX */
164 return LDAP_INAPPROPRIATE_MATCHING
;
175 AttributeDescription
*ad
,
176 struct berval
*atname
,
188 rc
= bdb_db_cache( op
->o_bd
, atname
, &db
);
190 if ( rc
!= LDAP_SUCCESS
) {
191 Debug( LDAP_DEBUG_ANY
,
192 "bdb_index_read: Could not open DB %s\n",
193 atname
->bv_val
, 0, 0 );
197 if( IS_SLAP_INDEX( mask
, SLAP_INDEX_PRESENT
) ) {
198 rc
= bdb_key_change( op
->o_bd
, db
, txn
, &presence_key
, id
, opid
);
204 if( IS_SLAP_INDEX( mask
, SLAP_INDEX_EQUALITY
) ) {
205 rc
= ad
->ad_type
->sat_equality
->smr_indexer(
206 LDAP_FILTER_EQUALITY
,
208 ad
->ad_type
->sat_syntax
,
209 ad
->ad_type
->sat_equality
,
210 atname
, vals
, &keys
, op
->o_tmpmemctx
);
212 if( rc
== LDAP_SUCCESS
&& keys
!= NULL
) {
213 for( i
=0; keys
[i
].bv_val
!= NULL
; i
++ ) {
214 rc
= bdb_key_change( op
->o_bd
, db
, txn
, &keys
[i
], id
, opid
);
216 ber_bvarray_free_x( keys
, op
->o_tmpmemctx
);
220 ber_bvarray_free_x( keys
, op
->o_tmpmemctx
);
225 if( IS_SLAP_INDEX( mask
, SLAP_INDEX_APPROX
) ) {
226 rc
= ad
->ad_type
->sat_approx
->smr_indexer(
229 ad
->ad_type
->sat_syntax
,
230 ad
->ad_type
->sat_approx
,
231 atname
, vals
, &keys
, op
->o_tmpmemctx
);
233 if( rc
== LDAP_SUCCESS
&& keys
!= NULL
) {
234 for( i
=0; keys
[i
].bv_val
!= NULL
; i
++ ) {
235 rc
= bdb_key_change( op
->o_bd
, db
, txn
, &keys
[i
], id
, opid
);
237 ber_bvarray_free_x( keys
, op
->o_tmpmemctx
);
241 ber_bvarray_free_x( keys
, op
->o_tmpmemctx
);
247 if( IS_SLAP_INDEX( mask
, SLAP_INDEX_SUBSTR
) ) {
248 rc
= ad
->ad_type
->sat_substr
->smr_indexer(
249 LDAP_FILTER_SUBSTRINGS
,
251 ad
->ad_type
->sat_syntax
,
252 ad
->ad_type
->sat_substr
,
253 atname
, vals
, &keys
, op
->o_tmpmemctx
);
255 if( rc
== LDAP_SUCCESS
&& keys
!= NULL
) {
256 for( i
=0; keys
[i
].bv_val
!= NULL
; i
++ ) {
257 rc
= bdb_key_change( op
->o_bd
, db
, txn
, &keys
[i
], id
, opid
);
259 ber_bvarray_free_x( keys
, op
->o_tmpmemctx
);
263 ber_bvarray_free_x( keys
, op
->o_tmpmemctx
);
271 /* The callers all know how to deal with these results */
273 case DB_LOCK_DEADLOCK
:
274 case DB_LOCK_NOTGRANTED
:
276 /* Anything else is bad news */
283 static int index_at_values(
286 AttributeDescription
*ad
,
294 slap_mask_t mask
= 0;
298 if ( opid
== BDB_INDEX_UPDATE_OP
)
299 ixop
= SLAP_INDEX_ADD_OP
;
301 if( type
->sat_sup
) {
303 rc
= index_at_values( op
, txn
, NULL
,
310 /* If this type has no AD, we've never used it before */
312 ai
= bdb_attr_mask( op
->o_bd
->be_private
, type
->sat_ad
);
314 #ifdef LDAP_COMP_MATCH
315 /* component indexing */
317 ComponentReference
*cr
;
318 for( cr
= ai
->ai_cr
; cr
; cr
= cr
->cr_next
) {
319 rc
= indexer( op
, txn
, cr
->cr_ad
, &type
->sat_cname
,
320 cr
->cr_nvals
, id
, ixop
,
326 /* If we're updating the index, just set the new bits that aren't
327 * already in the old mask.
329 if ( opid
== BDB_INDEX_UPDATE_OP
)
330 mask
= ai
->ai_newmask
& ~ai
->ai_indexmask
;
332 /* For regular updates, if there is a newmask use it. Otherwise
333 * just use the old mask.
335 mask
= ai
->ai_newmask
? ai
->ai_newmask
: ai
->ai_indexmask
;
337 rc
= indexer( op
, txn
, ad
, &type
->sat_cname
,
338 vals
, id
, ixop
, mask
);
346 AttributeDescription
*desc
;
348 desc
= ad_find_tags( type
, tags
);
350 ai
= bdb_attr_mask( op
->o_bd
->be_private
, desc
);
353 if ( opid
== BDB_INDEX_UPDATE_OP
)
354 mask
= ai
->ai_newmask
& ~ai
->ai_indexmask
;
356 mask
= ai
->ai_newmask
? ai
->ai_newmask
: ai
->ai_indexmask
;
358 rc
= indexer( op
, txn
, desc
, &desc
->ad_cname
,
359 vals
, id
, ixop
, mask
);
372 int bdb_index_values(
375 AttributeDescription
*desc
,
382 /* Never index ID 0 */
386 rc
= index_at_values( op
, txn
, desc
,
387 desc
->ad_type
, &desc
->ad_tags
,
393 /* Get the list of which indices apply to this attr */
396 struct bdb_info
*bdb
,
405 if( type
->sat_sup
) {
407 rc
= bdb_index_recset( bdb
, a
, type
->sat_sup
, tags
, ir
);
410 /* If this type has no AD, we've never used it before */
412 slot
= bdb_attr_slot( bdb
, type
->sat_ad
, NULL
);
414 ir
[slot
].ai
= bdb
->bi_attrs
[slot
];
415 al
= ch_malloc( sizeof( AttrList
));
417 al
->next
= ir
[slot
].attrs
;
422 AttributeDescription
*desc
;
424 desc
= ad_find_tags( type
, tags
);
426 slot
= bdb_attr_slot( bdb
, desc
, NULL
);
428 ir
[slot
].ai
= bdb
->bi_attrs
[slot
];
429 al
= ch_malloc( sizeof( AttrList
));
431 al
->next
= ir
[slot
].attrs
;
439 /* Apply the indices for the recset */
440 int bdb_index_recrun(
442 struct bdb_info
*bdb
,
451 /* Never index ID 0 */
455 for (i
=base
; i
<bdb
->bi_nattrs
; i
+=slap_tool_thread_max
) {
457 if ( !ir
->ai
) continue;
458 while (( al
= ir
->attrs
)) {
459 ir
->attrs
= al
->next
;
460 rc
= indexer( op
, NULL
, ir
->ai
->ai_desc
,
461 &ir
->ai
->ai_desc
->ad_type
->sat_cname
,
462 al
->attr
->a_nvals
, id
, SLAP_INDEX_ADD_OP
,
463 ir
->ai
->ai_indexmask
);
479 Attribute
*ap
= e
->e_attrs
;
480 #if 0 /* ifdef LDAP_COMP_MATCH */
481 ComponentReference
*cr_list
= NULL
;
482 ComponentReference
*cr
= NULL
, *dupped_cr
= NULL
;
484 ComponentSyntaxInfo
* csi_attr
;
489 struct berval value
= {0};
492 /* Never index ID 0 */
496 Debug( LDAP_DEBUG_TRACE
, "=> index_entry_%s( %ld, \"%s\" )\n",
497 opid
== SLAP_INDEX_DELETE_OP
? "del" : "add",
498 (long) e
->e_id
, e
->e_dn
);
500 /* add each attribute to the indexes */
501 for ( ; ap
!= NULL
; ap
= ap
->a_next
) {
502 #if 0 /* ifdef LDAP_COMP_MATCH */
504 /* see if attribute has components to be indexed */
505 ai
= bdb_attr_mask( op
->o_bd
->be_private
, ap
->a_desc
->ad_type
->sat_ad
);
508 if ( attr_converter
&& cr_list
) {
509 syn
= ap
->a_desc
->ad_type
->sat_syntax
;
510 ap
->a_comp_data
= op
->o_tmpalloc( sizeof( ComponentData
), op
->o_tmpmemctx
);
511 /* Memory chunk(nibble) pre-allocation for decoders */
512 mem_op
= nibble_mem_allocator ( 1024*16, 1024*4 );
513 ap
->a_comp_data
->cd_mem_op
= mem_op
;
514 for( cr
= cr_list
; cr
; cr
= cr
->cr_next
) {
515 /* count how many values in an attribute */
516 for( num_attr
=0; ap
->a_vals
[num_attr
].bv_val
!= NULL
; num_attr
++ );
518 cr
->cr_nvals
= (BerVarray
)op
->o_tmpalloc( sizeof( struct berval
)*num_attr
, op
->o_tmpmemctx
);
519 for( i
=0; ap
->a_vals
[i
].bv_val
!= NULL
; i
++ ) {
520 /* decoding attribute value */
521 decoded_comp
= attr_converter ( ap
, syn
, &ap
->a_vals
[i
] );
523 return LDAP_DECODING_ERROR
;
524 /* extracting the referenced component */
525 dupped_cr
= dup_comp_ref( op
, cr
);
526 csi_attr
= ((ComponentSyntaxInfo
*)decoded_comp
)->csi_comp_desc
->cd_extract_i( mem_op
, dupped_cr
, decoded_comp
);
528 return LDAP_DECODING_ERROR
;
529 cr
->cr_asn_type_id
= csi_attr
->csi_comp_desc
->cd_type_id
;
530 cr
->cr_ad
= (AttributeDescription
*)get_component_description ( cr
->cr_asn_type_id
);
532 return LDAP_INVALID_SYNTAX
;
533 at
= cr
->cr_ad
->ad_type
;
534 /* encoding the value of component in GSER */
535 rc
= component_encoder( mem_op
, csi_attr
, &value
);
536 if ( rc
!= LDAP_SUCCESS
)
537 return LDAP_ENCODING_ERROR
;
538 /* Normalize the encoded component values */
539 if ( at
->sat_equality
&& at
->sat_equality
->smr_normalize
) {
540 rc
= at
->sat_equality
->smr_normalize (
541 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
,
542 at
->sat_syntax
, at
->sat_equality
,
543 &value
, &cr
->cr_nvals
[i
], op
->o_tmpmemctx
);
545 cr
->cr_nvals
[i
] = value
;
548 /* The end of BerVarray */
549 cr
->cr_nvals
[num_attr
-1].bv_val
= NULL
;
550 cr
->cr_nvals
[num_attr
-1].bv_len
= 0;
552 op
->o_tmpfree( ap
->a_comp_data
, op
->o_tmpmemctx
);
553 nibble_mem_free ( mem_op
);
554 ap
->a_comp_data
= NULL
;
557 rc
= bdb_index_values( op
, txn
, ap
->a_desc
,
558 ap
->a_nvals
, e
->e_id
, opid
);
560 if( rc
!= LDAP_SUCCESS
) {
561 Debug( LDAP_DEBUG_TRACE
,
562 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
563 opid
== SLAP_INDEX_ADD_OP
? "add" : "del",
564 (long) e
->e_id
, e
->e_dn
);
569 Debug( LDAP_DEBUG_TRACE
, "<= index_entry_%s( %ld, \"%s\" ) success\n",
570 opid
== SLAP_INDEX_DELETE_OP
? "del" : "add",
571 (long) e
->e_id
, e
->e_dn
);