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