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