xref: /minix3/lib/libc/db/recno/rec_utils.c (revision 2639ae9b1755f49e4eb4b211ce63d8879b0edd4a)
1*2639ae9bSBen Gras /*	$NetBSD: rec_utils.c,v 1.12 2008/09/10 17:52:36 joerg Exp $	*/
2*2639ae9bSBen Gras 
3*2639ae9bSBen Gras /*-
4*2639ae9bSBen Gras  * Copyright (c) 1990, 1993, 1994
5*2639ae9bSBen Gras  *	The Regents of the University of California.  All rights reserved.
6*2639ae9bSBen Gras  *
7*2639ae9bSBen Gras  * Redistribution and use in source and binary forms, with or without
8*2639ae9bSBen Gras  * modification, are permitted provided that the following conditions
9*2639ae9bSBen Gras  * are met:
10*2639ae9bSBen Gras  * 1. Redistributions of source code must retain the above copyright
11*2639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer.
12*2639ae9bSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13*2639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer in the
14*2639ae9bSBen Gras  *    documentation and/or other materials provided with the distribution.
15*2639ae9bSBen Gras  * 3. Neither the name of the University nor the names of its contributors
16*2639ae9bSBen Gras  *    may be used to endorse or promote products derived from this software
17*2639ae9bSBen Gras  *    without specific prior written permission.
18*2639ae9bSBen Gras  *
19*2639ae9bSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*2639ae9bSBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*2639ae9bSBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*2639ae9bSBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*2639ae9bSBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*2639ae9bSBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*2639ae9bSBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*2639ae9bSBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*2639ae9bSBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*2639ae9bSBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*2639ae9bSBen Gras  * SUCH DAMAGE.
30*2639ae9bSBen Gras  */
31*2639ae9bSBen Gras 
32*2639ae9bSBen Gras #if HAVE_NBTOOL_CONFIG_H
33*2639ae9bSBen Gras #include "nbtool_config.h"
34*2639ae9bSBen Gras #endif
35*2639ae9bSBen Gras 
36*2639ae9bSBen Gras #include <sys/cdefs.h>
37*2639ae9bSBen Gras #ifndef __minix
38*2639ae9bSBen Gras __RCSID("$NetBSD: rec_utils.c,v 1.12 2008/09/10 17:52:36 joerg Exp $");
39*2639ae9bSBen Gras #endif
40*2639ae9bSBen Gras 
41*2639ae9bSBen Gras #include <sys/param.h>
42*2639ae9bSBen Gras 
43*2639ae9bSBen Gras #include <assert.h>
44*2639ae9bSBen Gras #include <stdio.h>
45*2639ae9bSBen Gras #include <stdlib.h>
46*2639ae9bSBen Gras #include <string.h>
47*2639ae9bSBen Gras 
48*2639ae9bSBen Gras #include <db.h>
49*2639ae9bSBen Gras #include "recno.h"
50*2639ae9bSBen Gras 
51*2639ae9bSBen Gras /*
52*2639ae9bSBen Gras  * __rec_ret --
53*2639ae9bSBen Gras  *	Build return data.
54*2639ae9bSBen Gras  *
55*2639ae9bSBen Gras  * Parameters:
56*2639ae9bSBen Gras  *	t:	tree
57*2639ae9bSBen Gras  *	e:	key/data pair to be returned
58*2639ae9bSBen Gras  *   nrec:	record number
59*2639ae9bSBen Gras  *    key:	user's key structure
60*2639ae9bSBen Gras  *	data:	user's data structure
61*2639ae9bSBen Gras  *
62*2639ae9bSBen Gras  * Returns:
63*2639ae9bSBen Gras  *	RET_SUCCESS, RET_ERROR.
64*2639ae9bSBen Gras  */
65*2639ae9bSBen Gras int
66*2639ae9bSBen Gras __rec_ret(BTREE *t, EPG *e, recno_t nrec, DBT *key, DBT *data)
67*2639ae9bSBen Gras {
68*2639ae9bSBen Gras 	RLEAF *rl;
69*2639ae9bSBen Gras 	void *p;
70*2639ae9bSBen Gras 
71*2639ae9bSBen Gras 	if (key == NULL)
72*2639ae9bSBen Gras 		goto dataonly;
73*2639ae9bSBen Gras 
74*2639ae9bSBen Gras 	/* We have to copy the key, it's not on the page. */
75*2639ae9bSBen Gras 	if (sizeof(recno_t) > t->bt_rkey.size) {
76*2639ae9bSBen Gras 		p = (void *)(t->bt_rkey.data == NULL ?
77*2639ae9bSBen Gras 		    malloc(sizeof(recno_t)) :
78*2639ae9bSBen Gras 		    realloc(t->bt_rkey.data, sizeof(recno_t)));
79*2639ae9bSBen Gras 		if (p == NULL)
80*2639ae9bSBen Gras 			return (RET_ERROR);
81*2639ae9bSBen Gras 		t->bt_rkey.data = p;
82*2639ae9bSBen Gras 		t->bt_rkey.size = sizeof(recno_t);
83*2639ae9bSBen Gras 	}
84*2639ae9bSBen Gras 	memmove(t->bt_rkey.data, &nrec, sizeof(recno_t));
85*2639ae9bSBen Gras 	key->size = sizeof(recno_t);
86*2639ae9bSBen Gras 	key->data = t->bt_rkey.data;
87*2639ae9bSBen Gras 
88*2639ae9bSBen Gras dataonly:
89*2639ae9bSBen Gras 	if (data == NULL)
90*2639ae9bSBen Gras 		return (RET_SUCCESS);
91*2639ae9bSBen Gras 
92*2639ae9bSBen Gras 	/*
93*2639ae9bSBen Gras 	 * We must copy big keys/data to make them contigous.  Otherwise,
94*2639ae9bSBen Gras 	 * leave the page pinned and don't copy unless the user specified
95*2639ae9bSBen Gras 	 * concurrent access.
96*2639ae9bSBen Gras 	 */
97*2639ae9bSBen Gras 	rl = GETRLEAF(e->page, e->index);
98*2639ae9bSBen Gras 	if (rl->flags & P_BIGDATA) {
99*2639ae9bSBen Gras 		if (__ovfl_get(t, rl->bytes,
100*2639ae9bSBen Gras 		    &data->size, &t->bt_rdata.data, &t->bt_rdata.size))
101*2639ae9bSBen Gras 			return (RET_ERROR);
102*2639ae9bSBen Gras 		data->data = t->bt_rdata.data;
103*2639ae9bSBen Gras 	} else if (F_ISSET(t, B_DB_LOCK)) {
104*2639ae9bSBen Gras 		/* Use +1 in case the first record retrieved is 0 length. */
105*2639ae9bSBen Gras 		if (rl->dsize + 1 > t->bt_rdata.size) {
106*2639ae9bSBen Gras 			p = (void *)(t->bt_rdata.data == NULL ?
107*2639ae9bSBen Gras 			    malloc(rl->dsize + 1) :
108*2639ae9bSBen Gras 			    realloc(t->bt_rdata.data, rl->dsize + 1));
109*2639ae9bSBen Gras 			if (p == NULL)
110*2639ae9bSBen Gras 				return (RET_ERROR);
111*2639ae9bSBen Gras 			t->bt_rdata.data = p;
112*2639ae9bSBen Gras 			t->bt_rdata.size = rl->dsize + 1;
113*2639ae9bSBen Gras 		}
114*2639ae9bSBen Gras 		memmove(t->bt_rdata.data, rl->bytes, rl->dsize);
115*2639ae9bSBen Gras 		data->size = rl->dsize;
116*2639ae9bSBen Gras 		data->data = t->bt_rdata.data;
117*2639ae9bSBen Gras 	} else {
118*2639ae9bSBen Gras 		data->size = rl->dsize;
119*2639ae9bSBen Gras 		data->data = rl->bytes;
120*2639ae9bSBen Gras 	}
121*2639ae9bSBen Gras 	return (RET_SUCCESS);
122*2639ae9bSBen Gras }
123