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