bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / experimental / util_ldap_cache.h
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef APU_LDAP_CACHE_H
18 #define APU_LDAP_CACHE_H
19
20 /*
21  * This switches LDAP support on or off.
22  */
23
24 /* this whole thing disappears if LDAP is not enabled */
25 #ifdef APU_HAS_LDAP
26
27
28 /*
29  * LDAP Cache Manager
30  */
31
32 #if APR_HAS_SHARED_MEMORY
33 #include <apr_shm.h>
34 #include <apr_rmm.h> /* EDD */
35 #endif
36
37 typedef struct util_cache_node_t {
38     void *payload;              /* Pointer to the payload */
39     apr_time_t add_time;        /* Time node was added to cache */
40     struct util_cache_node_t *next;
41 } util_cache_node_t;
42
43 typedef struct util_ald_cache util_ald_cache_t;
44
45 struct util_ald_cache {
46     unsigned long size;                 /* Size of cache array */
47     unsigned long maxentries;           /* Maximum number of cache entries */
48     unsigned long numentries;           /* Current number of cache entries */
49     unsigned long fullmark;             /* Used to keep track of when cache becomes 3/4 full */
50     apr_time_t marktime;                /* Time that the cache became 3/4 full */
51     unsigned long (*hash)(void *);      /* Func to hash the payload */
52     int (*compare)(void *, void *);     /* Func to compare two payloads */
53     void * (*copy)(util_ald_cache_t *cache, void *); /* Func to alloc mem and copy payload to new mem */
54     void (*free)(util_ald_cache_t *cache, void *); /* Func to free mem used by the payload */
55     void (*display)(request_rec *r, util_ald_cache_t *cache, void *); /* Func to display the payload contents */
56     util_cache_node_t **nodes;
57
58     unsigned long numpurges;    /* No. of times the cache has been purged */
59     double avg_purgetime;       /* Average time to purge the cache */
60     apr_time_t last_purge;      /* Time of the last purge */
61     unsigned long npurged;      /* Number of elements purged in last purge. This is not
62                                    obvious: it won't be 3/4 the size of the cache if 
63                                    there were a lot of expired entries. */
64
65     unsigned long fetches;      /* Number of fetches */
66     unsigned long hits;         /* Number of cache hits */
67     unsigned long inserts;      /* Number of inserts */
68     unsigned long removes;      /* Number of removes */
69
70 #if APR_HAS_SHARED_MEMORY
71     apr_shm_t *shm_addr;
72     apr_rmm_t *rmm_addr;
73 #endif
74
75 };
76
77 #ifndef WIN32
78 #define ALD_MM_FILE_MODE ( S_IRUSR|S_IWUSR )
79 #else
80 #define ALD_MM_FILE_MODE ( _S_IREAD|_S_IWRITE )
81 #endif
82
83
84 /*
85  * LDAP Cache
86  */
87
88 /*
89  * Maintain a cache of LDAP URLs that the server handles. Each node in
90  * the cache contains the search cache for that URL, and a compare cache
91  * for the URL. The compare cash is populated when doing require group
92  * compares.
93  */
94 typedef struct util_url_node_t {
95     const char *url;
96     util_ald_cache_t *search_cache;
97     util_ald_cache_t *compare_cache;
98     util_ald_cache_t *dn_compare_cache;
99 } util_url_node_t;
100
101 /*
102  * We cache every successful search and bind operation, using the username 
103  * as the key. Each node in the cache contains the returned DN, plus the 
104  * password used to bind.
105  */
106 typedef struct util_search_node_t {
107     const char *username;               /* Cache key */
108     const char *dn;                     /* DN returned from search */
109     const char *bindpw;                 /* The most recently used bind password; 
110                                            NULL if the bind failed */
111     apr_time_t lastbind;                /* Time of last successful bind */
112     const char **vals;                  /* Values of queried attributes */
113     int        numvals;                 /* Number of queried attributes */
114 } util_search_node_t;
115
116 /*
117  * We cache every successful compare operation, using the DN, attrib, and
118  * value as the key. 
119  */
120 typedef struct util_compare_node_t {
121     const char *dn;                     /* DN, attrib and value combine to be the key */
122     const char *attrib;                 
123     const char *value;
124     apr_time_t lastcompare;
125     int result;
126 } util_compare_node_t;
127
128 /*
129  * We cache every successful compare dn operation, using the dn in the require
130  * statement and the dn fetched based on the client-provided username.
131  */
132 typedef struct util_dn_compare_node_t {
133     const char *reqdn;          /* The DN in the require dn statement */
134     const char *dn;                     /* The DN found in the search */
135 } util_dn_compare_node_t;
136
137
138 /*
139  * Function prototypes for LDAP cache
140  */
141
142 /* util_ldap_cache.c */
143 unsigned long util_ldap_url_node_hash(void *n);
144 int util_ldap_url_node_compare(void *a, void *b);
145 void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c);
146 void util_ldap_url_node_free(util_ald_cache_t *cache, void *n);
147 void util_ldap_url_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
148
149 unsigned long util_ldap_search_node_hash(void *n);
150 int util_ldap_search_node_compare(void *a, void *b);
151 void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c);
152 void util_ldap_search_node_free(util_ald_cache_t *cache, void *n);
153 void util_ldap_search_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
154
155 unsigned long util_ldap_compare_node_hash(void *n);
156 int util_ldap_compare_node_compare(void *a, void *b);
157 void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c);
158 void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n);
159 void util_ldap_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
160
161 unsigned long util_ldap_dn_compare_node_hash(void *n);
162 int util_ldap_dn_compare_node_compare(void *a, void *b);
163 void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c);
164 void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n);
165 void util_ldap_dn_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
166
167
168 /* util_ldap_cache_mgr.c */
169
170 /* Cache alloc and free function, dealing or not with shm */
171 void util_ald_free(util_ald_cache_t *cache, const void *ptr);
172 void *util_ald_alloc(util_ald_cache_t *cache, unsigned long size);
173 const char *util_ald_strdup(util_ald_cache_t *cache, const char *s);
174
175 /* Cache managing function */
176 unsigned long util_ald_hash_string(int nstr, ...);
177 void util_ald_cache_purge(util_ald_cache_t *cache);
178 util_url_node_t *util_ald_create_caches(util_ldap_state_t *s, const char *url);
179 util_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
180                                 unsigned long (*hashfunc)(void *), 
181                                 int (*comparefunc)(void *, void *),
182                                 void * (*copyfunc)(util_ald_cache_t *cache, void *),
183                                 void (*freefunc)(util_ald_cache_t *cache, void *),
184                                 void (*displayfunc)(request_rec *r, util_ald_cache_t *cache, void *));
185                                 
186 void util_ald_destroy_cache(util_ald_cache_t *cache);
187 void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload);
188 void *util_ald_cache_insert(util_ald_cache_t *cache, void *payload);
189 void util_ald_cache_remove(util_ald_cache_t *cache, void *payload);
190 char *util_ald_cache_display_stats(request_rec *r, util_ald_cache_t *cache, char *name, char *id);
191
192 #endif /* APU_HAS_LDAP */
193 #endif /* APU_LDAP_CACHE_H */