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