1*f14fb602SLionel Sambuc /* $NetBSD: bt_split.c,v 1.20 2011/06/20 09:11:17 mrg Exp $ */
22639ae9bSBen Gras
32639ae9bSBen Gras /*-
42639ae9bSBen Gras * Copyright (c) 1990, 1993, 1994
52639ae9bSBen Gras * The Regents of the University of California. All rights reserved.
62639ae9bSBen Gras *
72639ae9bSBen Gras * This code is derived from software contributed to Berkeley by
82639ae9bSBen Gras * Mike Olson.
92639ae9bSBen Gras *
102639ae9bSBen Gras * Redistribution and use in source and binary forms, with or without
112639ae9bSBen Gras * modification, are permitted provided that the following conditions
122639ae9bSBen Gras * are met:
132639ae9bSBen Gras * 1. Redistributions of source code must retain the above copyright
142639ae9bSBen Gras * notice, this list of conditions and the following disclaimer.
152639ae9bSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162639ae9bSBen Gras * notice, this list of conditions and the following disclaimer in the
172639ae9bSBen Gras * documentation and/or other materials provided with the distribution.
182639ae9bSBen Gras * 3. Neither the name of the University nor the names of its contributors
192639ae9bSBen Gras * may be used to endorse or promote products derived from this software
202639ae9bSBen Gras * without specific prior written permission.
212639ae9bSBen Gras *
222639ae9bSBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232639ae9bSBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242639ae9bSBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252639ae9bSBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262639ae9bSBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272639ae9bSBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282639ae9bSBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292639ae9bSBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302639ae9bSBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312639ae9bSBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322639ae9bSBen Gras * SUCH DAMAGE.
332639ae9bSBen Gras */
342639ae9bSBen Gras
352639ae9bSBen Gras #if HAVE_NBTOOL_CONFIG_H
362639ae9bSBen Gras #include "nbtool_config.h"
372639ae9bSBen Gras #endif
382639ae9bSBen Gras
392639ae9bSBen Gras #include <sys/cdefs.h>
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: bt_split.c,v 1.20 2011/06/20 09:11:17 mrg Exp $");
412639ae9bSBen Gras
422639ae9bSBen Gras #include "namespace.h"
432639ae9bSBen Gras #include <sys/types.h>
442639ae9bSBen Gras
452639ae9bSBen Gras #include <assert.h>
462639ae9bSBen Gras #include <limits.h>
472639ae9bSBen Gras #include <stdio.h>
482639ae9bSBen Gras #include <stdlib.h>
492639ae9bSBen Gras #include <string.h>
502639ae9bSBen Gras
512639ae9bSBen Gras #include <db.h>
522639ae9bSBen Gras #include "btree.h"
532639ae9bSBen Gras
542639ae9bSBen Gras static int bt_broot(BTREE *, PAGE *, PAGE *, PAGE *);
552639ae9bSBen Gras static PAGE *bt_page(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
562639ae9bSBen Gras static int bt_preserve(BTREE *, pgno_t);
572639ae9bSBen Gras static PAGE *bt_psplit(BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t);
582639ae9bSBen Gras static PAGE *bt_root(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
592639ae9bSBen Gras static int bt_rroot(BTREE *, PAGE *, PAGE *, PAGE *);
602639ae9bSBen Gras static recno_t rec_total(PAGE *);
612639ae9bSBen Gras
622639ae9bSBen Gras #ifdef STATISTICS
632639ae9bSBen Gras unsigned long bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved;
642639ae9bSBen Gras #endif
652639ae9bSBen Gras
662639ae9bSBen Gras /*
672639ae9bSBen Gras * __BT_SPLIT -- Split the tree.
682639ae9bSBen Gras *
692639ae9bSBen Gras * Parameters:
702639ae9bSBen Gras * t: tree
712639ae9bSBen Gras * sp: page to split
722639ae9bSBen Gras * key: key to insert
732639ae9bSBen Gras * data: data to insert
742639ae9bSBen Gras * flags: BIGKEY/BIGDATA flags
752639ae9bSBen Gras * ilen: insert length
762639ae9bSBen Gras * skip: index to leave open
772639ae9bSBen Gras *
782639ae9bSBen Gras * Returns:
792639ae9bSBen Gras * RET_ERROR, RET_SUCCESS
802639ae9bSBen Gras */
812639ae9bSBen Gras int
__bt_split(BTREE * t,PAGE * sp,const DBT * key,const DBT * data,int flags,size_t ilen,uint32_t argskip)822639ae9bSBen Gras __bt_split(BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags,
832639ae9bSBen Gras size_t ilen, uint32_t argskip)
842639ae9bSBen Gras {
852639ae9bSBen Gras BINTERNAL *bi = NULL; /* pacify gcc */
862639ae9bSBen Gras BLEAF *bl = NULL, *tbl; /* pacify gcc */
872639ae9bSBen Gras DBT a, b;
882639ae9bSBen Gras EPGNO *parent;
892639ae9bSBen Gras PAGE *h, *l, *r, *lchild, *rchild;
902639ae9bSBen Gras indx_t nxtindex;
912639ae9bSBen Gras uint16_t skip;
922639ae9bSBen Gras uint32_t n, nbytes, nksize = 0; /* pacify gcc */
932639ae9bSBen Gras int parentsplit;
942639ae9bSBen Gras char *dest;
952639ae9bSBen Gras
962639ae9bSBen Gras /*
972639ae9bSBen Gras * Split the page into two pages, l and r. The split routines return
982639ae9bSBen Gras * a pointer to the page into which the key should be inserted and with
992639ae9bSBen Gras * skip set to the offset which should be used. Additionally, l and r
1002639ae9bSBen Gras * are pinned.
1012639ae9bSBen Gras */
1022639ae9bSBen Gras skip = argskip;
1032639ae9bSBen Gras h = sp->pgno == P_ROOT ?
1042639ae9bSBen Gras bt_root(t, sp, &l, &r, &skip, ilen) :
1052639ae9bSBen Gras bt_page(t, sp, &l, &r, &skip, ilen);
1062639ae9bSBen Gras if (h == NULL)
1072639ae9bSBen Gras return (RET_ERROR);
1082639ae9bSBen Gras
1092639ae9bSBen Gras /*
1102639ae9bSBen Gras * Insert the new key/data pair into the leaf page. (Key inserts
1112639ae9bSBen Gras * always cause a leaf page to split first.)
1122639ae9bSBen Gras */
1132639ae9bSBen Gras _DBFIT(ilen, indx_t);
1142639ae9bSBen Gras h->upper -= (indx_t)ilen;
1152639ae9bSBen Gras h->linp[skip] = h->upper;
1162639ae9bSBen Gras dest = (char *)(void *)h + h->upper;
1172639ae9bSBen Gras if (F_ISSET(t, R_RECNO))
1182639ae9bSBen Gras WR_RLEAF(dest, data, flags);
1192639ae9bSBen Gras else
1202639ae9bSBen Gras WR_BLEAF(dest, key, data, flags);
1212639ae9bSBen Gras
1222639ae9bSBen Gras /* If the root page was split, make it look right. */
1232639ae9bSBen Gras if (sp->pgno == P_ROOT &&
1242639ae9bSBen Gras (F_ISSET(t, R_RECNO) ?
1252639ae9bSBen Gras bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
1262639ae9bSBen Gras goto err2;
1272639ae9bSBen Gras
1282639ae9bSBen Gras /*
1292639ae9bSBen Gras * Now we walk the parent page stack -- a LIFO stack of the pages that
1302639ae9bSBen Gras * were traversed when we searched for the page that split. Each stack
1312639ae9bSBen Gras * entry is a page number and a page index offset. The offset is for
1322639ae9bSBen Gras * the page traversed on the search. We've just split a page, so we
1332639ae9bSBen Gras * have to insert a new key into the parent page.
1342639ae9bSBen Gras *
1352639ae9bSBen Gras * If the insert into the parent page causes it to split, may have to
1362639ae9bSBen Gras * continue splitting all the way up the tree. We stop if the root
1372639ae9bSBen Gras * splits or the page inserted into didn't have to split to hold the
1382639ae9bSBen Gras * new key. Some algorithms replace the key for the old page as well
1392639ae9bSBen Gras * as the new page. We don't, as there's no reason to believe that the
1402639ae9bSBen Gras * first key on the old page is any better than the key we have, and,
1412639ae9bSBen Gras * in the case of a key being placed at index 0 causing the split, the
1422639ae9bSBen Gras * key is unavailable.
1432639ae9bSBen Gras *
1442639ae9bSBen Gras * There are a maximum of 5 pages pinned at any time. We keep the left
1452639ae9bSBen Gras * and right pages pinned while working on the parent. The 5 are the
1462639ae9bSBen Gras * two children, left parent and right parent (when the parent splits)
1472639ae9bSBen Gras * and the root page or the overflow key page when calling bt_preserve.
1482639ae9bSBen Gras * This code must make sure that all pins are released other than the
1492639ae9bSBen Gras * root page or overflow page which is unlocked elsewhere.
1502639ae9bSBen Gras */
1512639ae9bSBen Gras while ((parent = BT_POP(t)) != NULL) {
1522639ae9bSBen Gras lchild = l;
1532639ae9bSBen Gras rchild = r;
1542639ae9bSBen Gras
1552639ae9bSBen Gras /* Get the parent page. */
1562639ae9bSBen Gras if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
1572639ae9bSBen Gras goto err2;
1582639ae9bSBen Gras
1592639ae9bSBen Gras /*
1602639ae9bSBen Gras * The new key goes ONE AFTER the index, because the split
1612639ae9bSBen Gras * was to the right.
1622639ae9bSBen Gras */
1632639ae9bSBen Gras skip = parent->index + 1;
1642639ae9bSBen Gras
1652639ae9bSBen Gras /*
1662639ae9bSBen Gras * Calculate the space needed on the parent page.
1672639ae9bSBen Gras *
1682639ae9bSBen Gras * Prefix trees: space hack when inserting into BINTERNAL
1692639ae9bSBen Gras * pages. Retain only what's needed to distinguish between
1702639ae9bSBen Gras * the new entry and the LAST entry on the page to its left.
1712639ae9bSBen Gras * If the keys compare equal, retain the entire key. Note,
1722639ae9bSBen Gras * we don't touch overflow keys, and the entire key must be
1732639ae9bSBen Gras * retained for the next-to-left most key on the leftmost
1742639ae9bSBen Gras * page of each level, or the search will fail. Applicable
1752639ae9bSBen Gras * ONLY to internal pages that have leaf pages as children.
1762639ae9bSBen Gras * Further reduction of the key between pairs of internal
1772639ae9bSBen Gras * pages loses too much information.
1782639ae9bSBen Gras */
1792639ae9bSBen Gras switch (rchild->flags & P_TYPE) {
1802639ae9bSBen Gras case P_BINTERNAL:
1812639ae9bSBen Gras bi = GETBINTERNAL(rchild, 0);
1822639ae9bSBen Gras nbytes = NBINTERNAL(bi->ksize);
1832639ae9bSBen Gras break;
1842639ae9bSBen Gras case P_BLEAF:
1852639ae9bSBen Gras bl = GETBLEAF(rchild, 0);
1862639ae9bSBen Gras nbytes = NBINTERNAL(bl->ksize);
1872639ae9bSBen Gras if (t->bt_pfx && !(bl->flags & P_BIGKEY) &&
1882639ae9bSBen Gras (h->prevpg != P_INVALID || skip > 1)) {
1892639ae9bSBen Gras size_t temp;
1902639ae9bSBen Gras tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1);
1912639ae9bSBen Gras a.size = tbl->ksize;
1922639ae9bSBen Gras a.data = tbl->bytes;
1932639ae9bSBen Gras b.size = bl->ksize;
1942639ae9bSBen Gras b.data = bl->bytes;
1952639ae9bSBen Gras temp = t->bt_pfx(&a, &b);
1962639ae9bSBen Gras _DBFIT(temp, uint32_t);
1972639ae9bSBen Gras nksize = (uint32_t)temp;
1982639ae9bSBen Gras n = NBINTERNAL(nksize);
1992639ae9bSBen Gras if (n < nbytes) {
2002639ae9bSBen Gras #ifdef STATISTICS
2012639ae9bSBen Gras bt_pfxsaved += nbytes - n;
2022639ae9bSBen Gras #endif
2032639ae9bSBen Gras nbytes = n;
2042639ae9bSBen Gras } else
2052639ae9bSBen Gras nksize = 0;
2062639ae9bSBen Gras } else
2072639ae9bSBen Gras nksize = 0;
2082639ae9bSBen Gras break;
2092639ae9bSBen Gras case P_RINTERNAL:
2102639ae9bSBen Gras case P_RLEAF:
2112639ae9bSBen Gras nbytes = NRINTERNAL;
2122639ae9bSBen Gras break;
2132639ae9bSBen Gras default:
2142639ae9bSBen Gras abort();
2152639ae9bSBen Gras }
2162639ae9bSBen Gras
2172639ae9bSBen Gras /* Split the parent page if necessary or shift the indices. */
2182639ae9bSBen Gras if ((uint32_t)h->upper - (uint32_t)h->lower < nbytes + sizeof(indx_t)) {
2192639ae9bSBen Gras sp = h;
2202639ae9bSBen Gras h = h->pgno == P_ROOT ?
2212639ae9bSBen Gras bt_root(t, h, &l, &r, &skip, nbytes) :
2222639ae9bSBen Gras bt_page(t, h, &l, &r, &skip, nbytes);
2232639ae9bSBen Gras if (h == NULL)
2242639ae9bSBen Gras goto err1;
2252639ae9bSBen Gras parentsplit = 1;
2262639ae9bSBen Gras } else {
2272639ae9bSBen Gras if (skip < (nxtindex = NEXTINDEX(h)))
2282639ae9bSBen Gras memmove(h->linp + skip + 1, h->linp + skip,
2292639ae9bSBen Gras (nxtindex - skip) * sizeof(indx_t));
2302639ae9bSBen Gras h->lower += sizeof(indx_t);
2312639ae9bSBen Gras parentsplit = 0;
2322639ae9bSBen Gras }
2332639ae9bSBen Gras
2342639ae9bSBen Gras /* Insert the key into the parent page. */
2352639ae9bSBen Gras switch (rchild->flags & P_TYPE) {
2362639ae9bSBen Gras case P_BINTERNAL:
2372639ae9bSBen Gras h->linp[skip] = h->upper -= nbytes;
2382639ae9bSBen Gras dest = (char *)(void *)h + h->linp[skip];
2392639ae9bSBen Gras memmove(dest, bi, nbytes);
2402639ae9bSBen Gras ((BINTERNAL *)(void *)dest)->pgno = rchild->pgno;
2412639ae9bSBen Gras break;
2422639ae9bSBen Gras case P_BLEAF:
2432639ae9bSBen Gras h->linp[skip] = h->upper -= nbytes;
2442639ae9bSBen Gras dest = (char *)(void *)h + h->linp[skip];
2452639ae9bSBen Gras WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
2462639ae9bSBen Gras rchild->pgno, bl->flags & P_BIGKEY);
2472639ae9bSBen Gras memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
248*f14fb602SLionel Sambuc if (bl->flags & P_BIGKEY) {
249*f14fb602SLionel Sambuc pgno_t pgno;
250*f14fb602SLionel Sambuc memcpy(&pgno, bl->bytes, sizeof(pgno));
251*f14fb602SLionel Sambuc if (bt_preserve(t, pgno) == RET_ERROR)
2522639ae9bSBen Gras goto err1;
253*f14fb602SLionel Sambuc }
2542639ae9bSBen Gras break;
2552639ae9bSBen Gras case P_RINTERNAL:
2562639ae9bSBen Gras /*
2572639ae9bSBen Gras * Update the left page count. If split
2582639ae9bSBen Gras * added at index 0, fix the correct page.
2592639ae9bSBen Gras */
2602639ae9bSBen Gras if (skip > 0)
2612639ae9bSBen Gras dest = (char *)(void *)h + h->linp[skip - 1];
2622639ae9bSBen Gras else
2632639ae9bSBen Gras dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1];
2642639ae9bSBen Gras ((RINTERNAL *)(void *)dest)->nrecs = rec_total(lchild);
2652639ae9bSBen Gras ((RINTERNAL *)(void *)dest)->pgno = lchild->pgno;
2662639ae9bSBen Gras
2672639ae9bSBen Gras /* Update the right page count. */
2682639ae9bSBen Gras h->linp[skip] = h->upper -= nbytes;
2692639ae9bSBen Gras dest = (char *)(void *)h + h->linp[skip];
2702639ae9bSBen Gras ((RINTERNAL *)(void *)dest)->nrecs = rec_total(rchild);
2712639ae9bSBen Gras ((RINTERNAL *)(void *)dest)->pgno = rchild->pgno;
2722639ae9bSBen Gras break;
2732639ae9bSBen Gras case P_RLEAF:
2742639ae9bSBen Gras /*
2752639ae9bSBen Gras * Update the left page count. If split
2762639ae9bSBen Gras * added at index 0, fix the correct page.
2772639ae9bSBen Gras */
2782639ae9bSBen Gras if (skip > 0)
2792639ae9bSBen Gras dest = (char *)(void *)h + h->linp[skip - 1];
2802639ae9bSBen Gras else
2812639ae9bSBen Gras dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1];
2822639ae9bSBen Gras ((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(lchild);
2832639ae9bSBen Gras ((RINTERNAL *)(void *)dest)->pgno = lchild->pgno;
2842639ae9bSBen Gras
2852639ae9bSBen Gras /* Update the right page count. */
2862639ae9bSBen Gras h->linp[skip] = h->upper -= nbytes;
2872639ae9bSBen Gras dest = (char *)(void *)h + h->linp[skip];
2882639ae9bSBen Gras ((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(rchild);
2892639ae9bSBen Gras ((RINTERNAL *)(void *)dest)->pgno = rchild->pgno;
2902639ae9bSBen Gras break;
2912639ae9bSBen Gras default:
2922639ae9bSBen Gras abort();
2932639ae9bSBen Gras }
2942639ae9bSBen Gras
2952639ae9bSBen Gras /* Unpin the held pages. */
2962639ae9bSBen Gras if (!parentsplit) {
2972639ae9bSBen Gras mpool_put(t->bt_mp, h, MPOOL_DIRTY);
2982639ae9bSBen Gras break;
2992639ae9bSBen Gras }
3002639ae9bSBen Gras
3012639ae9bSBen Gras /* If the root page was split, make it look right. */
3022639ae9bSBen Gras if (sp->pgno == P_ROOT &&
3032639ae9bSBen Gras (F_ISSET(t, R_RECNO) ?
3042639ae9bSBen Gras bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
3052639ae9bSBen Gras goto err1;
3062639ae9bSBen Gras
3072639ae9bSBen Gras mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
3082639ae9bSBen Gras mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
3092639ae9bSBen Gras }
3102639ae9bSBen Gras
3112639ae9bSBen Gras /* Unpin the held pages. */
3122639ae9bSBen Gras mpool_put(t->bt_mp, l, MPOOL_DIRTY);
3132639ae9bSBen Gras mpool_put(t->bt_mp, r, MPOOL_DIRTY);
3142639ae9bSBen Gras
3152639ae9bSBen Gras /* Clear any pages left on the stack. */
3162639ae9bSBen Gras return (RET_SUCCESS);
3172639ae9bSBen Gras
3182639ae9bSBen Gras /*
3192639ae9bSBen Gras * If something fails in the above loop we were already walking back
3202639ae9bSBen Gras * up the tree and the tree is now inconsistent. Nothing much we can
3212639ae9bSBen Gras * do about it but release any memory we're holding.
3222639ae9bSBen Gras */
3232639ae9bSBen Gras err1: mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
3242639ae9bSBen Gras mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
3252639ae9bSBen Gras
3262639ae9bSBen Gras err2: mpool_put(t->bt_mp, l, 0);
3272639ae9bSBen Gras mpool_put(t->bt_mp, r, 0);
3282639ae9bSBen Gras __dbpanic(t->bt_dbp);
3292639ae9bSBen Gras return (RET_ERROR);
3302639ae9bSBen Gras }
3312639ae9bSBen Gras
3322639ae9bSBen Gras /*
3332639ae9bSBen Gras * BT_PAGE -- Split a non-root page of a btree.
3342639ae9bSBen Gras *
3352639ae9bSBen Gras * Parameters:
3362639ae9bSBen Gras * t: tree
3372639ae9bSBen Gras * h: root page
3382639ae9bSBen Gras * lp: pointer to left page pointer
3392639ae9bSBen Gras * rp: pointer to right page pointer
3402639ae9bSBen Gras * skip: pointer to index to leave open
3412639ae9bSBen Gras * ilen: insert length
3422639ae9bSBen Gras *
3432639ae9bSBen Gras * Returns:
3442639ae9bSBen Gras * Pointer to page in which to insert or NULL on error.
3452639ae9bSBen Gras */
3462639ae9bSBen Gras static PAGE *
bt_page(BTREE * t,PAGE * h,PAGE ** lp,PAGE ** rp,indx_t * skip,size_t ilen)3472639ae9bSBen Gras bt_page(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
3482639ae9bSBen Gras {
3492639ae9bSBen Gras PAGE *l, *r, *tp;
3502639ae9bSBen Gras pgno_t npg;
3512639ae9bSBen Gras
3522639ae9bSBen Gras #ifdef STATISTICS
3532639ae9bSBen Gras ++bt_split;
3542639ae9bSBen Gras #endif
3552639ae9bSBen Gras /* Put the new right page for the split into place. */
3562639ae9bSBen Gras if ((r = __bt_new(t, &npg)) == NULL)
3572639ae9bSBen Gras return (NULL);
3582639ae9bSBen Gras r->pgno = npg;
3592639ae9bSBen Gras r->lower = BTDATAOFF;
3602639ae9bSBen Gras r->upper = t->bt_psize;
3612639ae9bSBen Gras r->nextpg = h->nextpg;
3622639ae9bSBen Gras r->prevpg = h->pgno;
3632639ae9bSBen Gras r->flags = h->flags & P_TYPE;
3642639ae9bSBen Gras
3652639ae9bSBen Gras /*
3662639ae9bSBen Gras * If we're splitting the last page on a level because we're appending
3672639ae9bSBen Gras * a key to it (skip is NEXTINDEX()), it's likely that the data is
3682639ae9bSBen Gras * sorted. Adding an empty page on the side of the level is less work
3692639ae9bSBen Gras * and can push the fill factor much higher than normal. If we're
3702639ae9bSBen Gras * wrong it's no big deal, we'll just do the split the right way next
3712639ae9bSBen Gras * time. It may look like it's equally easy to do a similar hack for
3722639ae9bSBen Gras * reverse sorted data, that is, split the tree left, but it's not.
3732639ae9bSBen Gras * Don't even try.
3742639ae9bSBen Gras */
3752639ae9bSBen Gras if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
3762639ae9bSBen Gras #ifdef STATISTICS
3772639ae9bSBen Gras ++bt_sortsplit;
3782639ae9bSBen Gras #endif
3792639ae9bSBen Gras h->nextpg = r->pgno;
3802639ae9bSBen Gras r->lower = BTDATAOFF + sizeof(indx_t);
3812639ae9bSBen Gras *skip = 0;
3822639ae9bSBen Gras *lp = h;
3832639ae9bSBen Gras *rp = r;
3842639ae9bSBen Gras return (r);
3852639ae9bSBen Gras }
3862639ae9bSBen Gras
3872639ae9bSBen Gras /* Put the new left page for the split into place. */
3882639ae9bSBen Gras if ((l = calloc(1, t->bt_psize)) == NULL) {
3892639ae9bSBen Gras mpool_put(t->bt_mp, r, 0);
3902639ae9bSBen Gras return (NULL);
3912639ae9bSBen Gras }
3922639ae9bSBen Gras #ifdef PURIFY
3932639ae9bSBen Gras memset(l, 0xff, t->bt_psize);
3942639ae9bSBen Gras #endif
3952639ae9bSBen Gras l->pgno = h->pgno;
3962639ae9bSBen Gras l->nextpg = r->pgno;
3972639ae9bSBen Gras l->prevpg = h->prevpg;
3982639ae9bSBen Gras l->lower = BTDATAOFF;
3992639ae9bSBen Gras l->upper = t->bt_psize;
4002639ae9bSBen Gras l->flags = h->flags & P_TYPE;
4012639ae9bSBen Gras
4022639ae9bSBen Gras /* Fix up the previous pointer of the page after the split page. */
4032639ae9bSBen Gras if (h->nextpg != P_INVALID) {
4042639ae9bSBen Gras if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) {
4052639ae9bSBen Gras free(l);
4062639ae9bSBen Gras /* XXX mpool_free(t->bt_mp, r->pgno); */
4072639ae9bSBen Gras return (NULL);
4082639ae9bSBen Gras }
4092639ae9bSBen Gras tp->prevpg = r->pgno;
4102639ae9bSBen Gras mpool_put(t->bt_mp, tp, MPOOL_DIRTY);
4112639ae9bSBen Gras }
4122639ae9bSBen Gras
4132639ae9bSBen Gras /*
4142639ae9bSBen Gras * Split right. The key/data pairs aren't sorted in the btree page so
4152639ae9bSBen Gras * it's simpler to copy the data from the split page onto two new pages
4162639ae9bSBen Gras * instead of copying half the data to the right page and compacting
4172639ae9bSBen Gras * the left page in place. Since the left page can't change, we have
4182639ae9bSBen Gras * to swap the original and the allocated left page after the split.
4192639ae9bSBen Gras */
4202639ae9bSBen Gras tp = bt_psplit(t, h, l, r, skip, ilen);
4212639ae9bSBen Gras
4222639ae9bSBen Gras /* Move the new left page onto the old left page. */
4232639ae9bSBen Gras memmove(h, l, t->bt_psize);
4242639ae9bSBen Gras if (tp == l)
4252639ae9bSBen Gras tp = h;
4262639ae9bSBen Gras free(l);
4272639ae9bSBen Gras
4282639ae9bSBen Gras *lp = h;
4292639ae9bSBen Gras *rp = r;
4302639ae9bSBen Gras return (tp);
4312639ae9bSBen Gras }
4322639ae9bSBen Gras
4332639ae9bSBen Gras /*
4342639ae9bSBen Gras * BT_ROOT -- Split the root page of a btree.
4352639ae9bSBen Gras *
4362639ae9bSBen Gras * Parameters:
4372639ae9bSBen Gras * t: tree
4382639ae9bSBen Gras * h: root page
4392639ae9bSBen Gras * lp: pointer to left page pointer
4402639ae9bSBen Gras * rp: pointer to right page pointer
4412639ae9bSBen Gras * skip: pointer to index to leave open
4422639ae9bSBen Gras * ilen: insert length
4432639ae9bSBen Gras *
4442639ae9bSBen Gras * Returns:
4452639ae9bSBen Gras * Pointer to page in which to insert or NULL on error.
4462639ae9bSBen Gras */
4472639ae9bSBen Gras static PAGE *
bt_root(BTREE * t,PAGE * h,PAGE ** lp,PAGE ** rp,indx_t * skip,size_t ilen)4482639ae9bSBen Gras bt_root(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
4492639ae9bSBen Gras {
4502639ae9bSBen Gras PAGE *l, *r, *tp;
4512639ae9bSBen Gras pgno_t lnpg, rnpg;
4522639ae9bSBen Gras
4532639ae9bSBen Gras #ifdef STATISTICS
4542639ae9bSBen Gras ++bt_split;
4552639ae9bSBen Gras ++bt_rootsplit;
4562639ae9bSBen Gras #endif
4572639ae9bSBen Gras /* Put the new left and right pages for the split into place. */
4582639ae9bSBen Gras if ((l = __bt_new(t, &lnpg)) == NULL ||
4592639ae9bSBen Gras (r = __bt_new(t, &rnpg)) == NULL)
4602639ae9bSBen Gras return (NULL);
4612639ae9bSBen Gras l->pgno = lnpg;
4622639ae9bSBen Gras r->pgno = rnpg;
4632639ae9bSBen Gras l->nextpg = r->pgno;
4642639ae9bSBen Gras r->prevpg = l->pgno;
4652639ae9bSBen Gras l->prevpg = r->nextpg = P_INVALID;
4662639ae9bSBen Gras l->lower = r->lower = BTDATAOFF;
4672639ae9bSBen Gras l->upper = r->upper = t->bt_psize;
4682639ae9bSBen Gras l->flags = r->flags = h->flags & P_TYPE;
4692639ae9bSBen Gras
4702639ae9bSBen Gras /* Split the root page. */
4712639ae9bSBen Gras tp = bt_psplit(t, h, l, r, skip, ilen);
4722639ae9bSBen Gras
4732639ae9bSBen Gras *lp = l;
4742639ae9bSBen Gras *rp = r;
4752639ae9bSBen Gras return (tp);
4762639ae9bSBen Gras }
4772639ae9bSBen Gras
4782639ae9bSBen Gras /*
4792639ae9bSBen Gras * BT_RROOT -- Fix up the recno root page after it has been split.
4802639ae9bSBen Gras *
4812639ae9bSBen Gras * Parameters:
4822639ae9bSBen Gras * t: tree
4832639ae9bSBen Gras * h: root page
4842639ae9bSBen Gras * l: left page
4852639ae9bSBen Gras * r: right page
4862639ae9bSBen Gras *
4872639ae9bSBen Gras * Returns:
4882639ae9bSBen Gras * RET_ERROR, RET_SUCCESS
4892639ae9bSBen Gras */
4902639ae9bSBen Gras static int
bt_rroot(BTREE * t,PAGE * h,PAGE * l,PAGE * r)4912639ae9bSBen Gras bt_rroot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
4922639ae9bSBen Gras {
4932639ae9bSBen Gras char *dest;
4942639ae9bSBen Gras uint32_t sz;
4952639ae9bSBen Gras size_t temp;
4962639ae9bSBen Gras
4972639ae9bSBen Gras temp = t->bt_psize - NRINTERNAL;
4982639ae9bSBen Gras _DBFIT(temp, uint32_t);
4992639ae9bSBen Gras sz = (uint32_t)temp;
5002639ae9bSBen Gras
5012639ae9bSBen Gras /* Insert the left and right keys, set the header information. */
5022639ae9bSBen Gras _DBFIT(sz, indx_t);
5032639ae9bSBen Gras h->linp[0] = h->upper = (indx_t)sz;
5042639ae9bSBen Gras dest = (char *)(void *)h + h->upper;
5052639ae9bSBen Gras WR_RINTERNAL(dest,
5062639ae9bSBen Gras l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno);
5072639ae9bSBen Gras
5082639ae9bSBen Gras h->linp[1] = h->upper -= NRINTERNAL;
5092639ae9bSBen Gras dest = (char *)(void *)h + h->upper;
5102639ae9bSBen Gras WR_RINTERNAL(dest,
5112639ae9bSBen Gras r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno);
5122639ae9bSBen Gras
5132639ae9bSBen Gras h->lower = BTDATAOFF + 2 * sizeof(indx_t);
5142639ae9bSBen Gras
5152639ae9bSBen Gras /* Unpin the root page, set to recno internal page. */
5162639ae9bSBen Gras h->flags &= ~P_TYPE;
5172639ae9bSBen Gras h->flags |= P_RINTERNAL;
5182639ae9bSBen Gras mpool_put(t->bt_mp, h, MPOOL_DIRTY);
5192639ae9bSBen Gras
5202639ae9bSBen Gras return (RET_SUCCESS);
5212639ae9bSBen Gras }
5222639ae9bSBen Gras
5232639ae9bSBen Gras /*
5242639ae9bSBen Gras * BT_BROOT -- Fix up the btree root page after it has been split.
5252639ae9bSBen Gras *
5262639ae9bSBen Gras * Parameters:
5272639ae9bSBen Gras * t: tree
5282639ae9bSBen Gras * h: root page
5292639ae9bSBen Gras * l: left page
5302639ae9bSBen Gras * r: right page
5312639ae9bSBen Gras *
5322639ae9bSBen Gras * Returns:
5332639ae9bSBen Gras * RET_ERROR, RET_SUCCESS
5342639ae9bSBen Gras */
5352639ae9bSBen Gras static int
bt_broot(BTREE * t,PAGE * h,PAGE * l,PAGE * r)5362639ae9bSBen Gras bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
5372639ae9bSBen Gras {
5382639ae9bSBen Gras BINTERNAL *bi = NULL; /* pacify gcc */
5392639ae9bSBen Gras BLEAF *bl;
5402639ae9bSBen Gras uint32_t nbytes;
5412639ae9bSBen Gras char *dest;
5422639ae9bSBen Gras
5432639ae9bSBen Gras /*
5442639ae9bSBen Gras * If the root page was a leaf page, change it into an internal page.
5452639ae9bSBen Gras * We copy the key we split on (but not the key's data, in the case of
5462639ae9bSBen Gras * a leaf page) to the new root page.
5472639ae9bSBen Gras *
5482639ae9bSBen Gras * The btree comparison code guarantees that the left-most key on any
5492639ae9bSBen Gras * level of the tree is never used, so it doesn't need to be filled in.
5502639ae9bSBen Gras */
5512639ae9bSBen Gras nbytes = NBINTERNAL(0);
5522639ae9bSBen Gras h->linp[0] = h->upper = t->bt_psize - nbytes;
5532639ae9bSBen Gras dest = (char *)(void *)h + h->upper;
5542639ae9bSBen Gras WR_BINTERNAL(dest, 0, l->pgno, 0);
5552639ae9bSBen Gras
5562639ae9bSBen Gras switch (h->flags & P_TYPE) {
5572639ae9bSBen Gras case P_BLEAF:
5582639ae9bSBen Gras bl = GETBLEAF(r, 0);
5592639ae9bSBen Gras nbytes = NBINTERNAL(bl->ksize);
5602639ae9bSBen Gras h->linp[1] = h->upper -= nbytes;
5612639ae9bSBen Gras dest = (char *)(void *)h + h->upper;
5622639ae9bSBen Gras WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
5632639ae9bSBen Gras memmove(dest, bl->bytes, bl->ksize);
5642639ae9bSBen Gras
5652639ae9bSBen Gras /*
5662639ae9bSBen Gras * If the key is on an overflow page, mark the overflow chain
5672639ae9bSBen Gras * so it isn't deleted when the leaf copy of the key is deleted.
5682639ae9bSBen Gras */
569*f14fb602SLionel Sambuc if (bl->flags & P_BIGKEY) {
570*f14fb602SLionel Sambuc pgno_t pgno;
571*f14fb602SLionel Sambuc memcpy(&pgno, bl->bytes, sizeof(pgno));
572*f14fb602SLionel Sambuc if (bt_preserve(t, pgno) == RET_ERROR)
5732639ae9bSBen Gras return (RET_ERROR);
574*f14fb602SLionel Sambuc }
5752639ae9bSBen Gras break;
5762639ae9bSBen Gras case P_BINTERNAL:
5772639ae9bSBen Gras bi = GETBINTERNAL(r, 0);
5782639ae9bSBen Gras nbytes = NBINTERNAL(bi->ksize);
5792639ae9bSBen Gras h->linp[1] = h->upper -= nbytes;
5802639ae9bSBen Gras dest = (char *)(void *)h + h->upper;
5812639ae9bSBen Gras memmove(dest, bi, nbytes);
5822639ae9bSBen Gras ((BINTERNAL *)(void *)dest)->pgno = r->pgno;
5832639ae9bSBen Gras break;
5842639ae9bSBen Gras default:
5852639ae9bSBen Gras abort();
5862639ae9bSBen Gras }
5872639ae9bSBen Gras
5882639ae9bSBen Gras /* There are two keys on the page. */
5892639ae9bSBen Gras h->lower = BTDATAOFF + 2 * sizeof(indx_t);
5902639ae9bSBen Gras
5912639ae9bSBen Gras /* Unpin the root page, set to btree internal page. */
5922639ae9bSBen Gras h->flags &= ~P_TYPE;
5932639ae9bSBen Gras h->flags |= P_BINTERNAL;
5942639ae9bSBen Gras mpool_put(t->bt_mp, h, MPOOL_DIRTY);
5952639ae9bSBen Gras
5962639ae9bSBen Gras return (RET_SUCCESS);
5972639ae9bSBen Gras }
5982639ae9bSBen Gras
5992639ae9bSBen Gras /*
6002639ae9bSBen Gras * BT_PSPLIT -- Do the real work of splitting the page.
6012639ae9bSBen Gras *
6022639ae9bSBen Gras * Parameters:
6032639ae9bSBen Gras * t: tree
6042639ae9bSBen Gras * h: page to be split
6052639ae9bSBen Gras * l: page to put lower half of data
6062639ae9bSBen Gras * r: page to put upper half of data
6072639ae9bSBen Gras * pskip: pointer to index to leave open
6082639ae9bSBen Gras * ilen: insert length
6092639ae9bSBen Gras *
6102639ae9bSBen Gras * Returns:
6112639ae9bSBen Gras * Pointer to page in which to insert.
6122639ae9bSBen Gras */
6132639ae9bSBen Gras static PAGE *
bt_psplit(BTREE * t,PAGE * h,PAGE * l,PAGE * r,indx_t * pskip,size_t ilen)6142639ae9bSBen Gras bt_psplit(BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen)
6152639ae9bSBen Gras {
6162639ae9bSBen Gras BINTERNAL *bi;
6172639ae9bSBen Gras BLEAF *bl;
6182639ae9bSBen Gras CURSOR *c;
6192639ae9bSBen Gras RLEAF *rl;
6202639ae9bSBen Gras PAGE *rval;
6212639ae9bSBen Gras void *src = NULL; /* pacify gcc */
6222639ae9bSBen Gras indx_t full, half, nxt, off, skip, top, used;
6232639ae9bSBen Gras uint32_t nbytes;
6242639ae9bSBen Gras size_t temp;
6252639ae9bSBen Gras int bigkeycnt, isbigkey;
6262639ae9bSBen Gras
6272639ae9bSBen Gras /*
6282639ae9bSBen Gras * Split the data to the left and right pages. Leave the skip index
6292639ae9bSBen Gras * open. Additionally, make some effort not to split on an overflow
6302639ae9bSBen Gras * key. This makes internal page processing faster and can save
6312639ae9bSBen Gras * space as overflow keys used by internal pages are never deleted.
6322639ae9bSBen Gras */
6332639ae9bSBen Gras bigkeycnt = 0;
6342639ae9bSBen Gras skip = *pskip;
6352639ae9bSBen Gras temp = t->bt_psize - BTDATAOFF;
6362639ae9bSBen Gras _DBFIT(temp, indx_t);
6372639ae9bSBen Gras full = (indx_t)temp;
6382639ae9bSBen Gras half = full / 2;
6392639ae9bSBen Gras used = 0;
6402639ae9bSBen Gras for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) {
6412639ae9bSBen Gras if (skip == off) {
6422639ae9bSBen Gras _DBFIT(ilen, uint32_t);
6432639ae9bSBen Gras nbytes = (uint32_t)ilen;
6442639ae9bSBen Gras isbigkey = 0; /* XXX: not really known. */
6452639ae9bSBen Gras } else
6462639ae9bSBen Gras switch (h->flags & P_TYPE) {
6472639ae9bSBen Gras case P_BINTERNAL:
6482639ae9bSBen Gras src = bi = GETBINTERNAL(h, nxt);
6492639ae9bSBen Gras nbytes = NBINTERNAL(bi->ksize);
6502639ae9bSBen Gras isbigkey = bi->flags & P_BIGKEY;
6512639ae9bSBen Gras break;
6522639ae9bSBen Gras case P_BLEAF:
6532639ae9bSBen Gras src = bl = GETBLEAF(h, nxt);
6542639ae9bSBen Gras nbytes = NBLEAF(bl);
6552639ae9bSBen Gras isbigkey = bl->flags & P_BIGKEY;
6562639ae9bSBen Gras break;
6572639ae9bSBen Gras case P_RINTERNAL:
6582639ae9bSBen Gras src = GETRINTERNAL(h, nxt);
6592639ae9bSBen Gras nbytes = NRINTERNAL;
6602639ae9bSBen Gras isbigkey = 0;
6612639ae9bSBen Gras break;
6622639ae9bSBen Gras case P_RLEAF:
6632639ae9bSBen Gras src = rl = GETRLEAF(h, nxt);
6642639ae9bSBen Gras nbytes = NRLEAF(rl);
6652639ae9bSBen Gras isbigkey = 0;
6662639ae9bSBen Gras break;
6672639ae9bSBen Gras default:
6682639ae9bSBen Gras abort();
6692639ae9bSBen Gras }
6702639ae9bSBen Gras
6712639ae9bSBen Gras /*
6722639ae9bSBen Gras * If the key/data pairs are substantial fractions of the max
6732639ae9bSBen Gras * possible size for the page, it's possible to get situations
6742639ae9bSBen Gras * where we decide to try and copy too much onto the left page.
6752639ae9bSBen Gras * Make sure that doesn't happen.
6762639ae9bSBen Gras */
6772639ae9bSBen Gras if ((skip <= off && used + nbytes + sizeof(indx_t) >= full) ||
6782639ae9bSBen Gras nxt == top - 1) {
6792639ae9bSBen Gras --off;
6802639ae9bSBen Gras break;
6812639ae9bSBen Gras }
6822639ae9bSBen Gras
6832639ae9bSBen Gras /* Copy the key/data pair, if not the skipped index. */
6842639ae9bSBen Gras if (skip != off) {
6852639ae9bSBen Gras ++nxt;
6862639ae9bSBen Gras
6872639ae9bSBen Gras l->linp[off] = l->upper -= nbytes;
6882639ae9bSBen Gras memmove((char *)(void *)l + l->upper, src, nbytes);
6892639ae9bSBen Gras }
6902639ae9bSBen Gras
6912639ae9bSBen Gras temp = nbytes + sizeof(indx_t);
6922639ae9bSBen Gras _DBFIT(temp, indx_t);
6932639ae9bSBen Gras used += (indx_t)temp;
6942639ae9bSBen Gras if (used >= half) {
6952639ae9bSBen Gras if (!isbigkey || bigkeycnt == 3)
6962639ae9bSBen Gras break;
6972639ae9bSBen Gras else
6982639ae9bSBen Gras ++bigkeycnt;
6992639ae9bSBen Gras }
7002639ae9bSBen Gras }
7012639ae9bSBen Gras
7022639ae9bSBen Gras /*
7032639ae9bSBen Gras * Off is the last offset that's valid for the left page.
7042639ae9bSBen Gras * Nxt is the first offset to be placed on the right page.
7052639ae9bSBen Gras */
7062639ae9bSBen Gras temp = (off + 1) * sizeof(indx_t);
7072639ae9bSBen Gras _DBFIT(temp, indx_t);
7082639ae9bSBen Gras l->lower += (indx_t)temp;
7092639ae9bSBen Gras
7102639ae9bSBen Gras /*
7112639ae9bSBen Gras * If splitting the page that the cursor was on, the cursor has to be
7122639ae9bSBen Gras * adjusted to point to the same record as before the split. If the
7132639ae9bSBen Gras * cursor is at or past the skipped slot, the cursor is incremented by
7142639ae9bSBen Gras * one. If the cursor is on the right page, it is decremented by the
7152639ae9bSBen Gras * number of records split to the left page.
7162639ae9bSBen Gras */
7172639ae9bSBen Gras c = &t->bt_cursor;
7182639ae9bSBen Gras if (F_ISSET(c, CURS_INIT) && c->pg.pgno == h->pgno) {
7192639ae9bSBen Gras if (c->pg.index >= skip)
7202639ae9bSBen Gras ++c->pg.index;
7212639ae9bSBen Gras if (c->pg.index < nxt) /* Left page. */
7222639ae9bSBen Gras c->pg.pgno = l->pgno;
7232639ae9bSBen Gras else { /* Right page. */
7242639ae9bSBen Gras c->pg.pgno = r->pgno;
7252639ae9bSBen Gras c->pg.index -= nxt;
7262639ae9bSBen Gras }
7272639ae9bSBen Gras }
7282639ae9bSBen Gras
7292639ae9bSBen Gras /*
7302639ae9bSBen Gras * If the skipped index was on the left page, just return that page.
7312639ae9bSBen Gras * Otherwise, adjust the skip index to reflect the new position on
7322639ae9bSBen Gras * the right page.
7332639ae9bSBen Gras */
7342639ae9bSBen Gras if (skip <= off) {
7352639ae9bSBen Gras skip = MAX_PAGE_OFFSET;
7362639ae9bSBen Gras rval = l;
7372639ae9bSBen Gras } else {
7382639ae9bSBen Gras rval = r;
7392639ae9bSBen Gras *pskip -= nxt;
7402639ae9bSBen Gras }
7412639ae9bSBen Gras
7422639ae9bSBen Gras for (off = 0; nxt < top; ++off) {
7432639ae9bSBen Gras if (skip == nxt) {
7442639ae9bSBen Gras ++off;
7452639ae9bSBen Gras skip = MAX_PAGE_OFFSET;
7462639ae9bSBen Gras }
7472639ae9bSBen Gras switch (h->flags & P_TYPE) {
7482639ae9bSBen Gras case P_BINTERNAL:
7492639ae9bSBen Gras src = bi = GETBINTERNAL(h, nxt);
7502639ae9bSBen Gras nbytes = NBINTERNAL(bi->ksize);
7512639ae9bSBen Gras break;
7522639ae9bSBen Gras case P_BLEAF:
7532639ae9bSBen Gras src = bl = GETBLEAF(h, nxt);
7542639ae9bSBen Gras nbytes = NBLEAF(bl);
7552639ae9bSBen Gras break;
7562639ae9bSBen Gras case P_RINTERNAL:
7572639ae9bSBen Gras src = GETRINTERNAL(h, nxt);
7582639ae9bSBen Gras nbytes = NRINTERNAL;
7592639ae9bSBen Gras break;
7602639ae9bSBen Gras case P_RLEAF:
7612639ae9bSBen Gras src = rl = GETRLEAF(h, nxt);
7622639ae9bSBen Gras nbytes = NRLEAF(rl);
7632639ae9bSBen Gras break;
7642639ae9bSBen Gras default:
7652639ae9bSBen Gras abort();
7662639ae9bSBen Gras }
7672639ae9bSBen Gras ++nxt;
7682639ae9bSBen Gras r->linp[off] = r->upper -= nbytes;
7692639ae9bSBen Gras memmove((char *)(void *)r + r->upper, src, nbytes);
7702639ae9bSBen Gras }
7712639ae9bSBen Gras temp = off * sizeof(indx_t);
7722639ae9bSBen Gras _DBFIT(temp, indx_t);
7732639ae9bSBen Gras r->lower += (indx_t)temp;
7742639ae9bSBen Gras
7752639ae9bSBen Gras /* If the key is being appended to the page, adjust the index. */
7762639ae9bSBen Gras if (skip == top)
7772639ae9bSBen Gras r->lower += sizeof(indx_t);
7782639ae9bSBen Gras
7792639ae9bSBen Gras return (rval);
7802639ae9bSBen Gras }
7812639ae9bSBen Gras
7822639ae9bSBen Gras /*
7832639ae9bSBen Gras * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
7842639ae9bSBen Gras *
7852639ae9bSBen Gras * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
7862639ae9bSBen Gras * record that references them gets deleted. Chains pointed to by internal
7872639ae9bSBen Gras * pages never get deleted. This routine marks a chain as pointed to by an
7882639ae9bSBen Gras * internal page.
7892639ae9bSBen Gras *
7902639ae9bSBen Gras * Parameters:
7912639ae9bSBen Gras * t: tree
7922639ae9bSBen Gras * pg: page number of first page in the chain.
7932639ae9bSBen Gras *
7942639ae9bSBen Gras * Returns:
7952639ae9bSBen Gras * RET_SUCCESS, RET_ERROR.
7962639ae9bSBen Gras */
7972639ae9bSBen Gras static int
bt_preserve(BTREE * t,pgno_t pg)7982639ae9bSBen Gras bt_preserve(BTREE *t, pgno_t pg)
7992639ae9bSBen Gras {
8002639ae9bSBen Gras PAGE *h;
8012639ae9bSBen Gras
8022639ae9bSBen Gras if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
8032639ae9bSBen Gras return (RET_ERROR);
8042639ae9bSBen Gras h->flags |= P_PRESERVE;
8052639ae9bSBen Gras mpool_put(t->bt_mp, h, MPOOL_DIRTY);
8062639ae9bSBen Gras return (RET_SUCCESS);
8072639ae9bSBen Gras }
8082639ae9bSBen Gras
8092639ae9bSBen Gras /*
8102639ae9bSBen Gras * REC_TOTAL -- Return the number of recno entries below a page.
8112639ae9bSBen Gras *
8122639ae9bSBen Gras * Parameters:
8132639ae9bSBen Gras * h: page
8142639ae9bSBen Gras *
8152639ae9bSBen Gras * Returns:
8162639ae9bSBen Gras * The number of recno entries below a page.
8172639ae9bSBen Gras *
8182639ae9bSBen Gras * XXX
8192639ae9bSBen Gras * These values could be set by the bt_psplit routine. The problem is that the
8202639ae9bSBen Gras * entry has to be popped off of the stack etc. or the values have to be passed
8212639ae9bSBen Gras * all the way back to bt_split/bt_rroot and it's not very clean.
8222639ae9bSBen Gras */
8232639ae9bSBen Gras static recno_t
rec_total(PAGE * h)8242639ae9bSBen Gras rec_total(PAGE *h)
8252639ae9bSBen Gras {
8262639ae9bSBen Gras recno_t recs;
8272639ae9bSBen Gras indx_t nxt, top;
8282639ae9bSBen Gras
8292639ae9bSBen Gras for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
8302639ae9bSBen Gras recs += GETRINTERNAL(h, nxt)->nrecs;
8312639ae9bSBen Gras return (recs);
8322639ae9bSBen Gras }
833