]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_split.c
2e8a8e2f86580da66eed5d6d7e62014abad7139d
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1993, 1994
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char sccsid
[] = "@(#)bt_split.c 8.9 (Berkeley) 7/26/94";
61 #endif /* LIBC_SCCS and not lint */
62 #include <sys/cdefs.h>
64 #include <sys/types.h>
74 static int bt_broot(BTREE
*, PAGE
*, PAGE
*, PAGE
*);
75 static PAGE
*bt_page (BTREE
*, PAGE
*, PAGE
**, PAGE
**, indx_t
*, size_t);
76 static int bt_preserve(BTREE
*, pgno_t
);
77 static PAGE
*bt_psplit (BTREE
*, PAGE
*, PAGE
*, PAGE
*, indx_t
*, size_t);
78 static PAGE
*bt_root (BTREE
*, PAGE
*, PAGE
**, PAGE
**, indx_t
*, size_t);
79 static int bt_rroot(BTREE
*, PAGE
*, PAGE
*, PAGE
*);
80 static recno_t
rec_total(PAGE
*);
83 u_long bt_rootsplit
, bt_split
, bt_sortsplit
, bt_pfxsaved
;
87 * __BT_SPLIT -- Split the tree.
93 * data: data to insert
94 * flags: BIGKEY/BIGDATA flags
96 * skip: index to leave open
99 * RET_ERROR, RET_SUCCESS
102 __bt_split(t
, sp
, key
, data
, flags
, ilen
, argskip
)
105 const DBT
*key
, *data
;
114 PAGE
*h
, *l
, *r
, *lchild
, *rchild
;
117 u_int32_t n
, nbytes
, nksize
;
122 * Split the page into two pages, l and r. The split routines return
123 * a pointer to the page into which the key should be inserted and with
124 * skip set to the offset which should be used. Additionally, l and r
128 h
= sp
->pgno
== P_ROOT
?
129 bt_root(t
, sp
, &l
, &r
, &skip
, ilen
) :
130 bt_page(t
, sp
, &l
, &r
, &skip
, ilen
);
135 * Insert the new key/data pair into the leaf page. (Key inserts
136 * always cause a leaf page to split first.)
138 h
->linp
[skip
] = h
->upper
-= ilen
;
139 dest
= (char *)h
+ h
->upper
;
140 if (F_ISSET(t
, R_RECNO
))
141 WR_RLEAF(dest
, data
, flags
)
143 WR_BLEAF(dest
, key
, data
, flags
)
145 /* If the root page was split, make it look right. */
146 if (sp
->pgno
== P_ROOT
&&
147 (F_ISSET(t
, R_RECNO
) ?
148 bt_rroot(t
, sp
, l
, r
) : bt_broot(t
, sp
, l
, r
)) == RET_ERROR
)
152 * Now we walk the parent page stack -- a LIFO stack of the pages that
153 * were traversed when we searched for the page that split. Each stack
154 * entry is a page number and a page index offset. The offset is for
155 * the page traversed on the search. We've just split a page, so we
156 * have to insert a new key into the parent page.
158 * If the insert into the parent page causes it to split, may have to
159 * continue splitting all the way up the tree. We stop if the root
160 * splits or the page inserted into didn't have to split to hold the
161 * new key. Some algorithms replace the key for the old page as well
162 * as the new page. We don't, as there's no reason to believe that the
163 * first key on the old page is any better than the key we have, and,
164 * in the case of a key being placed at index 0 causing the split, the
165 * key is unavailable.
167 * There are a maximum of 5 pages pinned at any time. We keep the left
168 * and right pages pinned while working on the parent. The 5 are the
169 * two children, left parent and right parent (when the parent splits)
170 * and the root page or the overflow key page when calling bt_preserve.
171 * This code must make sure that all pins are released other than the
172 * root page or overflow page which is unlocked elsewhere.
174 while ((parent
= BT_POP(t
)) != NULL
) {
178 /* Get the parent page. */
179 if ((h
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
183 * The new key goes ONE AFTER the index, because the split
186 skip
= parent
->index
+ 1;
189 * Calculate the space needed on the parent page.
191 * Prefix trees: space hack when inserting into BINTERNAL
192 * pages. Retain only what's needed to distinguish between
193 * the new entry and the LAST entry on the page to its left.
194 * If the keys compare equal, retain the entire key. Note,
195 * we don't touch overflow keys, and the entire key must be
196 * retained for the next-to-left most key on the leftmost
197 * page of each level, or the search will fail. Applicable
198 * ONLY to internal pages that have leaf pages as children.
199 * Further reduction of the key between pairs of internal
200 * pages loses too much information.
202 switch (rchild
->flags
& P_TYPE
) {
204 bi
= GETBINTERNAL(rchild
, 0);
205 nbytes
= NBINTERNAL(bi
->ksize
);
208 bl
= GETBLEAF(rchild
, 0);
209 nbytes
= NBINTERNAL(bl
->ksize
);
210 if (t
->bt_pfx
&& !(bl
->flags
& P_BIGKEY
) &&
211 (h
->prevpg
!= P_INVALID
|| skip
> 1)) {
212 tbl
= GETBLEAF(lchild
, NEXTINDEX(lchild
) - 1);
217 nksize
= t
->bt_pfx(&a
, &b
);
218 n
= NBINTERNAL(nksize
);
221 bt_pfxsaved
+= nbytes
- n
;
237 /* Split the parent page if necessary or shift the indices. */
238 if (h
->upper
- h
->lower
< nbytes
+ sizeof(indx_t
)) {
240 h
= h
->pgno
== P_ROOT
?
241 bt_root(t
, h
, &l
, &r
, &skip
, nbytes
) :
242 bt_page(t
, h
, &l
, &r
, &skip
, nbytes
);
247 if (skip
< (nxtindex
= NEXTINDEX(h
)))
248 memmove(h
->linp
+ skip
+ 1, h
->linp
+ skip
,
249 (nxtindex
- skip
) * sizeof(indx_t
));
250 h
->lower
+= sizeof(indx_t
);
254 /* Insert the key into the parent page. */
255 switch (rchild
->flags
& P_TYPE
) {
257 h
->linp
[skip
] = h
->upper
-= nbytes
;
258 dest
= (char *)h
+ h
->linp
[skip
];
259 memmove(dest
, bi
, nbytes
);
260 ((BINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
263 h
->linp
[skip
] = h
->upper
-= nbytes
;
264 dest
= (char *)h
+ h
->linp
[skip
];
265 WR_BINTERNAL(dest
, nksize
? nksize
: bl
->ksize
,
266 rchild
->pgno
, bl
->flags
& P_BIGKEY
);
267 memmove(dest
, bl
->bytes
, nksize
? nksize
: bl
->ksize
);
268 if (bl
->flags
& P_BIGKEY
&&
269 bt_preserve(t
, *(pgno_t
*)bl
->bytes
) == RET_ERROR
)
274 * Update the left page count. If split
275 * added at index 0, fix the correct page.
278 dest
= (char *)h
+ h
->linp
[skip
- 1];
280 dest
= (char *)l
+ l
->linp
[NEXTINDEX(l
) - 1];
281 ((RINTERNAL
*)dest
)->nrecs
= rec_total(lchild
);
282 ((RINTERNAL
*)dest
)->pgno
= lchild
->pgno
;
284 /* Update the right page count. */
285 h
->linp
[skip
] = h
->upper
-= nbytes
;
286 dest
= (char *)h
+ h
->linp
[skip
];
287 ((RINTERNAL
*)dest
)->nrecs
= rec_total(rchild
);
288 ((RINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
292 * Update the left page count. If split
293 * added at index 0, fix the correct page.
296 dest
= (char *)h
+ h
->linp
[skip
- 1];
298 dest
= (char *)l
+ l
->linp
[NEXTINDEX(l
) - 1];
299 ((RINTERNAL
*)dest
)->nrecs
= NEXTINDEX(lchild
);
300 ((RINTERNAL
*)dest
)->pgno
= lchild
->pgno
;
302 /* Update the right page count. */
303 h
->linp
[skip
] = h
->upper
-= nbytes
;
304 dest
= (char *)h
+ h
->linp
[skip
];
305 ((RINTERNAL
*)dest
)->nrecs
= NEXTINDEX(rchild
);
306 ((RINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
312 /* Unpin the held pages. */
314 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
318 /* If the root page was split, make it look right. */
319 if (sp
->pgno
== P_ROOT
&&
320 (F_ISSET(t
, R_RECNO
) ?
321 bt_rroot(t
, sp
, l
, r
) : bt_broot(t
, sp
, l
, r
)) == RET_ERROR
)
324 mpool_put(t
->bt_mp
, lchild
, MPOOL_DIRTY
);
325 mpool_put(t
->bt_mp
, rchild
, MPOOL_DIRTY
);
328 /* Unpin the held pages. */
329 mpool_put(t
->bt_mp
, l
, MPOOL_DIRTY
);
330 mpool_put(t
->bt_mp
, r
, MPOOL_DIRTY
);
332 /* Clear any pages left on the stack. */
333 return (RET_SUCCESS
);
336 * If something fails in the above loop we were already walking back
337 * up the tree and the tree is now inconsistent. Nothing much we can
338 * do about it but release any memory we're holding.
340 err1
: mpool_put(t
->bt_mp
, lchild
, MPOOL_DIRTY
);
341 mpool_put(t
->bt_mp
, rchild
, MPOOL_DIRTY
);
343 err2
: mpool_put(t
->bt_mp
, l
, 0);
344 mpool_put(t
->bt_mp
, r
, 0);
345 __dbpanic(t
->bt_dbp
);
350 * BT_PAGE -- Split a non-root page of a btree.
355 * lp: pointer to left page pointer
356 * rp: pointer to right page pointer
357 * skip: pointer to index to leave open
358 * ilen: insert length
361 * Pointer to page in which to insert or NULL on error.
364 bt_page(t
, h
, lp
, rp
, skip
, ilen
)
376 /* Put the new right page for the split into place. */
377 if ((r
= __bt_new(t
, &npg
)) == NULL
)
380 r
->lower
= BTDATAOFF
;
381 r
->upper
= t
->bt_psize
;
382 r
->nextpg
= h
->nextpg
;
384 r
->flags
= h
->flags
& P_TYPE
;
387 * If we're splitting the last page on a level because we're appending
388 * a key to it (skip is NEXTINDEX()), it's likely that the data is
389 * sorted. Adding an empty page on the side of the level is less work
390 * and can push the fill factor much higher than normal. If we're
391 * wrong it's no big deal, we'll just do the split the right way next
392 * time. It may look like it's equally easy to do a similar hack for
393 * reverse sorted data, that is, split the tree left, but it's not.
396 if (h
->nextpg
== P_INVALID
&& *skip
== NEXTINDEX(h
)) {
401 r
->lower
= BTDATAOFF
+ sizeof(indx_t
);
408 /* Put the new left page for the split into place. */
409 if ((l
= (PAGE
*)malloc(t
->bt_psize
)) == NULL
) {
410 mpool_put(t
->bt_mp
, r
, 0);
414 memset(l
, 0xff, t
->bt_psize
);
418 l
->prevpg
= h
->prevpg
;
419 l
->lower
= BTDATAOFF
;
420 l
->upper
= t
->bt_psize
;
421 l
->flags
= h
->flags
& P_TYPE
;
423 /* Fix up the previous pointer of the page after the split page. */
424 if (h
->nextpg
!= P_INVALID
) {
425 if ((tp
= mpool_get(t
->bt_mp
, h
->nextpg
, 0)) == NULL
) {
427 /* XXX mpool_free(t->bt_mp, r->pgno); */
430 tp
->prevpg
= r
->pgno
;
431 mpool_put(t
->bt_mp
, tp
, MPOOL_DIRTY
);
435 * Split right. The key/data pairs aren't sorted in the btree page so
436 * it's simpler to copy the data from the split page onto two new pages
437 * instead of copying half the data to the right page and compacting
438 * the left page in place. Since the left page can't change, we have
439 * to swap the original and the allocated left page after the split.
441 tp
= bt_psplit(t
, h
, l
, r
, skip
, ilen
);
443 /* Move the new left page onto the old left page. */
444 memmove(h
, l
, t
->bt_psize
);
455 * BT_ROOT -- Split the root page of a btree.
460 * lp: pointer to left page pointer
461 * rp: pointer to right page pointer
462 * skip: pointer to index to leave open
463 * ilen: insert length
466 * Pointer to page in which to insert or NULL on error.
469 bt_root(t
, h
, lp
, rp
, skip
, ilen
)
482 /* Put the new left and right pages for the split into place. */
483 if ((l
= __bt_new(t
, &lnpg
)) == NULL
||
484 (r
= __bt_new(t
, &rnpg
)) == NULL
)
490 l
->prevpg
= r
->nextpg
= P_INVALID
;
491 l
->lower
= r
->lower
= BTDATAOFF
;
492 l
->upper
= r
->upper
= t
->bt_psize
;
493 l
->flags
= r
->flags
= h
->flags
& P_TYPE
;
495 /* Split the root page. */
496 tp
= bt_psplit(t
, h
, l
, r
, skip
, ilen
);
504 * BT_RROOT -- Fix up the recno root page after it has been split.
513 * RET_ERROR, RET_SUCCESS
522 /* Insert the left and right keys, set the header information. */
523 h
->linp
[0] = h
->upper
= t
->bt_psize
- NRINTERNAL
;
524 dest
= (char *)h
+ h
->upper
;
526 l
->flags
& P_RLEAF
? NEXTINDEX(l
) : rec_total(l
), l
->pgno
);
528 h
->linp
[1] = h
->upper
-= NRINTERNAL
;
529 dest
= (char *)h
+ h
->upper
;
531 r
->flags
& P_RLEAF
? NEXTINDEX(r
) : rec_total(r
), r
->pgno
);
533 h
->lower
= BTDATAOFF
+ 2 * sizeof(indx_t
);
535 /* Unpin the root page, set to recno internal page. */
537 h
->flags
|= P_RINTERNAL
;
538 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
540 return (RET_SUCCESS
);
544 * BT_BROOT -- Fix up the btree root page after it has been split.
553 * RET_ERROR, RET_SUCCESS
566 * If the root page was a leaf page, change it into an internal page.
567 * We copy the key we split on (but not the key's data, in the case of
568 * a leaf page) to the new root page.
570 * The btree comparison code guarantees that the left-most key on any
571 * level of the tree is never used, so it doesn't need to be filled in.
573 nbytes
= NBINTERNAL(0);
574 h
->linp
[0] = h
->upper
= t
->bt_psize
- nbytes
;
575 dest
= (char *)h
+ h
->upper
;
576 WR_BINTERNAL(dest
, 0, l
->pgno
, 0);
578 switch (h
->flags
& P_TYPE
) {
581 nbytes
= NBINTERNAL(bl
->ksize
);
582 h
->linp
[1] = h
->upper
-= nbytes
;
583 dest
= (char *)h
+ h
->upper
;
584 WR_BINTERNAL(dest
, bl
->ksize
, r
->pgno
, 0);
585 memmove(dest
, bl
->bytes
, bl
->ksize
);
588 * If the key is on an overflow page, mark the overflow chain
589 * so it isn't deleted when the leaf copy of the key is deleted.
591 if (bl
->flags
& P_BIGKEY
&&
592 bt_preserve(t
, *(pgno_t
*)bl
->bytes
) == RET_ERROR
)
596 bi
= GETBINTERNAL(r
, 0);
597 nbytes
= NBINTERNAL(bi
->ksize
);
598 h
->linp
[1] = h
->upper
-= nbytes
;
599 dest
= (char *)h
+ h
->upper
;
600 memmove(dest
, bi
, nbytes
);
601 ((BINTERNAL
*)dest
)->pgno
= r
->pgno
;
607 /* There are two keys on the page. */
608 h
->lower
= BTDATAOFF
+ 2 * sizeof(indx_t
);
610 /* Unpin the root page, set to btree internal page. */
612 h
->flags
|= P_BINTERNAL
;
613 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
615 return (RET_SUCCESS
);
619 * BT_PSPLIT -- Do the real work of splitting the page.
623 * h: page to be split
624 * l: page to put lower half of data
625 * r: page to put upper half of data
626 * pskip: pointer to index to leave open
627 * ilen: insert length
630 * Pointer to page in which to insert.
633 bt_psplit(t
, h
, l
, r
, pskip
, ilen
)
645 indx_t full
, half
, nxt
, off
, skip
, top
, used
;
647 int bigkeycnt
, isbigkey
;
650 * Split the data to the left and right pages. Leave the skip index
651 * open. Additionally, make some effort not to split on an overflow
652 * key. This makes internal page processing faster and can save
653 * space as overflow keys used by internal pages are never deleted.
657 full
= t
->bt_psize
- BTDATAOFF
;
660 for (nxt
= off
= 0, top
= NEXTINDEX(h
); nxt
< top
; ++off
) {
663 isbigkey
= 0; /* XXX: not really known. */
665 switch (h
->flags
& P_TYPE
) {
667 src
= bi
= GETBINTERNAL(h
, nxt
);
668 nbytes
= NBINTERNAL(bi
->ksize
);
669 isbigkey
= bi
->flags
& P_BIGKEY
;
672 src
= bl
= GETBLEAF(h
, nxt
);
674 isbigkey
= bl
->flags
& P_BIGKEY
;
677 src
= GETRINTERNAL(h
, nxt
);
682 src
= rl
= GETRLEAF(h
, nxt
);
691 * If the key/data pairs are substantial fractions of the max
692 * possible size for the page, it's possible to get situations
693 * where we decide to try and copy too much onto the left page.
694 * Make sure that doesn't happen.
696 if ((skip
<= off
&& used
+ nbytes
+ sizeof(indx_t
) >= full
)
702 /* Copy the key/data pair, if not the skipped index. */
706 l
->linp
[off
] = l
->upper
-= nbytes
;
707 memmove((char *)l
+ l
->upper
, src
, nbytes
);
710 used
+= nbytes
+ sizeof(indx_t
);
712 if (!isbigkey
|| bigkeycnt
== 3)
720 * Off is the last offset that's valid for the left page.
721 * Nxt is the first offset to be placed on the right page.
723 l
->lower
+= (off
+ 1) * sizeof(indx_t
);
726 * If splitting the page that the cursor was on, the cursor has to be
727 * adjusted to point to the same record as before the split. If the
728 * cursor is at or past the skipped slot, the cursor is incremented by
729 * one. If the cursor is on the right page, it is decremented by the
730 * number of records split to the left page.
733 if (F_ISSET(c
, CURS_INIT
) && c
->pg
.pgno
== h
->pgno
) {
734 if (c
->pg
.index
>= skip
)
736 if (c
->pg
.index
< nxt
) /* Left page. */
737 c
->pg
.pgno
= l
->pgno
;
738 else { /* Right page. */
739 c
->pg
.pgno
= r
->pgno
;
745 * If the skipped index was on the left page, just return that page.
746 * Otherwise, adjust the skip index to reflect the new position on
757 for (off
= 0; nxt
< top
; ++off
) {
762 switch (h
->flags
& P_TYPE
) {
764 src
= bi
= GETBINTERNAL(h
, nxt
);
765 nbytes
= NBINTERNAL(bi
->ksize
);
768 src
= bl
= GETBLEAF(h
, nxt
);
772 src
= GETRINTERNAL(h
, nxt
);
776 src
= rl
= GETRLEAF(h
, nxt
);
783 r
->linp
[off
] = r
->upper
-= nbytes
;
784 memmove((char *)r
+ r
->upper
, src
, nbytes
);
786 r
->lower
+= off
* sizeof(indx_t
);
788 /* If the key is being appended to the page, adjust the index. */
790 r
->lower
+= sizeof(indx_t
);
796 * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
798 * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
799 * record that references them gets deleted. Chains pointed to by internal
800 * pages never get deleted. This routine marks a chain as pointed to by an
805 * pg: page number of first page in the chain.
808 * RET_SUCCESS, RET_ERROR.
817 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
819 h
->flags
|= P_PRESERVE
;
820 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
821 return (RET_SUCCESS
);
825 * REC_TOTAL -- Return the number of recno entries below a page.
831 * The number of recno entries below a page.
834 * These values could be set by the bt_psplit routine. The problem is that the
835 * entry has to be popped off of the stack etc. or the values have to be passed
836 * all the way back to bt_split/bt_rroot and it's not very clean.
845 for (recs
= 0, nxt
= 0, top
= NEXTINDEX(h
); nxt
< top
; ++nxt
)
846 recs
+= GETRINTERNAL(h
, nxt
)->nrecs
;