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