xref: /netbsd-src/lib/libc/db/btree/bt_utils.c (revision 0b9f50897e9a9c6709320fafb4c3787fddcc0a45)
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 /* from: static char sccsid[] = "@(#)bt_utils.c	8.2 (Berkeley) 9/7/93"; */
39 static char *rcsid = "$Id: bt_utils.c,v 1.4 1993/09/09 02:41:32 cgd Exp $";
40 #endif /* LIBC_SCCS and not lint */
41 
42 #include <sys/param.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include <db.h>
49 #include "btree.h"
50 
51 /*
52  * __BT_RET -- Build return key/data pair as a result of search or scan.
53  *
54  * Parameters:
55  *	t:	tree
56  *	d:	LEAF to be returned to the user.
57  *	key:	user's key structure (NULL if not to be filled in)
58  *	data:	user's data structure
59  *
60  * Returns:
61  *	RET_SUCCESS, RET_ERROR.
62  */
63 int
64 __bt_ret(t, e, key, data)
65 	BTREE *t;
66 	EPG *e;
67 	DBT *key, *data;
68 {
69 	register BLEAF *bl;
70 	register void *p;
71 
72 	bl = GETBLEAF(e->page, e->index);
73 
74 	/*
75 	 * We always copy big keys/data to make them contigous.  Otherwise,
76 	 * we leave the page pinned and don't copy unless the user specified
77 	 * concurrent access.
78 	 */
79 	if (bl->flags & P_BIGDATA) {
80 		if (__ovfl_get(t, bl->bytes + bl->ksize,
81 		    &data->size, &t->bt_dbuf, &t->bt_dbufsz))
82 			return (RET_ERROR);
83 		data->data = t->bt_dbuf;
84 	} else if (ISSET(t, B_DB_LOCK)) {
85 		/* Use +1 in case the first record retrieved is 0 length. */
86 		if (bl->dsize + 1 > t->bt_dbufsz) {
87 			if ((p = 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 = realloc(t->bt_kbuf, bl->ksize)) == NULL)
111 				return (RET_ERROR);
112 			t->bt_kbuf = p;
113 			t->bt_kbufsz = bl->ksize;
114 		}
115 		memmove(t->bt_kbuf, bl->bytes, bl->ksize);
116 		key->size = bl->ksize;
117 		key->data = t->bt_kbuf;
118 	} else {
119 		key->size = bl->ksize;
120 		key->data = bl->bytes;
121 	}
122 	return (RET_SUCCESS);
123 }
124 
125 /*
126  * __BT_CMP -- Compare a key to a given record.
127  *
128  * Parameters:
129  *	t:	tree
130  *	k1:	DBT pointer of first arg to comparison
131  *	e:	pointer to EPG for comparison
132  *
133  * Returns:
134  *	< 0 if k1 is < record
135  *	= 0 if k1 is = record
136  *	> 0 if k1 is > record
137  */
138 int
139 __bt_cmp(t, k1, e)
140 	BTREE *t;
141 	const DBT *k1;
142 	EPG *e;
143 {
144 	BINTERNAL *bi;
145 	BLEAF *bl;
146 	DBT k2;
147 	PAGE *h;
148 	void *bigkey;
149 
150 	/*
151 	 * The left-most key on internal pages, at any level of the tree, is
152 	 * guaranteed by the following code to be less than any user key.
153 	 * This saves us from having to update the leftmost key on an internal
154 	 * page when the user inserts a new key in the tree smaller than
155 	 * anything we've yet seen.
156 	 */
157 	h = e->page;
158 	if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
159 		return (1);
160 
161 	bigkey = NULL;
162 	if (h->flags & P_BLEAF) {
163 		bl = GETBLEAF(h, e->index);
164 		if (bl->flags & P_BIGKEY)
165 			bigkey = bl->bytes;
166 		else {
167 			k2.data = bl->bytes;
168 			k2.size = bl->ksize;
169 		}
170 	} else {
171 		bi = GETBINTERNAL(h, e->index);
172 		if (bi->flags & P_BIGKEY)
173 			bigkey = bi->bytes;
174 		else {
175 			k2.data = bi->bytes;
176 			k2.size = bi->ksize;
177 		}
178 	}
179 
180 	if (bigkey) {
181 		if (__ovfl_get(t, bigkey,
182 		    &k2.size, &t->bt_dbuf, &t->bt_dbufsz))
183 			return (RET_ERROR);
184 		k2.data = t->bt_dbuf;
185 	}
186 	return ((*t->bt_cmp)(k1, &k2));
187 }
188 
189 /*
190  * __BT_DEFCMP -- Default comparison routine.
191  *
192  * Parameters:
193  *	a:	DBT #1
194  *	b: 	DBT #2
195  *
196  * Returns:
197  *	< 0 if a is < b
198  *	= 0 if a is = b
199  *	> 0 if a is > b
200  */
201 int
202 __bt_defcmp(a, b)
203 	const DBT *a, *b;
204 {
205 	register u_char *p1, *p2;
206 	register int diff, len;
207 
208 	len = MIN(a->size, b->size);
209 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
210 		if (diff = *p1 - *p2)
211 			return (diff);
212 	return (a->size - b->size);
213 }
214 
215 /*
216  * __BT_DEFPFX -- Default prefix routine.
217  *
218  * Parameters:
219  *	a:	DBT #1
220  *	b: 	DBT #2
221  *
222  * Returns:
223  *	Number of bytes needed to distinguish b from a.
224  */
225 int
226 __bt_defpfx(a, b)
227 	const DBT *a, *b;
228 {
229 	register u_char *p1, *p2;
230 	register int len;
231 	int cnt;
232 
233 	cnt = 1;
234 	len = MIN(a->size, b->size);
235 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
236 		if (*p1 != *p2)
237 			return (cnt);
238 
239 	/* a->size must be <= b->size, or they wouldn't be in this order. */
240 	return (a->size < b->size ? a->size + 1 : a->size);
241 }
242