bt_seq.c revision 1.6 1 1.6 cgd /* $NetBSD: bt_seq.c,v 1.6 1995/02/27 13:20:51 cgd Exp $ */
2 1.6 cgd
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1990, 1993
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.5 cgd static char sccsid[] = "@(#)bt_seq.c 8.2 (Berkeley) 9/7/93";
42 1.6 cgd #else
43 1.6 cgd static char rcsid[] = "$NetBSD: bt_seq.c,v 1.6 1995/02/27 13:20:51 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.1 cgd static int bt_seqadv __P((BTREE *, EPG *, int));
58 1.1 cgd static int bt_seqset __P((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.1 cgd * The tree can be scanned sequentially, starting from either end of the tree
64 1.1 cgd * or from any specific key. A scan request before any scanning is done is
65 1.1 cgd * initialized as starting from the least node.
66 1.1 cgd *
67 1.1 cgd * Each tree has an EPGNO which has the current position of the cursor. The
68 1.1 cgd * cursor has to survive deletions/insertions in the tree without losing its
69 1.1 cgd * position. This is done by noting deletions without doing them, and then
70 1.1 cgd * doing them when the cursor moves (or the tree is closed).
71 1.1 cgd */
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * __BT_SEQ -- Btree sequential scan interface.
75 1.1 cgd *
76 1.1 cgd * Parameters:
77 1.1 cgd * dbp: pointer to access method
78 1.1 cgd * key: key for positioning and return value
79 1.1 cgd * data: data return value
80 1.1 cgd * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
81 1.1 cgd *
82 1.1 cgd * Returns:
83 1.1 cgd * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
84 1.1 cgd */
85 1.1 cgd int
86 1.1 cgd __bt_seq(dbp, key, data, flags)
87 1.1 cgd const DB *dbp;
88 1.1 cgd DBT *key, *data;
89 1.1 cgd u_int flags;
90 1.1 cgd {
91 1.1 cgd BTREE *t;
92 1.1 cgd EPG e;
93 1.1 cgd int status;
94 1.1 cgd
95 1.4 cgd t = dbp->internal;
96 1.4 cgd
97 1.4 cgd /* Toss any page pinned across calls. */
98 1.4 cgd if (t->bt_pinned != NULL) {
99 1.4 cgd mpool_put(t->bt_mp, t->bt_pinned, 0);
100 1.4 cgd t->bt_pinned = NULL;
101 1.4 cgd }
102 1.4 cgd
103 1.1 cgd /*
104 1.1 cgd * If scan unitialized as yet, or starting at a specific record, set
105 1.1 cgd * the scan to a specific key. Both bt_seqset and bt_seqadv pin the
106 1.1 cgd * page the cursor references if they're successful.
107 1.1 cgd */
108 1.1 cgd switch(flags) {
109 1.1 cgd case R_NEXT:
110 1.1 cgd case R_PREV:
111 1.1 cgd if (ISSET(t, B_SEQINIT)) {
112 1.1 cgd status = bt_seqadv(t, &e, flags);
113 1.1 cgd break;
114 1.1 cgd }
115 1.1 cgd /* FALLTHROUGH */
116 1.1 cgd case R_CURSOR:
117 1.1 cgd case R_FIRST:
118 1.1 cgd case R_LAST:
119 1.1 cgd status = bt_seqset(t, &e, key, flags);
120 1.1 cgd break;
121 1.1 cgd default:
122 1.1 cgd errno = EINVAL;
123 1.1 cgd return (RET_ERROR);
124 1.1 cgd }
125 1.1 cgd
126 1.1 cgd if (status == RET_SUCCESS) {
127 1.1 cgd status = __bt_ret(t, &e, key, data);
128 1.1 cgd
129 1.1 cgd /* Update the actual cursor. */
130 1.1 cgd t->bt_bcursor.pgno = e.page->pgno;
131 1.1 cgd t->bt_bcursor.index = e.index;
132 1.4 cgd
133 1.4 cgd /*
134 1.4 cgd * If the user is doing concurrent access, we copied the
135 1.4 cgd * key/data, toss the page.
136 1.4 cgd */
137 1.4 cgd if (ISSET(t, B_DB_LOCK))
138 1.4 cgd mpool_put(t->bt_mp, e.page, 0);
139 1.4 cgd else
140 1.4 cgd t->bt_pinned = e.page;
141 1.1 cgd SET(t, B_SEQINIT);
142 1.1 cgd }
143 1.1 cgd return (status);
144 1.1 cgd }
145 1.1 cgd
146 1.1 cgd /*
147 1.1 cgd * BT_SEQSET -- Set the sequential scan to a specific key.
148 1.1 cgd *
149 1.1 cgd * Parameters:
150 1.1 cgd * t: tree
151 1.1 cgd * ep: storage for returned key
152 1.1 cgd * key: key for initial scan position
153 1.1 cgd * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV
154 1.1 cgd *
155 1.1 cgd * Side effects:
156 1.1 cgd * Pins the page the cursor references.
157 1.1 cgd *
158 1.1 cgd * Returns:
159 1.1 cgd * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
160 1.1 cgd */
161 1.1 cgd static int
162 1.1 cgd bt_seqset(t, ep, key, flags)
163 1.1 cgd BTREE *t;
164 1.1 cgd EPG *ep;
165 1.1 cgd DBT *key;
166 1.1 cgd int flags;
167 1.1 cgd {
168 1.1 cgd EPG *e;
169 1.1 cgd PAGE *h;
170 1.1 cgd pgno_t pg;
171 1.1 cgd int exact;
172 1.1 cgd
173 1.1 cgd /*
174 1.1 cgd * Delete any already deleted record that we've been saving because
175 1.1 cgd * the cursor pointed to it. Since going to a specific key, should
176 1.1 cgd * delete any logically deleted records so they aren't found.
177 1.1 cgd */
178 1.1 cgd if (ISSET(t, B_DELCRSR) && __bt_crsrdel(t, &t->bt_bcursor))
179 1.1 cgd return (RET_ERROR);
180 1.1 cgd
181 1.1 cgd /*
182 1.1 cgd * Find the first, last or specific key in the tree and point the cursor
183 1.1 cgd * at it. The cursor may not be moved until a new key has been found.
184 1.1 cgd */
185 1.1 cgd switch(flags) {
186 1.1 cgd case R_CURSOR: /* Keyed scan. */
187 1.1 cgd /*
188 1.1 cgd * Find the first instance of the key or the smallest key which
189 1.1 cgd * is greater than or equal to the specified key. If run out
190 1.1 cgd * of keys, return RET_SPECIAL.
191 1.1 cgd */
192 1.1 cgd if (key->data == NULL || key->size == 0) {
193 1.1 cgd errno = EINVAL;
194 1.1 cgd return (RET_ERROR);
195 1.1 cgd }
196 1.1 cgd e = __bt_first(t, key, &exact); /* Returns pinned page. */
197 1.1 cgd if (e == NULL)
198 1.1 cgd return (RET_ERROR);
199 1.1 cgd /*
200 1.1 cgd * If at the end of a page, skip any empty pages and find the
201 1.1 cgd * next entry.
202 1.1 cgd */
203 1.1 cgd if (e->index == NEXTINDEX(e->page)) {
204 1.1 cgd h = e->page;
205 1.1 cgd do {
206 1.1 cgd pg = h->nextpg;
207 1.1 cgd mpool_put(t->bt_mp, h, 0);
208 1.1 cgd if (pg == P_INVALID)
209 1.1 cgd return (RET_SPECIAL);
210 1.1 cgd if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
211 1.1 cgd return (RET_ERROR);
212 1.1 cgd } while (NEXTINDEX(h) == 0);
213 1.1 cgd e->index = 0;
214 1.1 cgd e->page = h;
215 1.1 cgd }
216 1.1 cgd *ep = *e;
217 1.1 cgd break;
218 1.1 cgd case R_FIRST: /* First record. */
219 1.1 cgd case R_NEXT:
220 1.1 cgd /* Walk down the left-hand side of the tree. */
221 1.1 cgd for (pg = P_ROOT;;) {
222 1.1 cgd if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
223 1.1 cgd return (RET_ERROR);
224 1.1 cgd if (h->flags & (P_BLEAF | P_RLEAF))
225 1.1 cgd break;
226 1.1 cgd pg = GETBINTERNAL(h, 0)->pgno;
227 1.1 cgd mpool_put(t->bt_mp, h, 0);
228 1.1 cgd }
229 1.1 cgd
230 1.1 cgd /* Skip any empty pages. */
231 1.1 cgd while (NEXTINDEX(h) == 0 && h->nextpg != P_INVALID) {
232 1.1 cgd pg = h->nextpg;
233 1.1 cgd mpool_put(t->bt_mp, h, 0);
234 1.1 cgd if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
235 1.1 cgd return (RET_ERROR);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd if (NEXTINDEX(h) == 0) {
239 1.1 cgd mpool_put(t->bt_mp, h, 0);
240 1.1 cgd return (RET_SPECIAL);
241 1.1 cgd }
242 1.1 cgd
243 1.1 cgd ep->page = h;
244 1.1 cgd ep->index = 0;
245 1.1 cgd break;
246 1.1 cgd case R_LAST: /* Last record. */
247 1.1 cgd case R_PREV:
248 1.1 cgd /* Walk down the right-hand side of the tree. */
249 1.1 cgd for (pg = P_ROOT;;) {
250 1.1 cgd if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
251 1.1 cgd return (RET_ERROR);
252 1.1 cgd if (h->flags & (P_BLEAF | P_RLEAF))
253 1.1 cgd break;
254 1.1 cgd pg = GETBINTERNAL(h, NEXTINDEX(h) - 1)->pgno;
255 1.1 cgd mpool_put(t->bt_mp, h, 0);
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd /* Skip any empty pages. */
259 1.1 cgd while (NEXTINDEX(h) == 0 && h->prevpg != P_INVALID) {
260 1.1 cgd pg = h->prevpg;
261 1.1 cgd mpool_put(t->bt_mp, h, 0);
262 1.1 cgd if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
263 1.1 cgd return (RET_ERROR);
264 1.1 cgd }
265 1.1 cgd
266 1.1 cgd if (NEXTINDEX(h) == 0) {
267 1.1 cgd mpool_put(t->bt_mp, h, 0);
268 1.1 cgd return (RET_SPECIAL);
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd ep->page = h;
272 1.1 cgd ep->index = NEXTINDEX(h) - 1;
273 1.1 cgd break;
274 1.1 cgd }
275 1.1 cgd return (RET_SUCCESS);
276 1.1 cgd }
277 1.1 cgd
278 1.1 cgd /*
279 1.1 cgd * BT_SEQADVANCE -- Advance the sequential scan.
280 1.1 cgd *
281 1.1 cgd * Parameters:
282 1.1 cgd * t: tree
283 1.1 cgd * flags: R_NEXT, R_PREV
284 1.1 cgd *
285 1.1 cgd * Side effects:
286 1.1 cgd * Pins the page the new key/data record is on.
287 1.1 cgd *
288 1.1 cgd * Returns:
289 1.1 cgd * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
290 1.1 cgd */
291 1.1 cgd static int
292 1.1 cgd bt_seqadv(t, e, flags)
293 1.1 cgd BTREE *t;
294 1.1 cgd EPG *e;
295 1.1 cgd int flags;
296 1.1 cgd {
297 1.1 cgd EPGNO *c, delc;
298 1.1 cgd PAGE *h;
299 1.1 cgd indx_t index;
300 1.1 cgd pgno_t pg;
301 1.1 cgd
302 1.1 cgd /* Save the current cursor if going to delete it. */
303 1.1 cgd c = &t->bt_bcursor;
304 1.1 cgd if (ISSET(t, B_DELCRSR))
305 1.1 cgd delc = *c;
306 1.1 cgd
307 1.1 cgd if ((h = mpool_get(t->bt_mp, c->pgno, 0)) == NULL)
308 1.1 cgd return (RET_ERROR);
309 1.1 cgd
310 1.1 cgd /*
311 1.1 cgd * Find the next/previous record in the tree and point the cursor at it.
312 1.1 cgd * The cursor may not be moved until a new key has been found.
313 1.1 cgd */
314 1.1 cgd index = c->index;
315 1.1 cgd switch(flags) {
316 1.1 cgd case R_NEXT: /* Next record. */
317 1.1 cgd if (++index == NEXTINDEX(h)) {
318 1.1 cgd do {
319 1.1 cgd pg = h->nextpg;
320 1.1 cgd mpool_put(t->bt_mp, h, 0);
321 1.1 cgd if (pg == P_INVALID)
322 1.1 cgd return (RET_SPECIAL);
323 1.1 cgd if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
324 1.1 cgd return (RET_ERROR);
325 1.1 cgd } while (NEXTINDEX(h) == 0);
326 1.1 cgd index = 0;
327 1.1 cgd }
328 1.1 cgd break;
329 1.1 cgd case R_PREV: /* Previous record. */
330 1.1 cgd if (index-- == 0) {
331 1.1 cgd do {
332 1.1 cgd pg = h->prevpg;
333 1.1 cgd mpool_put(t->bt_mp, h, 0);
334 1.1 cgd if (pg == P_INVALID)
335 1.1 cgd return (RET_SPECIAL);
336 1.1 cgd if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
337 1.1 cgd return (RET_ERROR);
338 1.1 cgd } while (NEXTINDEX(h) == 0);
339 1.1 cgd index = NEXTINDEX(h) - 1;
340 1.1 cgd }
341 1.1 cgd break;
342 1.1 cgd }
343 1.1 cgd
344 1.1 cgd e->page = h;
345 1.1 cgd e->index = index;
346 1.1 cgd
347 1.1 cgd /*
348 1.1 cgd * Delete any already deleted record that we've been saving because the
349 1.1 cgd * cursor pointed to it. This could cause the new index to be shifted
350 1.1 cgd * down by one if the record we're deleting is on the same page and has
351 1.1 cgd * a larger index.
352 1.1 cgd */
353 1.1 cgd if (ISSET(t, B_DELCRSR)) {
354 1.1 cgd CLR(t, B_DELCRSR); /* Don't try twice. */
355 1.1 cgd if (c->pgno == delc.pgno && c->index > delc.index)
356 1.1 cgd --c->index;
357 1.1 cgd if (__bt_crsrdel(t, &delc))
358 1.1 cgd return (RET_ERROR);
359 1.1 cgd }
360 1.1 cgd return (RET_SUCCESS);
361 1.1 cgd }
362 1.1 cgd
363 1.1 cgd /*
364 1.1 cgd * __BT_CRSRDEL -- Delete the record referenced by the cursor.
365 1.1 cgd *
366 1.1 cgd * Parameters:
367 1.1 cgd * t: tree
368 1.1 cgd *
369 1.1 cgd * Returns:
370 1.1 cgd * RET_ERROR, RET_SUCCESS
371 1.1 cgd */
372 1.1 cgd int
373 1.1 cgd __bt_crsrdel(t, c)
374 1.1 cgd BTREE *t;
375 1.1 cgd EPGNO *c;
376 1.1 cgd {
377 1.1 cgd PAGE *h;
378 1.1 cgd int status;
379 1.1 cgd
380 1.1 cgd CLR(t, B_DELCRSR); /* Don't try twice. */
381 1.1 cgd if ((h = mpool_get(t->bt_mp, c->pgno, 0)) == NULL)
382 1.1 cgd return (RET_ERROR);
383 1.1 cgd status = __bt_dleaf(t, h, c->index);
384 1.1 cgd mpool_put(t->bt_mp, h, MPOOL_DIRTY);
385 1.1 cgd return (status);
386 1.1 cgd }
387