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