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.
21 #undef QUICK_DIRTY_DUPLICATE_CHECK
24 modify_check_duplicates(
25 AttributeDescription *ad,
30 char *textbuf, size_t textlen )
32 int i, j, numvals = 0, nummods,
34 BerVarray nvals = NULL, nmods;
37 * FIXME: better do the following
39 * - count the existing values
40 * - count the new values
42 * - if the existing values are less than the new ones {
43 * - normalize all the existing values
44 * - for each new value {
46 * - check with existing
47 * - cross-check with already normalized new vals
50 * - for each new value {
52 * - cross-check with already normalized new vals
54 * - for each existing value {
56 * - check with already normalized new values
60 * The first case is good when adding a lot of new values,
61 * and significantly at first import of values (e.g. adding
62 * a new group); the latter case seems to be quite important
63 * as well, because it is likely to be the most frequently
64 * used when administering the entry. The current
65 * implementation will always normalize all the existing
66 * values before checking. If there's no duplicate, the
67 * performances should not change; they will in case of error.
70 for ( nummods = 0; mods[ nummods ].bv_val != NULL; nummods++ )
71 /* count new values */ ;
74 for ( numvals = 0; vals[ numvals ].bv_val != NULL; numvals++ )
75 /* count existing values */ ;
78 if ( numvals > 0 && numvals < nummods ) {
79 nvals = ch_calloc( numvals + 1, sizeof( struct berval ) );
81 /* normalize the existing values first */
82 for ( j = 0; vals[ j ].bv_val != NULL; j++ ) {
83 rc = value_normalize( ad, SLAP_MR_EQUALITY,
84 &vals[ j ], &nvals[ j ], text );
86 /* existing attribute values must normalize */
87 assert( rc == LDAP_SUCCESS );
89 if ( rc != LDAP_SUCCESS ) {
90 nvals[ j ].bv_val = NULL;
94 nvals[ j ].bv_val = NULL;
98 * If the existing values are less than the new values,
99 * it is more convenient to normalize all the existing
100 * values and test each new value against them first,
101 * then to other already normalized values
103 nmods = ch_calloc( nummods + 1, sizeof( struct berval ) );
105 for ( i = 0; mods[ i ].bv_val != NULL; i++ ) {
106 rc = value_normalize( ad, SLAP_MR_EQUALITY,
107 &mods[ i ], &nmods[ i ], text );
109 if ( rc != LDAP_SUCCESS ) {
110 nmods[ i ].bv_val = NULL;
114 if ( numvals > 0 && numvals < nummods ) {
115 for ( j = 0; nvals[ j ].bv_val; j++ ) {
116 #ifdef QUICK_DIRTY_DUPLICATE_CHECK
117 if ( bvmatch( &nmods[ i ], &nvals[ j ] ) ) {
118 #else /* !QUICK_DIRTY_DUPLICATE_CHECK */
121 rc = (mr->smr_match)( &match,
122 SLAP_MR_VALUE_SYNTAX_MATCH,
123 ad->ad_type->sat_syntax,
124 mr, &nmods[ i ], &nvals[ j ] );
125 if ( rc != LDAP_SUCCESS ) {
126 nmods[ i + 1 ].bv_val = NULL;
131 #endif /* !QUICK_DIRTY_DUPLICATE_CHECK */
132 snprintf( textbuf, textlen,
133 "%s: value #%d provided more than once",
134 ad->ad_cname.bv_val, i );
135 rc = LDAP_TYPE_OR_VALUE_EXISTS;
136 nmods[ i + 1 ].bv_val = NULL;
142 for ( j = 0; j < i; j++ ) {
143 #ifdef QUICK_DIRTY_DUPLICATE_CHECK
144 if ( bvmatch( &nmods[ i ], &nmods[ j ] ) ) {
145 #else /* !QUICK_DIRTY_DUPLICATE_CHECK */
148 rc = (mr->smr_match)( &match,
149 SLAP_MR_VALUE_SYNTAX_MATCH,
150 ad->ad_type->sat_syntax,
151 mr, &nmods[ i ], &nmods[ j ] );
152 if ( rc != LDAP_SUCCESS ) {
153 nmods[ i + 1 ].bv_val = NULL;
158 #endif /* !QUICK_DIRTY_DUPLICATE_CHECK */
159 snprintf( textbuf, textlen,
160 "%s: value #%d provided more than once",
161 ad->ad_cname.bv_val, j );
162 rc = LDAP_TYPE_OR_VALUE_EXISTS;
163 nmods[ i + 1 ].bv_val = NULL;
168 nmods[ i ].bv_val = NULL;
171 * if new values are more than existing values, it is more
172 * convenient to normalize and check all new values first,
173 * then check each new value against existing values, which
174 * can be normalized in place
177 if ( numvals >= nummods ) {
178 for ( j = 0; vals[ j ].bv_val; j++ ) {
179 struct berval asserted;
181 rc = value_normalize( ad, SLAP_MR_EQUALITY,
182 &vals[ j ], &asserted, text );
184 if ( rc != LDAP_SUCCESS ) {
188 for ( i = 0; nmods[ i ].bv_val; i++ ) {
189 #ifdef QUICK_DIRTY_DUPLICATE_CHECK
190 if ( bvmatch( &nmods[ i ], &asserted ) ) {
191 #else /* !QUICK_DIRTY_DUPLICATE_CHECK */
194 rc = (mr->smr_match)( &match,
195 SLAP_MR_VALUE_SYNTAX_MATCH,
196 ad->ad_type->sat_syntax,
197 mr, &nmods[ i ], &asserted );
198 if ( rc != LDAP_SUCCESS ) {
203 #endif /* !QUICK_DIRTY_DUPLICATE_CHECK */
204 snprintf( textbuf, textlen,
205 "%s: value #%d provided more than once",
206 ad->ad_cname.bv_val, j );
207 rc = LDAP_TYPE_OR_VALUE_EXISTS;
217 ber_bvarray_free( nvals );
220 ber_bvarray_free( nmods );
231 char *textbuf, size_t textlen
236 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
239 switch( mod->sm_op ) {
243 case LDAP_MOD_REPLACE:
251 a = attr_find( e->e_attrs, mod->sm_desc );
253 /* check if the values we're adding already exist */
254 if( mr == NULL || !mr->smr_match ) {
256 /* do not allow add of additional attribute
257 if no equality rule exists */
259 snprintf( textbuf, textlen,
260 "modify/%s: %s: no equality matching rule",
261 op, mod->sm_desc->ad_cname.bv_val );
262 return LDAP_INAPPROPRIATE_MATCHING;
265 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
266 /* test asserted values against existing values */
268 for( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
269 if ( bvmatch( &mod->sm_bvalues[i],
272 /* value exists already */
274 snprintf( textbuf, textlen,
275 "modify/%s: %s: value #%i already exists",
276 op, mod->sm_desc->ad_cname.bv_val, j );
277 return LDAP_TYPE_OR_VALUE_EXISTS;
282 /* test asserted values against themselves */
283 for( j = 0; j < i; j++ ) {
284 if ( bvmatch( &mod->sm_bvalues[i],
285 &mod->sm_bvalues[j] ) ) {
287 /* value exists already */
289 snprintf( textbuf, textlen,
290 "modify/%s: %s: value #%i already exists",
291 op, mod->sm_desc->ad_cname.bv_val, j );
292 return LDAP_TYPE_OR_VALUE_EXISTS;
300 * The original code performs ( n ) normalizations
301 * and ( n * ( n - 1 ) / 2 ) matches, which hide
302 * the same number of normalizations. The new code
303 * performs the same number of normalizations ( n )
304 * and ( n * ( n - 1 ) / 2 ) mem compares, far less
305 * expensive than an entire match, if a match is
306 * equivalent to a normalization and a mem compare ...
308 * This is far more memory expensive than the previous,
309 * but it can heavily improve performances when big
310 * chunks of data are added (typical example is a group
311 * with thousands of DN-syntax members; on my system:
312 * for members of 5-RDN DNs,
314 members orig bvmatch (dirty) new
315 1000 0m38.456s 0m0.553s 0m0.608s
316 2000 2m33.341s 0m0.851s 0m1.003s
318 * Moreover, 100 groups with 10000 members each were
319 * added in 37m27.933s (an analogous LDIF file was
320 * loaded into Active Directory in 38m28.682s, BTW).
322 * Maybe we could switch to the new algorithm when
323 * the number of values overcomes a given threshold?
327 const char *text = NULL;
328 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
330 if ( mod->sm_bvalues[ 1 ].bv_val == 0 ) {
332 struct berval asserted;
335 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
336 &mod->sm_bvalues[ 0 ], &asserted, &text );
338 if ( rc != LDAP_SUCCESS ) {
342 for ( i = 0; a->a_vals[ i ].bv_val; i++ ) {
345 rc = value_match( &match, mod->sm_desc, mr,
346 SLAP_MR_VALUE_SYNTAX_MATCH,
347 &a->a_vals[ i ], &asserted, &text );
349 if( rc == LDAP_SUCCESS && match == 0 ) {
350 free( asserted.bv_val );
351 return LDAP_TYPE_OR_VALUE_EXISTS;
357 rc = modify_check_duplicates( mod->sm_desc, mr,
358 a ? a->a_vals : NULL, mod->sm_bvalues,
359 &text, textbuf, sizeof( textbuf ) );
361 if ( rc != LDAP_SUCCESS ) {
368 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
369 /* this should return result of attr_merge */
371 snprintf( textbuf, textlen,
372 "modify/%s: %s: merge error",
373 op, mod->sm_desc->ad_cname.bv_val );
381 modify_delete_values(
385 char *textbuf, size_t textlen
390 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
392 /* delete the entire attribute */
393 if ( mod->sm_bvalues == NULL ) {
394 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
396 if( rc != LDAP_SUCCESS ) {
398 snprintf( textbuf, textlen,
399 "modify/delete: %s: no such attribute",
400 mod->sm_desc->ad_cname.bv_val );
401 rc = LDAP_NO_SUCH_ATTRIBUTE;
406 if( mr == NULL || !mr->smr_match ) {
407 /* disallow specific attributes from being deleted if
410 snprintf( textbuf, textlen,
411 "modify/delete: %s: no equality matching rule",
412 mod->sm_desc->ad_cname.bv_val );
413 return LDAP_INAPPROPRIATE_MATCHING;
416 /* delete specific values - find the attribute first */
417 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
419 snprintf( textbuf, textlen,
420 "modify/delete: %s: no such attribute",
421 mod->sm_desc->ad_cname.bv_val );
422 return LDAP_NO_SUCH_ATTRIBUTE;
425 /* find each value to delete
427 * FIXME: need to optimize this operation too,
428 * see modify_check_duplicates()
430 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
432 struct berval asserted;
434 rc = value_normalize( mod->sm_desc,
440 if( rc != LDAP_SUCCESS ) return rc;
443 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
445 int rc = value_match( &match, mod->sm_desc, mr,
446 SLAP_MR_VALUE_SYNTAX_MATCH,
447 &a->a_vals[j], &asserted, text );
449 if( rc == LDAP_SUCCESS && match != 0 ) {
453 /* found a matching value */
457 free( a->a_vals[j].bv_val );
458 for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
459 a->a_vals[k - 1] = a->a_vals[k];
461 a->a_vals[k - 1].bv_val = NULL;
462 a->a_vals[k - 1].bv_len = 0;
467 free( asserted.bv_val );
469 /* looked through them all w/o finding it */
472 snprintf( textbuf, textlen,
473 "modify/delete: %s: no such value",
474 mod->sm_desc->ad_cname.bv_val );
475 return LDAP_NO_SUCH_ATTRIBUTE;
479 /* if no values remain, delete the entire attribute */
480 if ( a->a_vals[0].bv_val == NULL ) {
481 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
483 snprintf( textbuf, textlen,
484 "modify/delete: %s: no such attribute",
485 mod->sm_desc->ad_cname.bv_val );
486 return LDAP_NO_SUCH_ATTRIBUTE;
494 modify_replace_values(
498 char *textbuf, size_t textlen
501 (void) attr_delete( &e->e_attrs, mod->sm_desc );
503 if ( mod->sm_bvalues ) {
504 return modify_add_values( e, mod, text, textbuf, textlen );
517 if ( mod->sm_type.bv_val)
518 free( mod->sm_type.bv_val );
520 if ( mod->sm_bvalues != NULL )
521 ber_bvarray_free( mod->sm_bvalues );
534 for ( ; ml != NULL; ml = next ) {
537 slap_mod_free( &ml->sml_mod, 0 );