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