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