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. 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.
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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)rec_get.c 8.9 (Berkeley) 8/18/94";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/types.h>
51 * __REC_GET -- Get a record from the btree.
54 * dbp: pointer to access method
56 * data: data to return
57 * flag: currently unused
60 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
63 __rec_get(dbp
, key
, data
, flags
)
76 /* Toss any page pinned across calls. */
77 if (t
->bt_pinned
!= NULL
) {
78 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
82 /* Get currently doesn't take any flags, and keys of 0 are illegal. */
83 if (flags
|| (nrec
= *(recno_t
*)key
->data
) == 0) {
89 * If we haven't seen this record yet, try to find it in the
92 if (nrec
> t
->bt_nrecs
) {
93 if (F_ISSET(t
, R_EOF
| R_INMEM
))
95 if ((status
= t
->bt_irec(t
, nrec
)) != RET_SUCCESS
)
100 if ((e
= __rec_search(t
, nrec
, SEARCH
)) == NULL
)
103 status
= __rec_ret(t
, e
, 0, NULL
, data
);
104 if (F_ISSET(t
, B_DB_LOCK
))
105 mpool_put(t
->bt_mp
, e
->page
, 0);
107 t
->bt_pinned
= e
->page
;
112 * __REC_FPIPE -- Get fixed length records from a pipe.
116 * cnt: records to read
119 * RET_ERROR, RET_SUCCESS
132 if (t
->bt_rdata
.size
< t
->bt_reclen
) {
133 t
->bt_rdata
.data
= t
->bt_rdata
.data
== NULL
?
134 malloc(t
->bt_reclen
) :
135 realloc(t
->bt_rdata
.data
, t
->bt_reclen
);
136 if (t
->bt_rdata
.data
== NULL
)
138 t
->bt_rdata
.size
= t
->bt_reclen
;
140 data
.data
= t
->bt_rdata
.data
;
141 data
.size
= t
->bt_reclen
;
143 for (nrec
= t
->bt_nrecs
; nrec
< top
;) {
145 for (p
= t
->bt_rdata
.data
;; *p
++ = ch
)
146 if ((ch
= getc(t
->bt_rfp
)) == EOF
|| !--len
) {
150 memset(p
, t
->bt_bval
, len
);
152 nrec
, &data
, 0) != RET_SUCCESS
)
162 return (RET_SPECIAL
);
164 return (RET_SUCCESS
);
168 * __REC_VPIPE -- Get variable length records from a pipe.
172 * cnt: records to read
175 * RET_ERROR, RET_SUCCESS
190 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
191 for (p
= t
->bt_rdata
.data
,
192 sz
= t
->bt_rdata
.size
;; *p
++ = ch
, --sz
) {
193 if ((ch
= getc(t
->bt_rfp
)) == EOF
|| ch
== bval
) {
194 data
.data
= t
->bt_rdata
.data
;
195 data
.size
= p
- (u_char
*)t
->bt_rdata
.data
;
196 if (ch
== EOF
&& data
.size
== 0)
198 if (__rec_iput(t
, nrec
, &data
, 0)
204 len
= p
- (u_char
*)t
->bt_rdata
.data
;
205 t
->bt_rdata
.size
+= (sz
= 256);
206 t
->bt_rdata
.data
= t
->bt_rdata
.data
== NULL
?
207 malloc(t
->bt_rdata
.size
) :
208 realloc(t
->bt_rdata
.data
, t
->bt_rdata
.size
);
209 if (t
->bt_rdata
.data
== NULL
)
211 p
= (u_char
*)t
->bt_rdata
.data
+ len
;
219 return (RET_SPECIAL
);
221 return (RET_SUCCESS
);
225 * __REC_FMAP -- Get fixed length records from a file.
229 * cnt: records to read
232 * RET_ERROR, RET_SUCCESS
244 if (t
->bt_rdata
.size
< t
->bt_reclen
) {
245 t
->bt_rdata
.data
= t
->bt_rdata
.data
== NULL
?
246 malloc(t
->bt_reclen
) :
247 realloc(t
->bt_rdata
.data
, t
->bt_reclen
);
248 if (t
->bt_rdata
.data
== NULL
)
250 t
->bt_rdata
.size
= t
->bt_reclen
;
252 data
.data
= t
->bt_rdata
.data
;
253 data
.size
= t
->bt_reclen
;
255 sp
= (u_char
*)t
->bt_cmap
;
256 ep
= (u_char
*)t
->bt_emap
;
257 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
260 return (RET_SPECIAL
);
263 for (p
= t
->bt_rdata
.data
;
264 sp
< ep
&& len
> 0; *p
++ = *sp
++, --len
);
266 memset(p
, t
->bt_bval
, len
);
267 if (__rec_iput(t
, nrec
, &data
, 0) != RET_SUCCESS
)
270 t
->bt_cmap
= (caddr_t
)sp
;
271 return (RET_SUCCESS
);
275 * __REC_VMAP -- Get variable length records from a file.
279 * cnt: records to read
282 * RET_ERROR, RET_SUCCESS
294 sp
= (u_char
*)t
->bt_cmap
;
295 ep
= (u_char
*)t
->bt_emap
;
298 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
301 return (RET_SPECIAL
);
303 for (data
.data
= sp
; sp
< ep
&& *sp
!= bval
; ++sp
);
304 data
.size
= sp
- (u_char
*)data
.data
;
305 if (__rec_iput(t
, nrec
, &data
, 0) != RET_SUCCESS
)
309 t
->bt_cmap
= (caddr_t
)sp
;
310 return (RET_SUCCESS
);