1 /* $NetBSD: bt_utils.c,v 1.13 2008/09/10 17:52:35 joerg Exp $ */
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: bt_utils.c,v 1.13 2008/09/10 17:52:35 joerg Exp $");
42 #include <sys/param.h>
54 * Build return key/data pair.
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
66 * RET_SUCCESS, RET_ERROR.
69 __bt_ret(BTREE
*t
, EPG
*e
, DBT
*key
, DBT
*rkey
, DBT
*data
, DBT
*rdata
, int copy
)
74 bl
= GETBLEAF(e
->page
, e
->index
);
77 * We must copy big keys/data to make them contigous. Otherwise,
78 * leave the page pinned and don't copy unless the user specified
84 if (bl
->flags
& P_BIGKEY
) {
85 if (__ovfl_get(t
, bl
->bytes
,
86 &key
->size
, &rkey
->data
, &rkey
->size
))
88 key
->data
= rkey
->data
;
89 } else if (copy
|| F_ISSET(t
, B_DB_LOCK
)) {
90 if (bl
->ksize
> rkey
->size
) {
91 p
= (void *)(rkey
->data
== NULL
?
92 malloc(bl
->ksize
) : realloc(rkey
->data
, bl
->ksize
));
96 rkey
->size
= bl
->ksize
;
98 memmove(rkey
->data
, bl
->bytes
, bl
->ksize
);
99 key
->size
= bl
->ksize
;
100 key
->data
= rkey
->data
;
102 key
->size
= bl
->ksize
;
103 key
->data
= bl
->bytes
;
108 return (RET_SUCCESS
);
110 if (bl
->flags
& P_BIGDATA
) {
111 if (__ovfl_get(t
, bl
->bytes
+ bl
->ksize
,
112 &data
->size
, &rdata
->data
, &rdata
->size
))
114 data
->data
= rdata
->data
;
115 } else if (copy
|| F_ISSET(t
, B_DB_LOCK
)) {
116 /* Use +1 in case the first record retrieved is 0 length. */
117 if (bl
->dsize
+ 1 > rdata
->size
) {
118 p
= (void *)(rdata
->data
== NULL
?
119 malloc(bl
->dsize
+ 1) :
120 realloc(rdata
->data
, bl
->dsize
+ 1));
124 rdata
->size
= bl
->dsize
+ 1;
126 memmove(rdata
->data
, bl
->bytes
+ bl
->ksize
, bl
->dsize
);
127 data
->size
= bl
->dsize
;
128 data
->data
= rdata
->data
;
130 data
->size
= bl
->dsize
;
131 data
->data
= bl
->bytes
+ bl
->ksize
;
134 return (RET_SUCCESS
);
138 * __BT_CMP -- Compare a key to a given record.
142 * k1: DBT pointer of first arg to comparison
143 * e: pointer to EPG for comparison
146 * < 0 if k1 is < record
147 * = 0 if k1 is = record
148 * > 0 if k1 is > record
151 __bt_cmp(BTREE
*t
, const DBT
*k1
, EPG
*e
)
160 * The left-most key on internal pages, at any level of the tree, is
161 * guaranteed by the following code to be less than any user key.
162 * This saves us from having to update the leftmost key on an internal
163 * page when the user inserts a new key in the tree smaller than
164 * anything we've yet seen.
167 if (e
->index
== 0 && h
->prevpg
== P_INVALID
&& !(h
->flags
& P_BLEAF
))
171 if (h
->flags
& P_BLEAF
) {
172 bl
= GETBLEAF(h
, e
->index
);
173 if (bl
->flags
& P_BIGKEY
)
180 bi
= GETBINTERNAL(h
, e
->index
);
181 if (bi
->flags
& P_BIGKEY
)
190 if (__ovfl_get(t
, bigkey
,
191 &k2
.size
, &t
->bt_rdata
.data
, &t
->bt_rdata
.size
))
193 k2
.data
= t
->bt_rdata
.data
;
195 return ((*t
->bt_cmp
)(k1
, &k2
));
199 * __BT_DEFCMP -- Default comparison routine.
211 __bt_defcmp(const DBT
*a
, const DBT
*b
)
218 * If a size_t doesn't fit in an int, this routine can lose.
219 * What we need is a integral type which is guaranteed to be
220 * larger than a size_t, and there is no such thing.
222 len
= MIN(a
->size
, b
->size
);
223 for (p1
= a
->data
, p2
= b
->data
; len
--; ++p1
, ++p2
)
225 return ((int)*p1
- (int)*p2
);
226 return ((int)a
->size
- (int)b
->size
);
230 * __BT_DEFPFX -- Default prefix routine.
237 * Number of bytes needed to distinguish b from a.
240 __bt_defpfx(const DBT
*a
, const DBT
*b
)
246 len
= MIN(a
->size
, b
->size
);
247 for (p1
= a
->data
, p2
= b
->data
; len
--; ++p1
, ++p2
, ++cnt
)
251 /* a->size must be <= b->size, or they wouldn't be in this order. */
252 return (a
->size
< b
->size
? a
->size
+ 1 : a
->size
);