]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache-merge.c
notices and acknowledges
[openldap] / servers / slapd / back-meta / cache-merge.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2003 The OpenLDAP Foundation.
5  * Portions Copyright 2003 IBM Corporation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by the Apurva Kumar for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/socket.h>
26 #include <ac/string.h>
27 #include <ac/time.h>
28
29 #include "slap.h"
30 #include "ldif.h"
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
33 #include "ldap_pvt.h"
34 #undef ldap_debug       /* silence a warning in ldap-int.h */
35 #include "ldap_log.h"
36 #include "../../../libraries/libldap/ldap-int.h"
37 #include <sys/time.h>
38
39 static struct berval bv_queryid_any = BER_BVC( "(queryid=*)" );
40
41 static Attribute* 
42 add_attribute(AttributeDescription *ad,
43         Entry* e,
44         BerVarray value_array
45 ); 
46
47 static int
48 null_response (
49         Operation       *op,
50         SlapReply       *rs
51 ); 
52
53 static int 
54 normalize_values( Attribute* attr );    
55
56 struct entry_info {
57         int                     size_init; 
58         int                     size_final; 
59         int                     added; 
60         Entry*                  entry; 
61         struct berval*          uuid; 
62         struct timeval          tv;     /* time */ 
63         enum type_of_result     err; 
64         Backend*                glue_be; 
65 }; 
66
67
68 int 
69 get_entry_size(
70         Entry* e, 
71         int size_init, 
72         struct exception* result )
73 {
74         Attribute       *a;
75         struct berval   bv;
76         int             i; 
77         int             tmplen;
78         int             size=0;
79
80         if ( result )
81                 result->type = SUCCESS; 
82
83         if ( e->e_dn != NULL ) {
84                 tmplen = strlen( e->e_dn );
85                 size = LDIF_SIZE_NEEDED( 2, tmplen );
86         }
87
88         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
89                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
90                         bv = a->a_vals[i];
91                         tmplen = a->a_desc->ad_cname.bv_len;
92                         size += LDIF_SIZE_NEEDED( tmplen, bv.bv_len);
93                 }
94         }
95         if ((size < size_init) && result) {
96                 result->type = SIZE_ERR; 
97         }
98         return size;
99 }
100
101 int
102 merge_entry(
103         Operation               *op,
104         SlapReply               *rs,
105         struct berval*          query_uuid, 
106         struct exception*       result )
107 {
108         struct entry_info info;
109         int             rc;
110         Modifications* modlist = NULL;
111         const char*     text = NULL;
112         BerVarray               value_array; 
113         Attribute               *uuid_attr, *attr;
114         Entry                   *e;
115
116         SlapReply sreply = {REP_RESULT};
117
118         Operation op_tmp = *op;
119         slap_callback cb;
120
121         sreply.sr_entry = NULL; 
122         sreply.sr_nentries = 0; 
123
124         e = ( Entry * ) ch_calloc( 1, sizeof( Entry )); 
125
126         dnPrettyNormal(0, &rs->sr_entry->e_name, &op_tmp.o_req_dn, &op_tmp.o_req_ndn, op->o_tmpmemctx);
127         ber_dupbv( &e->e_name, &op_tmp.o_req_dn );
128         ber_dupbv( &e->e_nname, &op_tmp.o_req_ndn );
129         sl_free( op_tmp.o_req_ndn.bv_val, op->o_tmpmemctx );
130         sl_free( op_tmp.o_req_dn.bv_val, op->o_tmpmemctx );
131         op_tmp.o_req_dn = e->e_name;
132         op_tmp.o_req_ndn = e->e_nname;
133
134         e->e_private = NULL;
135         e->e_attrs = NULL; 
136         e->e_bv.bv_val = NULL; 
137
138         /* add queryid attribute */     
139         value_array = (struct berval *)malloc(2 * sizeof( struct berval) );
140         ber_dupbv(value_array, query_uuid);
141         value_array[1].bv_val = NULL;
142         value_array[1].bv_len = 0;
143
144         uuid_attr = add_attribute(slap_schema.si_ad_queryid, e, value_array); 
145
146         /* append the attribute list from the fetched entry */
147         uuid_attr->a_next = rs->sr_entry->e_attrs;
148         rs->sr_entry->e_attrs = NULL;
149
150         for ( attr = e->e_attrs; attr; attr = attr->a_next ) {
151                 if ( normalize_values( attr ) ) {
152                         info.err = MERGE_ERR; 
153                         result->rc = info.err;
154                         return 0;
155                 }
156         }
157
158         info.entry = e;
159         info.uuid = query_uuid;
160         info.size_init = get_entry_size( rs->sr_entry, 0, 0 );
161         info.size_final = 0;
162         info.added = 0;
163         info.glue_be = op->o_bd;
164         info.err = SUCCESS;
165         cb.sc_private = &info;
166         cb.sc_response = null_response;
167
168         op_tmp.o_tag = LDAP_REQ_ADD;
169         op_tmp.o_protocol = LDAP_VERSION3;
170         op_tmp.o_callback = &cb;
171         op_tmp.o_time = slap_get_time();
172         op_tmp.o_do_not_cache = 1;
173
174         op_tmp.ora_e = e;
175         rc = op->o_bd->be_add( &op_tmp, &sreply );
176
177         if ( rc != LDAP_SUCCESS ) {
178                 if ( rc == LDAP_ALREADY_EXISTS ) {
179                         slap_entry2mods( e, &modlist, &text );
180                         op_tmp.o_tag = LDAP_REQ_MODIFY;
181                         op_tmp.orm_modlist = modlist;
182                         op_tmp.o_req_dn = e->e_name;
183                         op_tmp.o_req_ndn = e->e_nname;
184                         rc = op->o_bd->be_modify( &op_tmp, &sreply );
185                         result->rc = info.added;
186                 } else if ( rc == LDAP_REFERRAL ||
187                                         rc == LDAP_NO_SUCH_OBJECT ) {
188                         syncrepl_add_glue( &op_tmp, e );
189                         result->rc = info.added;
190                 } else {
191                         result->rc = 0;
192                 }
193                 if ( modlist != NULL ) slap_mods_free( modlist );
194         } else {
195                 info.size_init = 0;
196                 result->rc = info.added;
197                 be_entry_release_w( &op_tmp, e );
198         }
199
200         if ( result->rc )
201                 info.size_final = get_entry_size( e, info.size_init, result );
202         else
203                 info.size_final = info.size_init;
204
205         return ( info.size_final - info.size_init );
206 }
207
208 static Attribute* 
209 add_attribute(AttributeDescription *ad,
210         Entry* e, 
211         BerVarray value_array) 
212 {
213         Attribute* new_attr, *last_attr; 
214         const char* text; 
215
216         if (e->e_attrs == NULL) 
217                 last_attr = NULL; 
218         else 
219                 for (last_attr = e->e_attrs; last_attr->a_next;
220                                 last_attr = last_attr->a_next)
221                         ; 
222
223         new_attr = (Attribute*)malloc(sizeof(Attribute));               
224         if (last_attr) 
225                 last_attr->a_next = new_attr;
226         else 
227                 e->e_attrs = new_attr; 
228
229         new_attr->a_next = NULL; 
230         new_attr->a_desc = NULL;
231         new_attr->a_vals = value_array; 
232         new_attr->a_desc = ad;
233
234         return new_attr; 
235 }
236
237 static int
238 null_response (
239         Operation       *op,
240         SlapReply       *rs )
241 {
242         return 0;
243 }
244
245 static int 
246 normalize_values( Attribute* attr ) 
247 {
248         int nvals, rc, i; 
249  
250         if (attr->a_vals == NULL) {
251                 attr->a_nvals = NULL; 
252                 return 0; 
253         } 
254
255         for ( nvals = 0; attr->a_vals[nvals].bv_val; nvals++ ) 
256                 ; 
257
258         attr->a_nvals = (struct berval*)ch_malloc((nvals+1)*sizeof(struct berval));
259
260         if ( attr->a_desc->ad_type->sat_equality &&
261                                 attr->a_desc->ad_type->sat_equality->smr_normalize )
262         {
263                 for ( i = 0; i < nvals; i++ ) {
264                         rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
265                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
266                                 attr->a_desc->ad_type->sat_syntax,
267                                 attr->a_desc->ad_type->sat_equality,
268                                 &attr->a_vals[i], &attr->a_nvals[i], NULL );
269                         if ( rc ) {
270 #ifdef NEW_LOGGING
271                                 LDAP_LOG( OPERATION, DETAIL1,
272                                         "Error in normalizing attribute %s value %d (%d)\n",
273                                         attr->a_desc->ad_cname.bv_val, i, rc );
274 #else
275                                 Debug( LDAP_DEBUG_ANY,
276                                         "Error in normalizing attribute %s value %d (%d)\n",
277                                         attr->a_desc->ad_cname.bv_val, i, rc );
278 #endif
279                                 return rc;
280                         }
281                 }
282         } else {
283                 for ( i = 0; i < nvals; i++ ) {
284                         ber_dupbv( &attr->a_nvals[i], &attr->a_vals[i] ); 
285                 }
286         }
287                         
288         attr->a_nvals[i].bv_val = NULL;
289         attr->a_nvals[i].bv_len = 0;
290
291         return LDAP_SUCCESS;
292 }