2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)rec_get.c 8.9 (Berkeley) 8/18/94";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
49 * __REC_GET -- Get a record from the btree.
52 * dbp: pointer to access method
54 * data: data to return
55 * flag: currently unused
58 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
61 __rec_get(dbp, key, data, flags)
74 /* Toss any page pinned across calls. */
75 if (t->bt_pinned != NULL) {
76 mpool_put(t->bt_mp, t->bt_pinned, 0);
80 /* Get currently doesn't take any flags, and keys of 0 are illegal. */
81 if (flags || (nrec = *(recno_t *)key->data) == 0) {
87 * If we haven't seen this record yet, try to find it in the
90 if (nrec > t->bt_nrecs) {
91 if (F_ISSET(t, R_EOF | R_INMEM))
93 if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
98 if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
101 status = __rec_ret(t, e, 0, NULL, data);
102 if (F_ISSET(t, B_DB_LOCK))
103 mpool_put(t->bt_mp, e->page, 0);
105 t->bt_pinned = e->page;
110 * __REC_FPIPE -- Get fixed length records from a pipe.
114 * cnt: records to read
117 * RET_ERROR, RET_SUCCESS
130 if (t->bt_rdata.size < t->bt_reclen) {
131 t->bt_rdata.data = t->bt_rdata.data == NULL ?
132 malloc(t->bt_reclen) :
133 reallocf(t->bt_rdata.data, t->bt_reclen);
134 if (t->bt_rdata.data == NULL)
136 t->bt_rdata.size = t->bt_reclen;
138 data.data = t->bt_rdata.data;
139 data.size = t->bt_reclen;
141 for (nrec = t->bt_nrecs; nrec < top;) {
143 for (p = t->bt_rdata.data;; *p++ = ch)
144 if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
148 memset(p, t->bt_bval, len);
150 nrec, &data, 0) != RET_SUCCESS)
160 return (RET_SPECIAL);
162 return (RET_SUCCESS);
166 * __REC_VPIPE -- Get variable length records from a pipe.
170 * cnt: records to read
173 * RET_ERROR, RET_SUCCESS
188 for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
189 for (p = t->bt_rdata.data,
190 sz = t->bt_rdata.size;; *p++ = ch, --sz) {
191 if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
192 data.data = t->bt_rdata.data;
193 data.size = p - (u_char *)t->bt_rdata.data;
194 if (ch == EOF && data.size == 0)
196 if (__rec_iput(t, nrec, &data, 0)
202 len = p - (u_char *)t->bt_rdata.data;
203 t->bt_rdata.size += (sz = 256);
204 t->bt_rdata.data = t->bt_rdata.data == NULL ?
205 malloc(t->bt_rdata.size) :
206 reallocf(t->bt_rdata.data, t->bt_rdata.size);
207 if (t->bt_rdata.data == NULL)
209 p = (u_char *)t->bt_rdata.data + len;
217 return (RET_SPECIAL);
219 return (RET_SUCCESS);
223 * __REC_FMAP -- Get fixed length records from a file.
227 * cnt: records to read
230 * RET_ERROR, RET_SUCCESS
242 if (t->bt_rdata.size < t->bt_reclen) {
243 t->bt_rdata.data = t->bt_rdata.data == NULL ?
244 malloc(t->bt_reclen) :
245 reallocf(t->bt_rdata.data, t->bt_reclen);
246 if (t->bt_rdata.data == NULL)
248 t->bt_rdata.size = t->bt_reclen;
250 data.data = t->bt_rdata.data;
251 data.size = t->bt_reclen;
253 sp = (u_char *)t->bt_cmap;
254 ep = (u_char *)t->bt_emap;
255 for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
258 return (RET_SPECIAL);
261 for (p = t->bt_rdata.data;
262 sp < ep && len > 0; *p++ = *sp++, --len);
264 memset(p, t->bt_bval, len);
265 if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
268 t->bt_cmap = (caddr_t)sp;
269 return (RET_SUCCESS);
273 * __REC_VMAP -- Get variable length records from a file.
277 * cnt: records to read
280 * RET_ERROR, RET_SUCCESS
292 sp = (u_char *)t->bt_cmap;
293 ep = (u_char *)t->bt_emap;
296 for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
299 return (RET_SPECIAL);
301 for (data.data = sp; sp < ep && *sp != bval; ++sp);
302 data.size = sp - (u_char *)data.data;
303 if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
307 t->bt_cmap = (caddr_t)sp;
308 return (RET_SUCCESS);