Home | History | Annotate | Line # | Download | only in recno
rec_get.c revision 1.9
      1 /*	$NetBSD: rec_get.c,v 1.9 1997/07/13 18:52:12 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)rec_get.c	8.9 (Berkeley) 8/18/94";
     40 #else
     41 __RCSID("$NetBSD: rec_get.c,v 1.9 1997/07/13 18:52:12 christos Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include <sys/types.h>
     46 
     47 #include <errno.h>
     48 #include <stddef.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 
     54 #include <db.h>
     55 #include "recno.h"
     56 
     57 /*
     58  * __REC_GET -- Get a record from the btree.
     59  *
     60  * Parameters:
     61  *	dbp:	pointer to access method
     62  *	key:	key to find
     63  *	data:	data to return
     64  *	flag:	currently unused
     65  *
     66  * Returns:
     67  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
     68  */
     69 int
     70 __rec_get(dbp, key, data, flags)
     71 	const DB *dbp;
     72 	const DBT *key;
     73 	DBT *data;
     74 	u_int flags;
     75 {
     76 	BTREE *t;
     77 	EPG *e;
     78 	recno_t nrec;
     79 	int status;
     80 
     81 	t = dbp->internal;
     82 
     83 	/* Toss any page pinned across calls. */
     84 	if (t->bt_pinned != NULL) {
     85 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     86 		t->bt_pinned = NULL;
     87 	}
     88 
     89 	/* Get currently doesn't take any flags, and keys of 0 are illegal. */
     90 	if (flags || (nrec = *(recno_t *)key->data) == 0) {
     91 		errno = EINVAL;
     92 		return (RET_ERROR);
     93 	}
     94 
     95 	/*
     96 	 * If we haven't seen this record yet, try to find it in the
     97 	 * original file.
     98 	 */
     99 	if (nrec > t->bt_nrecs) {
    100 		if (F_ISSET(t, R_EOF | R_INMEM))
    101 			return (RET_SPECIAL);
    102 		if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
    103 			return (status);
    104 	}
    105 
    106 	--nrec;
    107 	if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
    108 		return (RET_ERROR);
    109 
    110 	status = __rec_ret(t, e, 0, NULL, data);
    111 	if (F_ISSET(t, B_DB_LOCK))
    112 		mpool_put(t->bt_mp, e->page, 0);
    113 	else
    114 		t->bt_pinned = e->page;
    115 	return (status);
    116 }
    117 
    118 /*
    119  * __REC_FPIPE -- Get fixed length records from a pipe.
    120  *
    121  * Parameters:
    122  *	t:	tree
    123  *	cnt:	records to read
    124  *
    125  * Returns:
    126  *	RET_ERROR, RET_SUCCESS
    127  */
    128 int
    129 __rec_fpipe(t, top)
    130 	BTREE *t;
    131 	recno_t top;
    132 {
    133 	DBT data;
    134 	recno_t nrec;
    135 	size_t len;
    136 	int ch;
    137 	u_char *p;
    138 
    139 	if (t->bt_rdata.size < t->bt_reclen) {
    140 		t->bt_rdata.data = t->bt_rdata.data == NULL ?
    141 		    malloc(t->bt_reclen) :
    142 		    realloc(t->bt_rdata.data, t->bt_reclen);
    143 		if (t->bt_rdata.data == NULL)
    144 			return (RET_ERROR);
    145 		t->bt_rdata.size = t->bt_reclen;
    146 	}
    147 	data.data = t->bt_rdata.data;
    148 	data.size = t->bt_reclen;
    149 
    150 	for (nrec = t->bt_nrecs; nrec < top;) {
    151 		len = t->bt_reclen;
    152 		for (p = t->bt_rdata.data;; *p++ = ch)
    153 			if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
    154 				if (ch != EOF)
    155 					*p = ch;
    156 				if (len != 0)
    157 					memset(p, t->bt_bval, len);
    158 				if (__rec_iput(t,
    159 				    nrec, &data, 0) != RET_SUCCESS)
    160 					return (RET_ERROR);
    161 				++nrec;
    162 				break;
    163 			}
    164 		if (ch == EOF)
    165 			break;
    166 	}
    167 	if (nrec < top) {
    168 		F_SET(t, R_EOF);
    169 		return (RET_SPECIAL);
    170 	}
    171 	return (RET_SUCCESS);
    172 }
    173 
    174 /*
    175  * __REC_VPIPE -- Get variable length records from a pipe.
    176  *
    177  * Parameters:
    178  *	t:	tree
    179  *	cnt:	records to read
    180  *
    181  * Returns:
    182  *	RET_ERROR, RET_SUCCESS
    183  */
    184 int
    185 __rec_vpipe(t, top)
    186 	BTREE *t;
    187 	recno_t top;
    188 {
    189 	DBT data;
    190 	recno_t nrec;
    191 	indx_t len;
    192 	size_t sz;
    193 	int bval, ch;
    194 	u_char *p;
    195 
    196 	bval = t->bt_bval;
    197 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
    198 		for (p = t->bt_rdata.data,
    199 		    sz = t->bt_rdata.size;; *p++ = ch, --sz) {
    200 			if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
    201 				data.data = t->bt_rdata.data;
    202 				data.size = p - (u_char *)t->bt_rdata.data;
    203 				if (ch == EOF && data.size == 0)
    204 					break;
    205 				if (__rec_iput(t, nrec, &data, 0)
    206 				    != RET_SUCCESS)
    207 					return (RET_ERROR);
    208 				break;
    209 			}
    210 			if (sz == 0) {
    211 				len = p - (u_char *)t->bt_rdata.data;
    212 				t->bt_rdata.size += (sz = 256);
    213 				t->bt_rdata.data = t->bt_rdata.data == NULL ?
    214 				    malloc(t->bt_rdata.size) :
    215 				    realloc(t->bt_rdata.data, t->bt_rdata.size);
    216 				if (t->bt_rdata.data == NULL)
    217 					return (RET_ERROR);
    218 				p = (u_char *)t->bt_rdata.data + len;
    219 			}
    220 		}
    221 		if (ch == EOF)
    222 			break;
    223 	}
    224 	if (nrec < top) {
    225 		F_SET(t, R_EOF);
    226 		return (RET_SPECIAL);
    227 	}
    228 	return (RET_SUCCESS);
    229 }
    230 
    231 /*
    232  * __REC_FMAP -- Get fixed length records from a file.
    233  *
    234  * Parameters:
    235  *	t:	tree
    236  *	cnt:	records to read
    237  *
    238  * Returns:
    239  *	RET_ERROR, RET_SUCCESS
    240  */
    241 int
    242 __rec_fmap(t, top)
    243 	BTREE *t;
    244 	recno_t top;
    245 {
    246 	DBT data;
    247 	recno_t nrec;
    248 	u_char *sp, *ep, *p;
    249 	size_t len;
    250 
    251 	if (t->bt_rdata.size < t->bt_reclen) {
    252 		t->bt_rdata.data = t->bt_rdata.data == NULL ?
    253 		    malloc(t->bt_reclen) :
    254 		    realloc(t->bt_rdata.data, t->bt_reclen);
    255 		if (t->bt_rdata.data == NULL)
    256 			return (RET_ERROR);
    257 		t->bt_rdata.size = t->bt_reclen;
    258 	}
    259 	data.data = t->bt_rdata.data;
    260 	data.size = t->bt_reclen;
    261 
    262 	sp = (u_char *)t->bt_cmap;
    263 	ep = (u_char *)t->bt_emap;
    264 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
    265 		if (sp >= ep) {
    266 			F_SET(t, R_EOF);
    267 			return (RET_SPECIAL);
    268 		}
    269 		len = t->bt_reclen;
    270 		for (p = t->bt_rdata.data;
    271 		    sp < ep && len > 0; *p++ = *sp++, --len);
    272 		if (len != 0)
    273 			memset(p, t->bt_bval, len);
    274 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
    275 			return (RET_ERROR);
    276 	}
    277 	t->bt_cmap = (caddr_t)sp;
    278 	return (RET_SUCCESS);
    279 }
    280 
    281 /*
    282  * __REC_VMAP -- Get variable length records from a file.
    283  *
    284  * Parameters:
    285  *	t:	tree
    286  *	cnt:	records to read
    287  *
    288  * Returns:
    289  *	RET_ERROR, RET_SUCCESS
    290  */
    291 int
    292 __rec_vmap(t, top)
    293 	BTREE *t;
    294 	recno_t top;
    295 {
    296 	DBT data;
    297 	u_char *sp, *ep;
    298 	recno_t nrec;
    299 	int bval;
    300 
    301 	sp = (u_char *)t->bt_cmap;
    302 	ep = (u_char *)t->bt_emap;
    303 	bval = t->bt_bval;
    304 
    305 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
    306 		if (sp >= ep) {
    307 			F_SET(t, R_EOF);
    308 			return (RET_SPECIAL);
    309 		}
    310 		for (data.data = sp; sp < ep && *sp != bval; ++sp);
    311 		data.size = sp - (u_char *)data.data;
    312 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
    313 			return (RET_ERROR);
    314 		++sp;
    315 	}
    316 	t->bt_cmap = (caddr_t)sp;
    317 	return (RET_SUCCESS);
    318 }
    319