xref: /freebsd/lib/libc/db/btree/bt_utils.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)bt_utils.c	8.8 (Berkeley) 7/20/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/param.h>
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include <db.h>
45 #include "btree.h"
46 
47 /*
48  * __bt_ret --
49  *	Build return key/data pair.
50  *
51  * Parameters:
52  *	t:	tree
53  *	e:	key/data pair to be returned
54  *	key:	user's key structure (NULL if not to be filled in)
55  *	rkey:	memory area to hold key
56  *	data:	user's data structure (NULL if not to be filled in)
57  *	rdata:	memory area to hold data
58  *       copy:	always copy the key/data item
59  *
60  * Returns:
61  *	RET_SUCCESS, RET_ERROR.
62  */
63 int
64 __bt_ret(BTREE *t, EPG *e, DBT *key, DBT *rkey, DBT *data, DBT *rdata, int copy)
65 {
66 	BLEAF *bl;
67 	void *p;
68 
69 	bl = GETBLEAF(e->page, e->index);
70 
71 	/*
72 	 * We must copy big keys/data to make them contigous.  Otherwise,
73 	 * leave the page pinned and don't copy unless the user specified
74 	 * concurrent access.
75 	 */
76 	if (key == NULL)
77 		goto dataonly;
78 
79 	if (bl->flags & P_BIGKEY) {
80 		if (__ovfl_get(t, bl->bytes,
81 		    &key->size, &rkey->data, &rkey->size))
82 			return (RET_ERROR);
83 		key->data = rkey->data;
84 	} else if (copy || F_ISSET(t, B_DB_LOCK)) {
85 		if (bl->ksize > rkey->size) {
86 			p = realloc(rkey->data, bl->ksize);
87 			if (p == NULL)
88 				return (RET_ERROR);
89 			rkey->data = p;
90 			rkey->size = bl->ksize;
91 		}
92 		memmove(rkey->data, bl->bytes, bl->ksize);
93 		key->size = bl->ksize;
94 		key->data = rkey->data;
95 	} else {
96 		key->size = bl->ksize;
97 		key->data = bl->bytes;
98 	}
99 
100 dataonly:
101 	if (data == NULL)
102 		return (RET_SUCCESS);
103 
104 	if (bl->flags & P_BIGDATA) {
105 		if (__ovfl_get(t, bl->bytes + bl->ksize,
106 		    &data->size, &rdata->data, &rdata->size))
107 			return (RET_ERROR);
108 		data->data = rdata->data;
109 	} else if (copy || F_ISSET(t, B_DB_LOCK)) {
110 		/* Use +1 in case the first record retrieved is 0 length. */
111 		if (bl->dsize + 1 > rdata->size) {
112 			p = realloc(rdata->data, bl->dsize + 1);
113 			if (p == NULL)
114 				return (RET_ERROR);
115 			rdata->data = p;
116 			rdata->size = bl->dsize + 1;
117 		}
118 		memmove(rdata->data, bl->bytes + bl->ksize, bl->dsize);
119 		data->size = bl->dsize;
120 		data->data = rdata->data;
121 	} else {
122 		data->size = bl->dsize;
123 		data->data = bl->bytes + bl->ksize;
124 	}
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(BTREE *t, const DBT *k1, 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_rdata.data, &t->bt_rdata.size))
184 			return (RET_ERROR);
185 		k2.data = t->bt_rdata.data;
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(const DBT *a, const DBT *b)
204 {
205 	size_t len;
206 	u_char *p1, *p2;
207 
208 	/*
209 	 * XXX
210 	 * If a size_t doesn't fit in an int, this routine can lose.
211 	 * What we need is an integral type which is guaranteed to be
212 	 * larger than a size_t, and there is no such thing.
213 	 */
214 	len = MIN(a->size, b->size);
215 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
216 		if (*p1 != *p2)
217 			return ((int)*p1 - (int)*p2);
218 	return ((int)a->size - (int)b->size);
219 }
220 
221 /*
222  * __BT_DEFPFX -- Default prefix routine.
223  *
224  * Parameters:
225  *	a:	DBT #1
226  *	b:	DBT #2
227  *
228  * Returns:
229  *	Number of bytes needed to distinguish b from a.
230  */
231 size_t
232 __bt_defpfx(const DBT *a, const DBT *b)
233 {
234 	u_char *p1, *p2;
235 	size_t cnt, len;
236 
237 	cnt = 1;
238 	len = MIN(a->size, b->size);
239 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
240 		if (*p1 != *p2)
241 			return (cnt);
242 
243 	/* a->size must be <= b->size, or they wouldn't be in this order. */
244 	return (a->size < b->size ? a->size + 1 : a->size);
245 }
246