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