xref: /original-bsd/lib/libc/db/hash/hash.c (revision 37071c60)
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.4 (Berkeley) 10/12/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, dflags)
71 	const char *file;
72 	int flags, mode, dflags;
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 	/*
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
93 	 * we can check accesses.
94 	 */
95 	hashp->flags = flags;
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;
103 	}
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) {
110 		if (!(hashp = init_hash(hashp, file, (HASHINFO *)info)))
111 			RETURN_ERROR(errno, error1);
112 	} else {
113 		/* Table already exists */
114 		if (info && info->hash)
115 			hashp->hash = info->hash;
116 		else
117 			hashp->hash = __default_hash;
118 
119 		hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
120 #if BYTE_ORDER == LITTLE_ENDIAN
121 		swap_header(hashp);
122 #endif
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);
130 #define	OLDHASHVERSION	1
131 		if (hashp->VERSION != HASHVERSION &&
132 		    hashp->VERSION != OLDHASHVERSION)
133 			RETURN_ERROR(EFTYPE, error1);
134 		if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
135 			RETURN_ERROR(EFTYPE, error1);
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) /
142 			 hashp->SGSIZE;
143 		hashp->nsegs = 0;
144 		if (alloc_segs(hashp, nsegs))
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 */
151 		bpages = (hashp->SPARES[hashp->OVFL_POINT] +
152 		    (hashp->BSIZE << BYTE_SHIFT) - 1) >>
153 		    (hashp->BSHIFT + BYTE_SHIFT);
154 
155 		hashp->nmaps = bpages;
156 		(void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_long *));
157 	}
158 
159 	/* Initialize Buffer Manager */
160 	if (info && info->cachesize)
161 		__buf_init(hashp, info->cachesize);
162 	else
163 		__buf_init(hashp, DEF_BUFSIZE);
164 
165 	hashp->new_file = new_table;
166 	hashp->save_file = file && (hashp->flags & O_RDWR);
167 	hashp->cbucket = -1;
168 	if (!(dbp = malloc(sizeof(DB)))) {
169 		save_errno = errno;
170 		hdestroy(hashp);
171 		errno = save_errno;
172 		return (NULL);
173 	}
174 	dbp->internal = hashp;
175 	dbp->close = hash_close;
176 	dbp->del = hash_delete;
177 	dbp->fd = hash_fd;
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;
183 
184 #ifdef DEBUG
185 	(void)fprintf(stderr,
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",
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,
196 	    "OVFL POINT	     ", hashp->OVFL_POINT,
197 	    "LAST FREED      ", hashp->LAST_FREED,
198 	    "HIGH MASK       ", hashp->HIGH_MASK,
199 	    "LOW  MASK       ", hashp->LOW_MASK,
200 	    "NSEGS           ", hashp->nsegs,
201 	    "NKEYS           ", hashp->NKEYS);
202 #endif
203 #ifdef HASH_STATISTICS
204 	hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
205 #endif
206 	return (dbp);
207 
208 error1:
209 	if (hashp != NULL)
210 		(void)close(hashp->fp);
211 
212 error0:
213 	free(hashp);
214 	errno = save_errno;
215 	return (NULL);
216 }
217 
218 static int
219 hash_close(dbp)
220 	DB *dbp;
221 {
222 	HTAB *hashp;
223 	int retval;
224 
225 	if (!dbp)
226 		return (ERROR);
227 
228 	hashp = (HTAB *)dbp->internal;
229 	retval = hdestroy(hashp);
230 	free(dbp);
231 	return (retval);
232 }
233 
234 static int
235 hash_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 
251 /************************** LOCAL CREATION ROUTINES **********************/
252 static HTAB *
253 init_hash(hashp, file, info)
254 	HTAB *hashp;
255 	const char *file;
256 	HASHINFO *info;
257 {
258 	struct stat statbuf;
259 	int nelem;
260 
261 	nelem = 1;
262 	hashp->NKEYS = 0;
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;
270 	hashp->hash = __default_hash;
271 	memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
272 	memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
273 
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 
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 			}
291 		}
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;
305 		}
306 	}
307 	/* init_htab should destroy the table and set errno if it fails */
308 	if (init_htab(hashp, nelem))
309 		return (NULL);
310 	else
311 		return (hashp);
312 }
313 /*
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  */
319 static int
320 init_htab(hashp, nelem)
321 	HTAB *hashp;
322 	int nelem;
323 {
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 	 */
332 	nelem = (nelem - 1) / hashp->FFACTOR + 1;
333 
334 	l2 = __log2(MAX(nelem, 2));
335 	nbuckets = 1 << l2;
336 
337 	hashp->SPARES[l2] = l2 + 1;
338 	hashp->SPARES[l2 + 1] = l2 + 1;
339 	hashp->OVFL_POINT = l2;
340 	hashp->LAST_FREED = 2;
341 
342 	/* First bitmap page is at: splitpoint l2 page offset 1 */
343 	if (__init_bitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
344 		return (-1);
345 
346 	hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
347 	hashp->HIGH_MASK = (nbuckets << 1) - 1;
348 	hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
349 	    hashp->BSHIFT) + 1;
350 
351 	nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
352 	nsegs = 1 << __log2(nsegs);
353 
354 	if (nsegs > hashp->DSIZE)
355 		hashp->DSIZE = nsegs;
356 	return (alloc_segs(hashp, nsegs));
357 }
358 
359 /********************** DESTROY/CLOSE ROUTINES ************************/
360 
361 /*
362  * Flushes any changes to the file if necessary and destroys the hashp
363  * structure, freeing all allocated space.
364  */
365 static int
366 hdestroy(hashp)
367 	HTAB *hashp;
368 {
369 	int i, save_errno;
370 
371 	save_errno = 0;
372 
373 #ifdef HASH_STATISTICS
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]);
386 #endif
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);
399 	}
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 
410 	if (save_errno) {
411 		errno = save_errno;
412 		return (ERROR);
413 	}
414 	return (SUCCESS);
415 }
416 /*
417  * Write modified pages to disk
418  *
419  * Returns:
420  *	 0 == OK
421  *	-1 ERROR
422  */
423 static int
424 hash_sync(dbp, flags)
425 	const DB *dbp;
426 	u_int flags;
427 {
428 	HTAB *hashp;
429 
430 	if (flags != 0) {
431 		errno = EINVAL;
432 		return (ERROR);
433 	}
434 
435 	if (!dbp)
436 		return (ERROR);
437 
438 	hashp = (HTAB *)dbp->internal;
439 	if (!hashp->save_file)
440 		return (0);
441 	if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
442 		return (ERROR);
443 	hashp->new_file = 0;
444 	return (0);
445 }
446 
447 /*
448  * Returns:
449  *	 0 == OK
450  *	-1 indicates that errno should be set
451  */
452 static int
453 flush_meta(hashp)
454 	HTAB *hashp;
455 {
456 	HASHHDR *whdrp;
457 #if BYTE_ORDER == LITTLE_ENDIAN
458 	HASHHDR whdr;
459 #endif
460 	int fp, i, wsize;
461 
462 	if (!hashp->save_file)
463 		return (0);
464 	hashp->MAGIC = HASHMAGIC;
465 	hashp->VERSION = HASHVERSION;
466 	hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
467 
468 	fp = hashp->fp;
469 	whdrp = &hashp->hdr;
470 #if BYTE_ORDER == LITTLE_ENDIAN
471 	whdrp = &whdr;
472 	swap_header_copy(&hashp->hdr, whdrp);
473 #endif
474 	if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
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);
482 		}
483 	for (i = 0; i < NCACHED; i++)
484 		if (hashp->mapp[i])
485 			if (__put_page(hashp, (char *)hashp->mapp[i],
486 				hashp->BITMAPS[i], 0, 1))
487 				return (-1);
488 	return (0);
489 }
490 
491 /*******************************SEARCH ROUTINES *****************************/
492 /*
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  */
500 static int
501 hash_get(dbp, key, data, flag)
502 	const DB *dbp;
503 	const DBT *key;
504 	DBT *data;
505 	u_int flag;
506 {
507 	HTAB *hashp;
508 
509 	hashp = (HTAB *)dbp->internal;
510 	if (flag) {
511 		hashp->errno = errno = EINVAL;
512 		return (ERROR);
513 	}
514 	return (hash_access(hashp, HASH_GET, (DBT *)key, data));
515 }
516 
517 static int
518 hash_put(dbp, key, data, flag)
519 	const DB *dbp;
520 	DBT *key;
521 	const DBT *data;
522 	u_int flag;
523 {
524 	HTAB *hashp;
525 
526 	hashp = (HTAB *)dbp->internal;
527 	if (flag && flag != R_NOOVERWRITE) {
528 		hashp->errno = errno = EINVAL;
529 		return (ERROR);
530 	}
531 	if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
532 		hashp->errno = errno = EPERM;
533 		return (ERROR);
534 	}
535 	return (hash_access(hashp, flag == R_NOOVERWRITE ?
536 	    HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
537 }
538 
539 static int
540 hash_delete(dbp, key, flag)
541 	const DB *dbp;
542 	const DBT *key;
543 	u_int flag;		/* Ignored */
544 {
545 	HTAB *hashp;
546 
547 	hashp = (HTAB *)dbp->internal;
548 	if (flag && flag != R_CURSOR) {
549 		hashp->errno = errno = EINVAL;
550 		return (ERROR);
551 	}
552 	if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
553 		hashp->errno = errno = EPERM;
554 		return (ERROR);
555 	}
556 	return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
557 }
558 
559 /*
560  * Assume that hashp has been set in wrapper routine.
561  */
562 static int
563 hash_access(hashp, action, key, val)
564 	HTAB *hashp;
565 	ACTION action;
566 	DBT *key, *val;
567 {
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;
574 
575 #ifdef HASH_STATISTICS
576 	hash_accesses++;
577 #endif
578 
579 	off = hashp->BSIZE;
580 	size = key->size;
581 	kp = (char *)key->data;
582 	rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
583 	if (!rbufp)
584 		return (ERROR);
585 	save_bufp = rbufp;
586 
587 	/* Pin the bucket chain */
588 	rbufp->flags |= BUF_PIN;
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 &&
593 			    memcmp(kp, rbufp->page + *bp, size) == 0)
594 				goto found;
595 			off = bp[1];
596 #ifdef HASH_STATISTICS
597 			hash_collisions++;
598 #endif
599 			bp += 2;
600 			ndx += 2;
601 		} else if (bp[1] == OVFLPAGE) {
602 			rbufp = __get_buf(hashp, *bp, rbufp, 0);
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) {
613 			if ((ndx =
614 			    __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
615 				goto found;
616 			if (ndx == -2) {
617 				bufp = rbufp;
618 				if (!(pageno =
619 				    __find_last_page(hashp, &bufp))) {
620 					ndx = 0;
621 					rbufp = bufp;
622 					break;	/* FOR */
623 				}
624 				rbufp = __get_buf(hashp, pageno, bufp, 0);
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 			}
638 		}
639 
640 	/* Not found */
641 	switch (action) {
642 	case HASH_PUT:
643 	case HASH_PUTNEW:
644 		if (__addel(hashp, rbufp, key, val)) {
645 			save_bufp->flags &= ~BUF_PIN;
646 			return (ERROR);
647 		} else {
648 			save_bufp->flags &= ~BUF_PIN;
649 			return (SUCCESS);
650 		}
651 	case HASH_GET:
652 	case HASH_DELETE:
653 	default:
654 		save_bufp->flags &= ~BUF_PIN;
655 		return (ABNORMAL);
656 	}
657 
658 found:
659 	switch (action) {
660 	case HASH_PUTNEW:
661 		save_bufp->flags &= ~BUF_PIN;
662 		return (ABNORMAL);
663 	case HASH_GET:
664 		bp = (u_short *)rbufp->page;
665 		if (bp[ndx + 1] < REAL_KEY) {
666 			if (__big_return(hashp, rbufp, ndx, val, 0))
667 				return (ERROR);
668 		} else {
669 			val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
670 			val->size = bp[ndx] - bp[ndx + 1];
671 		}
672 		break;
673 	case HASH_PUT:
674 		if ((__delpair(hashp, rbufp, ndx)) ||
675 		    (__addel(hashp, rbufp, key, val))) {
676 			save_bufp->flags &= ~BUF_PIN;
677 			return (ERROR);
678 		}
679 		break;
680 	case HASH_DELETE:
681 		if (__delpair(hashp, rbufp, ndx))
682 			return (ERROR);
683 		break;
684 	default:
685 		abort();
686 	}
687 	save_bufp->flags &= ~BUF_PIN;
688 	return (SUCCESS);
689 }
690 
691 static int
692 hash_seq(dbp, key, data, flag)
693 	const DB *dbp;
694 	DBT *key, *data;
695 	u_int flag;
696 {
697 	register u_int bucket;
698 	register BUFHEAD *bufp;
699 	HTAB *hashp;
700 	u_short *bp, ndx;
701 
702 	hashp = (HTAB *)dbp->internal;
703 	if (flag && flag != R_FIRST && flag != R_NEXT) {
704 		hashp->errno = errno = EINVAL;
705 		return (ERROR);
706 	}
707 #ifdef HASH_STATISTICS
708 	hash_accesses++;
709 #endif
710 	if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
711 		hashp->cbucket = 0;
712 		hashp->cndx = 1;
713 		hashp->cpage = NULL;
714 	}
715 
716 	for (bp = NULL; !bp || !bp[0]; ) {
717 		if (!(bufp = hashp->cpage)) {
718 			for (bucket = hashp->cbucket;
719 			    bucket <= hashp->MAX_BUCKET;
720 			    bucket++, hashp->cndx = 1) {
721 				bufp = __get_buf(hashp, bucket, NULL, 0);
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;
736 
737 #ifdef DEBUG
738 		assert(bp);
739 		assert(bufp);
740 #endif
741 		while (bp[hashp->cndx + 1] == OVFLPAGE) {
742 			bufp = hashp->cpage =
743 			    __get_buf(hashp, bp[hashp->cndx], bufp, 0);
744 			if (!bufp)
745 				return (ERROR);
746 			bp = (u_short *)(bufp->page);
747 			hashp->cndx = 1;
748 		}
749 		if (!bp[0]) {
750 			hashp->cpage = NULL;
751 			++hashp->cbucket;
752 		}
753 	}
754 	ndx = hashp->cndx;
755 	if (bp[ndx + 1] < REAL_KEY) {
756 		if (__big_keydata(hashp, bufp, key, data, 1))
757 			return (ERROR);
758 	} else {
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;
770 	}
771 	return (SUCCESS);
772 }
773 
774 /********************************* UTILITIES ************************/
775 
776 /*
777  * Returns:
778  *	 0 ==> OK
779  *	-1 ==> Error
780  */
781 extern int
782 __expand_table(hashp)
783 	HTAB *hashp;
784 {
785 	u_int old_bucket, new_bucket;
786 	int dirsize, new_segnum, spare_ndx;
787 
788 #ifdef HASH_STATISTICS
789 	hash_expansions++;
790 #endif
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 */
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;
805 		}
806 		if (!(hashp->dir[new_segnum] =
807 			calloc(hashp->SGSIZE, sizeof(SEGMENT))))
808 			return (-1);
809 		hashp->exsegs++;
810 		hashp->nsegs++;
811 	}
812 	/*
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 	 */
817 	spare_ndx = __log2(hashp->MAX_BUCKET + 1);
818 	if (spare_ndx > hashp->OVFL_POINT) {
819 		hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
820 		hashp->OVFL_POINT = spare_ndx;
821 	}
822 
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;
827 	}
828 	/* Relocate records to the new bucket */
829 	return (__split_page(hashp, old_bucket, new_bucket));
830 }
831 
832 /*
833  * If realloc guarantees that the pointer is not destroyed if the realloc
834  * fails, then this routine can go away.
835  */
836 static void *
837 hash_realloc(p_ptr, oldsize, newsize)
838 	SEGMENT **p_ptr;
839 	int oldsize, newsize;
840 {
841 	register void *p;
842 
843 	if (p = malloc(newsize)) {
844 		memmove(p, *p_ptr, oldsize);
845 		memset((char *)p + oldsize, 0, newsize - oldsize);
846 		free(*p_ptr);
847 		*p_ptr = p;
848 	}
849 	return (p);
850 }
851 
852 extern u_int
853 __call_hash(hashp, k, len)
854 	HTAB *hashp;
855 	char *k;
856 	int len;
857 {
858 	int n, bucket;
859 
860 	n = hashp->hash(k, len);
861 	bucket = n & hashp->HIGH_MASK;
862 	if (bucket > hashp->MAX_BUCKET)
863 		bucket = bucket & hashp->LOW_MASK;
864 	return (bucket);
865 }
866 
867 /*
868  * Allocate segment table.  On error, destroy the table and set errno.
869  *
870  * Returns 0 on success
871  */
872 static int
873 alloc_segs(hashp, nsegs)
874 	HTAB *hashp;
875 	int nsegs;
876 {
877 	register int i;
878 	register SEGMENT store;
879 
880 	int save_errno;
881 
882 	if (!(hashp->dir = calloc(hashp->DSIZE, sizeof(SEGMENT *)))) {
883 		save_errno = errno;
884 		(void)hdestroy(hashp);
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;
892 		(void)hdestroy(hashp);
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);
899 }
900 
901 #if BYTE_ORDER == LITTLE_ENDIAN
902 /*
903  * Hashp->hdr needs to be byteswapped.
904  */
905 static void
906 swap_header_copy(srcp, destp)
907 	HASHHDR *srcp, *destp;
908 {
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);
919 	BLSWAP_COPY(srcp->ovfl_point, destp->ovfl_point);
920 	BLSWAP_COPY(srcp->last_freed, destp->last_freed);
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 	}
932 }
933 
934 static void
935 swap_header(hashp)
936 	HTAB *hashp;
937 {
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);
951 	BLSWAP(hdrp->ovfl_point);
952 	BLSWAP(hdrp->last_freed);
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 	}
964 }
965 #endif
966