Home | History | Annotate | Line # | Download | only in btree
bt_split.c revision 1.16
      1  1.16     joerg /*	$NetBSD: bt_split.c,v 1.16 2008/09/10 17:52:35 joerg Exp $	*/
      2   1.5       cgd 
      3   1.1       cgd /*-
      4   1.4       cgd  * Copyright (c) 1990, 1993, 1994
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Mike Olson.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.13       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35   1.7  christos #include <sys/cdefs.h>
     36  1.16     joerg __RCSID("$NetBSD: bt_split.c,v 1.16 2008/09/10 17:52:35 joerg Exp $");
     37   1.1       cgd 
     38   1.8       jtc #include "namespace.h"
     39   1.1       cgd #include <sys/types.h>
     40   1.1       cgd 
     41  1.14  christos #include <assert.h>
     42   1.1       cgd #include <limits.h>
     43   1.1       cgd #include <stdio.h>
     44   1.1       cgd #include <stdlib.h>
     45   1.1       cgd #include <string.h>
     46   1.1       cgd 
     47   1.1       cgd #include <db.h>
     48   1.1       cgd #include "btree.h"
     49   1.1       cgd 
     50  1.14  christos static int	 bt_broot(BTREE *, PAGE *, PAGE *, PAGE *);
     51  1.14  christos static PAGE	*bt_page(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
     52  1.14  christos static int	 bt_preserve(BTREE *, pgno_t);
     53  1.14  christos static PAGE	*bt_psplit(BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t);
     54  1.14  christos static PAGE	*bt_root(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
     55  1.14  christos static int	 bt_rroot(BTREE *, PAGE *, PAGE *, PAGE *);
     56  1.14  christos static recno_t	 rec_total(PAGE *);
     57   1.1       cgd 
     58   1.1       cgd #ifdef STATISTICS
     59  1.15     joerg unsigned long	bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved;
     60   1.1       cgd #endif
     61   1.1       cgd 
     62   1.1       cgd /*
     63   1.1       cgd  * __BT_SPLIT -- Split the tree.
     64   1.1       cgd  *
     65   1.1       cgd  * Parameters:
     66   1.1       cgd  *	t:	tree
     67   1.1       cgd  *	sp:	page to split
     68   1.1       cgd  *	key:	key to insert
     69   1.1       cgd  *	data:	data to insert
     70   1.1       cgd  *	flags:	BIGKEY/BIGDATA flags
     71   1.1       cgd  *	ilen:	insert length
     72   1.1       cgd  *	skip:	index to leave open
     73   1.1       cgd  *
     74   1.1       cgd  * Returns:
     75   1.1       cgd  *	RET_ERROR, RET_SUCCESS
     76   1.1       cgd  */
     77   1.1       cgd int
     78  1.14  christos __bt_split(BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags,
     79  1.15     joerg     size_t ilen, uint32_t argskip)
     80   1.1       cgd {
     81   1.7  christos 	BINTERNAL *bi = NULL;	/* pacify gcc */
     82   1.7  christos 	BLEAF *bl = NULL, *tbl;	/* pacify gcc */
     83   1.1       cgd 	DBT a, b;
     84   1.1       cgd 	EPGNO *parent;
     85   1.1       cgd 	PAGE *h, *l, *r, *lchild, *rchild;
     86   1.1       cgd 	indx_t nxtindex;
     87  1.15     joerg 	uint16_t skip;
     88  1.15     joerg 	uint32_t n, nbytes, nksize = 0; /* pacify gcc */
     89   1.1       cgd 	int parentsplit;
     90   1.1       cgd 	char *dest;
     91   1.1       cgd 
     92   1.1       cgd 	/*
     93   1.1       cgd 	 * Split the page into two pages, l and r.  The split routines return
     94   1.1       cgd 	 * a pointer to the page into which the key should be inserted and with
     95   1.1       cgd 	 * skip set to the offset which should be used.  Additionally, l and r
     96   1.1       cgd 	 * are pinned.
     97   1.1       cgd 	 */
     98   1.4       cgd 	skip = argskip;
     99   1.1       cgd 	h = sp->pgno == P_ROOT ?
    100   1.1       cgd 	    bt_root(t, sp, &l, &r, &skip, ilen) :
    101   1.1       cgd 	    bt_page(t, sp, &l, &r, &skip, ilen);
    102   1.1       cgd 	if (h == NULL)
    103   1.1       cgd 		return (RET_ERROR);
    104   1.1       cgd 
    105   1.1       cgd 	/*
    106   1.1       cgd 	 * Insert the new key/data pair into the leaf page.  (Key inserts
    107   1.1       cgd 	 * always cause a leaf page to split first.)
    108   1.1       cgd 	 */
    109  1.14  christos 	_DBFIT(ilen, indx_t);
    110  1.14  christos 	h->upper -= (indx_t)ilen;
    111  1.14  christos 	h->linp[skip] = h->upper;
    112  1.10  christos 	dest = (char *)(void *)h + h->upper;
    113   1.6       cgd 	if (F_ISSET(t, R_RECNO))
    114  1.14  christos 		WR_RLEAF(dest, data, flags);
    115   1.1       cgd 	else
    116  1.14  christos 		WR_BLEAF(dest, key, data, flags);
    117   1.1       cgd 
    118   1.1       cgd 	/* If the root page was split, make it look right. */
    119   1.1       cgd 	if (sp->pgno == P_ROOT &&
    120   1.6       cgd 	    (F_ISSET(t, R_RECNO) ?
    121   1.1       cgd 	    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
    122   1.1       cgd 		goto err2;
    123   1.1       cgd 
    124   1.1       cgd 	/*
    125   1.1       cgd 	 * Now we walk the parent page stack -- a LIFO stack of the pages that
    126   1.1       cgd 	 * were traversed when we searched for the page that split.  Each stack
    127   1.1       cgd 	 * entry is a page number and a page index offset.  The offset is for
    128   1.1       cgd 	 * the page traversed on the search.  We've just split a page, so we
    129   1.1       cgd 	 * have to insert a new key into the parent page.
    130   1.1       cgd 	 *
    131   1.1       cgd 	 * If the insert into the parent page causes it to split, may have to
    132   1.1       cgd 	 * continue splitting all the way up the tree.  We stop if the root
    133   1.1       cgd 	 * splits or the page inserted into didn't have to split to hold the
    134   1.1       cgd 	 * new key.  Some algorithms replace the key for the old page as well
    135   1.1       cgd 	 * as the new page.  We don't, as there's no reason to believe that the
    136   1.1       cgd 	 * first key on the old page is any better than the key we have, and,
    137   1.1       cgd 	 * in the case of a key being placed at index 0 causing the split, the
    138   1.1       cgd 	 * key is unavailable.
    139   1.1       cgd 	 *
    140   1.1       cgd 	 * There are a maximum of 5 pages pinned at any time.  We keep the left
    141   1.1       cgd 	 * and right pages pinned while working on the parent.   The 5 are the
    142   1.1       cgd 	 * two children, left parent and right parent (when the parent splits)
    143   1.1       cgd 	 * and the root page or the overflow key page when calling bt_preserve.
    144   1.1       cgd 	 * This code must make sure that all pins are released other than the
    145   1.1       cgd 	 * root page or overflow page which is unlocked elsewhere.
    146   1.1       cgd 	 */
    147   1.1       cgd 	while ((parent = BT_POP(t)) != NULL) {
    148   1.1       cgd 		lchild = l;
    149   1.1       cgd 		rchild = r;
    150   1.1       cgd 
    151   1.1       cgd 		/* Get the parent page. */
    152   1.1       cgd 		if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
    153   1.1       cgd 			goto err2;
    154   1.1       cgd 
    155   1.1       cgd 	 	/*
    156   1.1       cgd 		 * The new key goes ONE AFTER the index, because the split
    157   1.1       cgd 		 * was to the right.
    158   1.1       cgd 		 */
    159   1.1       cgd 		skip = parent->index + 1;
    160   1.1       cgd 
    161   1.1       cgd 		/*
    162   1.1       cgd 		 * Calculate the space needed on the parent page.
    163   1.1       cgd 		 *
    164   1.1       cgd 		 * Prefix trees: space hack when inserting into BINTERNAL
    165   1.1       cgd 		 * pages.  Retain only what's needed to distinguish between
    166   1.1       cgd 		 * the new entry and the LAST entry on the page to its left.
    167   1.1       cgd 		 * If the keys compare equal, retain the entire key.  Note,
    168   1.1       cgd 		 * we don't touch overflow keys, and the entire key must be
    169   1.1       cgd 		 * retained for the next-to-left most key on the leftmost
    170   1.1       cgd 		 * page of each level, or the search will fail.  Applicable
    171   1.1       cgd 		 * ONLY to internal pages that have leaf pages as children.
    172   1.1       cgd 		 * Further reduction of the key between pairs of internal
    173   1.1       cgd 		 * pages loses too much information.
    174   1.1       cgd 		 */
    175   1.1       cgd 		switch (rchild->flags & P_TYPE) {
    176   1.1       cgd 		case P_BINTERNAL:
    177   1.1       cgd 			bi = GETBINTERNAL(rchild, 0);
    178   1.1       cgd 			nbytes = NBINTERNAL(bi->ksize);
    179   1.1       cgd 			break;
    180   1.1       cgd 		case P_BLEAF:
    181   1.1       cgd 			bl = GETBLEAF(rchild, 0);
    182   1.1       cgd 			nbytes = NBINTERNAL(bl->ksize);
    183   1.1       cgd 			if (t->bt_pfx && !(bl->flags & P_BIGKEY) &&
    184   1.1       cgd 			    (h->prevpg != P_INVALID || skip > 1)) {
    185  1.14  christos 				size_t temp;
    186   1.1       cgd 				tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1);
    187   1.1       cgd 				a.size = tbl->ksize;
    188   1.1       cgd 				a.data = tbl->bytes;
    189   1.1       cgd 				b.size = bl->ksize;
    190   1.1       cgd 				b.data = bl->bytes;
    191  1.14  christos 				temp = t->bt_pfx(&a, &b);
    192  1.15     joerg 				_DBFIT(temp, uint32_t);
    193  1.15     joerg 				nksize = (uint32_t)temp;
    194   1.1       cgd 				n = NBINTERNAL(nksize);
    195   1.1       cgd 				if (n < nbytes) {
    196   1.1       cgd #ifdef STATISTICS
    197   1.1       cgd 					bt_pfxsaved += nbytes - n;
    198   1.1       cgd #endif
    199   1.1       cgd 					nbytes = n;
    200   1.1       cgd 				} else
    201   1.1       cgd 					nksize = 0;
    202   1.1       cgd 			} else
    203   1.1       cgd 				nksize = 0;
    204   1.1       cgd 			break;
    205   1.1       cgd 		case P_RINTERNAL:
    206   1.1       cgd 		case P_RLEAF:
    207   1.1       cgd 			nbytes = NRINTERNAL;
    208   1.1       cgd 			break;
    209   1.1       cgd 		default:
    210   1.1       cgd 			abort();
    211   1.1       cgd 		}
    212   1.1       cgd 
    213   1.1       cgd 		/* Split the parent page if necessary or shift the indices. */
    214   1.1       cgd 		if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
    215   1.1       cgd 			sp = h;
    216   1.1       cgd 			h = h->pgno == P_ROOT ?
    217   1.1       cgd 			    bt_root(t, h, &l, &r, &skip, nbytes) :
    218   1.1       cgd 			    bt_page(t, h, &l, &r, &skip, nbytes);
    219   1.1       cgd 			if (h == NULL)
    220   1.1       cgd 				goto err1;
    221   1.1       cgd 			parentsplit = 1;
    222   1.1       cgd 		} else {
    223   1.1       cgd 			if (skip < (nxtindex = NEXTINDEX(h)))
    224   1.1       cgd 				memmove(h->linp + skip + 1, h->linp + skip,
    225   1.1       cgd 				    (nxtindex - skip) * sizeof(indx_t));
    226   1.1       cgd 			h->lower += sizeof(indx_t);
    227   1.1       cgd 			parentsplit = 0;
    228   1.1       cgd 		}
    229   1.1       cgd 
    230   1.1       cgd 		/* Insert the key into the parent page. */
    231   1.6       cgd 		switch (rchild->flags & P_TYPE) {
    232   1.1       cgd 		case P_BINTERNAL:
    233   1.1       cgd 			h->linp[skip] = h->upper -= nbytes;
    234  1.10  christos 			dest = (char *)(void *)h + h->linp[skip];
    235   1.1       cgd 			memmove(dest, bi, nbytes);
    236  1.10  christos 			((BINTERNAL *)(void *)dest)->pgno = rchild->pgno;
    237   1.1       cgd 			break;
    238   1.1       cgd 		case P_BLEAF:
    239   1.1       cgd 			h->linp[skip] = h->upper -= nbytes;
    240  1.10  christos 			dest = (char *)(void *)h + h->linp[skip];
    241   1.1       cgd 			WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
    242   1.1       cgd 			    rchild->pgno, bl->flags & P_BIGKEY);
    243   1.1       cgd 			memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
    244   1.1       cgd 			if (bl->flags & P_BIGKEY &&
    245  1.10  christos 			    bt_preserve(t, *(pgno_t *)(void *)bl->bytes) ==
    246  1.10  christos 			    RET_ERROR)
    247   1.1       cgd 				goto err1;
    248   1.1       cgd 			break;
    249   1.1       cgd 		case P_RINTERNAL:
    250   1.1       cgd 			/*
    251   1.1       cgd 			 * Update the left page count.  If split
    252   1.1       cgd 			 * added at index 0, fix the correct page.
    253   1.1       cgd 			 */
    254   1.1       cgd 			if (skip > 0)
    255  1.10  christos 				dest = (char *)(void *)h + h->linp[skip - 1];
    256   1.1       cgd 			else
    257  1.10  christos 				dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1];
    258  1.10  christos 			((RINTERNAL *)(void *)dest)->nrecs = rec_total(lchild);
    259  1.10  christos 			((RINTERNAL *)(void *)dest)->pgno = lchild->pgno;
    260   1.1       cgd 
    261   1.1       cgd 			/* Update the right page count. */
    262   1.1       cgd 			h->linp[skip] = h->upper -= nbytes;
    263  1.10  christos 			dest = (char *)(void *)h + h->linp[skip];
    264  1.10  christos 			((RINTERNAL *)(void *)dest)->nrecs = rec_total(rchild);
    265  1.10  christos 			((RINTERNAL *)(void *)dest)->pgno = rchild->pgno;
    266   1.1       cgd 			break;
    267   1.1       cgd 		case P_RLEAF:
    268   1.1       cgd 			/*
    269   1.1       cgd 			 * Update the left page count.  If split
    270   1.1       cgd 			 * added at index 0, fix the correct page.
    271   1.1       cgd 			 */
    272   1.1       cgd 			if (skip > 0)
    273  1.10  christos 				dest = (char *)(void *)h + h->linp[skip - 1];
    274   1.1       cgd 			else
    275  1.10  christos 				dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1];
    276  1.10  christos 			((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(lchild);
    277  1.10  christos 			((RINTERNAL *)(void *)dest)->pgno = lchild->pgno;
    278   1.1       cgd 
    279   1.1       cgd 			/* Update the right page count. */
    280   1.1       cgd 			h->linp[skip] = h->upper -= nbytes;
    281  1.10  christos 			dest = (char *)(void *)h + h->linp[skip];
    282  1.10  christos 			((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(rchild);
    283  1.10  christos 			((RINTERNAL *)(void *)dest)->pgno = rchild->pgno;
    284   1.1       cgd 			break;
    285   1.1       cgd 		default:
    286   1.1       cgd 			abort();
    287   1.1       cgd 		}
    288   1.1       cgd 
    289   1.1       cgd 		/* Unpin the held pages. */
    290   1.1       cgd 		if (!parentsplit) {
    291   1.1       cgd 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    292   1.1       cgd 			break;
    293   1.1       cgd 		}
    294   1.1       cgd 
    295   1.1       cgd 		/* If the root page was split, make it look right. */
    296   1.1       cgd 		if (sp->pgno == P_ROOT &&
    297   1.6       cgd 		    (F_ISSET(t, R_RECNO) ?
    298   1.1       cgd 		    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
    299   1.1       cgd 			goto err1;
    300   1.1       cgd 
    301   1.1       cgd 		mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
    302   1.1       cgd 		mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
    303   1.1       cgd 	}
    304   1.1       cgd 
    305   1.1       cgd 	/* Unpin the held pages. */
    306   1.1       cgd 	mpool_put(t->bt_mp, l, MPOOL_DIRTY);
    307   1.1       cgd 	mpool_put(t->bt_mp, r, MPOOL_DIRTY);
    308   1.1       cgd 
    309   1.1       cgd 	/* Clear any pages left on the stack. */
    310   1.1       cgd 	return (RET_SUCCESS);
    311   1.1       cgd 
    312   1.1       cgd 	/*
    313   1.1       cgd 	 * If something fails in the above loop we were already walking back
    314   1.1       cgd 	 * up the tree and the tree is now inconsistent.  Nothing much we can
    315   1.1       cgd 	 * do about it but release any memory we're holding.
    316   1.1       cgd 	 */
    317   1.1       cgd err1:	mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
    318   1.1       cgd 	mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
    319   1.1       cgd 
    320   1.1       cgd err2:	mpool_put(t->bt_mp, l, 0);
    321   1.1       cgd 	mpool_put(t->bt_mp, r, 0);
    322   1.1       cgd 	__dbpanic(t->bt_dbp);
    323   1.1       cgd 	return (RET_ERROR);
    324   1.1       cgd }
    325   1.1       cgd 
    326   1.1       cgd /*
    327   1.1       cgd  * BT_PAGE -- Split a non-root page of a btree.
    328   1.1       cgd  *
    329   1.1       cgd  * Parameters:
    330   1.1       cgd  *	t:	tree
    331   1.1       cgd  *	h:	root page
    332   1.1       cgd  *	lp:	pointer to left page pointer
    333   1.1       cgd  *	rp:	pointer to right page pointer
    334   1.1       cgd  *	skip:	pointer to index to leave open
    335   1.1       cgd  *	ilen:	insert length
    336   1.1       cgd  *
    337   1.1       cgd  * Returns:
    338   1.1       cgd  *	Pointer to page in which to insert or NULL on error.
    339   1.1       cgd  */
    340   1.1       cgd static PAGE *
    341  1.14  christos bt_page(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
    342   1.1       cgd {
    343   1.1       cgd 	PAGE *l, *r, *tp;
    344   1.1       cgd 	pgno_t npg;
    345   1.1       cgd 
    346   1.1       cgd #ifdef STATISTICS
    347   1.1       cgd 	++bt_split;
    348   1.1       cgd #endif
    349   1.1       cgd 	/* Put the new right page for the split into place. */
    350   1.1       cgd 	if ((r = __bt_new(t, &npg)) == NULL)
    351   1.1       cgd 		return (NULL);
    352   1.1       cgd 	r->pgno = npg;
    353   1.1       cgd 	r->lower = BTDATAOFF;
    354   1.1       cgd 	r->upper = t->bt_psize;
    355   1.1       cgd 	r->nextpg = h->nextpg;
    356   1.1       cgd 	r->prevpg = h->pgno;
    357   1.1       cgd 	r->flags = h->flags & P_TYPE;
    358   1.1       cgd 
    359   1.1       cgd 	/*
    360   1.1       cgd 	 * If we're splitting the last page on a level because we're appending
    361   1.1       cgd 	 * a key to it (skip is NEXTINDEX()), it's likely that the data is
    362   1.1       cgd 	 * sorted.  Adding an empty page on the side of the level is less work
    363   1.1       cgd 	 * and can push the fill factor much higher than normal.  If we're
    364   1.1       cgd 	 * wrong it's no big deal, we'll just do the split the right way next
    365   1.1       cgd 	 * time.  It may look like it's equally easy to do a similar hack for
    366   1.1       cgd 	 * reverse sorted data, that is, split the tree left, but it's not.
    367   1.1       cgd 	 * Don't even try.
    368   1.1       cgd 	 */
    369   1.1       cgd 	if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
    370   1.1       cgd #ifdef STATISTICS
    371   1.1       cgd 		++bt_sortsplit;
    372   1.1       cgd #endif
    373   1.1       cgd 		h->nextpg = r->pgno;
    374   1.1       cgd 		r->lower = BTDATAOFF + sizeof(indx_t);
    375   1.1       cgd 		*skip = 0;
    376   1.1       cgd 		*lp = h;
    377   1.1       cgd 		*rp = r;
    378   1.1       cgd 		return (r);
    379   1.1       cgd 	}
    380   1.1       cgd 
    381   1.1       cgd 	/* Put the new left page for the split into place. */
    382   1.4       cgd 	if ((l = (PAGE *)malloc(t->bt_psize)) == NULL) {
    383   1.1       cgd 		mpool_put(t->bt_mp, r, 0);
    384   1.1       cgd 		return (NULL);
    385   1.1       cgd 	}
    386   1.6       cgd #ifdef PURIFY
    387   1.6       cgd 	memset(l, 0xff, t->bt_psize);
    388   1.6       cgd #endif
    389   1.1       cgd 	l->pgno = h->pgno;
    390   1.1       cgd 	l->nextpg = r->pgno;
    391   1.1       cgd 	l->prevpg = h->prevpg;
    392   1.1       cgd 	l->lower = BTDATAOFF;
    393   1.1       cgd 	l->upper = t->bt_psize;
    394   1.1       cgd 	l->flags = h->flags & P_TYPE;
    395   1.1       cgd 
    396   1.1       cgd 	/* Fix up the previous pointer of the page after the split page. */
    397   1.1       cgd 	if (h->nextpg != P_INVALID) {
    398   1.1       cgd 		if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) {
    399   1.1       cgd 			free(l);
    400   1.1       cgd 			/* XXX mpool_free(t->bt_mp, r->pgno); */
    401   1.1       cgd 			return (NULL);
    402   1.1       cgd 		}
    403   1.1       cgd 		tp->prevpg = r->pgno;
    404   1.6       cgd 		mpool_put(t->bt_mp, tp, MPOOL_DIRTY);
    405   1.1       cgd 	}
    406   1.1       cgd 
    407   1.1       cgd 	/*
    408   1.1       cgd 	 * Split right.  The key/data pairs aren't sorted in the btree page so
    409   1.1       cgd 	 * it's simpler to copy the data from the split page onto two new pages
    410   1.1       cgd 	 * instead of copying half the data to the right page and compacting
    411   1.1       cgd 	 * the left page in place.  Since the left page can't change, we have
    412   1.1       cgd 	 * to swap the original and the allocated left page after the split.
    413   1.1       cgd 	 */
    414   1.1       cgd 	tp = bt_psplit(t, h, l, r, skip, ilen);
    415   1.1       cgd 
    416   1.1       cgd 	/* Move the new left page onto the old left page. */
    417   1.1       cgd 	memmove(h, l, t->bt_psize);
    418   1.1       cgd 	if (tp == l)
    419   1.1       cgd 		tp = h;
    420   1.1       cgd 	free(l);
    421   1.1       cgd 
    422   1.1       cgd 	*lp = h;
    423   1.1       cgd 	*rp = r;
    424   1.1       cgd 	return (tp);
    425   1.1       cgd }
    426   1.1       cgd 
    427   1.1       cgd /*
    428   1.1       cgd  * BT_ROOT -- Split the root page of a btree.
    429   1.1       cgd  *
    430   1.1       cgd  * Parameters:
    431   1.1       cgd  *	t:	tree
    432   1.1       cgd  *	h:	root page
    433   1.1       cgd  *	lp:	pointer to left page pointer
    434   1.1       cgd  *	rp:	pointer to right page pointer
    435   1.1       cgd  *	skip:	pointer to index to leave open
    436   1.1       cgd  *	ilen:	insert length
    437   1.1       cgd  *
    438   1.1       cgd  * Returns:
    439   1.1       cgd  *	Pointer to page in which to insert or NULL on error.
    440   1.1       cgd  */
    441   1.1       cgd static PAGE *
    442  1.14  christos bt_root(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
    443   1.1       cgd {
    444   1.1       cgd 	PAGE *l, *r, *tp;
    445   1.1       cgd 	pgno_t lnpg, rnpg;
    446   1.1       cgd 
    447   1.1       cgd #ifdef STATISTICS
    448   1.1       cgd 	++bt_split;
    449   1.1       cgd 	++bt_rootsplit;
    450   1.1       cgd #endif
    451   1.1       cgd 	/* Put the new left and right pages for the split into place. */
    452   1.1       cgd 	if ((l = __bt_new(t, &lnpg)) == NULL ||
    453   1.1       cgd 	    (r = __bt_new(t, &rnpg)) == NULL)
    454   1.1       cgd 		return (NULL);
    455   1.1       cgd 	l->pgno = lnpg;
    456   1.1       cgd 	r->pgno = rnpg;
    457   1.1       cgd 	l->nextpg = r->pgno;
    458   1.1       cgd 	r->prevpg = l->pgno;
    459   1.1       cgd 	l->prevpg = r->nextpg = P_INVALID;
    460   1.1       cgd 	l->lower = r->lower = BTDATAOFF;
    461   1.1       cgd 	l->upper = r->upper = t->bt_psize;
    462   1.1       cgd 	l->flags = r->flags = h->flags & P_TYPE;
    463   1.1       cgd 
    464   1.1       cgd 	/* Split the root page. */
    465   1.1       cgd 	tp = bt_psplit(t, h, l, r, skip, ilen);
    466   1.1       cgd 
    467   1.1       cgd 	*lp = l;
    468   1.1       cgd 	*rp = r;
    469   1.1       cgd 	return (tp);
    470   1.1       cgd }
    471   1.1       cgd 
    472   1.1       cgd /*
    473   1.1       cgd  * BT_RROOT -- Fix up the recno root page after it has been split.
    474   1.1       cgd  *
    475   1.1       cgd  * Parameters:
    476   1.1       cgd  *	t:	tree
    477   1.1       cgd  *	h:	root page
    478   1.1       cgd  *	l:	left page
    479   1.1       cgd  *	r:	right page
    480   1.1       cgd  *
    481   1.1       cgd  * Returns:
    482   1.1       cgd  *	RET_ERROR, RET_SUCCESS
    483   1.1       cgd  */
    484   1.1       cgd static int
    485  1.14  christos bt_rroot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
    486   1.1       cgd {
    487   1.1       cgd 	char *dest;
    488  1.15     joerg 	uint32_t sz;
    489  1.14  christos 	size_t temp;
    490  1.14  christos 
    491  1.14  christos 	temp = t->bt_psize - NRINTERNAL;
    492  1.15     joerg 	_DBFIT(temp, uint32_t);
    493  1.15     joerg 	sz = (uint32_t)temp;
    494   1.1       cgd 
    495   1.1       cgd 	/* Insert the left and right keys, set the header information. */
    496  1.14  christos 	_DBFIT(sz, indx_t);
    497  1.14  christos 	h->linp[0] = h->upper = (indx_t)sz;
    498  1.10  christos 	dest = (char *)(void *)h + h->upper;
    499   1.1       cgd 	WR_RINTERNAL(dest,
    500   1.1       cgd 	    l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno);
    501   1.1       cgd 
    502   1.1       cgd 	h->linp[1] = h->upper -= NRINTERNAL;
    503  1.10  christos 	dest = (char *)(void *)h + h->upper;
    504   1.1       cgd 	WR_RINTERNAL(dest,
    505   1.1       cgd 	    r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno);
    506   1.1       cgd 
    507   1.1       cgd 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
    508   1.1       cgd 
    509   1.1       cgd 	/* Unpin the root page, set to recno internal page. */
    510   1.1       cgd 	h->flags &= ~P_TYPE;
    511   1.1       cgd 	h->flags |= P_RINTERNAL;
    512   1.1       cgd 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    513   1.1       cgd 
    514   1.1       cgd 	return (RET_SUCCESS);
    515   1.1       cgd }
    516   1.1       cgd 
    517   1.1       cgd /*
    518   1.1       cgd  * BT_BROOT -- Fix up the btree root page after it has been split.
    519   1.1       cgd  *
    520   1.1       cgd  * Parameters:
    521   1.1       cgd  *	t:	tree
    522   1.1       cgd  *	h:	root page
    523   1.1       cgd  *	l:	left page
    524   1.1       cgd  *	r:	right page
    525   1.1       cgd  *
    526   1.1       cgd  * Returns:
    527   1.1       cgd  *	RET_ERROR, RET_SUCCESS
    528   1.1       cgd  */
    529   1.1       cgd static int
    530  1.14  christos bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
    531   1.1       cgd {
    532   1.7  christos 	BINTERNAL *bi = NULL;	/* pacify gcc */
    533   1.1       cgd 	BLEAF *bl;
    534  1.15     joerg 	uint32_t nbytes;
    535   1.1       cgd 	char *dest;
    536   1.1       cgd 
    537   1.1       cgd 	/*
    538   1.1       cgd 	 * If the root page was a leaf page, change it into an internal page.
    539   1.1       cgd 	 * We copy the key we split on (but not the key's data, in the case of
    540   1.1       cgd 	 * a leaf page) to the new root page.
    541   1.1       cgd 	 *
    542   1.1       cgd 	 * The btree comparison code guarantees that the left-most key on any
    543   1.1       cgd 	 * level of the tree is never used, so it doesn't need to be filled in.
    544   1.1       cgd 	 */
    545   1.1       cgd 	nbytes = NBINTERNAL(0);
    546   1.1       cgd 	h->linp[0] = h->upper = t->bt_psize - nbytes;
    547  1.10  christos 	dest = (char *)(void *)h + h->upper;
    548   1.1       cgd 	WR_BINTERNAL(dest, 0, l->pgno, 0);
    549   1.1       cgd 
    550   1.6       cgd 	switch (h->flags & P_TYPE) {
    551   1.1       cgd 	case P_BLEAF:
    552   1.1       cgd 		bl = GETBLEAF(r, 0);
    553   1.1       cgd 		nbytes = NBINTERNAL(bl->ksize);
    554   1.1       cgd 		h->linp[1] = h->upper -= nbytes;
    555  1.10  christos 		dest = (char *)(void *)h + h->upper;
    556   1.1       cgd 		WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
    557   1.1       cgd 		memmove(dest, bl->bytes, bl->ksize);
    558   1.1       cgd 
    559   1.1       cgd 		/*
    560   1.1       cgd 		 * If the key is on an overflow page, mark the overflow chain
    561   1.1       cgd 		 * so it isn't deleted when the leaf copy of the key is deleted.
    562   1.1       cgd 		 */
    563   1.1       cgd 		if (bl->flags & P_BIGKEY &&
    564  1.10  christos 		    bt_preserve(t, *(pgno_t *)(void *)bl->bytes) == RET_ERROR)
    565   1.1       cgd 			return (RET_ERROR);
    566   1.1       cgd 		break;
    567   1.1       cgd 	case P_BINTERNAL:
    568   1.1       cgd 		bi = GETBINTERNAL(r, 0);
    569   1.1       cgd 		nbytes = NBINTERNAL(bi->ksize);
    570   1.1       cgd 		h->linp[1] = h->upper -= nbytes;
    571  1.10  christos 		dest = (char *)(void *)h + h->upper;
    572   1.1       cgd 		memmove(dest, bi, nbytes);
    573  1.10  christos 		((BINTERNAL *)(void *)dest)->pgno = r->pgno;
    574   1.1       cgd 		break;
    575   1.1       cgd 	default:
    576   1.1       cgd 		abort();
    577   1.1       cgd 	}
    578   1.1       cgd 
    579   1.1       cgd 	/* There are two keys on the page. */
    580   1.1       cgd 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
    581   1.1       cgd 
    582   1.1       cgd 	/* Unpin the root page, set to btree internal page. */
    583   1.1       cgd 	h->flags &= ~P_TYPE;
    584   1.1       cgd 	h->flags |= P_BINTERNAL;
    585   1.1       cgd 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    586   1.1       cgd 
    587   1.1       cgd 	return (RET_SUCCESS);
    588   1.1       cgd }
    589   1.1       cgd 
    590   1.1       cgd /*
    591   1.1       cgd  * BT_PSPLIT -- Do the real work of splitting the page.
    592   1.1       cgd  *
    593   1.1       cgd  * Parameters:
    594   1.1       cgd  *	t:	tree
    595   1.1       cgd  *	h:	page to be split
    596   1.1       cgd  *	l:	page to put lower half of data
    597   1.1       cgd  *	r:	page to put upper half of data
    598   1.1       cgd  *	pskip:	pointer to index to leave open
    599   1.1       cgd  *	ilen:	insert length
    600   1.1       cgd  *
    601   1.1       cgd  * Returns:
    602   1.1       cgd  *	Pointer to page in which to insert.
    603   1.1       cgd  */
    604   1.1       cgd static PAGE *
    605  1.14  christos bt_psplit(BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen)
    606   1.1       cgd {
    607   1.1       cgd 	BINTERNAL *bi;
    608   1.1       cgd 	BLEAF *bl;
    609   1.6       cgd 	CURSOR *c;
    610   1.1       cgd 	RLEAF *rl;
    611   1.1       cgd 	PAGE *rval;
    612   1.7  christos 	void *src = NULL;	/* pacify gcc */
    613   1.1       cgd 	indx_t full, half, nxt, off, skip, top, used;
    614  1.15     joerg 	uint32_t nbytes;
    615  1.14  christos 	size_t temp;
    616   1.1       cgd 	int bigkeycnt, isbigkey;
    617   1.1       cgd 
    618   1.1       cgd 	/*
    619   1.1       cgd 	 * Split the data to the left and right pages.  Leave the skip index
    620   1.1       cgd 	 * open.  Additionally, make some effort not to split on an overflow
    621   1.1       cgd 	 * key.  This makes internal page processing faster and can save
    622   1.1       cgd 	 * space as overflow keys used by internal pages are never deleted.
    623   1.1       cgd 	 */
    624   1.1       cgd 	bigkeycnt = 0;
    625   1.1       cgd 	skip = *pskip;
    626  1.14  christos 	temp = t->bt_psize - BTDATAOFF;
    627  1.14  christos 	_DBFIT(temp, indx_t);
    628  1.14  christos 	full = (indx_t)temp;
    629   1.1       cgd 	half = full / 2;
    630   1.1       cgd 	used = 0;
    631   1.1       cgd 	for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) {
    632   1.1       cgd 		if (skip == off) {
    633  1.15     joerg 			_DBFIT(ilen, uint32_t);
    634  1.15     joerg 			nbytes = (uint32_t)ilen;
    635   1.1       cgd 			isbigkey = 0;		/* XXX: not really known. */
    636   1.1       cgd 		} else
    637   1.1       cgd 			switch (h->flags & P_TYPE) {
    638   1.1       cgd 			case P_BINTERNAL:
    639   1.1       cgd 				src = bi = GETBINTERNAL(h, nxt);
    640   1.1       cgd 				nbytes = NBINTERNAL(bi->ksize);
    641   1.1       cgd 				isbigkey = bi->flags & P_BIGKEY;
    642   1.1       cgd 				break;
    643   1.1       cgd 			case P_BLEAF:
    644   1.1       cgd 				src = bl = GETBLEAF(h, nxt);
    645   1.1       cgd 				nbytes = NBLEAF(bl);
    646   1.1       cgd 				isbigkey = bl->flags & P_BIGKEY;
    647   1.1       cgd 				break;
    648   1.1       cgd 			case P_RINTERNAL:
    649   1.1       cgd 				src = GETRINTERNAL(h, nxt);
    650   1.1       cgd 				nbytes = NRINTERNAL;
    651   1.1       cgd 				isbigkey = 0;
    652   1.1       cgd 				break;
    653   1.1       cgd 			case P_RLEAF:
    654   1.1       cgd 				src = rl = GETRLEAF(h, nxt);
    655   1.1       cgd 				nbytes = NRLEAF(rl);
    656   1.1       cgd 				isbigkey = 0;
    657   1.1       cgd 				break;
    658   1.1       cgd 			default:
    659   1.1       cgd 				abort();
    660   1.1       cgd 			}
    661   1.1       cgd 
    662   1.1       cgd 		/*
    663   1.1       cgd 		 * If the key/data pairs are substantial fractions of the max
    664   1.1       cgd 		 * possible size for the page, it's possible to get situations
    665   1.1       cgd 		 * where we decide to try and copy too much onto the left page.
    666   1.1       cgd 		 * Make sure that doesn't happen.
    667   1.1       cgd 		 */
    668   1.9        is 		if ((skip <= off && used + nbytes + sizeof(indx_t) >= full) ||
    669   1.9        is 		    nxt == top - 1) {
    670   1.1       cgd 			--off;
    671   1.1       cgd 			break;
    672   1.1       cgd 		}
    673   1.1       cgd 
    674   1.1       cgd 		/* Copy the key/data pair, if not the skipped index. */
    675   1.1       cgd 		if (skip != off) {
    676   1.1       cgd 			++nxt;
    677   1.1       cgd 
    678   1.1       cgd 			l->linp[off] = l->upper -= nbytes;
    679  1.10  christos 			memmove((char *)(void *)l + l->upper, src, nbytes);
    680   1.1       cgd 		}
    681   1.1       cgd 
    682  1.14  christos 		temp = nbytes + sizeof(indx_t);
    683  1.14  christos 		_DBFIT(temp, indx_t);
    684  1.14  christos 		used += (indx_t)temp;
    685   1.1       cgd 		if (used >= half) {
    686   1.1       cgd 			if (!isbigkey || bigkeycnt == 3)
    687   1.1       cgd 				break;
    688   1.1       cgd 			else
    689   1.1       cgd 				++bigkeycnt;
    690   1.1       cgd 		}
    691   1.1       cgd 	}
    692   1.1       cgd 
    693   1.1       cgd 	/*
    694   1.1       cgd 	 * Off is the last offset that's valid for the left page.
    695   1.1       cgd 	 * Nxt is the first offset to be placed on the right page.
    696   1.1       cgd 	 */
    697  1.14  christos 	temp = (off + 1) * sizeof(indx_t);
    698  1.14  christos 	_DBFIT(temp, indx_t);
    699  1.14  christos 	l->lower += (indx_t)temp;
    700   1.1       cgd 
    701   1.1       cgd 	/*
    702   1.1       cgd 	 * If splitting the page that the cursor was on, the cursor has to be
    703   1.1       cgd 	 * adjusted to point to the same record as before the split.  If the
    704   1.1       cgd 	 * cursor is at or past the skipped slot, the cursor is incremented by
    705   1.1       cgd 	 * one.  If the cursor is on the right page, it is decremented by the
    706   1.1       cgd 	 * number of records split to the left page.
    707   1.1       cgd 	 */
    708   1.6       cgd 	c = &t->bt_cursor;
    709   1.6       cgd 	if (F_ISSET(c, CURS_INIT) && c->pg.pgno == h->pgno) {
    710   1.6       cgd 		if (c->pg.index >= skip)
    711   1.6       cgd 			++c->pg.index;
    712   1.6       cgd 		if (c->pg.index < nxt)			/* Left page. */
    713   1.6       cgd 			c->pg.pgno = l->pgno;
    714   1.1       cgd 		else {					/* Right page. */
    715   1.6       cgd 			c->pg.pgno = r->pgno;
    716   1.6       cgd 			c->pg.index -= nxt;
    717   1.1       cgd 		}
    718   1.1       cgd 	}
    719   1.1       cgd 
    720   1.1       cgd 	/*
    721   1.1       cgd 	 * If the skipped index was on the left page, just return that page.
    722   1.1       cgd 	 * Otherwise, adjust the skip index to reflect the new position on
    723   1.1       cgd 	 * the right page.
    724   1.1       cgd 	 */
    725   1.1       cgd 	if (skip <= off) {
    726  1.12   mycroft 		skip = MAX_PAGE_OFFSET;
    727   1.1       cgd 		rval = l;
    728   1.1       cgd 	} else {
    729   1.1       cgd 		rval = r;
    730   1.1       cgd 		*pskip -= nxt;
    731   1.1       cgd 	}
    732   1.1       cgd 
    733   1.1       cgd 	for (off = 0; nxt < top; ++off) {
    734   1.1       cgd 		if (skip == nxt) {
    735   1.1       cgd 			++off;
    736  1.12   mycroft 			skip = MAX_PAGE_OFFSET;
    737   1.1       cgd 		}
    738   1.1       cgd 		switch (h->flags & P_TYPE) {
    739   1.1       cgd 		case P_BINTERNAL:
    740   1.1       cgd 			src = bi = GETBINTERNAL(h, nxt);
    741   1.1       cgd 			nbytes = NBINTERNAL(bi->ksize);
    742   1.1       cgd 			break;
    743   1.1       cgd 		case P_BLEAF:
    744   1.1       cgd 			src = bl = GETBLEAF(h, nxt);
    745   1.1       cgd 			nbytes = NBLEAF(bl);
    746   1.1       cgd 			break;
    747   1.1       cgd 		case P_RINTERNAL:
    748   1.1       cgd 			src = GETRINTERNAL(h, nxt);
    749   1.1       cgd 			nbytes = NRINTERNAL;
    750   1.1       cgd 			break;
    751   1.1       cgd 		case P_RLEAF:
    752   1.1       cgd 			src = rl = GETRLEAF(h, nxt);
    753   1.1       cgd 			nbytes = NRLEAF(rl);
    754   1.1       cgd 			break;
    755   1.1       cgd 		default:
    756   1.1       cgd 			abort();
    757   1.1       cgd 		}
    758   1.1       cgd 		++nxt;
    759   1.1       cgd 		r->linp[off] = r->upper -= nbytes;
    760  1.10  christos 		memmove((char *)(void *)r + r->upper, src, nbytes);
    761   1.1       cgd 	}
    762  1.14  christos 	temp = off * sizeof(indx_t);
    763  1.14  christos 	_DBFIT(temp, indx_t);
    764  1.14  christos 	r->lower += (indx_t)temp;
    765   1.1       cgd 
    766   1.1       cgd 	/* If the key is being appended to the page, adjust the index. */
    767   1.1       cgd 	if (skip == top)
    768   1.1       cgd 		r->lower += sizeof(indx_t);
    769   1.1       cgd 
    770   1.1       cgd 	return (rval);
    771   1.1       cgd }
    772   1.1       cgd 
    773   1.1       cgd /*
    774   1.1       cgd  * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
    775   1.1       cgd  *
    776   1.1       cgd  * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
    777   1.1       cgd  * record that references them gets deleted.  Chains pointed to by internal
    778   1.1       cgd  * pages never get deleted.  This routine marks a chain as pointed to by an
    779   1.1       cgd  * internal page.
    780   1.1       cgd  *
    781   1.1       cgd  * Parameters:
    782   1.1       cgd  *	t:	tree
    783   1.1       cgd  *	pg:	page number of first page in the chain.
    784   1.1       cgd  *
    785   1.1       cgd  * Returns:
    786   1.1       cgd  *	RET_SUCCESS, RET_ERROR.
    787   1.1       cgd  */
    788   1.1       cgd static int
    789  1.14  christos bt_preserve(BTREE *t, pgno_t pg)
    790   1.1       cgd {
    791   1.1       cgd 	PAGE *h;
    792   1.1       cgd 
    793   1.1       cgd 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    794   1.1       cgd 		return (RET_ERROR);
    795   1.1       cgd 	h->flags |= P_PRESERVE;
    796   1.1       cgd 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    797   1.1       cgd 	return (RET_SUCCESS);
    798   1.1       cgd }
    799   1.1       cgd 
    800   1.1       cgd /*
    801   1.1       cgd  * REC_TOTAL -- Return the number of recno entries below a page.
    802   1.1       cgd  *
    803   1.1       cgd  * Parameters:
    804   1.1       cgd  *	h:	page
    805   1.1       cgd  *
    806   1.1       cgd  * Returns:
    807   1.1       cgd  *	The number of recno entries below a page.
    808   1.1       cgd  *
    809   1.1       cgd  * XXX
    810   1.1       cgd  * These values could be set by the bt_psplit routine.  The problem is that the
    811   1.1       cgd  * entry has to be popped off of the stack etc. or the values have to be passed
    812   1.1       cgd  * all the way back to bt_split/bt_rroot and it's not very clean.
    813   1.1       cgd  */
    814   1.1       cgd static recno_t
    815  1.14  christos rec_total(PAGE *h)
    816   1.1       cgd {
    817   1.1       cgd 	recno_t recs;
    818   1.1       cgd 	indx_t nxt, top;
    819   1.1       cgd 
    820   1.1       cgd 	for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
    821   1.1       cgd 		recs += GETRINTERNAL(h, nxt)->nrecs;
    822   1.1       cgd 	return (recs);
    823   1.1       cgd }
    824