2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2004 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
6 * Portions Copyright 2003 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Apurva Kumar for inclusion
19 * in OpenLDAP Software and subsequently rewritten by Howard Chu.
24 #ifdef SLAPD_OVER_PROXYCACHE
28 #include <ac/string.h>
36 /* query cache structs */
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 */
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 */
59 /* struct representing a query template
60 * e.g. template string = &(cn=)(mail=)
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 */
66 CachedQuery* query; /* most recent query cached for the template */
67 CachedQuery* query_last; /* oldest query cached for the template */
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 */
75 * Represents a set of projected attributes and any
76 * supersets among all specified sets of attributes.
80 AttributeName* attrs; /* specifies the set */
81 int count; /* number of attributes */
82 int* ID_array; /* array of indices of supersets of 'attrs' */
85 struct query_manager_s;
87 /* prototypes for functions for 1) query containment
88 * 2) query addition, 3) cache replacement
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 * );
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 */
99 CachedQuery* lru_top; /* top and bottom of LRU list */
100 CachedQuery* lru_bottom;
102 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
104 /* Query cache methods */
105 QCfunc *qcfunc; /* Query containment*/
106 CRfunc *crfunc; /* cache replacement */
107 AddQueryfunc *addfunc; /* add query */
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 */
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
126 int cc_period; /* interval between successive consistency checks (sec) */
130 ldap_pvt_thread_mutex_t cache_mutex;
131 ldap_pvt_thread_mutex_t remove_mutex;
133 query_manager* qm; /* query cache managed by the cache manager */
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 )";
143 /* Return 1 for an added entry, else 0 */
148 struct berval* query_uuid )
151 Modifications* modlist = NULL;
152 const char* text = NULL;
154 char textbuf[SLAP_TEXT_BUFLEN];
155 size_t textlen = sizeof(textbuf);
157 SlapReply sreply = {REP_RESULT};
159 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
164 /* add queryid attribute */
165 attr_merge_one( e, ad_queryid, query_uuid, NULL );
167 /* append the attribute list from the fetched entry */
168 e->e_attrs->a_next = attr;
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;
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 );
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 );
200 be_entry_release_w( op, e );
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,
214 struct berval pdn_incoming = BER_BVNULL;
216 if (scope_stored < scope_incoming)
219 if ( !dnIsSuffix(ndn_incoming, ndn_stored))
222 switch(scope_stored) {
223 case LDAP_SCOPE_BASE:
224 return (ndn_incoming->bv_len == ndn_stored->bv_len);
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);
232 case LDAP_SCOPE_ONELEVEL:
233 return (ndn_incoming->bv_len == ndn_stored->bv_len);
238 case LDAP_SCOPE_SUBTREE:
247 /* add query on top of LRU list */
249 add_query_on_top (query_manager* qm, CachedQuery* qc)
251 CachedQuery* top = qm->lru_top;
252 Query* q = (Query*)qc;
263 Debug( LDAP_DEBUG_ANY, "Base of added query = %s\n",
264 q->base.bv_val, 0, 0 );
267 /* remove_query from LRU list */
270 remove_query (query_manager* qm, CachedQuery* qc)
293 qc->lru_up = qc->lru_down = NULL;
297 invert_string( struct berval *bv )
302 for (i=0; i<bv->bv_len/2; i++) {
304 bv->bv_val[i] = bv->bv_val[bv->bv_len-i-1];
305 bv->bv_val[bv->bv_len-i-1] = c;
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
316 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
323 char* string1=ber1->bv_val;
324 char* string2=ber2->bv_val;
339 temp = strstr(arg1, arg2);
343 if ( temp && (position == 2 || temp == arg1) ) {
345 strcpy( arg1, string1 );
349 if ( position == 3 ) {
357 static struct berval*
358 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
360 struct berval* merged, *temp;
361 int i, any_count, count;
363 for (any_count=0; any && any[any_count].bv_val; any_count++)
373 merged = (struct berval*)(ch_malloc((count+1)*sizeof(struct berval)));
380 for (i=0; i<any_count; i++) {
393 strings_containment(struct berval* stored, struct berval* incoming)
395 struct berval* element;
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)) {
418 substr_containment_substr(Filter* stored, Filter* incoming)
424 struct berval init_incoming;
425 struct berval final_incoming;
426 struct berval *any_incoming = NULL;
427 struct berval *remaining_incoming = NULL;
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)))
434 ber_dupbv(&init_incoming, &(incoming->f_sub_initial));
435 ber_dupbv(&final_incoming, &(incoming->f_sub_final));
437 if (incoming->f_sub_any) {
438 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
442 any_incoming = (struct berval*)ch_malloc((any_count+1) *
443 sizeof(struct berval));
445 for (i=0; i<any_count; i++) {
446 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
448 any_incoming[any_count].bv_val = NULL;
449 any_incoming[any_count].bv_len = 0;
452 if (find_and_remove(&init_incoming,
453 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
454 &(stored->f_sub_final), 3))
456 if (stored->f_sub_any == NULL) {
460 remaining_incoming = merge_init_final(&init_incoming,
461 any_incoming, &final_incoming);
462 rc = strings_containment(stored->f_sub_any, remaining_incoming);
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);
474 substr_containment_equality(Filter* stored, Filter* incoming)
476 struct berval incoming_val[2];
479 ber_dupbv(incoming_val, &(incoming->f_av_value));
480 incoming_val[1].bv_val = NULL;
481 incoming_val[1].bv_len = 0;
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){
490 rc = strings_containment(stored->f_sub_any, incoming_val);
493 free(incoming_val[0].bv_val);
497 /* check whether query is contained in any of
498 * the cached queries in template template_index
501 query_containment(query_manager *qm,
505 QueryTemplate* templa= qm->templates;
508 Filter* inputf = query->filter;
509 struct berval* base = &(query->base);
510 int scope = query->scope;
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) {
524 if(base_scope_compare(&(q->base), base, q->scope, scope)) {
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;
538 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
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 );
556 switch (fs->f_choice) {
558 case LDAP_FILTER_AND:
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(
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(
579 case LDAP_FILTER_PRESENT:
584 case LDAP_FILTER_EQUALITY:
602 case LDAP_FILTER_NOT:
608 } while((res) && (fi != NULL) && (fs != NULL));
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);
616 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
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));
630 free_query (CachedQuery* qc)
632 Query* q = (Query*)qc;
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);
646 /* Add query to query cache */
647 static void add_query(
653 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
654 QueryTemplate* templ = (qm->templates)+template_index;
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;
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;
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;
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 );
685 Debug( LDAP_DEBUG_ANY, "Unlock AQ index = %d \n",
686 template_index, 0, 0 );
687 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
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);
696 remove_from_template (CachedQuery* qc, QueryTemplate* template)
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;
707 qc->next->prev = qc->prev;
708 qc->prev->next = qc->next;
711 template->no_of_queries--;
714 /* remove bottom query of LRU list from the query cache */
715 static void cache_replacement(query_manager* qm, struct berval *result)
720 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
721 bottom = qm->lru_bottom;
723 result->bv_val = NULL;
727 Debug ( LDAP_DEBUG_ANY,
728 "Cache replacement invoked without "
729 "any query in LRU list\n", 0, 0, 0 );
733 temp_id = bottom->template_id;
734 remove_query(qm, bottom);
735 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
737 *result = bottom->q_uuid;
738 bottom->q_uuid.bv_val = NULL;
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));
751 struct query_info *next;
763 struct query_info *qi;
766 if ( rs->sr_type != REP_SEARCH ) return 0;
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++)
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 );
789 struct berval* query_uuid)
791 struct query_info *qi, *qnext;
793 AttributeAssertion ava;
794 Filter filter = {LDAP_FILTER_EQUALITY};
795 SlapReply sreply = {REP_RESULT};
796 slap_callback cb = { NULL, remove_func, NULL, NULL };
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);
804 filter.f_av_desc = ad_queryid;
805 filter.f_av_value = *query_uuid;
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;
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;
825 op->o_bd->be_search( op, &sreply );
827 for ( qi=cb.sc_private; qi; qi=qnext ) {
830 op->o_req_dn = qi->xdn;
831 op->o_req_ndn = qi->xdn;
834 Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
835 query_uuid->bv_val, 0, 0 );
837 op->o_tag = LDAP_REQ_DELETE;
839 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
844 struct berval vals[2];
846 vals[0] = *query_uuid;
847 vals[1].bv_val = NULL;
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;
855 Debug( LDAP_DEBUG_ANY,
856 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
857 query_uuid->bv_val, 0, 0 );
859 op->orm_modlist = &mod;
861 op->o_bd->be_modify( op, &sreply );
863 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
864 op->o_tmpfree( qi, op->o_tmpmemctx );
871 AttributeName* attrs,
878 AttributeName* attrs_in,
885 struct berval* tempstr,
889 QueryTemplate *qt = qm->templates + template_id;
891 if (attr_set != qt->attr_set_index) {
892 int* id_array = qm->attr_sets[attr_set].ID_array;
894 while (*id_array != -1) {
895 if (*id_array == qt->attr_set_index)
902 return (qt->querystr.bv_len == tempstr->bv_len &&
903 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
910 AttributeName** filter_attrs,
913 AttributeDescription *ad;
915 switch ( f->f_choice ) {
916 case LDAP_FILTER_EQUALITY:
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 );
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 );
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 );
934 case LDAP_FILTER_APPROX:
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 );
940 case LDAP_FILTER_SUBSTRINGS:
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 );
946 case LDAP_FILTER_PRESENT:
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 );
952 case LDAP_FILTER_AND:
954 case LDAP_FILTER_NOT: {
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;
961 for ( f = f->f_list; f != NULL; f = f->f_next ) {
962 rc = filter2template( f, fstr, filter_attrs, filter_cnt );
965 sprintf( fstr->bv_val+fstr->bv_len, ")" );
966 fstr->bv_len += sizeof(")") - 1;
972 strcpy( fstr->bv_val, "(?=?)" );
973 fstr->bv_len += sizeof("(?=?)")-1;
977 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
978 (*filter_cnt + 2)*sizeof(AttributeName));
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;
1002 struct berval *query_uuid)
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;
1011 struct berval crp_uuid;
1012 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1013 Operation op_tmp = *op;
1015 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1016 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
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;
1022 Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
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",
1041 ldap_pvt_thread_mutex_lock(
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(
1050 Debug( LDAP_DEBUG_ANY,
1051 "QUERY REMOVED, CACHE ="
1053 cm->cur_entries, 0, 0 );
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 );
1065 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
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);
1077 proxy_cache_response(
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;
1087 if ( rs->sr_type == REP_SEARCH ) {
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.
1094 if ( si->count < si->max ) {
1096 e = entry_dup( rs->sr_entry );
1097 if ( !si->head ) si->head = e;
1098 if ( si->tail ) si->tail->e_private = e;
1103 for (;si->head; si->head=e) {
1104 e = si->head->e_private;
1105 si->head->e_private = NULL;
1106 entry_free(si->head);
1112 if (rs->sr_attrs != op->ors_attrs ) {
1113 op->o_tmpfree( rs->sr_attrs, op->o_tmpmemctx );
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;
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,
1126 if ( cm->cc_paused ) {
1127 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1128 if ( cm->cc_paused ) {
1130 ldap_pvt_runqueue_resched( &syncrepl_rq, cm->cc_arg, 0 );
1132 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1137 op->o_callback->sc_cleanup = slap_freeself_cb;
1139 return SLAP_CB_CONTINUE;
1145 AttributeName** new_attrs,
1146 AttributeName* attrs,
1147 AttributeName* filter_attrs )
1154 /* duplicate attrs */
1155 if (attrs == NULL) {
1158 for (count=0; attrs[count].an_name.bv_val; count++)
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;
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;
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);
1181 for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1182 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1184 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
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;
1194 (*new_attrs)[count].an_name.bv_val = NULL;
1195 (*new_attrs)[count].an_name.bv_len = 0;
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;
1212 AttributeName *filter_attrs = NULL;
1213 AttributeName *new_attrs = NULL;
1218 int template_id = -1;
1222 int oc_attr_absent = 1;
1224 struct berval tempstr;
1226 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
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;
1233 Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
1234 tempstr.bv_val, 0, 0 );
1237 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
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;
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))
1251 if (attr_set == qm->templates[i].attr_set_index) {
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);
1263 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1265 query.save_attrs = op->ors_attrs;
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;
1274 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
1276 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
1278 op->o_callback = NULL;
1279 i = cm->db.bd_info->bi_op_search( op, rs );
1281 op->o_callback = save_cb;
1285 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1287 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1288 if (cm->num_cached_queries >= cm->max_queries) {
1291 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1295 struct search_info *si;
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 ) {
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;
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 );
1317 query.attrs[ count ].an_name.bv_val = NULL;
1318 query.attrs[ count ].an_name.bv_len = 0;
1320 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
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;
1329 si->template_id = template_id;
1330 si->max = cm->num_entries_limit ;
1336 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1337 cb->sc_next = op->o_callback;
1338 op->o_callback = cb;
1341 slap_callback **pcb;
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 */
1347 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
1352 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE\n",
1358 return SLAP_CB_CONTINUE;
1363 AttributeName* attrs_in,
1364 AttributeName* attrs)
1366 int i, count1, count2;
1367 if ( attrs_in == NULL ) {
1368 return (attrs ? 0 : 1);
1370 if ( attrs == NULL )
1374 attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1378 attrs && attrs[count2].an_name.bv_val != NULL;
1381 if ( count1 != count2 )
1384 for ( i=0; i<count1; i++ ) {
1385 if ( !an_find(attrs, &attrs_in[i].an_name ))
1393 AttributeName* attrs,
1398 for (i=0; i<num; i++) {
1399 if (attrscmp(attrs, qm->attr_sets[i].attrs))
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;
1415 Connection conn = {0};
1417 SlapReply rs = {REP_RESULT};
1418 CachedQuery* query, *query_prev;
1419 int i, return_val, pause = 1;
1420 QueryTemplate* templ;
1422 connection_fake_init( &conn, &op, ctx );
1425 op.o_dn = cm->db.be_rootdn;
1426 op.o_ndn = cm->db.be_rootndn;
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",
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",
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",
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 ="
1461 cm->cur_entries, 0, 0 );
1463 query = query->prev;
1464 free_query(query_prev);
1466 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
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 );
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 );
1476 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
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 );
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;
1507 if ( strncasecmp( argv[0], "proxycache-", STRLENOF( "proxycache-" ) ) == 0 ) {
1508 save_argv0 = argv[0];
1509 argv[0] += STRLENOF( "proxycache-" );
1512 if ( strcasecmp( argv[0], "proxycache" ) == 0 ) {
1514 fprintf( stderr, "%s: line %d: missing arguments in \"proxycache"
1515 " <backend> <max_entries> <numattrsets> <entry limit> "
1516 "<cycle_time>\"\n", fname, lineno );
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] );
1526 if ( cm->db.bd_info->bi_db_init( &cm->db ) ) return( 1 );
1528 /* This type is in use, needs to be opened */
1529 cm->db.bd_info->bi_nDB++;
1531 cm->max_entries = atoi( argv[2] );
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 );
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;
1551 } else if ( strcasecmp( argv[0], "proxyattrset" ) == 0 ) {
1553 fprintf( stderr, "%s: line %d: missing arguments in \"proxyattrset "
1554 "<index> <attributes>\"\n", fname, lineno );
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",
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",
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 );
1579 attr_name->an_name.bv_val = NULL;
1580 attr_name->an_name.bv_len = 0;
1583 } else if ( strcasecmp( argv[0], "proxytemplate" ) == 0 ) {
1585 fprintf( stderr, "%s: line %d: missing argument(s) in "
1586 "\"proxytemplate <filter> <proj attr set> <TTL>\" line\n",
1590 if (( i = atoi( argv[2] )) >= cm->numattrsets ) {
1591 Debug( LDAP_DEBUG_ANY,
1592 "%s: line %d, template index invalid\n",
1596 num = cm->numtemplates;
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 );
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 );
1624 temp->querystr.bv_val = NULL;
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. */
1634 Debug( LDAP_DEBUG_ANY,
1635 "missing specifier for \"response-callback {head(default)|tail}\" "
1636 "callback position\n", 0, 0, 0 );
1640 if ( strcasecmp( argv[1], "head" ) == 0 ) {
1641 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
1643 } else if ( strcasecmp( argv[1], "tail" ) == 0 ) {
1644 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1647 Debug( LDAP_DEBUG_ANY,
1648 "unknown specifier %s for \"response-callback {head(default)|tail}\" "
1649 "callback position\n", argv[1], 0, 0 );
1655 rc = cm->db.bd_info->bi_db_config( &cm->db, fname, lineno, argc, argv );
1659 argv[0] = save_argv0;
1670 slap_overinst *on = (slap_overinst *)be->bd_info;
1674 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1675 on->on_bi.bi_private = cm;
1677 qm = (query_manager*)ch_malloc(sizeof(query_manager));
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;
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;
1695 qm->attr_sets = NULL;
1696 qm->templates = NULL;
1698 qm->lru_bottom = NULL;
1700 qm->qcfunc = query_containment;
1701 qm->crfunc = cache_replacement;
1702 qm->addfunc = add_query;
1703 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1705 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1706 ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1715 slap_overinst *on = (slap_overinst *)be->bd_info;
1716 cache_manager *cm = on->on_bi.bi_private;
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 );
1730 rc = backend_startup_one( &cm->db );
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 );
1739 /* Cached database must have the rootdn */
1740 if ( BER_BVISNULL( &cm->db.be_rootndn )
1741 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
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 );
1761 slap_overinst *on = (slap_overinst *)be->bd_info;
1762 cache_manager *cm = on->on_bi.bi_private;
1763 query_manager *qm = cm->qm;
1766 if ( cm->db.bd_info->bi_db_close ) {
1767 rc = cm->db.bd_info->bi_db_close( &cm->db );
1769 for ( i=0; i<cm->numtemplates; i++ ) {
1770 CachedQuery *qc, *qn;
1771 for ( qc = qm->templates[i].query; qc; qc = qn ) {
1775 free( qm->templates[i].querystr.bv_val );
1776 ldap_pvt_thread_rdwr_destroy( &qm->templates[i].t_rwlock );
1778 free( qm->templates );
1779 qm->templates = NULL;
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 );
1786 free( qm->attr_sets[i].attrs );
1788 free( qm->attr_sets );
1789 qm->attr_sets = NULL;
1795 proxy_cache_destroy(
1799 slap_overinst *on = (slap_overinst *)be->bd_info;
1800 cache_manager *cm = on->on_bi.bi_private;
1801 query_manager *qm = cm->qm;
1804 if ( cm->db.bd_info->bi_db_destroy ) {
1805 rc = cm->db.bd_info->bi_db_destroy( &cm->db );
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);
1816 find_supersets ( struct attr_set* attr_sets, int numsets )
1818 int num[MAX_ATTR_SETS];
1821 for ( i = 0; i < MAX_ATTR_SETS; i++ )
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;
1829 for ( i = 0; i < numsets; i++ ) {
1830 for ( j=i+1; j < numsets; j++ ) {
1831 res = compare_sets( attr_sets, i, j );
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;
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;
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
1867 compare_sets(struct attr_set* set, int i, int j)
1873 if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
1876 if ( set[i].attrs == NULL )
1879 if ( set[j].attrs == NULL )
1882 numI = set[i].count;
1883 numJ = set[j].count;
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 )
1893 if ( common == numI )
1896 if ( common == numJ )
1902 static slap_overinst proxy_cache;
1906 LDAPAttributeType *at;
1910 at = ldap_str2attributetype( queryid_schema, &code, &err,
1911 LDAP_SCHEMA_ALLOW_ALL );
1913 fprintf( stderr, "AttributeType Load failed %s %s\n",
1914 ldap_scherr2str(code), err );
1917 code = at_add( at, &err );
1919 slap_str2ad( at->at_names[0], &ad_queryid, &err );
1923 fprintf( stderr, "AttributeType Load failed %s %s\n",
1924 scherr2str(code), err );
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;
1936 return overlay_register( &proxy_cache );
1939 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
1940 int init_module(int argc, char *argv[]) {
1941 return pcache_init();
1945 #endif /* defined(SLAPD_OVER_PROXYCACHE) */