2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. ***REMOVED*** - see
17 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
18 * 4. 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 defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid
[] = "@(#)hash_bigkey.c 8.3 (Berkeley) 5/31/94";
37 #endif /* LIBC_SCCS and not lint */
44 * Big key/data handling for the hashing package.
59 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2_VACPP)
60 #include <sys/param.h>
75 /* #include "extern.h" */
77 static int collect_key
__P((HTAB
*, BUFHEAD
*, int, DBT
*, int));
78 static int collect_data
__P((HTAB
*, BUFHEAD
*, int, int));
83 * You need to do an insert and the key/data pair is too big
90 __big_insert(HTAB
*hashp
, BUFHEAD
*bufp
, const DBT
*key
, const DBT
*val
)
93 uint key_size
, n
, val_size
;
94 uint16 space
, move_bytes
, off
;
95 char *cp
, *key_data
, *val_data
;
97 cp
= bufp
->page
; /* Character pointer of p. */
100 key_data
= (char *)key
->data
;
101 key_size
= key
->size
;
102 val_data
= (char *)val
->data
;
103 val_size
= val
->size
;
105 /* First move the Key */
106 for (space
= FREESPACE(p
) - BIGOVERHEAD
; key_size
;
107 space
= FREESPACE(p
) - BIGOVERHEAD
) {
108 move_bytes
= PR_MIN(space
, key_size
);
109 off
= OFFSET(p
) - move_bytes
;
110 memmove(cp
+ off
, key_data
, move_bytes
);
111 key_size
-= move_bytes
;
112 key_data
+= move_bytes
;
116 FREESPACE(p
) = off
- PAGE_META(n
);
119 bufp
= __add_ovflpage(hashp
, bufp
);
125 move_bytes
= PR_MIN(FREESPACE(p
), val_size
);
126 off
= OFFSET(p
) - move_bytes
;
128 memmove(cp
+ off
, val_data
, move_bytes
);
129 val_data
+= move_bytes
;
130 val_size
-= move_bytes
;
131 p
[n
- 2] = FULL_KEY_DATA
;
132 FREESPACE(p
) = FREESPACE(p
) - move_bytes
;
137 p
= (uint16
*)bufp
->page
;
139 bufp
->flags
|= BUF_MOD
;
142 /* Now move the data */
143 for (space
= FREESPACE(p
) - BIGOVERHEAD
; val_size
;
144 space
= FREESPACE(p
) - BIGOVERHEAD
) {
145 move_bytes
= PR_MIN(space
, val_size
);
147 * Here's the hack to make sure that if the data ends on the
148 * same page as the key ends, FREESPACE is at least one.
150 if (space
== val_size
&& val_size
== val
->size
)
152 off
= OFFSET(p
) - move_bytes
;
153 memmove(cp
+ off
, val_data
, move_bytes
);
154 val_size
-= move_bytes
;
155 val_data
+= move_bytes
;
159 FREESPACE(p
) = off
- PAGE_META(n
);
163 bufp
= __add_ovflpage(hashp
, bufp
);
169 p
[n
] = FULL_KEY_DATA
;
170 bufp
->flags
|= BUF_MOD
;
176 * Called when bufp's page contains a partial key (index should be 1)
178 * All pages in the big key/data pair except bufp are freed. We cannot
179 * free bufp because the page pointing to it is lost and we can't get rid
187 __big_delete(HTAB
*hashp
, BUFHEAD
*bufp
)
189 register BUFHEAD
*last_bfp
, *rbufp
;
195 bp
= (uint16
*)bufp
->page
;
199 while (!key_done
|| (bp
[2] != FULL_KEY_DATA
)) {
200 if (bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
)
204 * If there is freespace left on a FULL_KEY_DATA page, then
205 * the data is short and fits entirely on this page, and this
208 if (bp
[2] == FULL_KEY_DATA
&& FREESPACE(bp
))
210 pageno
= bp
[bp
[0] - 1];
211 rbufp
->flags
|= BUF_MOD
;
212 rbufp
= __get_buf(hashp
, pageno
, rbufp
, 0);
214 __free_ovflpage(hashp
, last_bfp
);
217 return (-1); /* Error. */
218 bp
= (uint16
*)rbufp
->page
;
222 * If we get here then rbufp points to the last page of the big
223 * key/data pair. Bufp points to the first one -- it should now be
224 * empty pointing to the next page after this pair. Can't free it
225 * because we don't have the page pointing to it.
228 /* This is information from the last page of the pair. */
232 /* Now, bp is the first page of the pair. */
233 bp
= (uint16
*)bufp
->page
;
235 /* There is an overflow page. */
238 bufp
->ovfl
= rbufp
->ovfl
;
240 /* This is the last page. */
244 FREESPACE(bp
) = hashp
->BSIZE
- PAGE_META(n
);
245 OFFSET(bp
) = hashp
->BSIZE
- 1;
247 bufp
->flags
|= BUF_MOD
;
249 __free_ovflpage(hashp
, rbufp
);
250 if (last_bfp
!= rbufp
)
251 __free_ovflpage(hashp
, last_bfp
);
259 * -1 = get next overflow page
260 * -2 means key not found and this is big key/data
264 __find_bigpair(HTAB
*hashp
, BUFHEAD
*bufp
, int ndx
, char *key
, int size
)
272 bp
= (uint16
*)bufp
->page
;
277 for (bytes
= hashp
->BSIZE
- bp
[ndx
];
278 bytes
<= size
&& bp
[ndx
+ 1] == PARTIAL_KEY
;
279 bytes
= hashp
->BSIZE
- bp
[ndx
]) {
280 if (memcmp(p
+ bp
[ndx
], kkey
, bytes
))
284 bufp
= __get_buf(hashp
, bp
[ndx
+ 2], bufp
, 0);
292 if (bytes
!= ksize
|| memcmp(p
+ bp
[ndx
], kkey
, bytes
)) {
293 #ifdef HASH_STATISTICS
302 * Given the buffer pointer of the first overflow page of a big pair,
303 * find the end of the big pair
305 * This will set bpp to the buffer header of the last page of the big pair.
306 * It will return the pageno of the overflow page following the last page
307 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
311 __find_last_page(HTAB
*hashp
, BUFHEAD
**bpp
)
318 bp
= (uint16
*)bufp
->page
;
323 * This is the last page if: the tag is FULL_KEY_DATA and
324 * either only 2 entries OVFLPAGE marker is explicit there
325 * is freespace on the page.
327 if (bp
[2] == FULL_KEY_DATA
&&
328 ((n
== 2) || (bp
[n
] == OVFLPAGE
) || (FREESPACE(bp
))))
331 /* LJM bound the size of n to reasonable limits
333 if(n
> hashp
->BSIZE
/sizeof(uint16
))
337 bufp
= __get_buf(hashp
, pageno
, bufp
, 0);
339 return (0); /* Need to indicate an error! */
340 bp
= (uint16
*)bufp
->page
;
351 * Return the data for the key/data pair that begins on this page at this
352 * index (index should always be 1).
363 uint16
*bp
, len
, off
, save_addr
;
367 bp
= (uint16
*)bufp
->page
;
368 while (bp
[ndx
+ 1] == PARTIAL_KEY
) {
369 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
372 bp
= (uint16
*)bufp
->page
;
376 if (bp
[ndx
+ 1] == FULL_KEY
) {
377 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
380 bp
= (uint16
*)bufp
->page
;
382 save_addr
= save_p
->addr
;
386 if (!FREESPACE(bp
)) {
388 * This is a hack. We can't distinguish between
389 * FULL_KEY_DATA that contains complete data or
390 * incomplete data, so we require that if the data
391 * is complete, there is at least 1 byte of free
397 save_addr
= bufp
->addr
;
398 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
401 bp
= (uint16
*)bufp
->page
;
403 /* The data is all on one page. */
406 val
->data
= (uint8
*)tp
+ off
;
407 val
->size
= bp
[1] - off
;
409 if (bp
[0] == 2) { /* No more buckets in
415 hashp
->cpage
= __get_buf(hashp
,
416 bp
[bp
[0] - 1], bufp
, 0);
421 hashp
->cpage
->page
)[0]) {
430 /* pin our saved buf so that we don't lose if
431 * we run out of buffers */
432 save_flags
= save_p
->flags
;
433 save_p
->flags
|= BUF_PIN
;
434 val
->size
= collect_data(hashp
, bufp
, (int)len
, set_current
);
435 save_p
->flags
= save_flags
;
436 if (val
->size
== (size_t)-1)
438 if (save_p
->addr
!= save_addr
) {
439 /* We are pretty short on buffers. */
440 errno
= EINVAL
; /* OUT OF BUFFERS */
443 memmove(hashp
->tmp_buf
, (save_p
->page
) + off
, len
);
444 val
->data
= (uint8
*)hashp
->tmp_buf
;
450 * Count how big the total datasize is by looping through the pages. Then
451 * allocate a buffer and copy the data in the second loop. NOTE: Our caller
452 * may already have a bp which it is holding onto. The caller is
453 * responsible for copying that bp into our temp buffer. 'len' is how much
454 * space to reserve for that buffer.
468 * save the input buf head because we need to walk the list twice.
469 * pin it to make sure it doesn't leave the buffer pool.
470 * This has the effect of growing the buffer pool if necessary.
473 save_flags
= save_bufp
->flags
;
474 save_bufp
->flags
|= BUF_PIN
;
476 /* read the length of the buffer */
477 for (totlen
= len
; bufp
; bufp
= __get_buf(hashp
, bp
[bp
[0]-1], bufp
, 0)) {
478 bp
= (uint16
*)bufp
->page
;
479 mylen
= hashp
->BSIZE
- bp
[1];
481 /* if mylen ever goes negative it means that the
482 * page is screwed up.
485 save_bufp
->flags
= save_flags
;
489 if (bp
[2] == FULL_KEY_DATA
) { /* End of Data */
495 save_bufp
->flags
= save_flags
;
499 /* allocate a temp buf */
501 free(hashp
->tmp_buf
);
502 if ((hashp
->tmp_buf
= (char *)malloc((size_t)totlen
)) == NULL
) {
503 save_bufp
->flags
= save_flags
;
507 /* copy the buffers back into temp buf */
508 for (bufp
= save_bufp
; bufp
;
509 bufp
= __get_buf(hashp
, bp
[bp
[0]-1], bufp
, 0)) {
510 bp
= (uint16
*)bufp
->page
;
511 mylen
= hashp
->BSIZE
- bp
[1];
512 memmove(&hashp
->tmp_buf
[len
], (bufp
->page
) + bp
[1], (size_t)mylen
);
514 if (bp
[2] == FULL_KEY_DATA
) {
519 /* 'clear' the pin flags */
520 save_bufp
->flags
= save_flags
;
522 /* update the database cursor */
525 if (bp
[0] == 2) { /* No more buckets in chain */
529 hashp
->cpage
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
532 else if (!((uint16
*)hashp
->cpage
->page
)[0]) {
542 * Fill in the key and data for this big pair.
551 key
->size
= collect_key(hashp
, bufp
, 0, val
, set
);
552 if (key
->size
== (size_t)-1)
554 key
->data
= (uint8
*)hashp
->tmp_key
;
559 * Count how big the total key size is by recursing through the pages. Then
560 * collect the data, allocate a buffer and copy the key as you recurse up.
573 uint16
*bp
, save_addr
;
577 mylen
= hashp
->BSIZE
- bp
[1];
579 save_addr
= bufp
->addr
;
580 totlen
= len
+ mylen
;
581 if (bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
) { /* End of Key. */
582 if (hashp
->tmp_key
!= NULL
)
583 free(hashp
->tmp_key
);
584 if ((hashp
->tmp_key
= (char *)malloc((size_t)totlen
)) == NULL
)
586 if (__big_return(hashp
, bufp
, 1, val
, set
))
589 xbp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
590 if (!xbp
|| ((totlen
=
591 collect_key(hashp
, xbp
, totlen
, val
, set
)) < 1))
594 if (bufp
->addr
!= save_addr
) {
595 errno
= EINVAL
; /* MIS -- OUT OF BUFFERS */
598 memmove(&hashp
->tmp_key
[len
], (bufp
->page
) + bp
[1], (size_t)mylen
);
610 BUFHEAD
*op
, /* Pointer to where to put keys that go in old bucket */
611 BUFHEAD
*np
, /* Pointer to new bucket page */
612 /* Pointer to first page containing the big key/data */
614 uint32 addr
, /* Address of big_keyp */
615 uint32 obucket
,/* Old Bucket */
618 register BUFHEAD
*tmpp
;
623 uint16 free_space
, n
, off
;
627 /* Now figure out where the big key/data goes */
628 if (__big_keydata(hashp
, big_keyp
, &key
, &val
, 0))
630 change
= (__call_hash(hashp
,(char*) key
.data
, key
.size
) != obucket
);
632 if ((ret
->next_addr
= __find_last_page(hashp
, &big_keyp
))) {
634 __get_buf(hashp
, ret
->next_addr
, big_keyp
, 0)))
639 /* Now make one of np/op point to the big key/data pair */
641 assert(np
->ovfl
== NULL
);
648 tmpp
->flags
|= BUF_MOD
;
650 (void)fprintf(stderr
,
651 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp
->addr
,
652 (tmpp
->ovfl
? tmpp
->ovfl
->addr
: 0), (bp
? bp
->addr
: 0));
654 tmpp
->ovfl
= bp
; /* one of op/np point to big_keyp */
655 tp
= (uint16
*)tmpp
->page
;
658 #if 0 /* this get's tripped on database corrupted error */
659 assert(FREESPACE(tp
) >= OVFLSIZE
);
661 if(FREESPACE(tp
) < OVFLSIZE
)
662 return(DATABASE_CORRUPTED_ERROR
);
666 free_space
= FREESPACE(tp
);
667 tp
[++n
] = (uint16
)addr
;
671 FREESPACE(tp
) = free_space
- OVFLSIZE
;
674 * Finally, set the new and old return values. BIG_KEYP contains a
675 * pointer to the last page of the big key_data pair. Make sure that
676 * big_keyp has no following page (2 elements) or create an empty
683 tp
= (uint16
*)big_keyp
->page
;
684 big_keyp
->flags
|= BUF_MOD
;
687 * There may be either one or two offsets on this page. If
688 * there is one, then the overflow page is linked on normally
689 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
690 * the second offset and needs to get stuffed in after the
691 * next overflow page is added.
694 free_space
= FREESPACE(tp
);
697 FREESPACE(tp
) = free_space
+ OVFLSIZE
;
699 tmpp
= __add_ovflpage(hashp
, big_keyp
);