]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_seq.c
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char sccsid
[] = "@(#)bt_seq.c 8.7 (Berkeley) 7/20/94";
63 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
66 #include <sys/types.h>
76 static int __bt_first(BTREE
*, const DBT
*, EPG
*, int *);
77 static int __bt_seqadv(BTREE
*, EPG
*, int);
78 static int __bt_seqset(BTREE
*, EPG
*, DBT
*, int);
81 * Sequential scan support.
83 * The tree can be scanned sequentially, starting from either end of the
84 * tree or from any specific key. A scan request before any scanning is
85 * done is initialized as starting from the least node.
90 * Btree sequential scan interface.
93 * dbp: pointer to access method
94 * key: key for positioning and return value
95 * data: data return value
96 * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
99 * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
102 __bt_seq(dbp
, key
, data
, flags
)
113 /* Toss any page pinned across calls. */
114 if (t
->bt_pinned
!= NULL
) {
115 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
120 * If scan unitialized as yet, or starting at a specific record, set
121 * the scan to a specific key. Both __bt_seqset and __bt_seqadv pin
122 * the page the cursor references if they're successful.
127 if (F_ISSET(&t
->bt_cursor
, CURS_INIT
)) {
128 status
= __bt_seqadv(t
, &e
, flags
);
135 status
= __bt_seqset(t
, &e
, key
, flags
);
142 if (status
== RET_SUCCESS
) {
143 __bt_setcur(t
, e
.page
->pgno
, e
.index
);
146 __bt_ret(t
, &e
, key
, &t
->bt_rkey
, data
, &t
->bt_rdata
, 0);
149 * If the user is doing concurrent access, we copied the
150 * key/data, toss the page.
152 if (F_ISSET(t
, B_DB_LOCK
))
153 mpool_put(t
->bt_mp
, e
.page
, 0);
155 t
->bt_pinned
= e
.page
;
162 * Set the sequential scan to a specific key.
166 * ep: storage for returned key
167 * key: key for initial scan position
168 * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV
171 * Pins the page the cursor references.
174 * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
177 __bt_seqset(t
, ep
, key
, flags
)
188 * Find the first, last or specific key in the tree and point the
189 * cursor at it. The cursor may not be moved until a new key has
193 case R_CURSOR
: /* Keyed scan. */
195 * Find the first instance of the key or the smallest key
196 * which is greater than or equal to the specified key.
198 if (key
->data
== NULL
|| key
->size
== 0) {
202 return (__bt_first(t
, key
, ep
, &exact
));
203 case R_FIRST
: /* First record. */
205 /* Walk down the left-hand side of the tree. */
206 for (pg
= P_ROOT
;;) {
207 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
210 /* Check for an empty tree. */
211 if (NEXTINDEX(h
) == 0) {
212 mpool_put(t
->bt_mp
, h
, 0);
213 return (RET_SPECIAL
);
216 if (h
->flags
& (P_BLEAF
| P_RLEAF
))
218 pg
= GETBINTERNAL(h
, 0)->pgno
;
219 mpool_put(t
->bt_mp
, h
, 0);
224 case R_LAST
: /* Last record. */
226 /* Walk down the right-hand side of the tree. */
227 for (pg
= P_ROOT
;;) {
228 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
231 /* Check for an empty tree. */
232 if (NEXTINDEX(h
) == 0) {
233 mpool_put(t
->bt_mp
, h
, 0);
234 return (RET_SPECIAL
);
237 if (h
->flags
& (P_BLEAF
| P_RLEAF
))
239 pg
= GETBINTERNAL(h
, NEXTINDEX(h
) - 1)->pgno
;
240 mpool_put(t
->bt_mp
, h
, 0);
244 ep
->index
= NEXTINDEX(h
) - 1;
247 return (RET_SUCCESS
);
252 * Advance the sequential scan.
256 * flags: R_NEXT, R_PREV
259 * Pins the page the new key/data record is on.
262 * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
265 __bt_seqadv(t
, ep
, flags
)
277 * There are a couple of states that we can be in. The cursor has
278 * been initialized by the time we get here, but that's all we know.
283 * The cursor was deleted where there weren't any duplicate records,
284 * so the key was saved. Find out where that key would go in the
285 * current tree. It doesn't matter if the returned key is an exact
286 * match or not -- if it's an exact match, the record was added after
287 * the delete so we can just return it. If not, as long as there's
288 * a record there, return it.
290 if (F_ISSET(c
, CURS_ACQUIRE
))
291 return (__bt_first(t
, &c
->key
, ep
, &exact
));
293 /* Get the page referenced by the cursor. */
294 if ((h
= mpool_get(t
->bt_mp
, c
->pg
.pgno
, 0)) == NULL
)
298 * Find the next/previous record in the tree and point the cursor at
299 * it. The cursor may not be moved until a new key has been found.
302 case R_NEXT
: /* Next record. */
304 * The cursor was deleted in duplicate records, and moved
305 * forward to a record that has yet to be returned. Clear
306 * that flag, and return the record.
308 if (F_ISSET(c
, CURS_AFTER
))
311 if (++index
== NEXTINDEX(h
)) {
313 mpool_put(t
->bt_mp
, h
, 0);
315 return (RET_SPECIAL
);
316 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
321 case R_PREV
: /* Previous record. */
323 * The cursor was deleted in duplicate records, and moved
324 * backward to a record that has yet to be returned. Clear
325 * that flag, and return the record.
327 if (F_ISSET(c
, CURS_BEFORE
)) {
328 usecurrent
: F_CLR(c
, CURS_AFTER
| CURS_BEFORE
);
330 ep
->index
= c
->pg
.index
;
331 return (RET_SUCCESS
);
336 mpool_put(t
->bt_mp
, h
, 0);
338 return (RET_SPECIAL
);
339 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
341 index
= NEXTINDEX(h
) - 1;
349 return (RET_SUCCESS
);
354 * Find the first entry.
360 * exactp: pointer to exact match flag
363 * The first entry in the tree greater than or equal to key,
364 * or RET_SPECIAL if no such key exists.
367 __bt_first(t
, key
, erval
, exactp
)
378 * Find any matching record; __bt_search pins the page.
380 * If it's an exact match and duplicates are possible, walk backwards
381 * in the tree until we find the first one. Otherwise, make sure it's
382 * a valid key (__bt_search may return an index just past the end of a
383 * page) and return it.
385 if ((ep
= __bt_search(t
, key
, exactp
)) == NULL
)
388 if (F_ISSET(t
, B_NODUPS
)) {
390 return (RET_SUCCESS
);
394 * Walk backwards, as long as the entry matches and there are
395 * keys left in the tree. Save a copy of each match in case
401 if (save
.page
->pgno
!= ep
->page
->pgno
) {
402 mpool_put(t
->bt_mp
, save
.page
, 0);
405 save
.index
= ep
->index
;
408 * Don't unpin the page the last (or original) match
409 * was on, but make sure it's unpinned if an error
412 if (ep
->index
== 0) {
413 if (h
->prevpg
== P_INVALID
)
415 if (h
->pgno
!= save
.page
->pgno
)
416 mpool_put(t
->bt_mp
, h
, 0);
417 if ((h
= mpool_get(t
->bt_mp
,
418 h
->prevpg
, 0)) == NULL
) {
419 if (h
->pgno
== save
.page
->pgno
)
425 ep
->index
= NEXTINDEX(h
);
428 } while (__bt_cmp(t
, key
, ep
) == 0);
431 * Reach here with the last page that was looked at pinned,
432 * which may or may not be the same as the last (or original)
433 * match page. If it's not useful, release it.
435 if (h
->pgno
!= save
.page
->pgno
)
436 mpool_put(t
->bt_mp
, h
, 0);
439 return (RET_SUCCESS
);
442 /* If at the end of a page, find the next entry. */
443 if (ep
->index
== NEXTINDEX(ep
->page
)) {
446 mpool_put(t
->bt_mp
, h
, 0);
448 return (RET_SPECIAL
);
449 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
455 return (RET_SUCCESS
);
460 * Set the cursor to an entry in the tree.
468 __bt_setcur(t
, pgno
, index
)
473 /* Lose any already deleted key. */
474 if (t
->bt_cursor
.key
.data
!= NULL
) {
475 free(t
->bt_cursor
.key
.data
);
476 t
->bt_cursor
.key
.size
= 0;
477 t
->bt_cursor
.key
.data
= NULL
;
479 F_CLR(&t
->bt_cursor
, CURS_ACQUIRE
| CURS_AFTER
| CURS_BEFORE
);
481 /* Update the cursor. */
482 t
->bt_cursor
.pg
.pgno
= pgno
;
483 t
->bt_cursor
.pg
.index
= index
;
484 F_SET(&t
->bt_cursor
, CURS_INIT
);