xref: /freebsd/lib/libc/db/btree/bt_utils.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 /*-
2  * Copyright (c) 1990, 1993
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.4 (Berkeley) 2/21/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 			if ((p =
87 			    (void *)realloc(t->bt_dbuf, bl->dsize + 1)) == NULL)
88 				return (RET_ERROR);
89 			t->bt_dbuf = p;
90 			t->bt_dbufsz = bl->dsize + 1;
91 		}
92 		memmove(t->bt_dbuf, bl->bytes + bl->ksize, bl->dsize);
93 		data->size = bl->dsize;
94 		data->data = t->bt_dbuf;
95 	} else {
96 		data->size = bl->dsize;
97 		data->data = bl->bytes + bl->ksize;
98 	}
99 
100 	if (key == NULL)
101 		return (RET_SUCCESS);
102 
103 	if (bl->flags & P_BIGKEY) {
104 		if (__ovfl_get(t, bl->bytes,
105 		    &key->size, &t->bt_kbuf, &t->bt_kbufsz))
106 			return (RET_ERROR);
107 		key->data = t->bt_kbuf;
108 	} else if (ISSET(t, B_DB_LOCK)) {
109 		if (bl->ksize > t->bt_kbufsz) {
110 			if ((p =
111 			    (void *)realloc(t->bt_kbuf, bl->ksize)) == NULL)
112 				return (RET_ERROR);
113 			t->bt_kbuf = p;
114 			t->bt_kbufsz = bl->ksize;
115 		}
116 		memmove(t->bt_kbuf, bl->bytes, bl->ksize);
117 		key->size = bl->ksize;
118 		key->data = t->bt_kbuf;
119 	} else {
120 		key->size = bl->ksize;
121 		key->data = bl->bytes;
122 	}
123 	return (RET_SUCCESS);
124 }
125 
126 /*
127  * __BT_CMP -- Compare a key to a given record.
128  *
129  * Parameters:
130  *	t:	tree
131  *	k1:	DBT pointer of first arg to comparison
132  *	e:	pointer to EPG for comparison
133  *
134  * Returns:
135  *	< 0 if k1 is < record
136  *	= 0 if k1 is = record
137  *	> 0 if k1 is > record
138  */
139 int
140 __bt_cmp(t, k1, e)
141 	BTREE *t;
142 	const DBT *k1;
143 	EPG *e;
144 {
145 	BINTERNAL *bi;
146 	BLEAF *bl;
147 	DBT k2;
148 	PAGE *h;
149 	void *bigkey;
150 
151 	/*
152 	 * The left-most key on internal pages, at any level of the tree, is
153 	 * guaranteed by the following code to be less than any user key.
154 	 * This saves us from having to update the leftmost key on an internal
155 	 * page when the user inserts a new key in the tree smaller than
156 	 * anything we've yet seen.
157 	 */
158 	h = e->page;
159 	if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
160 		return (1);
161 
162 	bigkey = NULL;
163 	if (h->flags & P_BLEAF) {
164 		bl = GETBLEAF(h, e->index);
165 		if (bl->flags & P_BIGKEY)
166 			bigkey = bl->bytes;
167 		else {
168 			k2.data = bl->bytes;
169 			k2.size = bl->ksize;
170 		}
171 	} else {
172 		bi = GETBINTERNAL(h, e->index);
173 		if (bi->flags & P_BIGKEY)
174 			bigkey = bi->bytes;
175 		else {
176 			k2.data = bi->bytes;
177 			k2.size = bi->ksize;
178 		}
179 	}
180 
181 	if (bigkey) {
182 		if (__ovfl_get(t, bigkey,
183 		    &k2.size, &t->bt_dbuf, &t->bt_dbufsz))
184 			return (RET_ERROR);
185 		k2.data = t->bt_dbuf;
186 	}
187 	return ((*t->bt_cmp)(k1, &k2));
188 }
189 
190 /*
191  * __BT_DEFCMP -- Default comparison routine.
192  *
193  * Parameters:
194  *	a:	DBT #1
195  *	b: 	DBT #2
196  *
197  * Returns:
198  *	< 0 if a is < b
199  *	= 0 if a is = b
200  *	> 0 if a is > b
201  */
202 int
203 __bt_defcmp(a, b)
204 	const DBT *a, *b;
205 {
206 	register size_t len;
207 	register u_char *p1, *p2;
208 
209 	/*
210 	 * XXX
211 	 * If a size_t doesn't fit in an int, this routine can lose.
212 	 * What we need is a integral type which is guaranteed to be
213 	 * larger than a size_t, and there is no such thing.
214 	 */
215 	len = MIN(a->size, b->size);
216 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
217 		if (*p1 != *p2)
218 			return ((int)*p1 - (int)*p2);
219 	return ((int)a->size - (int)b->size);
220 }
221 
222 /*
223  * __BT_DEFPFX -- Default prefix routine.
224  *
225  * Parameters:
226  *	a:	DBT #1
227  *	b: 	DBT #2
228  *
229  * Returns:
230  *	Number of bytes needed to distinguish b from a.
231  */
232 size_t
233 __bt_defpfx(a, b)
234 	const DBT *a, *b;
235 {
236 	register u_char *p1, *p2;
237 	register size_t cnt, len;
238 
239 	cnt = 1;
240 	len = MIN(a->size, b->size);
241 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
242 		if (*p1 != *p2)
243 			return (cnt);
244 
245 	/* a->size must be <= b->size, or they wouldn't be in this order. */
246 	return (a->size < b->size ? a->size + 1 : a->size);
247 }
248