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