1 /* $NetBSD: hash_bigkey.c,v 1.22 2008/09/10 17:52:35 joerg Exp $ */
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: hash_bigkey.c,v 1.22 2008/09/10 17:52:35 joerg Exp $");
45 * Big key/data handling for the hashing package.
60 #include <sys/param.h>
73 static int collect_key(HTAB
*, BUFHEAD
*, int, DBT
*, int);
74 static int collect_data(HTAB
*, BUFHEAD
*, int, int);
79 * You need to do an insert and the key/data pair is too big
86 __big_insert(HTAB
*hashp
, BUFHEAD
*bufp
, const DBT
*key
, const DBT
*val
)
89 size_t key_size
, val_size
;
90 uint16_t space
, move_bytes
, off
;
91 char *cp
, *key_data
, *val_data
;
94 cp
= bufp
->page
; /* Character pointer of p. */
95 p
= (uint16_t *)(void *)cp
;
97 key_data
= (char *)key
->data
;
98 _DBFIT(key
->size
, int);
100 val_data
= (char *)val
->data
;
101 _DBFIT(val
->size
, int);
102 val_size
= val
->size
;
104 /* First move the Key */
106 temp
= FREESPACE(p
) - BIGOVERHEAD
;
107 _DBFIT(temp
, uint16_t);
108 space
= (uint16_t)temp
;
110 move_bytes
= MIN(space
, key_size
);
111 off
= OFFSET(p
) - move_bytes
;
112 memmove(cp
+ off
, key_data
, (size_t)move_bytes
);
113 key_size
-= move_bytes
;
114 key_data
+= move_bytes
;
118 temp
= off
- PAGE_META(n
);
119 _DBFIT(temp
, uint16_t);
120 FREESPACE(p
) = (uint16_t)temp
;
123 bufp
= __add_ovflpage(hashp
, bufp
);
128 space
= FREESPACE(p
);
130 move_bytes
= MIN(space
, val_size
);
132 * If the data would fit exactly in the
133 * remaining space, we must overflow it to the
134 * next page; otherwise the invariant that the
135 * data must end on a page with FREESPACE
136 * non-zero would fail.
138 if (space
== val_size
&& val_size
== val
->size
)
140 off
= OFFSET(p
) - move_bytes
;
141 memmove(cp
+ off
, val_data
, (size_t)move_bytes
);
142 val_data
+= move_bytes
;
143 val_size
-= move_bytes
;
145 p
[n
- 2] = FULL_KEY_DATA
;
146 FREESPACE(p
) = FREESPACE(p
) - move_bytes
;
153 p
= (uint16_t *)(void *)bufp
->page
;
155 bufp
->flags
|= BUF_MOD
;
156 temp
= FREESPACE(p
) - BIGOVERHEAD
;
157 _DBFIT(temp
, uint16_t);
158 space
= (uint16_t)temp
;
161 /* Now move the data */
162 temp
= FREESPACE(p
) - BIGOVERHEAD
;
163 _DBFIT(temp
, uint16_t);
164 space
= (uint16_t)temp
;
166 move_bytes
= MIN(space
, val_size
);
168 * Here's the hack to make sure that if the data ends on the
169 * same page as the key ends, FREESPACE is at least one.
171 if (space
== val_size
&& val_size
== val
->size
)
173 off
= OFFSET(p
) - move_bytes
;
174 memmove(cp
+ off
, val_data
, (size_t)move_bytes
);
175 val_size
-= move_bytes
;
176 val_data
+= move_bytes
;
180 temp
= off
- PAGE_META(n
);
181 _DBFIT(temp
, uint16_t);
182 FREESPACE(p
) = (uint16_t)temp
;
186 bufp
= __add_ovflpage(hashp
, bufp
);
190 p
= (uint16_t *)(void *)cp
;
192 p
[n
] = FULL_KEY_DATA
;
193 bufp
->flags
|= BUF_MOD
;
194 temp
= FREESPACE(p
) - BIGOVERHEAD
;
195 _DBFIT(temp
, uint16_t);
196 space
= (uint16_t)temp
;
202 * Called when bufp's page contains a partial key (index should be 1)
204 * All pages in the big key/data pair except bufp are freed. We cannot
205 * free bufp because the page pointing to it is lost and we can't get rid
213 __big_delete(HTAB
*hashp
, BUFHEAD
*bufp
)
215 BUFHEAD
*last_bfp
, *rbufp
;
216 uint16_t *bp
, pageno
;
222 bp
= (uint16_t *)(void *)bufp
->page
;
226 while (!key_done
|| (bp
[2] != FULL_KEY_DATA
)) {
227 if (bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
)
231 * If there is freespace left on a FULL_KEY_DATA page, then
232 * the data is short and fits entirely on this page, and this
235 if (bp
[2] == FULL_KEY_DATA
&& FREESPACE(bp
))
237 pageno
= bp
[bp
[0] - 1];
238 rbufp
->flags
|= BUF_MOD
;
239 rbufp
= __get_buf(hashp
, (uint32_t)pageno
, rbufp
, 0);
241 __free_ovflpage(hashp
, last_bfp
);
244 return (-1); /* Error. */
245 bp
= (uint16_t *)(void *)rbufp
->page
;
249 * If we get here then rbufp points to the last page of the big
250 * key/data pair. Bufp points to the first one -- it should now be
251 * empty pointing to the next page after this pair. Can't free it
252 * because we don't have the page pointing to it.
255 /* This is information from the last page of the pair. */
259 /* Now, bp is the first page of the pair. */
260 bp
= (uint16_t *)(void *)bufp
->page
;
262 /* There is an overflow page. */
265 bufp
->ovfl
= rbufp
->ovfl
;
267 /* This is the last page. */
271 temp
= hashp
->BSIZE
- PAGE_META(n
);
272 _DBFIT(temp
, uint16_t);
273 FREESPACE(bp
) = (uint16_t)temp
;
274 OFFSET(bp
) = hashp
->BSIZE
;
276 bufp
->flags
|= BUF_MOD
;
278 __free_ovflpage(hashp
, rbufp
);
279 if (last_bfp
&& last_bfp
!= rbufp
)
280 __free_ovflpage(hashp
, last_bfp
);
288 * -1 = get next overflow page
289 * -2 means key not found and this is big key/data
293 __find_bigpair(HTAB
*hashp
, BUFHEAD
*bufp
, int ndx
, char *key
, int size
)
301 bp
= (uint16_t *)(void *)bufp
->page
;
306 for (bytes
= hashp
->BSIZE
- bp
[ndx
];
307 bytes
<= size
&& bp
[ndx
+ 1] == PARTIAL_KEY
;
308 bytes
= hashp
->BSIZE
- bp
[ndx
]) {
309 if (memcmp(p
+ bp
[ndx
], kkey
, (size_t)bytes
))
313 bufp
= __get_buf(hashp
, (uint32_t)bp
[ndx
+ 2], bufp
, 0);
317 bp
= (uint16_t *)(void *)p
;
321 if (bytes
!= ksize
|| memcmp(p
+ bp
[ndx
], kkey
, (size_t)bytes
)) {
322 #ifdef HASH_STATISTICS
331 * Given the buffer pointer of the first overflow page of a big pair,
332 * find the end of the big pair
334 * This will set bpp to the buffer header of the last page of the big pair.
335 * It will return the pageno of the overflow page following the last page
336 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
340 __find_last_page(HTAB
*hashp
, BUFHEAD
**bpp
)
343 uint16_t *bp
, pageno
;
347 bp
= (uint16_t *)(void *)bufp
->page
;
352 * This is the last page if: the tag is FULL_KEY_DATA and
353 * either only 2 entries OVFLPAGE marker is explicit there
354 * is freespace on the page.
356 if (bp
[2] == FULL_KEY_DATA
&&
357 ((n
== 2) || (bp
[n
] == OVFLPAGE
) || (FREESPACE(bp
))))
361 bufp
= __get_buf(hashp
, (uint32_t)pageno
, bufp
, 0);
363 return (0); /* Need to indicate an error! */
364 bp
= (uint16_t *)(void *)bufp
->page
;
375 * Return the data for the key/data pair that begins on this page at this
376 * index (index should always be 1).
379 __big_return(HTAB
*hashp
, BUFHEAD
*bufp
, int ndx
, DBT
*val
, int set_current
)
382 uint16_t *bp
, len
, off
, save_addr
;
385 bp
= (uint16_t *)(void *)bufp
->page
;
386 while (bp
[ndx
+ 1] == PARTIAL_KEY
) {
387 bufp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
390 bp
= (uint16_t *)(void *)bufp
->page
;
394 if (bp
[ndx
+ 1] == FULL_KEY
) {
395 bufp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
398 bp
= (uint16_t *)(void *)bufp
->page
;
400 save_addr
= save_p
->addr
;
404 if (!FREESPACE(bp
)) {
406 * This is a hack. We can't distinguish between
407 * FULL_KEY_DATA that contains complete data or
408 * incomplete data, so we require that if the data
409 * is complete, there is at least 1 byte of free
415 save_addr
= bufp
->addr
;
416 bufp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
,
420 bp
= (uint16_t *)(void *)bufp
->page
;
422 /* The data is all on one page. */
423 tp
= (char *)(void *)bp
;
425 val
->data
= (uint8_t *)tp
+ off
;
426 val
->size
= bp
[1] - off
;
428 if (bp
[0] == 2) { /* No more buckets in
434 hashp
->cpage
= __get_buf(hashp
,
435 (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
439 if (!((uint16_t *)(void *)
440 hashp
->cpage
->page
)[0]) {
449 val
->size
= collect_data(hashp
, bufp
, (int)len
, set_current
);
450 if (val
->size
== (size_t)-1)
452 if (save_p
->addr
!= save_addr
) {
453 /* We are pretty short on buffers. */
454 errno
= EINVAL
; /* OUT OF BUFFERS */
457 memmove(hashp
->tmp_buf
, (save_p
->page
) + off
, (size_t)len
);
458 val
->data
= (uint8_t *)hashp
->tmp_buf
;
462 * Count how big the total datasize is by recursing through the pages. Then
463 * allocate a buffer and copy the data as you recurse up.
466 collect_data(HTAB
*hashp
, BUFHEAD
*bufp
, int len
, int set
)
475 bp
= (uint16_t *)(void *)p
;
476 mylen
= hashp
->BSIZE
- bp
[1];
477 save_addr
= bufp
->addr
;
479 if (bp
[2] == FULL_KEY_DATA
) { /* End of Data */
480 totlen
= len
+ mylen
;
482 free(hashp
->tmp_buf
);
483 if ((hashp
->tmp_buf
= calloc(1, (size_t)totlen
)) == NULL
)
487 if (bp
[0] == 2) { /* No more buckets in chain */
492 __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1],
496 else if (!((uint16_t *)(void *)hashp
->cpage
->page
)[0]) {
503 xbp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
504 if (!xbp
|| ((totlen
=
505 collect_data(hashp
, xbp
, len
+ mylen
, set
)) < 1))
508 if (bufp
->addr
!= save_addr
) {
509 errno
= EINVAL
; /* Out of buffers. */
512 memmove(&hashp
->tmp_buf
[len
], (bufp
->page
) + bp
[1], (size_t)mylen
);
517 * Fill in the key and data for this big pair.
520 __big_keydata(HTAB
*hashp
, BUFHEAD
*bufp
, DBT
*key
, DBT
*val
, int set
)
522 key
->size
= collect_key(hashp
, bufp
, 0, val
, set
);
523 if (key
->size
== (size_t)-1)
525 key
->data
= (uint8_t *)hashp
->tmp_key
;
530 * Count how big the total key size is by recursing through the pages. Then
531 * collect the data, allocate a buffer and copy the key as you recurse up.
534 collect_key(HTAB
*hashp
, BUFHEAD
*bufp
, int len
, DBT
*val
, int set
)
539 uint16_t *bp
, save_addr
;
542 bp
= (uint16_t *)(void *)p
;
543 mylen
= hashp
->BSIZE
- bp
[1];
545 save_addr
= bufp
->addr
;
546 totlen
= len
+ mylen
;
547 if (bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
) { /* End of Key. */
548 if (hashp
->tmp_key
!= NULL
)
549 free(hashp
->tmp_key
);
550 if ((hashp
->tmp_key
= calloc(1, (size_t)totlen
)) == NULL
)
552 if (__big_return(hashp
, bufp
, 1, val
, set
))
555 xbp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
556 if (!xbp
|| ((totlen
=
557 collect_key(hashp
, xbp
, totlen
, val
, set
)) < 1))
560 if (bufp
->addr
!= save_addr
) {
561 errno
= EINVAL
; /* MIS -- OUT OF BUFFERS */
564 memmove(&hashp
->tmp_key
[len
], (bufp
->page
) + bp
[1], (size_t)mylen
);
576 BUFHEAD
*op
, /* Pointer to where to put keys that go in old bucket */
577 BUFHEAD
*np
, /* Pointer to new bucket page */
578 /* Pointer to first page containing the big key/data */
580 int addr
, /* Address of big_keyp */
581 uint32_t obucket
,/* Old Bucket */
590 uint16_t free_space
, n
, off
;
595 /* Now figure out where the big key/data goes */
596 if (__big_keydata(hashp
, big_keyp
, &key
, &val
, 0))
598 change
= (__call_hash(hashp
, key
.data
, (int)key
.size
) != obucket
);
600 if ((ret
->next_addr
= __find_last_page(hashp
, &big_keyp
)) != 0) {
602 __get_buf(hashp
, (uint32_t)ret
->next_addr
, big_keyp
, 0)))
607 /* Now make one of np/op point to the big key/data pair */
608 _DIAGASSERT(np
->ovfl
== NULL
);
614 tmpp
->flags
|= BUF_MOD
;
616 (void)fprintf(stderr
,
617 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp
->addr
,
618 (tmpp
->ovfl
? tmpp
->ovfl
->addr
: 0), (bp
? bp
->addr
: 0));
620 tmpp
->ovfl
= bp
; /* one of op/np point to big_keyp */
621 tp
= (uint16_t *)(void *)tmpp
->page
;
622 _DIAGASSERT(FREESPACE(tp
) >= OVFLSIZE
);
625 free_space
= FREESPACE(tp
);
626 tp
[++n
] = (uint16_t)addr
;
630 temp
= free_space
- OVFLSIZE
;
631 _DBFIT(temp
, uint16_t);
632 FREESPACE(tp
) = (uint16_t)temp
;
635 * Finally, set the new and old return values. BIG_KEYP contains a
636 * pointer to the last page of the big key_data pair. Make sure that
637 * big_keyp has no following page (2 elements) or create an empty
644 tp
= (uint16_t *)(void *)big_keyp
->page
;
645 big_keyp
->flags
|= BUF_MOD
;
648 * There may be either one or two offsets on this page. If
649 * there is one, then the overflow page is linked on normally
650 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
651 * the second offset and needs to get stuffed in after the
652 * next overflow page is added.
655 free_space
= FREESPACE(tp
);
658 temp
= free_space
+ OVFLSIZE
;
659 _DBFIT(temp
, uint16_t);
660 FREESPACE(tp
) = (uint16_t)temp
;
662 tmpp
= __add_ovflpage(hashp
, big_keyp
);