4 * Copyright (C) 1989, 1990 by the Massachusetts Institute of Technology
5 * For copying and distribution information, please see the file
10 static char *rcsid_cache_dc = "$Header$";
13 #include <mit-copyright.h>
15 #include "mr_server.h"
16 EXEC SQL INCLUDE sqlca;
18 EXEC SQL WHENEVER SQLERROR CALL ingerr;
20 extern char *whoami, *strsave();
21 extern int ingres_errno, mr_errcode;
24 /*** NOTE **************************************************************
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:
35 * then we will have to rework the code that only looks at the first
36 * letter of the types.
38 ***********************************************************************
41 /* Cache parameters: */
42 #define CACHESIZE 101 /* number of cache slots */
43 #define NAMESZ 257 /* max size of a name */
52 struct item *trans; /* keep track of transactions */
55 static struct item cachehead;
58 /* statistics counters */
59 int cachehits = 0, cachemisses = 0;
62 /* Name hash function. */
64 int hashname(name, type)
68 register int val = *type;
71 val = val<<5 - val + *name++ - '`';
76 /* Initialize the cache, flushing any old data, and report the statistics
77 * if the cache was previously in use.
82 register struct item *i;
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));
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)
95 if (cachehead.prev != &cachehead)
97 cachehead.next = cachehead.prev = &cachehead;
98 cachehead.trans = (struct item *)NULL;
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.
106 int name_to_id(name, type, id)
111 register struct item *i, *t;
112 EXEC SQL BEGIN DECLARE SECTION;
115 EXEC SQL END DECLARE SECTION;
118 h = hashname(name, type);
119 for (i = cachehead.next; i != &cachehead; i = i->next) {
121 strcmp(name, i->name) ||
122 strcasecmp(type, i->type))
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;
141 EXEC SQL SELECT users_id INTO :j FROM users WHERE login=:iname;
145 EXEC SQL SELECT list_id INTO :j FROM list WHERE name=:iname;
149 EXEC SQL SELECT mach_id INTO :j FROM machine WHERE name=UPPERCASE(:iname);
153 EXEC SQL SELECT clu_id INTO :j FROM cluster WHERE name=:iname;
157 EXEC SQL SELECT filsys_id INTO :j FROM filesys WHERE label=:iname;
161 EXEC SQL SELECT string_id INTO :j FROM strings WHERE string=:iname;
166 if (sqlca.sqlcode == 100)
168 if (sqlca.sqlerrd[2] > 1)
169 return(MR_NOT_UNIQUE);
170 if (sqlca.sqlcode != 0)
171 return(MR_INGRES_ERR);
173 if (name[0] == '#' && !strcasecmp(type, "USER"))
175 if (cachesize < CACHESIZE) {
176 i = (struct item *) malloc(sizeof(struct item));
180 cachehead.prev = i->prev;
181 i->prev->next = &cachehead;
183 strcpy(i->name, name);
184 strcpy(i->type, type);
187 i->next = cachehead.next;
188 i->prev = &cachehead;
189 cachehead.next->prev = i;
191 /* find the end of the transaction chain & add this item */
192 for (t = &cachehead; t->trans && t != i; t = t->trans);
195 i->trans = (struct item *)NULL;
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.
206 int id_to_name(id, type, name)
211 register struct item *i, *t;
212 EXEC SQL BEGIN DECLARE SECTION;
215 EXEC SQL END DECLARE SECTION;
218 for (i = cachehead.next; i != &cachehead; i = i->next) {
219 if (i->id != id || strcasecmp(type, i->type)) continue;
221 *name = strsave(i->name);
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;
238 EXEC SQL SELECT login INTO :iname FROM users WHERE users_id=:j;
242 EXEC SQL SELECT name INTO :iname FROM list WHERE list_id=:j;
246 EXEC SQL SELECT name INTO :iname FROM machine WHERE mach_id=:j;
250 EXEC SQL SELECT name INTO :iname FROM cluster WHERE clu_id=:j;
254 EXEC SQL SELECT label INTO :iname FROM filesys WHERE filsys_id=:j;
258 EXEC SQL SELECT string INTO :iname FROM strings WHERE string_id=:j;
263 if (sqlca.sqlcode == 100) {
265 sprintf(iname, "#%d", j);
266 *name = strsave(iname);
269 if (sqlca.sqlerrd[2] > 1)
271 if (sqlca.sqlcode != 0)
272 return(MR_INGRES_ERR);
274 *name = strsave(strtrim(iname));
275 if (**name == '#' && !strcasecmp(type, "USER"))
277 if (cachesize < CACHESIZE) {
278 i = (struct item *) malloc(sizeof(struct item));
282 cachehead.prev = i->prev;
283 i->prev->next = &cachehead;
285 strcpy(i->name, *name);
286 strcpy(i->type, type);
287 i->nhash = hashname(*name, type);
289 i->next = cachehead.next;
290 i->prev = &cachehead;
291 cachehead.next->prev = i;
293 /* find the end of the transaction chain & add this item */
294 for (t = &cachehead; t->trans && t != i; t = t->trans);
297 i->trans = (struct item *)NULL;
303 /* Explicitly add something to the cache without doing a lookup in the
307 cache_entry(name, type, id)
312 register struct item *i, *t;
314 for (i = cachehead.next; i != &cachehead; i = i->next)
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);
322 if (cachesize < CACHESIZE) {
323 i = (struct item *) malloc(sizeof(struct item));
327 cachehead.prev = i->prev;
328 i->prev->next = &cachehead;
330 strcpy(i->name, name);
331 strcpy(i->type, type);
332 i->nhash = hashname(name, type);
334 i->next = cachehead.next;
335 i->prev = &cachehead;
336 cachehead.next->prev = i;
338 /* find the end of the transaction chain & add this item */
339 for (t = &cachehead; t->trans && t != i; t = t->trans);
342 i->trans = (struct item *)NULL;
348 /* Flush something that may or may not already be in the cache. */
350 flush_name(name, type)
355 register struct item *i;
357 h = hashname(name, type);
359 for (i = cachehead.next; i != &cachehead; i = i->next) {
360 if (!strcmp(name, i->name) && !strcasecmp(type, i->type)) {
362 i->next->prev = i->prev;
363 i->prev->next = i->next;
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.
377 cachehead.trans = (struct item *)NULL;
381 /* Called whan a transaction is aborted to throw away any cache changes
382 * from that transaction.
387 register struct item *i, *t;
389 for (i = cachehead.trans; i; i = t) {
391 flush_name(i->name, i->type);
393 cachehead.trans = (struct item *)NULL;