37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] =
"@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
57 #include <sys/types.h>
70 #include "../include/db.h"
75 static u_int32_t *fetch_bitmap __P((
HTAB *,
int));
76 static u_int32_t first_free __P((u_int32_t));
77 static int open_temp __P((
HTAB *));
78 static u_int16_t overflow_page __P((
HTAB *));
79 static void putpair __P((
char *,
const DBT *,
const DBT *));
80 static void squeeze_key __P((u_int16_t *,
const DBT *,
const DBT *));
84 #define PAGE_INIT(P) { \
85 ((u_int16_t *)(P))[0] = 0; \
86 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
87 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
100 register u_int16_t *bp, n, off;
107 off = OFFSET(bp) - key->size;
108 memmove(p + off, key->data, key->size);
113 memmove(p + off, val->data, val->size);
118 bp[n + 1] = off - ((n + 3) *
sizeof(u_int16_t));
128 __delpair(hashp, bufp, ndx)
133 register u_int16_t *bp, newoff;
137 bp = (u_int16_t *)bufp->page;
140 if (bp[ndx + 1] < REAL_KEY)
141 return (__big_delete(hashp, bufp));
143 newoff = bp[ndx - 1];
145 newoff = hashp->BSIZE;
146 pairlen = newoff - bp[ndx + 1];
148 if (ndx != (n - 1)) {
151 register char *src = bufp->page + (int)OFFSET(bp);
152 register char *dst = src + (int)pairlen;
153 memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
156 for (i = ndx + 2; i <= n; i += 2) {
157 if (bp[i + 1] == OVFLPAGE) {
159 bp[i - 1] = bp[i + 1];
161 bp[i - 2] = bp[i] + pairlen;
162 bp[i - 1] = bp[i + 1] + pairlen;
167 bp[n] = OFFSET(bp) + pairlen;
168 bp[n - 1] = bp[n + 1] + pairlen + 2 *
sizeof(u_int16_t);
172 bufp->flags |= BUF_MOD;
181 __split_page(hashp, obucket, nbucket)
183 u_int32_t obucket, nbucket;
185 register BUFHEAD *new_bufp, *old_bufp;
186 register u_int16_t *ino;
190 u_int16_t copyto, diff, off, moved;
193 copyto = (u_int16_t)hashp->BSIZE;
194 off = (u_int16_t)hashp->BSIZE;
195 old_bufp = __get_buf(hashp, obucket, NULL, 0);
196 if (old_bufp == NULL)
198 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
199 if (new_bufp == NULL)
202 old_bufp->flags |= (BUF_MOD | BUF_PIN);
203 new_bufp->flags |= (BUF_MOD | BUF_PIN);
205 ino = (u_int16_t *)(op = old_bufp->page);
210 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
211 if (ino[n + 1] < REAL_KEY) {
212 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
213 (
int)copyto, (
int)moved);
214 old_bufp->flags &= ~BUF_PIN;
215 new_bufp->flags &= ~BUF_PIN;
219 key.data = (u_char *)op + ino[n];
220 key.size = off - ino[n];
222 if (__call_hash(hashp, key.data, key.size) == obucket) {
226 copyto = ino[n + 1] + diff;
227 memmove(op + copyto, op + ino[n + 1],
229 ino[ndx] = copyto + ino[n] - ino[n + 1];
230 ino[ndx + 1] = copyto;
236 val.data = (u_char *)op + ino[n + 1];
237 val.size = ino[n] - ino[n + 1];
238 putpair(np, &key, &val);
247 FREESPACE(ino) = copyto -
sizeof(u_int16_t) * (ino[0] + 3);
248 OFFSET(ino) = copyto;
251 (void)fprintf(stderr,
"split %d/%d\n",
252 ((u_int16_t *)np)[0] / 2,
253 ((u_int16_t *)op)[0] / 2);
256 old_bufp->flags &= ~BUF_PIN;
257 new_bufp->flags &= ~BUF_PIN;
277 ugly_split(hashp, obucket, old_bufp, new_bufp, copyto, moved)
285 register u_int16_t *ino;
286 register u_int16_t *np;
287 register u_int16_t *op;
292 u_int16_t n, off, ov_addr, scopyto;
296 ino = (u_int16_t *)old_bufp->page;
297 np = (u_int16_t *)new_bufp->page;
298 op = (u_int16_t *)old_bufp->page;
300 scopyto = (u_int16_t)copyto;
304 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
305 if (__big_split(hashp, old_bufp,
306 new_bufp, bufp, bufp->addr, obucket, &ret))
311 op = (u_int16_t *)old_bufp->page;
315 np = (u_int16_t *)new_bufp->page;
319 cino = (
char *)bufp->page;
320 ino = (u_int16_t *)cino;
321 last_bfp = ret.nextp;
322 }
else if (ino[n + 1] == OVFLPAGE) {
328 ino[0] -= (moved + 2);
330 scopyto -
sizeof(u_int16_t) * (ino[0] + 3);
331 OFFSET(ino) = scopyto;
333 bufp = __get_buf(hashp, ov_addr, bufp, 0);
337 ino = (u_int16_t *)bufp->page;
339 scopyto = hashp->BSIZE;
343 __free_ovflpage(hashp, last_bfp);
348 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
350 key.data = (u_char *)cino + ino[n];
351 key.size = off - ino[n];
352 val.data = (u_char *)cino + ino[n + 1];
353 val.size = ino[n] - ino[n + 1];
356 if (__call_hash(hashp, key.data, key.size) == obucket) {
358 if (PAIRFITS(op, (&key), (&val)))
359 putpair((
char *)op, &key, &val);
362 __add_ovflpage(hashp, old_bufp);
365 op = (u_int16_t *)old_bufp->page;
366 putpair((
char *)op, &key, &val);
368 old_bufp->flags |= BUF_MOD;
371 if (PAIRFITS(np, (&key), (&val)))
372 putpair((
char *)np, &key, &val);
375 __add_ovflpage(hashp, new_bufp);
378 np = (u_int16_t *)new_bufp->page;
379 putpair((
char *)np, &key, &val);
381 new_bufp->flags |= BUF_MOD;
386 __free_ovflpage(hashp, last_bfp);
398 __addel(hashp, bufp, key, val)
401 const
DBT *key, *val;
403 register u_int16_t *bp, *sop;
406 bp = (u_int16_t *)bufp->page;
408 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
410 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
414 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
415 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
418 bp = (u_int16_t *)bufp->page;
421 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
422 squeeze_key(bp, key, val);
425 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
428 bp = (u_int16_t *)bufp->page;
431 if (PAIRFITS(bp, key, val))
432 putpair(bufp->page, key, val);
435 bufp = __add_ovflpage(hashp, bufp);
438 sop = (u_int16_t *)bufp->page;
440 if (PAIRFITS(sop, key, val))
441 putpair((
char *)sop, key, val);
443 if (__big_insert(hashp, bufp, key, val))
446 bufp->flags |= BUF_MOD;
453 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
454 return (__expand_table(hashp));
465 __add_ovflpage(hashp, bufp)
469 register u_int16_t *sp;
470 u_int16_t ndx, ovfl_num;
474 sp = (u_int16_t *)bufp->page;
477 if (hashp->FFACTOR == DEF_FFACTOR) {
478 hashp->FFACTOR = sp[0] >> 1;
479 if (hashp->FFACTOR < MIN_FFACTOR)
480 hashp->FFACTOR = MIN_FFACTOR;
482 bufp->flags |= BUF_MOD;
483 ovfl_num = overflow_page(hashp);
486 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
488 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
490 bufp->ovfl->flags |= BUF_MOD;
492 (void)fprintf(stderr,
"ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
493 tmp1, tmp2, bufp->ovfl->addr);
501 sp[ndx + 4] = OFFSET(sp);
502 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
503 sp[ndx + 1] = ovfl_num;
504 sp[ndx + 2] = OVFLPAGE;
506 #ifdef HASH_STATISTICS
518 __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap)
522 int is_bucket, is_disk, is_bitmap;
524 register int fd, page, size;
531 if ((fd == -1) || !is_disk) {
536 page = BUCKET_TO_PAGE(bucket);
538 page = OADDR_TO_PAGE(bucket);
539 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
540 ((rsize = read(fd, p, size)) == -1))
550 if (!is_bitmap && !bp[0]) {
553 if (hashp->LORDER != BYTE_ORDER) {
557 max = hashp->BSIZE >> 2;
558 for (i = 0; i < max; i++)
559 M_32_SWAP(((
int *)p)[i]);
563 for (i = 1; i <= max; i++)
578 __put_page(hashp, p, bucket, is_bucket, is_bitmap)
582 int is_bucket, is_bitmap;
584 register int fd, page, size;
588 if ((hashp->fp == -1) && open_temp(hashp))
592 if (hashp->LORDER != BYTE_ORDER) {
597 max = hashp->BSIZE >> 2;
598 for (i = 0; i < max; i++)
599 M_32_SWAP(((
int *)p)[i]);
601 max = ((u_int16_t *)p)[0] + 2;
602 for (i = 0; i <= max; i++)
603 M_16_SWAP(((u_int16_t *)p)[i]);
607 page = BUCKET_TO_PAGE(bucket);
609 page = OADDR_TO_PAGE(bucket);
610 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
611 ((wsize = write(fd, p, size)) == -1))
621 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
627 __ibitmap(hashp, pnum, nbits, ndx)
629 int pnum, nbits, ndx;
632 int clearbytes, clearints;
634 if ((ip = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
637 clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
638 clearbytes = clearints << INT_TO_BYTE;
639 (void)memset((
char *)ip, 0, clearbytes);
640 (void)memset(((
char *)ip) + clearbytes, 0xFF,
641 hashp->BSIZE - clearbytes);
642 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
644 hashp->BITMAPS[ndx] = (u_int16_t)pnum;
645 hashp->mapp[ndx] = ip;
653 register u_int32_t i, mask;
656 for (i = 0; i < BITS_PER_MAP; i++) {
668 register u_int32_t *freep = 0;
669 register int max_free, offset, splitnum;
671 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
675 splitnum = hashp->OVFL_POINT;
676 max_free = hashp->SPARES[splitnum];
678 free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
679 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
682 first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
683 for ( i = first_page; i <= free_page; i++ ) {
684 if (!(freep = (u_int32_t *)hashp->mapp[i]) &&
685 !(freep = fetch_bitmap(hashp, i)))
688 in_use_bits = free_bit;
690 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
692 if (i == first_page) {
693 bit = hashp->LAST_FREED &
694 ((hashp->BSIZE << BYTE_SHIFT) - 1);
695 j = bit / BITS_PER_MAP;
696 bit = bit & ~(BITS_PER_MAP - 1);
701 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
702 if (freep[j] != ALL_SET)
707 hashp->LAST_FREED = hashp->SPARES[splitnum];
708 hashp->SPARES[splitnum]++;
709 offset = hashp->SPARES[splitnum] -
710 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
712 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
713 if (offset > SPLITMASK) {
714 if (++splitnum >= NCACHED) {
715 if (write(STDERR_FILENO, OVMSG,
sizeof(OVMSG) - 1) < 0) {
719 hashp->OVFL_POINT = splitnum;
720 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
721 hashp->SPARES[splitnum-1]--;
726 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
728 if (free_page >= NCACHED) {
729 if (write(STDERR_FILENO, OVMSG,
sizeof(OVMSG) - 1) < 0) {
745 (
int)OADDR_OF(splitnum, offset), 1, free_page))
747 hashp->SPARES[splitnum]++;
752 if (offset > SPLITMASK) {
753 if (++splitnum >= NCACHED) {
754 if (write(STDERR_FILENO, OVMSG,
sizeof(OVMSG) - 1) < 0) {
758 hashp->OVFL_POINT = splitnum;
759 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
760 hashp->SPARES[splitnum-1]--;
769 SETBIT(freep, free_bit);
773 addr = OADDR_OF(splitnum, offset);
775 (void)fprintf(stderr,
"OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
776 addr, free_bit, free_page);
781 bit = bit + first_free(freep[j]);
792 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
793 if (bit >= hashp->LAST_FREED)
794 hashp->LAST_FREED = bit - 1;
797 for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
798 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
799 if (offset >= SPLITMASK)
801 addr = OADDR_OF(i, offset);
803 (void)fprintf(stderr,
"OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
815 __free_ovflpage(hashp, obufp)
819 register u_int16_t addr;
821 int bit_address, free_page, free_bit;
826 (void)fprintf(stderr,
"Freeing %d\n", addr);
828 ndx = (((u_int16_t)addr) >> SPLITSHIFT);
830 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
831 if (bit_address < hashp->LAST_FREED)
832 hashp->LAST_FREED = bit_address;
833 free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
834 free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
836 if (!(freep = hashp->mapp[free_page]))
837 freep = fetch_bitmap(hashp, free_page);
847 CLRBIT(freep, free_bit);
849 (void)fprintf(stderr,
"FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
850 obufp->addr, free_bit, free_page);
852 __reclaim_buf(hashp, obufp);
865 static char namestr[] =
"_hashXXXXXX";
868 (void)sigfillset(&set);
869 (void)sigprocmask(SIG_BLOCK, &set, &oset);
870 if ((hashp->fp = mkstemp(namestr)) != -1) {
871 (void)unlink(namestr);
872 (void)fcntl(hashp->fp, F_SETFD, 1);
874 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
875 return (hashp->fp != -1 ? 0 : -1);
883 squeeze_key(sp, key, val)
885 const
DBT *key, *val;
888 u_int16_t free_space, n, off, pageno;
892 free_space = FREESPACE(sp);
898 memmove(p + off, key->data, key->size);
901 memmove(p + off, val->data, val->size);
904 sp[n + 2] = OVFLPAGE;
905 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
910 fetch_bitmap(hashp, ndx)
914 if (ndx >= hashp->nmaps)
916 if ((hashp->mapp[ndx] = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
918 if (__get_page(hashp,
919 (
char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
920 free(hashp->mapp[ndx]);
923 return (hashp->mapp[ndx]);
934 (void)fprintf(stderr,
"%d ", addr);
935 bufp = __get_buf(hashp, addr, NULL, 0);
936 bp = (
short *)bufp->page;
937 while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
938 ((bp[0] > 2) && bp[2] < REAL_KEY))) {
939 oaddr = bp[bp[0] - 1];
940 (void)fprintf(stderr,
"%d ", (
int)oaddr);
941 bufp = __get_buf(hashp, (
int)oaddr, bufp, 0);
942 bp = (
short *)bufp->page;
944 (void)fprintf(stderr,
"\n");