]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache.h
1f7a50c0834f838fecaa8e0d6e8fc9002492601b
[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 /* cache specific errors */
31 enum type_of_result 
32
33     SUCCESS, 
34     CONN_ERR, 
35     RESULT_ERR, 
36     FILTER_ERR, 
37     REWRITING_ERR,
38     MERGE_ERR, 
39     REMOVE_ERR, 
40     SLIMIT_ERR, 
41     ABANDON_ERR, 
42     CREATE_ENTRY_ERR, 
43     TIMEOUT_ERR, 
44     SIZE_ERR, 
45     GET_SIZE_ERR
46 };   
47
48
49 struct exception {
50     enum type_of_result type; 
51     int  rc; 
52 };
53
54 /* query cache structs */
55 /* query */
56
57 typedef struct Query_s {
58         Filter*         filter;         /* Search Filter */
59         AttributeName*  attrs;          /* Projected attributes */
60         struct berval   base;           /* Search Base */
61         int             scope;          /* Search scope */
62 } Query;
63
64 /* struct representing a cached query */
65 typedef struct cached_query_s {
66         Query                           query;          /* LDAP query */ 
67         char*                           q_uuid;         /* query identifier */ 
68         int                             template_id;    /* template of the query */ 
69         time_t                          expiry_time;    /* time till the query is considered valid */ 
70         struct cached_query_s           *next;          /* next query in the template */
71         struct cached_query_s           *prev;          /* previous query in the template */
72         struct cached_query_s           *lru_up;        /* previous query in the LRU list */ 
73         struct cached_query_s           *lru_down;      /* next query in the LRU list */ 
74 } CachedQuery; 
75
76 /* struct representing a query template
77  * e.g. template string = &(cn=)(mail=) 
78  */
79 typedef struct query_template_s {
80         char*           querystr;       /* Filter string corresponding to the QT */
81         char*           base;           /* Search base */ 
82         int             attr_set_index; /* determines the projected attributes */ 
83
84         CachedQuery*    query;          /* most recent query cached for the template */ 
85         CachedQuery*    query_last;     /* oldest query cached for the template */              
86
87         int             no_of_queries;  /* Total number of queries in the template */
88         long            ttl;            /* TTL for the queries of this template */ 
89         ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */ 
90 } QueryTemplate;
91
92 /* 
93  * Represents a set of projected attributes and any
94  * supersets among all specified sets of attributes. 
95  */
96
97 struct attr_set {
98         AttributeName*  attrs;          /* specifies the set */
99         int             count;          /* number of attributes */ 
100         int*            ID_array;       /* array of indices of supersets of 'attrs' */ 
101 };
102
103 struct query_manager_s; 
104
105 /* prototypes for functions for 1) query containment 
106  * 2) query addition, 3) cache replacement 
107  */
108 typedef int     (*QCfunc)(struct query_manager_s*, Query*, int );
109 typedef void    (*AddQueryfunc)(struct query_manager_s*, Query*, int, char*, struct exception* );
110 typedef char*   (*CRfunc)(struct query_manager_s* );
111
112 /* LDAP query cache */ 
113 typedef struct query_manager_s {
114         struct attr_set*        attr_sets;              /* possible sets of projected attributes */
115         QueryTemplate*          templates;              /* cacheable templates */
116
117         CachedQuery*            lru_top;                /* top and bottom of LRU list */
118         CachedQuery*            lru_bottom;
119
120         ldap_pvt_thread_mutex_t         lru_mutex;      /* mutex for accessing LRU list */
121
122         /* Query cache methods */
123         QCfunc                  qcfunc;                 /* Query containment*/  
124         CRfunc                  crfunc;                 /* cache replacement */
125         AddQueryfunc            addfunc;                /* add query */ 
126 } query_manager; 
127
128 /* LDAP query cache manager */ 
129 typedef struct cache_manager_s {
130         unsigned long   cache_size;                     /* current cache size (bytes) */ 
131         unsigned long   thresh_hi;                      /* cache capacity (bytes) */
132         unsigned long   thresh_lo;                      /* lower threshold for cache replacement */
133         unsigned long   num_cached_queries;             /* total number of cached queries */
134         unsigned long   max_queries;                    /* upper bound on # of cached queries */ 
135         int     caching; 
136
137         int     numattrsets;                    /* number of attribute sets */
138         int     numtemplates;                   /* number of cacheable templates */
139         int     total_entries;                  /* total number of entries cached */ 
140         int     num_entries_limit;              /* max # of entries in a cacheable query */ 
141         int     threads;                        /* number of threads currently in meta_back_search */ 
142
143         int     cc_period;              /* interval between successive consistency checks (sec) */ 
144         int     cc_thread_started; 
145         ldap_pvt_thread_t   cc_thread; 
146
147         ldap_pvt_thread_mutex_t         cache_mutex;            
148         ldap_pvt_thread_mutex_t         remove_mutex;
149         ldap_pvt_thread_mutex_t         cc_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     Operation           *op,
158     SlapReply           *rs
159 ); 
160
161 /* config-cache.c */
162 int
163 meta_back_cache_config(
164         BackendDB       *be,
165         const char      *fname,
166         int             lineno,
167         int             argc,
168         char            **argv
169 ); 
170
171 /* query-cache.c */
172 int     query_containment(query_manager*, Query*, int); 
173 void    add_query(query_manager*, Query*, int, char*, struct exception*);
174 char*   cache_replacement(query_manager*);
175 void    remove_from_template (CachedQuery*, QueryTemplate*); 
176 void    remove_query (query_manager*, CachedQuery*);
177 void    free_query (CachedQuery*); 
178
179 /* substring.c */
180 int     substr_containment_substr(Filter*, Filter*); 
181 int     substr_containment_equality(Filter*, Filter*); 
182
183 /* template.c */
184 void 
185 filter2template( Filter *f, 
186                  struct berval *fstr, 
187                  AttributeName** filter_attrs, 
188                  int* filter_cnt, 
189                  struct exception* result
190 );
191
192 /* merge.c */
193
194 int
195 merge_entry (
196             Operation           *op, 
197             SlapReply           *rs,
198             struct berval       *query_uuid, 
199             struct exception    *result
200 ); 
201
202 int 
203 get_entry_size(Entry* e, 
204                int size_init, 
205                struct exception* result
206 ); 
207
208 /* remove.c */
209 int 
210 remove_query_data (
211                    Operation* conn,
212                    SlapReply *rs,
213                    struct berval* query_uuid, 
214                    struct exception* result
215 );
216 #endif