Commit | Line | Data |
---|---|---|
91b5c9e5 | 1 | /*- |
e659a03d KB |
2 | * Copyright (c) 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
91b5c9e5 | 4 | * |
ad787160 C |
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. | |
91b5c9e5 KB |
32 | */ |
33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | |
ed554bc5 | 35 | static char sccsid[] = "@(#)rec_get.c 8.2 (Berkeley) 9/7/93"; |
91b5c9e5 KB |
36 | #endif /* LIBC_SCCS and not lint */ |
37 | ||
38 | #include <sys/types.h> | |
49858941 | 39 | |
49858941 | 40 | #include <errno.h> |
91b5c9e5 KB |
41 | #include <stddef.h> |
42 | #include <stdio.h> | |
43 | #include <stdlib.h> | |
44 | #include <string.h> | |
49858941 KB |
45 | #include <unistd.h> |
46 | ||
94ac72c5 | 47 | #include <db.h> |
2b6272e5 | 48 | #include "recno.h" |
91b5c9e5 KB |
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; | |
2b6272e5 KB |
65 | const DBT *key; |
66 | DBT *data; | |
91b5c9e5 KB |
67 | u_int flags; |
68 | { | |
69 | BTREE *t; | |
70 | EPG *e; | |
71 | recno_t nrec; | |
30d0d664 | 72 | int status; |
91b5c9e5 | 73 | |
ed554bc5 C |
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. */ | |
91b5c9e5 KB |
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 | */ | |
9f21d024 | 92 | if (nrec > t->bt_nrecs) { |
1eba51f7 | 93 | if (ISSET(t, R_EOF | R_INMEM)) |
9f21d024 KB |
94 | return (RET_SPECIAL); |
95 | if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS) | |
91b5c9e5 | 96 | return (status); |
9f21d024 | 97 | } |
91b5c9e5 KB |
98 | |
99 | --nrec; | |
2b6272e5 | 100 | if ((e = __rec_search(t, nrec, SEARCH)) == NULL) |
91b5c9e5 KB |
101 | return (RET_ERROR); |
102 | ||
49858941 | 103 | status = __rec_ret(t, e, 0, NULL, data); |
ed554bc5 C |
104 | if (ISSET(t, B_DB_LOCK)) |
105 | mpool_put(t->bt_mp, e->page, 0); | |
106 | else | |
107 | t->bt_pinned = e->page; | |
91b5c9e5 KB |
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 | { | |
91b5c9e5 KB |
126 | DBT data; |
127 | recno_t nrec; | |
128 | size_t len; | |
129 | int ch; | |
130 | char *p; | |
131 | ||
91b5c9e5 KB |
132 | data.data = t->bt_dbuf; |
133 | data.size = t->bt_reclen; | |
134 | ||
135 | if (t->bt_dbufsz < t->bt_reclen) { | |
136 | if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL) | |
137 | return (RET_ERROR); | |
138 | t->bt_dbufsz = t->bt_reclen; | |
139 | } | |
140 | for (nrec = t->bt_nrecs; nrec < top; ++nrec) { | |
49858941 | 141 | len = t->bt_reclen; |
91b5c9e5 KB |
142 | for (p = t->bt_dbuf;; *p++ = ch) |
143 | if ((ch = getc(t->bt_rfp)) == EOF || !len--) { | |
144 | if (__rec_iput(t, nrec, &data, 0) | |
145 | != RET_SUCCESS) | |
146 | return (RET_ERROR); | |
147 | break; | |
148 | } | |
149 | if (ch == EOF) | |
150 | break; | |
151 | } | |
152 | if (nrec < top) { | |
1eba51f7 | 153 | SET(t, R_EOF); |
91b5c9e5 KB |
154 | return (RET_SPECIAL); |
155 | } | |
156 | return (RET_SUCCESS); | |
157 | } | |
158 | ||
159 | /* | |
160 | * __REC_VPIPE -- Get variable length records from a pipe. | |
161 | * | |
162 | * Parameters: | |
163 | * t: tree | |
164 | * cnt: records to read | |
165 | * | |
166 | * Returns: | |
167 | * RET_ERROR, RET_SUCCESS | |
168 | */ | |
169 | int | |
170 | __rec_vpipe(t, top) | |
171 | BTREE *t; | |
172 | recno_t top; | |
173 | { | |
91b5c9e5 KB |
174 | DBT data; |
175 | recno_t nrec; | |
93a5373b | 176 | indx_t len; |
91b5c9e5 KB |
177 | size_t sz; |
178 | int bval, ch; | |
179 | char *p; | |
180 | ||
91b5c9e5 KB |
181 | bval = t->bt_bval; |
182 | for (nrec = t->bt_nrecs; nrec < top; ++nrec) { | |
183 | for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) { | |
184 | if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) { | |
185 | data.data = t->bt_dbuf; | |
186 | data.size = p - t->bt_dbuf; | |
9ca6d6c7 KB |
187 | if (ch == EOF && data.size == 0) |
188 | break; | |
91b5c9e5 KB |
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 - t->bt_dbuf; | |
ab84f9e2 | 196 | t->bt_dbufsz += (sz = 256); |
91b5c9e5 | 197 | if ((t->bt_dbuf = |
ab84f9e2 | 198 | realloc(t->bt_dbuf, t->bt_dbufsz)) == NULL) |
91b5c9e5 KB |
199 | return (RET_ERROR); |
200 | p = t->bt_dbuf + len; | |
201 | } | |
202 | } | |
203 | if (ch == EOF) | |
204 | break; | |
205 | } | |
206 | if (nrec < top) { | |
1eba51f7 | 207 | SET(t, R_EOF); |
91b5c9e5 KB |
208 | return (RET_SPECIAL); |
209 | } | |
210 | return (RET_SUCCESS); | |
211 | } | |
212 | ||
213 | /* | |
214 | * __REC_FMAP -- Get fixed length records from a file. | |
215 | * | |
216 | * Parameters: | |
217 | * t: tree | |
218 | * cnt: records to read | |
219 | * | |
220 | * Returns: | |
221 | * RET_ERROR, RET_SUCCESS | |
222 | */ | |
223 | int | |
224 | __rec_fmap(t, top) | |
225 | BTREE *t; | |
226 | recno_t top; | |
227 | { | |
91b5c9e5 KB |
228 | DBT data; |
229 | recno_t nrec; | |
230 | caddr_t sp, ep; | |
231 | size_t len; | |
232 | char *p; | |
233 | ||
9f21d024 | 234 | sp = t->bt_cmap; |
91b5c9e5 KB |
235 | ep = t->bt_emap; |
236 | data.data = t->bt_dbuf; | |
237 | data.size = t->bt_reclen; | |
238 | ||
239 | if (t->bt_dbufsz < t->bt_reclen) { | |
240 | if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL) | |
241 | return (RET_ERROR); | |
242 | t->bt_dbufsz = t->bt_reclen; | |
243 | } | |
244 | for (nrec = t->bt_nrecs; nrec < top; ++nrec) { | |
245 | if (sp >= ep) { | |
1eba51f7 | 246 | SET(t, R_EOF); |
91b5c9e5 KB |
247 | return (RET_SPECIAL); |
248 | } | |
249 | len = t->bt_reclen; | |
250 | for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++); | |
251 | memset(p, t->bt_bval, len); | |
252 | if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) | |
253 | return (RET_ERROR); | |
254 | } | |
9f21d024 | 255 | t->bt_cmap = sp; |
91b5c9e5 KB |
256 | return (RET_SUCCESS); |
257 | } | |
258 | ||
259 | /* | |
260 | * __REC_VMAP -- Get variable length records from a file. | |
261 | * | |
262 | * Parameters: | |
263 | * t: tree | |
264 | * cnt: records to read | |
265 | * | |
266 | * Returns: | |
267 | * RET_ERROR, RET_SUCCESS | |
268 | */ | |
269 | int | |
270 | __rec_vmap(t, top) | |
271 | BTREE *t; | |
272 | recno_t top; | |
273 | { | |
91b5c9e5 | 274 | DBT data; |
91b5c9e5 | 275 | caddr_t sp, ep; |
2b6272e5 | 276 | recno_t nrec; |
91b5c9e5 KB |
277 | int bval; |
278 | ||
9f21d024 | 279 | sp = t->bt_cmap; |
91b5c9e5 KB |
280 | ep = t->bt_emap; |
281 | bval = t->bt_bval; | |
282 | ||
283 | for (nrec = t->bt_nrecs; nrec < top; ++nrec) { | |
284 | if (sp >= ep) { | |
1eba51f7 | 285 | SET(t, R_EOF); |
91b5c9e5 KB |
286 | return (RET_SPECIAL); |
287 | } | |
288 | for (data.data = sp; sp < ep && *sp != bval; ++sp); | |
289 | data.size = sp - (caddr_t)data.data; | |
290 | if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) | |
291 | return (RET_ERROR); | |
292 | ++sp; | |
293 | } | |
9f21d024 | 294 | t->bt_cmap = sp; |
91b5c9e5 KB |
295 | return (RET_SUCCESS); |
296 | } |