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