2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
8 * Copyright (c) 1990, 1993, 1994, 1995, 1996
9 * Keith Bostic. All rights reserved.
12 * Copyright (c) 1990, 1993, 1994, 1995
13 * The Regents of the University of California. All rights reserved.
15 * This code is derived from software contributed to Berkeley by
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 static const char sccsid
[] = "@(#)bt_compare.c 10.14 (Sleepycat) 10/9/98";
53 #ifndef NO_SYSTEM_INCLUDES
54 #include <sys/types.h>
65 * Compare a key to a given record.
67 * PUBLIC: int __bam_cmp __P((DB *, const DBT *,
68 * PUBLIC: PAGE *, u_int32_t, int (*)(const DBT *, const DBT *)));
71 __bam_cmp(dbp
, dbt
, h
, indx
, func
)
76 int (*func
)__P((const DBT
*, const DBT
*));
86 * < 0 if dbt is < page record
87 * = 0 if dbt is = page record
88 * > 0 if dbt is > page record
91 * We do not clear the pg_dbt DBT even though it's likely to contain
92 * random bits. That should be okay, because the app's comparison
93 * routine had better not be looking at fields other than data/size.
94 * We don't clear it because we go through this path a lot and it's
97 if (TYPE(h
) == P_LBTREE
|| TYPE(h
) == P_DUPLICATE
) {
98 bk
= GET_BKEYDATA(h
, indx
);
99 if (B_TYPE(bk
->type
) == B_OVERFLOW
)
100 bo
= (BOVERFLOW
*)bk
;
102 pg_dbt
.data
= bk
->data
;
103 pg_dbt
.size
= bk
->len
;
104 return (func(dbt
, &pg_dbt
));
108 * The following code guarantees that the left-most key on an
109 * internal page at any level of the btree is less than any
110 * user specified key. This saves us from having to update the
111 * leftmost key on an internal page when the user inserts a new
112 * key in the tree smaller than anything we've seen before.
114 if (indx
== 0 && h
->prev_pgno
== PGNO_INVALID
)
117 bi
= GET_BINTERNAL(h
, indx
);
118 if (B_TYPE(bi
->type
) == B_OVERFLOW
)
119 bo
= (BOVERFLOW
*)(bi
->data
);
121 pg_dbt
.data
= bi
->data
;
122 pg_dbt
.size
= bi
->len
;
123 return (func(dbt
, &pg_dbt
));
131 * We ignore __db_moff() errors, because we have no way of returning
134 (void) __db_moff(dbp
,
135 dbt
, bo
->pgno
, bo
->tlen
, func
== __bam_defcmp
? NULL
: func
, &ret
);
141 * Default comparison routine.
143 * PUBLIC: int __bam_defcmp __P((const DBT *, const DBT *));
159 * If a size_t doesn't fit into a long, or if the difference between
160 * any two characters doesn't fit into an int, this routine can lose.
161 * What we need is a signed integral type that's guaranteed to be at
162 * least as large as a size_t, and there is no such thing.
164 len
= a
->size
> b
->size
? b
->size
: a
->size
;
165 for (p1
= a
->data
, p2
= b
->data
; len
--; ++p1
, ++p2
)
167 return ((long)*p1
- (long)*p2
);
168 return ((long)a
->size
- (long)b
->size
);
173 * Default prefix routine.
175 * PUBLIC: size_t __bam_defpfx __P((const DBT *, const DBT *));
185 len
= a
->size
> b
->size
? b
->size
: a
->size
;
186 for (p1
= a
->data
, p2
= b
->data
; len
--; ++p1
, ++p2
, ++cnt
)
191 * We know that a->size must be <= b->size, or they wouldn't be
194 return (a
->size
< b
->size
? a
->size
+ 1 : a
->size
);