]> andersk Git - moira.git/blame - server/cache.dc
Version prior to beginning synchronization with opssrc version.
[moira.git] / server / cache.dc
CommitLineData
7de02494 1/*
2 * $Header$
3 *
4 * Copyright (C) 1989, 1990 by the Massachusetts Institute of Technology
5 * For copying and distribution information, please see the file
6 * <mit-copyright.h>.
7 */
8
9#ifndef lint
10static char *rcsid_cache_dc = "$Header$";
11#endif lint
12
13#include <mit-copyright.h>
14#include "query.h"
15#include "mr_server.h"
16EXEC SQL INCLUDE sqlca;
17
45bf7573 18EXEC SQL WHENEVER SQLERROR CALL ingerr;
a49e54ea 19
7de02494 20extern char *whoami, *strsave();
21extern int ingres_errno, mr_errcode;
22
23
24/*** NOTE **************************************************************
25 *
26 * This code depends on each type starting with a unique letter. If
27 * any new types are added to the system that begin with the same
28 * letter as one of the existing types:
29 * User
30 * List
31 * String
32 * Machine
33 * Cluster
34 * Filesystem
35 * then we will have to rework the code that only looks at the first
36 * letter of the types.
37 *
38 ***********************************************************************
39 */
40
41/* Cache parameters: */
42#define CACHESIZE 101 /* number of cache slots */
43#define NAMESZ 257 /* max size of a name */
44
45struct item {
46 char name[NAMESZ];
47 char type[9];
48 int nhash;
49 int id;
50 struct item *next;
51 struct item *prev;
a49e54ea 52 struct item *trans; /* keep track of transactions */
7de02494 53};
54
55static struct item cachehead;
56static int cachesize;
57
58/* statistics counters */
59int cachehits = 0, cachemisses = 0;
60
61
62/* Name hash function. */
63
64int hashname(name, type)
65register char *name;
66char *type;
67{
68 register int val = *type;
69
70 while (*name)
71 val = val<<5 - val + *name++ - '`';
72 return(val);
73}
74
75
76/* Initialize the cache, flushing any old data, and report the statistics
77 * if the cache was previously in use.
78 */
79
80flush_cache()
81{
82 register struct item *i;
83
84 if (cachehits + cachemisses != 0)
85 com_err(whoami, 0, "Flushing cache; %d hits, %d misses, %d%% hit rate",
86 cachehits, cachemisses,
87 (100 * cachehits) / (cachehits + cachemisses));
88 else
89 cachehead.next = cachehead.prev = &cachehead;
90 cachehits = cachemisses = cachesize = 0;
91 for (i = cachehead.next; i != &cachehead; i = i->next) {
92 if (i->prev != &cachehead)
93 free(i->prev);
94 }
95 if (cachehead.prev != &cachehead)
96 free(cachehead.prev);
97 cachehead.next = cachehead.prev = &cachehead;
a49e54ea 98 cachehead.trans = (struct item *)NULL;
7de02494 99}
100
101
102/* Do a name to ID translation. id will be updated with the answer if
103 * it is available, and as a side effect the cache is updated.
104 */
105
106int name_to_id(name, type, id)
107char *name;
108char *type;
109int *id;
110{
a49e54ea 111 register struct item *i, *t;
7de02494 112 EXEC SQL BEGIN DECLARE SECTION;
113 char *iname;
114 int j, rowcount;
115 EXEC SQL END DECLARE SECTION;
116 int h, ctr;
117
118 h = hashname(name, type);
119 for (i = cachehead.next; i != &cachehead; i = i->next) {
120 if (i->nhash != h ||
121 strcmp(name, i->name) ||
122 strcasecmp(type, i->type))
123 continue;
124 *id = i->id;
125 cachehits++;
126 i->next->prev = i->prev;
127 i->prev->next = i->next;
128 i->next = cachehead.next;
129 i->prev = &cachehead;
130 cachehead.next->prev = i;
131 cachehead.next = i;
132 return(MR_SUCCESS);
133 }
134
135 cachemisses++;
136 iname = name;
137
138 switch (*type) {
139 case 'U':
140 case 'u':
141 EXEC SQL SELECT users_id INTO :j FROM users WHERE login=:iname;
142 break;
143 case 'L':
144 case 'l':
145 EXEC SQL SELECT list_id INTO :j FROM list WHERE name=:iname;
146 break;
147 case 'M':
148 case 'm':
99e09b48 149 EXEC SQL SELECT mach_id INTO :j FROM machine WHERE name=UPPERCASE(:iname);
7de02494 150 break;
151 case 'C':
152 case 'c':
153 EXEC SQL SELECT clu_id INTO :j FROM cluster WHERE name=:iname;
154 break;
155 case 'F':
156 case 'f':
157 EXEC SQL SELECT filsys_id INTO :j FROM filesys WHERE label=:iname;
158 break;
159 case 'S':
160 case 's':
161 EXEC SQL SELECT string_id INTO :j FROM strings WHERE string=:iname;
162 break;
163 default:
164 return(MR_INTERNAL);
165 }
166 if (sqlca.sqlcode == 100)
167 return(MR_NO_MATCH);
168 if (sqlca.sqlerrd[2] > 1)
169 return(MR_NOT_UNIQUE);
170 if (sqlca.sqlcode != 0)
171 return(MR_INGRES_ERR);
172 *id = j;
173 if (name[0] == '#' && !strcasecmp(type, "USER"))
174 return(MR_SUCCESS);
175 if (cachesize < CACHESIZE) {
176 i = (struct item *) malloc(sizeof(struct item));
177 cachesize++;
178 } else {
179 i = cachehead.prev;
180 cachehead.prev = i->prev;
181 i->prev->next = &cachehead;
182 }
183 strcpy(i->name, name);
184 strcpy(i->type, type);
185 i->nhash = h;
186 i->id = j;
187 i->next = cachehead.next;
188 i->prev = &cachehead;
189 cachehead.next->prev = i;
190 cachehead.next = i;
a49e54ea 191 /* find the end of the transaction chain & add this item */
192 for (t = &cachehead; t->trans && t != i; t = t->trans);
193 if (t != i) {
194 t->trans = i;
195 i->trans = (struct item *)NULL;
196 }
7de02494 197 return(MR_SUCCESS);
198}
199
200
201/* Perform an ID to name mapping. name should be a pointer to a pointer to
202 * malloc'ed data. The buffer it refers to will be freed, and a new buffer
203 * allocated with the answer.
204 */
205
206int id_to_name(id, type, name)
207int id;
208char *type;
209char **name;
210{
45bf7573 211 register struct item *i, *t;
7de02494 212 EXEC SQL BEGIN DECLARE SECTION;
213 char iname[NAMESZ];
214 int j, rowcount;
215 EXEC SQL END DECLARE SECTION;
216 int ctr;
217
218 for (i = cachehead.next; i != &cachehead; i = i->next) {
219 if (i->id != id || strcasecmp(type, i->type)) continue;
220 free(*name);
221 *name = strsave(i->name);
222 cachehits++;
223 i->next->prev = i->prev;
224 i->prev->next = i->next;
225 i->next = cachehead.next;
226 i->prev = &cachehead;
227 cachehead.next->prev = i;
228 cachehead.next = i;
229 return(MR_SUCCESS);
230 }
231
232 cachemisses++;
233 j = id;
234
235 switch (*type) {
236 case 'U':
237 case 'u':
238 EXEC SQL SELECT login INTO :iname FROM users WHERE users_id=:j;
239 break;
240 case 'L':
241 case 'l':
242 EXEC SQL SELECT name INTO :iname FROM list WHERE list_id=:j;
243 break;
244 case 'M':
245 case 'm':
246 EXEC SQL SELECT name INTO :iname FROM machine WHERE mach_id=:j;
247 break;
248 case 'C':
249 case 'c':
250 EXEC SQL SELECT name INTO :iname FROM cluster WHERE clu_id=:j;
251 break;
252 case 'F':
253 case 'f':
254 EXEC SQL SELECT label INTO :iname FROM filesys WHERE filsys_id=:j;
255 break;
256 case 'S':
257 case 's':
258 EXEC SQL SELECT string INTO :iname FROM strings WHERE string_id=:j;
259 break;
260 default:
261 return(MR_INTERNAL);
262 }
263 if (sqlca.sqlcode == 100) {
264 free(*name);
265 sprintf(iname, "#%d", j);
266 *name = strsave(iname);
267 return(MR_NO_MATCH);
268 }
269 if (sqlca.sqlerrd[2] > 1)
270 return(MR_INTERNAL);
271 if (sqlca.sqlcode != 0)
272 return(MR_INGRES_ERR);
273 free(*name);
274 *name = strsave(strtrim(iname));
275 if (**name == '#' && !strcasecmp(type, "USER"))
276 return(MR_SUCCESS);
277 if (cachesize < CACHESIZE) {
278 i = (struct item *) malloc(sizeof(struct item));
279 cachesize++;
280 } else {
281 i = cachehead.prev;
282 cachehead.prev = i->prev;
283 i->prev->next = &cachehead;
284 }
285 strcpy(i->name, *name);
286 strcpy(i->type, type);
287 i->nhash = hashname(*name, type);
288 i->id = id;
289 i->next = cachehead.next;
290 i->prev = &cachehead;
291 cachehead.next->prev = i;
292 cachehead.next = i;
a49e54ea 293 /* find the end of the transaction chain & add this item */
294 for (t = &cachehead; t->trans && t != i; t = t->trans);
295 if (t != i) {
296 t->trans = i;
297 i->trans = (struct item *)NULL;
298 }
7de02494 299 return(MR_SUCCESS);
300}
301
302
303/* Explicitly add something to the cache without doing a lookup in the
304 * database.
305 */
306
307cache_entry(name, type, id)
308char *name;
309char *type;
310int id;
311{
a49e54ea 312 register struct item *i, *t;
7de02494 313
314 for (i = cachehead.next; i != &cachehead; i = i->next)
a49e54ea 315 if (i->id == id && !strcmp(i->type, type)) {
316 if (strcmp(i->name, name)) {
317 strcpy(i->name, name);
318 i->nhash = hashname(name, type);
319 }
320 return(MR_SUCCESS);
321 }
7de02494 322 if (cachesize < CACHESIZE) {
323 i = (struct item *) malloc(sizeof(struct item));
324 cachesize++;
325 } else {
326 i = cachehead.prev;
327 cachehead.prev = i->prev;
328 i->prev->next = &cachehead;
329 }
330 strcpy(i->name, name);
331 strcpy(i->type, type);
332 i->nhash = hashname(name, type);
333 i->id = id;
334 i->next = cachehead.next;
335 i->prev = &cachehead;
336 cachehead.next->prev = i;
337 cachehead.next = i;
a49e54ea 338 /* find the end of the transaction chain & add this item */
339 for (t = &cachehead; t->trans && t != i; t = t->trans);
340 if (t != i) {
341 t->trans = i;
342 i->trans = (struct item *)NULL;
343 }
7de02494 344 return(MR_SUCCESS);
345}
346
347
348/* Flush something that may or may not already be in the cache. */
349
350flush_name(name, type)
351char *name;
352char *type;
353{
354 int h;
355 register struct item *i;
356
357 h = hashname(name, type);
358
359 for (i = cachehead.next; i != &cachehead; i = i->next) {
360 if (!strcmp(name, i->name) && !strcasecmp(type, i->type)) {
361 cachesize--;
362 i->next->prev = i->prev;
363 i->prev->next = i->next;
364 free(i);
365 return(MR_SUCCESS);
366 }
367 }
368}
a49e54ea 369
370
371/* Called when a transaction is committed to also commit any cache changes.
372 * Just throws away the list of cache changes for this transaction.
373 */
374
375cache_commit()
376{
377 cachehead.trans = (struct item *)NULL;
378}
379
380
381/* Called whan a transaction is aborted to throw away any cache changes
382 * from that transaction.
383 */
384
385cache_abort()
386{
387 register struct item *i, *t;
388
389 for (i = cachehead.trans; i; i = t) {
390 t = i->trans;
391 flush_name(i->name, i->type);
392 }
393 cachehead.trans = (struct item *)NULL;
394}
This page took 0.158411 seconds and 5 git commands to generate.