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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)bt_split.c 8.9 (Berkeley) 7/26/94";
39 #endif /* LIBC_SCCS and not lint */
41 #include <sys/types.h>
51 static int bt_broot
__P((BTREE
*, PAGE
*, PAGE
*, PAGE
*));
53 __P((BTREE
*, PAGE
*, PAGE
**, PAGE
**, indx_t
*, size_t));
54 static int bt_preserve
__P((BTREE
*, pgno_t
));
55 static PAGE
*bt_psplit
56 __P((BTREE
*, PAGE
*, PAGE
*, PAGE
*, indx_t
*, size_t));
58 __P((BTREE
*, PAGE
*, PAGE
**, PAGE
**, indx_t
*, size_t));
59 static int bt_rroot
__P((BTREE
*, PAGE
*, PAGE
*, PAGE
*));
60 static recno_t rec_total
__P((PAGE
*));
63 u_long bt_rootsplit
, bt_split
, bt_sortsplit
, bt_pfxsaved
;
67 * __BT_SPLIT -- Split the tree.
73 * data: data to insert
74 * flags: BIGKEY/BIGDATA flags
76 * skip: index to leave open
79 * RET_ERROR, RET_SUCCESS
82 __bt_split(t
, sp
, key
, data
, flags
, ilen
, argskip
)
85 const DBT
*key
, *data
;
94 PAGE
*h
, *l
, *r
, *lchild
, *rchild
;
97 u_int32_t n
, nbytes
, nksize
;
102 * Split the page into two pages, l and r. The split routines return
103 * a pointer to the page into which the key should be inserted and with
104 * skip set to the offset which should be used. Additionally, l and r
108 h
= sp
->pgno
== P_ROOT
?
109 bt_root(t
, sp
, &l
, &r
, &skip
, ilen
) :
110 bt_page(t
, sp
, &l
, &r
, &skip
, ilen
);
115 * Insert the new key/data pair into the leaf page. (Key inserts
116 * always cause a leaf page to split first.)
118 h
->linp
[skip
] = h
->upper
-= ilen
;
119 dest
= (char *)h
+ h
->upper
;
120 if (F_ISSET(t
, R_RECNO
))
121 WR_RLEAF(dest
, data
, flags
)
123 WR_BLEAF(dest
, key
, data
, flags
)
125 /* If the root page was split, make it look right. */
126 if (sp
->pgno
== P_ROOT
&&
127 (F_ISSET(t
, R_RECNO
) ?
128 bt_rroot(t
, sp
, l
, r
) : bt_broot(t
, sp
, l
, r
)) == RET_ERROR
)
132 * Now we walk the parent page stack -- a LIFO stack of the pages that
133 * were traversed when we searched for the page that split. Each stack
134 * entry is a page number and a page index offset. The offset is for
135 * the page traversed on the search. We've just split a page, so we
136 * have to insert a new key into the parent page.
138 * If the insert into the parent page causes it to split, may have to
139 * continue splitting all the way up the tree. We stop if the root
140 * splits or the page inserted into didn't have to split to hold the
141 * new key. Some algorithms replace the key for the old page as well
142 * as the new page. We don't, as there's no reason to believe that the
143 * first key on the old page is any better than the key we have, and,
144 * in the case of a key being placed at index 0 causing the split, the
145 * key is unavailable.
147 * There are a maximum of 5 pages pinned at any time. We keep the left
148 * and right pages pinned while working on the parent. The 5 are the
149 * two children, left parent and right parent (when the parent splits)
150 * and the root page or the overflow key page when calling bt_preserve.
151 * This code must make sure that all pins are released other than the
152 * root page or overflow page which is unlocked elsewhere.
154 while ((parent
= BT_POP(t
)) != NULL
) {
158 /* Get the parent page. */
159 if ((h
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
163 * The new key goes ONE AFTER the index, because the split
166 skip
= parent
->index
+ 1;
169 * Calculate the space needed on the parent page.
171 * Prefix trees: space hack when inserting into BINTERNAL
172 * pages. Retain only what's needed to distinguish between
173 * the new entry and the LAST entry on the page to its left.
174 * If the keys compare equal, retain the entire key. Note,
175 * we don't touch overflow keys, and the entire key must be
176 * retained for the next-to-left most key on the leftmost
177 * page of each level, or the search will fail. Applicable
178 * ONLY to internal pages that have leaf pages as children.
179 * Further reduction of the key between pairs of internal
180 * pages loses too much information.
182 switch (rchild
->flags
& P_TYPE
) {
184 bi
= GETBINTERNAL(rchild
, 0);
185 nbytes
= NBINTERNAL(bi
->ksize
);
188 bl
= GETBLEAF(rchild
, 0);
189 nbytes
= NBINTERNAL(bl
->ksize
);
190 if (t
->bt_pfx
&& !(bl
->flags
& P_BIGKEY
) &&
191 (h
->prevpg
!= P_INVALID
|| skip
> 1)) {
192 tbl
= GETBLEAF(lchild
, NEXTINDEX(lchild
) - 1);
197 nksize
= t
->bt_pfx(&a
, &b
);
198 n
= NBINTERNAL(nksize
);
201 bt_pfxsaved
+= nbytes
- n
;
217 /* Split the parent page if necessary or shift the indices. */
218 if ((u_int32_t
) (h
->upper
- h
->lower
)
219 < nbytes
+ sizeof(indx_t
)) {
221 h
= h
->pgno
== P_ROOT
?
222 bt_root(t
, h
, &l
, &r
, &skip
, nbytes
) :
223 bt_page(t
, h
, &l
, &r
, &skip
, nbytes
);
228 if (skip
< (nxtindex
= NEXTINDEX(h
)))
229 memmove(h
->linp
+ skip
+ 1, h
->linp
+ skip
,
230 (nxtindex
- skip
) * sizeof(indx_t
));
231 h
->lower
+= sizeof(indx_t
);
235 /* Insert the key into the parent page. */
236 switch (rchild
->flags
& P_TYPE
) {
238 h
->linp
[skip
] = h
->upper
-= nbytes
;
239 dest
= (char *)h
+ h
->linp
[skip
];
240 memmove(dest
, bi
, nbytes
);
241 ((BINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
244 h
->linp
[skip
] = h
->upper
-= nbytes
;
245 dest
= (char *)h
+ h
->linp
[skip
];
246 WR_BINTERNAL(dest
, nksize
? nksize
: bl
->ksize
,
247 rchild
->pgno
, bl
->flags
& P_BIGKEY
);
248 memmove(dest
, bl
->bytes
, nksize
? nksize
: bl
->ksize
);
249 if (bl
->flags
& P_BIGKEY
&&
250 bt_preserve(t
, *(pgno_t
*)bl
->bytes
) == RET_ERROR
)
255 * Update the left page count. If split
256 * added at index 0, fix the correct page.
259 dest
= (char *)h
+ h
->linp
[skip
- 1];
261 dest
= (char *)l
+ l
->linp
[NEXTINDEX(l
) - 1];
262 ((RINTERNAL
*)dest
)->nrecs
= rec_total(lchild
);
263 ((RINTERNAL
*)dest
)->pgno
= lchild
->pgno
;
265 /* Update the right page count. */
266 h
->linp
[skip
] = h
->upper
-= nbytes
;
267 dest
= (char *)h
+ h
->linp
[skip
];
268 ((RINTERNAL
*)dest
)->nrecs
= rec_total(rchild
);
269 ((RINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
273 * Update the left page count. If split
274 * added at index 0, fix the correct page.
277 dest
= (char *)h
+ h
->linp
[skip
- 1];
279 dest
= (char *)l
+ l
->linp
[NEXTINDEX(l
) - 1];
280 ((RINTERNAL
*)dest
)->nrecs
= NEXTINDEX(lchild
);
281 ((RINTERNAL
*)dest
)->pgno
= lchild
->pgno
;
283 /* Update the right page count. */
284 h
->linp
[skip
] = h
->upper
-= nbytes
;
285 dest
= (char *)h
+ h
->linp
[skip
];
286 ((RINTERNAL
*)dest
)->nrecs
= NEXTINDEX(rchild
);
287 ((RINTERNAL
*)dest
)->pgno
= rchild
->pgno
;
293 /* Unpin the held pages. */
295 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
299 /* If the root page was split, make it look right. */
300 if (sp
->pgno
== P_ROOT
&&
301 (F_ISSET(t
, R_RECNO
) ?
302 bt_rroot(t
, sp
, l
, r
) : bt_broot(t
, sp
, l
, r
)) == RET_ERROR
)
305 mpool_put(t
->bt_mp
, lchild
, MPOOL_DIRTY
);
306 mpool_put(t
->bt_mp
, rchild
, MPOOL_DIRTY
);
309 /* Unpin the held pages. */
310 mpool_put(t
->bt_mp
, l
, MPOOL_DIRTY
);
311 mpool_put(t
->bt_mp
, r
, MPOOL_DIRTY
);
313 /* Clear any pages left on the stack. */
314 return (RET_SUCCESS
);
317 * If something fails in the above loop we were already walking back
318 * up the tree and the tree is now inconsistent. Nothing much we can
319 * do about it but release any memory we're holding.
321 err1
: mpool_put(t
->bt_mp
, lchild
, MPOOL_DIRTY
);
322 mpool_put(t
->bt_mp
, rchild
, MPOOL_DIRTY
);
324 err2
: mpool_put(t
->bt_mp
, l
, 0);
325 mpool_put(t
->bt_mp
, r
, 0);
326 __dbpanic(t
->bt_dbp
);
331 * BT_PAGE -- Split a non-root page of a btree.
336 * lp: pointer to left page pointer
337 * rp: pointer to right page pointer
338 * skip: pointer to index to leave open
339 * ilen: insert length
342 * Pointer to page in which to insert or NULL on error.
345 bt_page(t
, h
, lp
, rp
, skip
, ilen
)
357 /* Put the new right page for the split into place. */
358 if ((r
= __bt_new(t
, &npg
)) == NULL
)
361 r
->lower
= BTDATAOFF
;
362 r
->upper
= t
->bt_psize
;
363 r
->nextpg
= h
->nextpg
;
365 r
->flags
= h
->flags
& P_TYPE
;
368 * If we're splitting the last page on a level because we're appending
369 * a key to it (skip is NEXTINDEX()), it's likely that the data is
370 * sorted. Adding an empty page on the side of the level is less work
371 * and can push the fill factor much higher than normal. If we're
372 * wrong it's no big deal, we'll just do the split the right way next
373 * time. It may look like it's equally easy to do a similar hack for
374 * reverse sorted data, that is, split the tree left, but it's not.
377 if (h
->nextpg
== P_INVALID
&& *skip
== NEXTINDEX(h
)) {
382 r
->lower
= BTDATAOFF
+ sizeof(indx_t
);
389 /* Put the new left page for the split into place. */
390 if ((l
= (PAGE
*)malloc(t
->bt_psize
)) == NULL
) {
391 mpool_put(t
->bt_mp
, r
, 0);
395 memset(l
, 0xff, t
->bt_psize
);
399 l
->prevpg
= h
->prevpg
;
400 l
->lower
= BTDATAOFF
;
401 l
->upper
= t
->bt_psize
;
402 l
->flags
= h
->flags
& P_TYPE
;
404 /* Fix up the previous pointer of the page after the split page. */
405 if (h
->nextpg
!= P_INVALID
) {
406 if ((tp
= mpool_get(t
->bt_mp
, h
->nextpg
, 0)) == NULL
) {
408 /* XXX mpool_free(t->bt_mp, r->pgno); */
411 tp
->prevpg
= r
->pgno
;
412 mpool_put(t
->bt_mp
, tp
, MPOOL_DIRTY
);
416 * Split right. The key/data pairs aren't sorted in the btree page so
417 * it's simpler to copy the data from the split page onto two new pages
418 * instead of copying half the data to the right page and compacting
419 * the left page in place. Since the left page can't change, we have
420 * to swap the original and the allocated left page after the split.
422 tp
= bt_psplit(t
, h
, l
, r
, skip
, ilen
);
424 /* Move the new left page onto the old left page. */
425 memmove(h
, l
, t
->bt_psize
);
436 * BT_ROOT -- Split the root page of a btree.
441 * lp: pointer to left page pointer
442 * rp: pointer to right page pointer
443 * skip: pointer to index to leave open
444 * ilen: insert length
447 * Pointer to page in which to insert or NULL on error.
450 bt_root(t
, h
, lp
, rp
, skip
, ilen
)
463 /* Put the new left and right pages for the split into place. */
464 if ((l
= __bt_new(t
, &lnpg
)) == NULL
||
465 (r
= __bt_new(t
, &rnpg
)) == NULL
)
471 l
->prevpg
= r
->nextpg
= P_INVALID
;
472 l
->lower
= r
->lower
= BTDATAOFF
;
473 l
->upper
= r
->upper
= t
->bt_psize
;
474 l
->flags
= r
->flags
= h
->flags
& P_TYPE
;
476 /* Split the root page. */
477 tp
= bt_psplit(t
, h
, l
, r
, skip
, ilen
);
485 * BT_RROOT -- Fix up the recno root page after it has been split.
494 * RET_ERROR, RET_SUCCESS
503 /* Insert the left and right keys, set the header information. */
504 h
->linp
[0] = h
->upper
= t
->bt_psize
- NRINTERNAL
;
505 dest
= (char *)h
+ h
->upper
;
507 l
->flags
& P_RLEAF
? NEXTINDEX(l
) : rec_total(l
), l
->pgno
);
509 h
->linp
[1] = h
->upper
-= NRINTERNAL
;
510 dest
= (char *)h
+ h
->upper
;
512 r
->flags
& P_RLEAF
? NEXTINDEX(r
) : rec_total(r
), r
->pgno
);
514 h
->lower
= BTDATAOFF
+ 2 * sizeof(indx_t
);
516 /* Unpin the root page, set to recno internal page. */
518 h
->flags
|= P_RINTERNAL
;
519 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
521 return (RET_SUCCESS
);
525 * BT_BROOT -- Fix up the btree root page after it has been split.
534 * RET_ERROR, RET_SUCCESS
547 * If the root page was a leaf page, change it into an internal page.
548 * We copy the key we split on (but not the key's data, in the case of
549 * a leaf page) to the new root page.
551 * The btree comparison code guarantees that the left-most key on any
552 * level of the tree is never used, so it doesn't need to be filled in.
554 nbytes
= NBINTERNAL(0);
555 h
->linp
[0] = h
->upper
= t
->bt_psize
- nbytes
;
556 dest
= (char *)h
+ h
->upper
;
557 WR_BINTERNAL(dest
, 0, l
->pgno
, 0);
559 switch (h
->flags
& P_TYPE
) {
562 nbytes
= NBINTERNAL(bl
->ksize
);
563 h
->linp
[1] = h
->upper
-= nbytes
;
564 dest
= (char *)h
+ h
->upper
;
565 WR_BINTERNAL(dest
, bl
->ksize
, r
->pgno
, 0);
566 memmove(dest
, bl
->bytes
, bl
->ksize
);
569 * If the key is on an overflow page, mark the overflow chain
570 * so it isn't deleted when the leaf copy of the key is deleted.
572 if (bl
->flags
& P_BIGKEY
&&
573 bt_preserve(t
, *(pgno_t
*)bl
->bytes
) == RET_ERROR
)
577 bi
= GETBINTERNAL(r
, 0);
578 nbytes
= NBINTERNAL(bi
->ksize
);
579 h
->linp
[1] = h
->upper
-= nbytes
;
580 dest
= (char *)h
+ h
->upper
;
581 memmove(dest
, bi
, nbytes
);
582 ((BINTERNAL
*)dest
)->pgno
= r
->pgno
;
588 /* There are two keys on the page. */
589 h
->lower
= BTDATAOFF
+ 2 * sizeof(indx_t
);
591 /* Unpin the root page, set to btree internal page. */
593 h
->flags
|= P_BINTERNAL
;
594 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
596 return (RET_SUCCESS
);
600 * BT_PSPLIT -- Do the real work of splitting the page.
604 * h: page to be split
605 * l: page to put lower half of data
606 * r: page to put upper half of data
607 * pskip: pointer to index to leave open
608 * ilen: insert length
611 * Pointer to page in which to insert.
614 bt_psplit(t
, h
, l
, r
, pskip
, ilen
)
626 indx_t full
, half
, nxt
, off
, skip
, top
, used
;
628 int bigkeycnt
, isbigkey
;
631 * Split the data to the left and right pages. Leave the skip index
632 * open. Additionally, make some effort not to split on an overflow
633 * key. This makes internal page processing faster and can save
634 * space as overflow keys used by internal pages are never deleted.
638 full
= t
->bt_psize
- BTDATAOFF
;
641 for (nxt
= off
= 0, top
= NEXTINDEX(h
); nxt
< top
; ++off
) {
644 isbigkey
= 0; /* XXX: not really known. */
646 switch (h
->flags
& P_TYPE
) {
648 src
= bi
= GETBINTERNAL(h
, nxt
);
649 nbytes
= NBINTERNAL(bi
->ksize
);
650 isbigkey
= bi
->flags
& P_BIGKEY
;
653 src
= bl
= GETBLEAF(h
, nxt
);
655 isbigkey
= bl
->flags
& P_BIGKEY
;
658 src
= GETRINTERNAL(h
, nxt
);
663 src
= rl
= GETRLEAF(h
, nxt
);
672 * If the key/data pairs are substantial fractions of the max
673 * possible size for the page, it's possible to get situations
674 * where we decide to try and copy too much onto the left page.
675 * Make sure that doesn't happen.
678 used
+ nbytes
+ sizeof(indx_t
) >= full
|| nxt
== top
- 1) {
683 /* Copy the key/data pair, if not the skipped index. */
687 l
->linp
[off
] = l
->upper
-= nbytes
;
688 memmove((char *)l
+ l
->upper
, src
, nbytes
);
691 used
+= nbytes
+ sizeof(indx_t
);
693 if (!isbigkey
|| bigkeycnt
== 3)
701 * Off is the last offset that's valid for the left page.
702 * Nxt is the first offset to be placed on the right page.
704 l
->lower
+= (off
+ 1) * sizeof(indx_t
);
707 * If splitting the page that the cursor was on, the cursor has to be
708 * adjusted to point to the same record as before the split. If the
709 * cursor is at or past the skipped slot, the cursor is incremented by
710 * one. If the cursor is on the right page, it is decremented by the
711 * number of records split to the left page.
714 if (F_ISSET(c
, CURS_INIT
) && c
->pg
.pgno
== h
->pgno
) {
715 if (c
->pg
.index
>= skip
)
717 if (c
->pg
.index
< nxt
) /* Left page. */
718 c
->pg
.pgno
= l
->pgno
;
719 else { /* Right page. */
720 c
->pg
.pgno
= r
->pgno
;
726 * If the skipped index was on the left page, just return that page.
727 * Otherwise, adjust the skip index to reflect the new position on
738 for (off
= 0; nxt
< top
; ++off
) {
743 switch (h
->flags
& P_TYPE
) {
745 src
= bi
= GETBINTERNAL(h
, nxt
);
746 nbytes
= NBINTERNAL(bi
->ksize
);
749 src
= bl
= GETBLEAF(h
, nxt
);
753 src
= GETRINTERNAL(h
, nxt
);
757 src
= rl
= GETRLEAF(h
, nxt
);
764 r
->linp
[off
] = r
->upper
-= nbytes
;
765 memmove((char *)r
+ r
->upper
, src
, nbytes
);
767 r
->lower
+= off
* sizeof(indx_t
);
769 /* If the key is being appended to the page, adjust the index. */
771 r
->lower
+= sizeof(indx_t
);
777 * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
779 * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
780 * record that references them gets deleted. Chains pointed to by internal
781 * pages never get deleted. This routine marks a chain as pointed to by an
786 * pg: page number of first page in the chain.
789 * RET_SUCCESS, RET_ERROR.
798 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
800 h
->flags
|= P_PRESERVE
;
801 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
802 return (RET_SUCCESS
);
806 * REC_TOTAL -- Return the number of recno entries below a page.
812 * The number of recno entries below a page.
815 * These values could be set by the bt_psplit routine. The problem is that the
816 * entry has to be popped off of the stack etc. or the values have to be passed
817 * all the way back to bt_split/bt_rroot and it's not very clean.
826 for (recs
= 0, nxt
= 0, top
= NEXTINDEX(h
); nxt
< top
; ++nxt
)
827 recs
+= GETRINTERNAL(h
, nxt
)->nrecs
;