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