2 * Bacula hash table routines
4 * htable is a hash table of items (pointers). This code is
5 * adapted and enhanced from code I wrote in 1982 for a
6 * relocatable linker. At that time, the hash table size
7 * was fixed and a primary number, which essentially providing
8 * the hashing. In this program, the hash table can grow when
9 * it gets too full, so the table is a binary number. The
10 * hashing is provided using an idea from Tcl where the initial
11 * hash code is then "randomized" a simple calculation from
12 * a random number generator that multiplies by a big number
13 * then shifts the results down and does the binary division
14 * by masking. Increasing the size of the hash table is simple
15 * simply create a new larger table, walk the old table and
16 * insert each entry into the new table.
19 * Kern Sibbald, July MMIII
25 Copyright (C) 2000-2003 Kern Sibbald and John Walker
27 This program is free software; you can redistribute it and/or
28 modify it under the terms of the GNU General Public License as
29 published by the Free Software Foundation; either version 2 of
30 the License, or (at your option) any later version.
32 This program is distributed in the hope that it will be useful,
33 but WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 General Public License for more details.
37 You should have received a copy of the GNU General Public
38 License along with this program; if not, write to the Free
39 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
48 /* ===================================================================
53 * Create hash of key, stored in hash then
54 * create and return the pseudo random bucket index
56 void htable::hash_index(char *key)
59 for (char *p=key; *p; p++) {
60 hash += (hash << 3) + (uint32_t)*p;
62 /* Multiply by large prime number, take top bits, mask for remainder */
63 index = ((hash * 1103515249) >> rshift) & mask;
64 Dmsg2(100, "Leave hash_index hash=0x%x index=%d\n", hash, index);
68 htable::htable(void *item, void *link)
73 void htable::init(void *item, void *link)
75 loffset = (char *)link - (char *)item;
76 mask = 7; /* 3 bits => table size = 8 */
77 rshift = 27; /* start using bits 28, 29, 30 */
78 num_items = 0; /* number of entries in table */
79 buckets = 8; /* hash table size -- power of two */
80 max_items = buckets * 4; /* allow average 4 entries per chain */
81 table = (hlink **)malloc(buckets * sizeof(hlink *));
82 memset(table, 0, buckets * sizeof(hlink *));
87 void htable::grow_table()
89 Dmsg1(000, "Grow called old size = %d\n", buckets);
90 /* Setup a bigger table */
91 htable *big = (htable *)malloc(sizeof(htable));
92 big->loffset = loffset;
93 big->mask = mask<<1 | 1;
94 big->rshift = rshift - 1;
96 big->buckets = buckets * 2;
97 big->max_items = big->buckets * 4;
98 big->table = (hlink **)malloc(big->buckets * sizeof(hlink *));
99 memset(big->table, 0, big->buckets * sizeof(hlink *));
102 /* Insert all the items in the new hash table */
103 Dmsg1(100, "Before copy num_items=%d\n", num_items);
105 * We walk through the old smaller tree getting items,
106 * but since we are overwriting the colision links, we must
107 * explicitly save the item->next pointer and walk each
108 * colision chain ourselves. We do use next() for getting
109 * to the next bucket.
111 for (void *item=first(); item; ) {
112 void *ni = ((hlink *)((char *)item+loffset))->next; /* save link overwritten by insert */
113 Dmsg1(100, "Grow insert: %s\n", ((hlink *)((char *)item+loffset))->key);
114 big->insert(((hlink *)((char *)item+loffset))->key, item);
116 item = (void *)((char *)ni-loffset);
122 Dmsg1(100, "After copy new num_items=%d\n", big->num_items);
123 if (num_items != big->num_items) {
124 Dmsg0(000, "****** Big problems num_items mismatch ******\n");
127 memcpy(this, big, sizeof(htable)); /* move everything across */
129 Dmsg0(100, "Exit grow.\n");
132 bool htable::insert(char *key, void *item)
136 return false; /* already exists */
138 sm_check(__FILE__, __LINE__, False);
139 ASSERT(index < buckets);
140 Dmsg2(100, "Insert: hash=0x%x index=%d\n", (unsigned)hash, index);
141 hp = (hlink *)(((char *)item)+loffset);
142 Dmsg4(100, "Insert hp=0x%x index=%d item=0x%x offset=%u\n", (unsigned)hp,
143 index, (unsigned)item, loffset);
144 hp->next = table[index];
148 Dmsg3(100, "Insert hp->next=0x%x hp->hash=0x%x hp->key=%s\n",
149 (unsigned)hp->next, hp->hash, hp->key);
151 if (++num_items >= max_items) {
152 Dmsg2(100, "num_items=%d max_items=%d\n", num_items, max_items);
155 sm_check(__FILE__, __LINE__, False);
156 Dmsg3(100, "Leave insert index=%d num_items=%d key=%s\n", index, num_items, key);
160 void *htable::lookup(char *key)
163 for (hlink *hp=table[index]; hp; hp=(hlink *)hp->next) {
164 // Dmsg2(100, "hp=0x%x key=%s\n", (long)hp, hp->key);
165 if (hash == hp->hash && strcmp(key, hp->key) == 0) {
166 Dmsg1(100, "lookup return %x\n", ((char *)hp)-loffset);
167 return ((char *)hp)-loffset;
175 Dmsg1(100, "Enter next: walkptr=0x%x\n", (unsigned)walkptr);
177 walkptr = (hlink *)(walkptr->next);
179 while (!walkptr && walk_index < buckets) {
180 walkptr = table[walk_index++];
182 Dmsg3(100, "new walkptr=0x%x next=0x%x inx=%d\n", (unsigned)walkptr,
183 (unsigned)(walkptr->next), walk_index-1);
187 Dmsg2(100, "next: rtn 0x%x walk_index=%d\n",
188 (unsigned)(((char *)walkptr)-loffset), walk_index);
189 return ((char *)walkptr)-loffset;
191 Dmsg0(100, "next: return NULL\n");
195 void *htable::first()
197 Dmsg0(100, "Enter first\n");
198 walkptr = table[0]; /* get first bucket */
199 walk_index = 1; /* Point to next index */
200 while (!walkptr && walk_index < buckets) {
201 walkptr = table[walk_index++]; /* go to next bucket */
203 Dmsg3(100, "first new walkptr=0x%x next=0x%x inx=%d\n", (unsigned)walkptr,
204 (unsigned)(walkptr->next), walk_index-1);
208 Dmsg1(100, "Leave first walkptr=0x%x\n", (unsigned)walkptr);
209 return ((char *)walkptr)-loffset;
211 Dmsg0(100, "Leave first walkptr=NULL\n");
215 /* Destroy the table and its contents */
216 void htable::destroy()
228 Dmsg0(100, "Done destroy.\n");
246 MYJCR *save_jcr = NULL, *item;
250 jcrtbl = (htable *)malloc(sizeof(htable));
251 jcrtbl->init(jcr, &jcr->link);
253 Dmsg0(000, "Insert NITEMS items 0-19\n");
254 for (int i=0; i<NITEMS; i++) {
255 sprintf(mkey, "This is htable item %d", i);
256 jcr = (MYJCR *)malloc(sizeof(MYJCR));
257 Dmsg2(100, "link=0x%x jcr=0x%x\n", (unsigned)&jcr->link, (unsigned)jcr);
258 jcr->key = bstrdup(mkey);
260 jcrtbl->insert(jcr->key, jcr);
265 if (!(item = (MYJCR *)jcrtbl->lookup(save_jcr->key))) {
266 printf("Bad news: %s not found.\n", save_jcr->key);
268 printf("Item 10's key is: %s\n", item->key);
271 printf("Walk the hash table:\n");
272 for (MYJCR *jcr=(MYJCR *)jcrtbl->first(); jcr; jcr=(MYJCR *)jcrtbl->next() ) {
273 printf("htable item = %s\n", jcr->key);
277 printf("Got %d items -- %s\n", count, count==NITEMS?"OK":"***ERROR***");
278 printf("Calling destroy\n");
282 printf("Freed jcrtbl\n");