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