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