2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003 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.
21 * This work was rewritten by Howard Chu for OpenLDAP.
26 #ifdef SLAPD_OVER_PROXYCACHE
30 #include <ac/string.h>
37 /* query cache structs */
40 typedef struct Query_s {
41 Filter* filter; /* Search Filter */
42 AttributeName* attrs; /* Projected attributes */
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 BackendInfo *bi; /* underlying database info */
113 void *be_private; /* for the underlying database */
114 unsigned long num_cached_queries; /* total number of cached queries */
115 unsigned long max_queries; /* upper bound on # of cached queries */
116 int numattrsets; /* number of attribute sets */
117 int numtemplates; /* number of cacheable templates */
118 int cur_entries; /* current number of entries cached */
119 int max_entries; /* max number of entries cached */
120 int num_entries_limit; /* max # of entries in a cacheable query */
122 int cc_period; /* interval between successive consistency checks (sec) */
123 int cc_thread_started;
124 ldap_pvt_thread_t cc_thread;
126 ldap_pvt_thread_mutex_t cache_mutex;
127 ldap_pvt_thread_mutex_t remove_mutex;
129 query_manager* qm; /* query cache managed by the cache manager */
132 static AttributeDescription *ad_queryid;
133 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
134 "DESC 'list of queries the entry belongs to' "
135 "EQUALITY octetStringMatch "
136 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
137 "NO-USER-MODIFICATION USAGE directoryOperation )";
143 struct berval* query_uuid )
146 Modifications* modlist = NULL;
147 const char* text = NULL;
149 char textbuf[SLAP_TEXT_BUFLEN];
150 size_t textlen = sizeof(textbuf);
152 SlapReply sreply = {REP_RESULT};
154 slap_callback cb = { slap_null_cb, NULL };
159 /* add queryid attribute */
160 attr_merge_one( e, ad_queryid, query_uuid, NULL );
162 /* append the attribute list from the fetched entry */
163 e->e_attrs->a_next = attr;
165 op->o_tag = LDAP_REQ_ADD;
166 op->o_protocol = LDAP_VERSION3;
167 op->o_callback = &cb;
168 op->o_time = slap_get_time();
169 op->o_do_not_cache = 1;
172 op->o_req_dn = e->e_name;
173 op->o_req_ndn = e->e_nname;
174 rc = op->o_bd->be_add( op, &sreply );
176 if ( rc != LDAP_SUCCESS ) {
177 if ( rc == LDAP_ALREADY_EXISTS ) {
178 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
179 modlist->sml_op = LDAP_MOD_ADD;
180 op->o_tag = LDAP_REQ_MODIFY;
181 op->orm_modlist = modlist;
182 rc = op->o_bd->be_modify( op, &sreply );
183 slap_mods_free( modlist );
184 } else if ( rc == LDAP_REFERRAL ||
185 rc == LDAP_NO_SUCH_OBJECT ) {
186 syncrepl_add_glue( op, e );
191 be_entry_release_w( op, e );
197 /* compare base and scope of incoming and cached queries */
198 static int base_scope_compare(
199 struct berval* ndn_stored,
200 struct berval* ndn_incoming,
204 struct berval pdn_incoming = { 0, NULL };
206 if (scope_stored < scope_incoming)
209 if ( !dnIsSuffix(ndn_incoming, ndn_stored))
212 switch(scope_stored) {
213 case LDAP_SCOPE_BASE:
214 return (ndn_incoming->bv_len == ndn_stored->bv_len);
216 case LDAP_SCOPE_ONELEVEL:
217 switch(scope_incoming){
218 case LDAP_SCOPE_BASE:
219 dnParent(ndn_incoming, &pdn_incoming);
220 return (pdn_incoming.bv_len == ndn_stored->bv_len);
222 case LDAP_SCOPE_ONELEVEL:
223 return (ndn_incoming->bv_len == ndn_stored->bv_len);
228 case LDAP_SCOPE_SUBTREE:
237 /* add query on top of LRU list */
239 add_query_on_top (query_manager* qm, CachedQuery* qc)
241 CachedQuery* top = qm->lru_top;
242 Query* q = (Query*)qc;
254 LDAP_LOG( BACK_META, DETAIL1, "Base of added query = %s\n",
255 q->base.bv_val, 0, 0 );
257 Debug( LDAP_DEBUG_ANY, "Base of added query = %s\n",
258 q->base.bv_val, 0, 0 );
262 /* remove_query from LRU list */
265 remove_query (query_manager* qm, CachedQuery* qc)
288 qc->lru_up = qc->lru_down = NULL;
292 invert_string( struct berval *bv )
297 for (i=0; i<bv->bv_len/2; i++) {
299 bv->bv_val[i] = bv->bv_val[bv->bv_len-i-1];
300 bv->bv_val[bv->bv_len-i-1] = c;
304 /* find and remove string2 from string1
305 * from start if position = 1,
306 * from end if position = 3,
307 * from anywhere if position = 2
311 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
318 char* string1=ber1->bv_val;
319 char* string2=ber2->bv_val;
334 temp = strstr(arg1, arg2);
338 if ( temp && (position == 2 || temp == arg1) ) {
340 strcpy( arg1, string1 );
344 if ( position == 3 ) {
352 static struct berval*
353 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
355 struct berval* merged, *temp;
356 int i, any_count, count;
358 for (any_count=0; any && any[any_count].bv_val; any_count++)
368 merged = (struct berval*)(ch_malloc((count+1)*sizeof(struct berval)));
375 for (i=0; i<any_count; i++) {
388 strings_containment(struct berval* stored, struct berval* incoming)
390 struct berval* element;
394 for ( element=stored; element->bv_val != NULL; element++ ) {
395 for (j = k; incoming[j].bv_val != NULL; j++) {
396 if (find_and_remove(&(incoming[j]), element, 2)) {
413 substr_containment_substr(Filter* stored, Filter* incoming)
419 struct berval init_incoming;
420 struct berval final_incoming;
421 struct berval *any_incoming = NULL;
422 struct berval *remaining_incoming = NULL;
424 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
425 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
429 ber_dupbv(&init_incoming, &(incoming->f_sub_initial));
430 ber_dupbv(&final_incoming, &(incoming->f_sub_final));
432 if (incoming->f_sub_any) {
433 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
437 any_incoming = (struct berval*)ch_malloc((any_count+1) *
438 sizeof(struct berval));
440 for (i=0; i<any_count; i++) {
441 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
443 any_incoming[any_count].bv_val = NULL;
444 any_incoming[any_count].bv_len = 0;
447 if (find_and_remove(&init_incoming,
448 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
449 &(stored->f_sub_final), 3))
451 if (stored->f_sub_any == NULL) {
455 remaining_incoming = merge_init_final(&init_incoming,
456 any_incoming, &final_incoming);
457 rc = strings_containment(stored->f_sub_any, remaining_incoming);
460 free(init_incoming.bv_val);
461 free(final_incoming.bv_val);
462 if (any_incoming) ber_bvarray_free( any_incoming );
463 free(remaining_incoming);
469 substr_containment_equality(Filter* stored, Filter* incoming)
471 struct berval incoming_val[2];
474 ber_dupbv(incoming_val, &(incoming->f_av_value));
475 incoming_val[1].bv_val = NULL;
476 incoming_val[1].bv_len = 0;
478 if (find_and_remove(incoming_val,
479 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
480 &(stored->f_sub_final), 3)) {
481 if (stored->f_sub_any == NULL){
485 rc = strings_containment(stored->f_sub_any, incoming_val);
488 free(incoming_val[0].bv_val);
492 /* check whether query is contained in any of
493 * the cached queries in template template_index
496 query_containment(query_manager *qm,
500 QueryTemplate* templa= qm->templates;
503 Filter* inputf = query->filter;
504 struct berval* base = &(query->base);
505 int scope = query->scope;
512 MatchingRule* mrule = NULL;
513 if (inputf != NULL) {
515 LDAP_LOG( BACK_META, DETAIL1, "Lock QC index = %d\n",
516 template_index, 0, 0 );
518 Debug( LDAP_DEBUG_ANY, "Lock QC index = %d\n",
519 template_index, 0, 0 );
521 ldap_pvt_thread_rdwr_rlock(&(templa[template_index].t_rwlock));
522 for(qc=templa[template_index].query; qc != NULL; qc= qc->next) {
524 if(base_scope_compare(&(q->base), base, q->scope, scope)) {
529 switch (fs->f_choice) {
530 case LDAP_FILTER_EQUALITY:
531 if (fi->f_choice == LDAP_FILTER_EQUALITY)
532 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
538 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
544 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
545 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
546 &(fi->f_ava->aa_value),
547 &(fs->f_ava->aa_value), &text);
548 if (rc != LDAP_SUCCESS) {
549 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
551 LDAP_LOG( BACK_META, DETAIL1,
552 "Unlock: Exiting QC index=%d\n",
553 template_index, 0, 0 );
555 Debug( LDAP_DEBUG_ANY,
556 "Unlock: Exiting QC index=%d\n",
557 template_index, 0, 0 );
562 switch (fs->f_choice) {
564 case LDAP_FILTER_AND:
569 case LDAP_FILTER_SUBSTRINGS:
570 /* check if the equality query can be
571 * answered with cached substring query */
572 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
573 && substr_containment_equality(
576 /* check if the substring query can be
577 * answered with cached substring query */
578 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
579 ) && substr_containment_substr(
585 case LDAP_FILTER_PRESENT:
590 case LDAP_FILTER_EQUALITY:
608 case LDAP_FILTER_NOT:
614 } while((res) && (fi != NULL) && (fs != NULL));
617 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
618 if (qm->lru_top != qc) {
619 remove_query(qm, qc);
620 add_query_on_top(qm, qc);
622 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
628 LDAP_LOG( BACK_META, DETAIL1,
629 "Not answerable: Unlock QC index=%d\n",
630 template_index, 0, 0 );
632 Debug( LDAP_DEBUG_ANY,
633 "Not answerable: Unlock QC index=%d\n",
634 template_index, 0, 0 );
636 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
642 free_query (CachedQuery* qc)
644 Query* q = (Query*)qc;
645 free(qc->q_uuid.bv_val);
646 filter_free(q->filter);
647 free (q->base.bv_val);
654 /* Add query to query cache */
655 static void add_query(
661 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
662 QueryTemplate* templ = (qm->templates)+template_index;
664 new_cached_query->template_id = template_index;
665 new_cached_query->q_uuid = *uuid;
666 new_cached_query->lru_up = NULL;
667 new_cached_query->lru_down = NULL;
668 new_cached_query->expiry_time = slap_get_time() + templ->ttl;
670 LDAP_LOG( BACK_META, DETAIL1, "Added query expires at %ld\n",
671 (long) new_cached_query->expiry_time, 0, 0 );
673 Debug( LDAP_DEBUG_ANY, "Added query expires at %ld\n",
674 (long) new_cached_query->expiry_time, 0, 0 );
676 new_query = (Query*)new_cached_query;
678 ber_dupbv(&new_query->base, &query->base);
679 new_query->scope = query->scope;
680 new_query->filter = query->filter;
681 new_query->attrs = query->attrs;
685 LDAP_LOG( BACK_META, DETAIL1, "Lock AQ index = %d\n",
686 template_index, 0, 0 );
688 Debug( LDAP_DEBUG_ANY, "Lock AQ index = %d\n",
689 template_index, 0, 0 );
691 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
692 if (templ->query == NULL)
693 templ->query_last = new_cached_query;
695 templ->query->prev = new_cached_query;
696 new_cached_query->next = templ->query;
697 new_cached_query->prev = NULL;
698 templ->query = new_cached_query;
699 templ->no_of_queries++;
701 LDAP_LOG( BACK_META, DETAIL1, "TEMPLATE %d QUERIES++ %d\n",
702 template_index, templ->no_of_queries, 0 );
704 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES++ %d\n",
705 template_index, templ->no_of_queries, 0 );
709 LDAP_LOG( BACK_META, DETAIL1, "Unlock AQ index = %d \n",
710 template_index, 0, 0 );
712 Debug( LDAP_DEBUG_ANY, "Unlock AQ index = %d \n",
713 template_index, 0, 0 );
715 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
717 /* Adding on top of LRU list */
718 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
719 add_query_on_top(qm, new_cached_query);
720 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
724 remove_from_template (CachedQuery* qc, QueryTemplate* template)
726 if (!qc->prev && !qc->next) {
727 template->query_last = template->query = NULL;
728 } else if (qc->prev == NULL) {
729 qc->next->prev = NULL;
730 template->query = qc->next;
731 } else if (qc->next == NULL) {
732 qc->prev->next = NULL;
733 template->query_last = qc->prev;
735 qc->next->prev = qc->prev;
736 qc->prev->next = qc->next;
739 template->no_of_queries--;
742 /* remove bottom query of LRU list from the query cache */
743 static void cache_replacement(query_manager* qm, struct berval *result)
748 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
749 bottom = qm->lru_bottom;
751 result->bv_val = NULL;
756 LDAP_LOG ( BACK_META, DETAIL1,
757 "Cache replacement invoked without "
758 "any query in LRU list\n", 0, 0, 0 );
760 Debug ( LDAP_DEBUG_ANY,
761 "Cache replacement invoked without "
762 "any query in LRU list\n", 0, 0, 0 );
767 temp_id = bottom->template_id;
768 remove_query(qm, bottom);
769 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
771 *result = bottom->q_uuid;
772 bottom->q_uuid.bv_val = NULL;
775 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n", temp_id, 0, 0 );
777 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n", temp_id, 0, 0 );
779 ldap_pvt_thread_rdwr_wlock(&(qm->templates[temp_id].t_rwlock));
780 remove_from_template(bottom, (qm->templates+temp_id));
782 LDAP_LOG( BACK_META, DETAIL1, "TEMPLATE %d QUERIES-- %d\n",
783 temp_id, qm->templates[temp_id].no_of_queries, 0 );
785 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
786 temp_id, qm->templates[temp_id].no_of_queries, 0 );
789 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n", temp_id, 0, 0 );
791 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n", temp_id, 0, 0 );
793 ldap_pvt_thread_rdwr_wunlock(&(qm->templates[temp_id].t_rwlock));
808 struct query_info *info = op->o_callback->sc_private;
811 struct berval vals[2];
814 Operation op_tmp = *op;
816 SlapReply sreply = {REP_RESULT};
818 if (rs->sr_type == REP_RESULT)
821 for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
822 if (attr->a_desc == ad_queryid) {
823 for (count=0; attr->a_vals[count].bv_val; count++)
834 LDAP_LOG( BACK_META, DETAIL1,
835 "DELETING ENTRY TEMPLATE=%s\n",
836 attr->a_vals[0].bv_val, 0, 0 );
838 Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
839 attr->a_vals[0].bv_val, 0, 0 );
842 op_tmp.o_req_dn = rs->sr_entry->e_name;
843 op_tmp.o_req_ndn = rs->sr_entry->e_nname;
845 if (op->o_bd->be_delete(&op_tmp, rs) == LDAP_SUCCESS) {
851 vals[0] = *info->uuid;
852 vals[1].bv_val = NULL;
854 mod.sml_op = LDAP_MOD_DELETE;
855 mod.sml_desc = ad_queryid;
856 mod.sml_type = ad_queryid->ad_cname;
857 mod.sml_bvalues = vals;
860 LDAP_LOG( BACK_META, DETAIL1,
861 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
862 attr->a_vals[0].bv_val, 0, 0 );
864 Debug( LDAP_DEBUG_ANY, "REMOVING TEMP ATTR : TEMPLATE=%s\n",
865 attr->a_vals[0].bv_val, 0, 0 );
868 op_tmp.o_req_dn = rs->sr_entry->e_name;
869 op_tmp.o_req_ndn = rs->sr_entry->e_nname;
870 op_tmp.orm_modlist = &mod;
872 op->o_bd->be_modify( &op_tmp, &sreply );
881 struct berval* query_uuid)
883 struct query_info info;
885 Filter filter = {LDAP_FILTER_EQUALITY};
886 SlapReply sreply = {REP_RESULT};
887 slap_callback cb = { remove_func, NULL };
889 sreply.sr_entry = NULL;
890 sreply.sr_nentries = 0;
891 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
892 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
893 filter.f_av_desc = ad_queryid;
894 filter.f_av_value = *query_uuid;
895 info.uuid = query_uuid;
897 cb.sc_private = &info;
899 op->o_tag = LDAP_REQ_SEARCH;
900 op->o_protocol = LDAP_VERSION3;
901 op->o_callback = &cb;
902 op->o_time = slap_get_time();
903 op->o_do_not_cache = 1;
905 op->o_req_dn = op->o_bd->be_suffix[0];
906 op->o_req_ndn = op->o_bd->be_nsuffix[0];
907 op->ors_scope = LDAP_SCOPE_SUBTREE;
908 op->ors_deref = LDAP_DEREF_NEVER;
911 op->ors_filter = &filter;
912 op->ors_filterstr.bv_val = filter_str;
913 op->ors_filterstr.bv_len = strlen(filter_str);
914 op->ors_attrs = NULL;
915 op->ors_attrsonly = 0;
917 op->o_bd->be_search( op, &sreply );
924 AttributeName* attrs,
931 AttributeName* attrs_in,
938 struct berval* tempstr,
942 QueryTemplate *qt = qm->templates + template_id;
944 if (attr_set != qt->attr_set_index) {
945 int* id_array = qm->attr_sets[attr_set].ID_array;
947 while (*id_array != -1) {
948 if (*id_array == qt->attr_set_index)
955 return (qt->querystr.bv_len == tempstr->bv_len &&
956 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
968 AttributeName** filter_attrs,
971 AttributeDescription *ad;
973 switch ( f->f_choice ) {
974 case LDAP_FILTER_EQUALITY:
976 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
977 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
982 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
983 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
988 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
989 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
992 case LDAP_FILTER_APPROX:
994 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
995 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
998 case LDAP_FILTER_SUBSTRINGS:
1000 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
1001 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
1004 case LDAP_FILTER_PRESENT:
1006 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
1007 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
1010 case LDAP_FILTER_AND:
1011 case LDAP_FILTER_OR:
1012 case LDAP_FILTER_NOT: {
1014 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
1015 f->f_choice == LDAP_FILTER_AND ? '&' :
1016 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
1017 fstr->bv_len += sizeof("(%") - 1;
1019 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1020 rc = filter2template( f, fstr, filter_attrs, filter_cnt );
1023 sprintf( fstr->bv_val+fstr->bv_len, ")" );
1024 fstr->bv_len += sizeof(")") - 1;
1030 strcpy( fstr->bv_val, "(?=?)" );
1031 fstr->bv_len += sizeof("(?=?)")-1;
1035 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
1036 (*filter_cnt + 2)*sizeof(AttributeName));
1038 (*filter_attrs)[*filter_cnt].an_desc = ad;
1039 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
1040 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
1041 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
1046 struct search_info {
1048 slap_callback *prev;
1061 struct berval *query_uuid)
1063 struct search_info *si = op->o_callback->sc_private;
1064 slap_overinst *on = si->on;
1065 cache_manager *cm = on->on_bi.bi_private;
1066 query_manager* qm = cm->qm;
1070 struct berval crp_uuid;
1071 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1072 Operation op_tmp = *op;
1073 BackendDB db = *op->o_bd;
1075 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1076 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1079 op_tmp.o_dn = db.be_rootdn;
1080 op_tmp.o_ndn = db.be_rootndn;
1081 db.bd_info = cm->bi;
1082 db.be_private =cm->be_private;
1083 db.be_flags |= SLAP_BFLAG_NO_SCHEMA_CHECK;
1086 LDAP_LOG( BACK_META, DETAIL1, "UUID for query being added = %s\n",
1088 #else /* !NEW_LOGGING */
1089 Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1091 #endif /* !NEW_LOGGING */
1093 for ( e=si->head; e; e=si->head ) {
1094 si->head = e->e_private;
1095 e->e_private = NULL;
1097 LDAP_LOG( BACK_META, DETAIL2, "LOCKING REMOVE MUTEX\n",
1099 #else /* !NEW_LOGGING */
1100 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1101 #endif /* !NEW_LOGGING */
1102 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1104 LDAP_LOG( BACK_META, DETAIL2, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1105 #else /* !NEW_LOGGING */
1106 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1107 #endif /* !NEW_LOGGING */
1108 while ( cm->cur_entries > (cm->max_entries) ) {
1109 qm->crfunc(qm, &crp_uuid);
1110 if (crp_uuid.bv_val) {
1112 LDAP_LOG( BACK_META, DETAIL1,
1113 "Removing query UUID %s\n",
1114 crp_uuid.bv_val, 0, 0 );
1115 #else /* !NEW_LOGGING */
1116 Debug( LDAP_DEBUG_ANY,
1117 "Removing query UUID %s\n",
1118 crp_uuid.bv_val, 0, 0 );
1119 #endif /* !NEW_LOGGING */
1120 return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
1122 LDAP_LOG( BACK_META, DETAIL1,
1123 "QUERY REMOVED, SIZE=%d\n",
1125 #else /* !NEW_LOGGING */
1126 Debug( LDAP_DEBUG_ANY,
1127 "QUERY REMOVED, SIZE=%d\n",
1129 #endif /* !NEW_LOGGING */
1130 ldap_pvt_thread_mutex_lock(
1132 cm->cur_entries -= return_val;
1133 cm->num_cached_queries--;
1135 LDAP_LOG( BACK_META, DETAIL1,
1136 "STORED QUERIES = %lu\n",
1137 cm->num_cached_queries, 0, 0 );
1138 #else /* !NEW_LOGGING */
1139 Debug( LDAP_DEBUG_ANY,
1140 "STORED QUERIES = %lu\n",
1141 cm->num_cached_queries, 0, 0 );
1142 #endif /* !NEW_LOGGING */
1143 ldap_pvt_thread_mutex_unlock(
1146 LDAP_LOG( BACK_META, DETAIL1,
1147 "QUERY REMOVED, CACHE ="
1149 cm->cur_entries, 0, 0 );
1150 #else /* !NEW_LOGGING */
1151 Debug( LDAP_DEBUG_ANY,
1152 "QUERY REMOVED, CACHE ="
1154 cm->cur_entries, 0, 0 );
1155 #endif /* !NEW_LOGGING */
1159 return_val = merge_entry(&op_tmp, e, query_uuid);
1161 LDAP_LOG( BACK_META, DETAIL1,
1162 "ENTRY ADDED/MERGED, CACHE =%d entries\n",
1163 cm->cur_entries, 0, 0 );
1164 #else /* !NEW_LOGGING */
1165 Debug( LDAP_DEBUG_ANY,
1166 "ENTRY ADDED/MERGED, CACHE =%d entries\n",
1167 cm->cur_entries, 0, 0 );
1168 #endif /* !NEW_LOGGING */
1170 LDAP_LOG( BACK_META, DETAIL2, "UNLOCKING REMOVE MUTEX\n",
1172 #else /* !NEW_LOGGING */
1173 Debug( LDAP_DEBUG_NONE, "UNLOCKING REMOVE MUTEX\n", 0, 0, 0 );
1174 #endif /* !NEW_LOGGING */
1175 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1177 LDAP_LOG( BACK_META, DETAIL2, "UNLOCKED REMOVE MUTEX\n",
1179 #else /* !NEW_LOGGING */
1180 Debug( LDAP_DEBUG_NONE, "UNLOCKED REMOVE MUTEX\n", 0, 0, 0 );
1181 #endif /* !NEW_LOGGING */
1184 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1187 LDAP_LOG( BACK_META, DETAIL1,
1188 "ENTRY ADDED/MERGED, SIZE=%d, CACHED ENTRIES=%d\n",
1189 return_val, cm->cur_entries, 0 );
1190 #else /* !NEW_LOGGING */
1191 Debug( LDAP_DEBUG_ANY,
1192 "ENTRY ADDED/MERGED, SIZE=%d, CACHED ENTRIES=%d\n",
1193 return_val, cm->cur_entries, 0 );
1194 #endif /* !NEW_LOGGING */
1195 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1197 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1198 cm->num_cached_queries++;
1200 LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1201 cm->num_cached_queries, 0, 0 );
1202 #else /* !NEW_LOGGING */
1203 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1204 cm->num_cached_queries, 0, 0 );
1205 #endif /* !NEW_LOGGING */
1206 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1212 proxy_cache_response(
1216 struct search_info *si = op->o_callback->sc_private;
1217 slap_overinst *on = si->on;
1218 cache_manager *cm = on->on_bi.bi_private;
1219 query_manager* qm = cm->qm;
1222 if ( rs->sr_type == REP_SEARCH ) {
1224 /* If we haven't exceeded the limit for this query,
1225 * build a chain of answers to store. If we hit the
1226 * limit, empty the chain and ignore the rest.
1229 if ( si->count < si->max ) {
1231 e = entry_dup( rs->sr_entry );
1232 if ( !si->head ) si->head = e;
1233 if ( si->tail ) si->tail->e_private = e;
1238 for (;si->head; si->head=e) {
1239 e = si->head->e_private;
1240 si->head->e_private = NULL;
1241 entry_free(si->head);
1246 } else if ( rs->sr_type == REP_RESULT && !si->over ) {
1247 if ( cache_entries( op, rs, &uuid ) == 0)
1248 qm->addfunc(qm, &si->query, si->template_id, &uuid);
1250 return SLAP_CB_CONTINUE;
1256 AttributeName** new_attrs,
1257 AttributeName* attrs,
1258 AttributeName* filter_attrs )
1265 /* duplicate attrs */
1266 if (attrs == NULL) {
1269 for (count=0; attrs[count].an_name.bv_val; count++)
1272 *new_attrs = (AttributeName*)(op->o_tmpalloc((count+1)*
1273 sizeof(AttributeName), op->o_tmpmemctx));
1274 if (attrs == NULL) {
1275 (*new_attrs)[0].an_name.bv_val = "*";
1276 (*new_attrs)[0].an_name.bv_len = 1;
1277 (*new_attrs)[1].an_name.bv_val = NULL;
1278 (*new_attrs)[1].an_name.bv_len = 0;
1282 for (i=0; i<count; i++) {
1283 (*new_attrs)[i].an_name = attrs[i].an_name;
1284 (*new_attrs)[i].an_desc = attrs[i].an_desc;
1286 (*new_attrs)[count].an_name.bv_val = NULL;
1287 (*new_attrs)[count].an_name.bv_len = 0;
1288 alluser = an_find(*new_attrs, &AllUser);
1289 allop = an_find(*new_attrs, &AllOper);
1292 for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1293 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1295 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1300 *new_attrs = (AttributeName*)(op->o_tmprealloc(*new_attrs,
1301 (count+2)*sizeof(AttributeName), op->o_tmpmemctx));
1302 (*new_attrs)[count].an_name = filter_attrs[i].an_name;
1303 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc;
1305 (*new_attrs)[count].an_name.bv_val = NULL;
1306 (*new_attrs)[count].an_name.bv_len = 0;
1315 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1316 cache_manager *cm = on->on_bi.bi_private;
1317 query_manager* qm = cm->qm;
1323 AttributeName *filter_attrs = NULL;
1324 AttributeName *new_attrs = NULL;
1329 int template_id = -1;
1333 int oc_attr_absent = 1;
1335 struct berval tempstr;
1337 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1339 if (filter2template(op->ors_filter, &tempstr, &filter_attrs, &fattr_cnt)) {
1340 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1341 return SLAP_CB_CONTINUE;
1345 LDAP_LOG( BACK_META, DETAIL1, "query template of incoming query = %s\n",
1346 tempstr.bv_val, 0, 0 );
1347 #else /* !NEW_LOGGING */
1348 Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
1349 tempstr.bv_val, 0, 0 );
1350 #endif /* !NEW_LOGGING */
1353 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1355 query.filter = op->ors_filter;
1356 query.attrs = op->ors_attrs;
1357 query.base = op->o_req_ndn;
1358 query.scope = op->ors_scope;
1360 /* check for query containment */
1361 if (attr_set > -1) {
1362 for (i=0; i<cm->numtemplates; i++) {
1363 /* find if template i can potentially answer tempstr */
1364 if (!is_temp_answerable(attr_set, &tempstr, qm, i))
1366 if (attr_set == qm->templates[i].attr_set_index) {
1371 LDAP_LOG( BACK_META, DETAIL2,
1372 "Entering QC, querystr = %s\n",
1373 op->ors_filterstr.bv_val, 0, 0 );
1374 #else /* !NEW_LOGGING */
1375 Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1376 op->ors_filterstr.bv_val, 0, 0 );
1377 #endif /* !NEW_LOGGING */
1378 answerable = (*(qm->qcfunc))(qm, &query, i);
1384 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1389 LDAP_LOG( BACK_META, DETAIL1, "QUERY ANSWERABLE\n", 0, 0, 0 );
1390 #else /* !NEW_LOGGING */
1391 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
1392 #endif /* !NEW_LOGGING */
1394 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
1396 db.bd_info = cm->bi;
1397 db.be_private = cm->be_private;
1400 i = cm->bi->bi_op_search( op, rs );
1406 LDAP_LOG( BACK_META, DETAIL1, "QUERY NOT ANSWERABLE\n",
1408 #else /* !NEW_LOGGING */
1409 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1410 #endif /* !NEW_LOGGING */
1412 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1413 if (cm->num_cached_queries >= cm->max_queries) {
1416 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1420 struct search_info *si;
1422 LDAP_LOG( BACK_META, DETAIL1,
1423 "QUERY CACHEABLE\n", 0, 0, 0 );
1424 #else /* !NEW_LOGGING */
1425 Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
1426 #endif /* !NEW_LOGGING */
1427 query.filter = str2filter(op->ors_filterstr.bv_val);
1428 if (op->ors_attrs) {
1429 for ( count=0; op->ors_attrs[ count ].an_name.bv_val; count++ ) {
1430 if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass )
1433 query.attrs = (AttributeName*)ch_malloc( ( count + 1 + oc_attr_absent )
1434 *sizeof(AttributeName));
1435 for ( count=0; op->ors_attrs[ count ].an_name.bv_val; count++ ) {
1436 ber_dupbv( &query.attrs[count].an_name, &op->ors_attrs[count].an_name );
1437 query.attrs[count].an_desc = op->ors_attrs[count].an_desc;
1439 if ( oc_attr_absent ) {
1440 query.attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
1441 query.attrs[ count ].an_name = query.attrs[count].an_desc->ad_cname;
1444 query.attrs[ count ].an_name.bv_val = NULL;
1445 query.attrs[ count ].an_name.bv_len = 0;
1447 op->o_tmpfree(op->ors_attrs, op->o_tmpmemctx);
1448 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
1449 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1450 cb->sc_response = proxy_cache_response;
1451 cb->sc_private = (cb+1);
1452 si = cb->sc_private;
1454 si->prev = op->o_callback;
1456 si->template_id = template_id;
1457 si->max = cm->num_entries_limit ;
1462 op->o_callback = cb;
1465 LDAP_LOG( BACK_META, DETAIL1,
1466 "QUERY NOT CACHEABLE no\n",
1468 #else /* !NEW_LOGGING */
1469 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE no\n",
1471 #endif /* !NEW_LOGGING */
1476 return SLAP_CB_CONTINUE;
1481 AttributeName* attrs_in,
1482 AttributeName* attrs)
1484 int i, count1, count2;
1485 if ( attrs_in == NULL ) {
1486 return (attrs ? 0 : 1);
1488 if ( attrs == NULL )
1492 attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1496 attrs && attrs[count2].an_name.bv_val != NULL;
1499 if ( count1 != count2 )
1502 for ( i=0; i<count1; i++ ) {
1503 if ( !an_find(attrs, &attrs_in[i].an_name ))
1511 AttributeName* attrs,
1516 for (i=0; i<num; i++) {
1517 if (attrscmp(attrs, qm->attr_sets[i].attrs))
1524 consistency_check(void* operation)
1526 Operation op = *(Operation*)operation;
1527 BackendDB be = *op.o_bd;
1529 SlapReply rs = {REP_RESULT};
1531 slap_overinst *on = (slap_overinst *)op.o_bd->bd_info;
1532 cache_manager* cm = on->on_bi.bi_private;
1533 query_manager* qm = cm->qm;
1534 CachedQuery* query, *query_prev;
1538 QueryTemplate* templ;
1541 be.bd_info = cm->bi;
1542 be.be_private = cm->be_private;
1545 ldap_pvt_thread_sleep(cm->cc_period);
1546 for (i=0; qm->templates[i].querystr.bv_val; i++) {
1547 templ = qm->templates + i;
1548 query = templ->query_last;
1549 curr_time = slap_get_time();
1550 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1551 while (query && (query->expiry_time < curr_time)) {
1552 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1553 remove_query(qm, query);
1554 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1556 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n",
1558 #else /* !NEW_LOGGING */
1559 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1561 #endif /* !NEW_LOGGING */
1562 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1563 remove_from_template(query, templ);
1565 LDAP_LOG( BACK_META, DETAIL1,
1566 "TEMPLATE %d QUERIES-- %d\n",
1567 i, templ->no_of_queries, 0 );
1568 #else /* !NEW_LOGGING */
1569 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1570 i, templ->no_of_queries, 0 );
1571 #endif /* !NEW_LOGGING */
1573 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n",
1575 #else /* !NEW_LOGGING */
1576 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1578 #endif /* !NEW_LOGGING */
1579 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1580 uuid = query->q_uuid;
1581 return_val = remove_query_data(&op, &rs, &uuid);
1583 LDAP_LOG( BACK_META, DETAIL1,
1584 "STALE QUERY REMOVED, SIZE=%d\n",
1586 #else /* !NEW_LOGGING */
1587 Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1589 #endif /* !NEW_LOGGING */
1590 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1591 cm->cur_entries -= return_val;
1592 cm->num_cached_queries--;
1594 LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1595 cm->num_cached_queries, 0, 0 );
1596 #else /* !NEW_LOGGING */
1597 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1598 cm->num_cached_queries, 0, 0 );
1599 #endif /* !NEW_LOGGING */
1600 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1602 LDAP_LOG( BACK_META, DETAIL1,
1603 "STALE QUERY REMOVED, CACHE ="
1605 cm->cur_entries, 0, 0 );
1606 #else /* !NEW_LOGGING */
1607 Debug( LDAP_DEBUG_ANY,
1608 "STALE QUERY REMOVED, CACHE ="
1610 cm->cur_entries, 0, 0 );
1611 #endif /* !NEW_LOGGING */
1613 query = query->prev;
1614 free_query(query_prev);
1616 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1622 #define MAX_ATTR_SETS 500
1623 static void find_supersets( struct attr_set* attr_sets, int numsets );
1624 static int compare_sets( struct attr_set* setA, int, int );
1635 slap_overinst *on = (slap_overinst *)be->bd_info;
1636 cache_manager* cm = on->on_bi.bi_private;
1637 query_manager* qm = cm->qm;
1638 QueryTemplate* temp;
1639 AttributeName* attr_name;
1640 AttributeName* attrarray;
1641 const char* text=NULL;
1642 void *private = be->be_private;
1647 if ( strcasecmp( argv[0], "proxycache" ) == 0 ) {
1649 fprintf( stderr, "%s: line %d: missing arguments in \"proxycache"
1650 " <backend> <max_entries> <numattrsets> <entry limit> "
1651 "<cycle_time>\"\n", fname, lineno );
1655 cm->bi = backend_info( argv[1] );
1657 fprintf( stderr, "%s: line %d: backend %s unknown\n",
1658 fname, lineno, argv[1] );
1661 be->be_private = NULL;
1662 i = cm->bi->bi_db_init( be );
1663 cm->be_private = be->be_private;
1664 be->be_private = private;
1665 if ( i ) return( 1 );
1667 cm->max_entries = atoi( argv[2] );
1669 cm->numattrsets = atoi( argv[3] );
1670 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1671 fprintf( stderr, "%s: line %d: numattrsets must be <= %d\n",
1672 fname, lineno, MAX_ATTR_SETS );
1676 cm->num_entries_limit = atoi( argv[4] );
1677 cm->cc_period = atoi( argv[5] );
1679 LDAP_LOG( BACK_META, DETAIL1,
1680 "Total # of attribute sets to be cached = %d\n",
1681 cm->numattrsets, 0, 0 );
1683 Debug( LDAP_DEBUG_ANY,
1684 "Total # of attribute sets to be cached = %d\n",
1685 cm->numattrsets, 0, 0 );
1687 qm->attr_sets = ( struct attr_set * )ch_malloc( cm->numattrsets *
1688 sizeof( struct attr_set ));
1689 for ( i = 0; i < cm->numattrsets; i++ ) {
1690 qm->attr_sets[i].attrs = NULL;
1693 } else if ( strcasecmp( argv[0], "proxyattrset" ) == 0 ) {
1695 fprintf( stderr, "%s: line %d: missing arguments in \"proxyattrset "
1696 "<index> <attributes>\"\n", fname, lineno );
1700 LDAP_LOG( BACK_META, DETAIL1, "Attribute Set # %d\n",
1701 atoi( argv[1] ), 0, 0 );
1703 Debug( LDAP_DEBUG_ANY, "Attribute Set # %d\n",
1704 atoi( argv[1] ), 0, 0 );
1706 if (atoi(argv[1]) >= cm->numattrsets) {
1707 fprintf( stderr, "%s; line %d index out of bounds \n",
1711 index = atoi( argv[1] );
1712 if ( argv[2] && strcmp( argv[2], "*" ) ) {
1713 qm->attr_sets[index].count = argc - 2;
1714 qm->attr_sets[index].attrs = (AttributeName*)ch_malloc(
1715 (argc-1) * sizeof( AttributeName ));
1716 attr_name = qm->attr_sets[index].attrs;
1717 for ( i = 2; i < argc; i++ ) {
1719 LDAP_LOG( BACK_META, DETAIL1, "\t %s\n",
1722 Debug( LDAP_DEBUG_ANY, "\t %s\n",
1725 ber_str2bv( argv[i], 0, 1,
1726 &attr_name->an_name);
1727 attr_name->an_desc = NULL;
1728 slap_bv2ad( &attr_name->an_name,
1729 &attr_name->an_desc, &text );
1731 attr_name->an_name.bv_val = NULL;
1732 attr_name->an_name.bv_len = 0;
1735 } else if ( strcasecmp( argv[0], "proxytemplate" ) == 0 ) {
1737 fprintf( stderr, "%s: line %d: missing argument(s) in "
1738 "\"proxytemplate <filter> <proj attr set> <TTL>\" line\n",
1742 if (( i = atoi( argv[2] )) >= cm->numattrsets ) {
1744 LDAP_LOG( BACK_META, DETAIL1,
1745 "%s: line %d, template index invalid\n",
1748 Debug( LDAP_DEBUG_ANY,
1749 "%s: line %d, template index invalid\n",
1754 num = cm->numtemplates;
1756 find_supersets( qm->attr_sets, cm->numattrsets );
1757 qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
1758 ( num + 2 ) * sizeof( QueryTemplate ));
1759 temp = qm->templates + num;
1760 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
1761 temp->query = temp->query_last = NULL;
1762 temp->ttl = atoi( argv[3] );
1763 temp->no_of_queries = 0;
1764 if ( argv[1] == NULL ) {
1766 LDAP_LOG( BACK_META, DETAIL1,
1767 "Templates string not specified "
1768 "for template %d\n", num, 0, 0 );
1770 Debug( LDAP_DEBUG_ANY,
1771 "Templates string not specified "
1772 "for template %d\n", num, 0, 0 );
1776 ber_str2bv( argv[1], 0, 1, &temp->querystr );
1778 LDAP_LOG( BACK_META, DETAIL1, "Template:\n", 0, 0, 0 );
1780 Debug( LDAP_DEBUG_ANY, "Template:\n", 0, 0, 0 );
1783 LDAP_LOG( BACK_META, DETAIL1, " query template: %s\n",
1784 temp->querystr.bv_val, 0, 0 );
1786 Debug( LDAP_DEBUG_ANY, " query template: %s\n",
1787 temp->querystr.bv_val, 0, 0 );
1789 temp->attr_set_index = i;
1791 LDAP_LOG( BACK_META, DETAIL1, " attributes: \n", 0, 0, 0 );
1793 Debug( LDAP_DEBUG_ANY, " attributes: \n", 0, 0, 0 );
1795 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1796 for ( i=0; attrarray[i].an_name.bv_val; i++ )
1798 LDAP_LOG( BACK_META, DETAIL1, "\t%s\n",
1799 attrarray[i].an_name.bv_val, 0, 0 );
1801 Debug( LDAP_DEBUG_ANY, "\t%s\n",
1802 attrarray[i].an_name.bv_val, 0, 0 );
1806 temp->querystr.bv_val = NULL;
1811 be->be_private = cm->be_private;
1812 i = cm->bi->bi_db_config( be, fname, lineno, argc, argv );
1813 be->be_private = private;
1824 slap_overinst *on = (slap_overinst *)be->bd_info;
1828 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1829 on->on_bi.bi_private = cm;
1831 qm = (query_manager*)ch_malloc(sizeof(query_manager));
1834 cm->numattrsets = 0;
1835 cm->numtemplates = 0;
1836 cm->num_entries_limit = 5;
1837 cm->num_cached_queries = 0;
1838 cm->max_entries = 0;
1839 cm->cur_entries = 0;
1840 cm->max_queries = 10000;
1841 cm->cc_thread_started = 0;
1842 cm->cc_period = 1000;
1844 qm->attr_sets = NULL;
1845 qm->templates = NULL;
1847 qm->lru_bottom = NULL;
1849 qm->qcfunc = query_containment;
1850 qm->crfunc = cache_replacement;
1851 qm->addfunc = add_query;
1852 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1854 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1855 ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1864 slap_overinst *on = (slap_overinst *)be->bd_info;
1865 cache_manager *cm = on->on_bi.bi_private;
1866 void *private = be->be_private;
1869 if ( cm->be_private && cm->bi->bi_db_open ) {
1870 be->be_private = cm->be_private;
1871 rc = cm->bi->bi_db_open( be );
1872 be->be_private = private;
1882 slap_overinst *on = (slap_overinst *)be->bd_info;
1883 cache_manager *cm = on->on_bi.bi_private;
1884 void *private = be->be_private;
1887 if ( cm->be_private && cm->bi->bi_db_close ) {
1888 be->be_private = cm->be_private;
1889 rc = cm->bi->bi_db_close( be );
1890 be->be_private = private;
1896 proxy_cache_destroy(
1900 slap_overinst *on = (slap_overinst *)be->bd_info;
1901 cache_manager *cm = on->on_bi.bi_private;
1902 void *private = be->be_private;
1905 if ( cm->be_private && cm->bi->bi_db_destroy ) {
1906 be->be_private = cm->be_private;
1907 rc = cm->bi->bi_db_destroy( be );
1908 be->be_private = private;
1915 find_supersets ( struct attr_set* attr_sets, int numsets )
1917 int num[MAX_ATTR_SETS];
1920 for ( i = 0; i < MAX_ATTR_SETS; i++ )
1923 for ( i = 0; i < numsets; i++ ) {
1924 attr_sets[i].ID_array = (int*) ch_malloc( sizeof( int ) );
1925 attr_sets[i].ID_array[0] = -1;
1928 for ( i = 0; i < numsets; i++ ) {
1929 for ( j=i+1; j < numsets; j++ ) {
1930 res = compare_sets( attr_sets, i, j );
1936 id_array = attr_sets[i].ID_array;
1937 attr_sets[i].ID_array = (int *) ch_realloc( id_array,
1938 ( num[i] + 2 ) * sizeof( int ));
1939 attr_sets[i].ID_array[num[i]] = j;
1940 attr_sets[i].ID_array[num[i]+1] = -1;
1945 id_array = attr_sets[j].ID_array;
1946 attr_sets[j].ID_array = (int *) ch_realloc( id_array,
1947 ( num[j] + 2 ) * sizeof( int ));
1948 attr_sets[j].ID_array[num[j]] = i;
1949 attr_sets[j].ID_array[num[j]+1] = -1;
1958 * compares two sets of attributes (indices i and j)
1959 * returns 0: if neither set is contained in the other set
1960 * 1: if set i is contained in set j
1961 * 2: if set j is contained in set i
1962 * 3: the sets are equivalent
1966 compare_sets(struct attr_set* set, int i, int j)
1972 if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
1975 if ( set[i].attrs == NULL )
1978 if ( set[j].attrs == NULL )
1981 numI = set[i].count;
1982 numJ = set[j].count;
1984 for ( l=0; l < numI; l++ ) {
1985 for ( k = 0; k < numJ; k++ ) {
1986 if ( strcmp( set[i].attrs[l].an_name.bv_val,
1987 set[j].attrs[k].an_name.bv_val ) == 0 )
1992 if ( common == numI )
1995 if ( common == numJ )
2001 static slap_overinst proxy_cache;
2005 LDAPAttributeType *at;
2009 at = ldap_str2attributetype( queryid_schema, &code, &err,
2010 LDAP_SCHEMA_ALLOW_ALL );
2012 fprintf( stderr, "AttributeType Load failed %s %s\n",
2013 ldap_scherr2str(code), err );
2016 code = at_add( at, &err );
2018 slap_str2ad( at->at_names[0], &ad_queryid, &err );
2022 fprintf( stderr, "AttributeType Load failed %s %s\n",
2023 scherr2str(code), err );
2027 proxy_cache.on_bi.bi_type = "proxycache";
2028 proxy_cache.on_bi.bi_db_init = proxy_cache_init;
2029 proxy_cache.on_bi.bi_db_config = proxy_cache_config;
2030 proxy_cache.on_bi.bi_db_open = proxy_cache_open;
2031 proxy_cache.on_bi.bi_db_close = proxy_cache_close;
2032 proxy_cache.on_bi.bi_db_destroy = proxy_cache_destroy;
2033 proxy_cache.on_bi.bi_op_search = proxy_cache_search;
2035 return overlay_register( &proxy_cache );
2038 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
2039 int init_module(int argc, char *argv[]) {
2040 return pcache_init();
2044 #endif /* defined(SLAPD_OVER_PROXYCACHE) */