hash.c revision 1.8 1 /* $NetBSD: hash.c,v 1.8 1995/02/27 13:21:59 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Margo Seltzer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
42 #else
43 static char rcsid[] = "$NetBSD: hash.c,v 1.8 1995/02/27 13:21:59 cgd Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46
47 #include <sys/param.h>
48 #include <sys/stat.h>
49
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #ifdef DEBUG
57 #include <assert.h>
58 #endif
59
60 #include <db.h>
61 #include "hash.h"
62 #include "page.h"
63 #include "extern.h"
64
65 static int alloc_segs __P((HTAB *, int));
66 static int flush_meta __P((HTAB *));
67 static int hash_access __P((HTAB *, ACTION, DBT *, DBT *));
68 static int hash_close __P((DB *));
69 static int hash_delete __P((const DB *, const DBT *, u_int32_t));
70 static int hash_fd __P((const DB *));
71 static int hash_get __P((const DB *, const DBT *, DBT *, u_int32_t));
72 static int hash_put __P((const DB *, DBT *, const DBT *, u_int32_t));
73 static void *hash_realloc __P((SEGMENT **, int, int));
74 static int hash_seq __P((const DB *, DBT *, DBT *, u_int32_t));
75 static int hash_sync __P((const DB *, u_int32_t));
76 static int hdestroy __P((HTAB *));
77 static HTAB *init_hash __P((HTAB *, const char *, HASHINFO *));
78 static int init_htab __P((HTAB *, int));
79 #if BYTE_ORDER == LITTLE_ENDIAN
80 static void swap_header __P((HTAB *));
81 static void swap_header_copy __P((HASHHDR *, HASHHDR *));
82 #endif
83
84 /* Fast arithmetic, relying on powers of 2, */
85 #define MOD(x, y) ((x) & ((y) - 1))
86
87 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
88
89 /* Return values */
90 #define SUCCESS (0)
91 #define ERROR (-1)
92 #define ABNORMAL (1)
93
94 #ifdef HASH_STATISTICS
95 int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
96 #endif
97
98 /************************** INTERFACE ROUTINES ***************************/
99 /* OPEN/CLOSE */
100
101 extern DB *
102 __hash_open(file, flags, mode, info, dflags)
103 const char *file;
104 int flags, mode, dflags;
105 const HASHINFO *info; /* Special directives for create */
106 {
107 HTAB *hashp;
108 struct stat statbuf;
109 DB *dbp;
110 int bpages, hdrsize, new_table, nsegs, save_errno;
111
112 if ((flags & O_ACCMODE) == O_WRONLY) {
113 errno = EINVAL;
114 return (NULL);
115 }
116
117 if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
118 return (NULL);
119 hashp->fp = -1;
120
121 /*
122 * Even if user wants write only, we need to be able to read
123 * the actual file, so we need to open it read/write. But, the
124 * field in the hashp structure needs to be accurate so that
125 * we can check accesses.
126 */
127 hashp->flags = flags;
128
129 new_table = 0;
130 if (!file || (flags & O_TRUNC) ||
131 (stat(file, &statbuf) && (errno == ENOENT))) {
132 if (errno == ENOENT)
133 errno = 0; /* Just in case someone looks at errno */
134 new_table = 1;
135 }
136 if (file) {
137 if ((hashp->fp = open(file, flags, mode)) == -1)
138 RETURN_ERROR(errno, error0);
139 (void)fcntl(hashp->fp, F_SETFD, 1);
140 }
141 if (new_table) {
142 if (!(hashp = init_hash(hashp, file, (HASHINFO *)info)))
143 RETURN_ERROR(errno, error1);
144 } else {
145 /* Table already exists */
146 if (info && info->hash)
147 hashp->hash = info->hash;
148 else
149 hashp->hash = __default_hash;
150
151 hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
152 #if BYTE_ORDER == LITTLE_ENDIAN
153 swap_header(hashp);
154 #endif
155 if (hdrsize == -1)
156 RETURN_ERROR(errno, error1);
157 if (hdrsize != sizeof(HASHHDR))
158 RETURN_ERROR(EFTYPE, error1);
159 /* Verify file type, versions and hash function */
160 if (hashp->MAGIC != HASHMAGIC)
161 RETURN_ERROR(EFTYPE, error1);
162 #define OLDHASHVERSION 1
163 if (hashp->VERSION != HASHVERSION &&
164 hashp->VERSION != OLDHASHVERSION)
165 RETURN_ERROR(EFTYPE, error1);
166 if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
167 RETURN_ERROR(EFTYPE, error1);
168 /*
169 * Figure out how many segments we need. Max_Bucket is the
170 * maximum bucket number, so the number of buckets is
171 * max_bucket + 1.
172 */
173 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
174 hashp->SGSIZE;
175 hashp->nsegs = 0;
176 if (alloc_segs(hashp, nsegs))
177 /*
178 * If alloc_segs fails, table will have been destroyed
179 * and errno will have been set.
180 */
181 return (NULL);
182 /* Read in bitmaps */
183 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
184 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
185 (hashp->BSHIFT + BYTE_SHIFT);
186
187 hashp->nmaps = bpages;
188 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
189 }
190
191 /* Initialize Buffer Manager */
192 if (info && info->cachesize)
193 __buf_init(hashp, info->cachesize);
194 else
195 __buf_init(hashp, DEF_BUFSIZE);
196
197 hashp->new_file = new_table;
198 hashp->save_file = file && (hashp->flags & O_RDWR);
199 hashp->cbucket = -1;
200 if (!(dbp = (DB *)malloc(sizeof(DB)))) {
201 save_errno = errno;
202 hdestroy(hashp);
203 errno = save_errno;
204 return (NULL);
205 }
206 dbp->internal = hashp;
207 dbp->close = hash_close;
208 dbp->del = hash_delete;
209 dbp->fd = hash_fd;
210 dbp->get = hash_get;
211 dbp->put = hash_put;
212 dbp->seq = hash_seq;
213 dbp->sync = hash_sync;
214 dbp->type = DB_HASH;
215
216 #ifdef DEBUG
217 (void)fprintf(stderr,
218 "%s\n%s%x\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
219 "init_htab:",
220 "TABLE POINTER ", hashp,
221 "BUCKET SIZE ", hashp->BSIZE,
222 "BUCKET SHIFT ", hashp->BSHIFT,
223 "DIRECTORY SIZE ", hashp->DSIZE,
224 "SEGMENT SIZE ", hashp->SGSIZE,
225 "SEGMENT SHIFT ", hashp->SSHIFT,
226 "FILL FACTOR ", hashp->FFACTOR,
227 "MAX BUCKET ", hashp->MAX_BUCKET,
228 "OVFL POINT ", hashp->OVFL_POINT,
229 "LAST FREED ", hashp->LAST_FREED,
230 "HIGH MASK ", hashp->HIGH_MASK,
231 "LOW MASK ", hashp->LOW_MASK,
232 "NSEGS ", hashp->nsegs,
233 "NKEYS ", hashp->NKEYS);
234 #endif
235 #ifdef HASH_STATISTICS
236 hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
237 #endif
238 return (dbp);
239
240 error1:
241 if (hashp != NULL)
242 (void)close(hashp->fp);
243
244 error0:
245 free(hashp);
246 errno = save_errno;
247 return (NULL);
248 }
249
250 static int
251 hash_close(dbp)
252 DB *dbp;
253 {
254 HTAB *hashp;
255 int retval;
256
257 if (!dbp)
258 return (ERROR);
259
260 hashp = (HTAB *)dbp->internal;
261 retval = hdestroy(hashp);
262 free(dbp);
263 return (retval);
264 }
265
266 static int
267 hash_fd(dbp)
268 const DB *dbp;
269 {
270 HTAB *hashp;
271
272 if (!dbp)
273 return (ERROR);
274
275 hashp = (HTAB *)dbp->internal;
276 if (hashp->fp == -1) {
277 errno = ENOENT;
278 return (-1);
279 }
280 return (hashp->fp);
281 }
282
283 /************************** LOCAL CREATION ROUTINES **********************/
284 static HTAB *
285 init_hash(hashp, file, info)
286 HTAB *hashp;
287 const char *file;
288 HASHINFO *info;
289 {
290 struct stat statbuf;
291 int nelem;
292
293 nelem = 1;
294 hashp->NKEYS = 0;
295 hashp->LORDER = BYTE_ORDER;
296 hashp->BSIZE = DEF_BUCKET_SIZE;
297 hashp->BSHIFT = DEF_BUCKET_SHIFT;
298 hashp->SGSIZE = DEF_SEGSIZE;
299 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
300 hashp->DSIZE = DEF_DIRSIZE;
301 hashp->FFACTOR = DEF_FFACTOR;
302 hashp->hash = __default_hash;
303 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
304 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
305
306 /* Fix bucket size to be optimal for file system */
307 if (file != NULL) {
308 if (stat(file, &statbuf))
309 return (NULL);
310 hashp->BSIZE = statbuf.st_blksize;
311 hashp->BSHIFT = __log2(hashp->BSIZE);
312 }
313
314 if (info) {
315 if (info->bsize) {
316 /* Round pagesize up to power of 2 */
317 hashp->BSHIFT = __log2(info->bsize);
318 hashp->BSIZE = 1 << hashp->BSHIFT;
319 if (hashp->BSIZE > MAX_BSIZE) {
320 errno = EINVAL;
321 return (NULL);
322 }
323 }
324 if (info->ffactor)
325 hashp->FFACTOR = info->ffactor;
326 if (info->hash)
327 hashp->hash = info->hash;
328 if (info->nelem)
329 nelem = info->nelem;
330 if (info->lorder) {
331 if (info->lorder != BIG_ENDIAN &&
332 info->lorder != LITTLE_ENDIAN) {
333 errno = EINVAL;
334 return (NULL);
335 }
336 hashp->LORDER = info->lorder;
337 }
338 }
339 /* init_htab should destroy the table and set errno if it fails */
340 if (init_htab(hashp, nelem))
341 return (NULL);
342 else
343 return (hashp);
344 }
345 /*
346 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
347 * the table and set errno, so we just pass the error information along.
348 *
349 * Returns 0 on No Error
350 */
351 static int
352 init_htab(hashp, nelem)
353 HTAB *hashp;
354 int nelem;
355 {
356 register int nbuckets, nsegs;
357 int l2;
358
359 /*
360 * Divide number of elements by the fill factor and determine a
361 * desired number of buckets. Allocate space for the next greater
362 * power of two number of buckets.
363 */
364 nelem = (nelem - 1) / hashp->FFACTOR + 1;
365
366 l2 = __log2(MAX(nelem, 2));
367 nbuckets = 1 << l2;
368
369 hashp->SPARES[l2] = l2 + 1;
370 hashp->SPARES[l2 + 1] = l2 + 1;
371 hashp->OVFL_POINT = l2;
372 hashp->LAST_FREED = 2;
373
374 /* First bitmap page is at: splitpoint l2 page offset 1 */
375 if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
376 return (-1);
377
378 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
379 hashp->HIGH_MASK = (nbuckets << 1) - 1;
380 hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
381 hashp->BSHIFT) + 1;
382
383 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
384 nsegs = 1 << __log2(nsegs);
385
386 if (nsegs > hashp->DSIZE)
387 hashp->DSIZE = nsegs;
388 return (alloc_segs(hashp, nsegs));
389 }
390
391 /********************** DESTROY/CLOSE ROUTINES ************************/
392
393 /*
394 * Flushes any changes to the file if necessary and destroys the hashp
395 * structure, freeing all allocated space.
396 */
397 static int
398 hdestroy(hashp)
399 HTAB *hashp;
400 {
401 int i, save_errno;
402
403 save_errno = 0;
404
405 #ifdef HASH_STATISTICS
406 (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
407 hash_accesses, hash_collisions);
408 (void)fprintf(stderr, "hdestroy: expansions %ld\n",
409 hash_expansions);
410 (void)fprintf(stderr, "hdestroy: overflows %ld\n",
411 hash_overflows);
412 (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
413 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
414
415 for (i = 0; i < NCACHED; i++)
416 (void)fprintf(stderr,
417 "spares[%d] = %d\n", i, hashp->SPARES[i]);
418 #endif
419 /*
420 * Call on buffer manager to free buffers, and if required,
421 * write them to disk.
422 */
423 if (__buf_free(hashp, 1, hashp->save_file))
424 save_errno = errno;
425 if (hashp->dir) {
426 free(*hashp->dir); /* Free initial segments */
427 /* Free extra segments */
428 while (hashp->exsegs--)
429 free(hashp->dir[--hashp->nsegs]);
430 free(hashp->dir);
431 }
432 if (flush_meta(hashp) && !save_errno)
433 save_errno = errno;
434 /* Free Bigmaps */
435 for (i = 0; i < hashp->nmaps; i++)
436 if (hashp->mapp[i])
437 free(hashp->mapp[i]);
438
439 if (hashp->fp != -1)
440 (void)close(hashp->fp);
441
442 free(hashp);
443
444 if (save_errno) {
445 errno = save_errno;
446 return (ERROR);
447 }
448 return (SUCCESS);
449 }
450 /*
451 * Write modified pages to disk
452 *
453 * Returns:
454 * 0 == OK
455 * -1 ERROR
456 */
457 static int
458 hash_sync(dbp, flags)
459 const DB *dbp;
460 u_int32_t flags;
461 {
462 HTAB *hashp;
463
464 if (flags != 0) {
465 errno = EINVAL;
466 return (ERROR);
467 }
468
469 if (!dbp)
470 return (ERROR);
471
472 hashp = (HTAB *)dbp->internal;
473 if (!hashp->save_file)
474 return (0);
475 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
476 return (ERROR);
477 hashp->new_file = 0;
478 return (0);
479 }
480
481 /*
482 * Returns:
483 * 0 == OK
484 * -1 indicates that errno should be set
485 */
486 static int
487 flush_meta(hashp)
488 HTAB *hashp;
489 {
490 HASHHDR *whdrp;
491 #if BYTE_ORDER == LITTLE_ENDIAN
492 HASHHDR whdr;
493 #endif
494 int fp, i, wsize;
495
496 if (!hashp->save_file)
497 return (0);
498 hashp->MAGIC = HASHMAGIC;
499 hashp->VERSION = HASHVERSION;
500 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
501
502 fp = hashp->fp;
503 whdrp = &hashp->hdr;
504 #if BYTE_ORDER == LITTLE_ENDIAN
505 whdrp = &whdr;
506 swap_header_copy(&hashp->hdr, whdrp);
507 #endif
508 if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
509 ((wsize = write(fp, whdrp, sizeof(HASHHDR))) == -1))
510 return (-1);
511 else
512 if (wsize != sizeof(HASHHDR)) {
513 errno = EFTYPE;
514 hashp->errno = errno;
515 return (-1);
516 }
517 for (i = 0; i < NCACHED; i++)
518 if (hashp->mapp[i])
519 if (__put_page(hashp, (char *)hashp->mapp[i],
520 hashp->BITMAPS[i], 0, 1))
521 return (-1);
522 return (0);
523 }
524
525 /*******************************SEARCH ROUTINES *****************************/
526 /*
527 * All the access routines return
528 *
529 * Returns:
530 * 0 on SUCCESS
531 * 1 to indicate an external ERROR (i.e. key not found, etc)
532 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
533 */
534 static int
535 hash_get(dbp, key, data, flag)
536 const DB *dbp;
537 const DBT *key;
538 DBT *data;
539 u_int32_t flag;
540 {
541 HTAB *hashp;
542
543 hashp = (HTAB *)dbp->internal;
544 if (flag) {
545 hashp->errno = errno = EINVAL;
546 return (ERROR);
547 }
548 return (hash_access(hashp, HASH_GET, (DBT *)key, data));
549 }
550
551 static int
552 hash_put(dbp, key, data, flag)
553 const DB *dbp;
554 DBT *key;
555 const DBT *data;
556 u_int32_t flag;
557 {
558 HTAB *hashp;
559
560 hashp = (HTAB *)dbp->internal;
561 if (flag && flag != R_NOOVERWRITE) {
562 hashp->errno = errno = EINVAL;
563 return (ERROR);
564 }
565 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
566 hashp->errno = errno = EPERM;
567 return (ERROR);
568 }
569 return (hash_access(hashp, flag == R_NOOVERWRITE ?
570 HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
571 }
572
573 static int
574 hash_delete(dbp, key, flag)
575 const DB *dbp;
576 const DBT *key;
577 u_int32_t flag; /* Ignored */
578 {
579 HTAB *hashp;
580
581 hashp = (HTAB *)dbp->internal;
582 if (flag && flag != R_CURSOR) {
583 hashp->errno = errno = EINVAL;
584 return (ERROR);
585 }
586 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
587 hashp->errno = errno = EPERM;
588 return (ERROR);
589 }
590 return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
591 }
592
593 /*
594 * Assume that hashp has been set in wrapper routine.
595 */
596 static int
597 hash_access(hashp, action, key, val)
598 HTAB *hashp;
599 ACTION action;
600 DBT *key, *val;
601 {
602 register BUFHEAD *rbufp;
603 BUFHEAD *bufp, *save_bufp;
604 register u_int16_t *bp;
605 register int n, ndx, off, size;
606 register char *kp;
607 u_int16_t pageno;
608
609 #ifdef HASH_STATISTICS
610 hash_accesses++;
611 #endif
612
613 off = hashp->BSIZE;
614 size = key->size;
615 kp = (char *)key->data;
616 rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
617 if (!rbufp)
618 return (ERROR);
619 save_bufp = rbufp;
620
621 /* Pin the bucket chain */
622 rbufp->flags |= BUF_PIN;
623 for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
624 if (bp[1] >= REAL_KEY) {
625 /* Real key/data pair */
626 if (size == off - *bp &&
627 memcmp(kp, rbufp->page + *bp, size) == 0)
628 goto found;
629 off = bp[1];
630 #ifdef HASH_STATISTICS
631 hash_collisions++;
632 #endif
633 bp += 2;
634 ndx += 2;
635 } else if (bp[1] == OVFLPAGE) {
636 rbufp = __get_buf(hashp, *bp, rbufp, 0);
637 if (!rbufp) {
638 save_bufp->flags &= ~BUF_PIN;
639 return (ERROR);
640 }
641 /* FOR LOOP INIT */
642 bp = (u_int16_t *)rbufp->page;
643 n = *bp++;
644 ndx = 1;
645 off = hashp->BSIZE;
646 } else if (bp[1] < REAL_KEY) {
647 if ((ndx =
648 __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
649 goto found;
650 if (ndx == -2) {
651 bufp = rbufp;
652 if (!(pageno =
653 __find_last_page(hashp, &bufp))) {
654 ndx = 0;
655 rbufp = bufp;
656 break; /* FOR */
657 }
658 rbufp = __get_buf(hashp, pageno, bufp, 0);
659 if (!rbufp) {
660 save_bufp->flags &= ~BUF_PIN;
661 return (ERROR);
662 }
663 /* FOR LOOP INIT */
664 bp = (u_int16_t *)rbufp->page;
665 n = *bp++;
666 ndx = 1;
667 off = hashp->BSIZE;
668 } else {
669 save_bufp->flags &= ~BUF_PIN;
670 return (ERROR);
671 }
672 }
673
674 /* Not found */
675 switch (action) {
676 case HASH_PUT:
677 case HASH_PUTNEW:
678 if (__addel(hashp, rbufp, key, val)) {
679 save_bufp->flags &= ~BUF_PIN;
680 return (ERROR);
681 } else {
682 save_bufp->flags &= ~BUF_PIN;
683 return (SUCCESS);
684 }
685 case HASH_GET:
686 case HASH_DELETE:
687 default:
688 save_bufp->flags &= ~BUF_PIN;
689 return (ABNORMAL);
690 }
691
692 found:
693 switch (action) {
694 case HASH_PUTNEW:
695 save_bufp->flags &= ~BUF_PIN;
696 return (ABNORMAL);
697 case HASH_GET:
698 bp = (u_int16_t *)rbufp->page;
699 if (bp[ndx + 1] < REAL_KEY) {
700 if (__big_return(hashp, rbufp, ndx, val, 0))
701 return (ERROR);
702 } else {
703 val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
704 val->size = bp[ndx] - bp[ndx + 1];
705 }
706 break;
707 case HASH_PUT:
708 if ((__delpair(hashp, rbufp, ndx)) ||
709 (__addel(hashp, rbufp, key, val))) {
710 save_bufp->flags &= ~BUF_PIN;
711 return (ERROR);
712 }
713 break;
714 case HASH_DELETE:
715 if (__delpair(hashp, rbufp, ndx))
716 return (ERROR);
717 break;
718 default:
719 abort();
720 }
721 save_bufp->flags &= ~BUF_PIN;
722 return (SUCCESS);
723 }
724
725 static int
726 hash_seq(dbp, key, data, flag)
727 const DB *dbp;
728 DBT *key, *data;
729 u_int32_t flag;
730 {
731 register u_int32_t bucket;
732 register BUFHEAD *bufp;
733 HTAB *hashp;
734 u_int16_t *bp, ndx;
735
736 hashp = (HTAB *)dbp->internal;
737 if (flag && flag != R_FIRST && flag != R_NEXT) {
738 hashp->errno = errno = EINVAL;
739 return (ERROR);
740 }
741 #ifdef HASH_STATISTICS
742 hash_accesses++;
743 #endif
744 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
745 hashp->cbucket = 0;
746 hashp->cndx = 1;
747 hashp->cpage = NULL;
748 }
749
750 for (bp = NULL; !bp || !bp[0]; ) {
751 if (!(bufp = hashp->cpage)) {
752 for (bucket = hashp->cbucket;
753 bucket <= hashp->MAX_BUCKET;
754 bucket++, hashp->cndx = 1) {
755 bufp = __get_buf(hashp, bucket, NULL, 0);
756 if (!bufp)
757 return (ERROR);
758 hashp->cpage = bufp;
759 bp = (u_int16_t *)bufp->page;
760 if (bp[0])
761 break;
762 }
763 hashp->cbucket = bucket;
764 if (hashp->cbucket > hashp->MAX_BUCKET) {
765 hashp->cbucket = -1;
766 return (ABNORMAL);
767 }
768 } else
769 bp = (u_int16_t *)hashp->cpage->page;
770
771 #ifdef DEBUG
772 assert(bp);
773 assert(bufp);
774 #endif
775 while (bp[hashp->cndx + 1] == OVFLPAGE) {
776 bufp = hashp->cpage =
777 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
778 if (!bufp)
779 return (ERROR);
780 bp = (u_int16_t *)(bufp->page);
781 hashp->cndx = 1;
782 }
783 if (!bp[0]) {
784 hashp->cpage = NULL;
785 ++hashp->cbucket;
786 }
787 }
788 ndx = hashp->cndx;
789 if (bp[ndx + 1] < REAL_KEY) {
790 if (__big_keydata(hashp, bufp, key, data, 1))
791 return (ERROR);
792 } else {
793 key->data = (u_char *)hashp->cpage->page + bp[ndx];
794 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
795 data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
796 data->size = bp[ndx] - bp[ndx + 1];
797 ndx += 2;
798 if (ndx > bp[0]) {
799 hashp->cpage = NULL;
800 hashp->cbucket++;
801 hashp->cndx = 1;
802 } else
803 hashp->cndx = ndx;
804 }
805 return (SUCCESS);
806 }
807
808 /********************************* UTILITIES ************************/
809
810 /*
811 * Returns:
812 * 0 ==> OK
813 * -1 ==> Error
814 */
815 extern int
816 __expand_table(hashp)
817 HTAB *hashp;
818 {
819 u_int32_t old_bucket, new_bucket;
820 int dirsize, new_segnum, spare_ndx;
821
822 #ifdef HASH_STATISTICS
823 hash_expansions++;
824 #endif
825 new_bucket = ++hashp->MAX_BUCKET;
826 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
827
828 new_segnum = new_bucket >> hashp->SSHIFT;
829
830 /* Check if we need a new segment */
831 if (new_segnum >= hashp->nsegs) {
832 /* Check if we need to expand directory */
833 if (new_segnum >= hashp->DSIZE) {
834 /* Reallocate directory */
835 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
836 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
837 return (-1);
838 hashp->DSIZE = dirsize << 1;
839 }
840 if ((hashp->dir[new_segnum] =
841 (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
842 return (-1);
843 hashp->exsegs++;
844 hashp->nsegs++;
845 }
846 /*
847 * If the split point is increasing (MAX_BUCKET's log base 2
848 * * increases), we need to copy the current contents of the spare
849 * split bucket to the next bucket.
850 */
851 spare_ndx = __log2(hashp->MAX_BUCKET + 1);
852 if (spare_ndx > hashp->OVFL_POINT) {
853 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
854 hashp->OVFL_POINT = spare_ndx;
855 }
856
857 if (new_bucket > hashp->HIGH_MASK) {
858 /* Starting a new doubling */
859 hashp->LOW_MASK = hashp->HIGH_MASK;
860 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
861 }
862 /* Relocate records to the new bucket */
863 return (__split_page(hashp, old_bucket, new_bucket));
864 }
865
866 /*
867 * If realloc guarantees that the pointer is not destroyed if the realloc
868 * fails, then this routine can go away.
869 */
870 static void *
871 hash_realloc(p_ptr, oldsize, newsize)
872 SEGMENT **p_ptr;
873 int oldsize, newsize;
874 {
875 register void *p;
876
877 if (p = malloc(newsize)) {
878 memmove(p, *p_ptr, oldsize);
879 memset((char *)p + oldsize, 0, newsize - oldsize);
880 free(*p_ptr);
881 *p_ptr = p;
882 }
883 return (p);
884 }
885
886 extern u_int32_t
887 __call_hash(hashp, k, len)
888 HTAB *hashp;
889 char *k;
890 int len;
891 {
892 int n, bucket;
893
894 n = hashp->hash(k, len);
895 bucket = n & hashp->HIGH_MASK;
896 if (bucket > hashp->MAX_BUCKET)
897 bucket = bucket & hashp->LOW_MASK;
898 return (bucket);
899 }
900
901 /*
902 * Allocate segment table. On error, destroy the table and set errno.
903 *
904 * Returns 0 on success
905 */
906 static int
907 alloc_segs(hashp, nsegs)
908 HTAB *hashp;
909 int nsegs;
910 {
911 register int i;
912 register SEGMENT store;
913
914 int save_errno;
915
916 if ((hashp->dir =
917 (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
918 save_errno = errno;
919 (void)hdestroy(hashp);
920 errno = save_errno;
921 return (-1);
922 }
923 /* Allocate segments */
924 if ((store =
925 (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
926 save_errno = errno;
927 (void)hdestroy(hashp);
928 errno = save_errno;
929 return (-1);
930 }
931 for (i = 0; i < nsegs; i++, hashp->nsegs++)
932 hashp->dir[i] = &store[i << hashp->SSHIFT];
933 return (0);
934 }
935
936 #if BYTE_ORDER == LITTLE_ENDIAN
937 /*
938 * Hashp->hdr needs to be byteswapped.
939 */
940 static void
941 swap_header_copy(srcp, destp)
942 HASHHDR *srcp, *destp;
943 {
944 int i;
945
946 P_32_COPY(srcp->magic, destp->magic);
947 P_32_COPY(srcp->version, destp->version);
948 P_32_COPY(srcp->lorder, destp->lorder);
949 P_32_COPY(srcp->bsize, destp->bsize);
950 P_32_COPY(srcp->bshift, destp->bshift);
951 P_32_COPY(srcp->dsize, destp->dsize);
952 P_32_COPY(srcp->ssize, destp->ssize);
953 P_32_COPY(srcp->sshift, destp->sshift);
954 P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
955 P_32_COPY(srcp->last_freed, destp->last_freed);
956 P_32_COPY(srcp->max_bucket, destp->max_bucket);
957 P_32_COPY(srcp->high_mask, destp->high_mask);
958 P_32_COPY(srcp->low_mask, destp->low_mask);
959 P_32_COPY(srcp->ffactor, destp->ffactor);
960 P_32_COPY(srcp->nkeys, destp->nkeys);
961 P_32_COPY(srcp->hdrpages, destp->hdrpages);
962 P_32_COPY(srcp->h_charkey, destp->h_charkey);
963 for (i = 0; i < NCACHED; i++) {
964 P_32_COPY(srcp->spares[i], destp->spares[i]);
965 P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
966 }
967 }
968
969 static void
970 swap_header(hashp)
971 HTAB *hashp;
972 {
973 HASHHDR *hdrp;
974 int i;
975
976 hdrp = &hashp->hdr;
977
978 M_32_SWAP(hdrp->magic);
979 M_32_SWAP(hdrp->version);
980 M_32_SWAP(hdrp->lorder);
981 M_32_SWAP(hdrp->bsize);
982 M_32_SWAP(hdrp->bshift);
983 M_32_SWAP(hdrp->dsize);
984 M_32_SWAP(hdrp->ssize);
985 M_32_SWAP(hdrp->sshift);
986 M_32_SWAP(hdrp->ovfl_point);
987 M_32_SWAP(hdrp->last_freed);
988 M_32_SWAP(hdrp->max_bucket);
989 M_32_SWAP(hdrp->high_mask);
990 M_32_SWAP(hdrp->low_mask);
991 M_32_SWAP(hdrp->ffactor);
992 M_32_SWAP(hdrp->nkeys);
993 M_32_SWAP(hdrp->hdrpages);
994 M_32_SWAP(hdrp->h_charkey);
995 for (i = 0; i < NCACHED; i++) {
996 M_32_SWAP(hdrp->spares[i]);
997 M_16_SWAP(hdrp->bitmaps[i]);
998 }
999 }
1000 #endif
1001