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