2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2005 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>
35 /* query cache structs */
38 typedef struct Query_s {
39 Filter* filter; /* Search Filter */
40 AttributeName* attrs; /* Projected attributes */
41 AttributeName* save_attrs; /* original attributes, saved for response */
42 struct berval base; /* Search Base */
43 int scope; /* Search scope */
46 /* struct representing a cached query */
47 typedef struct cached_query_s {
48 Query query; /* LDAP query */
49 struct berval q_uuid; /* query identifier */
50 int template_id; /* template of the query */
51 time_t expiry_time; /* time till the query is considered valid */
52 struct cached_query_s *next; /* next query in the template */
53 struct cached_query_s *prev; /* previous query in the template */
54 struct cached_query_s *lru_up; /* previous query in the LRU list */
55 struct cached_query_s *lru_down; /* next query in the LRU list */
58 /* struct representing a query template
59 * e.g. template string = &(cn=)(mail=)
61 typedef struct query_template_s {
62 struct berval querystr; /* Filter string corresponding to the QT */
63 int attr_set_index; /* determines the projected attributes */
65 CachedQuery* query; /* most recent query cached for the template */
66 CachedQuery* query_last; /* oldest query cached for the template */
68 int no_of_queries; /* Total number of queries in the template */
69 long ttl; /* TTL for the queries of this template */
70 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
74 * Represents a set of projected attributes and any
75 * supersets among all specified sets of attributes.
79 AttributeName* attrs; /* specifies the set */
80 int count; /* number of attributes */
81 int* ID_array; /* array of indices of supersets of 'attrs' */
84 struct query_manager_s;
86 /* prototypes for functions for 1) query containment
87 * 2) query addition, 3) cache replacement
89 typedef int (QCfunc)(struct query_manager_s*, Query*, int );
90 typedef void (AddQueryfunc)(struct query_manager_s*, Query*, int, struct berval*);
91 typedef void (CRfunc)(struct query_manager_s*, struct berval * );
93 /* LDAP query cache */
94 typedef struct query_manager_s {
95 struct attr_set* attr_sets; /* possible sets of projected attributes */
96 QueryTemplate* templates; /* cacheable templates */
98 CachedQuery* lru_top; /* top and bottom of LRU list */
99 CachedQuery* lru_bottom;
101 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
103 /* Query cache methods */
104 QCfunc *qcfunc; /* Query containment*/
105 CRfunc *crfunc; /* cache replacement */
106 AddQueryfunc *addfunc; /* add query */
109 /* LDAP query cache manager */
110 typedef struct cache_manager_s {
111 BackendDB db; /* underlying database */
112 unsigned long num_cached_queries; /* total number of cached queries */
113 unsigned long max_queries; /* upper bound on # of cached queries */
114 int numattrsets; /* number of attribute sets */
115 int numtemplates; /* number of cacheable templates */
116 int cur_entries; /* current number of entries cached */
117 int max_entries; /* max number of entries cached */
118 int num_entries_limit; /* max # of entries in a cacheable query */
120 char response_cb; /* install the response callback
121 * at the tail of the callback list */
122 #define PCACHE_RESPONSE_CB_HEAD 0
123 #define PCACHE_RESPONSE_CB_TAIL 1
125 int cc_period; /* interval between successive consistency checks (sec) */
129 ldap_pvt_thread_mutex_t cache_mutex;
130 ldap_pvt_thread_mutex_t remove_mutex;
132 query_manager* qm; /* query cache managed by the cache manager */
135 static AttributeDescription *ad_queryid;
136 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
137 "DESC 'list of queries the entry belongs to' "
138 "EQUALITY octetStringMatch "
139 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
140 "NO-USER-MODIFICATION USAGE directoryOperation )";
142 /* Return 1 for an added entry, else 0 */
147 struct berval* query_uuid )
150 Modifications* modlist = NULL;
151 const char* text = NULL;
153 char textbuf[SLAP_TEXT_BUFLEN];
154 size_t textlen = sizeof(textbuf);
156 SlapReply sreply = {REP_RESULT};
158 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
163 /* add queryid attribute */
164 attr_merge_one( e, ad_queryid, query_uuid, NULL );
166 /* append the attribute list from the fetched entry */
167 e->e_attrs->a_next = attr;
169 op->o_tag = LDAP_REQ_ADD;
170 op->o_protocol = LDAP_VERSION3;
171 op->o_callback = &cb;
172 op->o_time = slap_get_time();
173 op->o_do_not_cache = 1;
176 op->o_req_dn = e->e_name;
177 op->o_req_ndn = e->e_nname;
178 rc = op->o_bd->be_add( op, &sreply );
180 if ( rc != LDAP_SUCCESS ) {
181 if ( rc == LDAP_ALREADY_EXISTS ) {
182 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
183 modlist->sml_op = LDAP_MOD_ADD;
184 op->o_tag = LDAP_REQ_MODIFY;
185 op->orm_modlist = modlist;
186 op->o_bd->be_modify( op, &sreply );
187 slap_mods_free( modlist );
188 } else if ( rc == LDAP_REFERRAL ||
189 rc == LDAP_NO_SUCH_OBJECT ) {
190 syncrepl_add_glue( op, e );
199 be_entry_release_w( op, e );
206 /* compare base and scope of incoming and cached queries */
207 static int base_scope_compare(
208 struct berval* ndn_stored,
209 struct berval* ndn_incoming,
213 struct berval pdn_incoming = BER_BVNULL;
215 if (scope_stored < scope_incoming)
218 if ( !dnIsSuffix(ndn_incoming, ndn_stored))
221 switch(scope_stored) {
222 case LDAP_SCOPE_BASE:
223 return (ndn_incoming->bv_len == ndn_stored->bv_len);
225 case LDAP_SCOPE_ONELEVEL:
226 switch(scope_incoming){
227 case LDAP_SCOPE_BASE:
228 dnParent(ndn_incoming, &pdn_incoming);
229 return (pdn_incoming.bv_len == ndn_stored->bv_len);
231 case LDAP_SCOPE_ONELEVEL:
232 return (ndn_incoming->bv_len == ndn_stored->bv_len);
237 case LDAP_SCOPE_SUBTREE:
246 /* add query on top of LRU list */
248 add_query_on_top (query_manager* qm, CachedQuery* qc)
250 CachedQuery* top = qm->lru_top;
251 Query* q = (Query*)qc;
262 Debug( LDAP_DEBUG_ANY, "Base of added query = %s\n",
263 q->base.bv_val, 0, 0 );
266 /* remove_query from LRU list */
269 remove_query (query_manager* qm, CachedQuery* qc)
292 qc->lru_up = qc->lru_down = NULL;
296 invert_string( struct berval *bv )
301 for (i=0; i<bv->bv_len/2; i++) {
303 bv->bv_val[i] = bv->bv_val[bv->bv_len-i-1];
304 bv->bv_val[bv->bv_len-i-1] = c;
308 /* find and remove string2 from string1
309 * from start if position = 1,
310 * from end if position = 3,
311 * from anywhere if position = 2
315 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
322 char* string1=ber1->bv_val;
323 char* string2=ber2->bv_val;
338 temp = strstr(arg1, arg2);
342 if ( temp && (position == 2 || temp == arg1) ) {
344 strcpy( arg1, string1 );
348 if ( position == 3 ) {
356 static struct berval*
357 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
359 struct berval* merged, *temp;
360 int i, any_count, count;
362 for (any_count=0; any && any[any_count].bv_val; any_count++)
372 merged = (struct berval*)(ch_malloc((count+1)*sizeof(struct berval)));
379 for (i=0; i<any_count; i++) {
392 strings_containment(struct berval* stored, struct berval* incoming)
394 struct berval* element;
398 for ( element=stored; element->bv_val != NULL; element++ ) {
399 for (j = k; incoming[j].bv_val != NULL; j++) {
400 if (find_and_remove(&(incoming[j]), element, 2)) {
417 substr_containment_substr(Filter* stored, Filter* incoming)
423 struct berval init_incoming;
424 struct berval final_incoming;
425 struct berval *any_incoming = NULL;
426 struct berval *remaining_incoming = NULL;
428 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
429 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
433 ber_dupbv(&init_incoming, &(incoming->f_sub_initial));
434 ber_dupbv(&final_incoming, &(incoming->f_sub_final));
436 if (incoming->f_sub_any) {
437 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
441 any_incoming = (struct berval*)ch_malloc((any_count+1) *
442 sizeof(struct berval));
444 for (i=0; i<any_count; i++) {
445 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
447 any_incoming[any_count].bv_val = NULL;
448 any_incoming[any_count].bv_len = 0;
451 if (find_and_remove(&init_incoming,
452 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
453 &(stored->f_sub_final), 3))
455 if (stored->f_sub_any == NULL) {
459 remaining_incoming = merge_init_final(&init_incoming,
460 any_incoming, &final_incoming);
461 rc = strings_containment(stored->f_sub_any, remaining_incoming);
464 free(init_incoming.bv_val);
465 free(final_incoming.bv_val);
466 if (any_incoming) ber_bvarray_free( any_incoming );
467 free(remaining_incoming);
473 substr_containment_equality(Filter* stored, Filter* incoming)
475 struct berval incoming_val[2];
478 ber_dupbv(incoming_val, &(incoming->f_av_value));
479 incoming_val[1].bv_val = NULL;
480 incoming_val[1].bv_len = 0;
482 if (find_and_remove(incoming_val,
483 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
484 &(stored->f_sub_final), 3)) {
485 if (stored->f_sub_any == NULL){
489 rc = strings_containment(stored->f_sub_any, incoming_val);
492 free(incoming_val[0].bv_val);
496 /* check whether query is contained in any of
497 * the cached queries in template template_index
500 query_containment(query_manager *qm,
504 QueryTemplate* templa= qm->templates;
507 Filter* inputf = query->filter;
508 struct berval* base = &(query->base);
509 int scope = query->scope;
516 MatchingRule* mrule = NULL;
517 if (inputf != NULL) {
518 Debug( LDAP_DEBUG_ANY, "Lock QC index = %d\n",
519 template_index, 0, 0 );
520 ldap_pvt_thread_rdwr_rlock(&(templa[template_index].t_rwlock));
521 for(qc=templa[template_index].query; qc != NULL; qc= qc->next) {
523 if(base_scope_compare(&(q->base), base, q->scope, scope)) {
528 switch (fs->f_choice) {
529 case LDAP_FILTER_EQUALITY:
530 if (fi->f_choice == LDAP_FILTER_EQUALITY)
531 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
537 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
543 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
544 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
545 &(fi->f_ava->aa_value),
546 &(fs->f_ava->aa_value), &text);
547 if (rc != LDAP_SUCCESS) {
548 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
549 Debug( LDAP_DEBUG_ANY,
550 "Unlock: Exiting QC index=%d\n",
551 template_index, 0, 0 );
555 switch (fs->f_choice) {
557 case LDAP_FILTER_AND:
562 case LDAP_FILTER_SUBSTRINGS:
563 /* check if the equality query can be
564 * answered with cached substring query */
565 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
566 && substr_containment_equality(
569 /* check if the substring query can be
570 * answered with cached substring query */
571 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
572 ) && substr_containment_substr(
578 case LDAP_FILTER_PRESENT:
583 case LDAP_FILTER_EQUALITY:
601 case LDAP_FILTER_NOT:
607 } while((res) && (fi != NULL) && (fs != NULL));
610 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
611 if (qm->lru_top != qc) {
612 remove_query(qm, qc);
613 add_query_on_top(qm, qc);
615 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
620 Debug( LDAP_DEBUG_ANY,
621 "Not answerable: Unlock QC index=%d\n",
622 template_index, 0, 0 );
623 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
629 free_query (CachedQuery* qc)
631 Query* q = (Query*)qc;
634 free(qc->q_uuid.bv_val);
635 filter_free(q->filter);
636 free (q->base.bv_val);
637 for (i=0; q->attrs[i].an_name.bv_val; i++) {
638 free(q->attrs[i].an_name.bv_val);
645 /* Add query to query cache */
646 static void add_query(
652 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
653 QueryTemplate* templ = (qm->templates)+template_index;
655 new_cached_query->template_id = template_index;
656 new_cached_query->q_uuid = *uuid;
657 new_cached_query->lru_up = NULL;
658 new_cached_query->lru_down = NULL;
659 new_cached_query->expiry_time = slap_get_time() + templ->ttl;
660 Debug( LDAP_DEBUG_ANY, "Added query expires at %ld\n",
661 (long) new_cached_query->expiry_time, 0, 0 );
662 new_query = (Query*)new_cached_query;
664 ber_dupbv(&new_query->base, &query->base);
665 new_query->scope = query->scope;
666 new_query->filter = query->filter;
667 new_query->attrs = query->attrs;
670 Debug( LDAP_DEBUG_ANY, "Lock AQ index = %d\n",
671 template_index, 0, 0 );
672 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
673 if (templ->query == NULL)
674 templ->query_last = new_cached_query;
676 templ->query->prev = new_cached_query;
677 new_cached_query->next = templ->query;
678 new_cached_query->prev = NULL;
679 templ->query = new_cached_query;
680 templ->no_of_queries++;
681 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES++ %d\n",
682 template_index, templ->no_of_queries, 0 );
684 Debug( LDAP_DEBUG_ANY, "Unlock AQ index = %d \n",
685 template_index, 0, 0 );
686 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
688 /* Adding on top of LRU list */
689 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
690 add_query_on_top(qm, new_cached_query);
691 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
695 remove_from_template (CachedQuery* qc, QueryTemplate* template)
697 if (!qc->prev && !qc->next) {
698 template->query_last = template->query = NULL;
699 } else if (qc->prev == NULL) {
700 qc->next->prev = NULL;
701 template->query = qc->next;
702 } else if (qc->next == NULL) {
703 qc->prev->next = NULL;
704 template->query_last = qc->prev;
706 qc->next->prev = qc->prev;
707 qc->prev->next = qc->next;
710 template->no_of_queries--;
713 /* remove bottom query of LRU list from the query cache */
714 static void cache_replacement(query_manager* qm, struct berval *result)
719 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
720 bottom = qm->lru_bottom;
722 result->bv_val = NULL;
726 Debug ( LDAP_DEBUG_ANY,
727 "Cache replacement invoked without "
728 "any query in LRU list\n", 0, 0, 0 );
732 temp_id = bottom->template_id;
733 remove_query(qm, bottom);
734 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
736 *result = bottom->q_uuid;
737 bottom->q_uuid.bv_val = NULL;
739 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n", temp_id, 0, 0 );
740 ldap_pvt_thread_rdwr_wlock(&(qm->templates[temp_id].t_rwlock));
741 remove_from_template(bottom, (qm->templates+temp_id));
742 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
743 temp_id, qm->templates[temp_id].no_of_queries, 0 );
744 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n", temp_id, 0, 0 );
745 ldap_pvt_thread_rdwr_wunlock(&(qm->templates[temp_id].t_rwlock));
750 struct query_info *next;
762 struct query_info *qi;
765 if ( rs->sr_type != REP_SEARCH ) return 0;
767 for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
768 if (attr->a_desc == ad_queryid) {
769 for (count=0; attr->a_vals[count].bv_val; count++)
774 if ( count == 0 ) return 0;
775 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
776 qi->next = op->o_callback->sc_private;
777 op->o_callback->sc_private = qi;
778 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
779 qi->del = ( count == 1 );
788 struct berval* query_uuid)
790 struct query_info *qi, *qnext;
792 #ifdef LDAP_COMP_MATCH
793 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
795 AttributeAssertion ava = { NULL, BER_BVNULL };
797 Filter filter = {LDAP_FILTER_EQUALITY};
798 SlapReply sreply = {REP_RESULT};
799 slap_callback cb = { NULL, remove_func, NULL, NULL };
802 sreply.sr_entry = NULL;
803 sreply.sr_nentries = 0;
804 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
805 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
807 filter.f_av_desc = ad_queryid;
808 filter.f_av_value = *query_uuid;
810 op->o_tag = LDAP_REQ_SEARCH;
811 op->o_protocol = LDAP_VERSION3;
812 op->o_callback = &cb;
813 op->o_time = slap_get_time();
814 op->o_do_not_cache = 1;
816 op->o_req_dn = op->o_bd->be_suffix[0];
817 op->o_req_ndn = op->o_bd->be_nsuffix[0];
818 op->ors_scope = LDAP_SCOPE_SUBTREE;
819 op->ors_deref = LDAP_DEREF_NEVER;
820 op->ors_slimit = SLAP_NO_LIMIT;
821 op->ors_tlimit = SLAP_NO_LIMIT;
822 op->ors_filter = &filter;
823 op->ors_filterstr.bv_val = filter_str;
824 op->ors_filterstr.bv_len = strlen(filter_str);
825 op->ors_attrs = NULL;
826 op->ors_attrsonly = 0;
828 op->o_bd->be_search( op, &sreply );
830 for ( qi=cb.sc_private; qi; qi=qnext ) {
833 op->o_req_dn = qi->xdn;
834 op->o_req_ndn = qi->xdn;
837 Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
838 query_uuid->bv_val, 0, 0 );
840 op->o_tag = LDAP_REQ_DELETE;
842 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
847 struct berval vals[2];
849 vals[0] = *query_uuid;
850 vals[1].bv_val = NULL;
852 mod.sml_op = LDAP_MOD_DELETE;
853 mod.sml_desc = ad_queryid;
854 mod.sml_type = ad_queryid->ad_cname;
855 mod.sml_values = vals;
856 mod.sml_nvalues = NULL;
858 Debug( LDAP_DEBUG_ANY,
859 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
860 query_uuid->bv_val, 0, 0 );
862 op->orm_modlist = &mod;
864 op->o_bd->be_modify( op, &sreply );
866 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
867 op->o_tmpfree( qi, op->o_tmpmemctx );
874 AttributeName* attrs,
881 AttributeName* attrs_in,
888 struct berval* tempstr,
892 QueryTemplate *qt = qm->templates + template_id;
894 if (attr_set != qt->attr_set_index) {
895 int* id_array = qm->attr_sets[attr_set].ID_array;
897 while (*id_array != -1) {
898 if (*id_array == qt->attr_set_index)
905 return (qt->querystr.bv_len == tempstr->bv_len &&
906 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
913 AttributeName** filter_attrs,
916 AttributeDescription *ad;
918 switch ( f->f_choice ) {
919 case LDAP_FILTER_EQUALITY:
921 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
922 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
927 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
928 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
933 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
934 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
937 case LDAP_FILTER_APPROX:
939 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
940 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
943 case LDAP_FILTER_SUBSTRINGS:
945 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
946 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
949 case LDAP_FILTER_PRESENT:
951 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
952 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
955 case LDAP_FILTER_AND:
957 case LDAP_FILTER_NOT: {
959 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
960 f->f_choice == LDAP_FILTER_AND ? '&' :
961 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
962 fstr->bv_len += sizeof("(%") - 1;
964 for ( f = f->f_list; f != NULL; f = f->f_next ) {
965 rc = filter2template( f, fstr, filter_attrs, filter_cnt );
968 sprintf( fstr->bv_val+fstr->bv_len, ")" );
969 fstr->bv_len += sizeof(")") - 1;
975 strcpy( fstr->bv_val, "(?=?)" );
976 fstr->bv_len += sizeof("(?=?)")-1;
980 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
981 (*filter_cnt + 2)*sizeof(AttributeName));
983 (*filter_attrs)[*filter_cnt].an_desc = ad;
984 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
985 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
986 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
1005 struct berval *query_uuid)
1007 struct search_info *si = op->o_callback->sc_private;
1008 slap_overinst *on = si->on;
1009 cache_manager *cm = on->on_bi.bi_private;
1010 query_manager* qm = cm->qm;
1014 struct berval crp_uuid;
1015 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1016 Operation op_tmp = *op;
1018 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1019 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1021 op_tmp.o_bd = &cm->db;
1022 op_tmp.o_dn = cm->db.be_rootdn;
1023 op_tmp.o_ndn = cm->db.be_rootndn;
1025 Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1028 for ( e=si->head; e; e=si->head ) {
1029 si->head = e->e_private;
1030 e->e_private = NULL;
1031 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1032 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1033 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1034 while ( cm->cur_entries > (cm->max_entries) ) {
1035 qm->crfunc(qm, &crp_uuid);
1036 if (crp_uuid.bv_val) {
1037 Debug( LDAP_DEBUG_ANY,
1038 "Removing query UUID %s\n",
1039 crp_uuid.bv_val, 0, 0 );
1040 return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
1041 Debug( LDAP_DEBUG_ANY,
1042 "QUERY REMOVED, SIZE=%d\n",
1044 ldap_pvt_thread_mutex_lock(
1046 cm->cur_entries -= return_val;
1047 cm->num_cached_queries--;
1048 Debug( LDAP_DEBUG_ANY,
1049 "STORED QUERIES = %lu\n",
1050 cm->num_cached_queries, 0, 0 );
1051 ldap_pvt_thread_mutex_unlock(
1053 Debug( LDAP_DEBUG_ANY,
1054 "QUERY REMOVED, CACHE ="
1056 cm->cur_entries, 0, 0 );
1060 return_val = merge_entry(&op_tmp, e, query_uuid);
1061 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1062 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1063 cm->cur_entries += return_val;
1064 Debug( LDAP_DEBUG_ANY,
1065 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1066 cm->cur_entries, 0, 0 );
1068 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1070 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1071 cm->num_cached_queries++;
1072 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1073 cm->num_cached_queries, 0, 0 );
1074 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1080 proxy_cache_response(
1084 struct search_info *si = op->o_callback->sc_private;
1085 slap_overinst *on = si->on;
1086 cache_manager *cm = on->on_bi.bi_private;
1087 query_manager* qm = cm->qm;
1090 if ( rs->sr_type == REP_SEARCH ) {
1092 /* If we haven't exceeded the limit for this query,
1093 * build a chain of answers to store. If we hit the
1094 * limit, empty the chain and ignore the rest.
1097 if ( si->count < si->max ) {
1099 e = entry_dup( rs->sr_entry );
1100 if ( !si->head ) si->head = e;
1101 if ( si->tail ) si->tail->e_private = e;
1106 for (;si->head; si->head=e) {
1107 e = si->head->e_private;
1108 si->head->e_private = NULL;
1109 entry_free(si->head);
1115 if (rs->sr_attrs != op->ors_attrs ) {
1116 op->o_tmpfree( rs->sr_attrs, op->o_tmpmemctx );
1118 rs->sr_attrs = si->query.save_attrs;
1119 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
1120 op->ors_attrs = si->query.save_attrs;
1121 si->query.save_attrs = NULL;
1123 } else if ( rs->sr_type == REP_RESULT ) {
1124 if ( si->count && cache_entries( op, rs, &uuid ) == 0 ) {
1125 qm->addfunc(qm, &si->query, si->template_id, &uuid);
1126 /* If the consistency checker suspended itself,
1129 if ( cm->cc_paused ) {
1130 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1131 if ( cm->cc_paused ) {
1133 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
1135 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1140 op->o_callback->sc_cleanup = slap_freeself_cb;
1142 return SLAP_CB_CONTINUE;
1148 AttributeName** new_attrs,
1149 AttributeName* attrs,
1150 AttributeName* filter_attrs )
1157 /* duplicate attrs */
1158 if (attrs == NULL) {
1161 for (count=0; attrs[count].an_name.bv_val; count++)
1164 *new_attrs = (AttributeName*)(op->o_tmpalloc((count+1)*
1165 sizeof(AttributeName), op->o_tmpmemctx));
1166 if (attrs == NULL) {
1167 (*new_attrs)[0].an_name.bv_val = "*";
1168 (*new_attrs)[0].an_name.bv_len = 1;
1169 (*new_attrs)[1].an_name.bv_val = NULL;
1170 (*new_attrs)[1].an_name.bv_len = 0;
1174 for (i=0; i<count; i++) {
1175 (*new_attrs)[i].an_name = attrs[i].an_name;
1176 (*new_attrs)[i].an_desc = attrs[i].an_desc;
1178 (*new_attrs)[count].an_name.bv_val = NULL;
1179 (*new_attrs)[count].an_name.bv_len = 0;
1180 alluser = an_find(*new_attrs, &AllUser);
1181 allop = an_find(*new_attrs, &AllOper);
1184 for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1185 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1187 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1192 *new_attrs = (AttributeName*)(op->o_tmprealloc(*new_attrs,
1193 (count+2)*sizeof(AttributeName), op->o_tmpmemctx));
1194 (*new_attrs)[count].an_name = filter_attrs[i].an_name;
1195 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc;
1197 (*new_attrs)[count].an_name.bv_val = NULL;
1198 (*new_attrs)[count].an_name.bv_len = 0;
1207 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1208 cache_manager *cm = on->on_bi.bi_private;
1209 query_manager* qm = cm->qm;
1215 AttributeName *filter_attrs = NULL;
1216 AttributeName *new_attrs = NULL;
1221 int template_id = -1;
1225 int oc_attr_absent = 1;
1227 struct berval tempstr;
1229 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1231 if (filter2template(op->ors_filter, &tempstr, &filter_attrs, &fattr_cnt)) {
1232 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1233 return SLAP_CB_CONTINUE;
1236 Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
1237 tempstr.bv_val, 0, 0 );
1240 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1242 query.filter = op->ors_filter;
1243 query.attrs = op->ors_attrs;
1244 query.save_attrs = NULL;
1245 query.base = op->o_req_ndn;
1246 query.scope = op->ors_scope;
1248 /* check for query containment */
1249 if (attr_set > -1) {
1250 for (i=0; i<cm->numtemplates; i++) {
1251 /* find if template i can potentially answer tempstr */
1252 if (!is_temp_answerable(attr_set, &tempstr, qm, i))
1254 if (attr_set == qm->templates[i].attr_set_index) {
1258 Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1259 op->ors_filterstr.bv_val, 0, 0 );
1260 answerable = (*(qm->qcfunc))(qm, &query, i);
1266 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1268 query.save_attrs = op->ors_attrs;
1272 /* Need to clear the callbacks of the original operation,
1273 * in case there are other overlays */
1274 BackendDB *save_bd = op->o_bd;
1275 slap_callback *save_cb = op->o_callback;
1277 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
1279 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
1281 op->o_callback = NULL;
1282 i = cm->db.bd_info->bi_op_search( op, rs );
1284 op->o_callback = save_cb;
1288 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1290 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1291 if (cm->num_cached_queries >= cm->max_queries) {
1294 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1298 struct search_info *si;
1300 Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
1301 query.filter = str2filter(op->ors_filterstr.bv_val);
1302 if (op->ors_attrs) {
1303 for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1304 if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass ) {
1308 query.attrs = (AttributeName *)ch_malloc( ( count + 1 + oc_attr_absent )
1309 *sizeof(AttributeName) );
1310 for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1311 ber_dupbv( &query.attrs[count].an_name, &op->ors_attrs[count].an_name );
1312 query.attrs[count].an_desc = op->ors_attrs[count].an_desc;
1314 if ( oc_attr_absent ) {
1315 query.attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
1316 ber_dupbv( &query.attrs[count].an_name,
1317 &slap_schema.si_ad_objectClass->ad_cname );
1320 query.attrs[ count ].an_name.bv_val = NULL;
1321 query.attrs[ count ].an_name.bv_len = 0;
1323 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
1325 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1326 cb->sc_response = proxy_cache_response;
1327 cb->sc_cleanup = NULL;
1328 cb->sc_private = (cb+1);
1329 si = cb->sc_private;
1332 si->template_id = template_id;
1333 si->max = cm->num_entries_limit ;
1339 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1340 cb->sc_next = op->o_callback;
1341 op->o_callback = cb;
1344 slap_callback **pcb;
1346 /* need to move the callback at the end, in case other
1347 * overlays are present, so that the final entry is
1348 * actually cached */
1350 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
1355 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE\n",
1361 return SLAP_CB_CONTINUE;
1366 AttributeName* attrs_in,
1367 AttributeName* attrs)
1369 int i, count1, count2;
1370 if ( attrs_in == NULL ) {
1371 return (attrs ? 0 : 1);
1373 if ( attrs == NULL )
1377 attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1381 attrs && attrs[count2].an_name.bv_val != NULL;
1384 if ( count1 != count2 )
1387 for ( i=0; i<count1; i++ ) {
1388 if ( !an_find(attrs, &attrs_in[i].an_name ))
1396 AttributeName* attrs,
1401 for (i=0; i<num; i++) {
1402 if (attrscmp(attrs, qm->attr_sets[i].attrs))
1413 struct re_s *rtask = arg;
1414 slap_overinst *on = rtask->arg;
1415 cache_manager *cm = on->on_bi.bi_private;
1416 query_manager *qm = cm->qm;
1417 Connection conn = {0};
1418 char opbuf[OPERATION_BUFFER_SIZE];
1421 SlapReply rs = {REP_RESULT};
1422 CachedQuery* query, *query_prev;
1423 int i, return_val, pause = 1;
1424 QueryTemplate* templ;
1426 op = (Operation *)opbuf;
1427 connection_fake_init( &conn, op, ctx );
1430 op->o_dn = cm->db.be_rootdn;
1431 op->o_ndn = cm->db.be_rootndn;
1435 for (i=0; qm->templates[i].querystr.bv_val; i++) {
1436 templ = qm->templates + i;
1437 query = templ->query_last;
1438 if ( query ) pause = 0;
1439 op->o_time = slap_get_time();
1440 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1441 while (query && (query->expiry_time < op->o_time)) {
1442 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1443 remove_query(qm, query);
1444 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1445 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1447 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1448 remove_from_template(query, templ);
1449 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1450 i, templ->no_of_queries, 0 );
1451 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1453 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1454 return_val = remove_query_data(op, &rs, &query->q_uuid);
1455 Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1457 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1458 cm->cur_entries -= return_val;
1459 cm->num_cached_queries--;
1460 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1461 cm->num_cached_queries, 0, 0 );
1462 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1463 Debug( LDAP_DEBUG_ANY,
1464 "STALE QUERY REMOVED, CACHE ="
1466 cm->cur_entries, 0, 0 );
1468 query = query->prev;
1469 free_query(query_prev);
1471 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1473 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1474 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1475 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1477 /* If there were no queries, defer processing for a while */
1478 cm->cc_paused = pause;
1479 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
1481 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1486 #define MAX_ATTR_SETS 500
1487 static void find_supersets( struct attr_set* attr_sets, int numsets );
1488 static int compare_sets( struct attr_set* setA, int, int );
1499 slap_overinst *on = (slap_overinst *)be->bd_info;
1500 cache_manager* cm = on->on_bi.bi_private;
1501 query_manager* qm = cm->qm;
1502 QueryTemplate* temp;
1503 AttributeName* attr_name;
1504 AttributeName* attrarray;
1505 const char* text=NULL;
1512 if ( strncasecmp( argv[0], "proxycache-", STRLENOF( "proxycache-" ) ) == 0 ) {
1513 argv0 = argv[0] + STRLENOF( "proxycache-" );
1518 if ( strcasecmp( argv0, "proxycache" ) == 0 ) {
1520 fprintf( stderr, "%s: line %d: missing arguments in \"proxycache"
1521 " <backend> <max_entries> <numattrsets> <entry limit> "
1522 "<cycle_time>\"\n", fname, lineno );
1526 cm->db.bd_info = backend_info( argv[1] );
1527 if ( !cm->db.bd_info ) {
1528 fprintf( stderr, "%s: line %d: backend %s unknown\n",
1529 fname, lineno, argv[1] );
1532 if ( cm->db.bd_info->bi_db_init( &cm->db ) ) return( 1 );
1534 /* This type is in use, needs to be opened */
1535 cm->db.bd_info->bi_nDB++;
1537 cm->max_entries = atoi( argv[2] );
1539 cm->numattrsets = atoi( argv[3] );
1540 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1541 fprintf( stderr, "%s: line %d: numattrsets must be <= %d\n",
1542 fname, lineno, MAX_ATTR_SETS );
1546 cm->num_entries_limit = atoi( argv[4] );
1547 cm->cc_period = atoi( argv[5] );
1548 Debug( LDAP_DEBUG_ANY,
1549 "Total # of attribute sets to be cached = %d\n",
1550 cm->numattrsets, 0, 0 );
1551 qm->attr_sets = ( struct attr_set * )ch_malloc( cm->numattrsets *
1552 sizeof( struct attr_set ));
1553 for ( i = 0; i < cm->numattrsets; i++ ) {
1554 qm->attr_sets[i].attrs = NULL;
1557 } else if ( strcasecmp( argv0, "proxyattrset" ) == 0 ) {
1559 fprintf( stderr, "%s: line %d: missing arguments in \"proxyattrset "
1560 "<index> <attributes>\"\n", fname, lineno );
1563 Debug( LDAP_DEBUG_ANY, "Attribute Set # %d\n",
1564 atoi( argv[1] ), 0, 0 );
1565 if (atoi(argv[1]) >= cm->numattrsets) {
1566 fprintf( stderr, "%s; line %d index out of bounds \n",
1570 index = atoi( argv[1] );
1571 if ( argv[2] && strcmp( argv[2], "*" ) ) {
1572 qm->attr_sets[index].count = argc - 2;
1573 qm->attr_sets[index].attrs = (AttributeName*)ch_malloc(
1574 (argc-1) * sizeof( AttributeName ));
1575 attr_name = qm->attr_sets[index].attrs;
1576 for ( i = 2; i < argc; i++ ) {
1577 Debug( LDAP_DEBUG_ANY, "\t %s\n",
1579 ber_str2bv( argv[i], 0, 1,
1580 &attr_name->an_name);
1581 attr_name->an_desc = NULL;
1582 slap_bv2ad( &attr_name->an_name,
1583 &attr_name->an_desc, &text );
1585 attr_name->an_name.bv_val = NULL;
1586 attr_name->an_name.bv_len = 0;
1589 } else if ( strcasecmp( argv0, "proxytemplate" ) == 0 ) {
1591 fprintf( stderr, "%s: line %d: missing argument(s) in "
1592 "\"proxytemplate <filter> <proj attr set> <TTL>\" line\n",
1596 if (( i = atoi( argv[2] )) >= cm->numattrsets ) {
1597 Debug( LDAP_DEBUG_ANY,
1598 "%s: line %d, template index invalid\n",
1602 num = cm->numtemplates;
1604 find_supersets( qm->attr_sets, cm->numattrsets );
1605 qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
1606 ( num + 2 ) * sizeof( QueryTemplate ));
1607 temp = qm->templates + num;
1608 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
1609 temp->query = temp->query_last = NULL;
1610 temp->ttl = atoi( argv[3] );
1611 temp->no_of_queries = 0;
1612 if ( argv[1] == NULL ) {
1613 Debug( LDAP_DEBUG_ANY,
1614 "Templates string not specified "
1615 "for template %d\n", num, 0, 0 );
1618 ber_str2bv( argv[1], 0, 1, &temp->querystr );
1619 Debug( LDAP_DEBUG_ANY, "Template:\n", 0, 0, 0 );
1620 Debug( LDAP_DEBUG_ANY, " query template: %s\n",
1621 temp->querystr.bv_val, 0, 0 );
1622 temp->attr_set_index = i;
1623 Debug( LDAP_DEBUG_ANY, " attributes: \n", 0, 0, 0 );
1624 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1625 for ( i=0; attrarray[i].an_name.bv_val; i++ )
1626 Debug( LDAP_DEBUG_ANY, "\t%s\n",
1627 attrarray[i].an_name.bv_val, 0, 0 );
1630 temp->querystr.bv_val = NULL;
1633 } else if ( strcasecmp( argv0, "response-callback" ) == 0 ) {
1634 /* set to "tail" to put the response callback
1635 * at the end of the callback list; this is required
1636 * in case other overlays are present, so that the
1637 * final entry is cached. */
1640 Debug( LDAP_DEBUG_ANY,
1641 "missing specifier for \"response-callback {head(default)|tail}\" "
1642 "callback position\n", 0, 0, 0 );
1646 if ( strcasecmp( argv[1], "head" ) == 0 ) {
1647 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
1649 } else if ( strcasecmp( argv[1], "tail" ) == 0 ) {
1650 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1653 Debug( LDAP_DEBUG_ANY,
1654 "unknown specifier %s for \"response-callback {head(default)|tail}\" "
1655 "callback position\n", argv[1], 0, 0 );
1661 rc = cm->db.bd_info->bi_db_config( &cm->db, fname, lineno, argc, argv );
1672 slap_overinst *on = (slap_overinst *)be->bd_info;
1676 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1677 on->on_bi.bi_private = cm;
1679 qm = (query_manager*)ch_malloc(sizeof(query_manager));
1682 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1683 cm->db.be_private = NULL;
1684 cm->db.be_pcl_mutexp = &cm->db.be_pcl_mutex;
1686 cm->numattrsets = 0;
1687 cm->numtemplates = 0;
1688 cm->num_entries_limit = 5;
1689 cm->num_cached_queries = 0;
1690 cm->max_entries = 0;
1691 cm->cur_entries = 0;
1692 cm->max_queries = 10000;
1693 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1694 cm->cc_period = 1000;
1697 qm->attr_sets = NULL;
1698 qm->templates = NULL;
1700 qm->lru_bottom = NULL;
1702 qm->qcfunc = query_containment;
1703 qm->crfunc = cache_replacement;
1704 qm->addfunc = add_query;
1705 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1707 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1708 ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1717 slap_overinst *on = (slap_overinst *)be->bd_info;
1718 cache_manager *cm = on->on_bi.bi_private;
1722 /* consistency check (add more...) */
1723 for ( i = 0; i < cm->numattrsets; i++ ) {
1724 if ( cm->qm->attr_sets[i].attrs == NULL ) {
1725 fprintf( stderr, "proxy_cache_open(): "
1726 "attr set %d (of %d) missing\n",
1727 i, cm->numattrsets );
1732 /* need to inherit something from the original database... */
1733 cm->db.be_def_limit = be->be_def_limit;
1734 cm->db.be_limits = be->be_limits;
1735 cm->db.be_acl = be->be_acl;
1736 cm->db.be_dfltaccess = be->be_dfltaccess;
1738 rc = backend_startup_one( &cm->db );
1740 /* There is no runqueue in TOOL mode */
1741 if ( slapMode & SLAP_SERVER_MODE ) {
1742 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1743 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
1744 consistency_check, on,
1745 "pcache_consistency", be->be_suffix[0].bv_val );
1746 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1748 /* Cached database must have the rootdn */
1749 if ( BER_BVISNULL( &cm->db.be_rootndn )
1750 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
1752 fprintf( stderr, "proxy_cache_open(): "
1753 "underlying database of type \"%s\"\n"
1754 " serving naming context \"%s\"\n"
1755 " has no \"rootdn\", required by \"proxycache\".\n",
1756 on->on_info->oi_orig->bi_type,
1757 cm->db.be_suffix[0].bv_val );
1770 slap_overinst *on = (slap_overinst *)be->bd_info;
1771 cache_manager *cm = on->on_bi.bi_private;
1772 query_manager *qm = cm->qm;
1775 /* cleanup stuff inherited from the original database... */
1776 cm->db.be_limits = NULL;
1777 cm->db.be_acl = NULL;
1779 if ( cm->db.bd_info->bi_db_close ) {
1780 rc = cm->db.bd_info->bi_db_close( &cm->db );
1782 for ( i=0; i<cm->numtemplates; i++ ) {
1783 CachedQuery *qc, *qn;
1784 for ( qc = qm->templates[i].query; qc; qc = qn ) {
1788 free( qm->templates[i].querystr.bv_val );
1789 ldap_pvt_thread_rdwr_destroy( &qm->templates[i].t_rwlock );
1791 free( qm->templates );
1792 qm->templates = NULL;
1794 for ( i=0; i<cm->numattrsets; i++ ) {
1795 free( qm->attr_sets[i].ID_array );
1796 for ( j=0; j<qm->attr_sets[i].count; j++ ) {
1797 free( qm->attr_sets[i].attrs[j].an_name.bv_val );
1799 free( qm->attr_sets[i].attrs );
1801 free( qm->attr_sets );
1802 qm->attr_sets = NULL;
1808 proxy_cache_destroy(
1812 slap_overinst *on = (slap_overinst *)be->bd_info;
1813 cache_manager *cm = on->on_bi.bi_private;
1814 query_manager *qm = cm->qm;
1817 if ( cm->db.bd_info->bi_db_destroy ) {
1818 rc = cm->db.bd_info->bi_db_destroy( &cm->db );
1820 ldap_pvt_thread_mutex_destroy(&qm->lru_mutex);
1821 ldap_pvt_thread_mutex_destroy(&cm->cache_mutex);
1822 ldap_pvt_thread_mutex_destroy(&cm->remove_mutex);
1829 find_supersets ( struct attr_set* attr_sets, int numsets )
1831 int num[MAX_ATTR_SETS];
1834 for ( i = 0; i < MAX_ATTR_SETS; i++ )
1837 for ( i = 0; i < numsets; i++ ) {
1838 attr_sets[i].ID_array = (int*) ch_malloc( sizeof( int ) );
1839 attr_sets[i].ID_array[0] = -1;
1842 for ( i = 0; i < numsets; i++ ) {
1843 for ( j=i+1; j < numsets; j++ ) {
1844 res = compare_sets( attr_sets, i, j );
1850 id_array = attr_sets[i].ID_array;
1851 attr_sets[i].ID_array = (int *) ch_realloc( id_array,
1852 ( num[i] + 2 ) * sizeof( int ));
1853 attr_sets[i].ID_array[num[i]] = j;
1854 attr_sets[i].ID_array[num[i]+1] = -1;
1859 id_array = attr_sets[j].ID_array;
1860 attr_sets[j].ID_array = (int *) ch_realloc( id_array,
1861 ( num[j] + 2 ) * sizeof( int ));
1862 attr_sets[j].ID_array[num[j]] = i;
1863 attr_sets[j].ID_array[num[j]+1] = -1;
1872 * compares two sets of attributes (indices i and j)
1873 * returns 0: if neither set is contained in the other set
1874 * 1: if set i is contained in set j
1875 * 2: if set j is contained in set i
1876 * 3: the sets are equivalent
1880 compare_sets(struct attr_set* set, int i, int j)
1886 if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
1889 if ( set[i].attrs == NULL )
1892 if ( set[j].attrs == NULL )
1895 numI = set[i].count;
1896 numJ = set[j].count;
1898 for ( l=0; l < numI; l++ ) {
1899 for ( k = 0; k < numJ; k++ ) {
1900 if ( strcmp( set[i].attrs[l].an_name.bv_val,
1901 set[j].attrs[k].an_name.bv_val ) == 0 )
1906 if ( common == numI )
1909 if ( common == numJ )
1915 static slap_overinst proxy_cache;
1919 LDAPAttributeType *at;
1923 at = ldap_str2attributetype( queryid_schema, &code, &err,
1924 LDAP_SCHEMA_ALLOW_ALL );
1926 fprintf( stderr, "AttributeType Load failed %s %s\n",
1927 ldap_scherr2str(code), err );
1930 code = at_add( at, 0, NULL, &err );
1932 slap_str2ad( at->at_names[0], &ad_queryid, &err );
1936 fprintf( stderr, "AttributeType Load failed %s %s\n",
1937 scherr2str(code), err );
1941 proxy_cache.on_bi.bi_type = "proxycache";
1942 proxy_cache.on_bi.bi_db_init = proxy_cache_init;
1943 proxy_cache.on_bi.bi_db_config = proxy_cache_config;
1944 proxy_cache.on_bi.bi_db_open = proxy_cache_open;
1945 proxy_cache.on_bi.bi_db_close = proxy_cache_close;
1946 proxy_cache.on_bi.bi_db_destroy = proxy_cache_destroy;
1947 proxy_cache.on_bi.bi_op_search = proxy_cache_search;
1949 return overlay_register( &proxy_cache );
1952 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
1953 int init_module(int argc, char *argv[]) {
1954 return pcache_init();
1958 #endif /* defined(SLAPD_OVER_PROXYCACHE) */