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