]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache.h
Formating
[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 this
5  * Software with or without fee, provided that the above copyright notice and
6  * all paragraphs of this notice appear in all copies, and that the name of IBM
7  * not be used in connection with the marketing of any product incorporating
8  * the Software or modifications thereof, without specific, written prior
9  * permission.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
12  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
13  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
14  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
15  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
16  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
17  */
18
19
20 #ifndef META_CACHE_H
21 #define META_CACHE_H
22 #include "slap.h"
23
24 /* cache specific errors */
25 enum type_of_result 
26
27     SUCCESS, 
28     CONN_ERR, 
29     RESULT_ERR, 
30     FILTER_ERR, 
31     REWRITING_ERR,
32     MERGE_ERR, 
33     REMOVE_ERR, 
34     SLIMIT_ERR, 
35     ABANDON_ERR, 
36     CREATE_ENTRY_ERR, 
37     TIMEOUT_ERR, 
38     SIZE_ERR, 
39     GET_SIZE_ERR
40 };   
41
42
43 struct exception {
44     enum type_of_result type; 
45     int  rc; 
46 };
47
48 /* query cache structs */
49 /* query */
50
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 */
56 } Query;
57
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 */ 
68 } CachedQuery; 
69
70 /* struct representing a query template
71  * e.g. template string = &(cn=)(mail=) 
72  */
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 */ 
77
78         CachedQuery*    query;          /* most recent query cached for the template */ 
79         CachedQuery*    query_last;     /* oldest query cached for the template */              
80
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 */ 
84 } QueryTemplate;
85
86 /* 
87  * Represents a set of projected attributes and any
88  * supersets among all specified sets of attributes. 
89  */
90
91 struct attr_set {
92         AttributeName*  attrs;          /* specifies the set */
93         int             count;          /* number of attributes */ 
94         int*            ID_array;       /* array of indices of supersets of 'attrs' */ 
95 };
96
97 struct query_manager_s; 
98
99 /* prototypes for functions for 1) query containment 
100  * 2) query addition, 3) cache replacement 
101  */
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* );
105
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 */
110
111         CachedQuery*            lru_top;                /* top and bottom of LRU list */
112         CachedQuery*            lru_bottom;
113
114         ldap_pvt_thread_mutex_t         lru_mutex;      /* mutex for accessing LRU list */
115
116         /* Query cache methods */
117         QCfunc                  qcfunc;                 /* Query containment*/  
118         CRfunc                  crfunc;                 /* cache replacement */
119         AddQueryfunc            addfunc;                /* add query */ 
120 } query_manager; 
121
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 */ 
129         int     caching; 
130
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 */ 
136
137         int     cc_period;              /* interval between successive consistency checks (sec) */ 
138         int     cc_thread_started; 
139         ldap_pvt_thread_t   cc_thread; 
140
141         ldap_pvt_thread_mutex_t         cache_mutex;            
142         ldap_pvt_thread_mutex_t         remove_mutex;
143         ldap_pvt_thread_mutex_t         cc_mutex;       
144
145         query_manager*   qm;    /* query cache managed by the cache manager */ 
146 } cache_manager; 
147
148 /* search-cache.c */
149 int
150 meta_back_cache_search(
151     Operation           *op,
152     SlapReply           *rs
153 ); 
154
155 /* config-cache.c */
156 int
157 meta_back_cache_config(
158         BackendDB       *be,
159         const char      *fname,
160         int             lineno,
161         int             argc,
162         char            **argv
163 ); 
164
165 /* query-cache.c */
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*); 
172
173 /* substring.c */
174 int     substr_containment_substr(Filter*, Filter*); 
175 int     substr_containment_equality(Filter*, Filter*); 
176
177 /* template.c */
178 void 
179 filter2template( Filter *f, 
180                  struct berval *fstr, 
181                  AttributeName** filter_attrs, 
182                  int* filter_cnt, 
183                  struct exception* result
184 );
185
186 /* merge.c */
187
188 int
189 merge_entry (
190             Operation           *op, 
191             SlapReply           *rs,
192             struct berval       *query_uuid, 
193             struct exception    *result
194 ); 
195
196 int 
197 get_entry_size(Entry* e, 
198                int size_init, 
199                struct exception* result
200 ); 
201
202 /* remove.c */
203 int 
204 remove_query_data (
205                    Operation* conn,
206                    SlapReply *rs,
207                    struct berval* query_uuid, 
208                    struct exception* result
209 );
210 #endif