1 /* $Header: hash.c,v 3.0.1.6 90/10/15 17:32:52 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 3.0.1.6 90/10/15 17:32:52 lwall
10 * patch29: non-existent array values no longer cause core dumps
11 * patch29: %foo = () will now clear dbm files
12 * patch29: dbm files couldn't be opened read only
13 * patch29: the cache array for dbm files wasn't correctly created on fetches
15 * Revision 3.0.1.5 90/08/13 22:18:27 lwall
16 * patch28: defined(@array) and defined(%array) didn't work right
18 * Revision 3.0.1.4 90/08/09 03:50:22 lwall
19 * patch19: dbmopen(name, 'filename', undef) now refrains from creating
21 * Revision 3.0.1.3 90/03/27 15:59:09 lwall
22 * patch16: @dbmvalues{'foo','bar'} could use the same cache entry for both values
24 * Revision 3.0.1.2 89/12/21 20:03:39 lwall
25 * patch7: errno may now be a macro with an lvalue
27 * Revision 3.0.1.1 89/11/11 04:34:18 lwall
28 * patch2: CX/UX needed to set the key each time in associative iterators
30 * Revision 3.0 89/10/18 15:18:32 lwall
38 static char coeff[] = {
39 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
40 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
41 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
42 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
43 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
44 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
45 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
46 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1};
48 static void hfreeentries();
51 hfetch(tb,key,klen,lval)
71 Newz(503,tb->tbl_array, tb->tbl_max + 1, HENT*);
76 /* The hash function we use on symbols has to be equal to the first
77 * character when taken modulo 128, so that str_reset() can be implemented
78 * efficiently. We throw in the second character and the last character
79 * (times 128) so that long chains of identifiers starting with the
80 * same letter don't have to be strEQ'ed within hfetch(), since it
81 * compares hash values before trying strEQ().
83 if (!tb->tbl_coeffsize)
84 hash = *key + 128 * key[1] + 128 * key[klen-1]; /* assuming klen > 0 */
85 else { /* use normal coefficients */
86 if (klen < tb->tbl_coeffsize)
89 maxi = tb->tbl_coeffsize;
90 for (s=key, i=0, hash = 0;
92 s++, i++, hash *= 5) {
93 hash += *s * coeff[i];
97 entry = tb->tbl_array[hash & tb->tbl_max];
98 for (; entry; entry = entry->hent_next) {
99 if (entry->hent_hash != hash) /* strings can't be equal */
101 if (entry->hent_klen != klen)
103 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
105 return entry->hent_val;
111 dcontent = dbm_fetch(tb->tbl_dbm,dkey);
112 if (dcontent.dptr) { /* found one */
113 str = Str_new(60,dcontent.dsize);
114 str_nset(str,dcontent.dptr,dcontent.dsize);
115 hstore(tb,key,klen,str,hash); /* cache it */
120 if (lval) { /* gonna assign to this, so it better be there */
122 hstore(tb,key,klen,str,hash);
129 hstore(tb,key,klen,val,hash)
138 register HENT *entry;
139 register HENT **oentry;
147 else if (!tb->tbl_coeffsize)
148 hash = *key + 128 * key[1] + 128 * key[klen-1];
149 else { /* use normal coefficients */
150 if (klen < tb->tbl_coeffsize)
153 maxi = tb->tbl_coeffsize;
154 for (s=key, i=0, hash = 0;
156 s++, i++, hash *= 5) {
157 hash += *s * coeff[i];
162 Newz(505,tb->tbl_array, tb->tbl_max + 1, HENT*);
164 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
167 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
168 if (entry->hent_hash != hash) /* strings can't be equal */
170 if (entry->hent_klen != klen)
172 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
174 Safefree(entry->hent_val);
175 entry->hent_val = val;
178 New(501,entry, 1, HENT);
180 entry->hent_klen = klen;
181 entry->hent_key = nsavestr(key,klen);
182 entry->hent_val = val;
183 entry->hent_hash = hash;
184 entry->hent_next = *oentry;
187 /* hdbmstore not necessary here because it's called from stabset() */
189 if (i) { /* initial entry? */
192 if (tb->tbl_dbm && tb->tbl_max >= DBM_CACHE_MAX)
195 if (tb->tbl_fill > tb->tbl_dosplit)
199 else if (tb->tbl_dbm) { /* is this just a cache for dbm file? */
200 void hentdelayfree();
202 entry = tb->tbl_array[hash & tb->tbl_max];
203 oentry = &entry->hent_next;
205 while (entry) { /* trim chain down to 1 entry */
206 *oentry = entry->hent_next;
207 hentdelayfree(entry); /* no doubt they'll want this next. */
225 register HENT *entry;
226 register HENT **oentry;
233 if (!tb || !tb->tbl_array)
235 if (!tb->tbl_coeffsize)
236 hash = *key + 128 * key[1] + 128 * key[klen-1];
237 else { /* use normal coefficients */
238 if (klen < tb->tbl_coeffsize)
241 maxi = tb->tbl_coeffsize;
242 for (s=key, i=0, hash = 0;
244 s++, i++, hash *= 5) {
245 hash += *s * coeff[i];
249 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
252 for (; entry; i=0, oentry = &entry->hent_next, entry = *oentry) {
253 if (entry->hent_hash != hash) /* strings can't be equal */
255 if (entry->hent_klen != klen)
257 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
259 *oentry = entry->hent_next;
260 str = str_static(entry->hent_val);
269 dbm_delete(tb->tbl_dbm,dkey);
285 int oldsize = tb->tbl_max + 1;
286 register int newsize = oldsize * 2;
290 register HENT *entry;
291 register HENT **oentry;
294 Renew(a, newsize, HENT*);
295 Zero(&a[oldsize], oldsize, HENT*); /* zero 2nd half*/
296 tb->tbl_max = --newsize;
297 tb->tbl_dosplit = tb->tbl_max * FILLPCT / 100;
300 for (i=0; i<oldsize; i++,a++) {
301 if (!*a) /* non-existent */
304 for (oentry = a, entry = *a; entry; entry = *oentry) {
305 if ((entry->hent_hash & newsize) != i) {
306 *oentry = entry->hent_next;
307 entry->hent_next = *b;
314 oentry = &entry->hent_next;
316 if (!*a) /* everything moved */
327 Newz(502,tb, 1, HASH);
329 tb->tbl_coeffsize = lookat;
330 tb->tbl_max = 7; /* it's a normal associative array */
331 tb->tbl_dosplit = tb->tbl_max * FILLPCT / 100;
334 tb->tbl_max = 127; /* it's a symbol table */
335 tb->tbl_dosplit = 128; /* so never split */
341 (void)hiterinit(tb); /* so each() will start off right */
351 str_free(hent->hent_val);
352 Safefree(hent->hent_key);
362 str_2static(hent->hent_val); /* free between statements */
363 Safefree(hent->hent_key);
374 hfreeentries(tb,dodbm);
378 (void)bzero((char*)tb->tbl_array, (tb->tbl_max + 1) * sizeof(HENT*));
383 hfreeentries(tb,dodbm)
388 register HENT *ohent = Null(HENT*);
399 if (!tb || !tb->tbl_array)
402 if ((old_dbm = tb->tbl_dbm) && dodbm) {
403 while (dkey = dbm_firstkey(tb->tbl_dbm), dkey.dptr) {
405 nextdkey = dbm_nextkey(tb->tbl_dbm, dkey);
406 dbm_delete(tb->tbl_dbm,dkey);
408 } while (dkey.dptr); /* one way or another, this works */
411 tb->tbl_dbm = 0; /* now clear just cache */
414 while (hent = hiternext(tb)) { /* concise but not very efficient */
420 tb->tbl_dbm = old_dbm;
431 hfreeentries(tb,dodbm);
432 Safefree(tb->tbl_array);
441 tb->tbl_eiter = Null(HENT*);
449 register HENT *entry;
454 entry = tb->tbl_eiter;
460 key.dptr = entry->hent_key;
461 key.dsize = entry->hent_klen;
462 key = dbm_nextkey(tb->tbl_dbm, key);
464 key = dbm_nextkey(tb->tbl_dbm);
467 key.dptr = entry->hent_key;
468 key.dsize = entry->hent_klen;
473 Newz(504,entry, 1, HENT);
474 tb->tbl_eiter = entry;
475 key = dbm_firstkey(tb->tbl_dbm);
477 entry->hent_key = key.dptr;
478 entry->hent_klen = key.dsize;
481 str_free(entry->hent_val);
483 tb->tbl_eiter = Null(HENT*);
490 Newz(506,tb->tbl_array, tb->tbl_max + 1, HENT*);
493 entry = entry->hent_next;
496 if (tb->tbl_riter > tb->tbl_max) {
500 entry = tb->tbl_array[tb->tbl_riter];
504 tb->tbl_eiter = entry;
509 hiterkey(entry,retlen)
510 register HENT *entry;
513 *retlen = entry->hent_klen;
514 return entry->hent_key;
520 register HENT *entry;
526 key.dptr = entry->hent_key;
527 key.dsize = entry->hent_klen;
528 content = dbm_fetch(tb->tbl_dbm,key);
529 if (!entry->hent_val)
530 entry->hent_val = Str_new(62,0);
531 str_nset(entry->hent_val,content.dptr,content.dsize);
534 return entry->hent_val;
538 #if defined(FCNTL) && ! defined(O_CREAT)
549 #define O_CREAT 01000
553 static int dbmrefcnt = 0;
557 hdbmopen(tb,fname,mode)
565 if (tb->tbl_dbm) /* never really closed it */
572 hclear(tb, FALSE); /* clear cache */
575 tb->tbl_dbm = dbm_open(fname, O_RDWR|O_CREAT, mode);
577 tb->tbl_dbm = dbm_open(fname, O_RDWR, mode);
579 tb->tbl_dbm = dbm_open(fname, O_RDONLY, mode);
582 fatal("Old dbm can only open one database");
583 sprintf(buf,"%s.dir",fname);
584 if (stat(buf, &statbuf) < 0) {
585 if (mode < 0 || close(creat(buf,mode)) < 0)
587 sprintf(buf,"%s.pag",fname);
588 if (close(creat(buf,mode)) < 0)
591 tb->tbl_dbm = dbminit(fname) >= 0;
593 if (!tb->tbl_array && tb->tbl_dbm != 0)
594 Newz(507,tb->tbl_array, tb->tbl_max + 1, HENT*);
595 return tb->tbl_dbm != 0;
602 if (tb && tb->tbl_dbm) {
604 dbm_close(tb->tbl_dbm);
607 /* dbmrefcnt--; */ /* doesn't work, rats */
611 warn("Close on unopened dbm file");
615 hdbmstore(tb,key,klen,str)
621 datum dkey, dcontent;
624 if (!tb || !tb->tbl_dbm)
628 dcontent.dptr = str_get(str);
629 dcontent.dsize = str->str_cur;
630 error = dbm_store(tb->tbl_dbm, dkey, dcontent, DBM_REPLACE);
633 fatal("No write permission to dbm file");
634 warn("dbm store returned %d, errno %d, key \"%s\"",error,errno,key);
636 dbm_clearerr(tb->tbl_dbm);
641 #endif /* SOME_DBM */