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:
36 * then we will have to rework the code that only looks at the first
37 * letter of the types.
39 ***********************************************************************
42 /* Cache parameters: */
43 #define CACHESIZE 101 /* number of cache slots */
44 #define NAMESZ 257 /* max size of a name */
53 struct item *trans; /* keep track of transactions */
56 static struct item cachehead;
59 /* statistics counters */
60 int cachehits = 0, cachemisses = 0;
63 /* Name hash function. */
65 int hashname(name, type)
69 register int val = *type;
72 val = val<<5 - val + *name++ - '`';
77 /* Initialize the cache, flushing any old data, and report the statistics
78 * if the cache was previously in use.
83 register struct item *i;
85 if (cachehits + cachemisses != 0)
86 com_err(whoami, 0, "Flushing cache; %d hits, %d misses, %d%% hit rate",
87 cachehits, cachemisses,
88 (100 * cachehits) / (cachehits + cachemisses));
90 cachehead.next = cachehead.prev = &cachehead;
91 cachehits = cachemisses = cachesize = 0;
92 for (i = cachehead.next; i != &cachehead; i = i->next) {
93 if (i->prev != &cachehead)
96 if (cachehead.prev != &cachehead)
98 cachehead.next = cachehead.prev = &cachehead;
99 cachehead.trans = (struct item *)NULL;
103 /* Do a name to ID translation. id will be updated with the answer if
104 * it is available, and as a side effect the cache is updated.
107 int name_to_id(name, type, id)
112 register struct item *i, *t;
113 EXEC SQL BEGIN DECLARE SECTION;
116 EXEC SQL END DECLARE SECTION;
119 h = hashname(name, type);
120 for (i = cachehead.next; i != &cachehead; i = i->next) {
122 strcmp(name, i->name) ||
123 strcasecmp(type, i->type))
127 i->next->prev = i->prev;
128 i->prev->next = i->next;
129 i->next = cachehead.next;
130 i->prev = &cachehead;
131 cachehead.next->prev = i;
142 if (index(iname, '@') || (strlen(iname) > 8)) {
146 EXEC SQL SELECT users_id INTO :j FROM users WHERE login=:iname;
150 EXEC SQL SELECT list_id INTO :j FROM list WHERE name=:iname;
154 EXEC SQL SELECT mach_id INTO :j FROM machine WHERE name=UPPERCASE(:iname);
158 EXEC SQL SELECT snet_id INTO :j FROM subnet WHERE name=UPPERCASE(:iname);
162 EXEC SQL SELECT clu_id INTO :j FROM cluster WHERE name=:iname;
166 EXEC SQL SELECT filsys_id INTO :j FROM filesys WHERE label=:iname;
170 EXEC SQL SELECT string_id INTO :j FROM strings WHERE string=:iname;
175 if (sqlca.sqlcode == 100)
177 if (sqlca.sqlerrd[2] > 1)
178 return(MR_NOT_UNIQUE);
179 if (sqlca.sqlcode != 0)
180 return(MR_INGRES_ERR);
182 if (name[0] == '#' && !strcasecmp(type, "USER"))
184 if (cachesize < CACHESIZE) {
185 i = (struct item *) malloc(sizeof(struct item));
189 cachehead.prev = i->prev;
190 i->prev->next = &cachehead;
192 strcpy(i->name, name);
193 strcpy(i->type, type);
196 i->next = cachehead.next;
197 i->prev = &cachehead;
198 cachehead.next->prev = i;
200 /* find the end of the transaction chain & add this item */
201 for (t = &cachehead; t->trans && t != i; t = t->trans);
204 i->trans = (struct item *)NULL;
210 /* Perform an ID to name mapping. name should be a pointer to a pointer to
211 * malloc'ed data. The buffer it refers to will be freed, and a new buffer
212 * allocated with the answer.
215 int id_to_name(id, type, name)
220 register struct item *i, *t;
221 EXEC SQL BEGIN DECLARE SECTION;
224 EXEC SQL END DECLARE SECTION;
227 for (i = cachehead.next; i != &cachehead; i = i->next) {
228 if (i->id != id || strcasecmp(type, i->type)) continue;
230 *name = strsave(i->name);
232 i->next->prev = i->prev;
233 i->prev->next = i->next;
234 i->next = cachehead.next;
235 i->prev = &cachehead;
236 cachehead.next->prev = i;
247 EXEC SQL SELECT CHAR(login) INTO :iname FROM users WHERE users_id=:j;
251 EXEC SQL SELECT CHAR(name) INTO :iname FROM list WHERE list_id=:j;
255 EXEC SQL SELECT CHAR(name) INTO :iname FROM machine WHERE mach_id=:j;
259 EXEC SQL SELECT CHAR(name) INTO :iname FROM subnet WHERE snet_id=:j;
263 EXEC SQL SELECT CHAR(name) INTO :iname FROM cluster WHERE clu_id=:j;
267 EXEC SQL SELECT CHAR(label) INTO :iname FROM filesys WHERE filsys_id=:j;
271 EXEC SQL SELECT CHAR(string) INTO :iname FROM strings WHERE string_id=:j;
276 if (sqlca.sqlcode == 100) {
278 sprintf(iname, "#%d", j);
279 *name = strsave(iname);
282 if (sqlca.sqlerrd[2] > 1)
284 if (sqlca.sqlcode != 0)
285 return(MR_INGRES_ERR);
287 *name = strsave(strtrim(iname));
288 if (**name == '#' && !strcasecmp(type, "USER"))
290 if (cachesize < CACHESIZE) {
291 i = (struct item *) malloc(sizeof(struct item));
295 cachehead.prev = i->prev;
296 i->prev->next = &cachehead;
298 strcpy(i->name, *name);
299 strcpy(i->type, type);
300 i->nhash = hashname(*name, type);
302 i->next = cachehead.next;
303 i->prev = &cachehead;
304 cachehead.next->prev = i;
306 /* find the end of the transaction chain & add this item */
307 for (t = &cachehead; t->trans && t != i; t = t->trans);
310 i->trans = (struct item *)NULL;
316 /* Explicitly add something to the cache without doing a lookup in the
320 cache_entry(name, type, id)
325 register struct item *i, *t;
327 for (i = cachehead.next; i != &cachehead; i = i->next)
328 if (i->id == id && !strcmp(i->type, type)) {
329 if (strcmp(i->name, name)) {
330 strcpy(i->name, name);
331 i->nhash = hashname(name, type);
335 if (cachesize < CACHESIZE) {
336 i = (struct item *) malloc(sizeof(struct item));
340 cachehead.prev = i->prev;
341 i->prev->next = &cachehead;
343 strcpy(i->name, name);
344 strcpy(i->type, type);
345 i->nhash = hashname(name, type);
347 i->next = cachehead.next;
348 i->prev = &cachehead;
349 cachehead.next->prev = i;
351 /* find the end of the transaction chain & add this item */
352 for (t = &cachehead; t->trans && t != i; t = t->trans);
355 i->trans = (struct item *)NULL;
361 /* Flush something that may or may not already be in the cache. */
363 flush_name(name, type)
368 register struct item *i;
370 h = hashname(name, type);
372 for (i = cachehead.next; i != &cachehead; i = i->next) {
373 if (!strcmp(name, i->name) && !strcasecmp(type, i->type)) {
375 i->next->prev = i->prev;
376 i->prev->next = i->next;
384 /* Called when a transaction is committed to also commit any cache changes.
385 * Just throws away the list of cache changes for this transaction.
390 cachehead.trans = (struct item *)NULL;
394 /* Called whan a transaction is aborted to throw away any cache changes
395 * from that transaction.
400 register struct item *i, *t;
402 for (i = cachehead.trans; i; i = t) {
404 flush_name(i->name, i->type);
406 cachehead.trans = (struct item *)NULL;