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 struct berval base; /* Search Base */
43 int scope; /* Search scope */
46 /* struct representing a cached query */
47 typedef struct cached_query_s {
48 Query query; /* LDAP query */
49 struct berval q_uuid; /* query identifier */
50 int template_id; /* template of the query */
51 time_t expiry_time; /* time till the query is considered valid */
52 struct cached_query_s *next; /* next query in the template */
53 struct cached_query_s *prev; /* previous query in the template */
54 struct cached_query_s *lru_up; /* previous query in the LRU list */
55 struct cached_query_s *lru_down; /* next query in the LRU list */
58 /* struct representing a query template
59 * e.g. template string = &(cn=)(mail=)
61 typedef struct query_template_s {
62 struct berval querystr; /* Filter string corresponding to the QT */
63 int attr_set_index; /* determines the projected attributes */
65 CachedQuery* query; /* most recent query cached for the template */
66 CachedQuery* query_last; /* oldest query cached for the template */
68 int no_of_queries; /* Total number of queries in the template */
69 long ttl; /* TTL for the queries of this template */
70 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
74 * Represents a set of projected attributes and any
75 * supersets among all specified sets of attributes.
79 AttributeName* attrs; /* specifies the set */
80 int count; /* number of attributes */
81 int* ID_array; /* array of indices of supersets of 'attrs' */
84 struct query_manager_s;
86 /* prototypes for functions for 1) query containment
87 * 2) query addition, 3) cache replacement
89 typedef int (QCfunc)(struct query_manager_s*, Query*, int );
90 typedef void (AddQueryfunc)(struct query_manager_s*, Query*, int, struct berval*);
91 typedef void (CRfunc)(struct query_manager_s*, struct berval * );
93 /* LDAP query cache */
94 typedef struct query_manager_s {
95 struct attr_set* attr_sets; /* possible sets of projected attributes */
96 QueryTemplate* templates; /* cacheable templates */
98 CachedQuery* lru_top; /* top and bottom of LRU list */
99 CachedQuery* lru_bottom;
101 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
103 /* Query cache methods */
104 QCfunc *qcfunc; /* Query containment*/
105 CRfunc *crfunc; /* cache replacement */
106 AddQueryfunc *addfunc; /* add query */
109 /* LDAP query cache manager */
110 typedef struct cache_manager_s {
111 BackendDB db; /* underlying database */
112 unsigned long num_cached_queries; /* total number of cached queries */
113 unsigned long max_queries; /* upper bound on # of cached queries */
114 int numattrsets; /* number of attribute sets */
115 int numtemplates; /* number of cacheable templates */
116 int cur_entries; /* current number of entries cached */
117 int max_entries; /* max number of entries cached */
118 int num_entries_limit; /* max # of entries in a cacheable query */
120 int cc_period; /* interval between successive consistency checks (sec) */
124 ldap_pvt_thread_mutex_t cache_mutex;
125 ldap_pvt_thread_mutex_t remove_mutex;
127 query_manager* qm; /* query cache managed by the cache manager */
130 static AttributeDescription *ad_queryid;
131 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
132 "DESC 'list of queries the entry belongs to' "
133 "EQUALITY octetStringMatch "
134 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
135 "NO-USER-MODIFICATION USAGE directoryOperation )";
137 /* Return 1 for an added entry, else 0 */
142 struct berval* query_uuid )
145 Modifications* modlist = NULL;
146 const char* text = NULL;
148 char textbuf[SLAP_TEXT_BUFLEN];
149 size_t textlen = sizeof(textbuf);
151 SlapReply sreply = {REP_RESULT};
153 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
158 /* add queryid attribute */
159 attr_merge_one( e, ad_queryid, query_uuid, NULL );
161 /* append the attribute list from the fetched entry */
162 e->e_attrs->a_next = attr;
164 op->o_tag = LDAP_REQ_ADD;
165 op->o_protocol = LDAP_VERSION3;
166 op->o_callback = &cb;
167 op->o_time = slap_get_time();
168 op->o_do_not_cache = 1;
171 op->o_req_dn = e->e_name;
172 op->o_req_ndn = e->e_nname;
173 rc = op->o_bd->be_add( op, &sreply );
175 if ( rc != LDAP_SUCCESS ) {
176 if ( rc == LDAP_ALREADY_EXISTS ) {
177 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
178 modlist->sml_op = LDAP_MOD_ADD;
179 op->o_tag = LDAP_REQ_MODIFY;
180 op->orm_modlist = modlist;
181 op->o_bd->be_modify( op, &sreply );
182 slap_mods_free( modlist );
183 } else if ( rc == LDAP_REFERRAL ||
184 rc == LDAP_NO_SUCH_OBJECT ) {
185 syncrepl_add_glue( op, e );
194 be_entry_release_w( op, e );
201 /* compare base and scope of incoming and cached queries */
202 static int base_scope_compare(
203 struct berval* ndn_stored,
204 struct berval* ndn_incoming,
208 struct berval pdn_incoming = BER_BVNULL;
210 if (scope_stored < scope_incoming)
213 if ( !dnIsSuffix(ndn_incoming, ndn_stored))
216 switch(scope_stored) {
217 case LDAP_SCOPE_BASE:
218 return (ndn_incoming->bv_len == ndn_stored->bv_len);
220 case LDAP_SCOPE_ONELEVEL:
221 switch(scope_incoming){
222 case LDAP_SCOPE_BASE:
223 dnParent(ndn_incoming, &pdn_incoming);
224 return (pdn_incoming.bv_len == ndn_stored->bv_len);
226 case LDAP_SCOPE_ONELEVEL:
227 return (ndn_incoming->bv_len == ndn_stored->bv_len);
232 case LDAP_SCOPE_SUBTREE:
241 /* add query on top of LRU list */
243 add_query_on_top (query_manager* qm, CachedQuery* qc)
245 CachedQuery* top = qm->lru_top;
246 Query* q = (Query*)qc;
258 LDAP_LOG( BACK_META, DETAIL1, "Base of added query = %s\n",
259 q->base.bv_val, 0, 0 );
261 Debug( LDAP_DEBUG_ANY, "Base of added query = %s\n",
262 q->base.bv_val, 0, 0 );
266 /* remove_query from LRU list */
269 remove_query (query_manager* qm, CachedQuery* qc)
292 qc->lru_up = qc->lru_down = NULL;
296 invert_string( struct berval *bv )
301 for (i=0; i<bv->bv_len/2; i++) {
303 bv->bv_val[i] = bv->bv_val[bv->bv_len-i-1];
304 bv->bv_val[bv->bv_len-i-1] = c;
308 /* find and remove string2 from string1
309 * from start if position = 1,
310 * from end if position = 3,
311 * from anywhere if position = 2
315 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
322 char* string1=ber1->bv_val;
323 char* string2=ber2->bv_val;
338 temp = strstr(arg1, arg2);
342 if ( temp && (position == 2 || temp == arg1) ) {
344 strcpy( arg1, string1 );
348 if ( position == 3 ) {
356 static struct berval*
357 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
359 struct berval* merged, *temp;
360 int i, any_count, count;
362 for (any_count=0; any && any[any_count].bv_val; any_count++)
372 merged = (struct berval*)(ch_malloc((count+1)*sizeof(struct berval)));
379 for (i=0; i<any_count; i++) {
392 strings_containment(struct berval* stored, struct berval* incoming)
394 struct berval* element;
398 for ( element=stored; element->bv_val != NULL; element++ ) {
399 for (j = k; incoming[j].bv_val != NULL; j++) {
400 if (find_and_remove(&(incoming[j]), element, 2)) {
417 substr_containment_substr(Filter* stored, Filter* incoming)
423 struct berval init_incoming;
424 struct berval final_incoming;
425 struct berval *any_incoming = NULL;
426 struct berval *remaining_incoming = NULL;
428 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
429 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
433 ber_dupbv(&init_incoming, &(incoming->f_sub_initial));
434 ber_dupbv(&final_incoming, &(incoming->f_sub_final));
436 if (incoming->f_sub_any) {
437 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
441 any_incoming = (struct berval*)ch_malloc((any_count+1) *
442 sizeof(struct berval));
444 for (i=0; i<any_count; i++) {
445 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
447 any_incoming[any_count].bv_val = NULL;
448 any_incoming[any_count].bv_len = 0;
451 if (find_and_remove(&init_incoming,
452 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
453 &(stored->f_sub_final), 3))
455 if (stored->f_sub_any == NULL) {
459 remaining_incoming = merge_init_final(&init_incoming,
460 any_incoming, &final_incoming);
461 rc = strings_containment(stored->f_sub_any, remaining_incoming);
464 free(init_incoming.bv_val);
465 free(final_incoming.bv_val);
466 if (any_incoming) ber_bvarray_free( any_incoming );
467 free(remaining_incoming);
473 substr_containment_equality(Filter* stored, Filter* incoming)
475 struct berval incoming_val[2];
478 ber_dupbv(incoming_val, &(incoming->f_av_value));
479 incoming_val[1].bv_val = NULL;
480 incoming_val[1].bv_len = 0;
482 if (find_and_remove(incoming_val,
483 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
484 &(stored->f_sub_final), 3)) {
485 if (stored->f_sub_any == NULL){
489 rc = strings_containment(stored->f_sub_any, incoming_val);
492 free(incoming_val[0].bv_val);
496 /* check whether query is contained in any of
497 * the cached queries in template template_index
500 query_containment(query_manager *qm,
504 QueryTemplate* templa= qm->templates;
507 Filter* inputf = query->filter;
508 struct berval* base = &(query->base);
509 int scope = query->scope;
516 MatchingRule* mrule = NULL;
517 if (inputf != NULL) {
519 LDAP_LOG( BACK_META, DETAIL1, "Lock QC index = %d\n",
520 template_index, 0, 0 );
522 Debug( LDAP_DEBUG_ANY, "Lock QC index = %d\n",
523 template_index, 0, 0 );
525 ldap_pvt_thread_rdwr_rlock(&(templa[template_index].t_rwlock));
526 for(qc=templa[template_index].query; qc != NULL; qc= qc->next) {
528 if(base_scope_compare(&(q->base), base, q->scope, scope)) {
533 switch (fs->f_choice) {
534 case LDAP_FILTER_EQUALITY:
535 if (fi->f_choice == LDAP_FILTER_EQUALITY)
536 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
542 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
548 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
549 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
550 &(fi->f_ava->aa_value),
551 &(fs->f_ava->aa_value), &text);
552 if (rc != LDAP_SUCCESS) {
553 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
555 LDAP_LOG( BACK_META, DETAIL1,
556 "Unlock: Exiting QC index=%d\n",
557 template_index, 0, 0 );
559 Debug( LDAP_DEBUG_ANY,
560 "Unlock: Exiting QC index=%d\n",
561 template_index, 0, 0 );
566 switch (fs->f_choice) {
568 case LDAP_FILTER_AND:
573 case LDAP_FILTER_SUBSTRINGS:
574 /* check if the equality query can be
575 * answered with cached substring query */
576 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
577 && substr_containment_equality(
580 /* check if the substring query can be
581 * answered with cached substring query */
582 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
583 ) && substr_containment_substr(
589 case LDAP_FILTER_PRESENT:
594 case LDAP_FILTER_EQUALITY:
612 case LDAP_FILTER_NOT:
618 } while((res) && (fi != NULL) && (fs != NULL));
621 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
622 if (qm->lru_top != qc) {
623 remove_query(qm, qc);
624 add_query_on_top(qm, qc);
626 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
632 LDAP_LOG( BACK_META, DETAIL1,
633 "Not answerable: Unlock QC index=%d\n",
634 template_index, 0, 0 );
636 Debug( LDAP_DEBUG_ANY,
637 "Not answerable: Unlock QC index=%d\n",
638 template_index, 0, 0 );
640 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
646 free_query (CachedQuery* qc)
648 Query* q = (Query*)qc;
651 free(qc->q_uuid.bv_val);
652 filter_free(q->filter);
653 free (q->base.bv_val);
654 for (i=0; q->attrs[i].an_name.bv_val; i++) {
655 free(q->attrs[i].an_name.bv_val);
662 /* Add query to query cache */
663 static void add_query(
669 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
670 QueryTemplate* templ = (qm->templates)+template_index;
672 new_cached_query->template_id = template_index;
673 new_cached_query->q_uuid = *uuid;
674 new_cached_query->lru_up = NULL;
675 new_cached_query->lru_down = NULL;
676 new_cached_query->expiry_time = slap_get_time() + templ->ttl;
678 LDAP_LOG( BACK_META, DETAIL1, "Added query expires at %ld\n",
679 (long) new_cached_query->expiry_time, 0, 0 );
681 Debug( LDAP_DEBUG_ANY, "Added query expires at %ld\n",
682 (long) new_cached_query->expiry_time, 0, 0 );
684 new_query = (Query*)new_cached_query;
686 ber_dupbv(&new_query->base, &query->base);
687 new_query->scope = query->scope;
688 new_query->filter = query->filter;
689 new_query->attrs = query->attrs;
693 LDAP_LOG( BACK_META, DETAIL1, "Lock AQ index = %d\n",
694 template_index, 0, 0 );
696 Debug( LDAP_DEBUG_ANY, "Lock AQ index = %d\n",
697 template_index, 0, 0 );
699 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
700 if (templ->query == NULL)
701 templ->query_last = new_cached_query;
703 templ->query->prev = new_cached_query;
704 new_cached_query->next = templ->query;
705 new_cached_query->prev = NULL;
706 templ->query = new_cached_query;
707 templ->no_of_queries++;
709 LDAP_LOG( BACK_META, DETAIL1, "TEMPLATE %d QUERIES++ %d\n",
710 template_index, templ->no_of_queries, 0 );
712 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES++ %d\n",
713 template_index, templ->no_of_queries, 0 );
717 LDAP_LOG( BACK_META, DETAIL1, "Unlock AQ index = %d \n",
718 template_index, 0, 0 );
720 Debug( LDAP_DEBUG_ANY, "Unlock AQ index = %d \n",
721 template_index, 0, 0 );
723 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
725 /* Adding on top of LRU list */
726 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
727 add_query_on_top(qm, new_cached_query);
728 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
732 remove_from_template (CachedQuery* qc, QueryTemplate* template)
734 if (!qc->prev && !qc->next) {
735 template->query_last = template->query = NULL;
736 } else if (qc->prev == NULL) {
737 qc->next->prev = NULL;
738 template->query = qc->next;
739 } else if (qc->next == NULL) {
740 qc->prev->next = NULL;
741 template->query_last = qc->prev;
743 qc->next->prev = qc->prev;
744 qc->prev->next = qc->next;
747 template->no_of_queries--;
750 /* remove bottom query of LRU list from the query cache */
751 static void cache_replacement(query_manager* qm, struct berval *result)
756 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
757 bottom = qm->lru_bottom;
759 result->bv_val = NULL;
764 LDAP_LOG ( BACK_META, DETAIL1,
765 "Cache replacement invoked without "
766 "any query in LRU list\n", 0, 0, 0 );
768 Debug ( LDAP_DEBUG_ANY,
769 "Cache replacement invoked without "
770 "any query in LRU list\n", 0, 0, 0 );
775 temp_id = bottom->template_id;
776 remove_query(qm, bottom);
777 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
779 *result = bottom->q_uuid;
780 bottom->q_uuid.bv_val = NULL;
783 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n", temp_id, 0, 0 );
785 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n", temp_id, 0, 0 );
787 ldap_pvt_thread_rdwr_wlock(&(qm->templates[temp_id].t_rwlock));
788 remove_from_template(bottom, (qm->templates+temp_id));
790 LDAP_LOG( BACK_META, DETAIL1, "TEMPLATE %d QUERIES-- %d\n",
791 temp_id, qm->templates[temp_id].no_of_queries, 0 );
793 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
794 temp_id, qm->templates[temp_id].no_of_queries, 0 );
797 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n", temp_id, 0, 0 );
799 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n", temp_id, 0, 0 );
801 ldap_pvt_thread_rdwr_wunlock(&(qm->templates[temp_id].t_rwlock));
806 struct query_info *next;
818 struct query_info *qi;
821 if ( rs->sr_type != REP_SEARCH ) return 0;
823 for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
824 if (attr->a_desc == ad_queryid) {
825 for (count=0; attr->a_vals[count].bv_val; count++)
830 if ( count == 0 ) return 0;
831 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
832 qi->next = op->o_callback->sc_private;
833 op->o_callback->sc_private = qi;
834 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
835 qi->del = ( count == 1 );
844 struct berval* query_uuid)
846 struct query_info *qi, *qnext;
848 AttributeAssertion ava;
849 Filter filter = {LDAP_FILTER_EQUALITY};
850 SlapReply sreply = {REP_RESULT};
851 slap_callback cb = { NULL, remove_func, NULL, NULL };
854 sreply.sr_entry = NULL;
855 sreply.sr_nentries = 0;
856 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
857 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
859 filter.f_av_desc = ad_queryid;
860 filter.f_av_value = *query_uuid;
862 op->o_tag = LDAP_REQ_SEARCH;
863 op->o_protocol = LDAP_VERSION3;
864 op->o_callback = &cb;
865 op->o_time = slap_get_time();
866 op->o_do_not_cache = 1;
868 op->o_req_dn = op->o_bd->be_suffix[0];
869 op->o_req_ndn = op->o_bd->be_nsuffix[0];
870 op->ors_scope = LDAP_SCOPE_SUBTREE;
871 op->ors_deref = LDAP_DEREF_NEVER;
874 op->ors_filter = &filter;
875 op->ors_filterstr.bv_val = filter_str;
876 op->ors_filterstr.bv_len = strlen(filter_str);
877 op->ors_attrs = NULL;
878 op->ors_attrsonly = 0;
880 op->o_bd->be_search( op, &sreply );
882 for ( qi=cb.sc_private; qi; qi=qnext ) {
885 op->o_req_dn = qi->xdn;
886 op->o_req_ndn = qi->xdn;
890 LDAP_LOG( BACK_META, DETAIL1,
891 "DELETING ENTRY TEMPLATE=%s\n",
892 query_uuid->bv_val, 0, 0 );
894 Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
895 query_uuid->bv_val, 0, 0 );
898 op->o_tag = LDAP_REQ_DELETE;
900 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
905 struct berval vals[2];
907 vals[0] = *query_uuid;
908 vals[1].bv_val = NULL;
910 mod.sml_op = LDAP_MOD_DELETE;
911 mod.sml_desc = ad_queryid;
912 mod.sml_type = ad_queryid->ad_cname;
913 mod.sml_values = vals;
916 LDAP_LOG( BACK_META, DETAIL1,
917 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
918 query_uuid->bv_val, 0, 0 );
920 Debug( LDAP_DEBUG_ANY,
921 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
922 query_uuid->bv_val, 0, 0 );
925 op->orm_modlist = &mod;
927 op->o_bd->be_modify( op, &sreply );
929 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
930 op->o_tmpfree( qi, op->o_tmpmemctx );
937 AttributeName* attrs,
944 AttributeName* attrs_in,
951 struct berval* tempstr,
955 QueryTemplate *qt = qm->templates + template_id;
957 if (attr_set != qt->attr_set_index) {
958 int* id_array = qm->attr_sets[attr_set].ID_array;
960 while (*id_array != -1) {
961 if (*id_array == qt->attr_set_index)
968 return (qt->querystr.bv_len == tempstr->bv_len &&
969 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
976 AttributeName** filter_attrs,
979 AttributeDescription *ad;
981 switch ( f->f_choice ) {
982 case LDAP_FILTER_EQUALITY:
984 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
985 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
990 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
991 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
996 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
997 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
1000 case LDAP_FILTER_APPROX:
1002 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
1003 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
1006 case LDAP_FILTER_SUBSTRINGS:
1008 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
1009 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
1012 case LDAP_FILTER_PRESENT:
1014 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
1015 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
1018 case LDAP_FILTER_AND:
1019 case LDAP_FILTER_OR:
1020 case LDAP_FILTER_NOT: {
1022 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
1023 f->f_choice == LDAP_FILTER_AND ? '&' :
1024 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
1025 fstr->bv_len += sizeof("(%") - 1;
1027 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1028 rc = filter2template( f, fstr, filter_attrs, filter_cnt );
1031 sprintf( fstr->bv_val+fstr->bv_len, ")" );
1032 fstr->bv_len += sizeof(")") - 1;
1038 strcpy( fstr->bv_val, "(?=?)" );
1039 fstr->bv_len += sizeof("(?=?)")-1;
1043 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
1044 (*filter_cnt + 2)*sizeof(AttributeName));
1046 (*filter_attrs)[*filter_cnt].an_desc = ad;
1047 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
1048 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
1049 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
1054 struct search_info {
1068 struct berval *query_uuid)
1070 struct search_info *si = op->o_callback->sc_private;
1071 slap_overinst *on = si->on;
1072 cache_manager *cm = on->on_bi.bi_private;
1073 query_manager* qm = cm->qm;
1077 struct berval crp_uuid;
1078 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1079 Operation op_tmp = *op;
1081 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1082 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1084 op_tmp.o_bd = &cm->db;
1085 op_tmp.o_dn = cm->db.be_rootdn;
1086 op_tmp.o_ndn = cm->db.be_rootndn;
1089 LDAP_LOG( BACK_META, DETAIL1, "UUID for query being added = %s\n",
1091 #else /* !NEW_LOGGING */
1092 Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1094 #endif /* !NEW_LOGGING */
1096 for ( e=si->head; e; e=si->head ) {
1097 si->head = e->e_private;
1098 e->e_private = NULL;
1100 LDAP_LOG( BACK_META, DETAIL2, "LOCKING REMOVE MUTEX\n",
1102 #else /* !NEW_LOGGING */
1103 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1104 #endif /* !NEW_LOGGING */
1105 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1107 LDAP_LOG( BACK_META, DETAIL2, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1108 #else /* !NEW_LOGGING */
1109 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1110 #endif /* !NEW_LOGGING */
1111 while ( cm->cur_entries > (cm->max_entries) ) {
1112 qm->crfunc(qm, &crp_uuid);
1113 if (crp_uuid.bv_val) {
1115 LDAP_LOG( BACK_META, DETAIL1,
1116 "Removing query UUID %s\n",
1117 crp_uuid.bv_val, 0, 0 );
1118 #else /* !NEW_LOGGING */
1119 Debug( LDAP_DEBUG_ANY,
1120 "Removing query UUID %s\n",
1121 crp_uuid.bv_val, 0, 0 );
1122 #endif /* !NEW_LOGGING */
1123 return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
1125 LDAP_LOG( BACK_META, DETAIL1,
1126 "QUERY REMOVED, SIZE=%d\n",
1128 #else /* !NEW_LOGGING */
1129 Debug( LDAP_DEBUG_ANY,
1130 "QUERY REMOVED, SIZE=%d\n",
1132 #endif /* !NEW_LOGGING */
1133 ldap_pvt_thread_mutex_lock(
1135 cm->cur_entries -= return_val;
1136 cm->num_cached_queries--;
1138 LDAP_LOG( BACK_META, DETAIL1,
1139 "STORED QUERIES = %lu\n",
1140 cm->num_cached_queries, 0, 0 );
1141 #else /* !NEW_LOGGING */
1142 Debug( LDAP_DEBUG_ANY,
1143 "STORED QUERIES = %lu\n",
1144 cm->num_cached_queries, 0, 0 );
1145 #endif /* !NEW_LOGGING */
1146 ldap_pvt_thread_mutex_unlock(
1149 LDAP_LOG( BACK_META, DETAIL1,
1150 "QUERY REMOVED, CACHE ="
1152 cm->cur_entries, 0, 0 );
1153 #else /* !NEW_LOGGING */
1154 Debug( LDAP_DEBUG_ANY,
1155 "QUERY REMOVED, CACHE ="
1157 cm->cur_entries, 0, 0 );
1158 #endif /* !NEW_LOGGING */
1162 return_val = merge_entry(&op_tmp, e, query_uuid);
1163 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1164 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1165 cm->cur_entries += return_val;
1167 LDAP_LOG( BACK_META, DETAIL1,
1168 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1169 cm->cur_entries, 0, 0 );
1170 #else /* !NEW_LOGGING */
1171 Debug( LDAP_DEBUG_ANY,
1172 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1173 cm->cur_entries, 0, 0 );
1174 #endif /* !NEW_LOGGING */
1176 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1178 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1179 cm->num_cached_queries++;
1181 LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1182 cm->num_cached_queries, 0, 0 );
1183 #else /* !NEW_LOGGING */
1184 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1185 cm->num_cached_queries, 0, 0 );
1186 #endif /* !NEW_LOGGING */
1187 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1193 proxy_cache_response(
1197 struct search_info *si = op->o_callback->sc_private;
1198 slap_overinst *on = si->on;
1199 cache_manager *cm = on->on_bi.bi_private;
1200 query_manager* qm = cm->qm;
1203 if ( rs->sr_type == REP_SEARCH ) {
1205 /* If we haven't exceeded the limit for this query,
1206 * build a chain of answers to store. If we hit the
1207 * limit, empty the chain and ignore the rest.
1210 if ( si->count < si->max ) {
1212 e = entry_dup( rs->sr_entry );
1213 if ( !si->head ) si->head = e;
1214 if ( si->tail ) si->tail->e_private = e;
1219 for (;si->head; si->head=e) {
1220 e = si->head->e_private;
1221 si->head->e_private = NULL;
1222 entry_free(si->head);
1227 } else if ( rs->sr_type == REP_RESULT && !si->over ) {
1228 if ( cache_entries( op, rs, &uuid ) == 0) {
1229 qm->addfunc(qm, &si->query, si->template_id, &uuid);
1230 /* If the consistency checker suspended itself,
1233 if ( cm->cc_paused ) {
1234 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1235 if ( cm->cc_paused ) {
1237 ldap_pvt_runqueue_resched( &syncrepl_rq, cm->cc_arg, 0 );
1239 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1243 return SLAP_CB_CONTINUE;
1249 AttributeName** new_attrs,
1250 AttributeName* attrs,
1251 AttributeName* filter_attrs )
1258 /* duplicate attrs */
1259 if (attrs == NULL) {
1262 for (count=0; attrs[count].an_name.bv_val; count++)
1265 *new_attrs = (AttributeName*)(op->o_tmpalloc((count+1)*
1266 sizeof(AttributeName), op->o_tmpmemctx));
1267 if (attrs == NULL) {
1268 (*new_attrs)[0].an_name.bv_val = "*";
1269 (*new_attrs)[0].an_name.bv_len = 1;
1270 (*new_attrs)[1].an_name.bv_val = NULL;
1271 (*new_attrs)[1].an_name.bv_len = 0;
1275 for (i=0; i<count; i++) {
1276 (*new_attrs)[i].an_name = attrs[i].an_name;
1277 (*new_attrs)[i].an_desc = attrs[i].an_desc;
1279 (*new_attrs)[count].an_name.bv_val = NULL;
1280 (*new_attrs)[count].an_name.bv_len = 0;
1281 alluser = an_find(*new_attrs, &AllUser);
1282 allop = an_find(*new_attrs, &AllOper);
1285 for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1286 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1288 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1293 *new_attrs = (AttributeName*)(op->o_tmprealloc(*new_attrs,
1294 (count+2)*sizeof(AttributeName), op->o_tmpmemctx));
1295 (*new_attrs)[count].an_name = filter_attrs[i].an_name;
1296 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc;
1298 (*new_attrs)[count].an_name.bv_val = NULL;
1299 (*new_attrs)[count].an_name.bv_len = 0;
1308 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1309 cache_manager *cm = on->on_bi.bi_private;
1310 query_manager* qm = cm->qm;
1316 AttributeName *filter_attrs = NULL;
1317 AttributeName *new_attrs = NULL;
1322 int template_id = -1;
1326 int oc_attr_absent = 1;
1328 struct berval tempstr;
1330 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1332 if (filter2template(op->ors_filter, &tempstr, &filter_attrs, &fattr_cnt)) {
1333 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1334 return SLAP_CB_CONTINUE;
1338 LDAP_LOG( BACK_META, DETAIL1, "query template of incoming query = %s\n",
1339 tempstr.bv_val, 0, 0 );
1340 #else /* !NEW_LOGGING */
1341 Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
1342 tempstr.bv_val, 0, 0 );
1343 #endif /* !NEW_LOGGING */
1346 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1348 query.filter = op->ors_filter;
1349 query.attrs = op->ors_attrs;
1350 query.base = op->o_req_ndn;
1351 query.scope = op->ors_scope;
1353 /* check for query containment */
1354 if (attr_set > -1) {
1355 for (i=0; i<cm->numtemplates; i++) {
1356 /* find if template i can potentially answer tempstr */
1357 if (!is_temp_answerable(attr_set, &tempstr, qm, i))
1359 if (attr_set == qm->templates[i].attr_set_index) {
1364 LDAP_LOG( BACK_META, DETAIL2,
1365 "Entering QC, querystr = %s\n",
1366 op->ors_filterstr.bv_val, 0, 0 );
1367 #else /* !NEW_LOGGING */
1368 Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1369 op->ors_filterstr.bv_val, 0, 0 );
1370 #endif /* !NEW_LOGGING */
1371 answerable = (*(qm->qcfunc))(qm, &query, i);
1377 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1380 BackendDB *be = op->o_bd;
1382 LDAP_LOG( BACK_META, DETAIL1, "QUERY ANSWERABLE\n", 0, 0, 0 );
1383 #else /* !NEW_LOGGING */
1384 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
1385 #endif /* !NEW_LOGGING */
1387 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
1389 i = cm->db.bd_info->bi_op_search( op, rs );
1395 LDAP_LOG( BACK_META, DETAIL1, "QUERY NOT ANSWERABLE\n",
1397 #else /* !NEW_LOGGING */
1398 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1399 #endif /* !NEW_LOGGING */
1401 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1402 if (cm->num_cached_queries >= cm->max_queries) {
1405 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1409 struct search_info *si;
1411 LDAP_LOG( BACK_META, DETAIL1,
1412 "QUERY CACHEABLE\n", 0, 0, 0 );
1413 #else /* !NEW_LOGGING */
1414 Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
1415 #endif /* !NEW_LOGGING */
1416 query.filter = str2filter(op->ors_filterstr.bv_val);
1417 if (op->ors_attrs) {
1418 for ( count=0; op->ors_attrs[ count ].an_name.bv_val; count++ ) {
1419 if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass )
1422 query.attrs = (AttributeName*)ch_malloc( ( count + 1 + oc_attr_absent )
1423 *sizeof(AttributeName));
1424 for ( count=0; op->ors_attrs[ count ].an_name.bv_val; count++ ) {
1425 ber_dupbv( &query.attrs[count].an_name, &op->ors_attrs[count].an_name );
1426 query.attrs[count].an_desc = op->ors_attrs[count].an_desc;
1428 if ( oc_attr_absent ) {
1429 query.attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
1430 ber_dupbv( &query.attrs[count].an_name,
1431 &slap_schema.si_ad_objectClass->ad_cname );
1434 query.attrs[ count ].an_name.bv_val = NULL;
1435 query.attrs[ count ].an_name.bv_len = 0;
1437 op->o_tmpfree(op->ors_attrs, op->o_tmpmemctx);
1438 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
1439 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1440 cb->sc_next = op->o_callback;
1441 cb->sc_response = proxy_cache_response;
1442 cb->sc_cleanup = NULL;
1443 cb->sc_private = (cb+1);
1444 si = cb->sc_private;
1447 si->template_id = template_id;
1448 si->max = cm->num_entries_limit ;
1453 op->o_callback = cb;
1456 LDAP_LOG( BACK_META, DETAIL1,
1457 "QUERY NOT CACHEABLE\n",
1459 #else /* !NEW_LOGGING */
1460 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE\n",
1462 #endif /* !NEW_LOGGING */
1467 return SLAP_CB_CONTINUE;
1472 AttributeName* attrs_in,
1473 AttributeName* attrs)
1475 int i, count1, count2;
1476 if ( attrs_in == NULL ) {
1477 return (attrs ? 0 : 1);
1479 if ( attrs == NULL )
1483 attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1487 attrs && attrs[count2].an_name.bv_val != NULL;
1490 if ( count1 != count2 )
1493 for ( i=0; i<count1; i++ ) {
1494 if ( !an_find(attrs, &attrs_in[i].an_name ))
1502 AttributeName* attrs,
1507 for (i=0; i<num; i++) {
1508 if (attrscmp(attrs, qm->attr_sets[i].attrs))
1519 struct re_s *rtask = arg;
1520 slap_overinst *on = rtask->arg;
1521 cache_manager *cm = on->on_bi.bi_private;
1522 query_manager *qm = cm->qm;
1524 Connection conn = {0};
1526 SlapReply rs = {REP_RESULT};
1527 CachedQuery* query, *query_prev;
1528 int i, return_val, pause = 1;
1529 QueryTemplate* templ;
1531 connection_fake_init( &conn, &op, ctx );
1534 op.o_dn = cm->db.be_rootdn;
1535 op.o_ndn = cm->db.be_rootndn;
1539 for (i=0; qm->templates[i].querystr.bv_val; i++) {
1540 templ = qm->templates + i;
1541 query = templ->query_last;
1542 if ( query ) pause = 0;
1543 op.o_time = slap_get_time();
1544 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1545 while (query && (query->expiry_time < op.o_time)) {
1546 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1547 remove_query(qm, query);
1548 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1550 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n",
1552 #else /* !NEW_LOGGING */
1553 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1555 #endif /* !NEW_LOGGING */
1556 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1557 remove_from_template(query, templ);
1559 LDAP_LOG( BACK_META, DETAIL1,
1560 "TEMPLATE %d QUERIES-- %d\n",
1561 i, templ->no_of_queries, 0 );
1562 #else /* !NEW_LOGGING */
1563 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1564 i, templ->no_of_queries, 0 );
1565 #endif /* !NEW_LOGGING */
1567 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n",
1569 #else /* !NEW_LOGGING */
1570 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1572 #endif /* !NEW_LOGGING */
1573 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1574 return_val = remove_query_data(&op, &rs, &query->q_uuid);
1576 LDAP_LOG( BACK_META, DETAIL1,
1577 "STALE QUERY REMOVED, SIZE=%d\n",
1579 #else /* !NEW_LOGGING */
1580 Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1582 #endif /* !NEW_LOGGING */
1583 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1584 cm->cur_entries -= return_val;
1585 cm->num_cached_queries--;
1587 LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1588 cm->num_cached_queries, 0, 0 );
1589 #else /* !NEW_LOGGING */
1590 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1591 cm->num_cached_queries, 0, 0 );
1592 #endif /* !NEW_LOGGING */
1593 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1595 LDAP_LOG( BACK_META, DETAIL1,
1596 "STALE QUERY REMOVED, CACHE ="
1598 cm->cur_entries, 0, 0 );
1599 #else /* !NEW_LOGGING */
1600 Debug( LDAP_DEBUG_ANY,
1601 "STALE QUERY REMOVED, CACHE ="
1603 cm->cur_entries, 0, 0 );
1604 #endif /* !NEW_LOGGING */
1606 query = query->prev;
1607 free_query(query_prev);
1609 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1611 /* If there were no queries, defer processing for a while */
1613 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1615 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
1616 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
1618 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 1 );
1619 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1625 #define MAX_ATTR_SETS 500
1626 static void find_supersets( struct attr_set* attr_sets, int numsets );
1627 static int compare_sets( struct attr_set* setA, int, int );
1638 slap_overinst *on = (slap_overinst *)be->bd_info;
1639 cache_manager* cm = on->on_bi.bi_private;
1640 query_manager* qm = cm->qm;
1641 QueryTemplate* temp;
1642 AttributeName* attr_name;
1643 AttributeName* attrarray;
1644 const char* text=NULL;
1649 if ( strcasecmp( argv[0], "proxycache" ) == 0 ) {
1651 fprintf( stderr, "%s: line %d: missing arguments in \"proxycache"
1652 " <backend> <max_entries> <numattrsets> <entry limit> "
1653 "<cycle_time>\"\n", fname, lineno );
1657 cm->db.bd_info = backend_info( argv[1] );
1658 if ( !cm->db.bd_info ) {
1659 fprintf( stderr, "%s: line %d: backend %s unknown\n",
1660 fname, lineno, argv[1] );
1663 if ( cm->db.bd_info->bi_db_init( &cm->db ) ) return( 1 );
1665 /* This type is in use, needs to be opened */
1666 cm->db.bd_info->bi_nDB++;
1668 cm->max_entries = atoi( argv[2] );
1670 cm->numattrsets = atoi( argv[3] );
1671 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1672 fprintf( stderr, "%s: line %d: numattrsets must be <= %d\n",
1673 fname, lineno, MAX_ATTR_SETS );
1677 cm->num_entries_limit = atoi( argv[4] );
1678 cm->cc_period = atoi( argv[5] );
1680 LDAP_LOG( BACK_META, DETAIL1,
1681 "Total # of attribute sets to be cached = %d\n",
1682 cm->numattrsets, 0, 0 );
1684 Debug( LDAP_DEBUG_ANY,
1685 "Total # of attribute sets to be cached = %d\n",
1686 cm->numattrsets, 0, 0 );
1688 qm->attr_sets = ( struct attr_set * )ch_malloc( cm->numattrsets *
1689 sizeof( struct attr_set ));
1690 for ( i = 0; i < cm->numattrsets; i++ ) {
1691 qm->attr_sets[i].attrs = NULL;
1694 } else if ( strcasecmp( argv[0], "proxyattrset" ) == 0 ) {
1696 fprintf( stderr, "%s: line %d: missing arguments in \"proxyattrset "
1697 "<index> <attributes>\"\n", fname, lineno );
1701 LDAP_LOG( BACK_META, DETAIL1, "Attribute Set # %d\n",
1702 atoi( argv[1] ), 0, 0 );
1704 Debug( LDAP_DEBUG_ANY, "Attribute Set # %d\n",
1705 atoi( argv[1] ), 0, 0 );
1707 if (atoi(argv[1]) >= cm->numattrsets) {
1708 fprintf( stderr, "%s; line %d index out of bounds \n",
1712 index = atoi( argv[1] );
1713 if ( argv[2] && strcmp( argv[2], "*" ) ) {
1714 qm->attr_sets[index].count = argc - 2;
1715 qm->attr_sets[index].attrs = (AttributeName*)ch_malloc(
1716 (argc-1) * sizeof( AttributeName ));
1717 attr_name = qm->attr_sets[index].attrs;
1718 for ( i = 2; i < argc; i++ ) {
1720 LDAP_LOG( BACK_META, DETAIL1, "\t %s\n",
1723 Debug( LDAP_DEBUG_ANY, "\t %s\n",
1726 ber_str2bv( argv[i], 0, 1,
1727 &attr_name->an_name);
1728 attr_name->an_desc = NULL;
1729 slap_bv2ad( &attr_name->an_name,
1730 &attr_name->an_desc, &text );
1732 attr_name->an_name.bv_val = NULL;
1733 attr_name->an_name.bv_len = 0;
1736 } else if ( strcasecmp( argv[0], "proxytemplate" ) == 0 ) {
1738 fprintf( stderr, "%s: line %d: missing argument(s) in "
1739 "\"proxytemplate <filter> <proj attr set> <TTL>\" line\n",
1743 if (( i = atoi( argv[2] )) >= cm->numattrsets ) {
1745 LDAP_LOG( BACK_META, DETAIL1,
1746 "%s: line %d, template index invalid\n",
1749 Debug( LDAP_DEBUG_ANY,
1750 "%s: line %d, template index invalid\n",
1755 num = cm->numtemplates;
1757 find_supersets( qm->attr_sets, cm->numattrsets );
1758 qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
1759 ( num + 2 ) * sizeof( QueryTemplate ));
1760 temp = qm->templates + num;
1761 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
1762 temp->query = temp->query_last = NULL;
1763 temp->ttl = atoi( argv[3] );
1764 temp->no_of_queries = 0;
1765 if ( argv[1] == NULL ) {
1767 LDAP_LOG( BACK_META, DETAIL1,
1768 "Templates string not specified "
1769 "for template %d\n", num, 0, 0 );
1771 Debug( LDAP_DEBUG_ANY,
1772 "Templates string not specified "
1773 "for template %d\n", num, 0, 0 );
1777 ber_str2bv( argv[1], 0, 1, &temp->querystr );
1779 LDAP_LOG( BACK_META, DETAIL1, "Template:\n", 0, 0, 0 );
1781 Debug( LDAP_DEBUG_ANY, "Template:\n", 0, 0, 0 );
1784 LDAP_LOG( BACK_META, DETAIL1, " query template: %s\n",
1785 temp->querystr.bv_val, 0, 0 );
1787 Debug( LDAP_DEBUG_ANY, " query template: %s\n",
1788 temp->querystr.bv_val, 0, 0 );
1790 temp->attr_set_index = i;
1792 LDAP_LOG( BACK_META, DETAIL1, " attributes: \n", 0, 0, 0 );
1794 Debug( LDAP_DEBUG_ANY, " attributes: \n", 0, 0, 0 );
1796 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1797 for ( i=0; attrarray[i].an_name.bv_val; i++ )
1799 LDAP_LOG( BACK_META, DETAIL1, "\t%s\n",
1800 attrarray[i].an_name.bv_val, 0, 0 );
1802 Debug( LDAP_DEBUG_ANY, "\t%s\n",
1803 attrarray[i].an_name.bv_val, 0, 0 );
1807 temp->querystr.bv_val = NULL;
1812 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno, argc, argv );
1822 slap_overinst *on = (slap_overinst *)be->bd_info;
1826 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1827 on->on_bi.bi_private = cm;
1829 qm = (query_manager*)ch_malloc(sizeof(query_manager));
1832 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1833 cm->db.be_private = NULL;
1835 cm->numattrsets = 0;
1836 cm->numtemplates = 0;
1837 cm->num_entries_limit = 5;
1838 cm->num_cached_queries = 0;
1839 cm->max_entries = 0;
1840 cm->cur_entries = 0;
1841 cm->max_queries = 10000;
1842 cm->cc_period = 1000;
1845 qm->attr_sets = NULL;
1846 qm->templates = NULL;
1848 qm->lru_bottom = NULL;
1850 qm->qcfunc = query_containment;
1851 qm->crfunc = cache_replacement;
1852 qm->addfunc = add_query;
1853 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1855 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1856 ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1865 slap_overinst *on = (slap_overinst *)be->bd_info;
1866 cache_manager *cm = on->on_bi.bi_private;
1869 if ( cm->db.bd_info->bi_db_open ) {
1870 rc = cm->db.bd_info->bi_db_open( &cm->db );
1873 /* There is no runqueue in TOOL mode */
1874 if ( slapMode & SLAP_SERVER_MODE ) {
1875 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1876 ldap_pvt_runqueue_insert( &syncrepl_rq, cm->cc_period,
1877 consistency_check, on );
1878 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1889 slap_overinst *on = (slap_overinst *)be->bd_info;
1890 cache_manager *cm = on->on_bi.bi_private;
1891 query_manager *qm = cm->qm;
1894 if ( cm->db.bd_info->bi_db_close ) {
1895 rc = cm->db.bd_info->bi_db_close( &cm->db );
1897 for ( i=0; i<cm->numtemplates; i++ ) {
1898 CachedQuery *qc, *qn;
1899 for ( qc = qm->templates[i].query; qc; qc = qn ) {
1903 free( qm->templates[i].querystr.bv_val );
1904 ldap_pvt_thread_rdwr_destroy( &qm->templates[i].t_rwlock );
1906 free( qm->templates );
1907 qm->templates = NULL;
1909 for ( i=0; i<cm->numattrsets; i++ ) {
1910 free( qm->attr_sets[i].ID_array );
1911 for ( j=0; j<qm->attr_sets[i].count; j++ ) {
1912 free( qm->attr_sets[i].attrs[j].an_name.bv_val );
1914 free( qm->attr_sets[i].attrs );
1916 free( qm->attr_sets );
1917 qm->attr_sets = NULL;
1923 proxy_cache_destroy(
1927 slap_overinst *on = (slap_overinst *)be->bd_info;
1928 cache_manager *cm = on->on_bi.bi_private;
1929 query_manager *qm = cm->qm;
1932 if ( cm->db.bd_info->bi_db_destroy ) {
1933 rc = cm->db.bd_info->bi_db_destroy( &cm->db );
1935 ldap_pvt_thread_mutex_destroy(&qm->lru_mutex);
1936 ldap_pvt_thread_mutex_destroy(&cm->cache_mutex);
1937 ldap_pvt_thread_mutex_destroy(&cm->remove_mutex);
1944 find_supersets ( struct attr_set* attr_sets, int numsets )
1946 int num[MAX_ATTR_SETS];
1949 for ( i = 0; i < MAX_ATTR_SETS; i++ )
1952 for ( i = 0; i < numsets; i++ ) {
1953 attr_sets[i].ID_array = (int*) ch_malloc( sizeof( int ) );
1954 attr_sets[i].ID_array[0] = -1;
1957 for ( i = 0; i < numsets; i++ ) {
1958 for ( j=i+1; j < numsets; j++ ) {
1959 res = compare_sets( attr_sets, i, j );
1965 id_array = attr_sets[i].ID_array;
1966 attr_sets[i].ID_array = (int *) ch_realloc( id_array,
1967 ( num[i] + 2 ) * sizeof( int ));
1968 attr_sets[i].ID_array[num[i]] = j;
1969 attr_sets[i].ID_array[num[i]+1] = -1;
1974 id_array = attr_sets[j].ID_array;
1975 attr_sets[j].ID_array = (int *) ch_realloc( id_array,
1976 ( num[j] + 2 ) * sizeof( int ));
1977 attr_sets[j].ID_array[num[j]] = i;
1978 attr_sets[j].ID_array[num[j]+1] = -1;
1987 * compares two sets of attributes (indices i and j)
1988 * returns 0: if neither set is contained in the other set
1989 * 1: if set i is contained in set j
1990 * 2: if set j is contained in set i
1991 * 3: the sets are equivalent
1995 compare_sets(struct attr_set* set, int i, int j)
2001 if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
2004 if ( set[i].attrs == NULL )
2007 if ( set[j].attrs == NULL )
2010 numI = set[i].count;
2011 numJ = set[j].count;
2013 for ( l=0; l < numI; l++ ) {
2014 for ( k = 0; k < numJ; k++ ) {
2015 if ( strcmp( set[i].attrs[l].an_name.bv_val,
2016 set[j].attrs[k].an_name.bv_val ) == 0 )
2021 if ( common == numI )
2024 if ( common == numJ )
2030 static slap_overinst proxy_cache;
2034 LDAPAttributeType *at;
2038 at = ldap_str2attributetype( queryid_schema, &code, &err,
2039 LDAP_SCHEMA_ALLOW_ALL );
2041 fprintf( stderr, "AttributeType Load failed %s %s\n",
2042 ldap_scherr2str(code), err );
2045 code = at_add( at, &err );
2047 slap_str2ad( at->at_names[0], &ad_queryid, &err );
2051 fprintf( stderr, "AttributeType Load failed %s %s\n",
2052 scherr2str(code), err );
2056 proxy_cache.on_bi.bi_type = "proxycache";
2057 proxy_cache.on_bi.bi_db_init = proxy_cache_init;
2058 proxy_cache.on_bi.bi_db_config = proxy_cache_config;
2059 proxy_cache.on_bi.bi_db_open = proxy_cache_open;
2060 proxy_cache.on_bi.bi_db_close = proxy_cache_close;
2061 proxy_cache.on_bi.bi_db_destroy = proxy_cache_destroy;
2062 proxy_cache.on_bi.bi_op_search = proxy_cache_search;
2064 return overlay_register( &proxy_cache );
2067 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
2068 int init_module(int argc, char *argv[]) {
2069 return pcache_init();
2073 #endif /* defined(SLAPD_OVER_PROXYCACHE) */