2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: bt_utils.c /main/3 1996/06/11 17:13:20 cde-hal $ */
25 * Copyright (c) 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #if defined(LIBC_SCCS) && !defined(lint)
61 static char sccsid[] = "@(#)bt_utils.c 8.2 (Berkeley) 9/7/93";
62 #endif /* LIBC_SCCS and not lint */
64 #include <sys/param.h>
74 * __BT_RET -- Build return key/data pair as a result of search or scan.
78 * d: LEAF to be returned to the user.
79 * key: user's key structure (NULL if not to be filled in)
80 * data: user's data structure
83 * RET_SUCCESS, RET_ERROR.
86 __bt_ret(t, e, key, data)
94 bl = GETBLEAF(e->page, e->index);
97 * We always copy big keys/data to make them contigous. Otherwise,
98 * we leave the page pinned and don't copy unless the user specified
101 if (bl->flags & P_BIGDATA) {
102 if (__ovfl_get(t, bl->bytes + bl->ksize,
103 &data->size, &t->bt_dbuf, &t->bt_dbufsz))
105 data->data = t->bt_dbuf;
106 } else if (ISSET(t, B_DB_LOCK)) {
107 /* Use +1 in case the first record retrieved is 0 length. */
108 if (bl->dsize + 1 > t->bt_dbufsz) {
109 if ((p = __fix_realloc(t->bt_dbuf, bl->dsize + 1)) == NULL)
112 t->bt_dbufsz = bl->dsize + 1;
114 memmove(t->bt_dbuf, bl->bytes + bl->ksize, bl->dsize);
115 data->size = bl->dsize;
116 data->data = t->bt_dbuf;
118 data->size = bl->dsize;
119 data->data = bl->bytes + bl->ksize;
123 return (RET_SUCCESS);
125 if (bl->flags & P_BIGKEY) {
126 if (__ovfl_get(t, bl->bytes,
127 &key->size, &t->bt_kbuf, &t->bt_kbufsz))
129 key->data = t->bt_kbuf;
130 } else if (ISSET(t, B_DB_LOCK)) {
131 if (bl->ksize > t->bt_kbufsz) {
132 if ((p = __fix_realloc(t->bt_kbuf, bl->ksize)) == NULL)
135 t->bt_kbufsz = bl->ksize;
137 memmove(t->bt_kbuf, bl->bytes, bl->ksize);
138 key->size = bl->ksize;
139 key->data = t->bt_kbuf;
141 key->size = bl->ksize;
142 key->data = bl->bytes;
144 return (RET_SUCCESS);
148 * __BT_CMP -- Compare a key to a given record.
152 * k1: DBT pointer of first arg to comparison
153 * e: pointer to EPG for comparison
156 * < 0 if k1 is < record
157 * = 0 if k1 is = record
158 * > 0 if k1 is > record
173 * The left-most key on internal pages, at any level of the tree, is
174 * guaranteed by the following code to be less than any user key.
175 * This saves us from having to update the leftmost key on an internal
176 * page when the user inserts a new key in the tree smaller than
177 * anything we've yet seen.
180 if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
184 if (h->flags & P_BLEAF) {
185 bl = GETBLEAF(h, e->index);
186 if (bl->flags & P_BIGKEY)
193 bi = GETBINTERNAL(h, e->index);
194 if (bi->flags & P_BIGKEY)
203 if (__ovfl_get(t, bigkey,
204 &k2.size, &t->bt_dbuf, &t->bt_dbufsz))
206 k2.data = t->bt_dbuf;
208 return ((*t->bt_cmp)(k1, &k2));
212 * __BT_DEFCMP -- Default comparison routine.
227 register u_char *p1, *p2;
228 register int diff, len;
230 len = MIN(a->size, b->size);
231 for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
232 if (diff = *p1 - *p2)
234 return (a->size - b->size);
238 * __BT_DEFPFX -- Default prefix routine.
245 * Number of bytes needed to distinguish b from a.
251 register u_char *p1, *p2;
256 len = MIN(a->size, b->size);
257 for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
261 /* a->size must be <= b->size, or they wouldn't be in this order. */
262 return (a->size < b->size ? a->size + 1 : a->size);