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>
37 /* query cache structs */
40 typedef struct Query_s {
41 Filter* filter; /* Search Filter */
42 AttributeName* attrs; /* Projected attributes */
43 AttributeName* save_attrs; /* original attributes, saved for response */
44 struct berval base; /* Search Base */
45 int scope; /* Search scope */
48 /* struct representing a cached query */
49 typedef struct cached_query_s {
50 Query query; /* LDAP query */
51 struct berval q_uuid; /* query identifier */
52 int template_id; /* template of the query */
53 time_t expiry_time; /* time till the query is considered valid */
54 struct cached_query_s *next; /* next query in the template */
55 struct cached_query_s *prev; /* previous query in the template */
56 struct cached_query_s *lru_up; /* previous query in the LRU list */
57 struct cached_query_s *lru_down; /* next query in the LRU list */
60 /* struct representing a query template
61 * e.g. template string = &(cn=)(mail=)
63 typedef struct query_template_s {
64 struct berval querystr; /* Filter string corresponding to the QT */
65 int attr_set_index; /* determines the projected attributes */
67 CachedQuery* query; /* most recent query cached for the template */
68 CachedQuery* query_last; /* oldest query cached for the template */
70 int no_of_queries; /* Total number of queries in the template */
71 long ttl; /* TTL for the queries of this template */
72 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
76 * Represents a set of projected attributes and any
77 * supersets among all specified sets of attributes.
81 AttributeName* attrs; /* specifies the set */
82 int count; /* number of attributes */
83 int* ID_array; /* array of indices of supersets of 'attrs' */
86 struct query_manager_s;
88 /* prototypes for functions for 1) query containment
89 * 2) query addition, 3) cache replacement
91 typedef int (QCfunc)(struct query_manager_s*, Query*, int );
92 typedef void (AddQueryfunc)(struct query_manager_s*, Query*, int, struct berval*);
93 typedef void (CRfunc)(struct query_manager_s*, struct berval * );
95 /* LDAP query cache */
96 typedef struct query_manager_s {
97 struct attr_set* attr_sets; /* possible sets of projected attributes */
98 QueryTemplate* templates; /* cacheable templates */
100 CachedQuery* lru_top; /* top and bottom of LRU list */
101 CachedQuery* lru_bottom;
103 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
105 /* Query cache methods */
106 QCfunc *qcfunc; /* Query containment*/
107 CRfunc *crfunc; /* cache replacement */
108 AddQueryfunc *addfunc; /* add query */
111 /* LDAP query cache manager */
112 typedef struct cache_manager_s {
113 BackendDB db; /* underlying database */
114 unsigned long num_cached_queries; /* total number of cached queries */
115 unsigned long max_queries; /* upper bound on # of cached queries */
116 int numattrsets; /* number of attribute sets */
117 int numtemplates; /* number of cacheable templates */
118 int cur_entries; /* current number of entries cached */
119 int max_entries; /* max number of entries cached */
120 int num_entries_limit; /* max # of entries in a cacheable query */
122 char response_cb; /* install the response callback
123 * at the tail of the callback list */
124 #define PCACHE_RESPONSE_CB_HEAD 0
125 #define PCACHE_RESPONSE_CB_TAIL 1
127 int cc_period; /* interval between successive consistency checks (sec) */
131 ldap_pvt_thread_mutex_t cache_mutex;
132 ldap_pvt_thread_mutex_t remove_mutex;
134 query_manager* qm; /* query cache managed by the cache manager */
137 static AttributeDescription *ad_queryid;
138 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
139 "DESC 'list of queries the entry belongs to' "
140 "EQUALITY octetStringMatch "
141 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
142 "NO-USER-MODIFICATION USAGE directoryOperation )";
144 /* Return 1 for an added entry, else 0 */
149 struct berval* query_uuid )
152 Modifications* modlist = NULL;
153 const char* text = NULL;
155 char textbuf[SLAP_TEXT_BUFLEN];
156 size_t textlen = sizeof(textbuf);
158 SlapReply sreply = {REP_RESULT};
160 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
165 /* add queryid attribute */
166 attr_merge_one( e, ad_queryid, query_uuid, NULL );
168 /* append the attribute list from the fetched entry */
169 e->e_attrs->a_next = attr;
171 op->o_tag = LDAP_REQ_ADD;
172 op->o_protocol = LDAP_VERSION3;
173 op->o_callback = &cb;
174 op->o_time = slap_get_time();
175 op->o_do_not_cache = 1;
178 op->o_req_dn = e->e_name;
179 op->o_req_ndn = e->e_nname;
180 rc = op->o_bd->be_add( op, &sreply );
182 if ( rc != LDAP_SUCCESS ) {
183 if ( rc == LDAP_ALREADY_EXISTS ) {
184 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
185 modlist->sml_op = LDAP_MOD_ADD;
186 op->o_tag = LDAP_REQ_MODIFY;
187 op->orm_modlist = modlist;
188 op->o_bd->be_modify( op, &sreply );
189 slap_mods_free( modlist );
190 } else if ( rc == LDAP_REFERRAL ||
191 rc == LDAP_NO_SUCH_OBJECT ) {
192 syncrepl_add_glue( op, e );
201 be_entry_release_w( op, e );
208 /* compare base and scope of incoming and cached queries */
209 static int base_scope_compare(
210 struct berval* ndn_stored,
211 struct berval* ndn_incoming,
215 struct berval pdn_incoming = BER_BVNULL;
217 if (scope_stored < scope_incoming)
220 if ( !dnIsSuffix(ndn_incoming, ndn_stored))
223 switch(scope_stored) {
224 case LDAP_SCOPE_BASE:
225 return (ndn_incoming->bv_len == ndn_stored->bv_len);
227 case LDAP_SCOPE_ONELEVEL:
228 switch(scope_incoming){
229 case LDAP_SCOPE_BASE:
230 dnParent(ndn_incoming, &pdn_incoming);
231 return (pdn_incoming.bv_len == ndn_stored->bv_len);
233 case LDAP_SCOPE_ONELEVEL:
234 return (ndn_incoming->bv_len == ndn_stored->bv_len);
239 case LDAP_SCOPE_SUBTREE:
248 /* add query on top of LRU list */
250 add_query_on_top (query_manager* qm, CachedQuery* qc)
252 CachedQuery* top = qm->lru_top;
253 Query* q = (Query*)qc;
264 Debug( LDAP_DEBUG_ANY, "Base of added query = %s\n",
265 q->base.bv_val, 0, 0 );
268 /* remove_query from LRU list */
271 remove_query (query_manager* qm, CachedQuery* qc)
294 qc->lru_up = qc->lru_down = NULL;
298 invert_string( struct berval *bv )
303 for (i=0; i<bv->bv_len/2; i++) {
305 bv->bv_val[i] = bv->bv_val[bv->bv_len-i-1];
306 bv->bv_val[bv->bv_len-i-1] = c;
310 /* find and remove string2 from string1
311 * from start if position = 1,
312 * from end if position = 3,
313 * from anywhere if position = 2
317 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
324 char* string1=ber1->bv_val;
325 char* string2=ber2->bv_val;
340 temp = strstr(arg1, arg2);
344 if ( temp && (position == 2 || temp == arg1) ) {
346 strcpy( arg1, string1 );
350 if ( position == 3 ) {
358 static struct berval*
359 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
361 struct berval* merged, *temp;
362 int i, any_count, count;
364 for (any_count=0; any && any[any_count].bv_val; any_count++)
374 merged = (struct berval*)(ch_malloc((count+1)*sizeof(struct berval)));
381 for (i=0; i<any_count; i++) {
394 strings_containment(struct berval* stored, struct berval* incoming)
396 struct berval* element;
400 for ( element=stored; element->bv_val != NULL; element++ ) {
401 for (j = k; incoming[j].bv_val != NULL; j++) {
402 if (find_and_remove(&(incoming[j]), element, 2)) {
419 substr_containment_substr(Filter* stored, Filter* incoming)
425 struct berval init_incoming;
426 struct berval final_incoming;
427 struct berval *any_incoming = NULL;
428 struct berval *remaining_incoming = NULL;
430 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
431 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
435 ber_dupbv(&init_incoming, &(incoming->f_sub_initial));
436 ber_dupbv(&final_incoming, &(incoming->f_sub_final));
438 if (incoming->f_sub_any) {
439 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
443 any_incoming = (struct berval*)ch_malloc((any_count+1) *
444 sizeof(struct berval));
446 for (i=0; i<any_count; i++) {
447 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
449 any_incoming[any_count].bv_val = NULL;
450 any_incoming[any_count].bv_len = 0;
453 if (find_and_remove(&init_incoming,
454 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
455 &(stored->f_sub_final), 3))
457 if (stored->f_sub_any == NULL) {
461 remaining_incoming = merge_init_final(&init_incoming,
462 any_incoming, &final_incoming);
463 rc = strings_containment(stored->f_sub_any, remaining_incoming);
466 free(init_incoming.bv_val);
467 free(final_incoming.bv_val);
468 if (any_incoming) ber_bvarray_free( any_incoming );
469 free(remaining_incoming);
475 substr_containment_equality(Filter* stored, Filter* incoming)
477 struct berval incoming_val[2];
480 ber_dupbv(incoming_val, &(incoming->f_av_value));
481 incoming_val[1].bv_val = NULL;
482 incoming_val[1].bv_len = 0;
484 if (find_and_remove(incoming_val,
485 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
486 &(stored->f_sub_final), 3)) {
487 if (stored->f_sub_any == NULL){
491 rc = strings_containment(stored->f_sub_any, incoming_val);
494 free(incoming_val[0].bv_val);
498 /* check whether query is contained in any of
499 * the cached queries in template template_index
502 query_containment(query_manager *qm,
506 QueryTemplate* templa= qm->templates;
509 Filter* inputf = query->filter;
510 struct berval* base = &(query->base);
511 int scope = query->scope;
518 MatchingRule* mrule = NULL;
519 if (inputf != NULL) {
520 Debug( LDAP_DEBUG_ANY, "Lock QC index = %d\n",
521 template_index, 0, 0 );
522 ldap_pvt_thread_rdwr_rlock(&(templa[template_index].t_rwlock));
523 for(qc=templa[template_index].query; qc != NULL; qc= qc->next) {
525 if(base_scope_compare(&(q->base), base, q->scope, scope)) {
530 switch (fs->f_choice) {
531 case LDAP_FILTER_EQUALITY:
532 if (fi->f_choice == LDAP_FILTER_EQUALITY)
533 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
539 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
545 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
546 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
547 &(fi->f_ava->aa_value),
548 &(fs->f_ava->aa_value), &text);
549 if (rc != LDAP_SUCCESS) {
550 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
551 Debug( LDAP_DEBUG_ANY,
552 "Unlock: Exiting QC index=%d\n",
553 template_index, 0, 0 );
557 switch (fs->f_choice) {
559 case LDAP_FILTER_AND:
564 case LDAP_FILTER_SUBSTRINGS:
565 /* check if the equality query can be
566 * answered with cached substring query */
567 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
568 && substr_containment_equality(
571 /* check if the substring query can be
572 * answered with cached substring query */
573 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
574 ) && substr_containment_substr(
580 case LDAP_FILTER_PRESENT:
585 case LDAP_FILTER_EQUALITY:
603 case LDAP_FILTER_NOT:
609 } while((res) && (fi != NULL) && (fs != NULL));
612 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
613 if (qm->lru_top != qc) {
614 remove_query(qm, qc);
615 add_query_on_top(qm, qc);
617 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
622 Debug( LDAP_DEBUG_ANY,
623 "Not answerable: Unlock QC index=%d\n",
624 template_index, 0, 0 );
625 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
631 free_query (CachedQuery* qc)
633 Query* q = (Query*)qc;
636 free(qc->q_uuid.bv_val);
637 filter_free(q->filter);
638 free (q->base.bv_val);
640 for (i=0; q->attrs[i].an_name.bv_val; i++) {
641 free(q->attrs[i].an_name.bv_val);
649 /* Add query to query cache */
650 static void add_query(
656 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
657 QueryTemplate* templ = (qm->templates)+template_index;
659 new_cached_query->template_id = template_index;
660 new_cached_query->q_uuid = *uuid;
661 new_cached_query->lru_up = NULL;
662 new_cached_query->lru_down = NULL;
663 new_cached_query->expiry_time = slap_get_time() + templ->ttl;
664 Debug( LDAP_DEBUG_ANY, "Added query expires at %ld\n",
665 (long) new_cached_query->expiry_time, 0, 0 );
666 new_query = (Query*)new_cached_query;
668 ber_dupbv(&new_query->base, &query->base);
669 new_query->scope = query->scope;
670 new_query->filter = query->filter;
671 new_query->attrs = query->attrs;
674 Debug( LDAP_DEBUG_ANY, "Lock AQ index = %d\n",
675 template_index, 0, 0 );
676 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
677 if (templ->query == NULL)
678 templ->query_last = new_cached_query;
680 templ->query->prev = new_cached_query;
681 new_cached_query->next = templ->query;
682 new_cached_query->prev = NULL;
683 templ->query = new_cached_query;
684 templ->no_of_queries++;
685 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES++ %d\n",
686 template_index, templ->no_of_queries, 0 );
688 Debug( LDAP_DEBUG_ANY, "Unlock AQ index = %d \n",
689 template_index, 0, 0 );
690 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
692 /* Adding on top of LRU list */
693 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
694 add_query_on_top(qm, new_cached_query);
695 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
699 remove_from_template (CachedQuery* qc, QueryTemplate* template)
701 if (!qc->prev && !qc->next) {
702 template->query_last = template->query = NULL;
703 } else if (qc->prev == NULL) {
704 qc->next->prev = NULL;
705 template->query = qc->next;
706 } else if (qc->next == NULL) {
707 qc->prev->next = NULL;
708 template->query_last = qc->prev;
710 qc->next->prev = qc->prev;
711 qc->prev->next = qc->next;
714 template->no_of_queries--;
717 /* remove bottom query of LRU list from the query cache */
718 static void cache_replacement(query_manager* qm, struct berval *result)
723 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
724 bottom = qm->lru_bottom;
726 result->bv_val = NULL;
730 Debug ( LDAP_DEBUG_ANY,
731 "Cache replacement invoked without "
732 "any query in LRU list\n", 0, 0, 0 );
736 temp_id = bottom->template_id;
737 remove_query(qm, bottom);
738 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
740 *result = bottom->q_uuid;
741 bottom->q_uuid.bv_val = NULL;
743 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n", temp_id, 0, 0 );
744 ldap_pvt_thread_rdwr_wlock(&(qm->templates[temp_id].t_rwlock));
745 remove_from_template(bottom, (qm->templates+temp_id));
746 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
747 temp_id, qm->templates[temp_id].no_of_queries, 0 );
748 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n", temp_id, 0, 0 );
749 ldap_pvt_thread_rdwr_wunlock(&(qm->templates[temp_id].t_rwlock));
754 struct query_info *next;
766 struct query_info *qi;
769 if ( rs->sr_type != REP_SEARCH ) return 0;
771 for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
772 if (attr->a_desc == ad_queryid) {
773 for (count=0; attr->a_vals[count].bv_val; count++)
778 if ( count == 0 ) return 0;
779 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
780 qi->next = op->o_callback->sc_private;
781 op->o_callback->sc_private = qi;
782 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
783 qi->del = ( count == 1 );
792 struct berval* query_uuid)
794 struct query_info *qi, *qnext;
796 #ifdef LDAP_COMP_MATCH
797 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
799 AttributeAssertion ava = { NULL, BER_BVNULL };
801 Filter filter = {LDAP_FILTER_EQUALITY};
802 SlapReply sreply = {REP_RESULT};
803 slap_callback cb = { NULL, remove_func, NULL, NULL };
806 sreply.sr_entry = NULL;
807 sreply.sr_nentries = 0;
808 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
809 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
811 filter.f_av_desc = ad_queryid;
812 filter.f_av_value = *query_uuid;
814 op->o_tag = LDAP_REQ_SEARCH;
815 op->o_protocol = LDAP_VERSION3;
816 op->o_callback = &cb;
817 op->o_time = slap_get_time();
818 op->o_do_not_cache = 1;
820 op->o_req_dn = op->o_bd->be_suffix[0];
821 op->o_req_ndn = op->o_bd->be_nsuffix[0];
822 op->ors_scope = LDAP_SCOPE_SUBTREE;
823 op->ors_deref = LDAP_DEREF_NEVER;
824 op->ors_slimit = SLAP_NO_LIMIT;
825 op->ors_tlimit = SLAP_NO_LIMIT;
826 op->ors_filter = &filter;
827 op->ors_filterstr.bv_val = filter_str;
828 op->ors_filterstr.bv_len = strlen(filter_str);
829 op->ors_attrs = NULL;
830 op->ors_attrsonly = 0;
832 op->o_bd->be_search( op, &sreply );
834 for ( qi=cb.sc_private; qi; qi=qnext ) {
837 op->o_req_dn = qi->xdn;
838 op->o_req_ndn = qi->xdn;
841 Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
842 query_uuid->bv_val, 0, 0 );
844 op->o_tag = LDAP_REQ_DELETE;
846 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
851 struct berval vals[2];
853 vals[0] = *query_uuid;
854 vals[1].bv_val = NULL;
856 mod.sml_op = LDAP_MOD_DELETE;
857 mod.sml_desc = ad_queryid;
858 mod.sml_type = ad_queryid->ad_cname;
859 mod.sml_values = vals;
860 mod.sml_nvalues = NULL;
862 Debug( LDAP_DEBUG_ANY,
863 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
864 query_uuid->bv_val, 0, 0 );
866 op->orm_modlist = &mod;
868 op->o_bd->be_modify( op, &sreply );
870 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
871 op->o_tmpfree( qi, op->o_tmpmemctx );
878 AttributeName* attrs,
885 AttributeName* attrs_in,
892 struct berval* tempstr,
896 QueryTemplate *qt = qm->templates + template_id;
898 if (attr_set != qt->attr_set_index) {
899 int* id_array = qm->attr_sets[attr_set].ID_array;
901 while (*id_array != -1) {
902 if (*id_array == qt->attr_set_index)
909 return (qt->querystr.bv_len == tempstr->bv_len &&
910 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
917 AttributeName** filter_attrs,
920 AttributeDescription *ad;
922 switch ( f->f_choice ) {
923 case LDAP_FILTER_EQUALITY:
925 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
926 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
931 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
932 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
937 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
938 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
941 case LDAP_FILTER_APPROX:
943 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
944 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
947 case LDAP_FILTER_SUBSTRINGS:
949 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
950 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
953 case LDAP_FILTER_PRESENT:
955 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
956 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
959 case LDAP_FILTER_AND:
961 case LDAP_FILTER_NOT: {
963 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
964 f->f_choice == LDAP_FILTER_AND ? '&' :
965 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
966 fstr->bv_len += sizeof("(%") - 1;
968 for ( f = f->f_list; f != NULL; f = f->f_next ) {
969 rc = filter2template( f, fstr, filter_attrs, filter_cnt );
972 sprintf( fstr->bv_val+fstr->bv_len, ")" );
973 fstr->bv_len += sizeof(")") - 1;
979 strcpy( fstr->bv_val, "(?=?)" );
980 fstr->bv_len += sizeof("(?=?)")-1;
984 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
985 (*filter_cnt + 2)*sizeof(AttributeName));
987 (*filter_attrs)[*filter_cnt].an_desc = ad;
988 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
989 (*filter_attrs)[*filter_cnt].an_oc = NULL;
990 (*filter_attrs)[*filter_cnt].an_oc_exclude = 0;
991 BER_BVZERO( &(*filter_attrs)[*filter_cnt+1].an_name );
1010 struct berval *query_uuid)
1012 struct search_info *si = op->o_callback->sc_private;
1013 slap_overinst *on = si->on;
1014 cache_manager *cm = on->on_bi.bi_private;
1015 query_manager* qm = cm->qm;
1018 struct berval crp_uuid;
1019 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1020 Operation op_tmp = *op;
1022 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1023 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1025 op_tmp.o_bd = &cm->db;
1026 op_tmp.o_dn = cm->db.be_rootdn;
1027 op_tmp.o_ndn = cm->db.be_rootndn;
1029 Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1032 for ( e=si->head; e; e=si->head ) {
1033 si->head = e->e_private;
1034 e->e_private = NULL;
1035 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1036 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1037 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1038 while ( cm->cur_entries > (cm->max_entries) ) {
1039 qm->crfunc(qm, &crp_uuid);
1040 if (crp_uuid.bv_val) {
1041 Debug( LDAP_DEBUG_ANY,
1042 "Removing query UUID %s\n",
1043 crp_uuid.bv_val, 0, 0 );
1044 return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
1045 Debug( LDAP_DEBUG_ANY,
1046 "QUERY REMOVED, SIZE=%d\n",
1048 ldap_pvt_thread_mutex_lock(
1050 cm->cur_entries -= return_val;
1051 cm->num_cached_queries--;
1052 Debug( LDAP_DEBUG_ANY,
1053 "STORED QUERIES = %lu\n",
1054 cm->num_cached_queries, 0, 0 );
1055 ldap_pvt_thread_mutex_unlock(
1057 Debug( LDAP_DEBUG_ANY,
1058 "QUERY REMOVED, CACHE ="
1060 cm->cur_entries, 0, 0 );
1064 return_val = merge_entry(&op_tmp, e, query_uuid);
1065 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1066 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1067 cm->cur_entries += return_val;
1068 Debug( LDAP_DEBUG_ANY,
1069 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1070 cm->cur_entries, 0, 0 );
1072 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1074 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1075 cm->num_cached_queries++;
1076 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1077 cm->num_cached_queries, 0, 0 );
1078 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1084 proxy_cache_response(
1088 struct search_info *si = op->o_callback->sc_private;
1089 slap_overinst *on = si->on;
1090 cache_manager *cm = on->on_bi.bi_private;
1091 query_manager* qm = cm->qm;
1094 if ( rs->sr_type == REP_SEARCH ) {
1096 /* If we haven't exceeded the limit for this query,
1097 * build a chain of answers to store. If we hit the
1098 * limit, empty the chain and ignore the rest.
1101 if ( si->count < si->max ) {
1103 e = entry_dup( rs->sr_entry );
1104 if ( !si->head ) si->head = e;
1105 if ( si->tail ) si->tail->e_private = e;
1110 for (;si->head; si->head=e) {
1111 e = si->head->e_private;
1112 si->head->e_private = NULL;
1113 entry_free(si->head);
1119 if (rs->sr_attrs != op->ors_attrs ) {
1120 op->o_tmpfree( rs->sr_attrs, op->o_tmpmemctx );
1122 rs->sr_attrs = si->query.save_attrs;
1123 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
1124 op->ors_attrs = si->query.save_attrs;
1125 si->query.save_attrs = NULL;
1127 } else if ( rs->sr_type == REP_RESULT ) {
1128 if ( si->count && cache_entries( op, rs, &uuid ) == 0 ) {
1129 qm->addfunc(qm, &si->query, si->template_id, &uuid);
1130 /* If the consistency checker suspended itself,
1133 if ( cm->cc_paused ) {
1134 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1135 if ( cm->cc_paused ) {
1137 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
1139 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1144 op->o_callback->sc_cleanup = slap_freeself_cb;
1146 return SLAP_CB_CONTINUE;
1152 AttributeName** new_attrs,
1153 AttributeName* attrs,
1154 AttributeName* filter_attrs )
1161 /* duplicate attrs */
1162 if (attrs == NULL) {
1165 for (count=0; attrs[count].an_name.bv_val; count++)
1168 *new_attrs = (AttributeName*)(op->o_tmpalloc((count+1)*
1169 sizeof(AttributeName), op->o_tmpmemctx));
1170 if (attrs == NULL) {
1171 BER_BVSTR( &(*new_attrs)[0].an_name, "*" );
1172 (*new_attrs)[0].an_desc = NULL;
1173 (*new_attrs)[0].an_oc = NULL;
1174 (*new_attrs)[0].an_oc_exclude = 0;
1175 BER_BVZERO( &(*new_attrs)[1].an_name );
1179 for (i=0; i<count; i++) {
1180 (*new_attrs)[i].an_name = attrs[i].an_name;
1181 (*new_attrs)[i].an_desc = attrs[i].an_desc;
1183 BER_BVZERO( &(*new_attrs)[count].an_name );
1184 alluser = an_find(*new_attrs, &AllUser);
1185 allop = an_find(*new_attrs, &AllOper);
1188 for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1189 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1191 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1196 *new_attrs = (AttributeName*)(op->o_tmprealloc(*new_attrs,
1197 (count+2)*sizeof(AttributeName), op->o_tmpmemctx));
1198 (*new_attrs)[count].an_name = filter_attrs[i].an_name;
1199 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc;
1200 (*new_attrs)[count].an_oc = NULL;
1201 (*new_attrs)[count].an_oc_exclude = 0;
1203 BER_BVZERO( &(*new_attrs)[count].an_name );
1212 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1213 cache_manager *cm = on->on_bi.bi_private;
1214 query_manager* qm = cm->qm;
1220 AttributeName *filter_attrs = NULL;
1225 int template_id = -1;
1229 int oc_attr_absent = 1;
1231 struct berval tempstr;
1233 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1235 if (filter2template(op->ors_filter, &tempstr, &filter_attrs, &fattr_cnt)) {
1236 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1237 return SLAP_CB_CONTINUE;
1240 Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
1241 tempstr.bv_val, 0, 0 );
1244 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1246 query.filter = op->ors_filter;
1247 query.attrs = op->ors_attrs;
1248 query.save_attrs = NULL;
1249 query.base = op->o_req_ndn;
1250 query.scope = op->ors_scope;
1252 /* check for query containment */
1253 if (attr_set > -1) {
1254 for (i=0; i<cm->numtemplates; i++) {
1255 /* find if template i can potentially answer tempstr */
1256 if (!is_temp_answerable(attr_set, &tempstr, qm, i))
1258 if (attr_set == qm->templates[i].attr_set_index) {
1262 Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1263 op->ors_filterstr.bv_val, 0, 0 );
1264 answerable = (*(qm->qcfunc))(qm, &query, i);
1270 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1272 query.save_attrs = op->ors_attrs;
1276 /* Need to clear the callbacks of the original operation,
1277 * in case there are other overlays */
1278 BackendDB *save_bd = op->o_bd;
1279 slap_callback *save_cb = op->o_callback;
1281 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
1283 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
1285 op->o_callback = NULL;
1286 i = cm->db.bd_info->bi_op_search( op, rs );
1288 op->o_callback = save_cb;
1292 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1294 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1295 if (cm->num_cached_queries >= cm->max_queries) {
1298 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1302 struct search_info *si;
1304 Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
1305 query.filter = str2filter(op->ors_filterstr.bv_val);
1306 if (op->ors_attrs) {
1307 for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1308 if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass ) {
1312 query.attrs = (AttributeName *)ch_malloc( ( count + 1 + oc_attr_absent )
1313 *sizeof(AttributeName) );
1314 for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1315 ber_dupbv( &query.attrs[count].an_name, &op->ors_attrs[count].an_name );
1316 query.attrs[count].an_desc = op->ors_attrs[count].an_desc;
1317 query.attrs[count].an_oc = op->ors_attrs[count].an_oc;
1318 query.attrs[count].an_oc_exclude = op->ors_attrs[count].an_oc_exclude;
1320 if ( oc_attr_absent ) {
1321 query.attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
1322 ber_dupbv( &query.attrs[count].an_name,
1323 &slap_schema.si_ad_objectClass->ad_cname );
1324 query.attrs[ count ].an_oc = NULL;
1325 query.attrs[ count ].an_oc_exclude = 0;
1328 BER_BVZERO( &query.attrs[ count ].an_name );
1330 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
1332 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1333 cb->sc_response = proxy_cache_response;
1334 cb->sc_cleanup = NULL;
1335 cb->sc_private = (cb+1);
1336 si = cb->sc_private;
1339 si->template_id = template_id;
1340 si->max = cm->num_entries_limit ;
1346 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1347 cb->sc_next = op->o_callback;
1348 op->o_callback = cb;
1351 slap_callback **pcb;
1353 /* need to move the callback at the end, in case other
1354 * overlays are present, so that the final entry is
1355 * actually cached */
1357 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
1362 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE\n",
1368 return SLAP_CB_CONTINUE;
1373 AttributeName* attrs_in,
1374 AttributeName* attrs)
1376 int i, count1, count2;
1377 if ( attrs_in == NULL ) {
1378 return (attrs ? 0 : 1);
1380 if ( attrs == NULL )
1384 attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1388 attrs && attrs[count2].an_name.bv_val != NULL;
1391 if ( count1 != count2 )
1394 for ( i=0; i<count1; i++ ) {
1395 if ( !an_find(attrs, &attrs_in[i].an_name ))
1403 AttributeName* attrs,
1408 for (i=0; i<num; i++) {
1409 if (attrscmp(attrs, qm->attr_sets[i].attrs))
1420 struct re_s *rtask = arg;
1421 slap_overinst *on = rtask->arg;
1422 cache_manager *cm = on->on_bi.bi_private;
1423 query_manager *qm = cm->qm;
1424 Connection conn = {0};
1425 char opbuf[OPERATION_BUFFER_SIZE];
1428 SlapReply rs = {REP_RESULT};
1429 CachedQuery* query, *query_prev;
1430 int i, return_val, pause = 1;
1431 QueryTemplate* templ;
1433 op = (Operation *)opbuf;
1434 connection_fake_init( &conn, op, ctx );
1437 op->o_dn = cm->db.be_rootdn;
1438 op->o_ndn = cm->db.be_rootndn;
1442 for (i=0; qm->templates[i].querystr.bv_val; i++) {
1443 templ = qm->templates + i;
1444 query = templ->query_last;
1445 if ( query ) pause = 0;
1446 op->o_time = slap_get_time();
1447 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1448 while (query && (query->expiry_time < op->o_time)) {
1449 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1450 remove_query(qm, query);
1451 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1452 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1454 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1455 remove_from_template(query, templ);
1456 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1457 i, templ->no_of_queries, 0 );
1458 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1460 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1461 return_val = remove_query_data(op, &rs, &query->q_uuid);
1462 Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1464 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1465 cm->cur_entries -= return_val;
1466 cm->num_cached_queries--;
1467 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1468 cm->num_cached_queries, 0, 0 );
1469 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1470 Debug( LDAP_DEBUG_ANY,
1471 "STALE QUERY REMOVED, CACHE ="
1473 cm->cur_entries, 0, 0 );
1475 query = query->prev;
1476 free_query(query_prev);
1478 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1480 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1481 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1482 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1484 /* If there were no queries, defer processing for a while */
1485 cm->cc_paused = pause;
1486 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
1488 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1493 #define MAX_ATTR_SETS 500
1496 * compares two sets of attributes (indices i and j)
1497 * returns 0: if neither set is contained in the other set
1498 * 1: if set i is contained in set j
1499 * 2: if set j is contained in set i
1500 * 3: the sets are equivalent
1504 compare_sets(struct attr_set* set, int i, int j)
1510 if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
1513 if ( set[i].attrs == NULL )
1516 if ( set[j].attrs == NULL )
1519 numI = set[i].count;
1520 numJ = set[j].count;
1522 for ( l=0; l < numI; l++ ) {
1523 for ( k = 0; k < numJ; k++ ) {
1524 if ( strcmp( set[i].attrs[l].an_name.bv_val,
1525 set[j].attrs[k].an_name.bv_val ) == 0 )
1530 if ( common == numI )
1533 if ( common == numJ )
1540 find_supersets ( struct attr_set* attr_sets, int numsets )
1542 int num[MAX_ATTR_SETS];
1545 for ( i = 0; i < MAX_ATTR_SETS; i++ )
1548 for ( i = 0; i < numsets; i++ ) {
1549 attr_sets[i].ID_array = (int*) ch_malloc( sizeof( int ) );
1550 attr_sets[i].ID_array[0] = -1;
1553 for ( i = 0; i < numsets; i++ ) {
1554 for ( j=i+1; j < numsets; j++ ) {
1555 res = compare_sets( attr_sets, i, j );
1561 id_array = attr_sets[i].ID_array;
1562 attr_sets[i].ID_array = (int *) ch_realloc( id_array,
1563 ( num[i] + 2 ) * sizeof( int ));
1564 attr_sets[i].ID_array[num[i]] = j;
1565 attr_sets[i].ID_array[num[i]+1] = -1;
1570 id_array = attr_sets[j].ID_array;
1571 attr_sets[j].ID_array = (int *) ch_realloc( id_array,
1572 ( num[j] + 2 ) * sizeof( int ));
1573 attr_sets[j].ID_array[num[j]] = i;
1574 attr_sets[j].ID_array[num[j]+1] = -1;
1589 static ConfigDriver pc_cf_gen;
1590 static ConfigLDAPadd pc_ldadd;
1591 static ConfigCfAdd pc_cfadd;
1593 static ConfigTable pccfg[] = {
1594 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
1596 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
1597 "( OLcfgOvAt:2.1 NAME 'olcProxyCache' "
1598 "DESC 'ProxyCache basic parameters' "
1599 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
1600 { "proxyattrset", "index> <attributes...",
1601 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
1602 "( OLcfgOvAt:2.2 NAME 'olcProxyAttrset' "
1603 "DESC 'A set of attributes to cache' "
1604 "SYNTAX OMsDirectoryString )", NULL, NULL },
1605 { "proxytemplate", "filter> <attrset-index> <TTL",
1606 4, 4, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
1607 "( OLcfgOvAt:2.3 NAME 'olcProxyTemplate' "
1608 "DESC 'Filter template, attrset, and cache TTL' "
1609 "SYNTAX OMsDirectoryString )", NULL, NULL },
1610 { "response-callback", "head|tail(default)",
1611 2, 2, 0, ARG_MAGIC|ARG_STRING|PC_RESP, pc_cf_gen,
1612 "( OLcfgOvAt:2.4 NAME 'olcProxyResponseCB' "
1613 "DESC 'Response callback position in overlay stack' "
1614 "SYNTAX OMsDirectoryString )", NULL, NULL },
1615 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1618 static ConfigOCs pcocs[] = {
1619 { "( OLcfgOvOc:2.1 "
1620 "NAME 'olcPcacheConfig' "
1621 "DESC 'ProxyCache configuration' "
1622 "SUP olcOverlayConfig "
1623 "MUST ( olcProxyCache $ olcProxyAttrset $ olcProxyTemplate ) "
1624 "MAY olcProxyResponseCB )", Cft_Overlay, pccfg, NULL, pc_cfadd },
1625 { "( OLcfgOvOc:2.2 "
1626 "NAME 'olcPcacheDatabase' "
1627 "DESC 'Cache database configuration' "
1628 "AUXILIARY )", Cft_Misc, pccfg, pc_ldadd },
1633 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
1638 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
1639 p->ce_bi->bi_cf_ocs != pcocs )
1640 return LDAP_CONSTRAINT_VIOLATION;
1642 on = (slap_overinst *)p->ce_bi;
1643 cm = on->on_bi.bi_private;
1645 return LDAP_SUCCESS;
1649 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
1651 CfEntryInfo *pe = p->e_private;
1652 slap_overinst *on = (slap_overinst *)pe->ce_bi;
1653 cache_manager *cm = on->on_bi.bi_private;
1656 /* FIXME: should not hardcode "olcDatabase" here */
1657 bv.bv_len = sprintf( ca->msg, "olcDatabase=%s", cm->db.bd_info->bi_type );
1658 bv.bv_val = ca->msg;
1661 /* We can only create this entry if the database is table-driven
1663 if ( cm->db.bd_info->bi_cf_ocs )
1664 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
1671 pc_cf_gen( ConfigArgs *c )
1673 slap_overinst *on = (slap_overinst *)c->bi;
1674 cache_manager* cm = on->on_bi.bi_private;
1675 query_manager* qm = cm->qm;
1676 QueryTemplate* temp;
1677 AttributeName* attr_name;
1678 AttributeName* attrarray;
1679 const char* text=NULL;
1683 if ( c->op == SLAP_CONFIG_EMIT ) {
1687 bv.bv_len = sprintf( c->msg, "%s %d %d %d %d",
1688 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
1689 cm->num_entries_limit, cm->cc_period );
1691 value_add_one( &c->rvalue_vals, &bv );
1694 for (i=0; i<cm->numattrsets; i++) {
1695 if ( !qm->attr_sets[i].count ) continue;
1697 bv.bv_len = sprintf( c->msg, "%d", i );
1699 /* count the attr length */
1700 for ( attr_name = qm->attr_sets[i].attrs;
1701 attr_name->an_name.bv_val; attr_name++ )
1702 bv.bv_len += attr_name->an_name.bv_len + 1;
1704 bv.bv_val = ch_malloc( bv.bv_len+1 );
1705 ptr = lutil_strcopy( bv.bv_val, c->msg );
1706 for ( attr_name = qm->attr_sets[i].attrs;
1707 attr_name->an_name.bv_val; attr_name++ ) {
1709 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
1711 ber_bvarray_add( &c->rvalue_vals, &bv );
1713 if ( !c->rvalue_vals )
1717 for (i=0; i<cm->numtemplates; i++) {
1718 bv.bv_len = sprintf( c->msg, " %d %d",
1719 qm->templates[i].attr_set_index,
1720 qm->templates[i].ttl );
1721 bv.bv_len += qm->templates[i].querystr.bv_len + 2;
1722 bv.bv_val = ch_malloc( bv.bv_len+1 );
1725 ptr = lutil_strcopy( ptr, qm->templates[i].querystr.bv_val );
1727 strcpy( ptr, c->msg );
1728 ber_bvarray_add( &c->rvalue_vals, &bv );
1730 if ( !c->rvalue_vals )
1734 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1736 bv.bv_len = STRLENOF("head");
1739 bv.bv_len = STRLENOF("tail");
1741 value_add_one( &c->rvalue_vals, &bv );
1745 } else if ( c->op == LDAP_MOD_DELETE ) {
1746 return 1; /* FIXME */
1758 cm->numattrsets = atoi( c->argv[3] );
1759 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1760 sprintf( c->msg, "numattrsets must be <= %d", MAX_ATTR_SETS );
1761 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1764 cm->db.bd_info = backend_info( c->argv[1] );
1765 if ( !cm->db.bd_info ) {
1766 sprintf( c->msg, "unknown backend type" );
1767 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1770 if ( cm->db.bd_info->bi_db_init( &cm->db ) ) {
1771 sprintf( c->msg, "backend %s init failed", c->argv[1] );
1772 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1776 /* This type is in use, needs to be opened */
1777 cm->db.bd_info->bi_nDB++;
1779 cm->max_entries = atoi( c->argv[2] );
1781 cm->num_entries_limit = atoi( c->argv[4] );
1782 cm->cc_period = atoi( c->argv[5] );
1783 Debug( LDAP_DEBUG_TRACE,
1784 "Total # of attribute sets to be cached = %d\n",
1785 cm->numattrsets, 0, 0 );
1786 qm->attr_sets = ( struct attr_set * )ch_malloc( cm->numattrsets *
1787 sizeof( struct attr_set ));
1788 for ( i = 0; i < cm->numattrsets; i++ ) {
1789 qm->attr_sets[i].attrs = NULL;
1793 num = atoi( c->argv[1] );
1794 if (num >= cm->numattrsets) {
1795 sprintf( c->msg, "attrset index out of bounds" );
1796 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1799 if ( c->argv[2] && strcmp( c->argv[2], "*" ) ) {
1800 qm->attr_sets[num].count = c->argc - 2;
1801 qm->attr_sets[num].attrs = (AttributeName*)ch_malloc(
1802 (c->argc-1) * sizeof( AttributeName ));
1803 attr_name = qm->attr_sets[num].attrs;
1804 for ( i = 2; i < c->argc; i++ ) {
1805 ber_str2bv( c->argv[i], 0, 1, &attr_name->an_name);
1806 attr_name->an_desc = NULL;
1807 if ( slap_bv2ad( &attr_name->an_name,
1808 &attr_name->an_desc, &text )) {
1809 strcpy( c->msg, text );
1810 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1811 ch_free( qm->attr_sets[num].attrs );
1812 qm->attr_sets[num].attrs = NULL;
1813 qm->attr_sets[num].count = 0;
1816 attr_name->an_oc = NULL;
1817 attr_name->an_oc_exclude = 0;
1819 BER_BVZERO( &attr_name->an_name );
1824 if (( i = atoi( c->argv[2] )) >= cm->numattrsets ) {
1825 sprintf( c->msg, "template index invalid" );
1826 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1829 num = cm->numtemplates;
1831 find_supersets( qm->attr_sets, cm->numattrsets );
1832 qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
1833 ( num + 2 ) * sizeof( QueryTemplate ));
1834 temp = qm->templates + num;
1835 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
1836 temp->query = temp->query_last = NULL;
1837 temp->ttl = atoi( c->argv[3] );
1838 temp->no_of_queries = 0;
1840 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
1841 Debug( LDAP_DEBUG_TRACE, "Template:\n", 0, 0, 0 );
1842 Debug( LDAP_DEBUG_TRACE, " query template: %s\n",
1843 temp->querystr.bv_val, 0, 0 );
1844 temp->attr_set_index = i;
1845 Debug( LDAP_DEBUG_TRACE, " attributes: \n", 0, 0, 0 );
1846 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1847 for ( i=0; attrarray[i].an_name.bv_val; i++ )
1848 Debug( LDAP_DEBUG_TRACE, "\t%s\n",
1849 attrarray[i].an_name.bv_val, 0, 0 );
1852 temp->querystr.bv_val = NULL;
1856 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
1857 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
1859 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
1860 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1863 sprintf( c->msg, "unknown specifier" );
1864 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1881 slap_overinst *on = (slap_overinst *)be->bd_info;
1882 cache_manager* cm = on->on_bi.bi_private;
1884 /* Something for the cache database? */
1885 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
1886 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
1888 return SLAP_CONF_UNKNOWN;
1896 slap_overinst *on = (slap_overinst *)be->bd_info;
1900 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1901 on->on_bi.bi_private = cm;
1903 qm = (query_manager*)ch_malloc(sizeof(query_manager));
1906 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1907 cm->db.be_private = NULL;
1908 cm->db.be_pcl_mutexp = &cm->db.be_pcl_mutex;
1910 cm->numattrsets = 0;
1911 cm->numtemplates = 0;
1912 cm->num_entries_limit = 5;
1913 cm->num_cached_queries = 0;
1914 cm->max_entries = 0;
1915 cm->cur_entries = 0;
1916 cm->max_queries = 10000;
1917 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1918 cm->cc_period = 1000;
1921 qm->attr_sets = NULL;
1922 qm->templates = NULL;
1924 qm->lru_bottom = NULL;
1926 qm->qcfunc = query_containment;
1927 qm->crfunc = cache_replacement;
1928 qm->addfunc = add_query;
1929 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1931 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1932 ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1941 slap_overinst *on = (slap_overinst *)be->bd_info;
1942 cache_manager *cm = on->on_bi.bi_private;
1945 /* need to inherit something from the original database... */
1946 cm->db.be_def_limit = be->be_def_limit;
1947 cm->db.be_limits = be->be_limits;
1948 cm->db.be_acl = be->be_acl;
1949 cm->db.be_dfltaccess = be->be_dfltaccess;
1951 rc = backend_startup_one( &cm->db );
1953 /* There is no runqueue in TOOL mode */
1954 if ( slapMode & SLAP_SERVER_MODE ) {
1955 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1956 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
1957 consistency_check, on,
1958 "pcache_consistency", be->be_suffix[0].bv_val );
1959 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1961 /* Cached database must have the rootdn */
1962 if ( BER_BVISNULL( &cm->db.be_rootndn )
1963 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
1965 Debug( LDAP_DEBUG_ANY, "proxy_cache_open(): "
1966 "underlying database of type \"%s\"\n"
1967 " serving naming context \"%s\"\n"
1968 " has no \"rootdn\", required by \"proxycache\".\n",
1969 on->on_info->oi_orig->bi_type,
1970 cm->db.be_suffix[0].bv_val, 0 );
1983 slap_overinst *on = (slap_overinst *)be->bd_info;
1984 cache_manager *cm = on->on_bi.bi_private;
1985 query_manager *qm = cm->qm;
1988 /* cleanup stuff inherited from the original database... */
1989 cm->db.be_limits = NULL;
1990 cm->db.be_acl = NULL;
1992 if ( cm->db.bd_info->bi_db_close ) {
1993 rc = cm->db.bd_info->bi_db_close( &cm->db );
1995 for ( i=0; i<cm->numtemplates; i++ ) {
1996 CachedQuery *qc, *qn;
1997 for ( qc = qm->templates[i].query; qc; qc = qn ) {
2001 free( qm->templates[i].querystr.bv_val );
2002 ldap_pvt_thread_rdwr_destroy( &qm->templates[i].t_rwlock );
2004 free( qm->templates );
2005 qm->templates = NULL;
2007 for ( i=0; i<cm->numattrsets; i++ ) {
2008 free( qm->attr_sets[i].ID_array );
2009 for ( j=0; j<qm->attr_sets[i].count; j++ ) {
2010 free( qm->attr_sets[i].attrs[j].an_name.bv_val );
2012 free( qm->attr_sets[i].attrs );
2014 free( qm->attr_sets );
2015 qm->attr_sets = NULL;
2021 proxy_cache_destroy(
2025 slap_overinst *on = (slap_overinst *)be->bd_info;
2026 cache_manager *cm = on->on_bi.bi_private;
2027 query_manager *qm = cm->qm;
2030 if ( cm->db.bd_info->bi_db_destroy ) {
2031 rc = cm->db.bd_info->bi_db_destroy( &cm->db );
2033 ldap_pvt_thread_mutex_destroy(&qm->lru_mutex);
2034 ldap_pvt_thread_mutex_destroy(&cm->cache_mutex);
2035 ldap_pvt_thread_mutex_destroy(&cm->remove_mutex);
2041 static slap_overinst proxy_cache;
2045 LDAPAttributeType *at;
2049 at = ldap_str2attributetype( queryid_schema, &code, &err,
2050 LDAP_SCHEMA_ALLOW_ALL );
2052 Debug( LDAP_DEBUG_ANY,
2053 "pcache_init: ldap_str2attributetype failed %s %s\n",
2054 ldap_scherr2str(code), err, 0 );
2057 code = at_add( at, 0, NULL, &err );
2059 slap_str2ad( at->at_names[0], &ad_queryid, &err );
2063 Debug( LDAP_DEBUG_ANY,
2064 "pcache_init: at_add failed %s %s\n",
2065 scherr2str(code), err, 0 );
2069 proxy_cache.on_bi.bi_type = "proxycache";
2070 proxy_cache.on_bi.bi_db_init = proxy_cache_init;
2071 proxy_cache.on_bi.bi_db_config = proxy_cache_config;
2072 proxy_cache.on_bi.bi_db_open = proxy_cache_open;
2073 proxy_cache.on_bi.bi_db_close = proxy_cache_close;
2074 proxy_cache.on_bi.bi_db_destroy = proxy_cache_destroy;
2075 proxy_cache.on_bi.bi_op_search = proxy_cache_search;
2077 proxy_cache.on_bi.bi_cf_ocs = pcocs;
2079 code = config_register_schema( pccfg, pcocs );
2080 if ( code ) return code;
2082 return overlay_register( &proxy_cache );
2085 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
2086 int init_module(int argc, char *argv[]) {
2087 return pcache_init();
2091 #endif /* defined(SLAPD_OVER_PROXYCACHE) */