]>
git.saurik.com Git - apple/libc.git/blob - db/recno/rec_get.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <sys/types.h>
69 * __REC_GET -- Get a record from the btree.
72 * dbp: pointer to access method
74 * data: data to return
75 * flag: currently unused
78 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
81 __rec_get(dbp
, key
, data
, flags
)
94 /* Toss any page pinned across calls. */
95 if (t
->bt_pinned
!= NULL
) {
96 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
100 /* Get currently doesn't take any flags, and keys of 0 are illegal. */
101 if (flags
|| (nrec
= *(recno_t
*)key
->data
) == 0) {
107 * If we haven't seen this record yet, try to find it in the
110 if (nrec
> t
->bt_nrecs
) {
111 if (ISSET(t
, R_EOF
| R_INMEM
))
112 return (RET_SPECIAL
);
113 if ((status
= t
->bt_irec(t
, nrec
)) != RET_SUCCESS
)
118 if ((e
= __rec_search(t
, nrec
, SEARCH
)) == NULL
)
121 status
= __rec_ret(t
, e
, 0, NULL
, data
);
122 if (ISSET(t
, B_DB_LOCK
))
123 mpool_put(t
->bt_mp
, e
->page
, 0);
125 t
->bt_pinned
= e
->page
;
130 * __REC_FPIPE -- Get fixed length records from a pipe.
134 * cnt: records to read
137 * RET_ERROR, RET_SUCCESS
150 if (t
->bt_dbufsz
< t
->bt_reclen
) {
152 (char *)realloc(t
->bt_dbuf
, t
->bt_reclen
)) == NULL
)
154 t
->bt_dbufsz
= t
->bt_reclen
;
156 data
.data
= t
->bt_dbuf
;
157 data
.size
= t
->bt_reclen
;
159 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
161 for (p
= t
->bt_dbuf
;; *p
++ = ch
)
162 if ((ch
= getc(t
->bt_rfp
)) == EOF
|| !len
--) {
163 if (__rec_iput(t
, nrec
, &data
, 0)
173 return (RET_SPECIAL
);
175 return (RET_SUCCESS
);
179 * __REC_VPIPE -- Get variable length records from a pipe.
183 * cnt: records to read
186 * RET_ERROR, RET_SUCCESS
201 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
202 for (p
= t
->bt_dbuf
, sz
= t
->bt_dbufsz
;; *p
++ = ch
, --sz
) {
203 if ((ch
= getc(t
->bt_rfp
)) == EOF
|| ch
== bval
) {
204 data
.data
= t
->bt_dbuf
;
205 data
.size
= p
- t
->bt_dbuf
;
206 if (ch
== EOF
&& data
.size
== 0)
208 if (__rec_iput(t
, nrec
, &data
, 0)
214 len
= p
- t
->bt_dbuf
;
215 t
->bt_dbufsz
+= (sz
= 256);
216 if ((t
->bt_dbuf
= (char *)realloc(t
->bt_dbuf
,
217 t
->bt_dbufsz
)) == NULL
)
219 p
= t
->bt_dbuf
+ len
;
227 return (RET_SPECIAL
);
229 return (RET_SUCCESS
);
233 * __REC_FMAP -- Get fixed length records from a file.
237 * cnt: records to read
240 * RET_ERROR, RET_SUCCESS
253 if (t
->bt_dbufsz
< t
->bt_reclen
) {
255 (char *)realloc(t
->bt_dbuf
, t
->bt_reclen
)) == NULL
)
257 t
->bt_dbufsz
= t
->bt_reclen
;
259 data
.data
= t
->bt_dbuf
;
260 data
.size
= t
->bt_reclen
;
264 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
267 return (RET_SPECIAL
);
270 for (p
= t
->bt_dbuf
; sp
< ep
&& len
--; *p
++ = *sp
++);
271 memset(p
, t
->bt_bval
, len
);
272 if (__rec_iput(t
, nrec
, &data
, 0) != RET_SUCCESS
)
276 return (RET_SUCCESS
);
280 * __REC_VMAP -- Get variable length records from a file.
284 * cnt: records to read
287 * RET_ERROR, RET_SUCCESS
303 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
306 return (RET_SPECIAL
);
308 for (data
.data
= sp
; sp
< ep
&& *sp
!= bval
; ++sp
);
309 data
.size
= sp
- (caddr_t
)data
.data
;
310 if (__rec_iput(t
, nrec
, &data
, 0) != RET_SUCCESS
)
315 return (RET_SUCCESS
);