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