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.
28 #include <ac/string.h>
35 /* query cache structs */
38 typedef struct Query_s {
39 Filter* filter; /* Search Filter */
40 AttributeName* attrs; /* Projected attributes */
41 struct berval base; /* Search Base */
42 int scope; /* Search scope */
45 /* struct representing a cached query */
46 typedef struct cached_query_s {
47 Query query; /* LDAP query */
48 struct berval q_uuid; /* query identifier */
49 int template_id; /* template of the query */
50 time_t expiry_time; /* time till the query is considered valid */
51 struct cached_query_s *next; /* next query in the template */
52 struct cached_query_s *prev; /* previous query in the template */
53 struct cached_query_s *lru_up; /* previous query in the LRU list */
54 struct cached_query_s *lru_down; /* next query in the LRU list */
57 /* struct representing a query template
58 * e.g. template string = &(cn=)(mail=)
60 typedef struct query_template_s {
61 struct berval querystr; /* Filter string corresponding to the QT */
62 int attr_set_index; /* determines the projected attributes */
64 CachedQuery* query; /* most recent query cached for the template */
65 CachedQuery* query_last; /* oldest query cached for the template */
67 int no_of_queries; /* Total number of queries in the template */
68 long ttl; /* TTL for the queries of this template */
69 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
73 * Represents a set of projected attributes and any
74 * supersets among all specified sets of attributes.
78 AttributeName* attrs; /* specifies the set */
79 int count; /* number of attributes */
80 int* ID_array; /* array of indices of supersets of 'attrs' */
83 struct query_manager_s;
85 /* prototypes for functions for 1) query containment
86 * 2) query addition, 3) cache replacement
88 typedef int (QCfunc)(struct query_manager_s*, Query*, int );
89 typedef void (AddQueryfunc)(struct query_manager_s*, Query*, int, struct berval*);
90 typedef void (CRfunc)(struct query_manager_s*, struct berval * );
92 /* LDAP query cache */
93 typedef struct query_manager_s {
94 struct attr_set* attr_sets; /* possible sets of projected attributes */
95 QueryTemplate* templates; /* cacheable templates */
97 CachedQuery* lru_top; /* top and bottom of LRU list */
98 CachedQuery* lru_bottom;
100 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
102 /* Query cache methods */
103 QCfunc *qcfunc; /* Query containment*/
104 CRfunc *crfunc; /* cache replacement */
105 AddQueryfunc *addfunc; /* add query */
108 /* LDAP query cache manager */
109 typedef struct cache_manager_s {
110 BackendInfo *bi; /* underlying database info */
111 void *be_private; /* for the 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) */
121 int cc_thread_started;
122 ldap_pvt_thread_t cc_thread;
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 )";
141 struct berval* query_uuid )
144 Modifications* modlist = NULL;
145 const char* text = NULL;
147 char textbuf[SLAP_TEXT_BUFLEN];
148 size_t textlen = sizeof(textbuf);
150 SlapReply sreply = {REP_RESULT};
152 slap_callback cb = { slap_null_cb, NULL };
157 /* add queryid attribute */
158 attr_merge_one( e, ad_queryid, query_uuid, NULL );
160 /* append the attribute list from the fetched entry */
161 e->e_attrs->a_next = attr;
163 op->o_tag = LDAP_REQ_ADD;
164 op->o_protocol = LDAP_VERSION3;
165 op->o_callback = &cb;
166 op->o_time = slap_get_time();
167 op->o_do_not_cache = 1;
170 rc = op->o_bd->be_add( op, &sreply );
172 if ( rc != LDAP_SUCCESS ) {
173 if ( rc == LDAP_ALREADY_EXISTS ) {
174 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
175 modlist->sml_op = LDAP_MOD_ADD;
176 op->o_tag = LDAP_REQ_MODIFY;
177 op->orm_modlist = modlist;
178 op->o_req_dn = e->e_name;
179 op->o_req_ndn = e->e_nname;
180 rc = op->o_bd->be_modify( op, &sreply );
181 slap_mods_free( modlist );
182 } else if ( rc == LDAP_REFERRAL ||
183 rc == LDAP_NO_SUCH_OBJECT ) {
184 syncrepl_add_glue( op, e );
193 /* compare base and scope of incoming and cached queries */
194 static int base_scope_compare(
195 struct berval* ndn_stored,
196 struct berval* ndn_incoming,
200 struct berval pdn_incoming = { 0, NULL };
202 if (scope_stored < scope_incoming)
205 if ( !dnIsSuffix(ndn_incoming, ndn_stored))
208 switch(scope_stored) {
209 case LDAP_SCOPE_BASE:
210 return (ndn_incoming->bv_len == ndn_stored->bv_len);
212 case LDAP_SCOPE_ONELEVEL:
213 switch(scope_incoming){
214 case LDAP_SCOPE_BASE:
215 dnParent(ndn_incoming, &pdn_incoming);
216 return (pdn_incoming.bv_len == ndn_stored->bv_len);
218 case LDAP_SCOPE_ONELEVEL:
219 return (ndn_incoming->bv_len == ndn_stored->bv_len);
224 case LDAP_SCOPE_SUBTREE:
233 /* add query on top of LRU list */
235 add_query_on_top (query_manager* qm, CachedQuery* qc)
237 CachedQuery* top = qm->lru_top;
238 Query* q = (Query*)qc;
250 LDAP_LOG( BACK_META, DETAIL1, "Base of added query = %s\n",
251 q->base.bv_val, 0, 0 );
253 Debug( LDAP_DEBUG_ANY, "Base of added query = %s\n",
254 q->base.bv_val, 0, 0 );
258 /* remove_query from LRU list */
261 remove_query (query_manager* qm, CachedQuery* qc)
284 qc->lru_up = qc->lru_down = NULL;
288 invert_string( struct berval *bv )
293 for (i=0; i<bv->bv_len/2; i++) {
295 bv->bv_val[i] = bv->bv_val[bv->bv_len-i-1];
296 bv->bv_val[bv->bv_len-i-1] = c;
300 /* find and remove string2 from string1
301 * from start if position = 1,
302 * from end if position = 3,
303 * from anywhere if position = 2
307 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
314 char* string1=ber1->bv_val;
315 char* string2=ber2->bv_val;
330 temp = strstr(arg1, arg2);
334 if ( temp && (position == 2 || temp == arg1) ) {
336 strcpy( arg1, string1 );
340 if ( position == 3 ) {
348 static struct berval*
349 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
351 struct berval* merged, *temp;
352 int i, any_count, count;
354 for (any_count=0; any && any[any_count].bv_val; any_count++)
364 merged = (struct berval*)(ch_malloc((count+1)*sizeof(struct berval)));
371 for (i=0; i<any_count; i++) {
384 strings_containment(struct berval* stored, struct berval* incoming)
386 struct berval* element;
390 for ( element=stored; element->bv_val != NULL; element++ ) {
391 for (j = k; incoming[j].bv_val != NULL; j++) {
392 if (find_and_remove(&(incoming[j]), element, 2)) {
409 substr_containment_substr(Filter* stored, Filter* incoming)
415 struct berval init_incoming;
416 struct berval final_incoming;
417 struct berval *any_incoming = NULL;
418 struct berval *remaining_incoming = NULL;
420 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
421 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
425 ber_dupbv(&init_incoming, &(incoming->f_sub_initial));
426 ber_dupbv(&final_incoming, &(incoming->f_sub_final));
428 if (incoming->f_sub_any) {
429 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
433 any_incoming = (struct berval*)ch_malloc((any_count+1) *
434 sizeof(struct berval));
436 for (i=0; i<any_count; i++) {
437 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
439 any_incoming[any_count].bv_val = NULL;
440 any_incoming[any_count].bv_len = 0;
443 if (find_and_remove(&init_incoming,
444 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
445 &(stored->f_sub_final), 3))
447 if (stored->f_sub_any == NULL) {
451 remaining_incoming = merge_init_final(&init_incoming,
452 any_incoming, &final_incoming);
453 rc = strings_containment(stored->f_sub_any, remaining_incoming);
456 free(init_incoming.bv_val);
457 free(final_incoming.bv_val);
458 if (any_incoming) ber_bvarray_free( any_incoming );
459 free(remaining_incoming);
465 substr_containment_equality(Filter* stored, Filter* incoming)
467 struct berval incoming_val[2];
470 ber_dupbv(incoming_val, &(incoming->f_av_value));
471 incoming_val[1].bv_val = NULL;
472 incoming_val[1].bv_len = 0;
474 if (find_and_remove(incoming_val,
475 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
476 &(stored->f_sub_final), 3)) {
477 if (stored->f_sub_any == NULL){
481 rc = strings_containment(stored->f_sub_any, incoming_val);
484 free(incoming_val[0].bv_val);
488 /* check whether query is contained in any of
489 * the cached queries in template template_index
492 query_containment(query_manager *qm,
496 QueryTemplate* templa= qm->templates;
499 Filter* inputf = query->filter;
500 struct berval* base = &(query->base);
501 int scope = query->scope;
508 MatchingRule* mrule = NULL;
509 if (inputf != NULL) {
511 LDAP_LOG( BACK_META, DETAIL1, "Lock QC index = %d\n",
512 template_index, 0, 0 );
514 Debug( LDAP_DEBUG_ANY, "Lock QC index = %d\n",
515 template_index, 0, 0 );
517 ldap_pvt_thread_rdwr_rlock(&(templa[template_index].t_rwlock));
518 for(qc=templa[template_index].query; qc != NULL; qc= qc->next) {
520 if(base_scope_compare(&(q->base), base, q->scope, scope)) {
525 switch (fs->f_choice) {
526 case LDAP_FILTER_EQUALITY:
527 if (fi->f_choice == LDAP_FILTER_EQUALITY)
528 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
534 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
540 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
541 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
542 &(fi->f_ava->aa_value),
543 &(fs->f_ava->aa_value), &text);
544 if (rc != LDAP_SUCCESS) {
545 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
547 LDAP_LOG( BACK_META, DETAIL1,
548 "Unlock: Exiting QC index=%d\n",
549 template_index, 0, 0 );
551 Debug( LDAP_DEBUG_ANY,
552 "Unlock: Exiting QC index=%d\n",
553 template_index, 0, 0 );
558 switch (fs->f_choice) {
560 case LDAP_FILTER_AND:
565 case LDAP_FILTER_SUBSTRINGS:
566 /* check if the equality query can be
567 * answered with cached substring query */
568 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
569 && substr_containment_equality(
572 /* check if the substring query can be
573 * answered with cached substring query */
574 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
575 ) && substr_containment_substr(
581 case LDAP_FILTER_PRESENT:
586 case LDAP_FILTER_EQUALITY:
604 case LDAP_FILTER_NOT:
610 } while((res) && (fi != NULL) && (fs != NULL));
613 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
614 if (qm->lru_top != qc) {
615 remove_query(qm, qc);
616 add_query_on_top(qm, qc);
618 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
624 LDAP_LOG( BACK_META, DETAIL1,
625 "Not answerable: Unlock QC index=%d\n",
626 template_index, 0, 0 );
628 Debug( LDAP_DEBUG_ANY,
629 "Not answerable: Unlock QC index=%d\n",
630 template_index, 0, 0 );
632 ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
638 free_query (CachedQuery* qc)
640 Query* q = (Query*)qc;
641 free(qc->q_uuid.bv_val);
642 filter_free(q->filter);
643 free (q->base.bv_val);
650 /* Add query to query cache */
651 static void add_query(
657 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
658 QueryTemplate* templ = (qm->templates)+template_index;
660 new_cached_query->template_id = template_index;
661 new_cached_query->q_uuid = *uuid;
662 new_cached_query->lru_up = NULL;
663 new_cached_query->lru_down = NULL;
664 new_cached_query->expiry_time = slap_get_time() + templ->ttl;
666 LDAP_LOG( BACK_META, DETAIL1, "Added query expires at %ld\n",
667 (long) new_cached_query->expiry_time, 0, 0 );
669 Debug( LDAP_DEBUG_ANY, "Added query expires at %ld\n",
670 (long) new_cached_query->expiry_time, 0, 0 );
672 new_query = (Query*)new_cached_query;
674 ber_dupbv(&new_query->base, &query->base);
675 new_query->scope = query->scope;
676 new_query->filter = query->filter;
677 new_query->attrs = query->attrs;
681 LDAP_LOG( BACK_META, DETAIL1, "Lock AQ index = %d\n",
682 template_index, 0, 0 );
684 Debug( LDAP_DEBUG_ANY, "Lock AQ index = %d\n",
685 template_index, 0, 0 );
687 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
688 if (templ->query == NULL)
689 templ->query_last = new_cached_query;
691 templ->query->prev = new_cached_query;
692 new_cached_query->next = templ->query;
693 new_cached_query->prev = NULL;
694 templ->query = new_cached_query;
695 templ->no_of_queries++;
697 LDAP_LOG( BACK_META, DETAIL1, "TEMPLATE %d QUERIES++ %d\n",
698 template_index, templ->no_of_queries, 0 );
700 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES++ %d\n",
701 template_index, templ->no_of_queries, 0 );
705 LDAP_LOG( BACK_META, DETAIL1, "Unlock AQ index = %d \n",
706 template_index, 0, 0 );
708 Debug( LDAP_DEBUG_ANY, "Unlock AQ index = %d \n",
709 template_index, 0, 0 );
711 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
713 /* Adding on top of LRU list */
714 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
715 add_query_on_top(qm, new_cached_query);
716 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
720 remove_from_template (CachedQuery* qc, QueryTemplate* template)
722 if (!qc->prev && !qc->next) {
723 template->query_last = template->query = NULL;
724 } else if (qc->prev == NULL) {
725 qc->next->prev = NULL;
726 template->query = qc->next;
727 } else if (qc->next == NULL) {
728 qc->prev->next = NULL;
729 template->query_last = qc->prev;
731 qc->next->prev = qc->prev;
732 qc->prev->next = qc->next;
735 template->no_of_queries--;
738 /* remove bottom query of LRU list from the query cache */
739 static void cache_replacement(query_manager* qm, struct berval *result)
744 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
745 bottom = qm->lru_bottom;
747 result->bv_val = NULL;
752 LDAP_LOG ( BACK_META, DETAIL1,
753 "Cache replacement invoked without "
754 "any query in LRU list\n", 0, 0, 0 );
756 Debug ( LDAP_DEBUG_ANY,
757 "Cache replacement invoked without "
758 "any query in LRU list\n", 0, 0, 0 );
762 temp_id = bottom->template_id;
763 remove_query(qm, bottom);
764 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
766 *result = bottom->q_uuid;
767 bottom->q_uuid.bv_val = NULL;
770 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n", temp_id, 0, 0 );
772 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n", temp_id, 0, 0 );
774 ldap_pvt_thread_rdwr_wlock(&(qm->templates[temp_id].t_rwlock));
775 remove_from_template(bottom, (qm->templates+temp_id));
777 LDAP_LOG( BACK_META, DETAIL1, "TEMPLATE %d QUERIES-- %d\n",
778 temp_id, qm->templates[temp_id].no_of_queries, 0 );
780 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
781 temp_id, qm->templates[temp_id].no_of_queries, 0 );
784 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n", temp_id, 0, 0 );
786 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n", temp_id, 0, 0 );
788 ldap_pvt_thread_rdwr_wunlock(&(qm->templates[temp_id].t_rwlock));
803 struct query_info *info = op->o_callback->sc_private;
806 struct berval vals[2];
809 Operation op_tmp = *op;
811 SlapReply sreply = {REP_RESULT};
813 if (rs->sr_type == REP_RESULT)
816 for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
817 if (attr->a_desc == ad_queryid) {
818 for (count=0; attr->a_vals[count].bv_val; count++)
829 LDAP_LOG( BACK_META, DETAIL1,
830 "DELETING ENTRY TEMPLATE=%s\n",
831 attr->a_vals[0].bv_val, 0, 0 );
833 Debug( LDAP_DEBUG_ANY, "DELETING ENTRY TEMPLATE=%s\n",
834 attr->a_vals[0].bv_val, 0, 0 );
837 op_tmp.o_req_dn = rs->sr_entry->e_name;
838 op_tmp.o_req_ndn = rs->sr_entry->e_nname;
840 if (op->o_bd->be_delete(&op_tmp, rs) == LDAP_SUCCESS) {
846 vals[0] = *info->uuid;
847 vals[1].bv_val = NULL;
849 mod.sml_op = LDAP_MOD_DELETE;
850 mod.sml_desc = ad_queryid;
851 mod.sml_type = ad_queryid->ad_cname;
852 mod.sml_bvalues = vals;
855 LDAP_LOG( BACK_META, DETAIL1,
856 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
857 attr->a_vals[0].bv_val, 0, 0 );
859 Debug( LDAP_DEBUG_ANY, "REMOVING TEMP ATTR : TEMPLATE=%s\n",
860 attr->a_vals[0].bv_val, 0, 0 );
863 op_tmp.o_req_dn = rs->sr_entry->e_name;
864 op_tmp.o_req_ndn = rs->sr_entry->e_nname;
865 op_tmp.orm_modlist = &mod;
867 op->o_bd->be_modify( &op_tmp, &sreply );
876 struct berval* query_uuid)
878 struct query_info info;
880 Filter filter = {LDAP_FILTER_EQUALITY};
881 SlapReply sreply = {REP_RESULT};
882 slap_callback cb = { remove_func, NULL };
884 sreply.sr_entry = NULL;
885 sreply.sr_nentries = 0;
886 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
887 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
888 filter.f_av_desc = ad_queryid;
889 filter.f_av_value = *query_uuid;
890 info.uuid = query_uuid;
892 cb.sc_private = &info;
894 op->o_tag = LDAP_REQ_SEARCH;
895 op->o_protocol = LDAP_VERSION3;
896 op->o_callback = &cb;
897 op->o_time = slap_get_time();
898 op->o_do_not_cache = 1;
900 op->o_req_dn = op->o_bd->be_suffix[0];
901 op->o_req_ndn = op->o_bd->be_nsuffix[0];
902 op->ors_scope = LDAP_SCOPE_SUBTREE;
903 op->ors_deref = LDAP_DEREF_NEVER;
906 op->ors_filter = &filter;
907 op->ors_filterstr.bv_val = filter_str;
908 op->ors_filterstr.bv_len = strlen(filter_str);
909 op->ors_attrs = NULL;
910 op->ors_attrsonly = 0;
912 op->o_bd->be_search( op, &sreply );
919 AttributeName* attrs,
926 AttributeName* attrs_in,
933 struct berval* tempstr,
937 QueryTemplate *qt = qm->templates + template_id;
939 if (attr_set != qt->attr_set_index) {
940 int* id_array = qm->attr_sets[attr_set].ID_array;
942 while (*id_array != -1) {
943 if (*id_array == qt->attr_set_index)
950 return (qt->querystr.bv_len == tempstr->bv_len &&
951 strcasecmp(qt->querystr.bv_val, tempstr->bv_val) == 0);
963 AttributeName** filter_attrs,
966 AttributeDescription *ad;
968 switch ( f->f_choice ) {
969 case LDAP_FILTER_EQUALITY:
971 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
972 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
977 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
978 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
983 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
984 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
987 case LDAP_FILTER_APPROX:
989 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
990 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
993 case LDAP_FILTER_SUBSTRINGS:
995 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
996 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
999 case LDAP_FILTER_PRESENT:
1001 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
1002 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
1005 case LDAP_FILTER_AND:
1006 case LDAP_FILTER_OR:
1007 case LDAP_FILTER_NOT: {
1009 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
1010 f->f_choice == LDAP_FILTER_AND ? '&' :
1011 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
1012 fstr->bv_len += sizeof("(%") - 1;
1014 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1015 rc = filter2template( f, fstr, filter_attrs, filter_cnt );
1018 sprintf( fstr->bv_val+fstr->bv_len, ")" );
1019 fstr->bv_len += sizeof(")") - 1;
1025 strcpy( fstr->bv_val, "(?=?)" );
1026 fstr->bv_len += sizeof("(?=?)")-1;
1030 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
1031 (*filter_cnt + 2)*sizeof(AttributeName));
1033 (*filter_attrs)[*filter_cnt].an_desc = ad;
1034 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
1035 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
1036 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
1041 struct search_info {
1043 slap_callback *prev;
1056 struct berval *query_uuid)
1058 struct search_info *si = op->o_callback->sc_private;
1059 slap_overinst *on = si->on;
1060 cache_manager *cm = on->on_bi.bi_private;
1061 query_manager* qm = cm->qm;
1065 struct berval crp_uuid;
1066 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1067 Operation op_tmp = *op;
1068 BackendDB db = *op->o_bd;
1070 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1071 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1074 db.bd_info = cm->bi;
1075 db.be_private =cm->be_private;
1076 db.be_flags |= SLAP_BFLAG_NO_SCHEMA_CHECK;
1079 LDAP_LOG( BACK_META, DETAIL1, "UUID for query being added = %s\n",
1081 #else /* !NEW_LOGGING */
1082 Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1084 #endif /* !NEW_LOGGING */
1086 for ( e=si->head; e; e=si->head ) {
1087 si->head = e->e_private;
1088 e->e_private = NULL;
1090 LDAP_LOG( BACK_META, DETAIL2, "LOCKING REMOVE MUTEX\n",
1092 #else /* !NEW_LOGGING */
1093 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1094 #endif /* !NEW_LOGGING */
1095 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1097 LDAP_LOG( BACK_META, DETAIL2, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1098 #else /* !NEW_LOGGING */
1099 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1100 #endif /* !NEW_LOGGING */
1101 while ( cm->cur_entries > (cm->max_entries) ) {
1102 qm->crfunc(qm, &crp_uuid);
1103 if (crp_uuid.bv_val) {
1105 LDAP_LOG( BACK_META, DETAIL1,
1106 "Removing query UUID %s\n",
1107 crp_uuid.bv_val, 0, 0 );
1108 #else /* !NEW_LOGGING */
1109 Debug( LDAP_DEBUG_ANY,
1110 "Removing query UUID %s\n",
1111 crp_uuid.bv_val, 0, 0 );
1112 #endif /* !NEW_LOGGING */
1113 return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
1115 LDAP_LOG( BACK_META, DETAIL1,
1116 "QUERY REMOVED, SIZE=%d\n",
1118 #else /* !NEW_LOGGING */
1119 Debug( LDAP_DEBUG_ANY,
1120 "QUERY REMOVED, SIZE=%d\n",
1122 #endif /* !NEW_LOGGING */
1123 ldap_pvt_thread_mutex_lock(
1125 cm->cur_entries -= return_val;
1126 cm->num_cached_queries--;
1128 LDAP_LOG( BACK_META, DETAIL1,
1129 "STORED QUERIES = %lu\n",
1130 cm->num_cached_queries, 0, 0 );
1131 #else /* !NEW_LOGGING */
1132 Debug( LDAP_DEBUG_ANY,
1133 "STORED QUERIES = %lu\n",
1134 cm->num_cached_queries, 0, 0 );
1135 #endif /* !NEW_LOGGING */
1136 ldap_pvt_thread_mutex_unlock(
1139 LDAP_LOG( BACK_META, DETAIL1,
1140 "QUERY REMOVED, CACHE ="
1142 cm->cur_entries, 0, 0 );
1143 #else /* !NEW_LOGGING */
1144 Debug( LDAP_DEBUG_ANY,
1145 "QUERY REMOVED, CACHE ="
1147 cm->cur_entries, 0, 0 );
1148 #endif /* !NEW_LOGGING */
1152 return_val = merge_entry(&op_tmp, e, query_uuid);
1154 LDAP_LOG( BACK_META, DETAIL1,
1155 "ENTRY ADDED/MERGED, CACHE =%d entries\n",
1156 cm->cur_entries, 0, 0 );
1157 #else /* !NEW_LOGGING */
1158 Debug( LDAP_DEBUG_ANY,
1159 "ENTRY ADDED/MERGED, CACHE =%d entries\n",
1160 cm->cur_entries, 0, 0 );
1161 #endif /* !NEW_LOGGING */
1163 LDAP_LOG( BACK_META, DETAIL2, "UNLOCKING REMOVE MUTEX\n",
1165 #else /* !NEW_LOGGING */
1166 Debug( LDAP_DEBUG_NONE, "UNLOCKING REMOVE MUTEX\n", 0, 0, 0 );
1167 #endif /* !NEW_LOGGING */
1168 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
1170 LDAP_LOG( BACK_META, DETAIL2, "UNLOCKED REMOVE MUTEX\n",
1172 #else /* !NEW_LOGGING */
1173 Debug( LDAP_DEBUG_NONE, "UNLOCKED REMOVE MUTEX\n", 0, 0, 0 );
1174 #endif /* !NEW_LOGGING */
1177 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1180 LDAP_LOG( BACK_META, DETAIL1,
1181 "ENTRY ADDED/MERGED, SIZE=%d, CACHED ENTRIES=%d\n",
1182 return_val, cm->cur_entries, 0 );
1183 #else /* !NEW_LOGGING */
1184 Debug( LDAP_DEBUG_ANY,
1185 "ENTRY ADDED/MERGED, SIZE=%d, CACHED ENTRIES=%d\n",
1186 return_val, cm->cur_entries, 0 );
1187 #endif /* !NEW_LOGGING */
1188 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1190 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1191 cm->num_cached_queries++;
1193 LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1194 cm->num_cached_queries, 0, 0 );
1195 #else /* !NEW_LOGGING */
1196 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1197 cm->num_cached_queries, 0, 0 );
1198 #endif /* !NEW_LOGGING */
1199 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1205 proxy_cache_response(
1209 struct search_info *si = op->o_callback->sc_private;
1210 slap_overinst *on = si->on;
1211 cache_manager *cm = on->on_bi.bi_private;
1212 query_manager* qm = cm->qm;
1215 if ( rs->sr_type == REP_SEARCH ) {
1217 /* If we haven't exceeded the limit for this query,
1218 * build a chain of answers to store. If we hit the
1219 * limit, empty the chain and ignore the rest.
1222 if ( si->count < si->max ) {
1224 e = entry_dup( rs->sr_entry );
1225 if ( !si->head ) si->head = e;
1226 if ( si->tail ) si->tail->e_private = e;
1231 for (;si->head; si->head=e) {
1232 e = si->head->e_private;
1233 si->head->e_private = NULL;
1234 entry_free(si->head);
1239 } else if ( rs->sr_type == REP_RESULT && !si->over ) {
1240 if ( cache_entries( op, rs, &uuid ) == 0)
1241 qm->addfunc(qm, &si->query, si->template_id, &uuid);
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 );
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 db.bd_info = cm->bi;
1390 db.be_private = cm->be_private;
1393 i = cm->bi->bi_op_search( op, rs );
1399 LDAP_LOG( BACK_META, DETAIL1, "QUERY NOT ANSWERABLE\n",
1401 #else /* !NEW_LOGGING */
1402 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1403 #endif /* !NEW_LOGGING */
1405 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1406 if (cm->num_cached_queries >= cm->max_queries) {
1409 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1413 struct search_info *si;
1415 LDAP_LOG( BACK_META, DETAIL1,
1416 "QUERY CACHEABLE\n", 0, 0, 0 );
1417 #else /* !NEW_LOGGING */
1418 Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
1419 #endif /* !NEW_LOGGING */
1420 query.filter = str2filter(op->ors_filterstr.bv_val);
1421 if (op->ors_attrs) {
1422 for ( count=0; op->ors_attrs[ count ].an_name.bv_val; count++ ) {
1423 if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass )
1426 query.attrs = (AttributeName*)ch_malloc( ( count + 1 + oc_attr_absent )
1427 *sizeof(AttributeName));
1428 for ( count=0; op->ors_attrs[ count ].an_name.bv_val; count++ ) {
1429 ber_dupbv( &query.attrs[count].an_name, &op->ors_attrs[count].an_name );
1430 query.attrs[count].an_desc = op->ors_attrs[count].an_desc;
1432 if ( oc_attr_absent ) {
1433 query.attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
1434 query.attrs[ count ].an_name = query.attrs[count].an_desc->ad_cname;
1437 query.attrs[ count ].an_name.bv_val = NULL;
1438 query.attrs[ count ].an_name.bv_len = 0;
1440 op->o_tmpfree(op->ors_attrs, op->o_tmpmemctx);
1441 add_filter_attrs(op, &op->ors_attrs, query.attrs, filter_attrs);
1442 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1443 cb->sc_response = proxy_cache_response;
1444 cb->sc_private = (cb+1);
1445 si = cb->sc_private;
1447 si->prev = op->o_callback;
1449 si->template_id = template_id;
1450 si->max = cm->num_entries_limit ;
1455 op->o_callback = cb;
1458 LDAP_LOG( BACK_META, DETAIL1,
1459 "QUERY NOT CACHEABLE no\n",
1461 #else /* !NEW_LOGGING */
1462 Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE no\n",
1464 #endif /* !NEW_LOGGING */
1469 return SLAP_CB_CONTINUE;
1474 AttributeName* attrs_in,
1475 AttributeName* attrs)
1477 int i, count1, count2;
1478 if ( attrs_in == NULL ) {
1479 return (attrs ? 0 : 1);
1481 if ( attrs == NULL )
1485 attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1489 attrs && attrs[count2].an_name.bv_val != NULL;
1492 if ( count1 != count2 )
1495 for ( i=0; i<count1; i++ ) {
1496 if ( !an_find(attrs, &attrs_in[i].an_name ))
1504 AttributeName* attrs,
1509 for (i=0; i<num; i++) {
1510 if (attrscmp(attrs, qm->attr_sets[i].attrs))
1517 consistency_check(void* operation)
1519 Operation op = *(Operation*)operation;
1520 BackendDB be = *op.o_bd;
1522 SlapReply rs = {REP_RESULT};
1524 slap_overinst *on = (slap_overinst *)op.o_bd->bd_info;
1525 cache_manager* cm = on->on_bi.bi_private;
1526 query_manager* qm = cm->qm;
1527 CachedQuery* query, *query_prev;
1531 QueryTemplate* templ;
1534 be.bd_info = cm->bi;
1535 be.be_private = cm->be_private;
1538 ldap_pvt_thread_sleep(cm->cc_period);
1539 for (i=0; qm->templates[i].querystr.bv_val; i++) {
1540 templ = qm->templates + i;
1541 query = templ->query_last;
1542 curr_time = slap_get_time();
1543 ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
1544 while (query && (query->expiry_time < curr_time)) {
1545 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1546 remove_query(qm, query);
1547 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1549 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n",
1551 #else /* !NEW_LOGGING */
1552 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1554 #endif /* !NEW_LOGGING */
1555 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1556 remove_from_template(query, templ);
1558 LDAP_LOG( BACK_META, DETAIL1,
1559 "TEMPLATE %d QUERIES-- %d\n",
1560 i, templ->no_of_queries, 0 );
1561 #else /* !NEW_LOGGING */
1562 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1563 i, templ->no_of_queries, 0 );
1564 #endif /* !NEW_LOGGING */
1566 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n",
1568 #else /* !NEW_LOGGING */
1569 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1571 #endif /* !NEW_LOGGING */
1572 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1573 uuid = query->q_uuid;
1574 return_val = remove_query_data(&op, &rs, &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);
1615 #define MAX_ATTR_SETS 500
1616 static void find_supersets( struct attr_set* attr_sets, int numsets );
1617 static int compare_sets( struct attr_set* setA, int, int );
1628 slap_overinst *on = (slap_overinst *)be->bd_info;
1629 cache_manager* cm = on->on_bi.bi_private;
1630 query_manager* qm = cm->qm;
1631 QueryTemplate* temp;
1632 AttributeName* attr_name;
1633 AttributeName* attrarray;
1634 const char* text=NULL;
1635 void *private = be->be_private;
1640 if ( strcasecmp( argv[0], "proxycache" ) == 0 ) {
1642 fprintf( stderr, "%s: line %d: missing arguments in \"proxycache"
1643 " <backend> <max_entries> <numattrsets> <entry limit> "
1644 "<cycle_time>\"\n", fname, lineno );
1648 cm->bi = backend_info( argv[1] );
1650 fprintf( stderr, "%s: line %d: backend %s unknown\n",
1651 fname, lineno, argv[1] );
1654 be->be_private = NULL;
1655 i = cm->bi->bi_db_init( be );
1656 cm->be_private = be->be_private;
1657 be->be_private = private;
1658 if ( i ) return( 1 );
1660 cm->max_entries = atoi( argv[2] );
1662 cm->numattrsets = atoi( argv[3] );
1663 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1664 fprintf( stderr, "%s: line %d: numattrsets must be <= %d\n",
1665 fname, lineno, MAX_ATTR_SETS );
1669 cm->num_entries_limit = atoi( argv[4] );
1670 cm->cc_period = atoi( argv[5] );
1672 LDAP_LOG( BACK_META, DETAIL1,
1673 "Total # of attribute sets to be cached = %d\n",
1674 cm->numattrsets, 0, 0 );
1676 Debug( LDAP_DEBUG_ANY,
1677 "Total # of attribute sets to be cached = %d\n",
1678 cm->numattrsets, 0, 0 );
1680 qm->attr_sets = ( struct attr_set * )ch_malloc( cm->numattrsets *
1681 sizeof( struct attr_set ));
1682 for ( i = 0; i < cm->numattrsets; i++ ) {
1683 qm->attr_sets[i].attrs = NULL;
1686 } else if ( strcasecmp( argv[0], "proxyattrset" ) == 0 ) {
1688 fprintf( stderr, "%s: line %d: missing arguments in \"proxyattrset "
1689 "<index> <attributes>\"\n", fname, lineno );
1693 LDAP_LOG( BACK_META, DETAIL1, "Attribute Set # %d\n",
1694 atoi( argv[1] ), 0, 0 );
1696 Debug( LDAP_DEBUG_ANY, "Attribute Set # %d\n",
1697 atoi( argv[1] ), 0, 0 );
1699 if (atoi(argv[1]) >= cm->numattrsets) {
1700 fprintf( stderr, "%s; line %d index out of bounds \n",
1704 index = atoi( argv[1] );
1705 if ( argv[2] && strcmp( argv[2], "*" ) ) {
1706 qm->attr_sets[index].count = argc - 2;
1707 qm->attr_sets[index].attrs = (AttributeName*)ch_malloc(
1708 (argc-1) * sizeof( AttributeName ));
1709 attr_name = qm->attr_sets[index].attrs;
1710 for ( i = 2; i < argc; i++ ) {
1712 LDAP_LOG( BACK_META, DETAIL1, "\t %s\n",
1715 Debug( LDAP_DEBUG_ANY, "\t %s\n",
1718 ber_str2bv( argv[i], 0, 1,
1719 &attr_name->an_name);
1720 attr_name->an_desc = NULL;
1721 slap_bv2ad( &attr_name->an_name,
1722 &attr_name->an_desc, &text );
1724 attr_name->an_name.bv_val = NULL;
1725 attr_name->an_name.bv_len = 0;
1728 } else if ( strcasecmp( argv[0], "proxytemplate" ) == 0 ) {
1730 fprintf( stderr, "%s: line %d: missing argument(s) in "
1731 "\"proxytemplate <filter> <proj attr set> <TTL>\" line\n",
1735 if (( i = atoi( argv[2] )) >= cm->numattrsets ) {
1737 LDAP_LOG( BACK_META, DETAIL1,
1738 "%s: line %d, template index invalid\n",
1741 Debug( LDAP_DEBUG_ANY,
1742 "%s: line %d, template index invalid\n",
1747 num = cm->numtemplates;
1749 find_supersets( qm->attr_sets, cm->numattrsets );
1750 qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
1751 ( num + 2 ) * sizeof( QueryTemplate ));
1752 temp = qm->templates + num;
1753 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
1754 temp->query = temp->query_last = NULL;
1755 temp->ttl = atoi( argv[3] );
1756 temp->no_of_queries = 0;
1757 if ( argv[1] == NULL ) {
1759 LDAP_LOG( BACK_META, DETAIL1,
1760 "Templates string not specified "
1761 "for template %d\n", num, 0, 0 );
1763 Debug( LDAP_DEBUG_ANY,
1764 "Templates string not specified "
1765 "for template %d\n", num, 0, 0 );
1769 ber_str2bv( argv[1], 0, 1, &temp->querystr );
1771 LDAP_LOG( BACK_META, DETAIL1, "Template:\n", 0, 0, 0 );
1773 Debug( LDAP_DEBUG_ANY, "Template:\n", 0, 0, 0 );
1776 LDAP_LOG( BACK_META, DETAIL1, " query template: %s\n",
1777 temp->querystr.bv_val, 0, 0 );
1779 Debug( LDAP_DEBUG_ANY, " query template: %s\n",
1780 temp->querystr.bv_val, 0, 0 );
1782 temp->attr_set_index = i;
1784 LDAP_LOG( BACK_META, DETAIL1, " attributes: \n", 0, 0, 0 );
1786 Debug( LDAP_DEBUG_ANY, " attributes: \n", 0, 0, 0 );
1788 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1789 for ( i=0; attrarray[i].an_name.bv_val; i++ )
1791 LDAP_LOG( BACK_META, DETAIL1, "\t%s\n",
1792 attrarray[i].an_name.bv_val, 0, 0 );
1794 Debug( LDAP_DEBUG_ANY, "\t%s\n",
1795 attrarray[i].an_name.bv_val, 0, 0 );
1799 temp->querystr.bv_val = NULL;
1804 be->be_private = cm->be_private;
1805 i = cm->bi->bi_db_config( be, fname, lineno, argc, argv );
1806 be->be_private = private;
1817 slap_overinst *on = (slap_overinst *)be->bd_info;
1821 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1822 on->on_bi.bi_private = cm;
1824 qm = (query_manager*)ch_malloc(sizeof(query_manager));
1827 cm->numattrsets = 0;
1828 cm->numtemplates = 0;
1829 cm->num_entries_limit = 5;
1830 cm->num_cached_queries = 0;
1831 cm->max_entries = 0;
1832 cm->cur_entries = 0;
1833 cm->max_queries = 10000;
1834 cm->cc_thread_started = 0;
1835 cm->cc_period = 1000;
1837 qm->attr_sets = NULL;
1838 qm->templates = NULL;
1840 qm->lru_bottom = NULL;
1842 qm->qcfunc = query_containment;
1843 qm->crfunc = cache_replacement;
1844 qm->addfunc = add_query;
1845 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1847 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1848 ldap_pvt_thread_mutex_init(&cm->remove_mutex);
1857 slap_overinst *on = (slap_overinst *)be->bd_info;
1858 cache_manager *cm = on->on_bi.bi_private;
1859 void *private = be->be_private;
1862 if ( cm->be_private && cm->bi->bi_db_open ) {
1863 be->be_private = cm->be_private;
1864 rc = cm->bi->bi_db_open( be );
1865 be->be_private = private;
1875 slap_overinst *on = (slap_overinst *)be->bd_info;
1876 cache_manager *cm = on->on_bi.bi_private;
1877 void *private = be->be_private;
1880 if ( cm->be_private && cm->bi->bi_db_close ) {
1881 be->be_private = cm->be_private;
1882 rc = cm->bi->bi_db_close( be );
1883 be->be_private = private;
1889 proxy_cache_destroy(
1893 slap_overinst *on = (slap_overinst *)be->bd_info;
1894 cache_manager *cm = on->on_bi.bi_private;
1895 void *private = be->be_private;
1898 if ( cm->be_private && cm->bi->bi_db_destroy ) {
1899 be->be_private = cm->be_private;
1900 rc = cm->bi->bi_db_destroy( be );
1901 be->be_private = private;
1908 find_supersets ( struct attr_set* attr_sets, int numsets )
1910 int num[MAX_ATTR_SETS];
1913 for ( i = 0; i < MAX_ATTR_SETS; i++ )
1916 for ( i = 0; i < numsets; i++ ) {
1917 attr_sets[i].ID_array = (int*) ch_malloc( sizeof( int ) );
1918 attr_sets[i].ID_array[0] = -1;
1921 for ( i = 0; i < numsets; i++ ) {
1922 for ( j=i+1; j < numsets; j++ ) {
1923 res = compare_sets( attr_sets, i, j );
1929 id_array = attr_sets[i].ID_array;
1930 attr_sets[i].ID_array = (int *) ch_realloc( id_array,
1931 ( num[i] + 2 ) * sizeof( int ));
1932 attr_sets[i].ID_array[num[i]] = j;
1933 attr_sets[i].ID_array[num[i]+1] = -1;
1938 id_array = attr_sets[j].ID_array;
1939 attr_sets[j].ID_array = (int *) ch_realloc( id_array,
1940 ( num[j] + 2 ) * sizeof( int ));
1941 attr_sets[j].ID_array[num[j]] = i;
1942 attr_sets[j].ID_array[num[j]+1] = -1;
1951 * compares two sets of attributes (indices i and j)
1952 * returns 0: if neither set is contained in the other set
1953 * 1: if set i is contained in set j
1954 * 2: if set j is contained in set i
1955 * 3: the sets are equivalent
1959 compare_sets(struct attr_set* set, int i, int j)
1965 if (( set[i].attrs == NULL ) && ( set[j].attrs == NULL ))
1968 if ( set[i].attrs == NULL )
1971 if ( set[j].attrs == NULL )
1974 numI = set[i].count;
1975 numJ = set[j].count;
1977 for ( l=0; l < numI; l++ ) {
1978 for ( k = 0; k < numJ; k++ ) {
1979 if ( strcmp( set[i].attrs[l].an_name.bv_val,
1980 set[j].attrs[k].an_name.bv_val ) == 0 )
1985 if ( common == numI )
1988 if ( common == numJ )
1994 static slap_overinst proxy_cache;
1998 LDAPAttributeType *at;
2002 at = ldap_str2attributetype( queryid_schema, &code, &err,
2003 LDAP_SCHEMA_ALLOW_ALL );
2005 fprintf( stderr, "AttributeType Load failed %s %s\n",
2006 ldap_scherr2str(code), err );
2009 code = at_add( at, &err );
2011 slap_str2ad( at->at_names[0], &ad_queryid, &err );
2015 fprintf( stderr, "AttributeType Load failed %s %s\n",
2016 scherr2str(code), err );
2020 proxy_cache.on_bi.bi_type = "proxycache";
2021 proxy_cache.on_bi.bi_db_init = proxy_cache_init;
2022 proxy_cache.on_bi.bi_db_config = proxy_cache_config;
2023 proxy_cache.on_bi.bi_db_open = proxy_cache_open;
2024 proxy_cache.on_bi.bi_db_close = proxy_cache_close;
2025 proxy_cache.on_bi.bi_db_destroy = proxy_cache_destroy;
2026 proxy_cache.on_bi.bi_op_search = proxy_cache_search;
2028 return overlay_register( &proxy_cache );