Home | History | Annotate | Line # | Download | only in recno
rec_get.c revision 1.14
      1 /*	$NetBSD: rec_get.c,v 1.14 2008/08/26 21:18:38 joerg 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. 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.
     18  *
     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
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char sccsid[] = "@(#)rec_get.c	8.9 (Berkeley) 8/18/94";
     36 #else
     37 __RCSID("$NetBSD: rec_get.c,v 1.14 2008/08/26 21:18:38 joerg Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include "namespace.h"
     42 #include <sys/types.h>
     43 
     44 #include <assert.h>
     45 #include <errno.h>
     46 #include <stddef.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 
     52 #include <db.h>
     53 #include "recno.h"
     54 
     55 /*
     56  * __REC_GET -- Get a record from the btree.
     57  *
     58  * Parameters:
     59  *	dbp:	pointer to access method
     60  *	key:	key to find
     61  *	data:	data to return
     62  *	flag:	currently unused
     63  *
     64  * Returns:
     65  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
     66  */
     67 int
     68 __rec_get(const DB *dbp, const DBT *key, DBT *data, u_int flags)
     69 {
     70 	BTREE *t;
     71 	EPG *e;
     72 	recno_t nrec;
     73 	int status;
     74 
     75 	t = dbp->internal;
     76 
     77 	/* Toss any page pinned across calls. */
     78 	if (t->bt_pinned != NULL) {
     79 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     80 		t->bt_pinned = NULL;
     81 	}
     82 
     83 	/* Get currently doesn't take any flags, and keys of 0 are illegal. */
     84 	if (flags || (nrec = *(recno_t *)key->data) == 0) {
     85 		errno = EINVAL;
     86 		return (RET_ERROR);
     87 	}
     88 
     89 	/*
     90 	 * If we haven't seen this record yet, try to find it in the
     91 	 * original file.
     92 	 */
     93 	if (nrec > t->bt_nrecs) {
     94 		if (F_ISSET(t, R_EOF | R_INMEM))
     95 			return (RET_SPECIAL);
     96 		if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
     97 			return (status);
     98 	}
     99 
    100 	--nrec;
    101 	if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
    102 		return (RET_ERROR);
    103 
    104 	status = __rec_ret(t, e, 0, NULL, data);
    105 	if (F_ISSET(t, B_DB_LOCK))
    106 		mpool_put(t->bt_mp, e->page, 0);
    107 	else
    108 		t->bt_pinned = e->page;
    109 	return (status);
    110 }
    111 
    112 /*
    113  * __REC_FPIPE -- Get fixed length records from a pipe.
    114  *
    115  * Parameters:
    116  *	t:	tree
    117  *	cnt:	records to read
    118  *
    119  * Returns:
    120  *	RET_ERROR, RET_SUCCESS
    121  */
    122 int
    123 __rec_fpipe(BTREE *t, recno_t top)
    124 {
    125 	DBT data;
    126 	recno_t nrec;
    127 	size_t len;
    128 	int ch;
    129 	uint8_t *p;
    130 
    131 	if (t->bt_rdata.size < t->bt_reclen) {
    132 		t->bt_rdata.data = t->bt_rdata.data == NULL ?
    133 		    malloc(t->bt_reclen) :
    134 		    realloc(t->bt_rdata.data, t->bt_reclen);
    135 		if (t->bt_rdata.data == NULL)
    136 			return (RET_ERROR);
    137 		t->bt_rdata.size = t->bt_reclen;
    138 	}
    139 	data.data = t->bt_rdata.data;
    140 	data.size = t->bt_reclen;
    141 
    142 	for (nrec = t->bt_nrecs; nrec < top;) {
    143 		len = t->bt_reclen;
    144 		for (p = t->bt_rdata.data;; *p++ = ch)
    145 			if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
    146 				if (ch != EOF)
    147 					*p = ch;
    148 				if (len != 0)
    149 					memset(p, t->bt_bval, len);
    150 				if (__rec_iput(t,
    151 				    nrec, &data, 0) != RET_SUCCESS)
    152 					return (RET_ERROR);
    153 				++nrec;
    154 				break;
    155 			}
    156 		if (ch == EOF)
    157 			break;
    158 	}
    159 	if (nrec < top) {
    160 		F_SET(t, R_EOF);
    161 		return (RET_SPECIAL);
    162 	}
    163 	return (RET_SUCCESS);
    164 }
    165 
    166 /*
    167  * __REC_VPIPE -- Get variable length records from a pipe.
    168  *
    169  * Parameters:
    170  *	t:	tree
    171  *	cnt:	records to read
    172  *
    173  * Returns:
    174  *	RET_ERROR, RET_SUCCESS
    175  */
    176 int
    177 __rec_vpipe(BTREE *t, recno_t top)
    178 {
    179 	DBT data;
    180 	recno_t nrec;
    181 	ptrdiff_t len;
    182 	size_t sz;
    183 	int bval, ch;
    184 	uint8_t *p;
    185 
    186 	bval = t->bt_bval;
    187 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
    188 		for (p = t->bt_rdata.data,
    189 		    sz = t->bt_rdata.size;; *p++ = ch, --sz) {
    190 			if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
    191 				data.data = t->bt_rdata.data;
    192 				data.size = p - (uint8_t *)t->bt_rdata.data;
    193 				if (ch == EOF && data.size == 0)
    194 					break;
    195 				if (__rec_iput(t, nrec, &data, 0)
    196 				    != RET_SUCCESS)
    197 					return (RET_ERROR);
    198 				break;
    199 			}
    200 			if (sz == 0) {
    201 				len = p - (uint8_t *)t->bt_rdata.data;
    202 				t->bt_rdata.size += (sz = 256);
    203 				t->bt_rdata.data = t->bt_rdata.data == NULL ?
    204 				    malloc(t->bt_rdata.size) :
    205 				    realloc(t->bt_rdata.data, t->bt_rdata.size);
    206 				if (t->bt_rdata.data == NULL)
    207 					return (RET_ERROR);
    208 				p = (uint8_t *)t->bt_rdata.data + len;
    209 			}
    210 		}
    211 		if (ch == EOF)
    212 			break;
    213 	}
    214 	if (nrec < top) {
    215 		F_SET(t, R_EOF);
    216 		return (RET_SPECIAL);
    217 	}
    218 	return (RET_SUCCESS);
    219 }
    220 
    221 /*
    222  * __REC_FMAP -- Get fixed length records from a file.
    223  *
    224  * Parameters:
    225  *	t:	tree
    226  *	cnt:	records to read
    227  *
    228  * Returns:
    229  *	RET_ERROR, RET_SUCCESS
    230  */
    231 int
    232 __rec_fmap(BTREE *t, recno_t top)
    233 {
    234 	DBT data;
    235 	recno_t nrec;
    236 	uint8_t *sp, *ep, *p;
    237 	size_t len;
    238 
    239 	if (t->bt_rdata.size < t->bt_reclen) {
    240 		t->bt_rdata.data = t->bt_rdata.data == NULL ?
    241 		    malloc(t->bt_reclen) :
    242 		    realloc(t->bt_rdata.data, t->bt_reclen);
    243 		if (t->bt_rdata.data == NULL)
    244 			return (RET_ERROR);
    245 		t->bt_rdata.size = t->bt_reclen;
    246 	}
    247 	data.data = t->bt_rdata.data;
    248 	data.size = t->bt_reclen;
    249 
    250 	sp = (uint8_t *)t->bt_cmap;
    251 	ep = (uint8_t *)t->bt_emap;
    252 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
    253 		if (sp >= ep) {
    254 			F_SET(t, R_EOF);
    255 			return (RET_SPECIAL);
    256 		}
    257 		len = t->bt_reclen;
    258 		for (p = t->bt_rdata.data;
    259 		    sp < ep && len > 0; *p++ = *sp++, --len);
    260 		if (len != 0)
    261 			memset(p, t->bt_bval, len);
    262 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
    263 			return (RET_ERROR);
    264 	}
    265 	t->bt_cmap = (caddr_t)sp;
    266 	return (RET_SUCCESS);
    267 }
    268 
    269 /*
    270  * __REC_VMAP -- Get variable length records from a file.
    271  *
    272  * Parameters:
    273  *	t:	tree
    274  *	cnt:	records to read
    275  *
    276  * Returns:
    277  *	RET_ERROR, RET_SUCCESS
    278  */
    279 int
    280 __rec_vmap(BTREE *t, recno_t top)
    281 {
    282 	DBT data;
    283 	uint8_t *sp, *ep;
    284 	recno_t nrec;
    285 	int bval;
    286 
    287 	sp = (uint8_t *)t->bt_cmap;
    288 	ep = (uint8_t *)t->bt_emap;
    289 	bval = t->bt_bval;
    290 
    291 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
    292 		if (sp >= ep) {
    293 			F_SET(t, R_EOF);
    294 			return (RET_SPECIAL);
    295 		}
    296 		for (data.data = sp; sp < ep && *sp != bval; ++sp);
    297 		data.size = sp - (uint8_t *)data.data;
    298 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
    299 			return (RET_ERROR);
    300 		++sp;
    301 	}
    302 	t->bt_cmap = (caddr_t)sp;
    303 	return (RET_SUCCESS);
    304 }
    305