xref: /openbsd-src/lib/libc/db/btree/bt_utils.c (revision 7ae68d51a02c87d3c56b7f5939be167a4c2c928c)
1 /*	$OpenBSD: bt_utils.c,v 1.5 2003/05/01 20:23:40 avsm Exp $	*/
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 #if 0
41 static char sccsid[] = "@(#)bt_utils.c	8.8 (Berkeley) 7/20/94";
42 #else
43 static const char rcsid[] = "$OpenBSD: bt_utils.c,v 1.5 2003/05/01 20:23:40 avsm Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46 
47 #include <sys/param.h>
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include <db.h>
54 #include "btree.h"
55 
56 /*
57  * __bt_ret --
58  *	Build return key/data pair.
59  *
60  * Parameters:
61  *	t:	tree
62  *	e:	key/data pair to be returned
63  *	key:	user's key structure (NULL if not to be filled in)
64  *	rkey:	memory area to hold key
65  *	data:	user's data structure (NULL if not to be filled in)
66  *	rdata:	memory area to hold data
67  *       copy:	always copy the key/data item
68  *
69  * Returns:
70  *	RET_SUCCESS, RET_ERROR.
71  */
72 int
73 __bt_ret(t, e, key, rkey, data, rdata, copy)
74 	BTREE *t;
75 	EPG *e;
76 	DBT *key, *rkey, *data, *rdata;
77 	int copy;
78 {
79 	BLEAF *bl;
80 	void *p;
81 
82 	bl = GETBLEAF(e->page, e->index);
83 
84 	/*
85 	 * We must copy big keys/data to make them contigous.  Otherwise,
86 	 * leave the page pinned and don't copy unless the user specified
87 	 * concurrent access.
88 	 */
89 	if (key == NULL)
90 		goto dataonly;
91 
92 	if (bl->flags & P_BIGKEY) {
93 		if (__ovfl_get(t, bl->bytes,
94 		    &key->size, &rkey->data, &rkey->size))
95 			return (RET_ERROR);
96 		key->data = rkey->data;
97 	} else if (copy || F_ISSET(t, B_DB_LOCK)) {
98 		if (bl->ksize > rkey->size) {
99 			p = (void *)(rkey->data == NULL ?
100 			    malloc(bl->ksize) : realloc(rkey->data, bl->ksize));
101 			if (p == NULL)
102 				return (RET_ERROR);
103 			rkey->data = p;
104 			rkey->size = bl->ksize;
105 		}
106 		memmove(rkey->data, bl->bytes, bl->ksize);
107 		key->size = bl->ksize;
108 		key->data = rkey->data;
109 	} else {
110 		key->size = bl->ksize;
111 		key->data = bl->bytes;
112 	}
113 
114 dataonly:
115 	if (data == NULL)
116 		return (RET_SUCCESS);
117 
118 	if (bl->flags & P_BIGDATA) {
119 		if (__ovfl_get(t, bl->bytes + bl->ksize,
120 		    &data->size, &rdata->data, &rdata->size))
121 			return (RET_ERROR);
122 		data->data = rdata->data;
123 	} else if (copy || F_ISSET(t, B_DB_LOCK)) {
124 		/* Use +1 in case the first record retrieved is 0 length. */
125 		if (bl->dsize + 1 > rdata->size) {
126 			p = (void *)(rdata->data == NULL ?
127 			    malloc(bl->dsize + 1) :
128 			    realloc(rdata->data, bl->dsize + 1));
129 			if (p == NULL)
130 				return (RET_ERROR);
131 			rdata->data = p;
132 			rdata->size = bl->dsize + 1;
133 		}
134 		memmove(rdata->data, bl->bytes + bl->ksize, bl->dsize);
135 		data->size = bl->dsize;
136 		data->data = rdata->data;
137 	} else {
138 		data->size = bl->dsize;
139 		data->data = bl->bytes + bl->ksize;
140 	}
141 
142 	return (RET_SUCCESS);
143 }
144 
145 /*
146  * __BT_CMP -- Compare a key to a given record.
147  *
148  * Parameters:
149  *	t:	tree
150  *	k1:	DBT pointer of first arg to comparison
151  *	e:	pointer to EPG for comparison
152  *
153  * Returns:
154  *	< 0 if k1 is < record
155  *	= 0 if k1 is = record
156  *	> 0 if k1 is > record
157  */
158 int
159 __bt_cmp(t, k1, e)
160 	BTREE *t;
161 	const DBT *k1;
162 	EPG *e;
163 {
164 	BINTERNAL *bi;
165 	BLEAF *bl;
166 	DBT k2;
167 	PAGE *h;
168 	void *bigkey;
169 
170 	/*
171 	 * The left-most key on internal pages, at any level of the tree, is
172 	 * guaranteed by the following code to be less than any user key.
173 	 * This saves us from having to update the leftmost key on an internal
174 	 * page when the user inserts a new key in the tree smaller than
175 	 * anything we've yet seen.
176 	 */
177 	h = e->page;
178 	if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
179 		return (1);
180 
181 	bigkey = NULL;
182 	if (h->flags & P_BLEAF) {
183 		bl = GETBLEAF(h, e->index);
184 		if (bl->flags & P_BIGKEY)
185 			bigkey = bl->bytes;
186 		else {
187 			k2.data = bl->bytes;
188 			k2.size = bl->ksize;
189 		}
190 	} else {
191 		bi = GETBINTERNAL(h, e->index);
192 		if (bi->flags & P_BIGKEY)
193 			bigkey = bi->bytes;
194 		else {
195 			k2.data = bi->bytes;
196 			k2.size = bi->ksize;
197 		}
198 	}
199 
200 	if (bigkey) {
201 		if (__ovfl_get(t, bigkey,
202 		    &k2.size, &t->bt_rdata.data, &t->bt_rdata.size))
203 			return (RET_ERROR);
204 		k2.data = t->bt_rdata.data;
205 	}
206 	return ((*t->bt_cmp)(k1, &k2));
207 }
208 
209 /*
210  * __BT_DEFCMP -- Default comparison routine.
211  *
212  * Parameters:
213  *	a:	DBT #1
214  *	b: 	DBT #2
215  *
216  * Returns:
217  *	< 0 if a is < b
218  *	= 0 if a is = b
219  *	> 0 if a is > b
220  */
221 int
222 __bt_defcmp(a, b)
223 	const DBT *a, *b;
224 {
225 	register size_t len;
226 	register u_char *p1, *p2;
227 
228 	/*
229 	 * XXX
230 	 * If a size_t doesn't fit in an int, this routine can lose.
231 	 * What we need is a integral type which is guaranteed to be
232 	 * larger than a size_t, and there is no such thing.
233 	 */
234 	len = MIN(a->size, b->size);
235 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
236 		if (*p1 != *p2)
237 			return ((int)*p1 - (int)*p2);
238 	return ((int)a->size - (int)b->size);
239 }
240 
241 /*
242  * __BT_DEFPFX -- Default prefix routine.
243  *
244  * Parameters:
245  *	a:	DBT #1
246  *	b: 	DBT #2
247  *
248  * Returns:
249  *	Number of bytes needed to distinguish b from a.
250  */
251 size_t
252 __bt_defpfx(a, b)
253 	const DBT *a, *b;
254 {
255 	register u_char *p1, *p2;
256 	register size_t cnt, len;
257 
258 	cnt = 1;
259 	len = MIN(a->size, b->size);
260 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
261 		if (*p1 != *p2)
262 			return (cnt);
263 
264 	/* a->size must be <= b->size, or they wouldn't be in this order. */
265 	return (a->size < b->size ? a->size + 1 : a->size);
266 }
267