]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/pcache.c
b631a58efded3f18488c4a081e533cb3fe046eda
[openldap] / servers / slapd / overlays / pcache.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2003-2004 The OpenLDAP Foundation.
5  * Portions Copyright 2003 IBM Corporation.
6  * Portions Copyright 2003 Symas Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Apurva Kumar for inclusion
19  * in OpenLDAP Software and subsequently rewritten by Howard Chu.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_PROXYCACHE
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "slap.h"
32 #include "ldap_pvt.h"
33 #include "lutil.h"
34 #include "ldap_rq.h"
35
36 /* query cache structs */
37 /* query */
38
39 typedef struct Query_s {
40         Filter*         filter;         /* Search Filter */
41         AttributeName*  attrs;          /* Projected attributes */
42         AttributeName*  save_attrs;     /* original attributes, saved for response */
43         struct berval   base;           /* Search Base */
44         int             scope;          /* Search scope */
45 } Query;
46
47 /* struct representing a cached query */
48 typedef struct cached_query_s {
49         Query                           query;          /* LDAP query */
50         struct berval                   q_uuid;         /* query identifier */
51         int                             template_id;    /* template of the query */
52         time_t                          expiry_time;    /* time till the query is considered valid */
53         struct cached_query_s           *next;          /* next query in the template */
54         struct cached_query_s           *prev;          /* previous query in the template */
55         struct cached_query_s           *lru_up;        /* previous query in the LRU list */
56         struct cached_query_s           *lru_down;      /* next query in the LRU list */
57 } CachedQuery;
58
59 /* struct representing a query template
60  * e.g. template string = &(cn=)(mail=)
61  */
62 typedef struct query_template_s {
63         struct berval   querystr;       /* Filter string corresponding to the QT */
64         int             attr_set_index; /* determines the projected attributes */
65
66         CachedQuery*    query;          /* most recent query cached for the template */
67         CachedQuery*    query_last;     /* oldest query cached for the template */
68
69         int             no_of_queries;  /* Total number of queries in the template */
70         long            ttl;            /* TTL for the queries of this template */
71         ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
72 } QueryTemplate;
73
74 /*
75  * Represents a set of projected attributes and any
76  * supersets among all specified sets of attributes.
77  */
78
79 struct attr_set {
80         AttributeName*  attrs;          /* specifies the set */
81         int             count;          /* number of attributes */
82         int*            ID_array;       /* array of indices of supersets of 'attrs' */
83 };
84
85 struct query_manager_s;
86
87 /* prototypes for functions for 1) query containment
88  * 2) query addition, 3) cache replacement
89  */
90 typedef int     (QCfunc)(struct query_manager_s*, Query*, int );
91 typedef void    (AddQueryfunc)(struct query_manager_s*, Query*, int, struct berval*);
92 typedef void    (CRfunc)(struct query_manager_s*, struct berval * );
93
94 /* LDAP query cache */
95 typedef struct query_manager_s {
96         struct attr_set*        attr_sets;              /* possible sets of projected attributes */
97         QueryTemplate*          templates;              /* cacheable templates */
98
99         CachedQuery*            lru_top;                /* top and bottom of LRU list */
100         CachedQuery*            lru_bottom;
101
102         ldap_pvt_thread_mutex_t         lru_mutex;      /* mutex for accessing LRU list */
103
104         /* Query cache methods */
105         QCfunc                  *qcfunc;                        /* Query containment*/
106         CRfunc                  *crfunc;                        /* cache replacement */
107         AddQueryfunc    *addfunc;                       /* add query */
108 } query_manager;
109
110 /* LDAP query cache manager */
111 typedef struct cache_manager_s {
112         BackendDB       db;     /* underlying database */
113         unsigned long   num_cached_queries;             /* total number of cached queries */
114         unsigned long   max_queries;                    /* upper bound on # of cached queries */
115         int     numattrsets;                    /* number of attribute sets */
116         int     numtemplates;                   /* number of cacheable templates */
117         int     cur_entries;                    /* current number of entries cached */
118         int     max_entries;                    /* max number of entries cached */
119         int     num_entries_limit;              /* max # of entries in a cacheable query */
120
121         char    response_cb;                    /* install the response callback
122                                                  * at the tail of the callback list */
123 #define PCACHE_RESPONSE_CB_HEAD 0
124 #define PCACHE_RESPONSE_CB_TAIL 1
125
126         int     cc_period;              /* interval between successive consistency checks (sec) */
127         int     cc_paused;
128         void    *cc_arg;
129
130         ldap_pvt_thread_mutex_t         cache_mutex;
131         ldap_pvt_thread_mutex_t         remove_mutex;
132
133         query_manager*   qm;    /* query cache managed by the cache manager */
134 } cache_manager;
135
136 static AttributeDescription *ad_queryid;
137 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
138                         "DESC 'list of queries the entry belongs to' "
139                         "EQUALITY octetStringMatch "
140                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
141                         "NO-USER-MODIFICATION USAGE directoryOperation )";
142
143 /* Return 1 for an added entry, else 0 */
144 static int
145 merge_entry(
146         Operation               *op,
147         Entry                   *e,
148         struct berval*          query_uuid )
149 {
150         int             rc;
151         Modifications* modlist = NULL;
152         const char*     text = NULL;
153         Attribute               *attr;
154         char                    textbuf[SLAP_TEXT_BUFLEN];
155         size_t                  textlen = sizeof(textbuf);
156
157         SlapReply sreply = {REP_RESULT};
158
159         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
160
161         attr = e->e_attrs;
162         e->e_attrs = NULL;
163
164         /* add queryid attribute */
165         attr_merge_one( e, ad_queryid, query_uuid, NULL );
166
167         /* append the attribute list from the fetched entry */
168         e->e_attrs->a_next = attr;
169
170         op->o_tag = LDAP_REQ_ADD;
171         op->o_protocol = LDAP_VERSION3;
172         op->o_callback = &cb;
173         op->o_time = slap_get_time();
174         op->o_do_not_cache = 1;
175
176         op->ora_e = e;
177         op->o_req_dn = e->e_name;
178         op->o_req_ndn = e->e_nname;
179         rc = op->o_bd->be_add( op, &sreply );
180
181         if ( rc != LDAP_SUCCESS ) {
182                 if ( rc == LDAP_ALREADY_EXISTS ) {
183                         slap_entry2mods( e, &modlist, &text, textbuf, textlen );
184                         modlist->sml_op = LDAP_MOD_ADD;
185                         op->o_tag = LDAP_REQ_MODIFY;
186                         op->orm_modlist = modlist;
187                         op->o_bd->be_modify( op, &sreply );
188                         slap_mods_free( modlist );
189                 } else if ( rc == LDAP_REFERRAL ||
190                                         rc == LDAP_NO_SUCH_OBJECT ) {
191                         syncrepl_add_glue( op, e );
192                         e = NULL;
193                         rc = 1;
194                 }
195                 if ( e ) {
196                         entry_free( e );
197                         rc = 0;
198                 }
199         } else {
200                 be_entry_release_w( op, e );
201                 rc = 1;
202         }
203
204         return rc;
205 }
206
207 /* compare base and scope of incoming and cached queries */
208 static int base_scope_compare(
209         struct berval* ndn_stored,
210         struct berval* ndn_incoming,
211         int scope_stored,
212         int scope_incoming      )
213 {
214         struct berval pdn_incoming = BER_BVNULL;
215
216         if (scope_stored < scope_incoming)
217                 return 0;
218
219         if ( !dnIsSuffix(ndn_incoming, ndn_stored))
220                 return 0;
221
222         switch(scope_stored) {
223         case LDAP_SCOPE_BASE:
224                 return (ndn_incoming->bv_len == ndn_stored->bv_len);
225
226         case LDAP_SCOPE_ONELEVEL:
227                 switch(scope_incoming){
228                 case LDAP_SCOPE_BASE:
229                         dnParent(ndn_incoming, &pdn_incoming);
230                         return (pdn_incoming.bv_len == ndn_stored->bv_len);
231
232                 case LDAP_SCOPE_ONELEVEL:
233                         return (ndn_incoming->bv_len == ndn_stored->bv_len);
234
235                 default:
236                         return 0;
237                 }
238         case LDAP_SCOPE_SUBTREE:
239                 return 1;
240                 break;
241         default:
242                 return 0;
243                 break;
244     }
245 }
246
247 /* add query on top of LRU list */
248 static void
249 add_query_on_top (query_manager* qm, CachedQuery* qc)
250 {
251         CachedQuery* top = qm->lru_top;
252         Query* q = (Query*)qc;
253
254         qm->lru_top = qc;
255
256         if (top)
257                 top->lru_up = qc;
258         else
259                 qm->lru_bottom = qc;
260
261         qc->lru_down = top;
262         qc->lru_up = NULL;
263         Debug( LDAP_DEBUG_ANY, "Base of added query = %s\n",
264                         q->base.bv_val, 0, 0 );
265 }
266
267 /* remove_query from LRU list */
268
269 static void
270 remove_query (query_manager* qm, CachedQuery* qc)
271 {
272         CachedQuery* up;
273         CachedQuery* down;
274
275         if (!qc)
276                 return;
277
278         up = qc->lru_up;
279         down = qc->lru_down;
280
281         if (!up)
282                 qm->lru_top = down;
283
284         if (!down)
285                 qm->lru_bottom = up;
286
287         if (down)
288                 down->lru_up = up;
289
290         if (up)
291                 up->lru_down = down;
292
293         qc->lru_up = qc->lru_down = NULL;
294 }
295
296 static void
297 invert_string( struct berval *bv )
298 {
299         int i;
300         char c;
301
302         for (i=0; i<bv->bv_len/2; i++) {
303                 c = bv->bv_val[i];
304                 bv->bv_val[i] = bv->bv_val[bv->bv_len-i-1];
305                 bv->bv_val[bv->bv_len-i-1] = c;
306         }
307 }
308
309 /* find and remove string2 from string1
310  * from start if position = 1,
311  * from end if position = 3,
312  * from anywhere if position = 2
313  */
314
315 static int
316 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
317 {
318         char* temp;
319         int len;
320         int ret=0;
321
322         char* arg1, *arg2;
323         char* string1=ber1->bv_val;
324         char* string2=ber2->bv_val;
325
326         if (string2 == NULL)
327                 return 1;
328         if (string1 == NULL)
329                 return 0;
330
331         if (position == 3) {
332                 invert_string(ber1);
333                 invert_string(ber2);
334         }
335
336         arg1 = string1;
337         arg2 = string2;
338
339         temp = strstr(arg1, arg2);
340
341         len = ber2->bv_len;
342
343         if ( temp && (position == 2 || temp == arg1) ) {
344                 string1 = temp+len;
345                 strcpy( arg1, string1 );
346                 ber1->bv_len -= len;
347                 ret = 1;
348         }
349         if ( position == 3 ) {
350                 invert_string(ber1);
351                 invert_string(ber2);
352         }
353         return ret;
354 }
355
356
357 static struct berval*
358 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
359 {
360         struct berval* merged, *temp;
361         int i, any_count, count;
362
363         for (any_count=0; any && any[any_count].bv_val; any_count++)
364                 ;
365
366         count = any_count;
367
368         if (init->bv_val)
369                 count++;
370         if (final->bv_val)
371                 count++;
372
373         merged = (struct berval*)(ch_malloc((count+1)*sizeof(struct berval)));
374         temp = merged;
375
376         if (init->bv_val) {
377                 *temp++ = *init;
378         }
379
380         for (i=0; i<any_count; i++) {
381                 *temp++ = *any++;
382         }
383
384         if (final->bv_val){
385                 *temp++ = *final;
386         }
387         temp->bv_val = NULL;
388         temp->bv_len = 0;
389         return merged;
390 }
391
392 static int
393 strings_containment(struct berval* stored, struct berval* incoming)
394 {
395         struct berval* element;
396         int k=0;
397         int j, rc = 0;
398
399         for ( element=stored; element->bv_val != NULL; element++ ) {
400                 for (j = k; incoming[j].bv_val != NULL; j++) {
401                         if (find_and_remove(&(incoming[j]), element, 2)) {
402                                 k = j;
403                                 rc = 1;
404                                 break;
405                         }
406                         rc = 0;
407                 }
408                 if ( rc ) {
409                         continue;
410                 } else {
411                         return 0;
412                 }
413         }
414         return 1;
415 }
416
417 static int
418 substr_containment_substr(Filter* stored, Filter* incoming)
419 {
420         int i;
421         int rc = 0;
422         int any_count = 0;
423
424         struct berval init_incoming;
425         struct berval final_incoming;
426         struct berval *any_incoming = NULL;
427         struct berval *remaining_incoming = NULL;
428
429         if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
430            || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
431                 return 0;
432
433
434         ber_dupbv(&init_incoming, &(incoming->f_sub_initial));
435         ber_dupbv(&final_incoming, &(incoming->f_sub_final));
436
437         if (incoming->f_sub_any) {
438                 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
439                                 any_count++ )
440                         ;
441
442                 any_incoming = (struct berval*)ch_malloc((any_count+1) *
443                                                 sizeof(struct berval));
444
445                 for (i=0; i<any_count; i++) {
446                         ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
447                 }
448                 any_incoming[any_count].bv_val = NULL;
449                 any_incoming[any_count].bv_len = 0;
450         }
451
452         if (find_and_remove(&init_incoming,
453                         &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
454                         &(stored->f_sub_final), 3))
455         {
456                 if (stored->f_sub_any == NULL) {
457                         rc = 1;
458                         goto final;
459                 }
460                 remaining_incoming = merge_init_final(&init_incoming,
461                                                 any_incoming, &final_incoming);
462                 rc = strings_containment(stored->f_sub_any, remaining_incoming);
463         }
464 final:
465         free(init_incoming.bv_val);
466         free(final_incoming.bv_val);
467         if (any_incoming) ber_bvarray_free( any_incoming );
468         free(remaining_incoming);
469
470         return rc;
471 }
472
473 static int
474 substr_containment_equality(Filter* stored, Filter* incoming)
475 {
476         struct berval incoming_val[2];
477         int rc = 0;
478
479         ber_dupbv(incoming_val, &(incoming->f_av_value));
480         incoming_val[1].bv_val = NULL;
481         incoming_val[1].bv_len = 0;
482
483         if (find_and_remove(incoming_val,
484                         &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
485                         &(stored->f_sub_final), 3)) {
486                 if (stored->f_sub_any == NULL){
487                         rc = 1;
488                         goto final;
489                 }
490                 rc = strings_containment(stored->f_sub_any, incoming_val);
491         }
492 final:
493         free(incoming_val[0].bv_val);
494         return rc;
495 }
496
497 /* check whether query is contained in any of
498  * the cached queries in template template_index
499  */
500 static int
501 query_containment(query_manager *qm,
502                   Query *query,
503                   int template_index)
504 {
505         QueryTemplate* templa= qm->templates;
506         CachedQuery* qc;
507         Query* q;
508         Filter* inputf = query->filter;
509         struct berval* base = &(query->base);
510         int scope = query->scope;
511         int res=0;
512         Filter* fs;
513         Filter* fi;
514         int ret, rc;
515         const char* text;
516
517         MatchingRule* mrule = NULL;
518         if (inputf != NULL) {
519                 Debug( LDAP_DEBUG_ANY, "Lock QC index = %d\n",
520                                 template_index, 0, 0 );
521                 ldap_pvt_thread_rdwr_rlock(&(templa[template_index].t_rwlock));
522                 for(qc=templa[template_index].query; qc != NULL; qc= qc->next) {
523                         q = (Query*)qc;
524                         if(base_scope_compare(&(q->base), base, q->scope, scope)) {
525                                 fi = inputf;
526                                 fs = q->filter;
527                                 do {
528                                         res=0;
529                                         switch (fs->f_choice) {
530                                         case LDAP_FILTER_EQUALITY:
531                                                 if (fi->f_choice == LDAP_FILTER_EQUALITY)
532                                                         mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
533                                                 else
534                                                         ret = 1;
535                                                 break;
536                                         case LDAP_FILTER_GE:
537                                         case LDAP_FILTER_LE:
538                                                 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
539                                                 break;
540                                         default:
541                                                 mrule = NULL; 
542                                         }
543                                         if (mrule) {
544                                                 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
545                                                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
546                                                         &(fi->f_ava->aa_value),
547                                                         &(fs->f_ava->aa_value), &text);
548                                                 if (rc != LDAP_SUCCESS) {
549                                                         ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
550                                                         Debug( LDAP_DEBUG_ANY,
551                                                         "Unlock: Exiting QC index=%d\n",
552                                                         template_index, 0, 0 );
553                                                         return 0;
554                                                 }
555                                         }
556                                         switch (fs->f_choice) {
557                                         case LDAP_FILTER_OR:
558                                         case LDAP_FILTER_AND:
559                                                 fs = fs->f_and;
560                                                 fi = fi->f_and;
561                                                 res=1;
562                                                 break;
563                                         case LDAP_FILTER_SUBSTRINGS:
564                                                 /* check if the equality query can be
565                                                 * answered with cached substring query */
566                                                 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
567                                                         && substr_containment_equality(
568                                                         fs, fi))
569                                                         res=1;
570                                                 /* check if the substring query can be
571                                                 * answered with cached substring query */
572                                                 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
573                                                         ) && substr_containment_substr(
574                                                         fs, fi))
575                                                         res= 1;
576                                                 fs=fs->f_next;
577                                                 fi=fi->f_next;
578                                                 break;
579                                         case LDAP_FILTER_PRESENT:
580                                                 res=1;
581                                                 fs=fs->f_next;
582                                                 fi=fi->f_next;
583                                                 break;
584                                         case LDAP_FILTER_EQUALITY:
585                                                 if (ret == 0)
586                                                         res = 1;
587                                                 fs=fs->f_next;
588                                                 fi=fi->f_next;
589                                                 break;
590                                         case LDAP_FILTER_GE:
591                                                 if (ret >= 0)
592                                                         res = 1;
593                                                 fs=fs->f_next;
594                                                 fi=fi->f_next;
595                                                 break;
596                                         case LDAP_FILTER_LE:
597                                                 if (ret <= 0)
598                                                         res = 1;
599                                                 fs=fs->f_next;
600                                                 fi=fi->f_next;
601                                                 break;
602                                         case LDAP_FILTER_NOT:
603                                                 res=0;
604                                                 break;
605                                         default:
606                                                 break;
607                                         }
608                                 } while((res) && (fi != NULL) && (fs != NULL));
609
610                                 if(res) {
611                                         ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
612                                         if (qm->lru_top != qc) {
613                                                 remove_query(qm, qc);
614                                                 add_query_on_top(qm, qc);
615                                         }
616                                         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
617                                         return 1;
618                                 }
619                         }
620                 }
621                 Debug( LDAP_DEBUG_ANY,
622                         "Not answerable: Unlock QC index=%d\n",
623                         template_index, 0, 0 );
624                 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
625         }
626         return 0;
627 }
628
629 static void
630 free_query (CachedQuery* qc)
631 {
632         Query* q = (Query*)qc;
633         int i;
634
635         free(qc->q_uuid.bv_val);
636         filter_free(q->filter);
637         free (q->base.bv_val);
638         for (i=0; q->attrs[i].an_name.bv_val; i++) {
639                 free(q->attrs[i].an_name.bv_val);
640         }
641         free(q->attrs);
642         free(qc);
643 }
644
645
646 /* Add query to query cache */
647 static void add_query(
648         query_manager* qm,
649         Query* query,
650         int template_index,
651         struct berval* uuid)
652 {
653         CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
654         QueryTemplate* templ = (qm->templates)+template_index;
655         Query* new_query;
656         new_cached_query->template_id = template_index;
657         new_cached_query->q_uuid = *uuid;
658         new_cached_query->lru_up = NULL;
659         new_cached_query->lru_down = NULL;
660         new_cached_query->expiry_time = slap_get_time() + templ->ttl;
661         Debug( LDAP_DEBUG_ANY, "Added query expires at %ld\n",
662                         (long) new_cached_query->expiry_time, 0, 0 );
663         new_query = (Query*)new_cached_query;
664
665         ber_dupbv(&new_query->base, &query->base);
666         new_query->scope = query->scope;
667         new_query->filter = query->filter;
668         new_query->attrs = query->attrs;
669
670         /* Adding a query    */
671         Debug( LDAP_DEBUG_ANY, "Lock AQ index = %d\n",
672                         template_index, 0, 0 );
673         ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
674         if (templ->query == NULL)
675                 templ->query_last = new_cached_query;
676         else
677                 templ->query->prev = new_cached_query;
678         new_cached_query->next = templ->query;
679         new_cached_query->prev = NULL;
680         templ->query = new_cached_query;
681         templ->no_of_queries++;
682         Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES++ %d\n",
683                         template_index, templ->no_of_queries, 0 );
684
685         Debug( LDAP_DEBUG_ANY, "Unlock AQ index = %d \n",
686                         template_index, 0, 0 );
687         ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
688
689         /* Adding on top of LRU list  */
690         ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
691         add_query_on_top(qm, new_cached_query);
692         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
693 }
694
695 static void
696 remove_from_template (CachedQuery* qc, QueryTemplate* template)
697 {
698         if (!qc->prev && !qc->next) {
699                 template->query_last = template->query = NULL;
700         } else if (qc->prev == NULL) {
701                 qc->next->prev = NULL;
702                 template->query = qc->next;
703         } else if (qc->next == NULL) {
704                 qc->prev->next = NULL;
705                 template->query_last = qc->prev;
706         } else {
707                 qc->next->prev = qc->prev;
708                 qc->prev->next = qc->next;
709         }
710
711         template->no_of_queries--;
712 }
713
714 /* remove bottom query of LRU list from the query cache */
715 static void cache_replacement(query_manager* qm, struct berval *result)
716 {
717         CachedQuery* bottom;
718         int temp_id;
719
720         ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
721         bottom = qm->lru_bottom;
722
723         result->bv_val = NULL;
724         result->bv_len = 0;
725
726         if (!bottom) {
727                 Debug ( LDAP_DEBUG_ANY,
728                         "Cache replacement invoked without "
729                         "any query in LRU list\n", 0, 0, 0 );
730                 return;
731         }
732
733         temp_id = bottom->template_id;
734         remove_query(qm, bottom);
735         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
736
737         *result = bottom->q_uuid;
738         bottom->q_uuid.bv_val = NULL;
739
740         Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n", temp_id, 0, 0 );
741         ldap_pvt_thread_rdwr_wlock(&(qm->templates[temp_id].t_rwlock));
742         remove_from_template(bottom, (qm->templates+temp_id));
743         Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
744                 temp_id, qm->templates[temp_id].no_of_queries, 0 );
745         Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n", temp_id, 0, 0 );
746         ldap_pvt_thread_rdwr_wunlock(&(qm->templates[temp_id].t_rwlock));
747         free_query(bottom);
748 }
749
750 struct query_info {
751         struct query_info *next;
752         struct berval xdn;
753         int del;
754 };
755
756 static int
757 remove_func (
758         Operation       *op,
759         SlapReply       *rs
760 )
761 {
762         Attribute *attr;
763         struct query_info *qi;
764         int count = 0;
765
766         if ( rs->sr_type != REP_SEARCH ) return 0;
767
768         for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
769                 if (attr->a_desc == ad_queryid) {
770                         for (count=0; attr->a_vals[count].bv_val; count++)
771                                 ;
772                         break;
773                 }
774         }
775         if ( count == 0 ) return 0;
776         qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
777         qi->next = op->o_callback->sc_private;
778         op->o_callback->sc_private = qi;
779         ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
780         qi->del = ( count == 1 );
781
782         return 0;
783 }
784
785 static int
786 remove_query_data (
787         Operation       *op,
788         SlapReply       *rs,
789         struct berval* query_uuid)
790 {
791         struct query_info       *qi, *qnext;
792         char                    filter_str[64];
793         AttributeAssertion      ava;
794         Filter                  filter = {LDAP_FILTER_EQUALITY};
795         SlapReply               sreply = {REP_RESULT};
796         slap_callback cb = { NULL, remove_func, NULL, NULL };
797         int deleted = 0;
798
799         sreply.sr_entry = NULL;
800         sreply.sr_nentries = 0;
801         op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
802                 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
803         filter.f_ava = &ava;
804         filter.f_av_desc = ad_queryid;
805         filter.f_av_value = *query_uuid;
806
807         op->o_tag = LDAP_REQ_SEARCH;
808         op->o_protocol = LDAP_VERSION3;
809         op->o_callback = &cb;
810         op->o_time = slap_get_time();
811         op->o_do_not_cache = 1;
812
813         op->o_req_dn = op->o_bd->be_suffix[0];
814         op->o_req_ndn = op->o_bd->be_nsuffix[0];
815         op->ors_scope = LDAP_SCOPE_SUBTREE;
816         op->ors_deref = LDAP_DEREF_NEVER;
817         op->ors_slimit = SLAP_NO_LIMIT;
818         op->ors_tlimit = SLAP_NO_LIMIT;
819         op->ors_filter = &filter;
820         op->ors_filterstr.bv_val = filter_str;
821         op->ors_filterstr.bv_len = strlen(filter_str);
822         op->ors_attrs = NULL;
823         op->ors_attrsonly = 0;
824
825         op->o_bd->be_search( op, &sreply );
826
827         for ( qi=cb.sc_private; qi; qi=qnext ) {
828                 qnext = qi->next;
829
830                 op->o_req_dn = qi->xdn;
831                 op->o_req_ndn = qi->xdn;
832
833                 if ( qi->del) {
834                         Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
835                                 query_uuid->bv_val, 0, 0 );
836
837                         op->o_tag = LDAP_REQ_DELETE;
838
839                         if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
840                                 deleted++;
841                         }
842                 } else {
843                         Modifications mod;
844                         struct berval vals[2];
845
846                         vals[0] = *query_uuid;
847                         vals[1].bv_val = NULL;
848                         vals[1].bv_len = 0;
849                         mod.sml_op = LDAP_MOD_DELETE;
850                         mod.sml_desc = ad_queryid;
851                         mod.sml_type = ad_queryid->ad_cname;
852                         mod.sml_values = vals;
853                         mod.sml_nvalues = NULL;
854                         mod.sml_next = NULL;
855                         Debug( LDAP_DEBUG_ANY,
856                                 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
857                                 query_uuid->bv_val, 0, 0 );
858
859                         op->orm_modlist = &mod;
860
861                         op->o_bd->be_modify( op, &sreply );
862                 }
863                 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
864                 op->o_tmpfree( qi, op->o_tmpmemctx );
865         }
866         return deleted;
867 }
868
869 static int
870 get_attr_set(
871         AttributeName* attrs,
872         query_manager* qm,
873         int num
874 );
875
876 static int
877 attrscmp(
878         AttributeName* attrs_in,
879         AttributeName* attrs
880 );
881
882 static int
883 is_temp_answerable(
884         int attr_set,
885         struct berval* tempstr,
886         query_manager* qm,
887         int template_id )
888 {
889         QueryTemplate *qt = qm->templates + template_id;
890
891         if (attr_set != qt->attr_set_index) {
892                 int* id_array = qm->attr_sets[attr_set].ID_array;
893
894                 while (*id_array != -1) {
895                         if (*id_array == qt->attr_set_index)
896                                 break;
897                         id_array++;
898                 }
899                 if (*id_array == -1)
900                         return 0;
901         }
902         return (qt->querystr.bv_len == tempstr->bv_len &&
903                 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
904 }
905
906 static int
907 filter2template(
908         Filter                  *f,
909         struct                  berval *fstr,
910         AttributeName**         filter_attrs,
911         int*                    filter_cnt )
912 {
913         AttributeDescription *ad;
914
915         switch ( f->f_choice ) {
916         case LDAP_FILTER_EQUALITY:
917                 ad = f->f_av_desc;
918                 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
919                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
920                 break;
921
922         case LDAP_FILTER_GE:
923                 ad = f->f_av_desc;
924                 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
925                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
926                 break;
927
928         case LDAP_FILTER_LE:
929                 ad = f->f_av_desc;
930                 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
931                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
932                 break;
933
934         case LDAP_FILTER_APPROX:
935                 ad = f->f_av_desc;
936                 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
937                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
938                 break;
939
940         case LDAP_FILTER_SUBSTRINGS:
941                 ad = f->f_sub_desc;
942                 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
943                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
944                 break;
945
946         case LDAP_FILTER_PRESENT:
947                 ad = f->f_desc;
948                 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
949                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
950                 break;
951
952         case LDAP_FILTER_AND:
953         case LDAP_FILTER_OR:
954         case LDAP_FILTER_NOT: {
955                 int rc = 0;
956                 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
957                         f->f_choice == LDAP_FILTER_AND ? '&' :
958                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
959                 fstr->bv_len += sizeof("(%") - 1;
960
961                 for ( f = f->f_list; f != NULL; f = f->f_next ) {
962                         rc = filter2template( f, fstr, filter_attrs, filter_cnt );
963                         if ( rc ) break;
964                 }
965                 sprintf( fstr->bv_val+fstr->bv_len, ")" );
966                 fstr->bv_len += sizeof(")") - 1;
967
968                 return rc;
969                 }
970
971         default:
972                 strcpy( fstr->bv_val, "(?=?)" );
973                 fstr->bv_len += sizeof("(?=?)")-1;
974                 return -1;
975         }
976
977         *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
978                                 (*filter_cnt + 2)*sizeof(AttributeName));
979
980         (*filter_attrs)[*filter_cnt].an_desc = ad;
981         (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
982         (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
983         (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
984         (*filter_cnt)++;
985         return 0;
986 }
987
988 struct search_info {
989         slap_overinst *on;
990         Query query;
991         int template_id;
992         int max;
993         int over;
994         int count;
995         Entry *head, *tail;
996 };
997
998 static int
999 cache_entries(
1000         Operation       *op,
1001         SlapReply       *rs,
1002         struct berval *query_uuid)
1003 {
1004         struct search_info *si = op->o_callback->sc_private;
1005         slap_overinst *on = si->on;
1006         cache_manager *cm = on->on_bi.bi_private;
1007         query_manager*          qm = cm->qm;
1008         int             i;
1009         int             return_val = 0;
1010         Entry           *e;
1011         struct berval   crp_uuid;
1012         char            uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1013         Operation op_tmp = *op;
1014
1015         query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1016         ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1017
1018         op_tmp.o_bd = &cm->db;
1019         op_tmp.o_dn = cm->db.be_rootdn;
1020         op_tmp.o_ndn = cm->db.be_rootndn;
1021
1022         Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1023                         uuidbuf, 0, 0 );
1024
1025         for ( e=si->head; e; e=si->head ) {
1026                 si->head = e->e_private;
1027                 e->e_private = NULL;
1028                 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1029                 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1030                 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1031                 while ( cm->cur_entries > (cm->max_entries) ) {
1032                                 qm->crfunc(qm, &crp_uuid);
1033                                 if (crp_uuid.bv_val) {
1034                                         Debug( LDAP_DEBUG_ANY,
1035                                                 "Removing query UUID %s\n",
1036                                                 crp_uuid.bv_val, 0, 0 );
1037                                         return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
1038                                         Debug( LDAP_DEBUG_ANY,
1039                                                 "QUERY REMOVED, SIZE=%d\n",
1040                                                 return_val, 0, 0);
1041                                         ldap_pvt_thread_mutex_lock(
1042                                                         &cm->cache_mutex );
1043                                         cm->cur_entries -= return_val;
1044                                         cm->num_cached_queries--;
1045                                         Debug( LDAP_DEBUG_ANY,
1046                                                 "STORED QUERIES = %lu\n",
1047                                                 cm->num_cached_queries, 0, 0 );
1048                                         ldap_pvt_thread_mutex_unlock(
1049                                                         &cm->cache_mutex );
1050                                         Debug( LDAP_DEBUG_ANY,
1051                                                 "QUERY REMOVED, CACHE ="
1052                                                 "%d entries\n",
1053                                                 cm->cur_entries, 0, 0 );
1054                                 }
1055                 }
1056
1057                 return_val = merge_entry(&op_tmp, e, query_uuid);
1058                 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1059                 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1060                 cm->cur_entries += return_val;
1061                 Debug( LDAP_DEBUG_ANY,
1062                         "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1063                         cm->cur_entries, 0, 0 );
1064                 return_val = 0;
1065                 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1066         }
1067         ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1068         cm->num_cached_queries++;
1069         Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1070                         cm->num_cached_queries, 0, 0 );
1071         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1072
1073         return return_val;
1074 }
1075
1076 static int
1077 proxy_cache_response(
1078         Operation       *op,
1079         SlapReply       *rs )
1080 {
1081         struct search_info *si = op->o_callback->sc_private;
1082         slap_overinst *on = si->on;
1083         cache_manager *cm = on->on_bi.bi_private;
1084         query_manager*          qm = cm->qm;
1085         struct berval uuid;
1086
1087         if ( rs->sr_type == REP_SEARCH ) {
1088                 Entry *e;
1089                 /* If we haven't exceeded the limit for this query,
1090                  * build a chain of answers to store. If we hit the
1091                  * limit, empty the chain and ignore the rest.
1092                  */
1093                 if ( !si->over ) {
1094                         if ( si->count < si->max ) {
1095                                 si->count++;
1096                                 e = entry_dup( rs->sr_entry );
1097                                 if ( !si->head ) si->head = e;
1098                                 if ( si->tail ) si->tail->e_private = e;
1099                                 si->tail = e;
1100                         } else {
1101                                 si->over = 1;
1102                                 si->count = 0;
1103                                 for (;si->head; si->head=e) {
1104                                         e = si->head->e_private;
1105                                         si->head->e_private = NULL;
1106                                         entry_free(si->head);
1107                                 }
1108                                 si->tail = NULL;
1109                         }
1110                 }
1111
1112                 if (rs->sr_attrs != op->ors_attrs ) {
1113                         op->o_tmpfree( rs->sr_attrs, op->o_tmpmemctx );
1114                 }
1115                 rs->sr_attrs = si->query.save_attrs;
1116                 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
1117                 op->ors_attrs = si->query.save_attrs;
1118                 si->query.save_attrs = NULL;
1119
1120         } else if ( rs->sr_type == REP_RESULT ) {
1121                 if ( si->count && cache_entries( op, rs, &uuid ) == 0 ) {
1122                         qm->addfunc(qm, &si->query, si->template_id, &uuid);
1123                         /* If the consistency checker suspended itself,
1124                          * wake it back up
1125                          */
1126                         if ( cm->cc_paused ) {
1127                                 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1128                                 if ( cm->cc_paused ) {
1129                                         cm->cc_paused = 0;
1130                                         ldap_pvt_runqueue_resched( &syncrepl_rq, cm->cc_arg, 0 );
1131                                 }
1132                                 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1133                         }
1134                 }
1135
1136                 /* free self */
1137                 op->o_callback->sc_cleanup = slap_freeself_cb;
1138         }
1139         return SLAP_CB_CONTINUE;
1140 }
1141
1142 static void
1143 add_filter_attrs(
1144         Operation *op,
1145         AttributeName** new_attrs,
1146         AttributeName* attrs,
1147         AttributeName* filter_attrs )
1148 {
1149         int alluser = 0;
1150         int allop = 0;
1151         int i;
1152         int count;
1153
1154         /* duplicate attrs */
1155         if (attrs == NULL) {
1156                 count = 1;
1157         } else {
1158                 for (count=0; attrs[count].an_name.bv_val; count++)
1159                         ;
1160         }
1161         *new_attrs = (AttributeName*)(op->o_tmpalloc((count+1)*
1162                 sizeof(AttributeName), op->o_tmpmemctx));
1163         if (attrs == NULL) {
1164                 (*new_attrs)[0].an_name.bv_val = "*";
1165                 (*new_attrs)[0].an_name.bv_len = 1;
1166                 (*new_attrs)[1].an_name.bv_val = NULL;
1167                 (*new_attrs)[1].an_name.bv_len = 0;
1168                 alluser = 1;
1169                 allop = 0;
1170         } else {
1171                 for (i=0; i<count; i++) {
1172                         (*new_attrs)[i].an_name = attrs[i].an_name;
1173                         (*new_attrs)[i].an_desc = attrs[i].an_desc;
1174                 }
1175                 (*new_attrs)[count].an_name.bv_val = NULL;
1176                 (*new_attrs)[count].an_name.bv_len = 0;
1177                 alluser = an_find(*new_attrs, &AllUser);
1178                 allop = an_find(*new_attrs, &AllOper);
1179         }
1180
1181         for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1182                 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1183                         continue;
1184                 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1185                         if (allop)
1186                                 continue;
1187                 } else if (alluser)
1188                         continue;
1189                 *new_attrs = (AttributeName*)(op->o_tmprealloc(*new_attrs,
1190                                         (count+2)*sizeof(AttributeName), op->o_tmpmemctx));
1191                 (*new_attrs)[count].an_name = filter_attrs[i].an_name;
1192                 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc;
1193                 count++;
1194                 (*new_attrs)[count].an_name.bv_val = NULL;
1195                 (*new_attrs)[count].an_name.bv_len = 0;
1196         }
1197 }
1198
1199 static int
1200 proxy_cache_search(
1201         Operation       *op,
1202         SlapReply       *rs )
1203 {
1204         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1205         cache_manager *cm = on->on_bi.bi_private;
1206         query_manager*          qm = cm->qm;
1207
1208         int count;
1209
1210         int i = -1;
1211
1212         AttributeName   *filter_attrs = NULL;
1213         AttributeName   *new_attrs = NULL;
1214
1215         Query           query;
1216
1217         int             attr_set = -1;
1218         int             template_id = -1;
1219         int             answerable = 0;
1220         int             cacheable = 0;
1221         int             fattr_cnt=0;
1222         int             oc_attr_absent = 1;
1223
1224         struct berval tempstr;
1225
1226         tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1227         tempstr.bv_len = 0;
1228         if (filter2template(op->ors_filter, &tempstr, &filter_attrs, &fattr_cnt)) {
1229                 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1230                 return SLAP_CB_CONTINUE;
1231         }
1232
1233         Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
1234                                         tempstr.bv_val, 0, 0 );
1235
1236         /* find attr set */
1237         attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1238
1239         query.filter = op->ors_filter;
1240         query.attrs = op->ors_attrs;
1241         query.save_attrs = NULL;
1242         query.base = op->o_req_ndn;
1243         query.scope = op->ors_scope;
1244
1245         /* check for query containment */
1246         if (attr_set > -1) {
1247                 for (i=0; i<cm->numtemplates; i++) {
1248                         /* find if template i can potentially answer tempstr */
1249                         if (!is_temp_answerable(attr_set, &tempstr, qm, i))
1250                                 continue;
1251                         if (attr_set == qm->templates[i].attr_set_index) {
1252                                 cacheable = 1;
1253                                 template_id = i;
1254                         }
1255                         Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1256                                         op->ors_filterstr.bv_val, 0, 0 );
1257                         answerable = (*(qm->qcfunc))(qm, &query, i);
1258
1259                         if (answerable)
1260                                 break;
1261                 }
1262         }
1263         op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1264
1265         query.save_attrs = op->ors_attrs;
1266         query.attrs = NULL;
1267
1268         if (answerable) {
1269                 /* Need to clear the callbacks of the original operation,
1270                  * in case there are other overlays */
1271                 BackendDB       *save_bd = op->o_bd;
1272                 slap_callback   *save_cb = op->o_callback;
1273
1274                 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
1275                 free(filter_attrs);
1276                 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
1277                 op->o_bd = &cm->db;
1278                 op->o_callback = NULL;
1279                 i = cm->db.bd_info->bi_op_search( op, rs );
1280                 op->o_bd = save_bd;
1281                 op->o_callback = save_cb;
1282                 return i;
1283         }
1284
1285         Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1286
1287         ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1288         if (cm->num_cached_queries >= cm->max_queries) {
1289                 cacheable = 0;
1290         }
1291         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1292
1293         if (cacheable) {
1294                 slap_callback           *cb;
1295                 struct search_info      *si;
1296
1297                 Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
1298                 query.filter = str2filter(op->ors_filterstr.bv_val);
1299                 if (op->ors_attrs) {
1300                         for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1301                                 if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass ) {
1302                                         oc_attr_absent = 0;
1303                                 }
1304                         }
1305                         query.attrs = (AttributeName *)ch_malloc( ( count + 1 + oc_attr_absent )
1306                                                                         *sizeof(AttributeName) );
1307                         for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1308                                 ber_dupbv( &query.attrs[count].an_name, &op->ors_attrs[count].an_name );
1309                                 query.attrs[count].an_desc = op->ors_attrs[count].an_desc;
1310                         }
1311                         if ( oc_attr_absent ) {
1312                                 query.attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
1313                                 ber_dupbv( &query.attrs[count].an_name,
1314                                         &slap_schema.si_ad_objectClass->ad_cname );
1315                                 count++;
1316                         }
1317                         query.attrs[ count ].an_name.bv_val = NULL;
1318                         query.attrs[ count ].an_name.bv_len = 0;
1319                 }
1320                 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
1321
1322                 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1323                 cb->sc_response = proxy_cache_response;
1324                 cb->sc_cleanup = NULL;
1325                 cb->sc_private = (cb+1);
1326                 si = cb->sc_private;
1327                 si->on = on;
1328                 si->query = query;
1329                 si->template_id = template_id;
1330                 si->max = cm->num_entries_limit ;
1331                 si->over = 0;
1332                 si->count = 0;
1333                 si->head = NULL;
1334                 si->tail = NULL;
1335
1336                 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1337                         cb->sc_next = op->o_callback;
1338                         op->o_callback = cb;
1339
1340                 } else {
1341                         slap_callback           **pcb;
1342
1343                         /* need to move the callback at the end, in case other
1344                          * overlays are present, so that the final entry is
1345                          * actually cached */
1346                         cb->sc_next = NULL;
1347                         for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
1348                         *pcb = cb;
1349                 }
1350
1351         } else {
1352                 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE\n",
1353                                         0, 0, 0);
1354         }
1355
1356         free(filter_attrs);
1357
1358         return SLAP_CB_CONTINUE;
1359 }
1360
1361 static int
1362 attrscmp(
1363         AttributeName* attrs_in,
1364         AttributeName* attrs)
1365 {
1366         int i, count1, count2;
1367         if ( attrs_in == NULL ) {
1368                 return (attrs ? 0 : 1);
1369         }
1370         if ( attrs == NULL )
1371                 return 0;
1372
1373         for ( count1=0;
1374               attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1375               count1++ )
1376                 ;
1377         for ( count2=0;
1378               attrs && attrs[count2].an_name.bv_val != NULL;
1379               count2++)
1380                 ;
1381         if ( count1 != count2 )
1382                 return 0;
1383
1384         for ( i=0; i<count1; i++ ) {
1385                 if ( !an_find(attrs, &attrs_in[i].an_name ))
1386                         return 0;
1387         }
1388         return 1;
1389 }
1390
1391 static int
1392 get_attr_set(
1393         AttributeName* attrs,
1394         query_manager* qm,
1395         int num )
1396 {
1397         int i;
1398         for (i=0; i<num; i++) {
1399                 if (attrscmp(attrs, qm->attr_sets[i].attrs))
1400                         return i;
1401         }
1402         return -1;
1403 }
1404
1405 static void*
1406 consistency_check(
1407         void *ctx,
1408         void *arg )
1409 {
1410         struct re_s *rtask = arg;
1411         slap_overinst *on = rtask->arg;
1412         cache_manager *cm = on->on_bi.bi_private;
1413         query_manager *qm = cm->qm;
1414         Operation op = {0};
1415         Connection conn = {0};
1416
1417         SlapReply rs = {REP_RESULT};
1418         CachedQuery* query, *query_prev;
1419         int i, return_val, pause = 1;
1420         QueryTemplate* templ;
1421
1422         connection_fake_init( &conn, &op, ctx );
1423
1424         op.o_bd = &cm->db;
1425         op.o_dn = cm->db.be_rootdn;
1426         op.o_ndn = cm->db.be_rootndn;
1427
1428         cm->cc_arg = arg;
1429
1430         for (i=0; qm->templates[i].querystr.bv_val; i++) {
1431                 templ = qm->templates + i;
1432                 query = templ->query_last;
1433                 if ( query ) pause = 0;
1434                 op.o_time = slap_get_time();
1435                 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1436                 while (query && (query->expiry_time < op.o_time)) {
1437                         ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1438                         remove_query(qm, query);
1439                         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1440                         Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1441                                         i, 0, 0 );
1442                         ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1443                         remove_from_template(query, templ);
1444                         Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1445                                         i, templ->no_of_queries, 0 );
1446                         Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1447                                         i, 0, 0 );
1448                         ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1449                         return_val = remove_query_data(&op, &rs, &query->q_uuid);
1450                         Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1451                                                 return_val, 0, 0 );
1452                         ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1453                         cm->cur_entries -= return_val;
1454                         cm->num_cached_queries--;
1455                         Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1456                                         cm->num_cached_queries, 0, 0 );
1457                         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1458                         Debug( LDAP_DEBUG_ANY,
1459                                 "STALE QUERY REMOVED, CACHE ="
1460                                 "%d entries\n",
1461                                 cm->cur_entries, 0, 0 );
1462                         query_prev = query;
1463                         query = query->prev;
1464                         free_query(query_prev);
1465                 }
1466                 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1467         }
1468         ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1469         if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
1470                 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
1471         }
1472         /* If there were no queries, defer processing for a while */
1473         cm->cc_paused = pause;
1474         ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, pause );
1475
1476         ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1477         return NULL;
1478 }
1479
1480
1481 #define MAX_ATTR_SETS 500
1482 static void find_supersets( struct attr_set* attr_sets, int numsets );
1483 static int compare_sets( struct attr_set* setA, int, int );
1484
1485 static int
1486 proxy_cache_config(
1487         BackendDB       *be,
1488         const char      *fname,
1489         int             lineno,
1490         int             argc,
1491         char            **argv
1492 )
1493 {
1494         slap_overinst   *on = (slap_overinst *)be->bd_info;
1495         cache_manager*  cm = on->on_bi.bi_private;
1496         query_manager*  qm = cm->qm;
1497         QueryTemplate*  temp;
1498         AttributeName*  attr_name;
1499         AttributeName*  attrarray;
1500         const char*     text=NULL;
1501         char            *save_argv0 = NULL;
1502
1503         int             index, i;
1504         int             num;
1505         int             rc = 0;
1506
1507         if ( strncasecmp( argv[0], "proxycache-", STRLENOF( "proxycache-" ) ) == 0 ) {
1508                 save_argv0 = argv[0];
1509                 argv[0] += STRLENOF( "proxycache-" );
1510         }
1511
1512         if ( strcasecmp( argv[0], "proxycache" ) == 0 ) {
1513                 if ( argc < 6 ) {
1514                         fprintf( stderr, "%s: line %d: missing arguments in \"proxycache"
1515                                 " <backend> <max_entries> <numattrsets> <entry limit> "
1516                                 "<cycle_time>\"\n", fname, lineno );
1517                         return( 1 );
1518                 }
1519
1520                 cm->db.bd_info = backend_info( argv[1] );
1521                 if ( !cm->db.bd_info ) {
1522                         fprintf( stderr, "%s: line %d: backend %s unknown\n",
1523                                 fname, lineno, argv[1] );
1524                         return( 1 );
1525                 }
1526                 if ( cm->db.bd_info->bi_db_init( &cm->db ) ) return( 1 );
1527
1528                 /* This type is in use, needs to be opened */
1529                 cm->db.bd_info->bi_nDB++;
1530
1531                 cm->max_entries = atoi( argv[2] );
1532
1533                 cm->numattrsets = atoi( argv[3] );
1534                 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1535                         fprintf( stderr, "%s: line %d: numattrsets must be <= %d\n",
1536                                 fname, lineno, MAX_ATTR_SETS );
1537                         return( 1 );
1538                 }
1539
1540                 cm->num_entries_limit = atoi( argv[4] );
1541                 cm->cc_period = atoi( argv[5] );
1542                 Debug( LDAP_DEBUG_ANY,
1543                                 "Total # of attribute sets to be cached = %d\n",
1544                                 cm->numattrsets, 0, 0 );
1545                 qm->attr_sets = ( struct attr_set * )ch_malloc( cm->numattrsets *
1546                                                 sizeof( struct attr_set ));
1547                 for ( i = 0; i < cm->numattrsets; i++ ) {
1548                         qm->attr_sets[i].attrs = NULL;
1549                 }
1550
1551         } else if ( strcasecmp( argv[0], "proxyattrset" ) == 0 ) {
1552                 if ( argc < 3 ) {
1553                         fprintf( stderr, "%s: line %d: missing arguments in \"proxyattrset "
1554                                 "<index> <attributes>\"\n", fname, lineno );
1555                         return( 1 );
1556                 }
1557                 Debug( LDAP_DEBUG_ANY, "Attribute Set # %d\n",
1558                                 atoi( argv[1] ), 0, 0 );
1559                 if (atoi(argv[1]) >= cm->numattrsets) {
1560                         fprintf( stderr, "%s; line %d index out of bounds \n",
1561                                         fname, lineno );
1562                         return 1;
1563                 }
1564                 index = atoi( argv[1] );
1565                 if ( argv[2] && strcmp( argv[2], "*" ) ) {
1566                         qm->attr_sets[index].count = argc - 2;
1567                         qm->attr_sets[index].attrs = (AttributeName*)ch_malloc(
1568                                                 (argc-1) * sizeof( AttributeName ));
1569                         attr_name = qm->attr_sets[index].attrs;
1570                         for ( i = 2; i < argc; i++ ) {
1571                                 Debug( LDAP_DEBUG_ANY, "\t %s\n",
1572                                                 argv[i], 0, 0 );
1573                                 ber_str2bv( argv[i], 0, 1,
1574                                                 &attr_name->an_name);
1575                                 attr_name->an_desc = NULL;
1576                                 slap_bv2ad( &attr_name->an_name,
1577                                                 &attr_name->an_desc, &text );
1578                                 attr_name++;
1579                                 attr_name->an_name.bv_val = NULL;
1580                                 attr_name->an_name.bv_len = 0;
1581                         }
1582                 }
1583         } else if ( strcasecmp( argv[0], "proxytemplate" ) == 0 ) {
1584                 if ( argc != 4 ) {
1585                         fprintf( stderr, "%s: line %d: missing argument(s) in "
1586                                 "\"proxytemplate <filter> <proj attr set> <TTL>\" line\n",
1587                                 fname, lineno );
1588                         return( 1 );
1589                 }
1590                 if (( i = atoi( argv[2] )) >= cm->numattrsets ) {
1591                         Debug( LDAP_DEBUG_ANY,
1592                                         "%s: line %d, template index invalid\n",
1593                                         fname, lineno, 0 );
1594                         return 1;
1595                 }
1596                 num = cm->numtemplates;
1597                 if ( num == 0 )
1598                         find_supersets( qm->attr_sets, cm->numattrsets );
1599                 qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
1600                                 ( num + 2 ) * sizeof( QueryTemplate ));
1601                 temp = qm->templates + num;
1602                 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
1603                 temp->query = temp->query_last = NULL;
1604                 temp->ttl = atoi( argv[3] );
1605                 temp->no_of_queries = 0;
1606                 if ( argv[1] == NULL ) {
1607                         Debug( LDAP_DEBUG_ANY,
1608                                         "Templates string not specified "
1609                                         "for template %d\n", num, 0, 0 );
1610                         return 1;
1611                 }
1612                 ber_str2bv( argv[1], 0, 1, &temp->querystr );
1613                 Debug( LDAP_DEBUG_ANY, "Template:\n", 0, 0, 0 );
1614                 Debug( LDAP_DEBUG_ANY, "  query template: %s\n",
1615                                 temp->querystr.bv_val, 0, 0 );
1616                 temp->attr_set_index = i;
1617                 Debug( LDAP_DEBUG_ANY, "  attributes: \n", 0, 0, 0 );
1618                 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1619                         for ( i=0; attrarray[i].an_name.bv_val; i++ )
1620                                 Debug( LDAP_DEBUG_ANY, "\t%s\n",
1621                                         attrarray[i].an_name.bv_val, 0, 0 );
1622                 }
1623                 temp++; 
1624                 temp->querystr.bv_val = NULL;
1625                 cm->numtemplates++;
1626
1627         } else if ( strcasecmp( argv[0], "response-callback" ) == 0 ) {
1628                 /* set to "tail" to put the response callback
1629                  * at the end of the callback list; this is required
1630                  * in case other overlays are present, so that the
1631                  * final entry is cached. */
1632
1633                 if ( argc < 2 ) {
1634                         Debug( LDAP_DEBUG_ANY,
1635                                         "missing specifier for \"response-callback {head(default)|tail}\" "
1636                                         "callback position\n", 0, 0, 0 );
1637                         return 1;
1638                 }
1639
1640                 if ( strcasecmp( argv[1], "head" ) == 0 ) {
1641                         cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
1642
1643                 } else if ( strcasecmp( argv[1], "tail" ) == 0 ) {
1644                         cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1645
1646                 } else {
1647                         Debug( LDAP_DEBUG_ANY,
1648                                         "unknown specifier %s for \"response-callback {head(default)|tail}\" "
1649                                         "callback position\n", argv[1], 0, 0 );
1650                         return 1;
1651                 }
1652         }
1653         /* anything else */
1654         else {
1655                 rc = cm->db.bd_info->bi_db_config( &cm->db, fname, lineno, argc, argv );
1656         }
1657
1658         if ( save_argv0 ) {
1659                 argv[0] = save_argv0;
1660         }
1661
1662         return rc;
1663 }
1664
1665 static int
1666 proxy_cache_init(
1667         BackendDB *be
1668 )
1669 {
1670         slap_overinst *on = (slap_overinst *)be->bd_info;
1671         cache_manager *cm;
1672         query_manager *qm;
1673
1674         cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1675         on->on_bi.bi_private = cm;
1676
1677         qm = (query_manager*)ch_malloc(sizeof(query_manager));
1678
1679         cm->db = *be;
1680         SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1681         cm->db.be_private = NULL;
1682         cm->db.be_pcl_mutexp = &cm->db.be_pcl_mutex;
1683         cm->qm = qm;
1684         cm->numattrsets = 0;
1685         cm->numtemplates = 0; 
1686         cm->num_entries_limit = 5;
1687         cm->num_cached_queries = 0;
1688         cm->max_entries = 0;
1689         cm->cur_entries = 0;
1690         cm->max_queries = 10000;
1691         cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1692         cm->cc_period = 1000;
1693         cm->cc_paused = 0;
1694
1695         qm->attr_sets = NULL;
1696         qm->templates = NULL;
1697         qm->lru_top = NULL;
1698         qm->lru_bottom = NULL;
1699
1700         qm->qcfunc = query_containment;
1701         qm->crfunc = cache_replacement;
1702         qm->addfunc = add_query;
1703         ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1704
1705         ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1706         ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1707         return 0;
1708 }
1709
1710 static int
1711 proxy_cache_open(
1712         BackendDB *be
1713 )
1714 {
1715         slap_overinst   *on = (slap_overinst *)be->bd_info;
1716         cache_manager   *cm = on->on_bi.bi_private;
1717         int             rc = 0;
1718         int             i;
1719
1720         /* consistency check (add more...) */
1721         for ( i = 0; i < cm->numattrsets; i++ ) {
1722                 if ( cm->qm->attr_sets[i].attrs == NULL ) {
1723                         fprintf( stderr, "proxy_cache_open(): "
1724                                 "attr set %d (of %d) missing\n",
1725                                 i, cm->numattrsets );
1726                         return 1;
1727                 }
1728         }
1729
1730         rc = backend_startup_one( &cm->db );
1731
1732         /* There is no runqueue in TOOL mode */
1733         if ( slapMode & SLAP_SERVER_MODE ) {
1734                 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1735                 ldap_pvt_runqueue_insert( &syncrepl_rq, cm->cc_period,
1736                         consistency_check, on );
1737                 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1738
1739                 /* Cached database must have the rootdn */
1740                 if ( BER_BVISNULL( &cm->db.be_rootndn )
1741                                 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
1742                 {
1743                         fprintf( stderr, "proxy_cache_open(): "
1744                                 "underlying database of type \"%s\"\n"
1745                                 "    serving naming context \"%s\"\n"
1746                                 "    has no \"rootdn\", required by \"proxycache\".\n",
1747                                 on->on_info->oi_orig->bi_type,
1748                                 cm->db.be_suffix[0].bv_val );
1749                         return 1;
1750                 }
1751         }
1752
1753         return rc;
1754 }
1755
1756 static int
1757 proxy_cache_close(
1758         BackendDB *be
1759 )
1760 {
1761         slap_overinst *on = (slap_overinst *)be->bd_info;
1762         cache_manager *cm = on->on_bi.bi_private;
1763         query_manager *qm = cm->qm;
1764         int i, j, rc = 0;
1765
1766         if ( cm->db.bd_info->bi_db_close ) {
1767                 rc = cm->db.bd_info->bi_db_close( &cm->db );
1768         }
1769         for ( i=0; i<cm->numtemplates; i++ ) {
1770                 CachedQuery *qc, *qn;
1771                 for ( qc = qm->templates[i].query; qc; qc = qn ) {
1772                         qn = qc->next;
1773                         free_query( qc );
1774                 }
1775                 free( qm->templates[i].querystr.bv_val );
1776                 ldap_pvt_thread_rdwr_destroy( &qm->templates[i].t_rwlock );
1777         }
1778         free( qm->templates );
1779         qm->templates = NULL;
1780
1781         for ( i=0; i<cm->numattrsets; i++ ) {
1782                 free( qm->attr_sets[i].ID_array );
1783                 for ( j=0; j<qm->attr_sets[i].count; j++ ) {
1784                         free( qm->attr_sets[i].attrs[j].an_name.bv_val );
1785                 }
1786                 free( qm->attr_sets[i].attrs );
1787         }
1788         free( qm->attr_sets );
1789         qm->attr_sets = NULL;
1790
1791         return rc;
1792 }
1793
1794 static int
1795 proxy_cache_destroy(
1796         BackendDB *be
1797 )
1798 {
1799         slap_overinst *on = (slap_overinst *)be->bd_info;
1800         cache_manager *cm = on->on_bi.bi_private;
1801         query_manager *qm = cm->qm;
1802         int rc = 0;
1803
1804         if ( cm->db.bd_info->bi_db_destroy ) {
1805                 rc = cm->db.bd_info->bi_db_destroy( &cm->db );
1806         }
1807         ldap_pvt_thread_mutex_destroy(&qm->lru_mutex);
1808         ldap_pvt_thread_mutex_destroy(&cm->cache_mutex);
1809         ldap_pvt_thread_mutex_destroy(&cm->remove_mutex);
1810         free( qm );
1811         free( cm );
1812         return rc;
1813 }
1814
1815 static void
1816 find_supersets ( struct attr_set* attr_sets, int numsets )
1817 {
1818         int num[MAX_ATTR_SETS];
1819         int i, j, res;
1820         int* id_array;
1821         for ( i = 0; i < MAX_ATTR_SETS; i++ )
1822                 num[i] = 0;
1823
1824         for ( i = 0; i < numsets; i++ ) {
1825                 attr_sets[i].ID_array = (int*) ch_malloc( sizeof( int ) );
1826                 attr_sets[i].ID_array[0] = -1;
1827         }
1828
1829         for ( i = 0; i < numsets; i++ ) {
1830                 for ( j=i+1; j < numsets; j++ ) {
1831                         res = compare_sets( attr_sets, i, j );
1832                         switch ( res ) {
1833                         case 0:
1834                                 break;
1835                         case 3:
1836                         case 1:
1837                                 id_array = attr_sets[i].ID_array;
1838                                 attr_sets[i].ID_array = (int *) ch_realloc( id_array,
1839                                                         ( num[i] + 2 ) * sizeof( int ));
1840                                 attr_sets[i].ID_array[num[i]] = j;
1841                                 attr_sets[i].ID_array[num[i]+1] = -1;
1842                                 num[i]++;
1843                                 if (res == 1)
1844                                         break;
1845                         case 2:
1846                                 id_array = attr_sets[j].ID_array;
1847                                 attr_sets[j].ID_array = (int *) ch_realloc( id_array,
1848                                                 ( num[j] + 2 ) * sizeof( int ));
1849                                 attr_sets[j].ID_array[num[j]] = i;
1850                                 attr_sets[j].ID_array[num[j]+1] = -1;
1851                                 num[j]++;
1852                                 break;
1853                         }
1854                 }
1855         }
1856 }
1857
1858 /*
1859  * compares two sets of attributes (indices i and j)
1860  * returns 0: if neither set is contained in the other set
1861  *         1: if set i is contained in set j
1862  *         2: if set j is contained in set i
1863  *         3: the sets are equivalent
1864  */
1865
1866 static int
1867 compare_sets(struct attr_set* set, int i, int j)
1868 {
1869         int k,l,numI,numJ;
1870         int common=0;
1871         int result=0;
1872
1873         if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
1874                 return 3;
1875
1876         if ( set[i].attrs == NULL )
1877                 return 2;
1878
1879         if ( set[j].attrs == NULL )
1880                 return 1;
1881
1882         numI = set[i].count;
1883         numJ = set[j].count;
1884
1885         for ( l=0; l < numI; l++ ) {
1886                 for ( k = 0; k < numJ; k++ ) {
1887                         if ( strcmp( set[i].attrs[l].an_name.bv_val,
1888                                      set[j].attrs[k].an_name.bv_val ) == 0 )
1889                                 common++;
1890                 }
1891         }
1892
1893         if ( common == numI )
1894                 result = 1;
1895
1896         if ( common == numJ )
1897                 result += 2;
1898
1899         return result;
1900 }
1901
1902 static slap_overinst proxy_cache;
1903
1904 int pcache_init()
1905 {
1906         LDAPAttributeType *at;
1907         int code;
1908         const char *err;
1909
1910         at = ldap_str2attributetype( queryid_schema, &code, &err,
1911                 LDAP_SCHEMA_ALLOW_ALL );
1912         if ( !at ) {
1913                 fprintf( stderr, "AttributeType Load failed %s %s\n",
1914                         ldap_scherr2str(code), err );
1915                 return code;
1916         }
1917         code = at_add( at, &err );
1918         if ( !code ) {
1919                 slap_str2ad( at->at_names[0], &ad_queryid, &err );
1920         }
1921         ldap_memfree( at );
1922         if ( code ) {
1923                 fprintf( stderr, "AttributeType Load failed %s %s\n",
1924                         scherr2str(code), err );
1925                 return code;
1926         }
1927
1928         proxy_cache.on_bi.bi_type = "proxycache";
1929         proxy_cache.on_bi.bi_db_init = proxy_cache_init;
1930         proxy_cache.on_bi.bi_db_config = proxy_cache_config;
1931         proxy_cache.on_bi.bi_db_open = proxy_cache_open;
1932         proxy_cache.on_bi.bi_db_close = proxy_cache_close;
1933         proxy_cache.on_bi.bi_db_destroy = proxy_cache_destroy;
1934         proxy_cache.on_bi.bi_op_search = proxy_cache_search;
1935
1936         return overlay_register( &proxy_cache );
1937 }
1938
1939 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
1940 int init_module(int argc, char *argv[]) {
1941         return pcache_init();
1942 }
1943 #endif
1944
1945 #endif  /* defined(SLAPD_OVER_PROXYCACHE) */