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