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