Home | History | Annotate | Line # | Download | only in btree
bt_seq.c revision 1.1.1.2
      1      1.1  cgd /*-
      2  1.1.1.2  cgd  * Copyright (c) 1990, 1993, 1994
      3      1.1  cgd  *	The Regents of the University of California.  All rights reserved.
      4      1.1  cgd  *
      5      1.1  cgd  * This code is derived from software contributed to Berkeley by
      6      1.1  cgd  * Mike Olson.
      7      1.1  cgd  *
      8      1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9      1.1  cgd  * modification, are permitted provided that the following conditions
     10      1.1  cgd  * are met:
     11      1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12      1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13      1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15      1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16      1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17      1.1  cgd  *    must display the following acknowledgement:
     18      1.1  cgd  *	This product includes software developed by the University of
     19      1.1  cgd  *	California, Berkeley and its contributors.
     20      1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21      1.1  cgd  *    may be used to endorse or promote products derived from this software
     22      1.1  cgd  *    without specific prior written permission.
     23      1.1  cgd  *
     24      1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25      1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26      1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27      1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28      1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29      1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30      1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31      1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32      1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33      1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34      1.1  cgd  * SUCH DAMAGE.
     35      1.1  cgd  */
     36      1.1  cgd 
     37      1.1  cgd #if defined(LIBC_SCCS) && !defined(lint)
     38  1.1.1.2  cgd static char sccsid[] = "@(#)bt_seq.c	8.7 (Berkeley) 7/20/94";
     39      1.1  cgd #endif /* LIBC_SCCS and not lint */
     40      1.1  cgd 
     41      1.1  cgd #include <sys/types.h>
     42      1.1  cgd 
     43      1.1  cgd #include <errno.h>
     44      1.1  cgd #include <stddef.h>
     45      1.1  cgd #include <stdio.h>
     46      1.1  cgd #include <stdlib.h>
     47      1.1  cgd 
     48      1.1  cgd #include <db.h>
     49      1.1  cgd #include "btree.h"
     50      1.1  cgd 
     51  1.1.1.2  cgd static int __bt_first __P((BTREE *, const DBT *, EPG *, int *));
     52  1.1.1.2  cgd static int __bt_seqadv __P((BTREE *, EPG *, int));
     53  1.1.1.2  cgd static int __bt_seqset __P((BTREE *, EPG *, DBT *, int));
     54      1.1  cgd 
     55      1.1  cgd /*
     56      1.1  cgd  * Sequential scan support.
     57      1.1  cgd  *
     58  1.1.1.2  cgd  * The tree can be scanned sequentially, starting from either end of the
     59  1.1.1.2  cgd  * tree or from any specific key.  A scan request before any scanning is
     60  1.1.1.2  cgd  * done is initialized as starting from the least node.
     61      1.1  cgd  */
     62      1.1  cgd 
     63      1.1  cgd /*
     64  1.1.1.2  cgd  * __bt_seq --
     65  1.1.1.2  cgd  *	Btree sequential scan interface.
     66      1.1  cgd  *
     67      1.1  cgd  * Parameters:
     68      1.1  cgd  *	dbp:	pointer to access method
     69      1.1  cgd  *	key:	key for positioning and return value
     70      1.1  cgd  *	data:	data return value
     71      1.1  cgd  *	flags:	R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
     72      1.1  cgd  *
     73      1.1  cgd  * Returns:
     74      1.1  cgd  *	RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
     75      1.1  cgd  */
     76      1.1  cgd int
     77      1.1  cgd __bt_seq(dbp, key, data, flags)
     78      1.1  cgd 	const DB *dbp;
     79      1.1  cgd 	DBT *key, *data;
     80      1.1  cgd 	u_int flags;
     81      1.1  cgd {
     82      1.1  cgd 	BTREE *t;
     83      1.1  cgd 	EPG e;
     84      1.1  cgd 	int status;
     85      1.1  cgd 
     86  1.1.1.1  cgd 	t = dbp->internal;
     87  1.1.1.1  cgd 
     88  1.1.1.1  cgd 	/* Toss any page pinned across calls. */
     89  1.1.1.1  cgd 	if (t->bt_pinned != NULL) {
     90  1.1.1.1  cgd 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     91  1.1.1.1  cgd 		t->bt_pinned = NULL;
     92  1.1.1.1  cgd 	}
     93  1.1.1.1  cgd 
     94      1.1  cgd 	/*
     95      1.1  cgd 	 * If scan unitialized as yet, or starting at a specific record, set
     96  1.1.1.2  cgd 	 * the scan to a specific key.  Both __bt_seqset and __bt_seqadv pin
     97  1.1.1.2  cgd 	 * the page the cursor references if they're successful.
     98      1.1  cgd 	 */
     99  1.1.1.2  cgd 	switch (flags) {
    100      1.1  cgd 	case R_NEXT:
    101      1.1  cgd 	case R_PREV:
    102  1.1.1.2  cgd 		if (F_ISSET(&t->bt_cursor, CURS_INIT)) {
    103  1.1.1.2  cgd 			status = __bt_seqadv(t, &e, flags);
    104      1.1  cgd 			break;
    105      1.1  cgd 		}
    106      1.1  cgd 		/* FALLTHROUGH */
    107      1.1  cgd 	case R_FIRST:
    108      1.1  cgd 	case R_LAST:
    109  1.1.1.2  cgd 	case R_CURSOR:
    110  1.1.1.2  cgd 		status = __bt_seqset(t, &e, key, flags);
    111      1.1  cgd 		break;
    112      1.1  cgd 	default:
    113      1.1  cgd 		errno = EINVAL;
    114      1.1  cgd 		return (RET_ERROR);
    115      1.1  cgd 	}
    116      1.1  cgd 
    117      1.1  cgd 	if (status == RET_SUCCESS) {
    118  1.1.1.2  cgd 		__bt_setcur(t, e.page->pgno, e.index);
    119      1.1  cgd 
    120  1.1.1.2  cgd 		status =
    121  1.1.1.2  cgd 		    __bt_ret(t, &e, key, &t->bt_rkey, data, &t->bt_rdata, 0);
    122  1.1.1.1  cgd 
    123  1.1.1.1  cgd 		/*
    124  1.1.1.1  cgd 		 * If the user is doing concurrent access, we copied the
    125  1.1.1.1  cgd 		 * key/data, toss the page.
    126  1.1.1.1  cgd 		 */
    127  1.1.1.2  cgd 		if (F_ISSET(t, B_DB_LOCK))
    128  1.1.1.1  cgd 			mpool_put(t->bt_mp, e.page, 0);
    129  1.1.1.1  cgd 		else
    130  1.1.1.1  cgd 			t->bt_pinned = e.page;
    131      1.1  cgd 	}
    132      1.1  cgd 	return (status);
    133      1.1  cgd }
    134      1.1  cgd 
    135      1.1  cgd /*
    136  1.1.1.2  cgd  * __bt_seqset --
    137  1.1.1.2  cgd  *	Set the sequential scan to a specific key.
    138      1.1  cgd  *
    139      1.1  cgd  * Parameters:
    140      1.1  cgd  *	t:	tree
    141      1.1  cgd  *	ep:	storage for returned key
    142      1.1  cgd  *	key:	key for initial scan position
    143      1.1  cgd  *	flags:	R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV
    144      1.1  cgd  *
    145      1.1  cgd  * Side effects:
    146      1.1  cgd  *	Pins the page the cursor references.
    147      1.1  cgd  *
    148      1.1  cgd  * Returns:
    149      1.1  cgd  *	RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
    150      1.1  cgd  */
    151      1.1  cgd static int
    152  1.1.1.2  cgd __bt_seqset(t, ep, key, flags)
    153      1.1  cgd 	BTREE *t;
    154      1.1  cgd 	EPG *ep;
    155      1.1  cgd 	DBT *key;
    156      1.1  cgd 	int flags;
    157      1.1  cgd {
    158      1.1  cgd 	PAGE *h;
    159      1.1  cgd 	pgno_t pg;
    160      1.1  cgd 	int exact;
    161      1.1  cgd 
    162      1.1  cgd 	/*
    163  1.1.1.2  cgd 	 * Find the first, last or specific key in the tree and point the
    164  1.1.1.2  cgd 	 * cursor at it.  The cursor may not be moved until a new key has
    165  1.1.1.2  cgd 	 * been found.
    166      1.1  cgd 	 */
    167  1.1.1.2  cgd 	switch (flags) {
    168      1.1  cgd 	case R_CURSOR:				/* Keyed scan. */
    169      1.1  cgd 		/*
    170  1.1.1.2  cgd 		 * Find the first instance of the key or the smallest key
    171  1.1.1.2  cgd 		 * which is greater than or equal to the specified key.
    172      1.1  cgd 		 */
    173      1.1  cgd 		if (key->data == NULL || key->size == 0) {
    174      1.1  cgd 			errno = EINVAL;
    175      1.1  cgd 			return (RET_ERROR);
    176      1.1  cgd 		}
    177  1.1.1.2  cgd 		return (__bt_first(t, key, ep, &exact));
    178      1.1  cgd 	case R_FIRST:				/* First record. */
    179      1.1  cgd 	case R_NEXT:
    180      1.1  cgd 		/* Walk down the left-hand side of the tree. */
    181      1.1  cgd 		for (pg = P_ROOT;;) {
    182      1.1  cgd 			if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    183      1.1  cgd 				return (RET_ERROR);
    184  1.1.1.2  cgd 
    185  1.1.1.2  cgd 			/* Check for an empty tree. */
    186  1.1.1.2  cgd 			if (NEXTINDEX(h) == 0) {
    187  1.1.1.2  cgd 				mpool_put(t->bt_mp, h, 0);
    188  1.1.1.2  cgd 				return (RET_SPECIAL);
    189  1.1.1.2  cgd 			}
    190  1.1.1.2  cgd 
    191      1.1  cgd 			if (h->flags & (P_BLEAF | P_RLEAF))
    192      1.1  cgd 				break;
    193      1.1  cgd 			pg = GETBINTERNAL(h, 0)->pgno;
    194      1.1  cgd 			mpool_put(t->bt_mp, h, 0);
    195      1.1  cgd 		}
    196      1.1  cgd 		ep->page = h;
    197      1.1  cgd 		ep->index = 0;
    198      1.1  cgd 		break;
    199      1.1  cgd 	case R_LAST:				/* Last record. */
    200      1.1  cgd 	case R_PREV:
    201      1.1  cgd 		/* Walk down the right-hand side of the tree. */
    202      1.1  cgd 		for (pg = P_ROOT;;) {
    203      1.1  cgd 			if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    204      1.1  cgd 				return (RET_ERROR);
    205  1.1.1.2  cgd 
    206  1.1.1.2  cgd 			/* Check for an empty tree. */
    207  1.1.1.2  cgd 			if (NEXTINDEX(h) == 0) {
    208  1.1.1.2  cgd 				mpool_put(t->bt_mp, h, 0);
    209  1.1.1.2  cgd 				return (RET_SPECIAL);
    210  1.1.1.2  cgd 			}
    211  1.1.1.2  cgd 
    212      1.1  cgd 			if (h->flags & (P_BLEAF | P_RLEAF))
    213      1.1  cgd 				break;
    214      1.1  cgd 			pg = GETBINTERNAL(h, NEXTINDEX(h) - 1)->pgno;
    215      1.1  cgd 			mpool_put(t->bt_mp, h, 0);
    216      1.1  cgd 		}
    217      1.1  cgd 
    218      1.1  cgd 		ep->page = h;
    219      1.1  cgd 		ep->index = NEXTINDEX(h) - 1;
    220      1.1  cgd 		break;
    221      1.1  cgd 	}
    222      1.1  cgd 	return (RET_SUCCESS);
    223      1.1  cgd }
    224      1.1  cgd 
    225      1.1  cgd /*
    226  1.1.1.2  cgd  * __bt_seqadvance --
    227  1.1.1.2  cgd  *	Advance the sequential scan.
    228      1.1  cgd  *
    229      1.1  cgd  * Parameters:
    230      1.1  cgd  *	t:	tree
    231      1.1  cgd  *	flags:	R_NEXT, R_PREV
    232      1.1  cgd  *
    233      1.1  cgd  * Side effects:
    234      1.1  cgd  *	Pins the page the new key/data record is on.
    235      1.1  cgd  *
    236      1.1  cgd  * Returns:
    237      1.1  cgd  *	RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
    238      1.1  cgd  */
    239      1.1  cgd static int
    240  1.1.1.2  cgd __bt_seqadv(t, ep, flags)
    241      1.1  cgd 	BTREE *t;
    242  1.1.1.2  cgd 	EPG *ep;
    243      1.1  cgd 	int flags;
    244      1.1  cgd {
    245  1.1.1.2  cgd 	CURSOR *c;
    246      1.1  cgd 	PAGE *h;
    247      1.1  cgd 	indx_t index;
    248      1.1  cgd 	pgno_t pg;
    249  1.1.1.2  cgd 	int exact;
    250  1.1.1.2  cgd 
    251  1.1.1.2  cgd 	/*
    252  1.1.1.2  cgd 	 * There are a couple of states that we can be in.  The cursor has
    253  1.1.1.2  cgd 	 * been initialized by the time we get here, but that's all we know.
    254  1.1.1.2  cgd 	 */
    255  1.1.1.2  cgd 	c = &t->bt_cursor;
    256      1.1  cgd 
    257  1.1.1.2  cgd 	/*
    258  1.1.1.2  cgd 	 * The cursor was deleted where there weren't any duplicate records,
    259  1.1.1.2  cgd 	 * so the key was saved.  Find out where that key would go in the
    260  1.1.1.2  cgd 	 * current tree.  It doesn't matter if the returned key is an exact
    261  1.1.1.2  cgd 	 * match or not -- if it's an exact match, the record was added after
    262  1.1.1.2  cgd 	 * the delete so we can just return it.  If not, as long as there's
    263  1.1.1.2  cgd 	 * a record there, return it.
    264  1.1.1.2  cgd 	 */
    265  1.1.1.2  cgd 	if (F_ISSET(c, CURS_ACQUIRE))
    266  1.1.1.2  cgd 		return (__bt_first(t, &c->key, ep, &exact));
    267      1.1  cgd 
    268  1.1.1.2  cgd 	/* Get the page referenced by the cursor. */
    269  1.1.1.2  cgd 	if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL)
    270      1.1  cgd 		return (RET_ERROR);
    271      1.1  cgd 
    272      1.1  cgd 	/*
    273  1.1.1.2  cgd  	 * Find the next/previous record in the tree and point the cursor at
    274  1.1.1.2  cgd 	 * it.  The cursor may not be moved until a new key has been found.
    275      1.1  cgd 	 */
    276  1.1.1.2  cgd 	switch (flags) {
    277      1.1  cgd 	case R_NEXT:			/* Next record. */
    278  1.1.1.2  cgd 		/*
    279  1.1.1.2  cgd 		 * The cursor was deleted in duplicate records, and moved
    280  1.1.1.2  cgd 		 * forward to a record that has yet to be returned.  Clear
    281  1.1.1.2  cgd 		 * that flag, and return the record.
    282  1.1.1.2  cgd 		 */
    283  1.1.1.2  cgd 		if (F_ISSET(c, CURS_AFTER))
    284  1.1.1.2  cgd 			goto usecurrent;
    285  1.1.1.2  cgd 		index = c->pg.index;
    286      1.1  cgd 		if (++index == NEXTINDEX(h)) {
    287  1.1.1.2  cgd 			pg = h->nextpg;
    288  1.1.1.2  cgd 			mpool_put(t->bt_mp, h, 0);
    289  1.1.1.2  cgd 			if (pg == P_INVALID)
    290  1.1.1.2  cgd 				return (RET_SPECIAL);
    291  1.1.1.2  cgd 			if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    292  1.1.1.2  cgd 				return (RET_ERROR);
    293      1.1  cgd 			index = 0;
    294      1.1  cgd 		}
    295      1.1  cgd 		break;
    296      1.1  cgd 	case R_PREV:			/* Previous record. */
    297  1.1.1.2  cgd 		/*
    298  1.1.1.2  cgd 		 * The cursor was deleted in duplicate records, and moved
    299  1.1.1.2  cgd 		 * backward to a record that has yet to be returned.  Clear
    300  1.1.1.2  cgd 		 * that flag, and return the record.
    301  1.1.1.2  cgd 		 */
    302  1.1.1.2  cgd 		if (F_ISSET(c, CURS_BEFORE)) {
    303  1.1.1.2  cgd usecurrent:		F_CLR(c, CURS_AFTER | CURS_BEFORE);
    304  1.1.1.2  cgd 			ep->page = h;
    305  1.1.1.2  cgd 			ep->index = c->pg.index;
    306  1.1.1.2  cgd 			return (RET_SUCCESS);
    307      1.1  cgd 		}
    308  1.1.1.2  cgd 		index = c->pg.index;
    309  1.1.1.2  cgd 		if (index == 0) {
    310  1.1.1.2  cgd 			pg = h->prevpg;
    311  1.1.1.2  cgd 			mpool_put(t->bt_mp, h, 0);
    312  1.1.1.2  cgd 			if (pg == P_INVALID)
    313  1.1.1.2  cgd 				return (RET_SPECIAL);
    314  1.1.1.2  cgd 			if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    315  1.1.1.2  cgd 				return (RET_ERROR);
    316  1.1.1.2  cgd 			index = NEXTINDEX(h) - 1;
    317  1.1.1.2  cgd 		} else
    318  1.1.1.2  cgd 			--index;
    319      1.1  cgd 		break;
    320      1.1  cgd 	}
    321      1.1  cgd 
    322  1.1.1.2  cgd 	ep->page = h;
    323  1.1.1.2  cgd 	ep->index = index;
    324  1.1.1.2  cgd 	return (RET_SUCCESS);
    325  1.1.1.2  cgd }
    326  1.1.1.2  cgd 
    327  1.1.1.2  cgd /*
    328  1.1.1.2  cgd  * __bt_first --
    329  1.1.1.2  cgd  *	Find the first entry.
    330  1.1.1.2  cgd  *
    331  1.1.1.2  cgd  * Parameters:
    332  1.1.1.2  cgd  *	t:	the tree
    333  1.1.1.2  cgd  *    key:	the key
    334  1.1.1.2  cgd  *  erval:	return EPG
    335  1.1.1.2  cgd  * exactp:	pointer to exact match flag
    336  1.1.1.2  cgd  *
    337  1.1.1.2  cgd  * Returns:
    338  1.1.1.2  cgd  *	The first entry in the tree greater than or equal to key,
    339  1.1.1.2  cgd  *	or RET_SPECIAL if no such key exists.
    340  1.1.1.2  cgd  */
    341  1.1.1.2  cgd static int
    342  1.1.1.2  cgd __bt_first(t, key, erval, exactp)
    343  1.1.1.2  cgd 	BTREE *t;
    344  1.1.1.2  cgd 	const DBT *key;
    345  1.1.1.2  cgd 	EPG *erval;
    346  1.1.1.2  cgd 	int *exactp;
    347  1.1.1.2  cgd {
    348  1.1.1.2  cgd 	PAGE *h;
    349  1.1.1.2  cgd 	EPG *ep, save;
    350  1.1.1.2  cgd 	pgno_t pg;
    351      1.1  cgd 
    352      1.1  cgd 	/*
    353  1.1.1.2  cgd 	 * Find any matching record; __bt_search pins the page.
    354  1.1.1.2  cgd 	 *
    355  1.1.1.2  cgd 	 * If it's an exact match and duplicates are possible, walk backwards
    356  1.1.1.2  cgd 	 * in the tree until we find the first one.  Otherwise, make sure it's
    357  1.1.1.2  cgd 	 * a valid key (__bt_search may return an index just past the end of a
    358  1.1.1.2  cgd 	 * page) and return it.
    359      1.1  cgd 	 */
    360  1.1.1.2  cgd 	if ((ep = __bt_search(t, key, exactp)) == NULL)
    361  1.1.1.2  cgd 		return (NULL);
    362  1.1.1.2  cgd 	if (*exactp) {
    363  1.1.1.2  cgd 		if (F_ISSET(t, B_NODUPS)) {
    364  1.1.1.2  cgd 			*erval = *ep;
    365  1.1.1.2  cgd 			return (RET_SUCCESS);
    366  1.1.1.2  cgd 		}
    367  1.1.1.2  cgd 
    368  1.1.1.2  cgd 		/*
    369  1.1.1.2  cgd 		 * Walk backwards, as long as the entry matches and there are
    370  1.1.1.2  cgd 		 * keys left in the tree.  Save a copy of each match in case
    371  1.1.1.2  cgd 		 * we go too far.
    372  1.1.1.2  cgd 		 */
    373  1.1.1.2  cgd 		save = *ep;
    374  1.1.1.2  cgd 		h = ep->page;
    375  1.1.1.2  cgd 		do {
    376  1.1.1.2  cgd 			if (save.page->pgno != ep->page->pgno) {
    377  1.1.1.2  cgd 				mpool_put(t->bt_mp, save.page, 0);
    378  1.1.1.2  cgd 				save = *ep;
    379  1.1.1.2  cgd 			} else
    380  1.1.1.2  cgd 				save.index = ep->index;
    381  1.1.1.2  cgd 
    382  1.1.1.2  cgd 			/*
    383  1.1.1.2  cgd 			 * Don't unpin the page the last (or original) match
    384  1.1.1.2  cgd 			 * was on, but make sure it's unpinned if an error
    385  1.1.1.2  cgd 			 * occurs.
    386  1.1.1.2  cgd 			 */
    387  1.1.1.2  cgd 			if (ep->index == 0) {
    388  1.1.1.2  cgd 				if (h->prevpg == P_INVALID)
    389  1.1.1.2  cgd 					break;
    390  1.1.1.2  cgd 				if (h->pgno != save.page->pgno)
    391  1.1.1.2  cgd 					mpool_put(t->bt_mp, h, 0);
    392  1.1.1.2  cgd 				if ((h = mpool_get(t->bt_mp,
    393  1.1.1.2  cgd 				    h->prevpg, 0)) == NULL) {
    394  1.1.1.2  cgd 					if (h->pgno == save.page->pgno)
    395  1.1.1.2  cgd 						mpool_put(t->bt_mp,
    396  1.1.1.2  cgd 						    save.page, 0);
    397  1.1.1.2  cgd 					return (RET_ERROR);
    398  1.1.1.2  cgd 				}
    399  1.1.1.2  cgd 				ep->page = h;
    400  1.1.1.2  cgd 				ep->index = NEXTINDEX(h);
    401  1.1.1.2  cgd 			}
    402  1.1.1.2  cgd 			--ep->index;
    403  1.1.1.2  cgd 		} while (__bt_cmp(t, key, ep) == 0);
    404  1.1.1.2  cgd 
    405  1.1.1.2  cgd 		/*
    406  1.1.1.2  cgd 		 * Reach here with the last page that was looked at pinned,
    407  1.1.1.2  cgd 		 * which may or may not be the same as the last (or original)
    408  1.1.1.2  cgd 		 * match page.  If it's not useful, release it.
    409  1.1.1.2  cgd 		 */
    410  1.1.1.2  cgd 		if (h->pgno != save.page->pgno)
    411  1.1.1.2  cgd 			mpool_put(t->bt_mp, h, 0);
    412  1.1.1.2  cgd 
    413  1.1.1.2  cgd 		*erval = save;
    414  1.1.1.2  cgd 		return (RET_SUCCESS);
    415  1.1.1.2  cgd 	}
    416  1.1.1.2  cgd 
    417  1.1.1.2  cgd 	/* If at the end of a page, find the next entry. */
    418  1.1.1.2  cgd 	if (ep->index == NEXTINDEX(ep->page)) {
    419  1.1.1.2  cgd 		h = ep->page;
    420  1.1.1.2  cgd 		pg = h->nextpg;
    421  1.1.1.2  cgd 		mpool_put(t->bt_mp, h, 0);
    422  1.1.1.2  cgd 		if (pg == P_INVALID)
    423  1.1.1.2  cgd 			return (RET_SPECIAL);
    424  1.1.1.2  cgd 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    425      1.1  cgd 			return (RET_ERROR);
    426  1.1.1.2  cgd 		ep->index = 0;
    427  1.1.1.2  cgd 		ep->page = h;
    428      1.1  cgd 	}
    429  1.1.1.2  cgd 	*erval = *ep;
    430      1.1  cgd 	return (RET_SUCCESS);
    431      1.1  cgd }
    432      1.1  cgd 
    433      1.1  cgd /*
    434  1.1.1.2  cgd  * __bt_setcur --
    435  1.1.1.2  cgd  *	Set the cursor to an entry in the tree.
    436      1.1  cgd  *
    437      1.1  cgd  * Parameters:
    438  1.1.1.2  cgd  *	t:	the tree
    439  1.1.1.2  cgd  *   pgno:	page number
    440  1.1.1.2  cgd  *  index:	page index
    441      1.1  cgd  */
    442  1.1.1.2  cgd void
    443  1.1.1.2  cgd __bt_setcur(t, pgno, index)
    444      1.1  cgd 	BTREE *t;
    445  1.1.1.2  cgd 	pgno_t pgno;
    446  1.1.1.2  cgd 	u_int index;
    447      1.1  cgd {
    448  1.1.1.2  cgd 	/* Lose any already deleted key. */
    449  1.1.1.2  cgd 	if (t->bt_cursor.key.data != NULL) {
    450  1.1.1.2  cgd 		free(t->bt_cursor.key.data);
    451  1.1.1.2  cgd 		t->bt_cursor.key.size = 0;
    452  1.1.1.2  cgd 		t->bt_cursor.key.data = NULL;
    453  1.1.1.2  cgd 	}
    454  1.1.1.2  cgd 	F_CLR(&t->bt_cursor, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE);
    455      1.1  cgd 
    456  1.1.1.2  cgd 	/* Update the cursor. */
    457  1.1.1.2  cgd 	t->bt_cursor.pg.pgno = pgno;
    458  1.1.1.2  cgd 	t->bt_cursor.pg.index = index;
    459  1.1.1.2  cgd 	F_SET(&t->bt_cursor, CURS_INIT);
    460      1.1  cgd }
    461