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>
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 AttributeAssertion ava;
793 Filter filter = {LDAP_FILTER_EQUALITY};
794 SlapReply sreply = {REP_RESULT};
795 slap_callback cb = { NULL, remove_func, NULL, NULL };
798 sreply.sr_entry = NULL;
799 sreply.sr_nentries = 0;
800 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
801 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
803 filter.f_av_desc = ad_queryid;
804 filter.f_av_value = *query_uuid;
806 op->o_tag = LDAP_REQ_SEARCH;
807 op->o_protocol = LDAP_VERSION3;
808 op->o_callback = &cb;
809 op->o_time = slap_get_time();
810 op->o_do_not_cache = 1;
812 op->o_req_dn = op->o_bd->be_suffix[0];
813 op->o_req_ndn = op->o_bd->be_nsuffix[0];
814 op->ors_scope = LDAP_SCOPE_SUBTREE;
815 op->ors_deref = LDAP_DEREF_NEVER;
816 op->ors_slimit = SLAP_NO_LIMIT;
817 op->ors_tlimit = SLAP_NO_LIMIT;
818 op->ors_filter = &filter;
819 op->ors_filterstr.bv_val = filter_str;
820 op->ors_filterstr.bv_len = strlen(filter_str);
821 op->ors_attrs = NULL;
822 op->ors_attrsonly = 0;
824 op->o_bd->be_search( op, &sreply );
826 for ( qi=cb.sc_private; qi; qi=qnext ) {
829 op->o_req_dn = qi->xdn;
830 op->o_req_ndn = qi->xdn;
833 Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
834 query_uuid->bv_val, 0, 0 );
836 op->o_tag = LDAP_REQ_DELETE;
838 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
843 struct berval vals[2];
845 vals[0] = *query_uuid;
846 vals[1].bv_val = NULL;
848 mod.sml_op = LDAP_MOD_DELETE;
849 mod.sml_desc = ad_queryid;
850 mod.sml_type = ad_queryid->ad_cname;
851 mod.sml_values = vals;
852 mod.sml_nvalues = NULL;
854 Debug( LDAP_DEBUG_ANY,
855 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
856 query_uuid->bv_val, 0, 0 );
858 op->orm_modlist = &mod;
860 op->o_bd->be_modify( op, &sreply );
862 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
863 op->o_tmpfree( qi, op->o_tmpmemctx );
870 AttributeName* attrs,
877 AttributeName* attrs_in,
884 struct berval* tempstr,
888 QueryTemplate *qt = qm->templates + template_id;
890 if (attr_set != qt->attr_set_index) {
891 int* id_array = qm->attr_sets[attr_set].ID_array;
893 while (*id_array != -1) {
894 if (*id_array == qt->attr_set_index)
901 return (qt->querystr.bv_len == tempstr->bv_len &&
902 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
909 AttributeName** filter_attrs,
912 AttributeDescription *ad;
914 switch ( f->f_choice ) {
915 case LDAP_FILTER_EQUALITY:
917 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
918 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
923 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
924 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
929 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
930 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
933 case LDAP_FILTER_APPROX:
935 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
936 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
939 case LDAP_FILTER_SUBSTRINGS:
941 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
942 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
945 case LDAP_FILTER_PRESENT:
947 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
948 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
951 case LDAP_FILTER_AND:
953 case LDAP_FILTER_NOT: {
955 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
956 f->f_choice == LDAP_FILTER_AND ? '&' :
957 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
958 fstr->bv_len += sizeof("(%") - 1;
960 for ( f = f->f_list; f != NULL; f = f->f_next ) {
961 rc = filter2template( f, fstr, filter_attrs, filter_cnt );
964 sprintf( fstr->bv_val+fstr->bv_len, ")" );
965 fstr->bv_len += sizeof(")") - 1;
971 strcpy( fstr->bv_val, "(?=?)" );
972 fstr->bv_len += sizeof("(?=?)")-1;
976 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
977 (*filter_cnt + 2)*sizeof(AttributeName));
979 (*filter_attrs)[*filter_cnt].an_desc = ad;
980 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
981 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
982 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
1001 struct berval *query_uuid)
1003 struct search_info *si = op->o_callback->sc_private;
1004 slap_overinst *on = si->on;
1005 cache_manager *cm = on->on_bi.bi_private;
1006 query_manager* qm = cm->qm;
1010 struct berval crp_uuid;
1011 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1012 Operation op_tmp = *op;
1014 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1015 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1017 op_tmp.o_bd = &cm->db;
1018 op_tmp.o_dn = cm->db.be_rootdn;
1019 op_tmp.o_ndn = cm->db.be_rootndn;
1021 Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1024 for ( e=si->head; e; e=si->head ) {
1025 si->head = e->e_private;
1026 e->e_private = NULL;
1027 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1028 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1029 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1030 while ( cm->cur_entries > (cm->max_entries) ) {
1031 qm->crfunc(qm, &crp_uuid);
1032 if (crp_uuid.bv_val) {
1033 Debug( LDAP_DEBUG_ANY,
1034 "Removing query UUID %s\n",
1035 crp_uuid.bv_val, 0, 0 );
1036 return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
1037 Debug( LDAP_DEBUG_ANY,
1038 "QUERY REMOVED, SIZE=%d\n",
1040 ldap_pvt_thread_mutex_lock(
1042 cm->cur_entries -= return_val;
1043 cm->num_cached_queries--;
1044 Debug( LDAP_DEBUG_ANY,
1045 "STORED QUERIES = %lu\n",
1046 cm->num_cached_queries, 0, 0 );
1047 ldap_pvt_thread_mutex_unlock(
1049 Debug( LDAP_DEBUG_ANY,
1050 "QUERY REMOVED, CACHE ="
1052 cm->cur_entries, 0, 0 );
1056 return_val = merge_entry(&op_tmp, e, query_uuid);
1057 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1058 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1059 cm->cur_entries += return_val;
1060 Debug( LDAP_DEBUG_ANY,
1061 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1062 cm->cur_entries, 0, 0 );
1064 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1066 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1067 cm->num_cached_queries++;
1068 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1069 cm->num_cached_queries, 0, 0 );
1070 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1076 proxy_cache_response(
1080 struct search_info *si = op->o_callback->sc_private;
1081 slap_overinst *on = si->on;
1082 cache_manager *cm = on->on_bi.bi_private;
1083 query_manager* qm = cm->qm;
1086 if ( rs->sr_type == REP_SEARCH ) {
1088 /* If we haven't exceeded the limit for this query,
1089 * build a chain of answers to store. If we hit the
1090 * limit, empty the chain and ignore the rest.
1093 if ( si->count < si->max ) {
1095 e = entry_dup( rs->sr_entry );
1096 if ( !si->head ) si->head = e;
1097 if ( si->tail ) si->tail->e_private = e;
1102 for (;si->head; si->head=e) {
1103 e = si->head->e_private;
1104 si->head->e_private = NULL;
1105 entry_free(si->head);
1111 if (rs->sr_attrs != op->ors_attrs ) {
1112 op->o_tmpfree( rs->sr_attrs, op->o_tmpmemctx );
1114 rs->sr_attrs = si->query.save_attrs;
1115 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
1116 op->ors_attrs = si->query.save_attrs;
1117 si->query.save_attrs = NULL;
1119 } else if ( rs->sr_type == REP_RESULT ) {
1120 if ( si->count && cache_entries( op, rs, &uuid ) == 0 ) {
1121 qm->addfunc(qm, &si->query, si->template_id, &uuid);
1122 /* If the consistency checker suspended itself,
1125 if ( cm->cc_paused ) {
1126 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1127 if ( cm->cc_paused ) {
1129 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
1131 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1136 op->o_callback->sc_cleanup = slap_freeself_cb;
1138 return SLAP_CB_CONTINUE;
1144 AttributeName** new_attrs,
1145 AttributeName* attrs,
1146 AttributeName* filter_attrs )
1153 /* duplicate attrs */
1154 if (attrs == NULL) {
1157 for (count=0; attrs[count].an_name.bv_val; count++)
1160 *new_attrs = (AttributeName*)(op->o_tmpalloc((count+1)*
1161 sizeof(AttributeName), op->o_tmpmemctx));
1162 if (attrs == NULL) {
1163 (*new_attrs)[0].an_name.bv_val = "*";
1164 (*new_attrs)[0].an_name.bv_len = 1;
1165 (*new_attrs)[1].an_name.bv_val = NULL;
1166 (*new_attrs)[1].an_name.bv_len = 0;
1170 for (i=0; i<count; i++) {
1171 (*new_attrs)[i].an_name = attrs[i].an_name;
1172 (*new_attrs)[i].an_desc = attrs[i].an_desc;
1174 (*new_attrs)[count].an_name.bv_val = NULL;
1175 (*new_attrs)[count].an_name.bv_len = 0;
1176 alluser = an_find(*new_attrs, &AllUser);
1177 allop = an_find(*new_attrs, &AllOper);
1180 for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1181 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1183 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1188 *new_attrs = (AttributeName*)(op->o_tmprealloc(*new_attrs,
1189 (count+2)*sizeof(AttributeName), op->o_tmpmemctx));
1190 (*new_attrs)[count].an_name = filter_attrs[i].an_name;
1191 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc;
1193 (*new_attrs)[count].an_name.bv_val = NULL;
1194 (*new_attrs)[count].an_name.bv_len = 0;
1203 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1204 cache_manager *cm = on->on_bi.bi_private;
1205 query_manager* qm = cm->qm;
1211 AttributeName *filter_attrs = NULL;
1212 AttributeName *new_attrs = NULL;
1217 int template_id = -1;
1221 int oc_attr_absent = 1;
1223 struct berval tempstr;
1225 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1227 if (filter2template(op->ors_filter, &tempstr, &filter_attrs, &fattr_cnt)) {
1228 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1229 return SLAP_CB_CONTINUE;
1232 Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
1233 tempstr.bv_val, 0, 0 );
1236 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1238 query.filter = op->ors_filter;
1239 query.attrs = op->ors_attrs;
1240 query.save_attrs = NULL;
1241 query.base = op->o_req_ndn;
1242 query.scope = op->ors_scope;
1244 /* check for query containment */
1245 if (attr_set > -1) {
1246 for (i=0; i<cm->numtemplates; i++) {
1247 /* find if template i can potentially answer tempstr */
1248 if (!is_temp_answerable(attr_set, &tempstr, qm, i))
1250 if (attr_set == qm->templates[i].attr_set_index) {
1254 Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1255 op->ors_filterstr.bv_val, 0, 0 );
1256 answerable = (*(qm->qcfunc))(qm, &query, i);
1262 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1264 query.save_attrs = op->ors_attrs;
1268 /* Need to clear the callbacks of the original operation,
1269 * in case there are other overlays */
1270 BackendDB *save_bd = op->o_bd;
1271 slap_callback *save_cb = op->o_callback;
1273 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
1275 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
1277 op->o_callback = NULL;
1278 i = cm->db.bd_info->bi_op_search( op, rs );
1280 op->o_callback = save_cb;
1284 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1286 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1287 if (cm->num_cached_queries >= cm->max_queries) {
1290 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1294 struct search_info *si;
1296 Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
1297 query.filter = str2filter(op->ors_filterstr.bv_val);
1298 if (op->ors_attrs) {
1299 for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1300 if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass ) {
1304 query.attrs = (AttributeName *)ch_malloc( ( count + 1 + oc_attr_absent )
1305 *sizeof(AttributeName) );
1306 for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1307 ber_dupbv( &query.attrs[count].an_name, &op->ors_attrs[count].an_name );
1308 query.attrs[count].an_desc = op->ors_attrs[count].an_desc;
1310 if ( oc_attr_absent ) {
1311 query.attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
1312 ber_dupbv( &query.attrs[count].an_name,
1313 &slap_schema.si_ad_objectClass->ad_cname );
1316 query.attrs[ count ].an_name.bv_val = NULL;
1317 query.attrs[ count ].an_name.bv_len = 0;
1319 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
1321 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1322 cb->sc_response = proxy_cache_response;
1323 cb->sc_cleanup = NULL;
1324 cb->sc_private = (cb+1);
1325 si = cb->sc_private;
1328 si->template_id = template_id;
1329 si->max = cm->num_entries_limit ;
1335 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1336 cb->sc_next = op->o_callback;
1337 op->o_callback = cb;
1340 slap_callback **pcb;
1342 /* need to move the callback at the end, in case other
1343 * overlays are present, so that the final entry is
1344 * actually cached */
1346 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
1351 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE\n",
1357 return SLAP_CB_CONTINUE;
1362 AttributeName* attrs_in,
1363 AttributeName* attrs)
1365 int i, count1, count2;
1366 if ( attrs_in == NULL ) {
1367 return (attrs ? 0 : 1);
1369 if ( attrs == NULL )
1373 attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1377 attrs && attrs[count2].an_name.bv_val != NULL;
1380 if ( count1 != count2 )
1383 for ( i=0; i<count1; i++ ) {
1384 if ( !an_find(attrs, &attrs_in[i].an_name ))
1392 AttributeName* attrs,
1397 for (i=0; i<num; i++) {
1398 if (attrscmp(attrs, qm->attr_sets[i].attrs))
1409 struct re_s *rtask = arg;
1410 slap_overinst *on = rtask->arg;
1411 cache_manager *cm = on->on_bi.bi_private;
1412 query_manager *qm = cm->qm;
1413 Connection conn = {0};
1414 char opbuf[OPERATION_BUFFER_SIZE];
1417 SlapReply rs = {REP_RESULT};
1418 CachedQuery* query, *query_prev;
1419 int i, return_val, pause = 1;
1420 QueryTemplate* templ;
1422 op = (Operation *)opbuf;
1423 connection_fake_init( &conn, op, ctx );
1426 op->o_dn = cm->db.be_rootdn;
1427 op->o_ndn = cm->db.be_rootndn;
1431 for (i=0; qm->templates[i].querystr.bv_val; i++) {
1432 templ = qm->templates + i;
1433 query = templ->query_last;
1434 if ( query ) pause = 0;
1435 op->o_time = slap_get_time();
1436 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1437 while (query && (query->expiry_time < op->o_time)) {
1438 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1439 remove_query(qm, query);
1440 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1441 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1443 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1444 remove_from_template(query, templ);
1445 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1446 i, templ->no_of_queries, 0 );
1447 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1449 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1450 return_val = remove_query_data(op, &rs, &query->q_uuid);
1451 Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1453 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1454 cm->cur_entries -= return_val;
1455 cm->num_cached_queries--;
1456 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1457 cm->num_cached_queries, 0, 0 );
1458 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1459 Debug( LDAP_DEBUG_ANY,
1460 "STALE QUERY REMOVED, CACHE ="
1462 cm->cur_entries, 0, 0 );
1464 query = query->prev;
1465 free_query(query_prev);
1467 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1469 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1470 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1471 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1473 /* If there were no queries, defer processing for a while */
1474 cm->cc_paused = pause;
1475 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
1477 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1482 #define MAX_ATTR_SETS 500
1483 static void find_supersets( struct attr_set* attr_sets, int numsets );
1484 static int compare_sets( struct attr_set* setA, int, int );
1495 slap_overinst *on = (slap_overinst *)be->bd_info;
1496 cache_manager* cm = on->on_bi.bi_private;
1497 query_manager* qm = cm->qm;
1498 QueryTemplate* temp;
1499 AttributeName* attr_name;
1500 AttributeName* attrarray;
1501 const char* text=NULL;
1502 char *save_argv0 = NULL;
1508 if ( strncasecmp( argv[0], "proxycache-", STRLENOF( "proxycache-" ) ) == 0 ) {
1509 save_argv0 = argv[0];
1510 argv[0] += STRLENOF( "proxycache-" );
1513 if ( strcasecmp( argv[0], "proxycache" ) == 0 ) {
1515 fprintf( stderr, "%s: line %d: missing arguments in \"proxycache"
1516 " <backend> <max_entries> <numattrsets> <entry limit> "
1517 "<cycle_time>\"\n", fname, lineno );
1521 cm->db.bd_info = backend_info( argv[1] );
1522 if ( !cm->db.bd_info ) {
1523 fprintf( stderr, "%s: line %d: backend %s unknown\n",
1524 fname, lineno, argv[1] );
1527 if ( cm->db.bd_info->bi_db_init( &cm->db ) ) return( 1 );
1529 /* This type is in use, needs to be opened */
1530 cm->db.bd_info->bi_nDB++;
1532 cm->max_entries = atoi( argv[2] );
1534 cm->numattrsets = atoi( argv[3] );
1535 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1536 fprintf( stderr, "%s: line %d: numattrsets must be <= %d\n",
1537 fname, lineno, MAX_ATTR_SETS );
1541 cm->num_entries_limit = atoi( argv[4] );
1542 cm->cc_period = atoi( argv[5] );
1543 Debug( LDAP_DEBUG_ANY,
1544 "Total # of attribute sets to be cached = %d\n",
1545 cm->numattrsets, 0, 0 );
1546 qm->attr_sets = ( struct attr_set * )ch_malloc( cm->numattrsets *
1547 sizeof( struct attr_set ));
1548 for ( i = 0; i < cm->numattrsets; i++ ) {
1549 qm->attr_sets[i].attrs = NULL;
1552 } else if ( strcasecmp( argv[0], "proxyattrset" ) == 0 ) {
1554 fprintf( stderr, "%s: line %d: missing arguments in \"proxyattrset "
1555 "<index> <attributes>\"\n", fname, lineno );
1558 Debug( LDAP_DEBUG_ANY, "Attribute Set # %d\n",
1559 atoi( argv[1] ), 0, 0 );
1560 if (atoi(argv[1]) >= cm->numattrsets) {
1561 fprintf( stderr, "%s; line %d index out of bounds \n",
1565 index = atoi( argv[1] );
1566 if ( argv[2] && strcmp( argv[2], "*" ) ) {
1567 qm->attr_sets[index].count = argc - 2;
1568 qm->attr_sets[index].attrs = (AttributeName*)ch_malloc(
1569 (argc-1) * sizeof( AttributeName ));
1570 attr_name = qm->attr_sets[index].attrs;
1571 for ( i = 2; i < argc; i++ ) {
1572 Debug( LDAP_DEBUG_ANY, "\t %s\n",
1574 ber_str2bv( argv[i], 0, 1,
1575 &attr_name->an_name);
1576 attr_name->an_desc = NULL;
1577 slap_bv2ad( &attr_name->an_name,
1578 &attr_name->an_desc, &text );
1580 attr_name->an_name.bv_val = NULL;
1581 attr_name->an_name.bv_len = 0;
1584 } else if ( strcasecmp( argv[0], "proxytemplate" ) == 0 ) {
1586 fprintf( stderr, "%s: line %d: missing argument(s) in "
1587 "\"proxytemplate <filter> <proj attr set> <TTL>\" line\n",
1591 if (( i = atoi( argv[2] )) >= cm->numattrsets ) {
1592 Debug( LDAP_DEBUG_ANY,
1593 "%s: line %d, template index invalid\n",
1597 num = cm->numtemplates;
1599 find_supersets( qm->attr_sets, cm->numattrsets );
1600 qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
1601 ( num + 2 ) * sizeof( QueryTemplate ));
1602 temp = qm->templates + num;
1603 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
1604 temp->query = temp->query_last = NULL;
1605 temp->ttl = atoi( argv[3] );
1606 temp->no_of_queries = 0;
1607 if ( argv[1] == NULL ) {
1608 Debug( LDAP_DEBUG_ANY,
1609 "Templates string not specified "
1610 "for template %d\n", num, 0, 0 );
1613 ber_str2bv( argv[1], 0, 1, &temp->querystr );
1614 Debug( LDAP_DEBUG_ANY, "Template:\n", 0, 0, 0 );
1615 Debug( LDAP_DEBUG_ANY, " query template: %s\n",
1616 temp->querystr.bv_val, 0, 0 );
1617 temp->attr_set_index = i;
1618 Debug( LDAP_DEBUG_ANY, " attributes: \n", 0, 0, 0 );
1619 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1620 for ( i=0; attrarray[i].an_name.bv_val; i++ )
1621 Debug( LDAP_DEBUG_ANY, "\t%s\n",
1622 attrarray[i].an_name.bv_val, 0, 0 );
1625 temp->querystr.bv_val = NULL;
1628 } else if ( strcasecmp( argv[0], "response-callback" ) == 0 ) {
1629 /* set to "tail" to put the response callback
1630 * at the end of the callback list; this is required
1631 * in case other overlays are present, so that the
1632 * final entry is cached. */
1635 Debug( LDAP_DEBUG_ANY,
1636 "missing specifier for \"response-callback {head(default)|tail}\" "
1637 "callback position\n", 0, 0, 0 );
1641 if ( strcasecmp( argv[1], "head" ) == 0 ) {
1642 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
1644 } else if ( strcasecmp( argv[1], "tail" ) == 0 ) {
1645 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1648 Debug( LDAP_DEBUG_ANY,
1649 "unknown specifier %s for \"response-callback {head(default)|tail}\" "
1650 "callback position\n", argv[1], 0, 0 );
1656 rc = cm->db.bd_info->bi_db_config( &cm->db, fname, lineno, argc, argv );
1660 argv[0] = save_argv0;
1671 slap_overinst *on = (slap_overinst *)be->bd_info;
1675 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1676 on->on_bi.bi_private = cm;
1678 qm = (query_manager*)ch_malloc(sizeof(query_manager));
1681 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1682 cm->db.be_private = NULL;
1683 cm->db.be_pcl_mutexp = &cm->db.be_pcl_mutex;
1685 cm->numattrsets = 0;
1686 cm->numtemplates = 0;
1687 cm->num_entries_limit = 5;
1688 cm->num_cached_queries = 0;
1689 cm->max_entries = 0;
1690 cm->cur_entries = 0;
1691 cm->max_queries = 10000;
1692 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1693 cm->cc_period = 1000;
1696 qm->attr_sets = NULL;
1697 qm->templates = NULL;
1699 qm->lru_bottom = NULL;
1701 qm->qcfunc = query_containment;
1702 qm->crfunc = cache_replacement;
1703 qm->addfunc = add_query;
1704 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1706 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1707 ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1716 slap_overinst *on = (slap_overinst *)be->bd_info;
1717 cache_manager *cm = on->on_bi.bi_private;
1721 /* consistency check (add more...) */
1722 for ( i = 0; i < cm->numattrsets; i++ ) {
1723 if ( cm->qm->attr_sets[i].attrs == NULL ) {
1724 fprintf( stderr, "proxy_cache_open(): "
1725 "attr set %d (of %d) missing\n",
1726 i, cm->numattrsets );
1731 /* need to inherit something from the original database... */
1732 cm->db.be_def_limit = be->be_def_limit;
1733 cm->db.be_limits = be->be_limits;
1734 cm->db.be_acl = be->be_acl;
1735 cm->db.be_dfltaccess = be->be_dfltaccess;
1737 rc = backend_startup_one( &cm->db );
1739 /* There is no runqueue in TOOL mode */
1740 if ( slapMode & SLAP_SERVER_MODE ) {
1741 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1742 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
1743 consistency_check, on );
1744 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1746 /* Cached database must have the rootdn */
1747 if ( BER_BVISNULL( &cm->db.be_rootndn )
1748 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
1750 fprintf( stderr, "proxy_cache_open(): "
1751 "underlying database of type \"%s\"\n"
1752 " serving naming context \"%s\"\n"
1753 " has no \"rootdn\", required by \"proxycache\".\n",
1754 on->on_info->oi_orig->bi_type,
1755 cm->db.be_suffix[0].bv_val );
1768 slap_overinst *on = (slap_overinst *)be->bd_info;
1769 cache_manager *cm = on->on_bi.bi_private;
1770 query_manager *qm = cm->qm;
1773 /* cleanup stuff inherited from the original database... */
1774 cm->db.be_limits = NULL;
1775 cm->db.be_acl = NULL;
1777 if ( cm->db.bd_info->bi_db_close ) {
1778 rc = cm->db.bd_info->bi_db_close( &cm->db );
1780 for ( i=0; i<cm->numtemplates; i++ ) {
1781 CachedQuery *qc, *qn;
1782 for ( qc = qm->templates[i].query; qc; qc = qn ) {
1786 free( qm->templates[i].querystr.bv_val );
1787 ldap_pvt_thread_rdwr_destroy( &qm->templates[i].t_rwlock );
1789 free( qm->templates );
1790 qm->templates = NULL;
1792 for ( i=0; i<cm->numattrsets; i++ ) {
1793 free( qm->attr_sets[i].ID_array );
1794 for ( j=0; j<qm->attr_sets[i].count; j++ ) {
1795 free( qm->attr_sets[i].attrs[j].an_name.bv_val );
1797 free( qm->attr_sets[i].attrs );
1799 free( qm->attr_sets );
1800 qm->attr_sets = NULL;
1806 proxy_cache_destroy(
1810 slap_overinst *on = (slap_overinst *)be->bd_info;
1811 cache_manager *cm = on->on_bi.bi_private;
1812 query_manager *qm = cm->qm;
1815 if ( cm->db.bd_info->bi_db_destroy ) {
1816 rc = cm->db.bd_info->bi_db_destroy( &cm->db );
1818 ldap_pvt_thread_mutex_destroy(&qm->lru_mutex);
1819 ldap_pvt_thread_mutex_destroy(&cm->cache_mutex);
1820 ldap_pvt_thread_mutex_destroy(&cm->remove_mutex);
1827 find_supersets ( struct attr_set* attr_sets, int numsets )
1829 int num[MAX_ATTR_SETS];
1832 for ( i = 0; i < MAX_ATTR_SETS; i++ )
1835 for ( i = 0; i < numsets; i++ ) {
1836 attr_sets[i].ID_array = (int*) ch_malloc( sizeof( int ) );
1837 attr_sets[i].ID_array[0] = -1;
1840 for ( i = 0; i < numsets; i++ ) {
1841 for ( j=i+1; j < numsets; j++ ) {
1842 res = compare_sets( attr_sets, i, j );
1848 id_array = attr_sets[i].ID_array;
1849 attr_sets[i].ID_array = (int *) ch_realloc( id_array,
1850 ( num[i] + 2 ) * sizeof( int ));
1851 attr_sets[i].ID_array[num[i]] = j;
1852 attr_sets[i].ID_array[num[i]+1] = -1;
1857 id_array = attr_sets[j].ID_array;
1858 attr_sets[j].ID_array = (int *) ch_realloc( id_array,
1859 ( num[j] + 2 ) * sizeof( int ));
1860 attr_sets[j].ID_array[num[j]] = i;
1861 attr_sets[j].ID_array[num[j]+1] = -1;
1870 * compares two sets of attributes (indices i and j)
1871 * returns 0: if neither set is contained in the other set
1872 * 1: if set i is contained in set j
1873 * 2: if set j is contained in set i
1874 * 3: the sets are equivalent
1878 compare_sets(struct attr_set* set, int i, int j)
1884 if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
1887 if ( set[i].attrs == NULL )
1890 if ( set[j].attrs == NULL )
1893 numI = set[i].count;
1894 numJ = set[j].count;
1896 for ( l=0; l < numI; l++ ) {
1897 for ( k = 0; k < numJ; k++ ) {
1898 if ( strcmp( set[i].attrs[l].an_name.bv_val,
1899 set[j].attrs[k].an_name.bv_val ) == 0 )
1904 if ( common == numI )
1907 if ( common == numJ )
1913 static slap_overinst proxy_cache;
1917 LDAPAttributeType *at;
1921 at = ldap_str2attributetype( queryid_schema, &code, &err,
1922 LDAP_SCHEMA_ALLOW_ALL );
1924 fprintf( stderr, "AttributeType Load failed %s %s\n",
1925 ldap_scherr2str(code), err );
1928 code = at_add( at, &err );
1930 slap_str2ad( at->at_names[0], &ad_queryid, &err );
1934 fprintf( stderr, "AttributeType Load failed %s %s\n",
1935 scherr2str(code), err );
1939 proxy_cache.on_bi.bi_type = "proxycache";
1940 proxy_cache.on_bi.bi_db_init = proxy_cache_init;
1941 proxy_cache.on_bi.bi_db_config = proxy_cache_config;
1942 proxy_cache.on_bi.bi_db_open = proxy_cache_open;
1943 proxy_cache.on_bi.bi_db_close = proxy_cache_close;
1944 proxy_cache.on_bi.bi_db_destroy = proxy_cache_destroy;
1945 proxy_cache.on_bi.bi_op_search = proxy_cache_search;
1947 return overlay_register( &proxy_cache );
1950 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
1951 int init_module(int argc, char *argv[]) {
1952 return pcache_init();
1956 #endif /* defined(SLAPD_OVER_PROXYCACHE) */