bt_utils.c revision 1.13.6.2 1 1.13.6.2 joerg /* $NetBSD: bt_utils.c,v 1.13.6.2 2008/09/10 17:52:36 joerg Exp $ */
2 1.13.6.2 joerg
3 1.13.6.2 joerg /*-
4 1.13.6.2 joerg * Copyright (c) 1990, 1993, 1994
5 1.13.6.2 joerg * The Regents of the University of California. All rights reserved.
6 1.13.6.2 joerg *
7 1.13.6.2 joerg * This code is derived from software contributed to Berkeley by
8 1.13.6.2 joerg * Mike Olson.
9 1.13.6.2 joerg *
10 1.13.6.2 joerg * Redistribution and use in source and binary forms, with or without
11 1.13.6.2 joerg * modification, are permitted provided that the following conditions
12 1.13.6.2 joerg * are met:
13 1.13.6.2 joerg * 1. Redistributions of source code must retain the above copyright
14 1.13.6.2 joerg * notice, this list of conditions and the following disclaimer.
15 1.13.6.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
16 1.13.6.2 joerg * notice, this list of conditions and the following disclaimer in the
17 1.13.6.2 joerg * documentation and/or other materials provided with the distribution.
18 1.13.6.2 joerg * 3. Neither the name of the University nor the names of its contributors
19 1.13.6.2 joerg * may be used to endorse or promote products derived from this software
20 1.13.6.2 joerg * without specific prior written permission.
21 1.13.6.2 joerg *
22 1.13.6.2 joerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.13.6.2 joerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.13.6.2 joerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.13.6.2 joerg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.13.6.2 joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.13.6.2 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.13.6.2 joerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.13.6.2 joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.13.6.2 joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.13.6.2 joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.13.6.2 joerg * SUCH DAMAGE.
33 1.13.6.2 joerg */
34 1.13.6.2 joerg
35 1.13.6.2 joerg #if HAVE_NBTOOL_CONFIG_H
36 1.13.6.2 joerg #include "nbtool_config.h"
37 1.13.6.2 joerg #endif
38 1.13.6.2 joerg
39 1.13.6.2 joerg #include <sys/cdefs.h>
40 1.13.6.2 joerg __RCSID("$NetBSD: bt_utils.c,v 1.13.6.2 2008/09/10 17:52:36 joerg Exp $");
41 1.13.6.2 joerg
42 1.13.6.2 joerg #include <sys/param.h>
43 1.13.6.2 joerg
44 1.13.6.2 joerg #include <assert.h>
45 1.13.6.2 joerg #include <stdio.h>
46 1.13.6.2 joerg #include <stdlib.h>
47 1.13.6.2 joerg #include <string.h>
48 1.13.6.2 joerg
49 1.13.6.2 joerg #include <db.h>
50 1.13.6.2 joerg #include "btree.h"
51 1.13.6.2 joerg
52 1.13.6.2 joerg /*
53 1.13.6.2 joerg * __bt_ret --
54 1.13.6.2 joerg * Build return key/data pair.
55 1.13.6.2 joerg *
56 1.13.6.2 joerg * Parameters:
57 1.13.6.2 joerg * t: tree
58 1.13.6.2 joerg * e: key/data pair to be returned
59 1.13.6.2 joerg * key: user's key structure (NULL if not to be filled in)
60 1.13.6.2 joerg * rkey: memory area to hold key
61 1.13.6.2 joerg * data: user's data structure (NULL if not to be filled in)
62 1.13.6.2 joerg * rdata: memory area to hold data
63 1.13.6.2 joerg * copy: always copy the key/data item
64 1.13.6.2 joerg *
65 1.13.6.2 joerg * Returns:
66 1.13.6.2 joerg * RET_SUCCESS, RET_ERROR.
67 1.13.6.2 joerg */
68 1.13.6.2 joerg int
69 1.13.6.2 joerg __bt_ret(BTREE *t, EPG *e, DBT *key, DBT *rkey, DBT *data, DBT *rdata, int copy)
70 1.13.6.2 joerg {
71 1.13.6.2 joerg BLEAF *bl;
72 1.13.6.2 joerg void *p;
73 1.13.6.2 joerg
74 1.13.6.2 joerg bl = GETBLEAF(e->page, e->index);
75 1.13.6.2 joerg
76 1.13.6.2 joerg /*
77 1.13.6.2 joerg * We must copy big keys/data to make them contigous. Otherwise,
78 1.13.6.2 joerg * leave the page pinned and don't copy unless the user specified
79 1.13.6.2 joerg * concurrent access.
80 1.13.6.2 joerg */
81 1.13.6.2 joerg if (key == NULL)
82 1.13.6.2 joerg goto dataonly;
83 1.13.6.2 joerg
84 1.13.6.2 joerg if (bl->flags & P_BIGKEY) {
85 1.13.6.2 joerg if (__ovfl_get(t, bl->bytes,
86 1.13.6.2 joerg &key->size, &rkey->data, &rkey->size))
87 1.13.6.2 joerg return (RET_ERROR);
88 1.13.6.2 joerg key->data = rkey->data;
89 1.13.6.2 joerg } else if (copy || F_ISSET(t, B_DB_LOCK)) {
90 1.13.6.2 joerg if (bl->ksize > rkey->size) {
91 1.13.6.2 joerg p = (void *)(rkey->data == NULL ?
92 1.13.6.2 joerg malloc(bl->ksize) : realloc(rkey->data, bl->ksize));
93 1.13.6.2 joerg if (p == NULL)
94 1.13.6.2 joerg return (RET_ERROR);
95 1.13.6.2 joerg rkey->data = p;
96 1.13.6.2 joerg rkey->size = bl->ksize;
97 1.13.6.2 joerg }
98 1.13.6.2 joerg memmove(rkey->data, bl->bytes, bl->ksize);
99 1.13.6.2 joerg key->size = bl->ksize;
100 1.13.6.2 joerg key->data = rkey->data;
101 1.13.6.2 joerg } else {
102 1.13.6.2 joerg key->size = bl->ksize;
103 1.13.6.2 joerg key->data = bl->bytes;
104 1.13.6.2 joerg }
105 1.13.6.2 joerg
106 1.13.6.2 joerg dataonly:
107 1.13.6.2 joerg if (data == NULL)
108 1.13.6.2 joerg return (RET_SUCCESS);
109 1.13.6.2 joerg
110 1.13.6.2 joerg if (bl->flags & P_BIGDATA) {
111 1.13.6.2 joerg if (__ovfl_get(t, bl->bytes + bl->ksize,
112 1.13.6.2 joerg &data->size, &rdata->data, &rdata->size))
113 1.13.6.2 joerg return (RET_ERROR);
114 1.13.6.2 joerg data->data = rdata->data;
115 1.13.6.2 joerg } else if (copy || F_ISSET(t, B_DB_LOCK)) {
116 1.13.6.2 joerg /* Use +1 in case the first record retrieved is 0 length. */
117 1.13.6.2 joerg if (bl->dsize + 1 > rdata->size) {
118 1.13.6.2 joerg p = (void *)(rdata->data == NULL ?
119 1.13.6.2 joerg malloc(bl->dsize + 1) :
120 1.13.6.2 joerg realloc(rdata->data, bl->dsize + 1));
121 1.13.6.2 joerg if (p == NULL)
122 1.13.6.2 joerg return (RET_ERROR);
123 1.13.6.2 joerg rdata->data = p;
124 1.13.6.2 joerg rdata->size = bl->dsize + 1;
125 1.13.6.2 joerg }
126 1.13.6.2 joerg memmove(rdata->data, bl->bytes + bl->ksize, bl->dsize);
127 1.13.6.2 joerg data->size = bl->dsize;
128 1.13.6.2 joerg data->data = rdata->data;
129 1.13.6.2 joerg } else {
130 1.13.6.2 joerg data->size = bl->dsize;
131 1.13.6.2 joerg data->data = bl->bytes + bl->ksize;
132 1.13.6.2 joerg }
133 1.13.6.2 joerg
134 1.13.6.2 joerg return (RET_SUCCESS);
135 1.13.6.2 joerg }
136 1.13.6.2 joerg
137 1.13.6.2 joerg /*
138 1.13.6.2 joerg * __BT_CMP -- Compare a key to a given record.
139 1.13.6.2 joerg *
140 1.13.6.2 joerg * Parameters:
141 1.13.6.2 joerg * t: tree
142 1.13.6.2 joerg * k1: DBT pointer of first arg to comparison
143 1.13.6.2 joerg * e: pointer to EPG for comparison
144 1.13.6.2 joerg *
145 1.13.6.2 joerg * Returns:
146 1.13.6.2 joerg * < 0 if k1 is < record
147 1.13.6.2 joerg * = 0 if k1 is = record
148 1.13.6.2 joerg * > 0 if k1 is > record
149 1.13.6.2 joerg */
150 1.13.6.2 joerg int
151 1.13.6.2 joerg __bt_cmp(BTREE *t, const DBT *k1, EPG *e)
152 1.13.6.2 joerg {
153 1.13.6.2 joerg BINTERNAL *bi;
154 1.13.6.2 joerg BLEAF *bl;
155 1.13.6.2 joerg DBT k2;
156 1.13.6.2 joerg PAGE *h;
157 1.13.6.2 joerg void *bigkey;
158 1.13.6.2 joerg
159 1.13.6.2 joerg /*
160 1.13.6.2 joerg * The left-most key on internal pages, at any level of the tree, is
161 1.13.6.2 joerg * guaranteed by the following code to be less than any user key.
162 1.13.6.2 joerg * This saves us from having to update the leftmost key on an internal
163 1.13.6.2 joerg * page when the user inserts a new key in the tree smaller than
164 1.13.6.2 joerg * anything we've yet seen.
165 1.13.6.2 joerg */
166 1.13.6.2 joerg h = e->page;
167 1.13.6.2 joerg if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
168 1.13.6.2 joerg return (1);
169 1.13.6.2 joerg
170 1.13.6.2 joerg bigkey = NULL;
171 1.13.6.2 joerg if (h->flags & P_BLEAF) {
172 1.13.6.2 joerg bl = GETBLEAF(h, e->index);
173 1.13.6.2 joerg if (bl->flags & P_BIGKEY)
174 1.13.6.2 joerg bigkey = bl->bytes;
175 1.13.6.2 joerg else {
176 1.13.6.2 joerg k2.data = bl->bytes;
177 1.13.6.2 joerg k2.size = bl->ksize;
178 1.13.6.2 joerg }
179 1.13.6.2 joerg } else {
180 1.13.6.2 joerg bi = GETBINTERNAL(h, e->index);
181 1.13.6.2 joerg if (bi->flags & P_BIGKEY)
182 1.13.6.2 joerg bigkey = bi->bytes;
183 1.13.6.2 joerg else {
184 1.13.6.2 joerg k2.data = bi->bytes;
185 1.13.6.2 joerg k2.size = bi->ksize;
186 1.13.6.2 joerg }
187 1.13.6.2 joerg }
188 1.13.6.2 joerg
189 1.13.6.2 joerg if (bigkey) {
190 1.13.6.2 joerg if (__ovfl_get(t, bigkey,
191 1.13.6.2 joerg &k2.size, &t->bt_rdata.data, &t->bt_rdata.size))
192 1.13.6.2 joerg return (RET_ERROR);
193 1.13.6.2 joerg k2.data = t->bt_rdata.data;
194 1.13.6.2 joerg }
195 1.13.6.2 joerg return ((*t->bt_cmp)(k1, &k2));
196 1.13.6.2 joerg }
197 1.13.6.2 joerg
198 1.13.6.2 joerg /*
199 1.13.6.2 joerg * __BT_DEFCMP -- Default comparison routine.
200 1.13.6.2 joerg *
201 1.13.6.2 joerg * Parameters:
202 1.13.6.2 joerg * a: DBT #1
203 1.13.6.2 joerg * b: DBT #2
204 1.13.6.2 joerg *
205 1.13.6.2 joerg * Returns:
206 1.13.6.2 joerg * < 0 if a is < b
207 1.13.6.2 joerg * = 0 if a is = b
208 1.13.6.2 joerg * > 0 if a is > b
209 1.13.6.2 joerg */
210 1.13.6.2 joerg int
211 1.13.6.2 joerg __bt_defcmp(const DBT *a, const DBT *b)
212 1.13.6.2 joerg {
213 1.13.6.2 joerg size_t len;
214 1.13.6.2 joerg uint8_t *p1, *p2;
215 1.13.6.2 joerg
216 1.13.6.2 joerg /*
217 1.13.6.2 joerg * XXX
218 1.13.6.2 joerg * If a size_t doesn't fit in an int, this routine can lose.
219 1.13.6.2 joerg * What we need is a integral type which is guaranteed to be
220 1.13.6.2 joerg * larger than a size_t, and there is no such thing.
221 1.13.6.2 joerg */
222 1.13.6.2 joerg len = MIN(a->size, b->size);
223 1.13.6.2 joerg for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
224 1.13.6.2 joerg if (*p1 != *p2)
225 1.13.6.2 joerg return ((int)*p1 - (int)*p2);
226 1.13.6.2 joerg return ((int)a->size - (int)b->size);
227 1.13.6.2 joerg }
228 1.13.6.2 joerg
229 1.13.6.2 joerg /*
230 1.13.6.2 joerg * __BT_DEFPFX -- Default prefix routine.
231 1.13.6.2 joerg *
232 1.13.6.2 joerg * Parameters:
233 1.13.6.2 joerg * a: DBT #1
234 1.13.6.2 joerg * b: DBT #2
235 1.13.6.2 joerg *
236 1.13.6.2 joerg * Returns:
237 1.13.6.2 joerg * Number of bytes needed to distinguish b from a.
238 1.13.6.2 joerg */
239 1.13.6.2 joerg size_t
240 1.13.6.2 joerg __bt_defpfx(const DBT *a, const DBT *b)
241 1.13.6.2 joerg {
242 1.13.6.2 joerg uint8_t *p1, *p2;
243 1.13.6.2 joerg size_t cnt, len;
244 1.13.6.2 joerg
245 1.13.6.2 joerg cnt = 1;
246 1.13.6.2 joerg len = MIN(a->size, b->size);
247 1.13.6.2 joerg for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
248 1.13.6.2 joerg if (*p1 != *p2)
249 1.13.6.2 joerg return (cnt);
250 1.13.6.2 joerg
251 1.13.6.2 joerg /* a->size must be <= b->size, or they wouldn't be in this order. */
252 1.13.6.2 joerg return (a->size < b->size ? a->size + 1 : a->size);
253 1.13.6.2 joerg }
254