2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)bt_seq.c 8.7 (Berkeley) 7/20/94
37 * $DragonFly: src/lib/libcr/db/btree/Attic/bt_seq.c,v 1.4 2003/11/12 20:21:26 eirikn Exp $
40 #include <sys/types.h>
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);
55 * Sequential scan support.
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.
64 * Btree sequential scan interface.
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.
73 * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
76 __bt_seq(dbp, key, data, flags)
87 /* Toss any page pinned across calls. */
88 if (t->bt_pinned != NULL) {
89 mpool_put(t->bt_mp, t->bt_pinned, 0);
94 * If scan unitialized as yet, or starting at a specific record, set
95 * the scan to a specific key. Both __bt_seqset and __bt_seqadv pin
96 * the page the cursor references if they're successful.
101 if (F_ISSET(&t->bt_cursor, CURS_INIT)) {
102 status = __bt_seqadv(t, &e, flags);
109 status = __bt_seqset(t, &e, key, flags);
116 if (status == RET_SUCCESS) {
117 __bt_setcur(t, e.page->pgno, e.index);
120 __bt_ret(t, &e, key, &t->bt_rkey, data, &t->bt_rdata, 0);
123 * If the user is doing concurrent access, we copied the
124 * key/data, toss the page.
126 if (F_ISSET(t, B_DB_LOCK))
127 mpool_put(t->bt_mp, e.page, 0);
129 t->bt_pinned = e.page;
136 * Set the sequential scan to a specific key.
140 * ep: storage for returned key
141 * key: key for initial scan position
142 * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV
145 * Pins the page the cursor references.
148 * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
151 __bt_seqset(t, ep, key, flags)
162 * Find the first, last or specific key in the tree and point the
163 * cursor at it. The cursor may not be moved until a new key has
167 case R_CURSOR: /* Keyed scan. */
169 * Find the first instance of the key or the smallest key
170 * which is greater than or equal to the specified key.
172 if (key->data == NULL || key->size == 0) {
176 return (__bt_first(t, key, ep, &exact));
177 case R_FIRST: /* First record. */
179 /* Walk down the left-hand side of the tree. */
180 for (pg = P_ROOT;;) {
181 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
184 /* Check for an empty tree. */
185 if (NEXTINDEX(h) == 0) {
186 mpool_put(t->bt_mp, h, 0);
187 return (RET_SPECIAL);
190 if (h->flags & (P_BLEAF | P_RLEAF))
192 pg = GETBINTERNAL(h, 0)->pgno;
193 mpool_put(t->bt_mp, h, 0);
198 case R_LAST: /* Last record. */
200 /* Walk down the right-hand side of the tree. */
201 for (pg = P_ROOT;;) {
202 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
205 /* Check for an empty tree. */
206 if (NEXTINDEX(h) == 0) {
207 mpool_put(t->bt_mp, h, 0);
208 return (RET_SPECIAL);
211 if (h->flags & (P_BLEAF | P_RLEAF))
213 pg = GETBINTERNAL(h, NEXTINDEX(h) - 1)->pgno;
214 mpool_put(t->bt_mp, h, 0);
218 ep->index = NEXTINDEX(h) - 1;
221 return (RET_SUCCESS);
226 * Advance the sequential scan.
230 * flags: R_NEXT, R_PREV
233 * Pins the page the new key/data record is on.
236 * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
239 __bt_seqadv(t, ep, flags)
251 * There are a couple of states that we can be in. The cursor has
252 * been initialized by the time we get here, but that's all we know.
257 * The cursor was deleted where there weren't any duplicate records,
258 * so the key was saved. Find out where that key would go in the
259 * current tree. It doesn't matter if the returned key is an exact
260 * match or not -- if it's an exact match, the record was added after
261 * the delete so we can just return it. If not, as long as there's
262 * a record there, return it.
264 if (F_ISSET(c, CURS_ACQUIRE))
265 return (__bt_first(t, &c->key, ep, &exact));
267 /* Get the page referenced by the cursor. */
268 if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL)
272 * Find the next/previous record in the tree and point the cursor at
273 * it. The cursor may not be moved until a new key has been found.
276 case R_NEXT: /* Next record. */
278 * The cursor was deleted in duplicate records, and moved
279 * forward to a record that has yet to be returned. Clear
280 * that flag, and return the record.
282 if (F_ISSET(c, CURS_AFTER))
285 if (++index == NEXTINDEX(h)) {
287 mpool_put(t->bt_mp, h, 0);
289 return (RET_SPECIAL);
290 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
295 case R_PREV: /* Previous record. */
297 * The cursor was deleted in duplicate records, and moved
298 * backward to a record that has yet to be returned. Clear
299 * that flag, and return the record.
301 if (F_ISSET(c, CURS_BEFORE)) {
302 usecurrent: F_CLR(c, CURS_AFTER | CURS_BEFORE);
304 ep->index = c->pg.index;
305 return (RET_SUCCESS);
310 mpool_put(t->bt_mp, h, 0);
312 return (RET_SPECIAL);
313 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
315 index = NEXTINDEX(h) - 1;
323 return (RET_SUCCESS);
328 * Find the first entry.
334 * exactp: pointer to exact match flag
337 * The first entry in the tree greater than or equal to key,
338 * or RET_SPECIAL if no such key exists.
341 __bt_first(t, key, erval, exactp)
352 * Find any matching record; __bt_search pins the page.
354 * If it's an exact match and duplicates are possible, walk backwards
355 * in the tree until we find the first one. Otherwise, make sure it's
356 * a valid key (__bt_search may return an index just past the end of a
357 * page) and return it.
359 if ((ep = __bt_search(t, key, exactp)) == NULL)
362 if (F_ISSET(t, B_NODUPS)) {
364 return (RET_SUCCESS);
368 * Walk backwards, as long as the entry matches and there are
369 * keys left in the tree. Save a copy of each match in case
375 if (save.page->pgno != ep->page->pgno) {
376 mpool_put(t->bt_mp, save.page, 0);
379 save.index = ep->index;
382 * Don't unpin the page the last (or original) match
383 * was on, but make sure it's unpinned if an error
386 if (ep->index == 0) {
387 if (h->prevpg == P_INVALID)
389 if (h->pgno != save.page->pgno)
390 mpool_put(t->bt_mp, h, 0);
391 if ((h = mpool_get(t->bt_mp,
392 h->prevpg, 0)) == NULL) {
393 if (h->pgno == save.page->pgno)
399 ep->index = NEXTINDEX(h);
402 } while (__bt_cmp(t, key, ep) == 0);
405 * Reach here with the last page that was looked at pinned,
406 * which may or may not be the same as the last (or original)
407 * match page. If it's not useful, release it.
409 if (h->pgno != save.page->pgno)
410 mpool_put(t->bt_mp, h, 0);
413 return (RET_SUCCESS);
416 /* If at the end of a page, find the next entry. */
417 if (ep->index == NEXTINDEX(ep->page)) {
420 mpool_put(t->bt_mp, h, 0);
422 return (RET_SPECIAL);
423 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
429 return (RET_SUCCESS);
434 * Set the cursor to an entry in the tree.
442 __bt_setcur(t, pgno, index)
447 /* Lose any already deleted key. */
448 if (t->bt_cursor.key.data != NULL) {
449 free(t->bt_cursor.key.data);
450 t->bt_cursor.key.size = 0;
451 t->bt_cursor.key.data = NULL;
453 F_CLR(&t->bt_cursor, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE);
455 /* Update the cursor. */
456 t->bt_cursor.pg.pgno = pgno;
457 t->bt_cursor.pg.index = index;
458 F_SET(&t->bt_cursor, CURS_INIT);