1 /* $NetBSD: hash_bigkey.c,v 1.24 2012/03/13 21:13:32 christos 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.24 2012/03/13 21:13:32 christos 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 size_t kspace
= MIN(space
, key_size
);
111 _DBFIT(kspace
, uint16_t);
112 move_bytes
= (uint16_t)kspace
;
113 off
= OFFSET(p
) - move_bytes
;
114 memmove(cp
+ off
, key_data
, (size_t)move_bytes
);
115 key_size
-= move_bytes
;
116 key_data
+= move_bytes
;
120 temp
= off
- PAGE_META(n
);
121 _DBFIT(temp
, uint16_t);
122 FREESPACE(p
) = (uint16_t)temp
;
125 bufp
= __add_ovflpage(hashp
, bufp
);
130 space
= FREESPACE(p
);
132 size_t vspace
= MIN(space
, val_size
);
133 _DBFIT(vspace
, uint16_t);
134 move_bytes
= (uint16_t)vspace
;
136 * If the data would fit exactly in the
137 * remaining space, we must overflow it to the
138 * next page; otherwise the invariant that the
139 * data must end on a page with FREESPACE
140 * non-zero would fail.
142 if (space
== val_size
&& val_size
== val
->size
)
144 off
= OFFSET(p
) - move_bytes
;
145 memmove(cp
+ off
, val_data
, (size_t)move_bytes
);
146 val_data
+= move_bytes
;
147 val_size
-= move_bytes
;
149 p
[n
- 2] = FULL_KEY_DATA
;
150 FREESPACE(p
) = FREESPACE(p
) - move_bytes
;
157 p
= (uint16_t *)(void *)bufp
->page
;
159 bufp
->flags
|= BUF_MOD
;
160 temp
= FREESPACE(p
) - BIGOVERHEAD
;
161 _DBFIT(temp
, uint16_t);
162 space
= (uint16_t)temp
;
165 /* Now move the data */
166 temp
= FREESPACE(p
) - BIGOVERHEAD
;
167 _DBFIT(temp
, uint16_t);
168 space
= (uint16_t)temp
;
170 size_t vspace
= MIN(space
, val_size
);
171 _DBFIT(vspace
, uint16_t);
172 move_bytes
= (uint16_t)vspace
;
174 * Here's the hack to make sure that if the data ends on the
175 * same page as the key ends, FREESPACE is at least one.
177 if (space
== val_size
&& val_size
== val
->size
)
179 off
= OFFSET(p
) - move_bytes
;
180 memmove(cp
+ off
, val_data
, (size_t)move_bytes
);
181 val_size
-= move_bytes
;
182 val_data
+= move_bytes
;
186 temp
= off
- PAGE_META(n
);
187 _DBFIT(temp
, uint16_t);
188 FREESPACE(p
) = (uint16_t)temp
;
192 bufp
= __add_ovflpage(hashp
, bufp
);
196 p
= (uint16_t *)(void *)cp
;
198 p
[n
] = FULL_KEY_DATA
;
199 bufp
->flags
|= BUF_MOD
;
200 temp
= FREESPACE(p
) - BIGOVERHEAD
;
201 _DBFIT(temp
, uint16_t);
202 space
= (uint16_t)temp
;
208 * Called when bufp's page contains a partial key (index should be 1)
210 * All pages in the big key/data pair except bufp are freed. We cannot
211 * free bufp because the page pointing to it is lost and we can't get rid
219 __big_delete(HTAB
*hashp
, BUFHEAD
*bufp
)
221 BUFHEAD
*last_bfp
, *rbufp
;
222 uint16_t *bp
, pageno
;
228 bp
= (uint16_t *)(void *)bufp
->page
;
232 while (!key_done
|| (bp
[2] != FULL_KEY_DATA
)) {
233 if (bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
)
237 * If there is freespace left on a FULL_KEY_DATA page, then
238 * the data is short and fits entirely on this page, and this
241 if (bp
[2] == FULL_KEY_DATA
&& FREESPACE(bp
))
243 pageno
= bp
[bp
[0] - 1];
244 rbufp
->flags
|= BUF_MOD
;
245 rbufp
= __get_buf(hashp
, (uint32_t)pageno
, rbufp
, 0);
247 __free_ovflpage(hashp
, last_bfp
);
250 return (-1); /* Error. */
251 bp
= (uint16_t *)(void *)rbufp
->page
;
255 * If we get here then rbufp points to the last page of the big
256 * key/data pair. Bufp points to the first one -- it should now be
257 * empty pointing to the next page after this pair. Can't free it
258 * because we don't have the page pointing to it.
261 /* This is information from the last page of the pair. */
265 /* Now, bp is the first page of the pair. */
266 bp
= (uint16_t *)(void *)bufp
->page
;
268 /* There is an overflow page. */
271 bufp
->ovfl
= rbufp
->ovfl
;
273 /* This is the last page. */
277 temp
= hashp
->BSIZE
- PAGE_META(n
);
278 _DBFIT(temp
, uint16_t);
279 FREESPACE(bp
) = (uint16_t)temp
;
280 OFFSET(bp
) = hashp
->BSIZE
;
282 bufp
->flags
|= BUF_MOD
;
284 __free_ovflpage(hashp
, rbufp
);
285 if (last_bfp
&& last_bfp
!= rbufp
)
286 __free_ovflpage(hashp
, last_bfp
);
294 * -1 = get next overflow page
295 * -2 means key not found and this is big key/data
299 __find_bigpair(HTAB
*hashp
, BUFHEAD
*bufp
, int ndx
, char *key
, int size
)
307 bp
= (uint16_t *)(void *)bufp
->page
;
312 for (bytes
= hashp
->BSIZE
- bp
[ndx
];
313 bytes
<= size
&& bp
[ndx
+ 1] == PARTIAL_KEY
;
314 bytes
= hashp
->BSIZE
- bp
[ndx
]) {
315 if (memcmp(p
+ bp
[ndx
], kkey
, (size_t)bytes
))
319 bufp
= __get_buf(hashp
, (uint32_t)bp
[ndx
+ 2], bufp
, 0);
323 bp
= (uint16_t *)(void *)p
;
327 if (bytes
!= ksize
|| memcmp(p
+ bp
[ndx
], kkey
, (size_t)bytes
)) {
328 #ifdef HASH_STATISTICS
337 * Given the buffer pointer of the first overflow page of a big pair,
338 * find the end of the big pair
340 * This will set bpp to the buffer header of the last page of the big pair.
341 * It will return the pageno of the overflow page following the last page
342 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
346 __find_last_page(HTAB
*hashp
, BUFHEAD
**bpp
)
349 uint16_t *bp
, pageno
;
353 bp
= (uint16_t *)(void *)bufp
->page
;
358 * This is the last page if: the tag is FULL_KEY_DATA and
359 * either only 2 entries OVFLPAGE marker is explicit there
360 * is freespace on the page.
362 if (bp
[2] == FULL_KEY_DATA
&&
363 ((n
== 2) || (bp
[n
] == OVFLPAGE
) || (FREESPACE(bp
))))
367 bufp
= __get_buf(hashp
, (uint32_t)pageno
, bufp
, 0);
369 return (0); /* Need to indicate an error! */
370 bp
= (uint16_t *)(void *)bufp
->page
;
381 * Return the data for the key/data pair that begins on this page at this
382 * index (index should always be 1).
385 __big_return(HTAB
*hashp
, BUFHEAD
*bufp
, int ndx
, DBT
*val
, int set_current
)
388 uint16_t *bp
, len
, off
, save_addr
;
391 bp
= (uint16_t *)(void *)bufp
->page
;
392 while (bp
[ndx
+ 1] == PARTIAL_KEY
) {
393 bufp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
396 bp
= (uint16_t *)(void *)bufp
->page
;
400 if (bp
[ndx
+ 1] == FULL_KEY
) {
401 bufp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
404 bp
= (uint16_t *)(void *)bufp
->page
;
406 save_addr
= save_p
->addr
;
410 if (!FREESPACE(bp
)) {
412 * This is a hack. We can't distinguish between
413 * FULL_KEY_DATA that contains complete data or
414 * incomplete data, so we require that if the data
415 * is complete, there is at least 1 byte of free
421 save_addr
= bufp
->addr
;
422 bufp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
,
426 bp
= (uint16_t *)(void *)bufp
->page
;
428 /* The data is all on one page. */
429 tp
= (char *)(void *)bp
;
431 val
->data
= (uint8_t *)tp
+ off
;
432 val
->size
= bp
[1] - off
;
434 if (bp
[0] == 2) { /* No more buckets in
440 hashp
->cpage
= __get_buf(hashp
,
441 (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
445 if (!((uint16_t *)(void *)
446 hashp
->cpage
->page
)[0]) {
455 val
->size
= collect_data(hashp
, bufp
, (int)len
, set_current
);
456 if (val
->size
== (size_t)-1)
458 if (save_p
->addr
!= save_addr
) {
459 /* We are pretty short on buffers. */
460 errno
= EINVAL
; /* OUT OF BUFFERS */
463 memmove(hashp
->tmp_buf
, (save_p
->page
) + off
, (size_t)len
);
464 val
->data
= (uint8_t *)hashp
->tmp_buf
;
468 * Count how big the total datasize is by recursing through the pages. Then
469 * allocate a buffer and copy the data as you recurse up.
472 collect_data(HTAB
*hashp
, BUFHEAD
*bufp
, int len
, int set
)
481 bp
= (uint16_t *)(void *)p
;
482 mylen
= hashp
->BSIZE
- bp
[1];
483 save_addr
= bufp
->addr
;
485 if (bp
[2] == FULL_KEY_DATA
) { /* End of Data */
486 totlen
= len
+ mylen
;
488 free(hashp
->tmp_buf
);
489 if ((hashp
->tmp_buf
= calloc(1, (size_t)totlen
)) == NULL
)
493 if (bp
[0] == 2) { /* No more buckets in chain */
498 __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1],
502 else if (!((uint16_t *)(void *)hashp
->cpage
->page
)[0]) {
509 xbp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
510 if (!xbp
|| ((totlen
=
511 collect_data(hashp
, xbp
, len
+ mylen
, set
)) < 1))
514 if (bufp
->addr
!= save_addr
) {
515 errno
= EINVAL
; /* Out of buffers. */
518 memmove(&hashp
->tmp_buf
[len
], (bufp
->page
) + bp
[1], (size_t)mylen
);
523 * Fill in the key and data for this big pair.
526 __big_keydata(HTAB
*hashp
, BUFHEAD
*bufp
, DBT
*key
, DBT
*val
, int set
)
528 key
->size
= collect_key(hashp
, bufp
, 0, val
, set
);
529 if (key
->size
== (size_t)-1)
531 key
->data
= (uint8_t *)hashp
->tmp_key
;
536 * Count how big the total key size is by recursing through the pages. Then
537 * collect the data, allocate a buffer and copy the key as you recurse up.
540 collect_key(HTAB
*hashp
, BUFHEAD
*bufp
, int len
, DBT
*val
, int set
)
545 uint16_t *bp
, save_addr
;
548 bp
= (uint16_t *)(void *)p
;
549 mylen
= hashp
->BSIZE
- bp
[1];
551 save_addr
= bufp
->addr
;
552 totlen
= len
+ mylen
;
553 if (bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
) { /* End of Key. */
554 if (hashp
->tmp_key
!= NULL
)
555 free(hashp
->tmp_key
);
556 if ((hashp
->tmp_key
= calloc(1, (size_t)totlen
)) == NULL
)
558 if (__big_return(hashp
, bufp
, 1, val
, set
))
561 xbp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
562 if (!xbp
|| ((totlen
=
563 collect_key(hashp
, xbp
, totlen
, val
, set
)) < 1))
566 if (bufp
->addr
!= save_addr
) {
567 errno
= EINVAL
; /* MIS -- OUT OF BUFFERS */
570 memmove(&hashp
->tmp_key
[len
], (bufp
->page
) + bp
[1], (size_t)mylen
);
582 BUFHEAD
*op
, /* Pointer to where to put keys that go in old bucket */
583 BUFHEAD
*np
, /* Pointer to new bucket page */
584 /* Pointer to first page containing the big key/data */
586 int addr
, /* Address of big_keyp */
587 uint32_t obucket
,/* Old Bucket */
596 uint16_t free_space
, n
, off
;
601 /* Now figure out where the big key/data goes */
602 if (__big_keydata(hashp
, big_keyp
, &key
, &val
, 0))
604 change
= (__call_hash(hashp
, key
.data
, (int)key
.size
) != obucket
);
606 if ((ret
->next_addr
= __find_last_page(hashp
, &big_keyp
)) != 0) {
608 __get_buf(hashp
, (uint32_t)ret
->next_addr
, big_keyp
, 0)))
613 /* Now make one of np/op point to the big key/data pair */
614 _DIAGASSERT(np
->ovfl
== NULL
);
620 tmpp
->flags
|= BUF_MOD
;
622 (void)fprintf(stderr
,
623 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp
->addr
,
624 (tmpp
->ovfl
? tmpp
->ovfl
->addr
: 0), (bp
? bp
->addr
: 0));
626 tmpp
->ovfl
= bp
; /* one of op/np point to big_keyp */
627 tp
= (uint16_t *)(void *)tmpp
->page
;
628 _DIAGASSERT(FREESPACE(tp
) >= OVFLSIZE
);
631 free_space
= FREESPACE(tp
);
632 tp
[++n
] = (uint16_t)addr
;
636 temp
= free_space
- OVFLSIZE
;
637 _DBFIT(temp
, uint16_t);
638 FREESPACE(tp
) = (uint16_t)temp
;
641 * Finally, set the new and old return values. BIG_KEYP contains a
642 * pointer to the last page of the big key_data pair. Make sure that
643 * big_keyp has no following page (2 elements) or create an empty
650 tp
= (uint16_t *)(void *)big_keyp
->page
;
651 big_keyp
->flags
|= BUF_MOD
;
654 * There may be either one or two offsets on this page. If
655 * there is one, then the overflow page is linked on normally
656 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
657 * the second offset and needs to get stuffed in after the
658 * next overflow page is added.
661 free_space
= FREESPACE(tp
);
664 temp
= free_space
+ OVFLSIZE
;
665 _DBFIT(temp
, uint16_t);
666 FREESPACE(tp
) = (uint16_t)temp
;
668 tmpp
= __add_ovflpage(hashp
, big_keyp
);