1 /* $NetBSD: rec_get.c,v 1.18 2013/12/25 19:42:23 christos Exp $ */
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: rec_get.c,v 1.18 2013/12/25 19:42:23 christos Exp $");
39 #include "namespace.h"
40 #include <sys/types.h>
54 * __REC_GET -- Get a record from the btree.
57 * dbp: pointer to access method
59 * data: data to return
60 * flag: currently unused
63 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
66 __rec_get(const DB
*dbp
, const DBT
*key
, DBT
*data
, u_int flags
)
75 /* Toss any page pinned across calls. */
76 if (t
->bt_pinned
!= NULL
) {
77 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
81 /* Get currently doesn't take any flags, and keys of 0 are illegal. */
82 if (flags
|| (nrec
= *(recno_t
*)key
->data
) == 0) {
88 * If we haven't seen this record yet, try to find it in the
91 if (nrec
> t
->bt_nrecs
) {
92 if (F_ISSET(t
, R_EOF
| R_INMEM
))
94 if ((status
= t
->bt_irec(t
, nrec
)) != RET_SUCCESS
)
99 if ((e
= __rec_search(t
, nrec
, SEARCH
)) == NULL
)
102 status
= __rec_ret(t
, e
, 0, NULL
, data
);
103 if (F_ISSET(t
, B_DB_LOCK
))
104 mpool_put(t
->bt_mp
, e
->page
, 0);
106 t
->bt_pinned
= e
->page
;
111 * __REC_FPIPE -- Get fixed length records from a pipe.
115 * cnt: records to read
118 * RET_ERROR, RET_SUCCESS
121 __rec_fpipe(BTREE
*t
, recno_t top
)
129 if (t
->bt_rdata
.size
< t
->bt_reclen
) {
130 void *np
= realloc(t
->bt_rdata
.data
, t
->bt_reclen
);
133 t
->bt_rdata
.data
= np
;
134 t
->bt_rdata
.size
= t
->bt_reclen
;
136 data
.data
= t
->bt_rdata
.data
;
137 data
.size
= t
->bt_reclen
;
139 for (nrec
= t
->bt_nrecs
; nrec
< top
;) {
141 for (p
= t
->bt_rdata
.data
;; *p
++ = ch
)
142 if ((ch
= getc(t
->bt_rfp
)) == EOF
|| !--len
) {
146 memset(p
, t
->bt_bval
, len
);
148 nrec
, &data
, 0) != RET_SUCCESS
)
158 return (RET_SPECIAL
);
160 return (RET_SUCCESS
);
164 * __REC_VPIPE -- Get variable length records from a pipe.
168 * cnt: records to read
171 * RET_ERROR, RET_SUCCESS
174 __rec_vpipe(BTREE
*t
, recno_t top
)
183 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
184 for (p
= t
->bt_rdata
.data
,
185 sz
= t
->bt_rdata
.size
;; *p
++ = ch
, --sz
) {
186 if ((ch
= getc(t
->bt_rfp
)) == EOF
|| ch
== bval
) {
187 data
.data
= t
->bt_rdata
.data
;
188 data
.size
= p
- (uint8_t *)t
->bt_rdata
.data
;
189 if (ch
== EOF
&& data
.size
== 0)
191 if (__rec_iput(t
, nrec
, &data
, 0)
197 ptrdiff_t len
= p
- (uint8_t *)t
->bt_rdata
.data
;
198 size_t tot
= t
->bt_rdata
.size
+ (sz
= 256);
199 void *np
= realloc(t
->bt_rdata
.data
, tot
);
202 t
->bt_rdata
.size
= tot
;
203 t
->bt_rdata
.data
= np
;
204 p
= (uint8_t *)t
->bt_rdata
.data
+ len
;
212 return (RET_SPECIAL
);
214 return (RET_SUCCESS
);
218 * __REC_FMAP -- Get fixed length records from a file.
222 * cnt: records to read
225 * RET_ERROR, RET_SUCCESS
228 __rec_fmap(BTREE
*t
, recno_t top
)
232 uint8_t *sp
, *ep
, *p
;
235 if (t
->bt_rdata
.size
< t
->bt_reclen
) {
236 void *np
= realloc(t
->bt_rdata
.data
, t
->bt_reclen
);
239 t
->bt_rdata
.data
= np
;
240 t
->bt_rdata
.size
= t
->bt_reclen
;
242 data
.data
= t
->bt_rdata
.data
;
243 data
.size
= t
->bt_reclen
;
245 sp
= (uint8_t *)t
->bt_cmap
;
246 ep
= (uint8_t *)t
->bt_emap
;
247 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
250 return (RET_SPECIAL
);
253 for (p
= t
->bt_rdata
.data
;
254 sp
< ep
&& len
> 0; *p
++ = *sp
++, --len
);
256 memset(p
, t
->bt_bval
, len
);
257 if (__rec_iput(t
, nrec
, &data
, 0) != RET_SUCCESS
)
260 t
->bt_cmap
= (caddr_t
)sp
;
261 return (RET_SUCCESS
);
265 * __REC_VMAP -- Get variable length records from a file.
269 * cnt: records to read
272 * RET_ERROR, RET_SUCCESS
275 __rec_vmap(BTREE
*t
, recno_t top
)
282 sp
= (uint8_t *)t
->bt_cmap
;
283 ep
= (uint8_t *)t
->bt_emap
;
286 for (nrec
= t
->bt_nrecs
; nrec
< top
; ++nrec
) {
289 return (RET_SPECIAL
);
291 for (data
.data
= sp
; sp
< ep
&& *sp
!= bval
; ++sp
);
292 data
.size
= sp
- (uint8_t *)data
.data
;
293 if (__rec_iput(t
, nrec
, &data
, 0) != RET_SUCCESS
)
297 t
->bt_cmap
= (caddr_t
)sp
;
298 return (RET_SUCCESS
);