1 /* attr.c - backend routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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/socket.h>
22 #include <ac/string.h>
35 const AttributeDescription *desc = v_desc;
36 const AttrInfo *a = v_a;
37 return SLAP_PTRCMP(desc, a->ai_desc);
46 const AttrInfo *a = v_a, *b = v_b;
47 return SLAP_PTRCMP(a->ai_desc, b->ai_desc);
53 AttributeDescription *desc )
56 return avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
60 bdb_attr_index_config(
71 char **indexes = NULL;
73 attrs = ldap_str2charray( argv[0], "," );
76 fprintf( stderr, "%s: line %d: "
77 "no attributes specified: %s\n",
78 fname, lineno, argv[0] );
79 return LDAP_PARAM_ERROR;
83 indexes = ldap_str2charray( argv[1], "," );
85 if( indexes == NULL ) {
86 fprintf( stderr, "%s: line %d: "
87 "no indexes specified: %s\n",
88 fname, lineno, argv[1] );
89 return LDAP_PARAM_ERROR;
93 if( indexes == NULL ) {
94 mask = bdb->bi_defaultmask;
99 for ( i = 0; indexes[i] != NULL; i++ ) {
101 rc = slap_str2index( indexes[i], &index );
103 if( rc != LDAP_SUCCESS ) {
104 fprintf( stderr, "%s: line %d: "
105 "index type \"%s\" undefined\n",
106 fname, lineno, indexes[i] );
107 return LDAP_PARAM_ERROR;
115 fprintf( stderr, "%s: line %d: "
116 "no indexes selected\n",
118 return LDAP_PARAM_ERROR;
121 for ( i = 0; attrs[i] != NULL; i++ ) {
123 AttributeDescription *ad;
125 #ifdef LDAP_COMP_MATCH
126 ComponentReference* cr = NULL;
127 AttrInfo *a_cr = NULL;
130 if( strcasecmp( attrs[i], "default" ) == 0 ) {
131 bdb->bi_defaultmask |= mask;
135 #ifdef LDAP_COMP_MATCH
136 if ( is_component_reference( attrs[i] ) ) {
137 rc = extract_component_reference( attrs[i], &cr );
138 if ( rc != LDAP_SUCCESS ) {
139 fprintf( stderr, "%s: line %d: "
140 "index component reference\"%s\" undefined\n",
141 fname, lineno, attrs[i] );
144 cr->cr_indexmask = mask;
146 * After extracting a component reference
147 * only the name of a attribute will be remaining
153 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
155 #ifdef LDAP_COMP_MATCH
159 rc = slap_str2ad( attrs[i], &ad, &text );
161 if( rc != LDAP_SUCCESS ) {
162 fprintf( stderr, "%s: line %d: "
163 "index attribute \"%s\" undefined\n",
164 fname, lineno, attrs[i] );
168 if( slap_ad_is_binary( ad ) ) {
169 fprintf( stderr, "%s: line %d: "
170 "index of attribute \"%s\" disallowed\n",
171 fname, lineno, attrs[i] );
172 return LDAP_UNWILLING_TO_PERFORM;
175 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
176 ad->ad_type->sat_approx
177 && ad->ad_type->sat_approx->smr_indexer
178 && ad->ad_type->sat_approx->smr_filter ) )
180 fprintf( stderr, "%s: line %d: "
181 "approx index of attribute \"%s\" disallowed\n",
182 fname, lineno, attrs[i] );
183 return LDAP_INAPPROPRIATE_MATCHING;
186 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
187 ad->ad_type->sat_equality
188 && ad->ad_type->sat_equality->smr_indexer
189 && ad->ad_type->sat_equality->smr_filter ) )
191 fprintf( stderr, "%s: line %d: "
192 "equality index of attribute \"%s\" disallowed\n",
193 fname, lineno, attrs[i] );
194 return LDAP_INAPPROPRIATE_MATCHING;
197 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
198 ad->ad_type->sat_substr
199 && ad->ad_type->sat_substr->smr_indexer
200 && ad->ad_type->sat_substr->smr_filter ) )
202 fprintf( stderr, "%s: line %d: "
203 "substr index of attribute \"%s\" disallowed\n",
204 fname, lineno, attrs[i] );
205 return LDAP_INAPPROPRIATE_MATCHING;
208 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
209 ad->ad_cname.bv_val, mask, 0 );
213 if ( bdb->bi_flags & BDB_IS_OPEN ) {
215 a->ai_newmask = mask;
217 a->ai_indexmask = mask;
221 #ifdef LDAP_COMP_MATCH
223 a_cr = avl_find( bdb->bi_attrs, ad, ainfo_type_cmp );
226 * AttrInfo is already in AVL
227 * just add the extracted component reference
230 rc = insert_component_reference( cr, &a_cr->ai_cr );
231 if ( rc != LDAP_SUCCESS) {
232 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
233 return LDAP_PARAM_ERROR;
237 rc = insert_component_reference( cr, &a->ai_cr );
238 if ( rc != LDAP_SUCCESS) {
239 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
240 return LDAP_PARAM_ERROR;
245 rc = avl_insert( &bdb->bi_attrs, (caddr_t) a,
246 ainfo_cmp, avl_dup_error );
249 if ( bdb->bi_flags & BDB_IS_OPEN ) {
250 AttrInfo *b = avl_find( bdb->bi_attrs, ad, ainfo_type_cmp );
251 /* If we were editing this attr, reset it */
252 b->ai_indexmask &= ~BDB_INDEX_DELETING;
253 /* If this is leftover from a previous add, commit it */
255 b->ai_indexmask = b->ai_newmask;
256 b->ai_newmask = a->ai_newmask;
260 fprintf( stderr, "%s: line %d: duplicate index definition "
261 "for attr \"%s\" (ignored)\n",
262 fname, lineno, attrs[i] );
264 return LDAP_PARAM_ERROR;
268 ldap_charray_free( attrs );
269 if ( indexes != NULL ) ldap_charray_free( indexes );
275 bdb_attr_index_unparser( void *v1, void *v2 )
282 slap_index2bvlen( ai->ai_indexmask, &bv );
284 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
285 ptr = ch_malloc( bv.bv_len+1 );
286 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
288 slap_index2bv( ai->ai_indexmask, &bv );
290 ber_bvarray_add( bva, &bv );
294 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
295 static AttrInfo aidef = { &addef };
298 bdb_attr_index_unparse( struct bdb_info *bdb, BerVarray *bva )
300 if ( bdb->bi_defaultmask ) {
301 aidef.ai_indexmask = bdb->bi_defaultmask;
302 bdb_attr_index_unparser( &aidef, bva );
304 avl_apply( bdb->bi_attrs, bdb_attr_index_unparser, bva, -1, AVL_INORDER );
308 bdb_attrinfo_free( void *v )
311 #ifdef LDAP_COMP_MATCH
318 bdb_attr_index_destroy( Avlnode *tree )
320 avl_free( tree, bdb_attrinfo_free );
323 void bdb_attr_index_free( struct bdb_info *bdb, AttributeDescription *ad )
327 ai = avl_delete( &bdb->bi_attrs, ad, ainfo_type_cmp );
329 bdb_attrinfo_free( ai );
332 /* Get a list of AttrInfo's to delete */
334 typedef struct Alist {
340 bdb_attrinfo_flush( void *v1, void *arg )
344 if ( ai->ai_indexmask & BDB_INDEX_DELETING ) {
346 Alist *a = ch_malloc( sizeof( Alist ));
354 void bdb_attr_flush( struct bdb_info *bdb )
356 Alist *al = NULL, *a2;
358 avl_apply( bdb->bi_attrs, bdb_attrinfo_flush, &al, -1, AVL_INORDER );
360 while (( a2 = al )) {
362 avl_delete( &bdb->bi_attrs, a2->ptr, ainfo_cmp );
363 bdb_attrinfo_free( a2->ptr );