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