xref: /freebsd/lib/libc/db/recno/rec_get.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4ef5d438eSPaul Traina  * Copyright (c) 1990, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
3258f0484fSRodney W. Grimes #include <sys/types.h>
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #include <errno.h>
3558f0484fSRodney W. Grimes #include <stddef.h>
3658f0484fSRodney W. Grimes #include <stdio.h>
3758f0484fSRodney W. Grimes #include <stdlib.h>
3858f0484fSRodney W. Grimes #include <string.h>
3958f0484fSRodney W. Grimes #include <unistd.h>
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <db.h>
4258f0484fSRodney W. Grimes #include "recno.h"
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes /*
4558f0484fSRodney W. Grimes  * __REC_GET -- Get a record from the btree.
4658f0484fSRodney W. Grimes  *
4758f0484fSRodney W. Grimes  * Parameters:
4858f0484fSRodney W. Grimes  *	dbp:	pointer to access method
4958f0484fSRodney W. Grimes  *	key:	key to find
5058f0484fSRodney W. Grimes  *	data:	data to return
5158f0484fSRodney W. Grimes  *	flag:	currently unused
5258f0484fSRodney W. Grimes  *
5358f0484fSRodney W. Grimes  * Returns:
5458f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
5558f0484fSRodney W. Grimes  */
5658f0484fSRodney W. Grimes int
__rec_get(const DB * dbp,const DBT * key,DBT * data,u_int flags)570ac22237SXin LI __rec_get(const DB *dbp, const DBT *key, DBT *data, u_int flags)
5858f0484fSRodney W. Grimes {
5958f0484fSRodney W. Grimes 	BTREE *t;
6058f0484fSRodney W. Grimes 	EPG *e;
6158f0484fSRodney W. Grimes 	recno_t nrec;
6258f0484fSRodney W. Grimes 	int status;
6358f0484fSRodney W. Grimes 
6458f0484fSRodney W. Grimes 	t = dbp->internal;
6558f0484fSRodney W. Grimes 
6658f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
6758f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
6858f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
6958f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
7058f0484fSRodney W. Grimes 	}
7158f0484fSRodney W. Grimes 
7258f0484fSRodney W. Grimes 	/* Get currently doesn't take any flags, and keys of 0 are illegal. */
7358f0484fSRodney W. Grimes 	if (flags || (nrec = *(recno_t *)key->data) == 0) {
7458f0484fSRodney W. Grimes 		errno = EINVAL;
7558f0484fSRodney W. Grimes 		return (RET_ERROR);
7658f0484fSRodney W. Grimes 	}
7758f0484fSRodney W. Grimes 
7858f0484fSRodney W. Grimes 	/*
7958f0484fSRodney W. Grimes 	 * If we haven't seen this record yet, try to find it in the
8058f0484fSRodney W. Grimes 	 * original file.
8158f0484fSRodney W. Grimes 	 */
8258f0484fSRodney W. Grimes 	if (nrec > t->bt_nrecs) {
83ef5d438eSPaul Traina 		if (F_ISSET(t, R_EOF | R_INMEM))
8458f0484fSRodney W. Grimes 			return (RET_SPECIAL);
8558f0484fSRodney W. Grimes 		if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
8658f0484fSRodney W. Grimes 			return (status);
8758f0484fSRodney W. Grimes 	}
8858f0484fSRodney W. Grimes 
8958f0484fSRodney W. Grimes 	--nrec;
9058f0484fSRodney W. Grimes 	if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
9158f0484fSRodney W. Grimes 		return (RET_ERROR);
9258f0484fSRodney W. Grimes 
9358f0484fSRodney W. Grimes 	status = __rec_ret(t, e, 0, NULL, data);
94ef5d438eSPaul Traina 	if (F_ISSET(t, B_DB_LOCK))
9558f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, e->page, 0);
9658f0484fSRodney W. Grimes 	else
9758f0484fSRodney W. Grimes 		t->bt_pinned = e->page;
9858f0484fSRodney W. Grimes 	return (status);
9958f0484fSRodney W. Grimes }
10058f0484fSRodney W. Grimes 
10158f0484fSRodney W. Grimes /*
10258f0484fSRodney W. Grimes  * __REC_FPIPE -- Get fixed length records from a pipe.
10358f0484fSRodney W. Grimes  *
10458f0484fSRodney W. Grimes  * Parameters:
10558f0484fSRodney W. Grimes  *	t:	tree
10658f0484fSRodney W. Grimes  *	cnt:	records to read
10758f0484fSRodney W. Grimes  *
10858f0484fSRodney W. Grimes  * Returns:
10958f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
11058f0484fSRodney W. Grimes  */
11158f0484fSRodney W. Grimes int
__rec_fpipe(BTREE * t,recno_t top)1120ac22237SXin LI __rec_fpipe(BTREE *t, recno_t top)
11358f0484fSRodney W. Grimes {
11458f0484fSRodney W. Grimes 	DBT data;
11558f0484fSRodney W. Grimes 	recno_t nrec;
11658f0484fSRodney W. Grimes 	size_t len;
11758f0484fSRodney W. Grimes 	int ch;
118ef5d438eSPaul Traina 	u_char *p;
11958f0484fSRodney W. Grimes 
120ef5d438eSPaul Traina 	if (t->bt_rdata.size < t->bt_reclen) {
1217ccf00dfSXin LI 		t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_reclen);
122ef5d438eSPaul Traina 		if (t->bt_rdata.data == NULL)
12358f0484fSRodney W. Grimes 			return (RET_ERROR);
124ef5d438eSPaul Traina 		t->bt_rdata.size = t->bt_reclen;
12558f0484fSRodney W. Grimes 	}
126ef5d438eSPaul Traina 	data.data = t->bt_rdata.data;
12758f0484fSRodney W. Grimes 	data.size = t->bt_reclen;
12858f0484fSRodney W. Grimes 
129ef5d438eSPaul Traina 	for (nrec = t->bt_nrecs; nrec < top;) {
13058f0484fSRodney W. Grimes 		len = t->bt_reclen;
131ef5d438eSPaul Traina 		for (p = t->bt_rdata.data;; *p++ = ch)
132ef5d438eSPaul Traina 			if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
133ef5d438eSPaul Traina 				if (ch != EOF)
134ef5d438eSPaul Traina 					*p = ch;
135ef5d438eSPaul Traina 				if (len != 0)
136ef5d438eSPaul Traina 					memset(p, t->bt_bval, len);
137ef5d438eSPaul Traina 				if (__rec_iput(t,
138ef5d438eSPaul Traina 				    nrec, &data, 0) != RET_SUCCESS)
13958f0484fSRodney W. Grimes 					return (RET_ERROR);
140ef5d438eSPaul Traina 				++nrec;
14158f0484fSRodney W. Grimes 				break;
14258f0484fSRodney W. Grimes 			}
14358f0484fSRodney W. Grimes 		if (ch == EOF)
14458f0484fSRodney W. Grimes 			break;
14558f0484fSRodney W. Grimes 	}
14658f0484fSRodney W. Grimes 	if (nrec < top) {
147ef5d438eSPaul Traina 		F_SET(t, R_EOF);
14858f0484fSRodney W. Grimes 		return (RET_SPECIAL);
14958f0484fSRodney W. Grimes 	}
15058f0484fSRodney W. Grimes 	return (RET_SUCCESS);
15158f0484fSRodney W. Grimes }
15258f0484fSRodney W. Grimes 
15358f0484fSRodney W. Grimes /*
15458f0484fSRodney W. Grimes  * __REC_VPIPE -- Get variable length records from a pipe.
15558f0484fSRodney W. Grimes  *
15658f0484fSRodney W. Grimes  * Parameters:
15758f0484fSRodney W. Grimes  *	t:	tree
15858f0484fSRodney W. Grimes  *	cnt:	records to read
15958f0484fSRodney W. Grimes  *
16058f0484fSRodney W. Grimes  * Returns:
16158f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
16258f0484fSRodney W. Grimes  */
16358f0484fSRodney W. Grimes int
__rec_vpipe(BTREE * t,recno_t top)1640ac22237SXin LI __rec_vpipe(BTREE *t, recno_t top)
16558f0484fSRodney W. Grimes {
16658f0484fSRodney W. Grimes 	DBT data;
16758f0484fSRodney W. Grimes 	recno_t nrec;
168d045c515SDavid E. O'Brien 	size_t len;
16958f0484fSRodney W. Grimes 	size_t sz;
17058f0484fSRodney W. Grimes 	int bval, ch;
171ef5d438eSPaul Traina 	u_char *p;
17258f0484fSRodney W. Grimes 
17358f0484fSRodney W. Grimes 	bval = t->bt_bval;
17458f0484fSRodney W. Grimes 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
175ef5d438eSPaul Traina 		for (p = t->bt_rdata.data,
176ef5d438eSPaul Traina 		    sz = t->bt_rdata.size;; *p++ = ch, --sz) {
17758f0484fSRodney W. Grimes 			if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
178ef5d438eSPaul Traina 				data.data = t->bt_rdata.data;
179ef5d438eSPaul Traina 				data.size = p - (u_char *)t->bt_rdata.data;
18058f0484fSRodney W. Grimes 				if (ch == EOF && data.size == 0)
18158f0484fSRodney W. Grimes 					break;
18258f0484fSRodney W. Grimes 				if (__rec_iput(t, nrec, &data, 0)
18358f0484fSRodney W. Grimes 				    != RET_SUCCESS)
18458f0484fSRodney W. Grimes 					return (RET_ERROR);
18558f0484fSRodney W. Grimes 				break;
18658f0484fSRodney W. Grimes 			}
18758f0484fSRodney W. Grimes 			if (sz == 0) {
188ef5d438eSPaul Traina 				len = p - (u_char *)t->bt_rdata.data;
189ef5d438eSPaul Traina 				t->bt_rdata.size += (sz = 256);
1907ccf00dfSXin LI 				t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_rdata.size);
191ef5d438eSPaul Traina 				if (t->bt_rdata.data == NULL)
19258f0484fSRodney W. Grimes 					return (RET_ERROR);
193ef5d438eSPaul Traina 				p = (u_char *)t->bt_rdata.data + len;
19458f0484fSRodney W. Grimes 			}
19558f0484fSRodney W. Grimes 		}
19658f0484fSRodney W. Grimes 		if (ch == EOF)
19758f0484fSRodney W. Grimes 			break;
19858f0484fSRodney W. Grimes 	}
19958f0484fSRodney W. Grimes 	if (nrec < top) {
200ef5d438eSPaul Traina 		F_SET(t, R_EOF);
20158f0484fSRodney W. Grimes 		return (RET_SPECIAL);
20258f0484fSRodney W. Grimes 	}
20358f0484fSRodney W. Grimes 	return (RET_SUCCESS);
20458f0484fSRodney W. Grimes }
20558f0484fSRodney W. Grimes 
20658f0484fSRodney W. Grimes /*
20758f0484fSRodney W. Grimes  * __REC_FMAP -- Get fixed length records from a file.
20858f0484fSRodney W. Grimes  *
20958f0484fSRodney W. Grimes  * Parameters:
21058f0484fSRodney W. Grimes  *	t:	tree
21158f0484fSRodney W. Grimes  *	cnt:	records to read
21258f0484fSRodney W. Grimes  *
21358f0484fSRodney W. Grimes  * Returns:
21458f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
21558f0484fSRodney W. Grimes  */
21658f0484fSRodney W. Grimes int
__rec_fmap(BTREE * t,recno_t top)2170ac22237SXin LI __rec_fmap(BTREE *t, recno_t top)
21858f0484fSRodney W. Grimes {
21958f0484fSRodney W. Grimes 	DBT data;
22058f0484fSRodney W. Grimes 	recno_t nrec;
221ef5d438eSPaul Traina 	u_char *sp, *ep, *p;
22258f0484fSRodney W. Grimes 	size_t len;
22358f0484fSRodney W. Grimes 
224ef5d438eSPaul Traina 	if (t->bt_rdata.size < t->bt_reclen) {
2257ccf00dfSXin LI 		t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_reclen);
226ef5d438eSPaul Traina 		if (t->bt_rdata.data == NULL)
22758f0484fSRodney W. Grimes 			return (RET_ERROR);
228ef5d438eSPaul Traina 		t->bt_rdata.size = t->bt_reclen;
22958f0484fSRodney W. Grimes 	}
230ef5d438eSPaul Traina 	data.data = t->bt_rdata.data;
23158f0484fSRodney W. Grimes 	data.size = t->bt_reclen;
23258f0484fSRodney W. Grimes 
233ef5d438eSPaul Traina 	sp = (u_char *)t->bt_cmap;
234ef5d438eSPaul Traina 	ep = (u_char *)t->bt_emap;
23558f0484fSRodney W. Grimes 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
23658f0484fSRodney W. Grimes 		if (sp >= ep) {
237ef5d438eSPaul Traina 			F_SET(t, R_EOF);
23858f0484fSRodney W. Grimes 			return (RET_SPECIAL);
23958f0484fSRodney W. Grimes 		}
24058f0484fSRodney W. Grimes 		len = t->bt_reclen;
241ef5d438eSPaul Traina 		for (p = t->bt_rdata.data;
242ef5d438eSPaul Traina 		    sp < ep && len > 0; *p++ = *sp++, --len);
243ef5d438eSPaul Traina 		if (len != 0)
24458f0484fSRodney W. Grimes 			memset(p, t->bt_bval, len);
24558f0484fSRodney W. Grimes 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
24658f0484fSRodney W. Grimes 			return (RET_ERROR);
24758f0484fSRodney W. Grimes 	}
248ef5d438eSPaul Traina 	t->bt_cmap = (caddr_t)sp;
24958f0484fSRodney W. Grimes 	return (RET_SUCCESS);
25058f0484fSRodney W. Grimes }
25158f0484fSRodney W. Grimes 
25258f0484fSRodney W. Grimes /*
25358f0484fSRodney W. Grimes  * __REC_VMAP -- Get variable length records from a file.
25458f0484fSRodney W. Grimes  *
25558f0484fSRodney W. Grimes  * Parameters:
25658f0484fSRodney W. Grimes  *	t:	tree
25758f0484fSRodney W. Grimes  *	cnt:	records to read
25858f0484fSRodney W. Grimes  *
25958f0484fSRodney W. Grimes  * Returns:
26058f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
26158f0484fSRodney W. Grimes  */
26258f0484fSRodney W. Grimes int
__rec_vmap(BTREE * t,recno_t top)2630ac22237SXin LI __rec_vmap(BTREE *t, recno_t top)
26458f0484fSRodney W. Grimes {
26558f0484fSRodney W. Grimes 	DBT data;
266ef5d438eSPaul Traina 	u_char *sp, *ep;
26758f0484fSRodney W. Grimes 	recno_t nrec;
26858f0484fSRodney W. Grimes 	int bval;
26958f0484fSRodney W. Grimes 
270ef5d438eSPaul Traina 	sp = (u_char *)t->bt_cmap;
271ef5d438eSPaul Traina 	ep = (u_char *)t->bt_emap;
27258f0484fSRodney W. Grimes 	bval = t->bt_bval;
27358f0484fSRodney W. Grimes 
27458f0484fSRodney W. Grimes 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
27558f0484fSRodney W. Grimes 		if (sp >= ep) {
276ef5d438eSPaul Traina 			F_SET(t, R_EOF);
27758f0484fSRodney W. Grimes 			return (RET_SPECIAL);
27858f0484fSRodney W. Grimes 		}
27958f0484fSRodney W. Grimes 		for (data.data = sp; sp < ep && *sp != bval; ++sp);
280ef5d438eSPaul Traina 		data.size = sp - (u_char *)data.data;
28158f0484fSRodney W. Grimes 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
28258f0484fSRodney W. Grimes 			return (RET_ERROR);
28358f0484fSRodney W. Grimes 		++sp;
28458f0484fSRodney W. Grimes 	}
285ef5d438eSPaul Traina 	t->bt_cmap = (caddr_t)sp;
28658f0484fSRodney W. Grimes 	return (RET_SUCCESS);
28758f0484fSRodney W. Grimes }
288