2 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1995 Regents of the University of Michigan.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that this notice is preserved and that due credit is given
11 * to the University of Michigan at Ann Arbor. The name of the University
12 * may not be used to endorse or promote products derived from this
13 * software without specific prior written permission. This software
14 * is provided ``as is'' without express or implied warranty.
22 modify_check_duplicates(
23 AttributeDescription *ad,
28 char *textbuf, size_t textlen )
30 int i, j, numvals = 0, nummods,
32 BerVarray nvals = NULL, nmods;
35 * FIXME: better do the following
37 * - count the existing values
38 * - count the new values
40 * - if the existing values are less than the new ones {
41 * - normalize all the existing values
42 * - for each new value {
44 * - check with existing
45 * - cross-check with already normalized new vals
48 * - for each new value {
50 * - cross-check with already normalized new vals
52 * - for each existing value {
54 * - check with already normalized new values
58 * The first case is good when adding a lot of new values,
59 * and significantly at first import of values (e.g. adding
60 * a new group); the latter case seems to be quite important
61 * as well, because it is likely to be the most frequently
62 * used when administering the entry. The current
63 * implementation will always normalize all the existing
64 * values before checking. If there's no duplicate, the
65 * performances should not change; they will in case of error.
68 for ( nummods = 0; mods[ nummods ].bv_val != NULL; nummods++ )
69 /* count new values */ ;
72 for ( numvals = 0; vals[ numvals ].bv_val != NULL; numvals++ )
73 /* count existing values */ ;
75 if ( numvals < nummods ) {
76 nvals = ch_calloc( numvals + 1, sizeof( struct berval ) );
78 /* normalize the existing values first */
79 for ( j = 0; vals[ j ].bv_val != NULL; j++ ) {
80 rc = value_normalize( ad, SLAP_MR_EQUALITY,
81 &vals[ j ], &nvals[ j ], text );
83 /* existing attribute values must normalize */
84 assert( rc == LDAP_SUCCESS );
86 if ( rc != LDAP_SUCCESS ) {
87 nvals[ j ].bv_val = NULL;
91 nvals[ j ].bv_val = NULL;
96 * If the existing values are less than the new values,
97 * it is more convenient to normalize all the existing
98 * values and test each new value against them first,
99 * then to other already normalized values
101 nmods = ch_calloc( nummods + 1, sizeof( struct berval ) );
103 for ( i = 0; mods[ i ].bv_val != NULL; i++ ) {
104 rc = value_normalize( ad, SLAP_MR_EQUALITY,
105 &mods[ i ], &nmods[ i ], text );
107 if ( rc != LDAP_SUCCESS ) {
108 nmods[ i ].bv_val = NULL;
112 if ( numvals > 0 && numvals < nummods ) {
113 for ( j = 0; nvals[ j ].bv_val; j++ ) {
116 rc = (*mr->smr_match)( &match,
117 SLAP_MR_VALUE_SYNTAX_MATCH,
118 ad->ad_type->sat_syntax,
119 mr, &nmods[ i ], &nvals[ j ] );
120 if ( rc != LDAP_SUCCESS ) {
121 nmods[ i + 1 ].bv_val = NULL;
126 snprintf( textbuf, textlen,
127 "%s: value #%d provided more than once",
128 ad->ad_cname.bv_val, i );
129 rc = LDAP_TYPE_OR_VALUE_EXISTS;
130 nmods[ i + 1 ].bv_val = NULL;
136 for ( j = 0; j < i; j++ ) {
139 rc = (*mr->smr_match)( &match,
140 SLAP_MR_VALUE_SYNTAX_MATCH,
141 ad->ad_type->sat_syntax,
142 mr, &nmods[ i ], &nmods[ j ] );
143 if ( rc != LDAP_SUCCESS ) {
144 nmods[ i + 1 ].bv_val = NULL;
149 snprintf( textbuf, textlen,
150 "%s: value #%d provided more than once",
151 ad->ad_cname.bv_val, j );
152 rc = LDAP_TYPE_OR_VALUE_EXISTS;
153 nmods[ i + 1 ].bv_val = NULL;
158 nmods[ i ].bv_val = NULL;
161 * if new values are more than existing values, it is more
162 * convenient to normalize and check all new values first,
163 * then check each new value against existing values, which
164 * can be normalized in place
167 if ( numvals >= nummods ) {
168 for ( j = 0; vals[ j ].bv_val; j++ ) {
169 struct berval asserted;
171 rc = value_normalize( ad, SLAP_MR_EQUALITY,
172 &vals[ j ], &asserted, text );
174 if ( rc != LDAP_SUCCESS ) {
178 for ( i = 0; nmods[ i ].bv_val; i++ ) {
181 rc = (*mr->smr_match)( &match,
182 SLAP_MR_VALUE_SYNTAX_MATCH,
183 ad->ad_type->sat_syntax,
184 mr, &nmods[ i ], &asserted );
185 if ( rc != LDAP_SUCCESS ) {
190 snprintf( textbuf, textlen,
191 "%s: value #%d provided more than once",
192 ad->ad_cname.bv_val, j );
193 rc = LDAP_TYPE_OR_VALUE_EXISTS;
203 ber_bvarray_free( nvals );
206 ber_bvarray_free( nmods );
217 char *textbuf, size_t textlen
222 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
225 switch( mod->sm_op ) {
229 case LDAP_MOD_REPLACE:
237 a = attr_find( e->e_attrs, mod->sm_desc );
239 /* check if the values we're adding already exist */
240 if( mr == NULL || !mr->smr_match ) {
242 /* do not allow add of additional attribute
243 if no equality rule exists */
245 snprintf( textbuf, textlen,
246 "modify/%s: %s: no equality matching rule",
247 op, mod->sm_desc->ad_cname.bv_val );
248 return LDAP_INAPPROPRIATE_MATCHING;
251 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
252 /* test asserted values against existing values */
254 for( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
255 if ( bvmatch( &mod->sm_bvalues[i],
258 /* value exists already */
260 snprintf( textbuf, textlen,
261 "modify/%s: %s: value #%i already exists",
262 op, mod->sm_desc->ad_cname.bv_val, j );
263 return LDAP_TYPE_OR_VALUE_EXISTS;
268 /* test asserted values against themselves */
269 for( j = 0; j < i; j++ ) {
270 if ( bvmatch( &mod->sm_bvalues[i],
271 &mod->sm_bvalues[j] ) ) {
273 /* value exists already */
275 snprintf( textbuf, textlen,
276 "modify/%s: %s: value #%i already exists",
277 op, mod->sm_desc->ad_cname.bv_val, j );
278 return LDAP_TYPE_OR_VALUE_EXISTS;
286 * The original code performs ( n ) normalizations
287 * and ( n * ( n - 1 ) / 2 ) matches, which hide
288 * the same number of normalizations. The new code
289 * performs the same number of normalizations ( n )
290 * and ( n * ( n - 1 ) / 2 ) mem compares, far less
291 * expensive than an entire match, if a match is
292 * equivalent to a normalization and a mem compare ...
294 * This is far more memory expensive than the previous,
295 * but it can heavily improve performances when big
296 * chunks of data are added (typical example is a group
297 * with thousands of DN-syntax members; on my system:
298 * for members of 5-RDN DNs,
300 members orig bvmatch (dirty) new
301 1000 0m38.456s 0m0.553s 0m0.608s
302 2000 2m33.341s 0m0.851s 0m1.003s
304 * Moreover, 100 groups with 10000 members each were
305 * added in 37m27.933s (an analogous LDIF file was
306 * loaded into Active Directory in 38m28.682s, BTW).
308 * Maybe we could switch to the new algorithm when
309 * the number of values overcomes a given threshold?
313 const char *text = NULL;
314 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
316 if ( mod->sm_bvalues[ 1 ].bv_val == 0 ) {
318 struct berval asserted;
321 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
322 &mod->sm_bvalues[ 0 ], &asserted, &text );
324 if ( rc != LDAP_SUCCESS ) {
328 for ( i = 0; a->a_vals[ i ].bv_val; i++ ) {
331 rc = value_match( &match, mod->sm_desc, mr,
332 SLAP_MR_VALUE_SYNTAX_MATCH,
333 &a->a_vals[ i ], &asserted, &text );
335 if( rc == LDAP_SUCCESS && match == 0 ) {
336 free( asserted.bv_val );
337 return LDAP_TYPE_OR_VALUE_EXISTS;
343 rc = modify_check_duplicates( mod->sm_desc, mr,
344 a ? a->a_vals : NULL, mod->sm_bvalues,
345 &text, textbuf, sizeof( textbuf ) );
347 if ( rc != LDAP_SUCCESS ) {
354 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
355 /* this should return result of attr_merge */
357 snprintf( textbuf, textlen,
358 "modify/%s: %s: merge error",
359 op, mod->sm_desc->ad_cname.bv_val );
367 modify_delete_values(
371 char *textbuf, size_t textlen
374 int i, j, k, rc = LDAP_SUCCESS;
376 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
377 BerVarray nvals = NULL;
380 /* delete the entire attribute */
381 if ( mod->sm_bvalues == NULL ) {
382 rc = attr_delete( &e->e_attrs, mod->sm_desc );
384 if( rc != LDAP_SUCCESS ) {
386 snprintf( textbuf, textlen,
387 "modify/delete: %s: no such attribute",
388 mod->sm_desc->ad_cname.bv_val );
389 rc = LDAP_NO_SUCH_ATTRIBUTE;
394 if( mr == NULL || !mr->smr_match ) {
395 /* disallow specific attributes from being deleted if
398 snprintf( textbuf, textlen,
399 "modify/delete: %s: no equality matching rule",
400 mod->sm_desc->ad_cname.bv_val );
401 return LDAP_INAPPROPRIATE_MATCHING;
404 /* delete specific values - find the attribute first */
405 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
407 snprintf( textbuf, textlen,
408 "modify/delete: %s: no such attribute",
409 mod->sm_desc->ad_cname.bv_val );
410 return LDAP_NO_SUCH_ATTRIBUTE;
413 /* find each value to delete */
414 for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ )
415 /* count existing values */ ;
417 nvals = (BerVarray)ch_calloc( j + 1, sizeof ( struct berval ) );
419 /* normalize existing values */
420 for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ ) {
421 rc = value_normalize( a->a_desc, SLAP_MR_EQUALITY,
422 &a->a_vals[ j ], &nvals[ j ], text );
424 if ( rc != LDAP_SUCCESS ) {
425 nvals[ j ].bv_val = NULL;
430 for ( i = 0; mod->sm_bvalues[ i ].bv_val != NULL; i++ ) {
431 struct berval asserted;
434 /* normalize the value to be deleted */
435 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
436 &mod->sm_bvalues[ i ], &asserted, text );
438 if( rc != LDAP_SUCCESS ) {
443 for ( j = 0; nvals[ j ].bv_val != NULL; j++ ) {
446 if ( nvals[ j ].bv_val == &dummy ) {
450 rc = (*mr->smr_match)( &match,
451 SLAP_MR_VALUE_SYNTAX_MATCH,
452 a->a_desc->ad_type->sat_syntax,
453 mr, &nvals[ j ], &asserted );
455 if ( rc != LDAP_SUCCESS ) {
456 free( asserted.bv_val );
466 /* delete value and mark it as dummy */
467 free( nvals[ j ].bv_val );
468 nvals[ j ].bv_val = &dummy;
473 free( asserted.bv_val );
477 snprintf( textbuf, textlen,
478 "modify/delete: %s: no such value",
479 mod->sm_desc->ad_cname.bv_val );
480 rc = LDAP_NO_SUCH_ATTRIBUTE;
485 /* compact array skipping dummies */
486 for ( k = 0, j = 0; nvals[ k ].bv_val != NULL; j++, k++ ) {
488 /* delete and skip dummies */ ;
489 for ( ; nvals[ k ].bv_val == &dummy; k++ ) {
490 free( a->a_vals[ k ].bv_val );
494 a->a_vals[ j ] = a->a_vals[ k ];
497 if ( a->a_vals[ k ].bv_val == NULL ) {
501 a->a_vals[ j ].bv_val = NULL;
503 assert( i == k - j );
505 /* if no values remain, delete the entire attribute */
506 if ( a->a_vals[0].bv_val == NULL ) {
507 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
509 snprintf( textbuf, textlen,
510 "modify/delete: %s: no such attribute",
511 mod->sm_desc->ad_cname.bv_val );
512 rc = LDAP_NO_SUCH_ATTRIBUTE;
518 /* delete the remaining normalized values */
519 for ( j = 0; nvals[ j ].bv_val != NULL; j++ ) {
520 if ( nvals[ j ].bv_val != &dummy ) {
521 ber_memfree( nvals[ j ].bv_val );
524 ber_memfree( nvals );
531 modify_replace_values(
535 char *textbuf, size_t textlen
538 (void) attr_delete( &e->e_attrs, mod->sm_desc );
540 if ( mod->sm_bvalues ) {
541 return modify_add_values( e, mod, text, textbuf, textlen );
554 if ( mod->sm_type.bv_val)
555 free( mod->sm_type.bv_val );
557 if ( mod->sm_bvalues != NULL )
558 ber_bvarray_free( mod->sm_bvalues );
571 for ( ; ml != NULL; ml = next ) {
574 slap_mod_free( &ml->sml_mod, 0 );