xref: /csrg-svn/lib/libc/db/recno/rec_utils.c (revision 66213)
150995Sbostic /*-
261207Sbostic  * Copyright (c) 1990, 1993
361207Sbostic  *	The Regents of the University of California.  All rights reserved.
450995Sbostic  *
550995Sbostic  * %sccs.include.redist.c%
650995Sbostic  */
750995Sbostic 
850995Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*66213Sbostic static char sccsid[] = "@(#)rec_utils.c	8.3 (Berkeley) 02/21/94";
1050995Sbostic #endif /* LIBC_SCCS and not lint */
1150995Sbostic 
1250995Sbostic #include <sys/param.h>
1356761Sbostic 
1450995Sbostic #include <stdio.h>
1550995Sbostic #include <stdlib.h>
1650995Sbostic #include <string.h>
1756761Sbostic 
1857933Sbostic #include <db.h>
1951093Sbostic #include "recno.h"
2050995Sbostic 
2150995Sbostic /*
2250995Sbostic  * __REC_RET -- Build return data as a result of search or scan.
2350995Sbostic  *
2450995Sbostic  * Parameters:
2550995Sbostic  *	t:	tree
2650995Sbostic  *	d:	LEAF to be returned to the user.
2750995Sbostic  *	data:	user's data structure
2850995Sbostic  *
2950995Sbostic  * Returns:
3050995Sbostic  *	RET_SUCCESS, RET_ERROR.
3150995Sbostic  */
3250995Sbostic int
__rec_ret(t,e,nrec,key,data)3356761Sbostic __rec_ret(t, e, nrec, key, data)
3450995Sbostic 	BTREE *t;
3550995Sbostic 	EPG *e;
3656761Sbostic 	recno_t nrec;
3756761Sbostic 	DBT *key, *data;
3850995Sbostic {
3950995Sbostic 	register RLEAF *rl;
4056761Sbostic 	register void *p;
4150995Sbostic 
4256761Sbostic 	if (data == NULL)
4356761Sbostic 		goto retkey;
4456761Sbostic 
4550995Sbostic 	rl = GETRLEAF(e->page, e->index);
4656761Sbostic 
4764464Sbostic 	/*
4864464Sbostic 	 * We always copy big data to make it contigous.  Otherwise, we
4964464Sbostic 	 * leave the page pinned and don't copy unless the user specified
5064464Sbostic 	 * concurrent access.
5164464Sbostic 	 */
5250995Sbostic 	if (rl->flags & P_BIGDATA) {
5350995Sbostic 		if (__ovfl_get(t, rl->bytes,
5450995Sbostic 		    &data->size, &t->bt_dbuf, &t->bt_dbufsz))
5550995Sbostic 			return (RET_ERROR);
5664464Sbostic 		data->data = t->bt_dbuf;
5764464Sbostic 	} else if (ISSET(t, B_DB_LOCK)) {
5854285Sbostic 		/* Use +1 in case the first record retrieved is 0 length. */
5954285Sbostic 		if (rl->dsize + 1 > t->bt_dbufsz) {
60*66213Sbostic 			if ((p =
61*66213Sbostic 			    (void *)realloc(t->bt_dbuf, rl->dsize + 1)) == NULL)
6250995Sbostic 				return (RET_ERROR);
6351093Sbostic 			t->bt_dbuf = p;
6454285Sbostic 			t->bt_dbufsz = rl->dsize + 1;
6550995Sbostic 		}
6658015Sbostic 		memmove(t->bt_dbuf, rl->bytes, rl->dsize);
6750995Sbostic 		data->size = rl->dsize;
6864464Sbostic 		data->data = t->bt_dbuf;
6964464Sbostic 	} else {
7064464Sbostic 		data->size = rl->dsize;
7164464Sbostic 		data->data = rl->bytes;
7250995Sbostic 	}
7350995Sbostic 
7456761Sbostic retkey:	if (key == NULL)
7556761Sbostic 		return (RET_SUCCESS);
7656761Sbostic 
7764464Sbostic 	/* We have to copy the key, it's not on the page. */
7856761Sbostic 	if (sizeof(recno_t) > t->bt_kbufsz) {
79*66213Sbostic 		if ((p = (void *)realloc(t->bt_kbuf, sizeof(recno_t))) == NULL)
8056761Sbostic 			return (RET_ERROR);
8156761Sbostic 		t->bt_kbuf = p;
8256761Sbostic 		t->bt_kbufsz = sizeof(recno_t);
8356761Sbostic 	}
8458015Sbostic 	memmove(t->bt_kbuf, &nrec, sizeof(recno_t));
8556761Sbostic 	key->size = sizeof(recno_t);
8656761Sbostic 	key->data = t->bt_kbuf;
8750995Sbostic 	return (RET_SUCCESS);
8850995Sbostic }
89