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