]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_utils.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
69 * __BT_RET -- Build return key/data pair as a result of search or scan.
73 * d: LEAF to be returned to the user.
74 * key: user's key structure (NULL if not to be filled in)
75 * data: user's data structure
78 * RET_SUCCESS, RET_ERROR.
81 __bt_ret(t
, e
, key
, data
)
89 bl
= GETBLEAF(e
->page
, e
->index
);
92 * We always copy big keys/data to make them contigous. Otherwise,
93 * we leave the page pinned and don't copy unless the user specified
96 if (bl
->flags
& P_BIGDATA
) {
97 if (__ovfl_get(t
, bl
->bytes
+ bl
->ksize
,
98 &data
->size
, &t
->bt_dbuf
, &t
->bt_dbufsz
))
100 data
->data
= t
->bt_dbuf
;
101 } else if (ISSET(t
, B_DB_LOCK
)) {
102 /* Use +1 in case the first record retrieved is 0 length. */
103 if (bl
->dsize
+ 1 > t
->bt_dbufsz
) {
105 (void *)realloc(t
->bt_dbuf
, bl
->dsize
+ 1)) == NULL
)
108 t
->bt_dbufsz
= bl
->dsize
+ 1;
110 memmove(t
->bt_dbuf
, bl
->bytes
+ bl
->ksize
, bl
->dsize
);
111 data
->size
= bl
->dsize
;
112 data
->data
= t
->bt_dbuf
;
114 data
->size
= bl
->dsize
;
115 data
->data
= bl
->bytes
+ bl
->ksize
;
119 return (RET_SUCCESS
);
121 if (bl
->flags
& P_BIGKEY
) {
122 if (__ovfl_get(t
, bl
->bytes
,
123 &key
->size
, &t
->bt_kbuf
, &t
->bt_kbufsz
))
125 key
->data
= t
->bt_kbuf
;
126 } else if (ISSET(t
, B_DB_LOCK
)) {
127 if (bl
->ksize
> t
->bt_kbufsz
) {
129 (void *)realloc(t
->bt_kbuf
, bl
->ksize
)) == NULL
)
132 t
->bt_kbufsz
= bl
->ksize
;
134 memmove(t
->bt_kbuf
, bl
->bytes
, bl
->ksize
);
135 key
->size
= bl
->ksize
;
136 key
->data
= t
->bt_kbuf
;
138 key
->size
= bl
->ksize
;
139 key
->data
= bl
->bytes
;
141 return (RET_SUCCESS
);
145 * __BT_CMP -- Compare a key to a given record.
149 * k1: DBT pointer of first arg to comparison
150 * e: pointer to EPG for comparison
153 * < 0 if k1 is < record
154 * = 0 if k1 is = record
155 * > 0 if k1 is > record
170 * The left-most key on internal pages, at any level of the tree, is
171 * guaranteed by the following code to be less than any user key.
172 * This saves us from having to update the leftmost key on an internal
173 * page when the user inserts a new key in the tree smaller than
174 * anything we've yet seen.
177 if (e
->index
== 0 && h
->prevpg
== P_INVALID
&& !(h
->flags
& P_BLEAF
))
181 if (h
->flags
& P_BLEAF
) {
182 bl
= GETBLEAF(h
, e
->index
);
183 if (bl
->flags
& P_BIGKEY
)
190 bi
= GETBINTERNAL(h
, e
->index
);
191 if (bi
->flags
& P_BIGKEY
)
200 if (__ovfl_get(t
, bigkey
,
201 &k2
.size
, &t
->bt_dbuf
, &t
->bt_dbufsz
))
203 k2
.data
= t
->bt_dbuf
;
205 return ((*t
->bt_cmp
)(k1
, &k2
));
209 * __BT_DEFCMP -- Default comparison routine.
225 register u_char
*p1
, *p2
;
229 * If a size_t doesn't fit in an int, this routine can lose.
230 * What we need is a integral type which is guaranteed to be
231 * larger than a size_t, and there is no such thing.
233 len
= MIN(a
->size
, b
->size
);
234 for (p1
= a
->data
, p2
= b
->data
; len
--; ++p1
, ++p2
)
236 return ((int)*p1
- (int)*p2
);
237 return ((int)a
->size
- (int)b
->size
);
241 * __BT_DEFPFX -- Default prefix routine.
248 * Number of bytes needed to distinguish b from a.
254 register u_char
*p1
, *p2
;
255 register size_t cnt
, len
;
258 len
= MIN(a
->size
, b
->size
);
259 for (p1
= a
->data
, p2
= b
->data
; len
--; ++p1
, ++p2
, ++cnt
)
263 /* a->size must be <= b->size, or they wouldn't be in this order. */
264 return (a
->size
< b
->size
? a
->size
+ 1 : a
->size
);