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