2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2004 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
6 * Portions Copyright 2003 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Apurva Kumar for inclusion
19 * in OpenLDAP Software and subsequently rewritten by Howard Chu.
24 #ifdef SLAPD_OVER_PROXYCACHE
28 #include <ac/string.h>
36 /* query cache structs */
39 typedef struct Query_s {
40 Filter* filter; /* Search Filter */
41 AttributeName* attrs; /* Projected attributes */
42 AttributeName* save_attrs; /* original attributes, saved for response */
43 struct berval base; /* Search Base */
44 int scope; /* Search scope */
47 /* struct representing a cached query */
48 typedef struct cached_query_s {
49 Query query; /* LDAP query */
50 struct berval q_uuid; /* query identifier */
51 int template_id; /* template of the query */
52 time_t expiry_time; /* time till the query is considered valid */
53 struct cached_query_s *next; /* next query in the template */
54 struct cached_query_s *prev; /* previous query in the template */
55 struct cached_query_s *lru_up; /* previous query in the LRU list */
56 struct cached_query_s *lru_down; /* next query in the LRU list */
59 /* struct representing a query template
60 * e.g. template string = &(cn=)(mail=)
62 typedef struct query_template_s {
63 struct berval querystr; /* Filter string corresponding to the QT */
64 int attr_set_index; /* determines the projected attributes */
66 CachedQuery* query; /* most recent query cached for the template */
67 CachedQuery* query_last; /* oldest query cached for the template */
69 int no_of_queries; /* Total number of queries in the template */
70 long ttl; /* TTL for the queries of this template */
71 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
75 * Represents a set of projected attributes and any
76 * supersets among all specified sets of attributes.
80 AttributeName* attrs; /* specifies the set */
81 int count; /* number of attributes */
82 int* ID_array; /* array of indices of supersets of 'attrs' */
85 struct query_manager_s;
87 /* prototypes for functions for 1) query containment
88 * 2) query addition, 3) cache replacement
90 typedef int (QCfunc)(struct query_manager_s*, Query*, int );
91 typedef void (AddQueryfunc)(struct query_manager_s*, Query*, int, struct berval*);
92 typedef void (CRfunc)(struct query_manager_s*, struct berval * );
94 /* LDAP query cache */
95 typedef struct query_manager_s {
96 struct attr_set* attr_sets; /* possible sets of projected attributes */
97 QueryTemplate* templates; /* cacheable templates */
99 CachedQuery* lru_top; /* top and bottom of LRU list */
100 CachedQuery* lru_bottom;
102 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
104 /* Query cache methods */
105 QCfunc *qcfunc; /* Query containment*/
106 CRfunc *crfunc; /* cache replacement */
107 AddQueryfunc *addfunc; /* add query */
110 /* LDAP query cache manager */
111 typedef struct cache_manager_s {
112 BackendDB db; /* underlying database */
113 unsigned long num_cached_queries; /* total number of cached queries */
114 unsigned long max_queries; /* upper bound on # of cached queries */
115 int numattrsets; /* number of attribute sets */
116 int numtemplates; /* number of cacheable templates */
117 int cur_entries; /* current number of entries cached */
118 int max_entries; /* max number of entries cached */
119 int num_entries_limit; /* max # of entries in a cacheable query */
121 char response_cb; /* install the response callback
122 * at the tail of the callback list */
123 #define PCACHE_RESPONSE_CB_HEAD 0
124 #define PCACHE_RESPONSE_CB_TAIL 1
126 int cc_period; /* interval between successive consistency checks (sec) */
130 ldap_pvt_thread_mutex_t cache_mutex;
131 ldap_pvt_thread_mutex_t remove_mutex;
133 query_manager* qm; /* query cache managed by the cache manager */
136 static AttributeDescription *ad_queryid;
137 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
138 "DESC 'list of queries the entry belongs to' "
139 "EQUALITY octetStringMatch "
140 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
141 "NO-USER-MODIFICATION USAGE directoryOperation )";
143 /* Return 1 for an added entry, else 0 */
148 struct berval* query_uuid )
151 Modifications* modlist = NULL;
152 const char* text = NULL;
154 char textbuf[SLAP_TEXT_BUFLEN];
155 size_t textlen = sizeof(textbuf);
157 SlapReply sreply = {REP_RESULT};
159 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
164 /* add queryid attribute */
165 attr_merge_one( e, ad_queryid, query_uuid, NULL );
167 /* append the attribute list from the fetched entry */
168 e->e_attrs->a_next = attr;
170 op->o_tag = LDAP_REQ_ADD;
171 op->o_protocol = LDAP_VERSION3;
172 op->o_callback = &cb;
173 op->o_time = slap_get_time();
174 op->o_do_not_cache = 1;
177 op->o_req_dn = e->e_name;
178 op->o_req_ndn = e->e_nname;
179 rc = op->o_bd->be_add( op, &sreply );
181 if ( rc != LDAP_SUCCESS ) {
182 if ( rc == LDAP_ALREADY_EXISTS ) {
183 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
184 modlist->sml_op = LDAP_MOD_ADD;
185 op->o_tag = LDAP_REQ_MODIFY;
186 op->orm_modlist = modlist;
187 op->o_bd->be_modify( op, &sreply );
188 slap_mods_free( modlist );
189 } else if ( rc == LDAP_REFERRAL ||
190 rc == LDAP_NO_SUCH_OBJECT ) {
191 syncrepl_add_glue( op, e );
200 be_entry_release_w( op, e );
207 /* compare base and scope of incoming and cached queries */
208 static int base_scope_compare(
209 struct berval* ndn_stored,
210 struct berval* ndn_incoming,
214 struct berval pdn_incoming = BER_BVNULL;
216 if (scope_stored < scope_incoming)
219 if ( !dnIsSuffix(ndn_incoming, ndn_stored))
222 switch(scope_stored) {
223 case LDAP_SCOPE_BASE:
224 return (ndn_incoming->bv_len == ndn_stored->bv_len);
226 case LDAP_SCOPE_ONELEVEL:
227 switch(scope_incoming){
228 case LDAP_SCOPE_BASE:
229 dnParent(ndn_incoming, &pdn_incoming);
230 return (pdn_incoming.bv_len == ndn_stored->bv_len);
232 case LDAP_SCOPE_ONELEVEL:
233 return (ndn_incoming->bv_len == ndn_stored->bv_len);
238 case LDAP_SCOPE_SUBTREE:
247 /* add query on top of LRU list */
249 add_query_on_top (query_manager* qm, CachedQuery* qc)
251 CachedQuery* top = qm->lru_top;
252 Query* q = (Query*)qc;
264 LDAP_LOG( BACK_META, DETAIL1, "Base of added query = %s\n",
265 q->base.bv_val, 0, 0 );
267 Debug( LDAP_DEBUG_ANY, "Base of added query = %s\n",
268 q->base.bv_val, 0, 0 );
272 /* remove_query from LRU list */
275 remove_query (query_manager* qm, CachedQuery* qc)
298 qc->lru_up = qc->lru_down = NULL;
302 invert_string( struct berval *bv )
307 for (i=0; i<bv->bv_len/2; i++) {
309 bv->bv_val[i] = bv->bv_val[bv->bv_len-i-1];
310 bv->bv_val[bv->bv_len-i-1] = c;
314 /* find and remove string2 from string1
315 * from start if position = 1,
316 * from end if position = 3,
317 * from anywhere if position = 2
321 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
328 char* string1=ber1->bv_val;
329 char* string2=ber2->bv_val;
344 temp = strstr(arg1, arg2);
348 if ( temp && (position == 2 || temp == arg1) ) {
350 strcpy( arg1, string1 );
354 if ( position == 3 ) {
362 static struct berval*
363 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
365 struct berval* merged, *temp;
366 int i, any_count, count;
368 for (any_count=0; any && any[any_count].bv_val; any_count++)
378 merged = (struct berval*)(ch_malloc((count+1)*sizeof(struct berval)));
385 for (i=0; i<any_count; i++) {
398 strings_containment(struct berval* stored, struct berval* incoming)
400 struct berval* element;
404 for ( element=stored; element->bv_val != NULL; element++ ) {
405 for (j = k; incoming[j].bv_val != NULL; j++) {
406 if (find_and_remove(&(incoming[j]), element, 2)) {
423 substr_containment_substr(Filter* stored, Filter* incoming)
429 struct berval init_incoming;
430 struct berval final_incoming;
431 struct berval *any_incoming = NULL;
432 struct berval *remaining_incoming = NULL;
434 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
435 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
439 ber_dupbv(&init_incoming, &(incoming->f_sub_initial));
440 ber_dupbv(&final_incoming, &(incoming->f_sub_final));
442 if (incoming->f_sub_any) {
443 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
447 any_incoming = (struct berval*)ch_malloc((any_count+1) *
448 sizeof(struct berval));
450 for (i=0; i<any_count; i++) {
451 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
453 any_incoming[any_count].bv_val = NULL;
454 any_incoming[any_count].bv_len = 0;
457 if (find_and_remove(&init_incoming,
458 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
459 &(stored->f_sub_final), 3))
461 if (stored->f_sub_any == NULL) {
465 remaining_incoming = merge_init_final(&init_incoming,
466 any_incoming, &final_incoming);
467 rc = strings_containment(stored->f_sub_any, remaining_incoming);
470 free(init_incoming.bv_val);
471 free(final_incoming.bv_val);
472 if (any_incoming) ber_bvarray_free( any_incoming );
473 free(remaining_incoming);
479 substr_containment_equality(Filter* stored, Filter* incoming)
481 struct berval incoming_val[2];
484 ber_dupbv(incoming_val, &(incoming->f_av_value));
485 incoming_val[1].bv_val = NULL;
486 incoming_val[1].bv_len = 0;
488 if (find_and_remove(incoming_val,
489 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
490 &(stored->f_sub_final), 3)) {
491 if (stored->f_sub_any == NULL){
495 rc = strings_containment(stored->f_sub_any, incoming_val);
498 free(incoming_val[0].bv_val);
502 /* check whether query is contained in any of
503 * the cached queries in template template_index
506 query_containment(query_manager *qm,
510 QueryTemplate* templa= qm->templates;
513 Filter* inputf = query->filter;
514 struct berval* base = &(query->base);
515 int scope = query->scope;
522 MatchingRule* mrule = NULL;
523 if (inputf != NULL) {
525 LDAP_LOG( BACK_META, DETAIL1, "Lock QC index = %d\n",
526 template_index, 0, 0 );
528 Debug( LDAP_DEBUG_ANY, "Lock QC index = %d\n",
529 template_index, 0, 0 );
531 ldap_pvt_thread_rdwr_rlock(&(templa[template_index].t_rwlock));
532 for(qc=templa[template_index].query; qc != NULL; qc= qc->next) {
534 if(base_scope_compare(&(q->base), base, q->scope, scope)) {
539 switch (fs->f_choice) {
540 case LDAP_FILTER_EQUALITY:
541 if (fi->f_choice == LDAP_FILTER_EQUALITY)
542 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
548 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
554 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
555 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
556 &(fi->f_ava->aa_value),
557 &(fs->f_ava->aa_value), &text);
558 if (rc != LDAP_SUCCESS) {
559 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
561 LDAP_LOG( BACK_META, DETAIL1,
562 "Unlock: Exiting QC index=%d\n",
563 template_index, 0, 0 );
565 Debug( LDAP_DEBUG_ANY,
566 "Unlock: Exiting QC index=%d\n",
567 template_index, 0, 0 );
572 switch (fs->f_choice) {
574 case LDAP_FILTER_AND:
579 case LDAP_FILTER_SUBSTRINGS:
580 /* check if the equality query can be
581 * answered with cached substring query */
582 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
583 && substr_containment_equality(
586 /* check if the substring query can be
587 * answered with cached substring query */
588 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
589 ) && substr_containment_substr(
595 case LDAP_FILTER_PRESENT:
600 case LDAP_FILTER_EQUALITY:
618 case LDAP_FILTER_NOT:
624 } while((res) && (fi != NULL) && (fs != NULL));
627 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
628 if (qm->lru_top != qc) {
629 remove_query(qm, qc);
630 add_query_on_top(qm, qc);
632 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
638 LDAP_LOG( BACK_META, DETAIL1,
639 "Not answerable: Unlock QC index=%d\n",
640 template_index, 0, 0 );
642 Debug( LDAP_DEBUG_ANY,
643 "Not answerable: Unlock QC index=%d\n",
644 template_index, 0, 0 );
646 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
652 free_query (CachedQuery* qc)
654 Query* q = (Query*)qc;
657 free(qc->q_uuid.bv_val);
658 filter_free(q->filter);
659 free (q->base.bv_val);
660 for (i=0; q->attrs[i].an_name.bv_val; i++) {
661 free(q->attrs[i].an_name.bv_val);
668 /* Add query to query cache */
669 static void add_query(
675 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
676 QueryTemplate* templ = (qm->templates)+template_index;
678 new_cached_query->template_id = template_index;
679 new_cached_query->q_uuid = *uuid;
680 new_cached_query->lru_up = NULL;
681 new_cached_query->lru_down = NULL;
682 new_cached_query->expiry_time = slap_get_time() + templ->ttl;
684 LDAP_LOG( BACK_META, DETAIL1, "Added query expires at %ld\n",
685 (long) new_cached_query->expiry_time, 0, 0 );
687 Debug( LDAP_DEBUG_ANY, "Added query expires at %ld\n",
688 (long) new_cached_query->expiry_time, 0, 0 );
690 new_query = (Query*)new_cached_query;
692 ber_dupbv(&new_query->base, &query->base);
693 new_query->scope = query->scope;
694 new_query->filter = query->filter;
695 new_query->attrs = query->attrs;
699 LDAP_LOG( BACK_META, DETAIL1, "Lock AQ index = %d\n",
700 template_index, 0, 0 );
702 Debug( LDAP_DEBUG_ANY, "Lock AQ index = %d\n",
703 template_index, 0, 0 );
705 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
706 if (templ->query == NULL)
707 templ->query_last = new_cached_query;
709 templ->query->prev = new_cached_query;
710 new_cached_query->next = templ->query;
711 new_cached_query->prev = NULL;
712 templ->query = new_cached_query;
713 templ->no_of_queries++;
715 LDAP_LOG( BACK_META, DETAIL1, "TEMPLATE %d QUERIES++ %d\n",
716 template_index, templ->no_of_queries, 0 );
718 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES++ %d\n",
719 template_index, templ->no_of_queries, 0 );
723 LDAP_LOG( BACK_META, DETAIL1, "Unlock AQ index = %d \n",
724 template_index, 0, 0 );
726 Debug( LDAP_DEBUG_ANY, "Unlock AQ index = %d \n",
727 template_index, 0, 0 );
729 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
731 /* Adding on top of LRU list */
732 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
733 add_query_on_top(qm, new_cached_query);
734 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
738 remove_from_template (CachedQuery* qc, QueryTemplate* template)
740 if (!qc->prev && !qc->next) {
741 template->query_last = template->query = NULL;
742 } else if (qc->prev == NULL) {
743 qc->next->prev = NULL;
744 template->query = qc->next;
745 } else if (qc->next == NULL) {
746 qc->prev->next = NULL;
747 template->query_last = qc->prev;
749 qc->next->prev = qc->prev;
750 qc->prev->next = qc->next;
753 template->no_of_queries--;
756 /* remove bottom query of LRU list from the query cache */
757 static void cache_replacement(query_manager* qm, struct berval *result)
762 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
763 bottom = qm->lru_bottom;
765 result->bv_val = NULL;
770 LDAP_LOG ( BACK_META, DETAIL1,
771 "Cache replacement invoked without "
772 "any query in LRU list\n", 0, 0, 0 );
774 Debug ( LDAP_DEBUG_ANY,
775 "Cache replacement invoked without "
776 "any query in LRU list\n", 0, 0, 0 );
781 temp_id = bottom->template_id;
782 remove_query(qm, bottom);
783 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
785 *result = bottom->q_uuid;
786 bottom->q_uuid.bv_val = NULL;
789 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n", temp_id, 0, 0 );
791 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n", temp_id, 0, 0 );
793 ldap_pvt_thread_rdwr_wlock(&(qm->templates[temp_id].t_rwlock));
794 remove_from_template(bottom, (qm->templates+temp_id));
796 LDAP_LOG( BACK_META, DETAIL1, "TEMPLATE %d QUERIES-- %d\n",
797 temp_id, qm->templates[temp_id].no_of_queries, 0 );
799 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
800 temp_id, qm->templates[temp_id].no_of_queries, 0 );
803 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n", temp_id, 0, 0 );
805 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n", temp_id, 0, 0 );
807 ldap_pvt_thread_rdwr_wunlock(&(qm->templates[temp_id].t_rwlock));
812 struct query_info *next;
824 struct query_info *qi;
827 if ( rs->sr_type != REP_SEARCH ) return 0;
829 for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
830 if (attr->a_desc == ad_queryid) {
831 for (count=0; attr->a_vals[count].bv_val; count++)
836 if ( count == 0 ) return 0;
837 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
838 qi->next = op->o_callback->sc_private;
839 op->o_callback->sc_private = qi;
840 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
841 qi->del = ( count == 1 );
850 struct berval* query_uuid)
852 struct query_info *qi, *qnext;
854 AttributeAssertion ava;
855 Filter filter = {LDAP_FILTER_EQUALITY};
856 SlapReply sreply = {REP_RESULT};
857 slap_callback cb = { NULL, remove_func, NULL, NULL };
860 sreply.sr_entry = NULL;
861 sreply.sr_nentries = 0;
862 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
863 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
865 filter.f_av_desc = ad_queryid;
866 filter.f_av_value = *query_uuid;
868 op->o_tag = LDAP_REQ_SEARCH;
869 op->o_protocol = LDAP_VERSION3;
870 op->o_callback = &cb;
871 op->o_time = slap_get_time();
872 op->o_do_not_cache = 1;
874 op->o_req_dn = op->o_bd->be_suffix[0];
875 op->o_req_ndn = op->o_bd->be_nsuffix[0];
876 op->ors_scope = LDAP_SCOPE_SUBTREE;
877 op->ors_deref = LDAP_DEREF_NEVER;
878 op->ors_slimit = SLAP_NO_LIMIT;
879 op->ors_tlimit = SLAP_NO_LIMIT;
880 op->ors_filter = &filter;
881 op->ors_filterstr.bv_val = filter_str;
882 op->ors_filterstr.bv_len = strlen(filter_str);
883 op->ors_attrs = NULL;
884 op->ors_attrsonly = 0;
886 op->o_bd->be_search( op, &sreply );
888 for ( qi=cb.sc_private; qi; qi=qnext ) {
891 op->o_req_dn = qi->xdn;
892 op->o_req_ndn = qi->xdn;
896 LDAP_LOG( BACK_META, DETAIL1,
897 "DELETING ENTRY TEMPLATE=%s\n",
898 query_uuid->bv_val, 0, 0 );
900 Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
901 query_uuid->bv_val, 0, 0 );
904 op->o_tag = LDAP_REQ_DELETE;
906 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
911 struct berval vals[2];
913 vals[0] = *query_uuid;
914 vals[1].bv_val = NULL;
916 mod.sml_op = LDAP_MOD_DELETE;
917 mod.sml_desc = ad_queryid;
918 mod.sml_type = ad_queryid->ad_cname;
919 mod.sml_values = vals;
920 mod.sml_nvalues = NULL;
923 LDAP_LOG( BACK_META, DETAIL1,
924 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
925 query_uuid->bv_val, 0, 0 );
927 Debug( LDAP_DEBUG_ANY,
928 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
929 query_uuid->bv_val, 0, 0 );
932 op->orm_modlist = &mod;
934 op->o_bd->be_modify( op, &sreply );
936 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
937 op->o_tmpfree( qi, op->o_tmpmemctx );
944 AttributeName* attrs,
951 AttributeName* attrs_in,
958 struct berval* tempstr,
962 QueryTemplate *qt = qm->templates + template_id;
964 if (attr_set != qt->attr_set_index) {
965 int* id_array = qm->attr_sets[attr_set].ID_array;
967 while (*id_array != -1) {
968 if (*id_array == qt->attr_set_index)
975 return (qt->querystr.bv_len == tempstr->bv_len &&
976 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
983 AttributeName** filter_attrs,
986 AttributeDescription *ad;
988 switch ( f->f_choice ) {
989 case LDAP_FILTER_EQUALITY:
991 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
992 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
997 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
998 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
1001 case LDAP_FILTER_LE:
1003 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
1004 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
1007 case LDAP_FILTER_APPROX:
1009 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
1010 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
1013 case LDAP_FILTER_SUBSTRINGS:
1015 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
1016 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
1019 case LDAP_FILTER_PRESENT:
1021 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
1022 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
1025 case LDAP_FILTER_AND:
1026 case LDAP_FILTER_OR:
1027 case LDAP_FILTER_NOT: {
1029 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
1030 f->f_choice == LDAP_FILTER_AND ? '&' :
1031 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
1032 fstr->bv_len += sizeof("(%") - 1;
1034 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1035 rc = filter2template( f, fstr, filter_attrs, filter_cnt );
1038 sprintf( fstr->bv_val+fstr->bv_len, ")" );
1039 fstr->bv_len += sizeof(")") - 1;
1045 strcpy( fstr->bv_val, "(?=?)" );
1046 fstr->bv_len += sizeof("(?=?)")-1;
1050 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
1051 (*filter_cnt + 2)*sizeof(AttributeName));
1053 (*filter_attrs)[*filter_cnt].an_desc = ad;
1054 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
1055 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
1056 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
1061 struct search_info {
1075 struct berval *query_uuid)
1077 struct search_info *si = op->o_callback->sc_private;
1078 slap_overinst *on = si->on;
1079 cache_manager *cm = on->on_bi.bi_private;
1080 query_manager* qm = cm->qm;
1084 struct berval crp_uuid;
1085 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1086 Operation op_tmp = *op;
1088 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1089 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1091 op_tmp.o_bd = &cm->db;
1092 op_tmp.o_dn = cm->db.be_rootdn;
1093 op_tmp.o_ndn = cm->db.be_rootndn;
1096 LDAP_LOG( BACK_META, DETAIL1, "UUID for query being added = %s\n",
1098 #else /* !NEW_LOGGING */
1099 Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1101 #endif /* !NEW_LOGGING */
1103 for ( e=si->head; e; e=si->head ) {
1104 si->head = e->e_private;
1105 e->e_private = NULL;
1107 LDAP_LOG( BACK_META, DETAIL2, "LOCKING REMOVE MUTEX\n",
1109 #else /* !NEW_LOGGING */
1110 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1111 #endif /* !NEW_LOGGING */
1112 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1114 LDAP_LOG( BACK_META, DETAIL2, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1115 #else /* !NEW_LOGGING */
1116 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1117 #endif /* !NEW_LOGGING */
1118 while ( cm->cur_entries > (cm->max_entries) ) {
1119 qm->crfunc(qm, &crp_uuid);
1120 if (crp_uuid.bv_val) {
1122 LDAP_LOG( BACK_META, DETAIL1,
1123 "Removing query UUID %s\n",
1124 crp_uuid.bv_val, 0, 0 );
1125 #else /* !NEW_LOGGING */
1126 Debug( LDAP_DEBUG_ANY,
1127 "Removing query UUID %s\n",
1128 crp_uuid.bv_val, 0, 0 );
1129 #endif /* !NEW_LOGGING */
1130 return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
1132 LDAP_LOG( BACK_META, DETAIL1,
1133 "QUERY REMOVED, SIZE=%d\n",
1135 #else /* !NEW_LOGGING */
1136 Debug( LDAP_DEBUG_ANY,
1137 "QUERY REMOVED, SIZE=%d\n",
1139 #endif /* !NEW_LOGGING */
1140 ldap_pvt_thread_mutex_lock(
1142 cm->cur_entries -= return_val;
1143 cm->num_cached_queries--;
1145 LDAP_LOG( BACK_META, DETAIL1,
1146 "STORED QUERIES = %lu\n",
1147 cm->num_cached_queries, 0, 0 );
1148 #else /* !NEW_LOGGING */
1149 Debug( LDAP_DEBUG_ANY,
1150 "STORED QUERIES = %lu\n",
1151 cm->num_cached_queries, 0, 0 );
1152 #endif /* !NEW_LOGGING */
1153 ldap_pvt_thread_mutex_unlock(
1156 LDAP_LOG( BACK_META, DETAIL1,
1157 "QUERY REMOVED, CACHE ="
1159 cm->cur_entries, 0, 0 );
1160 #else /* !NEW_LOGGING */
1161 Debug( LDAP_DEBUG_ANY,
1162 "QUERY REMOVED, CACHE ="
1164 cm->cur_entries, 0, 0 );
1165 #endif /* !NEW_LOGGING */
1169 return_val = merge_entry(&op_tmp, e, query_uuid);
1170 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1171 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1172 cm->cur_entries += return_val;
1174 LDAP_LOG( BACK_META, DETAIL1,
1175 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1176 cm->cur_entries, 0, 0 );
1177 #else /* !NEW_LOGGING */
1178 Debug( LDAP_DEBUG_ANY,
1179 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1180 cm->cur_entries, 0, 0 );
1181 #endif /* !NEW_LOGGING */
1183 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1185 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1186 cm->num_cached_queries++;
1188 LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1189 cm->num_cached_queries, 0, 0 );
1190 #else /* !NEW_LOGGING */
1191 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1192 cm->num_cached_queries, 0, 0 );
1193 #endif /* !NEW_LOGGING */
1194 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1200 proxy_cache_response(
1204 struct search_info *si = op->o_callback->sc_private;
1205 slap_overinst *on = si->on;
1206 cache_manager *cm = on->on_bi.bi_private;
1207 query_manager* qm = cm->qm;
1210 if ( rs->sr_type == REP_SEARCH ) {
1212 /* If we haven't exceeded the limit for this query,
1213 * build a chain of answers to store. If we hit the
1214 * limit, empty the chain and ignore the rest.
1217 if ( si->count < si->max ) {
1219 e = entry_dup( rs->sr_entry );
1220 if ( !si->head ) si->head = e;
1221 if ( si->tail ) si->tail->e_private = e;
1226 for (;si->head; si->head=e) {
1227 e = si->head->e_private;
1228 si->head->e_private = NULL;
1229 entry_free(si->head);
1235 if (rs->sr_attrs != op->ors_attrs ) {
1236 op->o_tmpfree( rs->sr_attrs, op->o_tmpmemctx );
1238 rs->sr_attrs = si->query.save_attrs;
1239 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
1240 op->ors_attrs = si->query.save_attrs;
1241 si->query.save_attrs = NULL;
1243 } else if ( rs->sr_type == REP_RESULT ) {
1244 if ( si->count && cache_entries( op, rs, &uuid ) == 0 ) {
1245 qm->addfunc(qm, &si->query, si->template_id, &uuid);
1246 /* If the consistency checker suspended itself,
1249 if ( cm->cc_paused ) {
1250 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1251 if ( cm->cc_paused ) {
1253 ldap_pvt_runqueue_resched( &syncrepl_rq, cm->cc_arg, 0 );
1255 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1260 op->o_callback->sc_cleanup = slap_freeself_cb;
1262 return SLAP_CB_CONTINUE;
1268 AttributeName** new_attrs,
1269 AttributeName* attrs,
1270 AttributeName* filter_attrs )
1277 /* duplicate attrs */
1278 if (attrs == NULL) {
1281 for (count=0; attrs[count].an_name.bv_val; count++)
1284 *new_attrs = (AttributeName*)(op->o_tmpalloc((count+1)*
1285 sizeof(AttributeName), op->o_tmpmemctx));
1286 if (attrs == NULL) {
1287 (*new_attrs)[0].an_name.bv_val = "*";
1288 (*new_attrs)[0].an_name.bv_len = 1;
1289 (*new_attrs)[1].an_name.bv_val = NULL;
1290 (*new_attrs)[1].an_name.bv_len = 0;
1294 for (i=0; i<count; i++) {
1295 (*new_attrs)[i].an_name = attrs[i].an_name;
1296 (*new_attrs)[i].an_desc = attrs[i].an_desc;
1298 (*new_attrs)[count].an_name.bv_val = NULL;
1299 (*new_attrs)[count].an_name.bv_len = 0;
1300 alluser = an_find(*new_attrs, &AllUser);
1301 allop = an_find(*new_attrs, &AllOper);
1304 for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1305 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1307 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1312 *new_attrs = (AttributeName*)(op->o_tmprealloc(*new_attrs,
1313 (count+2)*sizeof(AttributeName), op->o_tmpmemctx));
1314 (*new_attrs)[count].an_name = filter_attrs[i].an_name;
1315 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc;
1317 (*new_attrs)[count].an_name.bv_val = NULL;
1318 (*new_attrs)[count].an_name.bv_len = 0;
1327 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1328 cache_manager *cm = on->on_bi.bi_private;
1329 query_manager* qm = cm->qm;
1335 AttributeName *filter_attrs = NULL;
1336 AttributeName *new_attrs = NULL;
1341 int template_id = -1;
1345 int oc_attr_absent = 1;
1347 struct berval tempstr;
1349 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1351 if (filter2template(op->ors_filter, &tempstr, &filter_attrs, &fattr_cnt)) {
1352 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1353 return SLAP_CB_CONTINUE;
1357 LDAP_LOG( BACK_META, DETAIL1, "query template of incoming query = %s\n",
1358 tempstr.bv_val, 0, 0 );
1359 #else /* !NEW_LOGGING */
1360 Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
1361 tempstr.bv_val, 0, 0 );
1362 #endif /* !NEW_LOGGING */
1365 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1367 query.filter = op->ors_filter;
1368 query.attrs = op->ors_attrs;
1369 query.save_attrs = NULL;
1370 query.base = op->o_req_ndn;
1371 query.scope = op->ors_scope;
1373 /* check for query containment */
1374 if (attr_set > -1) {
1375 for (i=0; i<cm->numtemplates; i++) {
1376 /* find if template i can potentially answer tempstr */
1377 if (!is_temp_answerable(attr_set, &tempstr, qm, i))
1379 if (attr_set == qm->templates[i].attr_set_index) {
1384 LDAP_LOG( BACK_META, DETAIL2,
1385 "Entering QC, querystr = %s\n",
1386 op->ors_filterstr.bv_val, 0, 0 );
1387 #else /* !NEW_LOGGING */
1388 Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1389 op->ors_filterstr.bv_val, 0, 0 );
1390 #endif /* !NEW_LOGGING */
1391 answerable = (*(qm->qcfunc))(qm, &query, i);
1397 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1399 query.save_attrs = op->ors_attrs;
1403 /* Need to clear the callbacks of the original operation,
1404 * in case there are other overlays */
1405 BackendDB *save_bd = op->o_bd;
1406 slap_callback *save_cb = op->o_callback;
1409 LDAP_LOG( BACK_META, DETAIL1, "QUERY ANSWERABLE\n", 0, 0, 0 );
1410 #else /* !NEW_LOGGING */
1411 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
1412 #endif /* !NEW_LOGGING */
1414 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
1416 op->o_callback = NULL;
1417 i = cm->db.bd_info->bi_op_search( op, rs );
1419 op->o_callback = save_cb;
1424 LDAP_LOG( BACK_META, DETAIL1, "QUERY NOT ANSWERABLE\n",
1426 #else /* !NEW_LOGGING */
1427 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1428 #endif /* !NEW_LOGGING */
1430 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1431 if (cm->num_cached_queries >= cm->max_queries) {
1434 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1438 struct search_info *si;
1441 LDAP_LOG( BACK_META, DETAIL1,
1442 "QUERY CACHEABLE\n", 0, 0, 0 );
1443 #else /* !NEW_LOGGING */
1444 Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
1445 #endif /* !NEW_LOGGING */
1446 query.filter = str2filter(op->ors_filterstr.bv_val);
1447 if (op->ors_attrs) {
1448 for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1449 if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass ) {
1453 query.attrs = (AttributeName *)ch_malloc( ( count + 1 + oc_attr_absent )
1454 *sizeof(AttributeName) );
1455 for ( count = 0; !BER_BVISNULL( &op->ors_attrs[ count ].an_name ); count++ ) {
1456 ber_dupbv( &query.attrs[count].an_name, &op->ors_attrs[count].an_name );
1457 query.attrs[count].an_desc = op->ors_attrs[count].an_desc;
1459 if ( oc_attr_absent ) {
1460 query.attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
1461 ber_dupbv( &query.attrs[count].an_name,
1462 &slap_schema.si_ad_objectClass->ad_cname );
1465 query.attrs[ count ].an_name.bv_val = NULL;
1466 query.attrs[ count ].an_name.bv_len = 0;
1468 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
1470 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1471 cb->sc_response = proxy_cache_response;
1472 cb->sc_cleanup = NULL;
1473 cb->sc_private = (cb+1);
1474 si = cb->sc_private;
1477 si->template_id = template_id;
1478 si->max = cm->num_entries_limit ;
1484 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1485 cb->sc_next = op->o_callback;
1486 op->o_callback = cb;
1489 slap_callback **pcb;
1491 /* need to move the callback at the end, in case other
1492 * overlays are present, so that the final entry is
1493 * actually cached */
1495 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
1501 LDAP_LOG( BACK_META, DETAIL1,
1502 "QUERY NOT CACHEABLE\n",
1504 #else /* !NEW_LOGGING */
1505 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE\n",
1507 #endif /* !NEW_LOGGING */
1512 return SLAP_CB_CONTINUE;
1517 AttributeName* attrs_in,
1518 AttributeName* attrs)
1520 int i, count1, count2;
1521 if ( attrs_in == NULL ) {
1522 return (attrs ? 0 : 1);
1524 if ( attrs == NULL )
1528 attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1532 attrs && attrs[count2].an_name.bv_val != NULL;
1535 if ( count1 != count2 )
1538 for ( i=0; i<count1; i++ ) {
1539 if ( !an_find(attrs, &attrs_in[i].an_name ))
1547 AttributeName* attrs,
1552 for (i=0; i<num; i++) {
1553 if (attrscmp(attrs, qm->attr_sets[i].attrs))
1564 struct re_s *rtask = arg;
1565 slap_overinst *on = rtask->arg;
1566 cache_manager *cm = on->on_bi.bi_private;
1567 query_manager *qm = cm->qm;
1569 Connection conn = {0};
1571 SlapReply rs = {REP_RESULT};
1572 CachedQuery* query, *query_prev;
1573 int i, return_val, pause = 1;
1574 QueryTemplate* templ;
1576 connection_fake_init( &conn, &op, ctx );
1579 op.o_dn = cm->db.be_rootdn;
1580 op.o_ndn = cm->db.be_rootndn;
1584 for (i=0; qm->templates[i].querystr.bv_val; i++) {
1585 templ = qm->templates + i;
1586 query = templ->query_last;
1587 if ( query ) pause = 0;
1588 op.o_time = slap_get_time();
1589 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1590 while (query && (query->expiry_time < op.o_time)) {
1591 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1592 remove_query(qm, query);
1593 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1595 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n",
1597 #else /* !NEW_LOGGING */
1598 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1600 #endif /* !NEW_LOGGING */
1601 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1602 remove_from_template(query, templ);
1604 LDAP_LOG( BACK_META, DETAIL1,
1605 "TEMPLATE %d QUERIES-- %d\n",
1606 i, templ->no_of_queries, 0 );
1607 #else /* !NEW_LOGGING */
1608 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1609 i, templ->no_of_queries, 0 );
1610 #endif /* !NEW_LOGGING */
1612 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n",
1614 #else /* !NEW_LOGGING */
1615 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1617 #endif /* !NEW_LOGGING */
1618 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1619 return_val = remove_query_data(&op, &rs, &query->q_uuid);
1621 LDAP_LOG( BACK_META, DETAIL1,
1622 "STALE QUERY REMOVED, SIZE=%d\n",
1624 #else /* !NEW_LOGGING */
1625 Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1627 #endif /* !NEW_LOGGING */
1628 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1629 cm->cur_entries -= return_val;
1630 cm->num_cached_queries--;
1632 LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1633 cm->num_cached_queries, 0, 0 );
1634 #else /* !NEW_LOGGING */
1635 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1636 cm->num_cached_queries, 0, 0 );
1637 #endif /* !NEW_LOGGING */
1638 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1640 LDAP_LOG( BACK_META, DETAIL1,
1641 "STALE QUERY REMOVED, CACHE ="
1643 cm->cur_entries, 0, 0 );
1644 #else /* !NEW_LOGGING */
1645 Debug( LDAP_DEBUG_ANY,
1646 "STALE QUERY REMOVED, CACHE ="
1648 cm->cur_entries, 0, 0 );
1649 #endif /* !NEW_LOGGING */
1651 query = query->prev;
1652 free_query(query_prev);
1654 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1656 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1657 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
1658 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
1660 /* If there were no queries, defer processing for a while */
1661 cm->cc_paused = pause;
1662 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, pause );
1664 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1669 #define MAX_ATTR_SETS 500
1670 static void find_supersets( struct attr_set* attr_sets, int numsets );
1671 static int compare_sets( struct attr_set* setA, int, int );
1682 slap_overinst *on = (slap_overinst *)be->bd_info;
1683 cache_manager* cm = on->on_bi.bi_private;
1684 query_manager* qm = cm->qm;
1685 QueryTemplate* temp;
1686 AttributeName* attr_name;
1687 AttributeName* attrarray;
1688 const char* text=NULL;
1689 char *save_argv0 = NULL;
1695 if ( strncasecmp( argv[0], "proxycache-", STRLENOF( "proxycache-" ) ) == 0 ) {
1696 save_argv0 = argv[0];
1697 argv[0] += STRLENOF( "proxycache-" );
1700 if ( strcasecmp( argv[0], "proxycache" ) == 0 ) {
1702 fprintf( stderr, "%s: line %d: missing arguments in \"proxycache"
1703 " <backend> <max_entries> <numattrsets> <entry limit> "
1704 "<cycle_time>\"\n", fname, lineno );
1708 cm->db.bd_info = backend_info( argv[1] );
1709 if ( !cm->db.bd_info ) {
1710 fprintf( stderr, "%s: line %d: backend %s unknown\n",
1711 fname, lineno, argv[1] );
1714 if ( cm->db.bd_info->bi_db_init( &cm->db ) ) return( 1 );
1716 /* This type is in use, needs to be opened */
1717 cm->db.bd_info->bi_nDB++;
1719 cm->max_entries = atoi( argv[2] );
1721 cm->numattrsets = atoi( argv[3] );
1722 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1723 fprintf( stderr, "%s: line %d: numattrsets must be <= %d\n",
1724 fname, lineno, MAX_ATTR_SETS );
1728 cm->num_entries_limit = atoi( argv[4] );
1729 cm->cc_period = atoi( argv[5] );
1731 LDAP_LOG( BACK_META, DETAIL1,
1732 "Total # of attribute sets to be cached = %d\n",
1733 cm->numattrsets, 0, 0 );
1735 Debug( LDAP_DEBUG_ANY,
1736 "Total # of attribute sets to be cached = %d\n",
1737 cm->numattrsets, 0, 0 );
1739 qm->attr_sets = ( struct attr_set * )ch_malloc( cm->numattrsets *
1740 sizeof( struct attr_set ));
1741 for ( i = 0; i < cm->numattrsets; i++ ) {
1742 qm->attr_sets[i].attrs = NULL;
1745 } else if ( strcasecmp( argv[0], "proxyattrset" ) == 0 ) {
1747 fprintf( stderr, "%s: line %d: missing arguments in \"proxyattrset "
1748 "<index> <attributes>\"\n", fname, lineno );
1752 LDAP_LOG( BACK_META, DETAIL1, "Attribute Set # %d\n",
1753 atoi( argv[1] ), 0, 0 );
1755 Debug( LDAP_DEBUG_ANY, "Attribute Set # %d\n",
1756 atoi( argv[1] ), 0, 0 );
1758 if (atoi(argv[1]) >= cm->numattrsets) {
1759 fprintf( stderr, "%s; line %d index out of bounds \n",
1763 index = atoi( argv[1] );
1764 if ( argv[2] && strcmp( argv[2], "*" ) ) {
1765 qm->attr_sets[index].count = argc - 2;
1766 qm->attr_sets[index].attrs = (AttributeName*)ch_malloc(
1767 (argc-1) * sizeof( AttributeName ));
1768 attr_name = qm->attr_sets[index].attrs;
1769 for ( i = 2; i < argc; i++ ) {
1771 LDAP_LOG( BACK_META, DETAIL1, "\t %s\n",
1774 Debug( LDAP_DEBUG_ANY, "\t %s\n",
1777 ber_str2bv( argv[i], 0, 1,
1778 &attr_name->an_name);
1779 attr_name->an_desc = NULL;
1780 slap_bv2ad( &attr_name->an_name,
1781 &attr_name->an_desc, &text );
1783 attr_name->an_name.bv_val = NULL;
1784 attr_name->an_name.bv_len = 0;
1787 } else if ( strcasecmp( argv[0], "proxytemplate" ) == 0 ) {
1789 fprintf( stderr, "%s: line %d: missing argument(s) in "
1790 "\"proxytemplate <filter> <proj attr set> <TTL>\" line\n",
1794 if (( i = atoi( argv[2] )) >= cm->numattrsets ) {
1796 LDAP_LOG( BACK_META, DETAIL1,
1797 "%s: line %d, template index invalid\n",
1800 Debug( LDAP_DEBUG_ANY,
1801 "%s: line %d, template index invalid\n",
1806 num = cm->numtemplates;
1808 find_supersets( qm->attr_sets, cm->numattrsets );
1809 qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
1810 ( num + 2 ) * sizeof( QueryTemplate ));
1811 temp = qm->templates + num;
1812 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
1813 temp->query = temp->query_last = NULL;
1814 temp->ttl = atoi( argv[3] );
1815 temp->no_of_queries = 0;
1816 if ( argv[1] == NULL ) {
1818 LDAP_LOG( BACK_META, DETAIL1,
1819 "Templates string not specified "
1820 "for template %d\n", num, 0, 0 );
1822 Debug( LDAP_DEBUG_ANY,
1823 "Templates string not specified "
1824 "for template %d\n", num, 0, 0 );
1828 ber_str2bv( argv[1], 0, 1, &temp->querystr );
1830 LDAP_LOG( BACK_META, DETAIL1, "Template:\n", 0, 0, 0 );
1832 Debug( LDAP_DEBUG_ANY, "Template:\n", 0, 0, 0 );
1835 LDAP_LOG( BACK_META, DETAIL1, " query template: %s\n",
1836 temp->querystr.bv_val, 0, 0 );
1838 Debug( LDAP_DEBUG_ANY, " query template: %s\n",
1839 temp->querystr.bv_val, 0, 0 );
1841 temp->attr_set_index = i;
1843 LDAP_LOG( BACK_META, DETAIL1, " attributes: \n", 0, 0, 0 );
1845 Debug( LDAP_DEBUG_ANY, " attributes: \n", 0, 0, 0 );
1847 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1848 for ( i=0; attrarray[i].an_name.bv_val; i++ )
1850 LDAP_LOG( BACK_META, DETAIL1, "\t%s\n",
1851 attrarray[i].an_name.bv_val, 0, 0 );
1853 Debug( LDAP_DEBUG_ANY, "\t%s\n",
1854 attrarray[i].an_name.bv_val, 0, 0 );
1858 temp->querystr.bv_val = NULL;
1861 } else if ( strcasecmp( argv[0], "response-callback" ) == 0 ) {
1862 /* set to "tail" to put the response callback
1863 * at the end of the callback list; this is required
1864 * in case other overlays are present, so that the
1865 * final entry is cached. */
1869 LDAP_LOG( BACK_META, DETAIL1,
1870 "missing specifier for \"response-callback {head(default)|tail}\" "
1871 "callback position\n", 0, 0, 0 );
1873 Debug( LDAP_DEBUG_ANY,
1874 "missing specifier for \"response-callback {head(default)|tail}\" "
1875 "callback position\n", 0, 0, 0 );
1880 if ( strcasecmp( argv[1], "head" ) == 0 ) {
1881 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
1883 } else if ( strcasecmp( argv[1], "tail" ) == 0 ) {
1884 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1888 LDAP_LOG( BACK_META, DETAIL1,
1889 "unknown specifier %s for \"response-callback {head(default)|tail}\" "
1890 "callback position\n", argv[1], 0, 0 );
1892 Debug( LDAP_DEBUG_ANY,
1893 "unknown specifier %s for \"response-callback {head(default)|tail}\" "
1894 "callback position\n", argv[1], 0, 0 );
1901 rc = cm->db.bd_info->bi_db_config( &cm->db, fname, lineno, argc, argv );
1905 argv[0] = save_argv0;
1916 slap_overinst *on = (slap_overinst *)be->bd_info;
1920 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1921 on->on_bi.bi_private = cm;
1923 qm = (query_manager*)ch_malloc(sizeof(query_manager));
1926 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1927 cm->db.be_private = NULL;
1928 cm->db.be_pcl_mutexp = &cm->db.be_pcl_mutex;
1930 cm->numattrsets = 0;
1931 cm->numtemplates = 0;
1932 cm->num_entries_limit = 5;
1933 cm->num_cached_queries = 0;
1934 cm->max_entries = 0;
1935 cm->cur_entries = 0;
1936 cm->max_queries = 10000;
1937 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1938 cm->cc_period = 1000;
1941 qm->attr_sets = NULL;
1942 qm->templates = NULL;
1944 qm->lru_bottom = NULL;
1946 qm->qcfunc = query_containment;
1947 qm->crfunc = cache_replacement;
1948 qm->addfunc = add_query;
1949 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1951 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1952 ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1961 slap_overinst *on = (slap_overinst *)be->bd_info;
1962 cache_manager *cm = on->on_bi.bi_private;
1966 /* consistency check (add more...) */
1967 for ( i = 0; i < cm->numattrsets; i++ ) {
1968 if ( cm->qm->attr_sets[i].attrs == NULL ) {
1969 fprintf( stderr, "proxy_cache_open(): "
1970 "attr set %d (of %d) missing\n",
1971 i, cm->numattrsets );
1976 rc = backend_startup_one( &cm->db );
1978 /* There is no runqueue in TOOL mode */
1979 if ( slapMode & SLAP_SERVER_MODE ) {
1980 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1981 ldap_pvt_runqueue_insert( &syncrepl_rq, cm->cc_period,
1982 consistency_check, on );
1983 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1985 /* Cached database must have the rootdn */
1986 if ( BER_BVISNULL( &cm->db.be_rootndn )
1987 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
1989 fprintf( stderr, "proxy_cache_open(): "
1990 "underlying database of type \"%s\"\n"
1991 " serving naming context \"%s\"\n"
1992 " has no \"rootdn\", required by \"proxycache\".\n",
1993 on->on_info->oi_orig->bi_type,
1994 cm->db.be_suffix[0].bv_val );
2007 slap_overinst *on = (slap_overinst *)be->bd_info;
2008 cache_manager *cm = on->on_bi.bi_private;
2009 query_manager *qm = cm->qm;
2012 if ( cm->db.bd_info->bi_db_close ) {
2013 rc = cm->db.bd_info->bi_db_close( &cm->db );
2015 for ( i=0; i<cm->numtemplates; i++ ) {
2016 CachedQuery *qc, *qn;
2017 for ( qc = qm->templates[i].query; qc; qc = qn ) {
2021 free( qm->templates[i].querystr.bv_val );
2022 ldap_pvt_thread_rdwr_destroy( &qm->templates[i].t_rwlock );
2024 free( qm->templates );
2025 qm->templates = NULL;
2027 for ( i=0; i<cm->numattrsets; i++ ) {
2028 free( qm->attr_sets[i].ID_array );
2029 for ( j=0; j<qm->attr_sets[i].count; j++ ) {
2030 free( qm->attr_sets[i].attrs[j].an_name.bv_val );
2032 free( qm->attr_sets[i].attrs );
2034 free( qm->attr_sets );
2035 qm->attr_sets = NULL;
2041 proxy_cache_destroy(
2045 slap_overinst *on = (slap_overinst *)be->bd_info;
2046 cache_manager *cm = on->on_bi.bi_private;
2047 query_manager *qm = cm->qm;
2050 if ( cm->db.bd_info->bi_db_destroy ) {
2051 rc = cm->db.bd_info->bi_db_destroy( &cm->db );
2053 ldap_pvt_thread_mutex_destroy(&qm->lru_mutex);
2054 ldap_pvt_thread_mutex_destroy(&cm->cache_mutex);
2055 ldap_pvt_thread_mutex_destroy(&cm->remove_mutex);
2062 find_supersets ( struct attr_set* attr_sets, int numsets )
2064 int num[MAX_ATTR_SETS];
2067 for ( i = 0; i < MAX_ATTR_SETS; i++ )
2070 for ( i = 0; i < numsets; i++ ) {
2071 attr_sets[i].ID_array = (int*) ch_malloc( sizeof( int ) );
2072 attr_sets[i].ID_array[0] = -1;
2075 for ( i = 0; i < numsets; i++ ) {
2076 for ( j=i+1; j < numsets; j++ ) {
2077 res = compare_sets( attr_sets, i, j );
2083 id_array = attr_sets[i].ID_array;
2084 attr_sets[i].ID_array = (int *) ch_realloc( id_array,
2085 ( num[i] + 2 ) * sizeof( int ));
2086 attr_sets[i].ID_array[num[i]] = j;
2087 attr_sets[i].ID_array[num[i]+1] = -1;
2092 id_array = attr_sets[j].ID_array;
2093 attr_sets[j].ID_array = (int *) ch_realloc( id_array,
2094 ( num[j] + 2 ) * sizeof( int ));
2095 attr_sets[j].ID_array[num[j]] = i;
2096 attr_sets[j].ID_array[num[j]+1] = -1;
2105 * compares two sets of attributes (indices i and j)
2106 * returns 0: if neither set is contained in the other set
2107 * 1: if set i is contained in set j
2108 * 2: if set j is contained in set i
2109 * 3: the sets are equivalent
2113 compare_sets(struct attr_set* set, int i, int j)
2119 if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
2122 if ( set[i].attrs == NULL )
2125 if ( set[j].attrs == NULL )
2128 numI = set[i].count;
2129 numJ = set[j].count;
2131 for ( l=0; l < numI; l++ ) {
2132 for ( k = 0; k < numJ; k++ ) {
2133 if ( strcmp( set[i].attrs[l].an_name.bv_val,
2134 set[j].attrs[k].an_name.bv_val ) == 0 )
2139 if ( common == numI )
2142 if ( common == numJ )
2148 static slap_overinst proxy_cache;
2152 LDAPAttributeType *at;
2156 at = ldap_str2attributetype( queryid_schema, &code, &err,
2157 LDAP_SCHEMA_ALLOW_ALL );
2159 fprintf( stderr, "AttributeType Load failed %s %s\n",
2160 ldap_scherr2str(code), err );
2163 code = at_add( at, &err );
2165 slap_str2ad( at->at_names[0], &ad_queryid, &err );
2169 fprintf( stderr, "AttributeType Load failed %s %s\n",
2170 scherr2str(code), err );
2174 proxy_cache.on_bi.bi_type = "proxycache";
2175 proxy_cache.on_bi.bi_db_init = proxy_cache_init;
2176 proxy_cache.on_bi.bi_db_config = proxy_cache_config;
2177 proxy_cache.on_bi.bi_db_open = proxy_cache_open;
2178 proxy_cache.on_bi.bi_db_close = proxy_cache_close;
2179 proxy_cache.on_bi.bi_db_destroy = proxy_cache_destroy;
2180 proxy_cache.on_bi.bi_op_search = proxy_cache_search;
2182 return overlay_register( &proxy_cache );
2185 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
2186 int init_module(int argc, char *argv[]) {
2187 return pcache_init();
2191 #endif /* defined(SLAPD_OVER_PROXYCACHE) */