]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache.h
silence warning
[openldap] / servers / slapd / back-meta / cache.h
1 /* Copyright (c) 2003 by International Business Machines, Inc.
2  *
3  * International Business Machines, Inc. (hereinafter called IBM) grants
4  * permission under its copyrights to use, copy, modify, and distribute 
5 this
6  * Software with or without fee, provided that the above copyright notice 
7 and
8  * all paragraphs of this notice appear in all copies, and that the name 
9 of IBM
10  * not be used in connection with the marketing of any product 
11 incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER 
19 ARISING
20  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, 
21 EVEN
22  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
23  */
24
25
26 #ifndef META_CACHE_H
27 #define META_CACHE_H
28 #include "slap.h"
29
30 #ifdef LDAP_CACHING
31 /* cache specific errors */
32 enum type_of_result 
33
34     SUCCESS, 
35     CONN_ERR, 
36     RESULT_ERR, 
37     FILTER_ERR, 
38     REWRITING_ERR,
39     MERGE_ERR, 
40     REMOVE_ERR, 
41     SLIMIT_ERR, 
42     ABANDON_ERR, 
43     CREATE_ENTRY_ERR, 
44     TIMEOUT_ERR, 
45     SIZE_ERR, 
46     GET_SIZE_ERR
47 };   
48
49
50 struct exception {
51     enum type_of_result type; 
52     int  rc; 
53 };
54
55 /* query cache structs */
56 /* query */
57
58 typedef struct Query_s {
59         Filter*         filter;         /* Search Filter */
60         AttributeName*  attrs;          /* Projected attributes */
61         struct berval   base;           /* Search Base */
62         int             scope;          /* Search scope */
63 } Query;
64
65 /* struct representing a cached query */
66 typedef struct cached_query_s {
67         Query                           query;          /* LDAP query */ 
68         char*                           q_uuid;         /* query identifier */ 
69         int                             template_id;    /* template of the query */ 
70         time_t                          expiry_time;    /* time till the query is considered valid */ 
71         struct cached_query_s           *next;          /* next query in the template */
72         struct cached_query_s           *prev;          /* previous query in the template */
73         struct cached_query_s           *lru_up;        /* previous query in the LRU list */ 
74         struct cached_query_s           *lru_down;      /* next query in the LRU list */ 
75 } CachedQuery; 
76
77 /* struct representing a query template
78  * e.g. template string = &(cn=)(mail=) 
79  */
80 typedef struct query_template_s {
81         char*           querystr;       /* Filter string corresponding to the QT */
82         char*           base;           /* Search base */ 
83         int             attr_set_index; /* determines the projected attributes */ 
84
85         CachedQuery*    query;          /* most recent query cached for the template */ 
86         CachedQuery*    query_last;     /* oldest query cached for the template */              
87
88         int             no_of_queries;  /* Total number of queries in the template */
89         long            ttl;            /* TTL for the queries of this template */ 
90         ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */ 
91 } QueryTemplate;
92
93 /* 
94  * Represents a set of projected attributes and any
95  * supersets among all specified sets of attributes. 
96  */
97
98 struct attr_set {
99         AttributeName*  attrs;          /* specifies the set */
100         int             count;          /* number of attributes */ 
101         int*            ID_array;       /* array of indices of supersets of 'attrs' */ 
102 };
103
104 struct query_manager_s; 
105
106 /* prototypes for functions for 1) query containment 
107  * 2) query addition, 3) cache replacement 
108  */
109 typedef int     (*QCfunc)(struct query_manager_s*, Query*, int );
110 typedef void    (*AddQueryfunc)(struct query_manager_s*, Query*, int, char*, struct exception* );
111 typedef char*   (*CRfunc)(struct query_manager_s* );
112
113 /* LDAP query cache */ 
114 typedef struct query_manager_s {
115         struct attr_set*        attr_sets;              /* possible sets of projected attributes */
116         QueryTemplate*          templates;              /* cacheable templates */
117
118         CachedQuery*            lru_top;                /* top and bottom of LRU list */
119         CachedQuery*            lru_bottom;
120
121         ldap_pvt_thread_mutex_t         lru_mutex;      /* mutex for accessing LRU list */
122
123         /* Query cache methods */
124         QCfunc                  qcfunc;                 /* Query containment*/  
125         CRfunc                  crfunc;                 /* cache replacement */
126         AddQueryfunc            addfunc;                /* add query */ 
127 } query_manager; 
128
129 /* LDAP query cache manager */ 
130 typedef struct cache_manager_s {
131         unsigned long   cache_size;                     /* current cache size (bytes) */ 
132         unsigned long   thresh_hi;                      /* cache capacity (bytes) */
133         unsigned long   thresh_lo;                      /* lower threshold for cache replacement */
134         unsigned long   num_cached_queries;             /* total number of cached queries */
135         unsigned long   max_queries;                    /* upper bound on # of cached queries */ 
136         int     caching; 
137
138         int     numattrsets;                    /* number of attribute sets */
139         int     numtemplates;                   /* number of cacheable templates */
140         int     total_entries;                  /* total number of entries cached */ 
141         int     num_entries_limit;              /* max # of entries in a cacheable query */ 
142         int     threads;                        /* number of threads currently in meta_back_search */ 
143
144         int     consistency_cycle_time;         /* interval between successive consistency checks (sec) */ 
145         int     consistency_time;               /* time when consistency check was last performed (sec) */ 
146
147         ldap_pvt_thread_mutex_t         cache_mutex;            
148         ldap_pvt_thread_mutex_t         remove_mutex;
149         ldap_pvt_thread_mutex_t         consistency_mutex;      
150
151         query_manager*   qm;    /* query cache managed by the cache manager */ 
152 } cache_manager; 
153
154 /* search-cache.c */
155 int
156 meta_back_cache_search(
157     Backend             *be,
158     Connection          *conn,
159     Operation           *op,
160     struct berval       *base,
161     struct berval       *nbase,
162     int                 scope,
163     int                 deref,
164     int                 slimit,
165     int                 tlimit,
166     Filter              *filt,
167     struct berval       *filterstr,
168     AttributeName       *attributes,
169     int                 attrsonly
170 ); 
171
172 /* config-cache.c */
173 int
174 meta_back_cache_config(
175         BackendDB       *be,
176         const char      *fname,
177         int             lineno,
178         int             argc,
179         char            **argv
180 ); 
181
182 /* query-cache.c */
183 int     query_containment(query_manager*, Query*, int); 
184 void    add_query(query_manager*, Query*, int, char*, struct exception*);
185 char*   cache_replacement(query_manager*);
186 void    remove_from_template (CachedQuery*, QueryTemplate*); 
187 void    remove_query (query_manager*, CachedQuery*);
188 void    free_query (CachedQuery*); 
189
190 /* substring.c */
191 int     substr_containment_substr(Filter*, Filter*); 
192 int     substr_containment_equality(Filter*, Filter*); 
193
194 /* template.c */
195 void 
196 filter2template( Filter *f, 
197                  struct berval *fstr, 
198                  AttributeName** filter_attrs, 
199                  int* filter_cnt, 
200                  struct exception* result
201 );
202
203 /* merge.c */
204
205 int
206 merge_entry (Backend* be,
207             Connection* conn, 
208             Entry* e, 
209             struct berval* query_uuid, 
210             struct exception* result
211 ); 
212
213 int 
214 get_entry_size(Entry* e, 
215                int size_init, 
216                struct exception* result
217 ); 
218
219 void 
220 callback_null_response( Connection *conn, 
221                        Operation *o, 
222                        ber_tag_t tag,
223                        ber_int_t msgid, 
224                        ber_int_t err, 
225                        const char *matched,
226                        const char *text, 
227                        BerVarray ref, 
228                        const char *resoid,
229                        struct berval *resdata, 
230                        struct berval *sasldata, 
231                        LDAPControl **c
232 );
233
234 void callback_null_sresult( Connection *conn, 
235                            Operation *o, 
236                            ber_int_t err,
237                            const char *matched, 
238                            const char *text, 
239                            BerVarray refs, 
240                            LDAPControl **c,
241                            int nentries
242 ); 
243
244 /* remove.c */
245 int 
246 remove_query_data (Backend* be,
247                    Connection* conn, 
248                    struct berval* query_uuid, 
249                    struct exception* result
250 );
251 #endif
252 #endif