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