]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_split.c
d11c37f04c57dc080280a155c736a9a0a860ef90
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/types.h>
69 static int bt_broot
__P((BTREE
*, PAGE
*, PAGE
*, PAGE
*));
71 __P((BTREE
*, PAGE
*, PAGE
**, PAGE
**, indx_t
*, size_t));
72 static int bt_preserve
__P((BTREE
*, pgno_t
));
73 static PAGE
*bt_psplit
74 __P((BTREE
*, PAGE
*, PAGE
*, PAGE
*, indx_t
*, size_t));
76 __P((BTREE
*, PAGE
*, PAGE
**, PAGE
**, indx_t
*, size_t));
77 static int bt_rroot
__P((BTREE
*, PAGE
*, PAGE
*, PAGE
*));
78 static recno_t rec_total
__P((PAGE
*));
81 u_long bt_rootsplit
, bt_split
, bt_sortsplit
, bt_pfxsaved
;
85 * __BT_SPLIT -- Split the tree.
91 * data: data to insert
92 * flags: BIGKEY/BIGDATA flags
94 * skip: index to leave open
97 * RET_ERROR, RET_SUCCESS
100 __bt_split(t
, sp
, key
, data
, flags
, ilen
, skip
)
103 const DBT
*key
, *data
;
112 PAGE
*h
, *l
, *r
, *lchild
, *rchild
;
114 size_t n
, nbytes
, nksize
;
119 * Split the page into two pages, l and r. The split routines return
120 * a pointer to the page into which the key should be inserted and with
121 * skip set to the offset which should be used. Additionally, l and r
124 h
= sp
->pgno
== P_ROOT
?
125 bt_root(t
, sp
, &l
, &r
, &skip
, ilen
) :
126 bt_page(t
, sp
, &l
, &r
, &skip
, ilen
);
131 * Insert the new key/data pair into the leaf page. (Key inserts
132 * always cause a leaf page to split first.)
134 h
->linp
[skip
] = h
->upper
-= ilen
;
135 dest
= (char *)h
+ h
->upper
;
136 if (ISSET(t
, R_RECNO
))
137 WR_RLEAF(dest
, data
, flags
)
139 WR_BLEAF(dest
, key
, data
, flags
)
141 /* If the root page was split, make it look right. */
142 if (sp
->pgno
== P_ROOT
&&
144 bt_rroot(t
, sp
, l
, r
) : bt_broot(t
, sp
, l
, r
)) == RET_ERROR
)
148 * Now we walk the parent page stack -- a LIFO stack of the pages that
149 * were traversed when we searched for the page that split. Each stack
150 * entry is a page number and a page index offset. The offset is for
151 * the page traversed on the search. We've just split a page, so we
152 * have to insert a new key into the parent page.
154 * If the insert into the parent page causes it to split, may have to
155 * continue splitting all the way up the tree. We stop if the root
156 * splits or the page inserted into didn't have to split to hold the
157 * new key. Some algorithms replace the key for the old page as well
158 * as the new page. We don't, as there's no reason to believe that the
159 * first key on the old page is any better than the key we have, and,
160 * in the case of a key being placed at index 0 causing the split, the
161 * key is unavailable.
163 * There are a maximum of 5 pages pinned at any time. We keep the left
164 * and right pages pinned while working on the parent. The 5 are the
165 * two children, left parent and right parent (when the parent splits)
166 * and the root page or the overflow key page when calling bt_preserve.
167 * This code must make sure that all pins are released other than the
168 * root page or overflow page which is unlocked elsewhere.
170 while ((parent
= BT_POP(t
)) != NULL
) {
174 /* Get the parent page. */
175 if ((h
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
179 * The new key goes ONE AFTER the index, because the split
182 skip
= parent
->index
+ 1;
185 * Calculate the space needed on the parent page.
187 * Prefix trees: space hack when inserting into BINTERNAL
188 * pages. Retain only what's needed to distinguish between
189 * the new entry and the LAST entry on the page to its left.
190 * If the keys compare equal, retain the entire key. Note,
191 * we don't touch overflow keys, and the entire key must be
192 * retained for the next-to-left most key on the leftmost
193 * page of each level, or the search will fail. Applicable
194 * ONLY to internal pages that have leaf pages as children.
195 * Further reduction of the key between pairs of internal
196 * pages loses too much information.
198 switch (rchild
->flags
& P_TYPE
) {
200 bi
= GETBINTERNAL(rchild
, 0);
201 nbytes
= NBINTERNAL(bi
->ksize
);
204 bl
= GETBLEAF(rchild
, 0);
205 nbytes
= NBINTERNAL(bl
->ksize
);
206 if (t
->bt_pfx
&& !(bl
->flags
& P_BIGKEY
) &&
207 (h
->prevpg
!= P_INVALID
|| skip
> 1)) {
208 tbl
= GETBLEAF(lchild
, NEXTINDEX(lchild
) - 1);
213 nksize
= t
->bt_pfx(&a
, &b
);
214 n
= NBINTERNAL(nksize
);
217 bt_pfxsaved
+= nbytes
- n
;
233 /* Split the parent page if necessary or shift the indices. */
234 if (h
->upper
- h
->lower
< nbytes
+ sizeof(indx_t
)) {
236 h
= h
->pgno
== P_ROOT
?
237 bt_root(t
, h
, &l
, &r
, &skip
, nbytes
) :
238 bt_page(t
, h
, &l
, &r
, &skip
, nbytes
);
243 if (skip
< (nxtindex
= NEXTINDEX(h
)))
244 memmove(h
->linp
+ skip
+ 1, h
->linp
+ skip
,
245 (nxtindex
- skip
) * sizeof(indx_t
));
246 h
->lower
+= sizeof(indx_t
);
250 /* Insert the key into the parent page. */
251 switch(rchild
->flags
& P_TYPE
) {
253 h
->linp
[skip
] = h
->upper
-= nbytes
;
254 dest
= (char *)h
+ h
->linp
[skip
];
255 memmove(dest
, bi
, nbytes
);
256 ((BINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
259 h
->linp
[skip
] = h
->upper
-= nbytes
;
260 dest
= (char *)h
+ h
->linp
[skip
];
261 WR_BINTERNAL(dest
, nksize
? nksize
: bl
->ksize
,
262 rchild
->pgno
, bl
->flags
& P_BIGKEY
);
263 memmove(dest
, bl
->bytes
, nksize
? nksize
: bl
->ksize
);
264 if (bl
->flags
& P_BIGKEY
&&
265 bt_preserve(t
, *(pgno_t
*)bl
->bytes
) == RET_ERROR
)
270 * Update the left page count. If split
271 * added at index 0, fix the correct page.
274 dest
= (char *)h
+ h
->linp
[skip
- 1];
276 dest
= (char *)l
+ l
->linp
[NEXTINDEX(l
) - 1];
277 ((RINTERNAL
*)dest
)->nrecs
= rec_total(lchild
);
278 ((RINTERNAL
*)dest
)->pgno
= lchild
->pgno
;
280 /* Update the right page count. */
281 h
->linp
[skip
] = h
->upper
-= nbytes
;
282 dest
= (char *)h
+ h
->linp
[skip
];
283 ((RINTERNAL
*)dest
)->nrecs
= rec_total(rchild
);
284 ((RINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
288 * Update the left page count. If split
289 * added at index 0, fix the correct page.
292 dest
= (char *)h
+ h
->linp
[skip
- 1];
294 dest
= (char *)l
+ l
->linp
[NEXTINDEX(l
) - 1];
295 ((RINTERNAL
*)dest
)->nrecs
= NEXTINDEX(lchild
);
296 ((RINTERNAL
*)dest
)->pgno
= lchild
->pgno
;
298 /* Update the right page count. */
299 h
->linp
[skip
] = h
->upper
-= nbytes
;
300 dest
= (char *)h
+ h
->linp
[skip
];
301 ((RINTERNAL
*)dest
)->nrecs
= NEXTINDEX(rchild
);
302 ((RINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
308 /* Unpin the held pages. */
310 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
314 /* If the root page was split, make it look right. */
315 if (sp
->pgno
== P_ROOT
&&
317 bt_rroot(t
, sp
, l
, r
) : bt_broot(t
, sp
, l
, r
)) == RET_ERROR
)
320 mpool_put(t
->bt_mp
, lchild
, MPOOL_DIRTY
);
321 mpool_put(t
->bt_mp
, rchild
, MPOOL_DIRTY
);
324 /* Unpin the held pages. */
325 mpool_put(t
->bt_mp
, l
, MPOOL_DIRTY
);
326 mpool_put(t
->bt_mp
, r
, MPOOL_DIRTY
);
328 /* Clear any pages left on the stack. */
329 return (RET_SUCCESS
);
332 * If something fails in the above loop we were already walking back
333 * up the tree and the tree is now inconsistent. Nothing much we can
334 * do about it but release any memory we're holding.
336 err1
: mpool_put(t
->bt_mp
, lchild
, MPOOL_DIRTY
);
337 mpool_put(t
->bt_mp
, rchild
, MPOOL_DIRTY
);
339 err2
: mpool_put(t
->bt_mp
, l
, 0);
340 mpool_put(t
->bt_mp
, r
, 0);
341 __dbpanic(t
->bt_dbp
);
346 * BT_PAGE -- Split a non-root page of a btree.
351 * lp: pointer to left page pointer
352 * rp: pointer to right page pointer
353 * skip: pointer to index to leave open
354 * ilen: insert length
357 * Pointer to page in which to insert or NULL on error.
360 bt_page(t
, h
, lp
, rp
, skip
, ilen
)
372 /* Put the new right page for the split into place. */
373 if ((r
= __bt_new(t
, &npg
)) == NULL
)
376 r
->lower
= BTDATAOFF
;
377 r
->upper
= t
->bt_psize
;
378 r
->nextpg
= h
->nextpg
;
380 r
->flags
= h
->flags
& P_TYPE
;
383 * If we're splitting the last page on a level because we're appending
384 * a key to it (skip is NEXTINDEX()), it's likely that the data is
385 * sorted. Adding an empty page on the side of the level is less work
386 * and can push the fill factor much higher than normal. If we're
387 * wrong it's no big deal, we'll just do the split the right way next
388 * time. It may look like it's equally easy to do a similar hack for
389 * reverse sorted data, that is, split the tree left, but it's not.
392 if (h
->nextpg
== P_INVALID
&& *skip
== NEXTINDEX(h
)) {
397 r
->lower
= BTDATAOFF
+ sizeof(indx_t
);
404 /* Put the new left page for the split into place. */
405 if ((l
= (PAGE
*)malloc(t
->bt_psize
)) == NULL
) {
406 mpool_put(t
->bt_mp
, r
, 0);
411 l
->prevpg
= h
->prevpg
;
412 l
->lower
= BTDATAOFF
;
413 l
->upper
= t
->bt_psize
;
414 l
->flags
= h
->flags
& P_TYPE
;
416 /* Fix up the previous pointer of the page after the split page. */
417 if (h
->nextpg
!= P_INVALID
) {
418 if ((tp
= mpool_get(t
->bt_mp
, h
->nextpg
, 0)) == NULL
) {
420 /* XXX mpool_free(t->bt_mp, r->pgno); */
423 tp
->prevpg
= r
->pgno
;
424 mpool_put(t
->bt_mp
, tp
, 0);
428 * Split right. The key/data pairs aren't sorted in the btree page so
429 * it's simpler to copy the data from the split page onto two new pages
430 * instead of copying half the data to the right page and compacting
431 * the left page in place. Since the left page can't change, we have
432 * to swap the original and the allocated left page after the split.
434 tp
= bt_psplit(t
, h
, l
, r
, skip
, ilen
);
436 /* Move the new left page onto the old left page. */
437 memmove(h
, l
, t
->bt_psize
);
448 * BT_ROOT -- Split the root page of a btree.
453 * lp: pointer to left page pointer
454 * rp: pointer to right page pointer
455 * skip: pointer to index to leave open
456 * ilen: insert length
459 * Pointer to page in which to insert or NULL on error.
462 bt_root(t
, h
, lp
, rp
, skip
, ilen
)
475 /* Put the new left and right pages for the split into place. */
476 if ((l
= __bt_new(t
, &lnpg
)) == NULL
||
477 (r
= __bt_new(t
, &rnpg
)) == NULL
)
483 l
->prevpg
= r
->nextpg
= P_INVALID
;
484 l
->lower
= r
->lower
= BTDATAOFF
;
485 l
->upper
= r
->upper
= t
->bt_psize
;
486 l
->flags
= r
->flags
= h
->flags
& P_TYPE
;
488 /* Split the root page. */
489 tp
= bt_psplit(t
, h
, l
, r
, skip
, ilen
);
497 * BT_RROOT -- Fix up the recno root page after it has been split.
506 * RET_ERROR, RET_SUCCESS
515 /* Insert the left and right keys, set the header information. */
516 h
->linp
[0] = h
->upper
= t
->bt_psize
- NRINTERNAL
;
517 dest
= (char *)h
+ h
->upper
;
519 l
->flags
& P_RLEAF
? NEXTINDEX(l
) : rec_total(l
), l
->pgno
);
521 h
->linp
[1] = h
->upper
-= NRINTERNAL
;
522 dest
= (char *)h
+ h
->upper
;
524 r
->flags
& P_RLEAF
? NEXTINDEX(r
) : rec_total(r
), r
->pgno
);
526 h
->lower
= BTDATAOFF
+ 2 * sizeof(indx_t
);
528 /* Unpin the root page, set to recno internal page. */
530 h
->flags
|= P_RINTERNAL
;
531 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
533 return (RET_SUCCESS
);
537 * BT_BROOT -- Fix up the btree root page after it has been split.
546 * RET_ERROR, RET_SUCCESS
559 * If the root page was a leaf page, change it into an internal page.
560 * We copy the key we split on (but not the key's data, in the case of
561 * a leaf page) to the new root page.
563 * The btree comparison code guarantees that the left-most key on any
564 * level of the tree is never used, so it doesn't need to be filled in.
566 nbytes
= NBINTERNAL(0);
567 h
->linp
[0] = h
->upper
= t
->bt_psize
- nbytes
;
568 dest
= (char *)h
+ h
->upper
;
569 WR_BINTERNAL(dest
, 0, l
->pgno
, 0);
571 switch(h
->flags
& P_TYPE
) {
574 nbytes
= NBINTERNAL(bl
->ksize
);
575 h
->linp
[1] = h
->upper
-= nbytes
;
576 dest
= (char *)h
+ h
->upper
;
577 WR_BINTERNAL(dest
, bl
->ksize
, r
->pgno
, 0);
578 memmove(dest
, bl
->bytes
, bl
->ksize
);
581 * If the key is on an overflow page, mark the overflow chain
582 * so it isn't deleted when the leaf copy of the key is deleted.
584 if (bl
->flags
& P_BIGKEY
&&
585 bt_preserve(t
, *(pgno_t
*)bl
->bytes
) == RET_ERROR
)
589 bi
= GETBINTERNAL(r
, 0);
590 nbytes
= NBINTERNAL(bi
->ksize
);
591 h
->linp
[1] = h
->upper
-= nbytes
;
592 dest
= (char *)h
+ h
->upper
;
593 memmove(dest
, bi
, nbytes
);
594 ((BINTERNAL
*)dest
)->pgno
= r
->pgno
;
600 /* There are two keys on the page. */
601 h
->lower
= BTDATAOFF
+ 2 * sizeof(indx_t
);
603 /* Unpin the root page, set to btree internal page. */
605 h
->flags
|= P_BINTERNAL
;
606 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
608 return (RET_SUCCESS
);
612 * BT_PSPLIT -- Do the real work of splitting the page.
616 * h: page to be split
617 * l: page to put lower half of data
618 * r: page to put upper half of data
619 * pskip: pointer to index to leave open
620 * ilen: insert length
623 * Pointer to page in which to insert.
626 bt_psplit(t
, h
, l
, r
, pskip
, ilen
)
638 indx_t full
, half
, nxt
, off
, skip
, top
, used
;
640 int bigkeycnt
, isbigkey
;
643 * Split the data to the left and right pages. Leave the skip index
644 * open. Additionally, make some effort not to split on an overflow
645 * key. This makes internal page processing faster and can save
646 * space as overflow keys used by internal pages are never deleted.
650 full
= t
->bt_psize
- BTDATAOFF
;
653 for (nxt
= off
= 0, top
= NEXTINDEX(h
); nxt
< top
; ++off
) {
656 isbigkey
= 0; /* XXX: not really known. */
658 switch (h
->flags
& P_TYPE
) {
660 src
= bi
= GETBINTERNAL(h
, nxt
);
661 nbytes
= NBINTERNAL(bi
->ksize
);
662 isbigkey
= bi
->flags
& P_BIGKEY
;
665 src
= bl
= GETBLEAF(h
, nxt
);
667 isbigkey
= bl
->flags
& P_BIGKEY
;
670 src
= GETRINTERNAL(h
, nxt
);
675 src
= rl
= GETRLEAF(h
, nxt
);
684 * If the key/data pairs are substantial fractions of the max
685 * possible size for the page, it's possible to get situations
686 * where we decide to try and copy too much onto the left page.
687 * Make sure that doesn't happen.
689 if (skip
<= off
&& used
+ nbytes
>= full
) {
694 /* Copy the key/data pair, if not the skipped index. */
698 l
->linp
[off
] = l
->upper
-= nbytes
;
699 memmove((char *)l
+ l
->upper
, src
, nbytes
);
704 if (!isbigkey
|| bigkeycnt
== 3)
712 * Off is the last offset that's valid for the left page.
713 * Nxt is the first offset to be placed on the right page.
715 l
->lower
+= (off
+ 1) * sizeof(indx_t
);
718 * If splitting the page that the cursor was on, the cursor has to be
719 * adjusted to point to the same record as before the split. If the
720 * cursor is at or past the skipped slot, the cursor is incremented by
721 * one. If the cursor is on the right page, it is decremented by the
722 * number of records split to the left page.
724 * Don't bother checking for the B_SEQINIT flag, the page number will
728 if (c
->pgno
== h
->pgno
) {
729 if (c
->index
>= skip
)
731 if (c
->index
< nxt
) /* Left page. */
733 else { /* Right page. */
740 * If the skipped index was on the left page, just return that page.
741 * Otherwise, adjust the skip index to reflect the new position on
752 for (off
= 0; nxt
< top
; ++off
) {
757 switch (h
->flags
& P_TYPE
) {
759 src
= bi
= GETBINTERNAL(h
, nxt
);
760 nbytes
= NBINTERNAL(bi
->ksize
);
763 src
= bl
= GETBLEAF(h
, nxt
);
767 src
= GETRINTERNAL(h
, nxt
);
771 src
= rl
= GETRLEAF(h
, nxt
);
778 r
->linp
[off
] = r
->upper
-= nbytes
;
779 memmove((char *)r
+ r
->upper
, src
, nbytes
);
781 r
->lower
+= off
* sizeof(indx_t
);
783 /* If the key is being appended to the page, adjust the index. */
785 r
->lower
+= sizeof(indx_t
);
791 * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
793 * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
794 * record that references them gets deleted. Chains pointed to by internal
795 * pages never get deleted. This routine marks a chain as pointed to by an
800 * pg: page number of first page in the chain.
803 * RET_SUCCESS, RET_ERROR.
812 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
814 h
->flags
|= P_PRESERVE
;
815 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
816 return (RET_SUCCESS
);
820 * REC_TOTAL -- Return the number of recno entries below a page.
826 * The number of recno entries below a page.
829 * These values could be set by the bt_psplit routine. The problem is that the
830 * entry has to be popped off of the stack etc. or the values have to be passed
831 * all the way back to bt_split/bt_rroot and it's not very clean.
840 for (recs
= 0, nxt
= 0, top
= NEXTINDEX(h
); nxt
< top
; ++nxt
)
841 recs
+= GETRINTERNAL(h
, nxt
)->nrecs
;