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