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