2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Apurva Kumar for inclusion
18 * in OpenLDAP Software.
25 /* cache specific errors */
44 enum type_of_result type;
48 /* query cache structs */
51 typedef struct Query_s {
52 Filter* filter; /* Search Filter */
53 AttributeName* attrs; /* Projected attributes */
54 struct berval base; /* Search Base */
55 int scope; /* Search scope */
58 /* struct representing a cached query */
59 typedef struct cached_query_s {
60 Query query; /* LDAP query */
61 char* q_uuid; /* query identifier */
62 int template_id; /* template of the query */
63 time_t expiry_time; /* time till the query is considered valid */
64 struct cached_query_s *next; /* next query in the template */
65 struct cached_query_s *prev; /* previous query in the template */
66 struct cached_query_s *lru_up; /* previous query in the LRU list */
67 struct cached_query_s *lru_down; /* next query in the LRU list */
70 /* struct representing a query template
71 * e.g. template string = &(cn=)(mail=)
73 typedef struct query_template_s {
74 char* querystr; /* Filter string corresponding to the QT */
75 char* base; /* Search base */
76 int attr_set_index; /* determines the projected attributes */
78 CachedQuery* query; /* most recent query cached for the template */
79 CachedQuery* query_last; /* oldest query cached for the template */
81 int no_of_queries; /* Total number of queries in the template */
82 long ttl; /* TTL for the queries of this template */
83 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
87 * Represents a set of projected attributes and any
88 * supersets among all specified sets of attributes.
92 AttributeName* attrs; /* specifies the set */
93 int count; /* number of attributes */
94 int* ID_array; /* array of indices of supersets of 'attrs' */
97 struct query_manager_s;
99 /* prototypes for functions for 1) query containment
100 * 2) query addition, 3) cache replacement
102 typedef int (*QCfunc)(struct query_manager_s*, Query*, int );
103 typedef void (*AddQueryfunc)(struct query_manager_s*, Query*, int, char*, struct exception* );
104 typedef char* (*CRfunc)(struct query_manager_s* );
106 /* LDAP query cache */
107 typedef struct query_manager_s {
108 struct attr_set* attr_sets; /* possible sets of projected attributes */
109 QueryTemplate* templates; /* cacheable templates */
111 CachedQuery* lru_top; /* top and bottom of LRU list */
112 CachedQuery* lru_bottom;
114 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
116 /* Query cache methods */
117 QCfunc qcfunc; /* Query containment*/
118 CRfunc crfunc; /* cache replacement */
119 AddQueryfunc addfunc; /* add query */
122 /* LDAP query cache manager */
123 typedef struct cache_manager_s {
124 unsigned long cache_size; /* current cache size (bytes) */
125 unsigned long thresh_hi; /* cache capacity (bytes) */
126 unsigned long thresh_lo; /* lower threshold for cache replacement */
127 unsigned long num_cached_queries; /* total number of cached queries */
128 unsigned long max_queries; /* upper bound on # of cached queries */
131 int numattrsets; /* number of attribute sets */
132 int numtemplates; /* number of cacheable templates */
133 int total_entries; /* total number of entries cached */
134 int num_entries_limit; /* max # of entries in a cacheable query */
135 int threads; /* number of threads currently in meta_back_search */
137 int cc_period; /* interval between successive consistency checks (sec) */
138 int cc_thread_started;
139 ldap_pvt_thread_t cc_thread;
141 ldap_pvt_thread_mutex_t cache_mutex;
142 ldap_pvt_thread_mutex_t remove_mutex;
143 ldap_pvt_thread_mutex_t cc_mutex;
145 query_manager* qm; /* query cache managed by the cache manager */
150 meta_back_cache_search(
157 meta_back_cache_config(
166 int query_containment(query_manager*, Query*, int);
167 void add_query(query_manager*, Query*, int, char*, struct exception*);
168 char* cache_replacement(query_manager*);
169 void remove_from_template (CachedQuery*, QueryTemplate*);
170 void remove_query (query_manager*, CachedQuery*);
171 void free_query (CachedQuery*);
174 int substr_containment_substr(Filter*, Filter*);
175 int substr_containment_equality(Filter*, Filter*);
179 filter2template( Filter *f,
181 AttributeName** filter_attrs,
183 struct exception* result
192 struct berval *query_uuid,
193 struct exception *result
197 get_entry_size(Entry* e,
199 struct exception* result
207 struct berval* query_uuid,
208 struct exception* result