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