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