]>
git.saurik.com Git - apple/libc.git/blob - db/hash/hash_bigkey.c
fa8e8ddecd2b46166071c17f5a6c17bfc805a4c3
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * Big key/data handling for the hashing package.
80 #include <sys/param.h>
96 static int collect_key
__P((HTAB
*, BUFHEAD
*, int, DBT
*, int));
97 static int collect_data
__P((HTAB
*, BUFHEAD
*, int, int));
102 * You need to do an insert and the key/data pair is too big
109 __big_insert(hashp
, bufp
, key
, val
)
112 const DBT
*key
, *val
;
115 int key_size
, n
, val_size
;
116 u_short space
, move_bytes
, off
;
117 char *cp
, *key_data
, *val_data
;
119 cp
= bufp
->page
; /* Character pointer of p. */
122 key_data
= (char *)key
->data
;
123 key_size
= key
->size
;
124 val_data
= (char *)val
->data
;
125 val_size
= val
->size
;
127 /* First move the Key */
128 for (space
= FREESPACE(p
) - BIGOVERHEAD
; key_size
;
129 space
= FREESPACE(p
) - BIGOVERHEAD
) {
130 move_bytes
= MIN(space
, key_size
);
131 off
= OFFSET(p
) - move_bytes
;
132 memmove(cp
+ off
, key_data
, move_bytes
);
133 key_size
-= move_bytes
;
134 key_data
+= move_bytes
;
138 FREESPACE(p
) = off
- PAGE_META(n
);
141 bufp
= __add_ovflpage(hashp
, bufp
);
147 move_bytes
= MIN(FREESPACE(p
), val_size
);
148 off
= OFFSET(p
) - move_bytes
;
150 memmove(cp
+ off
, val_data
, move_bytes
);
151 val_data
+= move_bytes
;
152 val_size
-= move_bytes
;
153 p
[n
- 2] = FULL_KEY_DATA
;
154 FREESPACE(p
) = FREESPACE(p
) - move_bytes
;
158 p
= (u_short
*)bufp
->page
;
160 bufp
->flags
|= BUF_MOD
;
163 /* Now move the data */
164 for (space
= FREESPACE(p
) - BIGOVERHEAD
; val_size
;
165 space
= FREESPACE(p
) - BIGOVERHEAD
) {
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
, move_bytes
);
175 val_size
-= move_bytes
;
176 val_data
+= move_bytes
;
180 FREESPACE(p
) = off
- PAGE_META(n
);
184 bufp
= __add_ovflpage(hashp
, bufp
);
190 p
[n
] = FULL_KEY_DATA
;
191 bufp
->flags
|= BUF_MOD
;
197 * Called when bufp's page contains a partial key (index should be 1)
199 * All pages in the big key/data pair except bufp are freed. We cannot
200 * free bufp because the page pointing to it is lost and we can't get rid
208 __big_delete(hashp
, bufp
)
212 register BUFHEAD
*last_bfp
, *rbufp
;
218 bp
= (u_short
*)bufp
->page
;
222 while (!key_done
|| (bp
[2] != FULL_KEY_DATA
)) {
223 if (bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
)
227 * If there is freespace left on a FULL_KEY_DATA page, then
228 * the data is short and fits entirely on this page, and this
231 if (bp
[2] == FULL_KEY_DATA
&& FREESPACE(bp
))
233 pageno
= bp
[bp
[0] - 1];
234 rbufp
->flags
|= BUF_MOD
;
235 rbufp
= __get_buf(hashp
, pageno
, rbufp
, 0);
237 __free_ovflpage(hashp
, last_bfp
);
240 return (-1); /* Error. */
241 bp
= (u_short
*)rbufp
->page
;
245 * If we get here then rbufp points to the last page of the big
246 * key/data pair. Bufp points to the first one -- it should now be
247 * empty pointing to the next page after this pair. Can't free it
248 * because we don't have the page pointing to it.
251 /* This is information from the last page of the pair. */
255 /* Now, bp is the first page of the pair. */
256 bp
= (u_short
*)bufp
->page
;
258 /* There is an overflow page. */
261 bufp
->ovfl
= rbufp
->ovfl
;
263 /* This is the last page. */
267 FREESPACE(bp
) = hashp
->BSIZE
- PAGE_META(n
);
268 OFFSET(bp
) = hashp
->BSIZE
- 1;
270 bufp
->flags
|= BUF_MOD
;
272 __free_ovflpage(hashp
, rbufp
);
273 if (last_bfp
!= rbufp
)
274 __free_ovflpage(hashp
, last_bfp
);
282 * -1 = get next overflow page
283 * -2 means key not found and this is big key/data
287 __find_bigpair(hashp
, bufp
, ndx
, key
, size
)
294 register u_short
*bp
;
300 bp
= (u_short
*)bufp
->page
;
305 for (bytes
= hashp
->BSIZE
- bp
[ndx
];
306 bytes
<= size
&& bp
[ndx
+ 1] == PARTIAL_KEY
;
307 bytes
= hashp
->BSIZE
- bp
[ndx
]) {
308 if (memcmp(p
+ bp
[ndx
], kkey
, bytes
))
312 bufp
= __get_buf(hashp
, bp
[ndx
+ 2], bufp
, 0);
320 if (bytes
!= ksize
|| memcmp(p
+ bp
[ndx
], kkey
, bytes
)) {
321 #ifdef HASH_STATISTICS
330 * Given the buffer pointer of the first overflow page of a big pair,
331 * find the end of the big pair
333 * This will set bpp to the buffer header of the last page of the big pair.
334 * It will return the pageno of the overflow page following the last page
335 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
339 __find_last_page(hashp
, bpp
)
348 bp
= (u_short
*)bufp
->page
;
353 * This is the last page if: the tag is FULL_KEY_DATA and
354 * either only 2 entries OVFLPAGE marker is explicit there
355 * is freespace on the page.
357 if (bp
[2] == FULL_KEY_DATA
&&
358 ((n
== 2) || (bp
[n
] == OVFLPAGE
) || (FREESPACE(bp
))))
362 bufp
= __get_buf(hashp
, pageno
, bufp
, 0);
364 return (0); /* Need to indicate an error! */
365 bp
= (u_short
*)bufp
->page
;
376 * Return the data for the key/data pair that begins on this page at this
377 * index (index should always be 1).
380 __big_return(hashp
, bufp
, ndx
, val
, set_current
)
388 u_short
*bp
, len
, off
, save_addr
;
391 bp
= (u_short
*)bufp
->page
;
392 while (bp
[ndx
+ 1] == PARTIAL_KEY
) {
393 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
396 bp
= (u_short
*)bufp
->page
;
400 if (bp
[ndx
+ 1] == FULL_KEY
) {
401 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
404 bp
= (u_short
*)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
, bp
[bp
[0] - 1], bufp
, 0);
425 bp
= (u_short
*)bufp
->page
;
427 /* The data is all on one page. */
430 val
->data
= (u_char
*)tp
+ off
;
431 val
->size
= bp
[1] - off
;
433 if (bp
[0] == 2) { /* No more buckets in
439 hashp
->cpage
= __get_buf(hashp
,
440 bp
[bp
[0] - 1], bufp
, 0);
445 hashp
->cpage
->page
)[0]) {
454 val
->size
= collect_data(hashp
, bufp
, (int)len
, set_current
);
457 if (save_p
->addr
!= save_addr
) {
458 /* We are pretty short on buffers. */
459 errno
= EINVAL
; /* OUT OF BUFFERS */
462 memmove(hashp
->tmp_buf
, (save_p
->page
) + off
, len
);
463 val
->data
= (u_char
*)hashp
->tmp_buf
;
467 * Count how big the total datasize is by recursing through the pages. Then
468 * allocate a buffer and copy the data as you recurse up.
471 collect_data(hashp
, bufp
, len
, set
)
476 register u_short
*bp
;
484 mylen
= hashp
->BSIZE
- bp
[1];
485 save_addr
= bufp
->addr
;
487 if (bp
[2] == FULL_KEY_DATA
) { /* End of Data */
488 totlen
= len
+ mylen
;
490 free(hashp
->tmp_buf
);
491 if ((hashp
->tmp_buf
= (char *)malloc(totlen
)) == NULL
)
495 if (bp
[0] == 2) { /* No more buckets in chain */
500 __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
503 else if (!((u_short
*)hashp
->cpage
->page
)[0]) {
510 xbp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
511 if (!xbp
|| ((totlen
=
512 collect_data(hashp
, xbp
, len
+ mylen
, set
)) < 1))
515 if (bufp
->addr
!= save_addr
) {
516 errno
= EINVAL
; /* Out of buffers. */
519 memmove(&hashp
->tmp_buf
[len
], (bufp
->page
) + bp
[1], mylen
);
524 * Fill in the key and data for this big pair.
527 __big_keydata(hashp
, bufp
, key
, val
, set
)
533 key
->size
= collect_key(hashp
, bufp
, 0, val
, set
);
536 key
->data
= (u_char
*)hashp
->tmp_key
;
541 * Count how big the total key size is by recursing through the pages. Then
542 * collect the data, allocate a buffer and copy the key as you recurse up.
545 collect_key(hashp
, bufp
, len
, val
, set
)
555 u_short
*bp
, save_addr
;
559 mylen
= hashp
->BSIZE
- bp
[1];
561 save_addr
= bufp
->addr
;
562 totlen
= len
+ mylen
;
563 if (bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
) { /* End of Key. */
564 if (hashp
->tmp_key
!= NULL
)
565 free(hashp
->tmp_key
);
566 if ((hashp
->tmp_key
= (char *)malloc(totlen
)) == NULL
)
568 if (__big_return(hashp
, bufp
, 1, val
, set
))
571 xbp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
572 if (!xbp
|| ((totlen
=
573 collect_key(hashp
, xbp
, totlen
, val
, set
)) < 1))
576 if (bufp
->addr
!= save_addr
) {
577 errno
= EINVAL
; /* MIS -- OUT OF BUFFERS */
580 memmove(&hashp
->tmp_key
[len
], (bufp
->page
) + bp
[1], mylen
);
590 __big_split(hashp
, op
, np
, big_keyp
, addr
, obucket
, ret
)
592 BUFHEAD
*op
; /* Pointer to where to put keys that go in old bucket */
593 BUFHEAD
*np
; /* Pointer to new bucket page */
594 /* Pointer to first page containing the big key/data */
596 int addr
; /* Address of big_keyp */
597 u_int obucket
;/* Old Bucket */
600 register BUFHEAD
*tmpp
;
601 register u_short
*tp
;
605 u_short free_space
, n
, off
;
609 /* Now figure out where the big key/data goes */
610 if (__big_keydata(hashp
, big_keyp
, &key
, &val
, 0))
612 change
= (__call_hash(hashp
, key
.data
, key
.size
) != obucket
);
614 if (ret
->next_addr
= __find_last_page(hashp
, &big_keyp
)) {
616 __get_buf(hashp
, ret
->next_addr
, big_keyp
, 0)))
621 /* Now make one of np/op point to the big key/data pair */
623 assert(np
->ovfl
== NULL
);
630 tmpp
->flags
|= BUF_MOD
;
632 (void)fprintf(stderr
,
633 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp
->addr
,
634 (tmpp
->ovfl
? tmpp
->ovfl
->addr
: 0), (bp
? bp
->addr
: 0));
636 tmpp
->ovfl
= bp
; /* one of op/np point to big_keyp */
637 tp
= (u_short
*)tmpp
->page
;
639 assert(FREESPACE(tp
) >= OVFLSIZE
);
643 free_space
= FREESPACE(tp
);
644 tp
[++n
] = (u_short
)addr
;
648 FREESPACE(tp
) = free_space
- OVFLSIZE
;
651 * Finally, set the new and old return values. BIG_KEYP contains a
652 * pointer to the last page of the big key_data pair. Make sure that
653 * big_keyp has no following page (2 elements) or create an empty
660 tp
= (u_short
*)big_keyp
->page
;
661 big_keyp
->flags
|= BUF_MOD
;
664 * There may be either one or two offsets on this page. If
665 * there is one, then the overflow page is linked on normally
666 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
667 * the second offset and needs to get stuffed in after the
668 * next overflow page is added.
671 free_space
= FREESPACE(tp
);
674 FREESPACE(tp
) = free_space
+ OVFLSIZE
;
676 tmpp
= __add_ovflpage(hashp
, big_keyp
);