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 * 3. 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
29 * @(#)rec_get.c 8.9 (Berkeley) 8/18/94
30 * $FreeBSD: head/lib/libc/db/recno/rec_get.c 189387 2009-03-05 00:57:01Z delphij $
33 #include <sys/types.h>
46 * __REC_GET -- Get a record from the btree.
49 * dbp: pointer to access method
51 * data: data to return
52 * flag: currently unused
55 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
58 __rec_get(const DB *dbp, const DBT *key, DBT *data, unsigned int flags)
67 /* Toss any page pinned across calls. */
68 if (t->bt_pinned != NULL) {
69 mpool_put(t->bt_mp, t->bt_pinned, 0);
73 /* Get currently doesn't take any flags, and keys of 0 are illegal. */
74 if (flags || (nrec = *(recno_t *)key->data) == 0) {
80 * If we haven't seen this record yet, try to find it in the
83 if (nrec > t->bt_nrecs) {
84 if (F_ISSET(t, R_EOF | R_INMEM))
86 if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
91 if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
94 status = __rec_ret(t, e, 0, NULL, data);
95 if (F_ISSET(t, B_DB_LOCK))
96 mpool_put(t->bt_mp, e->page, 0);
98 t->bt_pinned = e->page;
103 * __REC_FPIPE -- Get fixed length records from a pipe.
107 * cnt: records to read
110 * RET_ERROR, RET_SUCCESS
113 __rec_fpipe(BTREE *t, recno_t top)
121 if (t->bt_rdata.size < t->bt_reclen) {
122 t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_reclen);
123 if (t->bt_rdata.data == NULL)
125 t->bt_rdata.size = t->bt_reclen;
127 data.data = t->bt_rdata.data;
128 data.size = t->bt_reclen;
130 for (nrec = t->bt_nrecs; nrec < top;) {
132 for (p = t->bt_rdata.data;; *p++ = ch)
133 if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
137 memset(p, t->bt_bval, len);
139 nrec, &data, 0) != RET_SUCCESS)
149 return (RET_SPECIAL);
151 return (RET_SUCCESS);
155 * __REC_VPIPE -- Get variable length records from a pipe.
159 * cnt: records to read
162 * RET_ERROR, RET_SUCCESS
165 __rec_vpipe(BTREE *t, recno_t top)
175 for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
176 for (p = t->bt_rdata.data,
177 sz = t->bt_rdata.size;; *p++ = ch, --sz) {
178 if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
179 data.data = t->bt_rdata.data;
181 (unsigned char *)t->bt_rdata.data;
182 if (ch == EOF && data.size == 0)
184 if (__rec_iput(t, nrec, &data, 0)
190 len = p - (unsigned char *)t->bt_rdata.data;
191 t->bt_rdata.size += (sz = 256);
192 t->bt_rdata.data = reallocf(t->bt_rdata.data,
194 if (t->bt_rdata.data == NULL)
196 p = (unsigned char *)t->bt_rdata.data + len;
204 return (RET_SPECIAL);
206 return (RET_SUCCESS);
210 * __REC_FMAP -- Get fixed length records from a file.
214 * cnt: records to read
217 * RET_ERROR, RET_SUCCESS
220 __rec_fmap(BTREE *t, recno_t top)
224 unsigned char *sp, *ep, *p;
227 if (t->bt_rdata.size < t->bt_reclen) {
228 t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_reclen);
229 if (t->bt_rdata.data == NULL)
231 t->bt_rdata.size = t->bt_reclen;
233 data.data = t->bt_rdata.data;
234 data.size = t->bt_reclen;
236 sp = (unsigned char *)t->bt_cmap;
237 ep = (unsigned char *)t->bt_emap;
238 for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
241 return (RET_SPECIAL);
244 for (p = t->bt_rdata.data;
245 sp < ep && len > 0; *p++ = *sp++, --len);
247 memset(p, t->bt_bval, len);
248 if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
251 t->bt_cmap = (caddr_t)sp;
252 return (RET_SUCCESS);
256 * __REC_VMAP -- Get variable length records from a file.
260 * cnt: records to read
263 * RET_ERROR, RET_SUCCESS
266 __rec_vmap(BTREE *t, recno_t top)
269 unsigned char *sp, *ep;
273 sp = (unsigned char *)t->bt_cmap;
274 ep = (unsigned char *)t->bt_emap;
277 for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
280 return (RET_SPECIAL);
282 for (data.data = sp; sp < ep && *sp != bval; ++sp);
283 data.size = sp - (unsigned char *)data.data;
284 if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
288 t->bt_cmap = (caddr_t)sp;
289 return (RET_SUCCESS);