1*54925bf6Swillf /*-
2*54925bf6Swillf  * Copyright (c) 1991, 1993, 1994
3*54925bf6Swillf  *	The Regents of the University of California.  All rights reserved.
4*54925bf6Swillf  *
5*54925bf6Swillf  * Redistribution and use in source and binary forms, with or without
6*54925bf6Swillf  * modification, are permitted provided that the following conditions
7*54925bf6Swillf  * are met:
8*54925bf6Swillf  * 1. Redistributions of source code must retain the above copyright
9*54925bf6Swillf  *    notice, this list of conditions and the following disclaimer.
10*54925bf6Swillf  * 2. Redistributions in binary form must reproduce the above copyright
11*54925bf6Swillf  *    notice, this list of conditions and the following disclaimer in the
12*54925bf6Swillf  *    documentation and/or other materials provided with the distribution.
13*54925bf6Swillf  * 3. All advertising materials mentioning features or use of this software
14*54925bf6Swillf  *    must display the following acknowledgement:
15*54925bf6Swillf  *	This product includes software developed by the University of
16*54925bf6Swillf  *	California, Berkeley and its contributors.
17*54925bf6Swillf  * 4. Neither the name of the University nor the names of its contributors
18*54925bf6Swillf  *    may be used to endorse or promote products derived from this software
19*54925bf6Swillf  *    without specific prior written permission.
20*54925bf6Swillf  *
21*54925bf6Swillf  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*54925bf6Swillf  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*54925bf6Swillf  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*54925bf6Swillf  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*54925bf6Swillf  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*54925bf6Swillf  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*54925bf6Swillf  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*54925bf6Swillf  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*54925bf6Swillf  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*54925bf6Swillf  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*54925bf6Swillf  * SUCH DAMAGE.
32*54925bf6Swillf  */
33*54925bf6Swillf 
34*54925bf6Swillf #if defined(LIBC_SCCS) && !defined(lint)
35*54925bf6Swillf static char sccsid[] = "@(#)rec_seq.c	8.3 (Berkeley) 7/14/94";
36*54925bf6Swillf #endif /* not lint */
37*54925bf6Swillf 
38*54925bf6Swillf #include <sys/types.h>
39*54925bf6Swillf 
40*54925bf6Swillf #include <errno.h>
41*54925bf6Swillf #include <limits.h>
42*54925bf6Swillf #include <stdio.h>
43*54925bf6Swillf #include <string.h>
44*54925bf6Swillf 
45*54925bf6Swillf #include "db-int.h"
46*54925bf6Swillf #include "recno.h"
47*54925bf6Swillf 
48*54925bf6Swillf /*
49*54925bf6Swillf  * __REC_SEQ -- Recno sequential scan interface.
50*54925bf6Swillf  *
51*54925bf6Swillf  * Parameters:
52*54925bf6Swillf  *	dbp:	pointer to access method
53*54925bf6Swillf  *	key:	key for positioning and return value
54*54925bf6Swillf  *	data:	data return value
55*54925bf6Swillf  *	flags:	R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
56*54925bf6Swillf  *
57*54925bf6Swillf  * Returns:
58*54925bf6Swillf  *	RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
59*54925bf6Swillf  */
60*54925bf6Swillf int
__rec_seq(dbp,key,data,flags)61*54925bf6Swillf __rec_seq(dbp, key, data, flags)
62*54925bf6Swillf 	const DB *dbp;
63*54925bf6Swillf 	DBT *key, *data;
64*54925bf6Swillf 	u_int flags;
65*54925bf6Swillf {
66*54925bf6Swillf 	BTREE *t;
67*54925bf6Swillf 	EPG *e;
68*54925bf6Swillf 	recno_t nrec;
69*54925bf6Swillf 	int status;
70*54925bf6Swillf 
71*54925bf6Swillf 	t = dbp->internal;
72*54925bf6Swillf 
73*54925bf6Swillf 	/* Toss any page pinned across calls. */
74*54925bf6Swillf 	if (t->bt_pinned != NULL) {
75*54925bf6Swillf 		mpool_put(t->bt_mp, t->bt_pinned, 0);
76*54925bf6Swillf 		t->bt_pinned = NULL;
77*54925bf6Swillf 	}
78*54925bf6Swillf 
79*54925bf6Swillf 	switch(flags) {
80*54925bf6Swillf 	case R_CURSOR:
81*54925bf6Swillf 		if ((nrec = *(recno_t *)key->data) == 0)
82*54925bf6Swillf 			goto einval;
83*54925bf6Swillf 		break;
84*54925bf6Swillf 	case R_NEXT:
85*54925bf6Swillf 		if (F_ISSET(&t->bt_cursor, CURS_INIT)) {
86*54925bf6Swillf 			nrec = t->bt_cursor.rcursor + 1;
87*54925bf6Swillf 			break;
88*54925bf6Swillf 		}
89*54925bf6Swillf 		/* FALLTHROUGH */
90*54925bf6Swillf 	case R_FIRST:
91*54925bf6Swillf 		nrec = 1;
92*54925bf6Swillf 		break;
93*54925bf6Swillf 	case R_PREV:
94*54925bf6Swillf 		if (F_ISSET(&t->bt_cursor, CURS_INIT)) {
95*54925bf6Swillf 			if ((nrec = t->bt_cursor.rcursor - 1) == 0)
96*54925bf6Swillf 				return (RET_SPECIAL);
97*54925bf6Swillf 			break;
98*54925bf6Swillf 		}
99*54925bf6Swillf 		/* FALLTHROUGH */
100*54925bf6Swillf 	case R_LAST:
101*54925bf6Swillf 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
102*54925bf6Swillf 		    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
103*54925bf6Swillf 			return (RET_ERROR);
104*54925bf6Swillf 		nrec = t->bt_nrecs;
105*54925bf6Swillf 		break;
106*54925bf6Swillf 	default:
107*54925bf6Swillf einval:		errno = EINVAL;
108*54925bf6Swillf 		return (RET_ERROR);
109*54925bf6Swillf 	}
110*54925bf6Swillf 
111*54925bf6Swillf 	if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) {
112*54925bf6Swillf 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
113*54925bf6Swillf 		    (status = t->bt_irec(t, nrec)) != RET_SUCCESS)
114*54925bf6Swillf 			return (status);
115*54925bf6Swillf 		if (t->bt_nrecs == 0 || nrec > t->bt_nrecs)
116*54925bf6Swillf 			return (RET_SPECIAL);
117*54925bf6Swillf 	}
118*54925bf6Swillf 
119*54925bf6Swillf 	if ((e = __rec_search(t, nrec - 1, SEARCH)) == NULL)
120*54925bf6Swillf 		return (RET_ERROR);
121*54925bf6Swillf 
122*54925bf6Swillf 	F_SET(&t->bt_cursor, CURS_INIT);
123*54925bf6Swillf 	t->bt_cursor.rcursor = nrec;
124*54925bf6Swillf 
125*54925bf6Swillf 	status = __rec_ret(t, e, nrec, key, data);
126*54925bf6Swillf 	if (F_ISSET(t, B_DB_LOCK))
127*54925bf6Swillf 		mpool_put(t->bt_mp, e->page, 0);
128*54925bf6Swillf 	else
129*54925bf6Swillf 		t->bt_pinned = e->page;
130*54925bf6Swillf 	return (status);
131*54925bf6Swillf }
132