xref: /csrg-svn/lib/libc/db/recno/rec_utils.c (revision 64464)
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*64464Sbostic static char sccsid[] = "@(#)rec_utils.c	8.2 (Berkeley) 09/07/93";
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
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 
47*64464Sbostic 	/*
48*64464Sbostic 	 * We always copy big data to make it contigous.  Otherwise, we
49*64464Sbostic 	 * leave the page pinned and don't copy unless the user specified
50*64464Sbostic 	 * concurrent access.
51*64464Sbostic 	 */
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);
56*64464Sbostic 		data->data = t->bt_dbuf;
57*64464Sbostic 	} 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) {
6054285Sbostic 			if ((p = realloc(t->bt_dbuf, rl->dsize + 1)) == NULL)
6150995Sbostic 				return (RET_ERROR);
6251093Sbostic 			t->bt_dbuf = p;
6354285Sbostic 			t->bt_dbufsz = rl->dsize + 1;
6450995Sbostic 		}
6558015Sbostic 		memmove(t->bt_dbuf, rl->bytes, rl->dsize);
6650995Sbostic 		data->size = rl->dsize;
67*64464Sbostic 		data->data = t->bt_dbuf;
68*64464Sbostic 	} else {
69*64464Sbostic 		data->size = rl->dsize;
70*64464Sbostic 		data->data = rl->bytes;
7150995Sbostic 	}
7250995Sbostic 
7356761Sbostic retkey:	if (key == NULL)
7456761Sbostic 		return (RET_SUCCESS);
7556761Sbostic 
76*64464Sbostic 	/* We have to copy the key, it's not on the page. */
7756761Sbostic 	if (sizeof(recno_t) > t->bt_kbufsz) {
7856761Sbostic 		if ((p = realloc(t->bt_kbuf, sizeof(recno_t))) == NULL)
7956761Sbostic 			return (RET_ERROR);
8056761Sbostic 		t->bt_kbuf = p;
8156761Sbostic 		t->bt_kbufsz = sizeof(recno_t);
8256761Sbostic 	}
8358015Sbostic 	memmove(t->bt_kbuf, &nrec, sizeof(recno_t));
8456761Sbostic 	key->size = sizeof(recno_t);
8556761Sbostic 	key->data = t->bt_kbuf;
8650995Sbostic 	return (RET_SUCCESS);
8750995Sbostic }
88