xref: /csrg-svn/lib/libc/db/recno/rec_seq.c (revision 50995)
1*50995Sbostic /*-
2*50995Sbostic  * Copyright (c) 1991 The Regents of the University of California.
3*50995Sbostic  * All rights reserved.
4*50995Sbostic  *
5*50995Sbostic  * %sccs.include.redist.c%
6*50995Sbostic  */
7*50995Sbostic 
8*50995Sbostic #ifndef lint
9*50995Sbostic static char sccsid[] = "@(#)rec_seq.c	5.1 (Berkeley) 09/04/91";
10*50995Sbostic #endif /* not lint */
11*50995Sbostic 
12*50995Sbostic #include <sys/types.h>
13*50995Sbostic #include <errno.h>
14*50995Sbostic #include <db.h>
15*50995Sbostic #include <limits.h>
16*50995Sbostic #include <stdio.h>
17*50995Sbostic #include "../btree/btree.h"
18*50995Sbostic 
19*50995Sbostic /*
20*50995Sbostic  * __REC_SEQ -- Recno sequential scan interface.
21*50995Sbostic  *
22*50995Sbostic  * Parameters:
23*50995Sbostic  *	dbp:	pointer to access method
24*50995Sbostic  *	key:	key for positioning and return value
25*50995Sbostic  *	data:	data return value
26*50995Sbostic  *	flags:	R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
27*50995Sbostic  *
28*50995Sbostic  * Returns:
29*50995Sbostic  *	RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
30*50995Sbostic  */
31*50995Sbostic int
32*50995Sbostic __rec_seq(dbp, key, data, flags)
33*50995Sbostic 	const DB *dbp;
34*50995Sbostic 	DBT *key, *data;
35*50995Sbostic 	u_int flags;
36*50995Sbostic {
37*50995Sbostic 	BTREE *t;
38*50995Sbostic 	EPG *e;
39*50995Sbostic 	recno_t nrec;
40*50995Sbostic 	int exact, status;
41*50995Sbostic 
42*50995Sbostic 	t = dbp->internal;
43*50995Sbostic 	switch(flags) {
44*50995Sbostic 	case R_CURSOR:
45*50995Sbostic 		if ((nrec = *(recno_t *)key->data) == 0) {
46*50995Sbostic 			errno = EINVAL;
47*50995Sbostic 			return (RET_ERROR);
48*50995Sbostic 		}
49*50995Sbostic 		break;
50*50995Sbostic 	case R_NEXT:
51*50995Sbostic 		if (ISSET(t, BTF_SEQINIT)) {
52*50995Sbostic 			nrec = t->bt_rcursor + 1;
53*50995Sbostic 			break;
54*50995Sbostic 		}
55*50995Sbostic 		/* FALLTHROUGH */
56*50995Sbostic 	case R_FIRST:
57*50995Sbostic 		nrec = 1;
58*50995Sbostic 		SET(t, BTF_SEQINIT);
59*50995Sbostic 		break;
60*50995Sbostic 	case R_PREV:
61*50995Sbostic 		if (ISSET(t, BTF_SEQINIT)) {
62*50995Sbostic 			nrec = t->bt_rcursor - 1;
63*50995Sbostic 			break;
64*50995Sbostic 		}
65*50995Sbostic 		/* FALLTHROUGH */
66*50995Sbostic 	case R_LAST:
67*50995Sbostic 		if (t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
68*50995Sbostic 			return (RET_ERROR);
69*50995Sbostic 		nrec = t->bt_nrecs;
70*50995Sbostic 		SET(t, BTF_SEQINIT);
71*50995Sbostic 		break;
72*50995Sbostic 	default:
73*50995Sbostic 		errno = EINVAL;
74*50995Sbostic 		return (RET_ERROR);
75*50995Sbostic 	}
76*50995Sbostic 
77*50995Sbostic 	if (nrec > t->bt_nrecs && (status = t->bt_irec(t, nrec)) != RET_SUCCESS)
78*50995Sbostic 		return (status);
79*50995Sbostic 
80*50995Sbostic 	if ((e = __rec_search(t, nrec - 1, &exact)) == NULL)
81*50995Sbostic 		return (RET_ERROR);
82*50995Sbostic 
83*50995Sbostic 	if (!exact) {
84*50995Sbostic 		mpool_put(t->bt_mp, e->page, 0);
85*50995Sbostic 		return (RET_SPECIAL);
86*50995Sbostic 	}
87*50995Sbostic 
88*50995Sbostic 	if ((status = __rec_ret(t, e, data)) == RET_SUCCESS)
89*50995Sbostic 		t->bt_rcursor = nrec;
90*50995Sbostic 	mpool_put(t->bt_mp, e->page, 0);
91*50995Sbostic 	return (status);
92*50995Sbostic }
93