]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache-merge.c
Proxy cache patch (by Jong, Apurva, & Kurt)
[openldap] / servers / slapd / back-meta / cache-merge.c
1 /* Copyright (c) 2003 by International Business Machines, Inc.
2  *
3  * International Business Machines, Inc. (hereinafter called IBM) grants
4  * permission under its copyrights to use, copy, modify, and distribute this
5  * Software with or without fee, provided that the above copyright notice and
6  * all paragraphs of this notice appear in all copies, and that the name of IBM
7  * not be used in connection with the marketing of any product incorporating
8  * the Software or modifications thereof, without specific, written prior
9  * permission.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
12  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
13  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
14  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
15  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
16  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26
27 #include "slap.h"
28 #include "ldif.h"
29 #include "../back-ldap/back-ldap.h"
30 #include "back-meta.h"
31 #include "ldap_pvt.h"
32 #undef ldap_debug       /* silence a warning in ldap-int.h */
33 #include "ldap_log.h"
34 #include "../../../libraries/libldap/ldap-int.h"
35 #include <sys/time.h>
36
37 #ifdef LDAP_CACHING
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         dnPrettyNormal(0, &rs->sr_entry->e_name, &e->e_name, &e->e_nname, op->o_tmpmemctx);
126
127         e->e_private = NULL;
128         e->e_attrs = NULL; 
129         e->e_bv.bv_val = NULL; 
130
131         /* add queryid attribute */     
132         value_array = (struct berval *)malloc(2 * sizeof( struct berval) );
133         ber_dupbv(value_array, query_uuid);
134         value_array[1].bv_val = NULL;
135         value_array[1].bv_len = 0;
136
137         uuid_attr = add_attribute(slap_schema.si_ad_queryid, e, value_array); 
138
139         /* append the attribute list from the fetched entry */
140         uuid_attr->a_next = rs->sr_entry->e_attrs;
141         rs->sr_entry->e_attrs = NULL;
142
143         for ( attr = e->e_attrs; attr; attr = attr->a_next ) {
144                 if ( normalize_values( attr ) ) {
145                         info.err = MERGE_ERR; 
146                         result->rc = info.err;
147                         return 0;
148                 }
149         }
150
151         info.entry = e;
152         info.uuid = query_uuid;
153         info.size_init = get_entry_size( rs->sr_entry, 0, 0 );
154         info.size_final = 0;
155         info.added = 0;
156         info.glue_be = op->o_bd;
157         info.err = SUCCESS;
158         cb.sc_private = &info;
159         cb.sc_response = null_response;
160
161         op_tmp.o_tag = LDAP_REQ_ADD;
162         op_tmp.o_protocol = LDAP_VERSION3;
163         op_tmp.o_callback = &cb;
164         op_tmp.o_caching_on = 0;
165         op_tmp.o_time = slap_get_time();
166         op_tmp.o_do_not_cache = 1;
167
168         op_tmp.ora_e = e;
169         op_tmp.o_req_dn = e->e_name;
170         op_tmp.o_req_ndn = e->e_nname;
171         rc = op->o_bd->be_add( &op_tmp, &sreply );
172
173         if ( rc != LDAP_SUCCESS ) {
174                 if ( rc == LDAP_ALREADY_EXISTS ) {
175                         slap_entry2mods( e, &modlist, &text );
176                         op_tmp.o_tag = LDAP_REQ_MODIFY;
177                         op_tmp.orm_modlist = modlist;
178                         op_tmp.o_req_dn = e->e_name;
179                         op_tmp.o_req_ndn = e->e_nname;
180                         rc = op->o_bd->be_modify( &op_tmp, &sreply );
181                         result->rc = info.added;
182                 } else if ( rc == LDAP_REFERRAL ||
183                                         rc == LDAP_NO_SUCH_OBJECT ) {
184                         slap_entry2mods( e, &modlist, &text );
185                         syncrepl_add_glue( NULL, NULL, &op_tmp, e, modlist, 0, NULL, NULL );
186                         result->rc = info.added;
187                 } else {
188                         result->rc = 0;
189                 }
190                 if ( modlist != NULL ) slap_mods_free( modlist );
191         } else {
192                 info.size_init = 0;
193                 result->rc = info.added;
194                 be_entry_release_w( &op_tmp, e );
195         }
196
197         if ( result->rc )
198                 info.size_final = get_entry_size( e, info.size_init, result );
199         else
200                 info.size_final = info.size_init;
201
202         return ( info.size_final - info.size_init );
203 }
204
205 static Attribute* 
206 add_attribute(AttributeDescription *ad,
207         Entry* e, 
208         BerVarray value_array) 
209 {
210         Attribute* new_attr, *last_attr; 
211         const char* text; 
212
213         if (e->e_attrs == NULL) 
214                 last_attr = NULL; 
215         else 
216                 for (last_attr = e->e_attrs; last_attr->a_next;
217                                 last_attr = last_attr->a_next)
218                         ; 
219
220         new_attr = (Attribute*)malloc(sizeof(Attribute));               
221         if (last_attr) 
222                 last_attr->a_next = new_attr;
223         else 
224                 e->e_attrs = new_attr; 
225
226         new_attr->a_next = NULL; 
227         new_attr->a_desc = NULL;
228         new_attr->a_vals = value_array; 
229         new_attr->a_desc = ad;
230
231         return new_attr; 
232 }
233
234 static int
235 null_response (
236         Operation       *op,
237         SlapReply       *rs )
238 {
239         return 0;
240 }
241
242 static int 
243 normalize_values( Attribute* attr ) 
244 {
245         int nvals, rc, i; 
246  
247         if (attr->a_vals == NULL) {
248                 attr->a_nvals = NULL; 
249                 return 0; 
250         } 
251
252         for ( nvals = 0; attr->a_vals[nvals].bv_val; nvals++ ) 
253                 ; 
254
255         attr->a_nvals = (struct berval*)ch_malloc((nvals+1)*sizeof(struct berval));
256
257         if ( attr->a_desc->ad_type->sat_equality &&
258                                 attr->a_desc->ad_type->sat_equality->smr_normalize )
259         {
260                 for ( i = 0; i < nvals; i++ ) {
261                         rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
262                                 0,
263                                 attr->a_desc->ad_type->sat_syntax,
264                                 attr->a_desc->ad_type->sat_equality,
265                                 &attr->a_vals[i], &attr->a_nvals[i], NULL );
266                         if ( rc ) {
267 #ifdef NEW_LOGGING
268                                 LDAP_LOG( OPERATION, DETAIL1,
269                                         "Error in normalizing attribute %s value %d (%d)\n",
270                                         attr->a_desc->ad_cname.bv_val, i, rc );
271 #else
272                                 Debug( LDAP_DEBUG_ANY,
273                                         "Error in normalizing attribute %s value %d (%d)\n",
274                                         attr->a_desc->ad_cname.bv_val, i, rc );
275 #endif
276                                 return rc;
277                         }
278                 }
279         } else {
280                 for ( i = 0; i < nvals; i++ ) {
281                         ber_dupbv( &attr->a_nvals[i], &attr->a_vals[i] ); 
282                 }
283         }
284                         
285         attr->a_nvals[i].bv_val = NULL;
286         attr->a_nvals[i].bv_len = 0;
287
288         return LDAP_SUCCESS;
289 }
290
291 #endif /* LDAP_CACHING */