xref: /titanic_41/usr/src/lib/krb5/plugins/kdb/db2/libdb2/btree/bt_utils.c (revision 54925bf60766fbb4f1f2d7c843721406a7b7a3fb)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*-
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Mike Olson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)bt_utils.c	8.8 (Berkeley) 7/20/94";
41 #endif /* LIBC_SCCS and not lint */
42 
43 #include <sys/param.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include "db-int.h"
50 #include "btree.h"
51 
52 /*
53  * __bt_ret --
54  *	Build return key/data pair.
55  *
56  * Parameters:
57  *	t:	tree
58  *	e:	key/data pair to be returned
59  *	key:	user's key structure (NULL if not to be filled in)
60  *	rkey:	memory area to hold key
61  *	data:	user's data structure (NULL if not to be filled in)
62  *	rdata:	memory area to hold data
63  *       copy:	always copy the key/data item
64  *
65  * Returns:
66  *	RET_SUCCESS, RET_ERROR.
67  */
68 int
__bt_ret(t,e,key,rkey,data,rdata,copy)69 __bt_ret(t, e, key, rkey, data, rdata, copy)
70 	BTREE *t;
71 	EPG *e;
72 	DBT *key, *rkey, *data, *rdata;
73 	int copy;
74 {
75 	BLEAF *bl;
76 	void *p;
77 
78 	bl = GETBLEAF(e->page, e->index);
79 
80 	/*
81 	 * We must copy big keys/data to make them contigous.  Otherwise,
82 	 * leave the page pinned and don't copy unless the user specified
83 	 * concurrent access.
84 	 */
85 	if (key == NULL)
86 		goto dataonly;
87 
88 	if (bl->flags & P_BIGKEY) {
89 		if (__ovfl_get(t, bl->bytes,
90 		    &key->size, &rkey->data, &rkey->size))
91 			return (RET_ERROR);
92 		key->data = rkey->data;
93 	} else if (copy || F_ISSET(t, B_DB_LOCK)) {
94 		if (bl->ksize > rkey->size) {
95 			p = (void *)(rkey->data == NULL ?
96 			    malloc(bl->ksize) : realloc(rkey->data, bl->ksize));
97 			if (p == NULL)
98 				return (RET_ERROR);
99 			rkey->data = p;
100 			rkey->size = bl->ksize;
101 		}
102 		memmove(rkey->data, bl->bytes, bl->ksize);
103 		key->size = bl->ksize;
104 		key->data = rkey->data;
105 	} else {
106 		key->size = bl->ksize;
107 		key->data = bl->bytes;
108 	}
109 
110 dataonly:
111 	if (data == NULL)
112 		return (RET_SUCCESS);
113 
114 	if (bl->flags & P_BIGDATA) {
115 		if (__ovfl_get(t, bl->bytes + bl->ksize,
116 		    &data->size, &rdata->data, &rdata->size))
117 			return (RET_ERROR);
118 		data->data = rdata->data;
119 	} else if (copy || F_ISSET(t, B_DB_LOCK)) {
120 		/* Use +1 in case the first record retrieved is 0 length. */
121 		if (bl->dsize + 1 > rdata->size) {
122 			p = (void *)(rdata->data == NULL ?
123 			    malloc(bl->dsize + 1) :
124 			    realloc(rdata->data, bl->dsize + 1));
125 			if (p == NULL)
126 				return (RET_ERROR);
127 			rdata->data = p;
128 			rdata->size = bl->dsize + 1;
129 		}
130 		memmove(rdata->data, bl->bytes + bl->ksize, bl->dsize);
131 		data->size = bl->dsize;
132 		data->data = rdata->data;
133 	} else {
134 		data->size = bl->dsize;
135 		data->data = bl->bytes + bl->ksize;
136 	}
137 
138 	return (RET_SUCCESS);
139 }
140 
141 /*
142  * __BT_CMP -- Compare a key to a given record.
143  *
144  * Parameters:
145  *	t:	tree
146  *	k1:	DBT pointer of first arg to comparison
147  *	e:	pointer to EPG for comparison
148  *
149  * Returns:
150  *	< 0 if k1 is < record
151  *	= 0 if k1 is = record
152  *	> 0 if k1 is > record
153  */
154 int
__bt_cmp(t,k1,e)155 __bt_cmp(t, k1, e)
156 	BTREE *t;
157 	const DBT *k1;
158 	EPG *e;
159 {
160 	BINTERNAL *bi;
161 	BLEAF *bl;
162 	DBT k2;
163 	PAGE *h;
164 	void *bigkey;
165 
166 	/*
167 	 * The left-most key on internal pages, at any level of the tree, is
168 	 * guaranteed by the following code to be less than any user key.
169 	 * This saves us from having to update the leftmost key on an internal
170 	 * page when the user inserts a new key in the tree smaller than
171 	 * anything we've yet seen.
172 	 */
173 	h = e->page;
174 	if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
175 		return (1);
176 
177 	bigkey = NULL;
178 	if (h->flags & P_BLEAF) {
179 		bl = GETBLEAF(h, e->index);
180 		if (bl->flags & P_BIGKEY)
181 			bigkey = bl->bytes;
182 		else {
183 			k2.data = bl->bytes;
184 			k2.size = bl->ksize;
185 		}
186 	} else {
187 		bi = GETBINTERNAL(h, e->index);
188 		if (bi->flags & P_BIGKEY)
189 			bigkey = bi->bytes;
190 		else {
191 			k2.data = bi->bytes;
192 			k2.size = bi->ksize;
193 		}
194 	}
195 
196 	if (bigkey) {
197 		if (__ovfl_get(t, bigkey,
198 		    &k2.size, &t->bt_rdata.data, &t->bt_rdata.size))
199 			return (RET_ERROR);
200 		k2.data = t->bt_rdata.data;
201 	}
202 	return ((*t->bt_cmp)(k1, &k2));
203 }
204 
205 /*
206  * __BT_DEFCMP -- Default comparison routine.
207  *
208  * Parameters:
209  *	a:	DBT #1
210  *	b: 	DBT #2
211  *
212  * Returns:
213  *	< 0 if a is < b
214  *	= 0 if a is = b
215  *	> 0 if a is > b
216  */
217 int
__bt_defcmp(a,b)218 __bt_defcmp(a, b)
219 	const DBT *a, *b;
220 {
221 	register size_t len;
222 	register u_char *p1, *p2;
223 
224 	/*
225 	 * XXX
226 	 * If a size_t doesn't fit in an int, this routine can lose.
227 	 * What we need is a integral type which is guaranteed to be
228 	 * larger than a size_t, and there is no such thing.
229 	 */
230 	len = MIN(a->size, b->size);
231 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
232 		if (*p1 != *p2)
233 			return ((int)*p1 - (int)*p2);
234 	return ((int)a->size - (int)b->size);
235 }
236 
237 /*
238  * __BT_DEFPFX -- Default prefix routine.
239  *
240  * Parameters:
241  *	a:	DBT #1
242  *	b: 	DBT #2
243  *
244  * Returns:
245  *	Number of bytes needed to distinguish b from a.
246  */
247 size_t
__bt_defpfx(a,b)248 __bt_defpfx(a, b)
249 	const DBT *a, *b;
250 {
251 	register u_char *p1, *p2;
252 	register size_t cnt, len;
253 
254 	cnt = 1;
255 	len = MIN(a->size, b->size);
256 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
257 		if (*p1 != *p2)
258 			return (cnt);
259 
260 	/* a->size must be <= b->size, or they wouldn't be in this order. */
261 	return (a->size < b->size ? a->size + 1 : a->size);
262 }
263