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 if (index(iname, '@') || (strlen(iname) > 8)) {
145 EXEC SQL SELECT users_id INTO :j FROM users WHERE login=:iname;
149 EXEC SQL SELECT list_id INTO :j FROM list WHERE name=:iname;
153 EXEC SQL SELECT mach_id INTO :j FROM machine WHERE name=UPPERCASE(:iname);
157 EXEC SQL SELECT clu_id INTO :j FROM cluster WHERE name=:iname;
161 EXEC SQL SELECT filsys_id INTO :j FROM filesys WHERE label=:iname;
165 EXEC SQL SELECT string_id INTO :j FROM strings WHERE string=:iname;
170 if (sqlca.sqlcode == 100)
172 if (sqlca.sqlerrd[2] > 1)
173 return(MR_NOT_UNIQUE);
174 if (sqlca.sqlcode != 0)
175 return(MR_INGRES_ERR);
177 if (name[0] == '#' && !strcasecmp(type, "USER"))
179 if (cachesize < CACHESIZE) {
180 i = (struct item *) malloc(sizeof(struct item));
184 cachehead.prev = i->prev;
185 i->prev->next = &cachehead;
187 strcpy(i->name, name);
188 strcpy(i->type, type);
191 i->next = cachehead.next;
192 i->prev = &cachehead;
193 cachehead.next->prev = i;
195 /* find the end of the transaction chain & add this item */
196 for (t = &cachehead; t->trans && t != i; t = t->trans);
199 i->trans = (struct item *)NULL;
205 /* Perform an ID to name mapping. name should be a pointer to a pointer to
206 * malloc'ed data. The buffer it refers to will be freed, and a new buffer
207 * allocated with the answer.
210 int id_to_name(id, type, name)
215 register struct item *i, *t;
216 EXEC SQL BEGIN DECLARE SECTION;
219 EXEC SQL END DECLARE SECTION;
222 for (i = cachehead.next; i != &cachehead; i = i->next) {
223 if (i->id != id || strcasecmp(type, i->type)) continue;
225 *name = strsave(i->name);
227 i->next->prev = i->prev;
228 i->prev->next = i->next;
229 i->next = cachehead.next;
230 i->prev = &cachehead;
231 cachehead.next->prev = i;
242 EXEC SQL SELECT CHAR(login) INTO :iname FROM users WHERE users_id=:j;
246 EXEC SQL SELECT CHAR(name) INTO :iname FROM list WHERE list_id=:j;
250 EXEC SQL SELECT CHAR(name) INTO :iname FROM machine WHERE mach_id=:j;
254 EXEC SQL SELECT CHAR(name) INTO :iname FROM cluster WHERE clu_id=:j;
258 EXEC SQL SELECT CHAR(label) INTO :iname FROM filesys WHERE filsys_id=:j;
262 EXEC SQL SELECT CHAR(string) INTO :iname FROM strings WHERE string_id=:j;
267 if (sqlca.sqlcode == 100) {
269 sprintf(iname, "#%d", j);
270 *name = strsave(iname);
273 if (sqlca.sqlerrd[2] > 1)
275 if (sqlca.sqlcode != 0)
276 return(MR_INGRES_ERR);
278 *name = strsave(strtrim(iname));
279 if (**name == '#' && !strcasecmp(type, "USER"))
281 if (cachesize < CACHESIZE) {
282 i = (struct item *) malloc(sizeof(struct item));
286 cachehead.prev = i->prev;
287 i->prev->next = &cachehead;
289 strcpy(i->name, *name);
290 strcpy(i->type, type);
291 i->nhash = hashname(*name, type);
293 i->next = cachehead.next;
294 i->prev = &cachehead;
295 cachehead.next->prev = i;
297 /* find the end of the transaction chain & add this item */
298 for (t = &cachehead; t->trans && t != i; t = t->trans);
301 i->trans = (struct item *)NULL;
307 /* Explicitly add something to the cache without doing a lookup in the
311 cache_entry(name, type, id)
316 register struct item *i, *t;
318 for (i = cachehead.next; i != &cachehead; i = i->next)
319 if (i->id == id && !strcmp(i->type, type)) {
320 if (strcmp(i->name, name)) {
321 strcpy(i->name, name);
322 i->nhash = hashname(name, type);
326 if (cachesize < CACHESIZE) {
327 i = (struct item *) malloc(sizeof(struct item));
331 cachehead.prev = i->prev;
332 i->prev->next = &cachehead;
334 strcpy(i->name, name);
335 strcpy(i->type, type);
336 i->nhash = hashname(name, type);
338 i->next = cachehead.next;
339 i->prev = &cachehead;
340 cachehead.next->prev = i;
342 /* find the end of the transaction chain & add this item */
343 for (t = &cachehead; t->trans && t != i; t = t->trans);
346 i->trans = (struct item *)NULL;
352 /* Flush something that may or may not already be in the cache. */
354 flush_name(name, type)
359 register struct item *i;
361 h = hashname(name, type);
363 for (i = cachehead.next; i != &cachehead; i = i->next) {
364 if (!strcmp(name, i->name) && !strcasecmp(type, i->type)) {
366 i->next->prev = i->prev;
367 i->prev->next = i->next;
375 /* Called when a transaction is committed to also commit any cache changes.
376 * Just throws away the list of cache changes for this transaction.
381 cachehead.trans = (struct item *)NULL;
385 /* Called whan a transaction is aborted to throw away any cache changes
386 * from that transaction.
391 register struct item *i, *t;
393 for (i = cachehead.trans; i; i = t) {
395 flush_name(i->name, i->type);
397 cachehead.trans = (struct item *)NULL;