rec_get.c revision 1.4 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1990, 1993
3 1.1 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
35 1.4 cgd /* from: static char sccsid[] = "@(#)rec_get.c 8.2 (Berkeley) 9/7/93"; */
36 1.4 cgd static char *rcsid = "$Id: rec_get.c,v 1.4 1993/09/09 02:42:23 cgd Exp $";
37 1.1 cgd #endif /* LIBC_SCCS and not lint */
38 1.1 cgd
39 1.1 cgd #include <sys/types.h>
40 1.1 cgd
41 1.1 cgd #include <errno.h>
42 1.1 cgd #include <stddef.h>
43 1.1 cgd #include <stdio.h>
44 1.1 cgd #include <stdlib.h>
45 1.1 cgd #include <string.h>
46 1.1 cgd #include <unistd.h>
47 1.1 cgd
48 1.1 cgd #include <db.h>
49 1.1 cgd #include "recno.h"
50 1.1 cgd
51 1.1 cgd /*
52 1.1 cgd * __REC_GET -- Get a record from the btree.
53 1.1 cgd *
54 1.1 cgd * Parameters:
55 1.1 cgd * dbp: pointer to access method
56 1.1 cgd * key: key to find
57 1.1 cgd * data: data to return
58 1.1 cgd * flag: currently unused
59 1.1 cgd *
60 1.1 cgd * Returns:
61 1.1 cgd * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
62 1.1 cgd */
63 1.1 cgd int
64 1.1 cgd __rec_get(dbp, key, data, flags)
65 1.1 cgd const DB *dbp;
66 1.1 cgd const DBT *key;
67 1.1 cgd DBT *data;
68 1.1 cgd u_int flags;
69 1.1 cgd {
70 1.1 cgd BTREE *t;
71 1.1 cgd EPG *e;
72 1.1 cgd recno_t nrec;
73 1.1 cgd int status;
74 1.1 cgd
75 1.4 cgd t = dbp->internal;
76 1.4 cgd
77 1.4 cgd /* Toss any page pinned across calls. */
78 1.4 cgd if (t->bt_pinned != NULL) {
79 1.4 cgd mpool_put(t->bt_mp, t->bt_pinned, 0);
80 1.4 cgd t->bt_pinned = NULL;
81 1.4 cgd }
82 1.4 cgd
83 1.4 cgd /* Get currently doesn't take any flags, and keys of 0 are illegal. */
84 1.1 cgd if (flags || (nrec = *(recno_t *)key->data) == 0) {
85 1.1 cgd errno = EINVAL;
86 1.1 cgd return (RET_ERROR);
87 1.1 cgd }
88 1.1 cgd
89 1.1 cgd /*
90 1.1 cgd * If we haven't seen this record yet, try to find it in the
91 1.1 cgd * original file.
92 1.1 cgd */
93 1.1 cgd if (nrec > t->bt_nrecs) {
94 1.1 cgd if (ISSET(t, R_EOF | R_INMEM))
95 1.1 cgd return (RET_SPECIAL);
96 1.1 cgd if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
97 1.1 cgd return (status);
98 1.1 cgd }
99 1.1 cgd
100 1.1 cgd --nrec;
101 1.1 cgd if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
102 1.1 cgd return (RET_ERROR);
103 1.1 cgd
104 1.1 cgd status = __rec_ret(t, e, 0, NULL, data);
105 1.4 cgd if (ISSET(t, B_DB_LOCK))
106 1.4 cgd mpool_put(t->bt_mp, e->page, 0);
107 1.4 cgd else
108 1.4 cgd t->bt_pinned = e->page;
109 1.1 cgd return (status);
110 1.1 cgd }
111 1.1 cgd
112 1.1 cgd /*
113 1.1 cgd * __REC_FPIPE -- Get fixed length records from a pipe.
114 1.1 cgd *
115 1.1 cgd * Parameters:
116 1.1 cgd * t: tree
117 1.1 cgd * cnt: records to read
118 1.1 cgd *
119 1.1 cgd * Returns:
120 1.1 cgd * RET_ERROR, RET_SUCCESS
121 1.1 cgd */
122 1.1 cgd int
123 1.1 cgd __rec_fpipe(t, top)
124 1.1 cgd BTREE *t;
125 1.1 cgd recno_t top;
126 1.1 cgd {
127 1.1 cgd DBT data;
128 1.1 cgd recno_t nrec;
129 1.1 cgd size_t len;
130 1.1 cgd int ch;
131 1.1 cgd char *p;
132 1.1 cgd
133 1.1 cgd data.data = t->bt_dbuf;
134 1.1 cgd data.size = t->bt_reclen;
135 1.1 cgd
136 1.1 cgd if (t->bt_dbufsz < t->bt_reclen) {
137 1.1 cgd if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
138 1.1 cgd return (RET_ERROR);
139 1.1 cgd t->bt_dbufsz = t->bt_reclen;
140 1.1 cgd }
141 1.1 cgd for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
142 1.1 cgd len = t->bt_reclen;
143 1.1 cgd for (p = t->bt_dbuf;; *p++ = ch)
144 1.1 cgd if ((ch = getc(t->bt_rfp)) == EOF || !len--) {
145 1.1 cgd if (__rec_iput(t, nrec, &data, 0)
146 1.1 cgd != RET_SUCCESS)
147 1.1 cgd return (RET_ERROR);
148 1.1 cgd break;
149 1.1 cgd }
150 1.1 cgd if (ch == EOF)
151 1.1 cgd break;
152 1.1 cgd }
153 1.1 cgd if (nrec < top) {
154 1.1 cgd SET(t, R_EOF);
155 1.1 cgd return (RET_SPECIAL);
156 1.1 cgd }
157 1.1 cgd return (RET_SUCCESS);
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * __REC_VPIPE -- Get variable length records from a pipe.
162 1.1 cgd *
163 1.1 cgd * Parameters:
164 1.1 cgd * t: tree
165 1.1 cgd * cnt: records to read
166 1.1 cgd *
167 1.1 cgd * Returns:
168 1.1 cgd * RET_ERROR, RET_SUCCESS
169 1.1 cgd */
170 1.1 cgd int
171 1.1 cgd __rec_vpipe(t, top)
172 1.1 cgd BTREE *t;
173 1.1 cgd recno_t top;
174 1.1 cgd {
175 1.1 cgd DBT data;
176 1.1 cgd recno_t nrec;
177 1.1 cgd indx_t len;
178 1.1 cgd size_t sz;
179 1.1 cgd int bval, ch;
180 1.1 cgd char *p;
181 1.1 cgd
182 1.1 cgd bval = t->bt_bval;
183 1.1 cgd for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
184 1.1 cgd for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) {
185 1.1 cgd if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
186 1.1 cgd data.data = t->bt_dbuf;
187 1.1 cgd data.size = p - t->bt_dbuf;
188 1.1 cgd if (ch == EOF && data.size == 0)
189 1.1 cgd break;
190 1.1 cgd if (__rec_iput(t, nrec, &data, 0)
191 1.1 cgd != RET_SUCCESS)
192 1.1 cgd return (RET_ERROR);
193 1.1 cgd break;
194 1.1 cgd }
195 1.1 cgd if (sz == 0) {
196 1.1 cgd len = p - t->bt_dbuf;
197 1.1 cgd t->bt_dbufsz += (sz = 256);
198 1.1 cgd if ((t->bt_dbuf =
199 1.1 cgd realloc(t->bt_dbuf, t->bt_dbufsz)) == NULL)
200 1.1 cgd return (RET_ERROR);
201 1.1 cgd p = t->bt_dbuf + len;
202 1.1 cgd }
203 1.1 cgd }
204 1.1 cgd if (ch == EOF)
205 1.1 cgd break;
206 1.1 cgd }
207 1.1 cgd if (nrec < top) {
208 1.1 cgd SET(t, R_EOF);
209 1.1 cgd return (RET_SPECIAL);
210 1.1 cgd }
211 1.1 cgd return (RET_SUCCESS);
212 1.1 cgd }
213 1.1 cgd
214 1.1 cgd /*
215 1.1 cgd * __REC_FMAP -- Get fixed length records from a file.
216 1.1 cgd *
217 1.1 cgd * Parameters:
218 1.1 cgd * t: tree
219 1.1 cgd * cnt: records to read
220 1.1 cgd *
221 1.1 cgd * Returns:
222 1.1 cgd * RET_ERROR, RET_SUCCESS
223 1.1 cgd */
224 1.1 cgd int
225 1.1 cgd __rec_fmap(t, top)
226 1.1 cgd BTREE *t;
227 1.1 cgd recno_t top;
228 1.1 cgd {
229 1.1 cgd DBT data;
230 1.1 cgd recno_t nrec;
231 1.1 cgd caddr_t sp, ep;
232 1.1 cgd size_t len;
233 1.1 cgd char *p;
234 1.1 cgd
235 1.1 cgd sp = t->bt_cmap;
236 1.1 cgd ep = t->bt_emap;
237 1.1 cgd data.data = t->bt_dbuf;
238 1.1 cgd data.size = t->bt_reclen;
239 1.1 cgd
240 1.1 cgd if (t->bt_dbufsz < t->bt_reclen) {
241 1.1 cgd if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
242 1.1 cgd return (RET_ERROR);
243 1.1 cgd t->bt_dbufsz = t->bt_reclen;
244 1.1 cgd }
245 1.1 cgd for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
246 1.1 cgd if (sp >= ep) {
247 1.1 cgd SET(t, R_EOF);
248 1.1 cgd return (RET_SPECIAL);
249 1.1 cgd }
250 1.1 cgd len = t->bt_reclen;
251 1.1 cgd for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++);
252 1.1 cgd memset(p, t->bt_bval, len);
253 1.1 cgd if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
254 1.1 cgd return (RET_ERROR);
255 1.1 cgd }
256 1.1 cgd t->bt_cmap = sp;
257 1.1 cgd return (RET_SUCCESS);
258 1.1 cgd }
259 1.1 cgd
260 1.1 cgd /*
261 1.1 cgd * __REC_VMAP -- Get variable length records from a file.
262 1.1 cgd *
263 1.1 cgd * Parameters:
264 1.1 cgd * t: tree
265 1.1 cgd * cnt: records to read
266 1.1 cgd *
267 1.1 cgd * Returns:
268 1.1 cgd * RET_ERROR, RET_SUCCESS
269 1.1 cgd */
270 1.1 cgd int
271 1.1 cgd __rec_vmap(t, top)
272 1.1 cgd BTREE *t;
273 1.1 cgd recno_t top;
274 1.1 cgd {
275 1.1 cgd DBT data;
276 1.1 cgd caddr_t sp, ep;
277 1.1 cgd recno_t nrec;
278 1.1 cgd int bval;
279 1.1 cgd
280 1.1 cgd sp = t->bt_cmap;
281 1.1 cgd ep = t->bt_emap;
282 1.1 cgd bval = t->bt_bval;
283 1.1 cgd
284 1.1 cgd for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
285 1.1 cgd if (sp >= ep) {
286 1.1 cgd SET(t, R_EOF);
287 1.1 cgd return (RET_SPECIAL);
288 1.1 cgd }
289 1.1 cgd for (data.data = sp; sp < ep && *sp != bval; ++sp);
290 1.1 cgd data.size = sp - (caddr_t)data.data;
291 1.1 cgd if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
292 1.1 cgd return (RET_ERROR);
293 1.1 cgd ++sp;
294 1.1 cgd }
295 1.1 cgd t->bt_cmap = sp;
296 1.1 cgd return (RET_SUCCESS);
297 1.1 cgd }
298