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