]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/types.h>
72 * __REC_GET -- Get a record from the btree.
75 * dbp: pointer to access method
77 * data: data to return
78 * flag: currently unused
81 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
84 __rec_get(dbp
, key
, data
, flags
)
97 /* Toss any page pinned across calls. */
98 if (t
->bt_pinned
!= NULL
) {
99 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
103 /* Get currently doesn't take any flags, and keys of 0 are illegal. */
104 if (flags
|| (nrec
= *(recno_t
*)key
->data
) == 0) {
110 * If we haven't seen this record yet, try to find it in the
113 if (nrec
> t
->bt_nrecs
) {
114 if (ISSET(t
, R_EOF
| R_INMEM
))
115 return (RET_SPECIAL
);
116 if ((status
= t
->bt_irec(t
, nrec
)) != RET_SUCCESS
)
121 if ((e
= __rec_search(t
, nrec
, SEARCH
)) == NULL
)
124 status
= __rec_ret(t
, e
, 0, NULL
, data
);
125 if (ISSET(t
, B_DB_LOCK
))
126 mpool_put(t
->bt_mp
, e
->page
, 0);
128 t
->bt_pinned
= e
->page
;
133 * __REC_FPIPE -- Get fixed length records from a pipe.
137 * cnt: records to read
140 * RET_ERROR, RET_SUCCESS
153 if (t
->bt_dbufsz
< t
->bt_reclen
) {
155 (char *)realloc(t
->bt_dbuf
, t
->bt_reclen
)) == NULL
)
157 t
->bt_dbufsz
= t
->bt_reclen
;
159 data
.data
= t
->bt_dbuf
;
160 data
.size
= t
->bt_reclen
;
162 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
164 for (p
= t
->bt_dbuf
;; *p
++ = ch
)
165 if ((ch
= getc(t
->bt_rfp
)) == EOF
|| !len
--) {
166 if (__rec_iput(t
, nrec
, &data
, 0)
176 return (RET_SPECIAL
);
178 return (RET_SUCCESS
);
182 * __REC_VPIPE -- Get variable length records from a pipe.
186 * cnt: records to read
189 * RET_ERROR, RET_SUCCESS
204 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
205 for (p
= t
->bt_dbuf
, sz
= t
->bt_dbufsz
;; *p
++ = ch
, --sz
) {
206 if ((ch
= getc(t
->bt_rfp
)) == EOF
|| ch
== bval
) {
207 data
.data
= t
->bt_dbuf
;
208 data
.size
= p
- t
->bt_dbuf
;
209 if (ch
== EOF
&& data
.size
== 0)
211 if (__rec_iput(t
, nrec
, &data
, 0)
217 len
= p
- t
->bt_dbuf
;
218 t
->bt_dbufsz
+= (sz
= 256);
219 if ((t
->bt_dbuf
= (char *)realloc(t
->bt_dbuf
,
220 t
->bt_dbufsz
)) == NULL
)
222 p
= t
->bt_dbuf
+ len
;
230 return (RET_SPECIAL
);
232 return (RET_SUCCESS
);
236 * __REC_FMAP -- Get fixed length records from a file.
240 * cnt: records to read
243 * RET_ERROR, RET_SUCCESS
256 if (t
->bt_dbufsz
< t
->bt_reclen
) {
258 (char *)realloc(t
->bt_dbuf
, t
->bt_reclen
)) == NULL
)
260 t
->bt_dbufsz
= t
->bt_reclen
;
262 data
.data
= t
->bt_dbuf
;
263 data
.size
= t
->bt_reclen
;
267 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
270 return (RET_SPECIAL
);
273 for (p
= t
->bt_dbuf
; sp
< ep
&& len
--; *p
++ = *sp
++);
274 memset(p
, t
->bt_bval
, len
);
275 if (__rec_iput(t
, nrec
, &data
, 0) != RET_SUCCESS
)
279 return (RET_SUCCESS
);
283 * __REC_VMAP -- Get variable length records from a file.
287 * cnt: records to read
290 * RET_ERROR, RET_SUCCESS
306 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
309 return (RET_SPECIAL
);
311 for (data
.data
= sp
; sp
< ep
&& *sp
!= bval
; ++sp
);
312 data
.size
= sp
- (caddr_t
)data
.data
;
313 if (__rec_iput(t
, nrec
, &data
, 0) != RET_SUCCESS
)
318 return (RET_SUCCESS
);