1 /* $NetBSD: bt_utils.c,v 1.9 2003/08/07 16:42:41 agc 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. 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. 21 * 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 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)bt_utils.c 8.8 (Berkeley) 7/20/94"; 39 #else 40 __RCSID("$NetBSD: bt_utils.c,v 1.9 2003/08/07 16:42:41 agc Exp $"); 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 #include <sys/param.h> 45 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 #include <db.h> 51 #include "btree.h" 52 53 /* 54 * __bt_ret -- 55 * Build return key/data pair. 56 * 57 * Parameters: 58 * t: tree 59 * e: key/data pair to be returned 60 * key: user's key structure (NULL if not to be filled in) 61 * rkey: memory area to hold key 62 * data: user's data structure (NULL if not to be filled in) 63 * rdata: memory area to hold data 64 * copy: always copy the key/data item 65 * 66 * Returns: 67 * RET_SUCCESS, RET_ERROR. 68 */ 69 int 70 __bt_ret(t, e, key, rkey, data, rdata, copy) 71 BTREE *t; 72 EPG *e; 73 DBT *key, *rkey, *data, *rdata; 74 int copy; 75 { 76 BLEAF *bl; 77 void *p; 78 79 bl = GETBLEAF(e->page, e->index); 80 81 /* 82 * We must copy big keys/data to make them contigous. Otherwise, 83 * leave the page pinned and don't copy unless the user specified 84 * concurrent access. 85 */ 86 if (key == NULL) 87 goto dataonly; 88 89 if (bl->flags & P_BIGKEY) { 90 if (__ovfl_get(t, bl->bytes, 91 &key->size, &rkey->data, &rkey->size)) 92 return (RET_ERROR); 93 key->data = rkey->data; 94 } else if (copy || F_ISSET(t, B_DB_LOCK)) { 95 if (bl->ksize > rkey->size) { 96 p = (void *)(rkey->data == NULL ? 97 malloc(bl->ksize) : realloc(rkey->data, bl->ksize)); 98 if (p == NULL) 99 return (RET_ERROR); 100 rkey->data = p; 101 rkey->size = bl->ksize; 102 } 103 memmove(rkey->data, bl->bytes, bl->ksize); 104 key->size = bl->ksize; 105 key->data = rkey->data; 106 } else { 107 key->size = bl->ksize; 108 key->data = bl->bytes; 109 } 110 111 dataonly: 112 if (data == NULL) 113 return (RET_SUCCESS); 114 115 if (bl->flags & P_BIGDATA) { 116 if (__ovfl_get(t, bl->bytes + bl->ksize, 117 &data->size, &rdata->data, &rdata->size)) 118 return (RET_ERROR); 119 data->data = rdata->data; 120 } else if (copy || F_ISSET(t, B_DB_LOCK)) { 121 /* Use +1 in case the first record retrieved is 0 length. */ 122 if (bl->dsize + 1 > rdata->size) { 123 p = (void *)(rdata->data == NULL ? 124 malloc(bl->dsize + 1) : 125 realloc(rdata->data, bl->dsize + 1)); 126 if (p == NULL) 127 return (RET_ERROR); 128 rdata->data = p; 129 rdata->size = bl->dsize + 1; 130 } 131 memmove(rdata->data, bl->bytes + bl->ksize, bl->dsize); 132 data->size = bl->dsize; 133 data->data = rdata->data; 134 } else { 135 data->size = bl->dsize; 136 data->data = bl->bytes + bl->ksize; 137 } 138 139 return (RET_SUCCESS); 140 } 141 142 /* 143 * __BT_CMP -- Compare a key to a given record. 144 * 145 * Parameters: 146 * t: tree 147 * k1: DBT pointer of first arg to comparison 148 * e: pointer to EPG for comparison 149 * 150 * Returns: 151 * < 0 if k1 is < record 152 * = 0 if k1 is = record 153 * > 0 if k1 is > record 154 */ 155 int 156 __bt_cmp(t, k1, e) 157 BTREE *t; 158 const DBT *k1; 159 EPG *e; 160 { 161 BINTERNAL *bi; 162 BLEAF *bl; 163 DBT k2; 164 PAGE *h; 165 void *bigkey; 166 167 /* 168 * The left-most key on internal pages, at any level of the tree, is 169 * guaranteed by the following code to be less than any user key. 170 * This saves us from having to update the leftmost key on an internal 171 * page when the user inserts a new key in the tree smaller than 172 * anything we've yet seen. 173 */ 174 h = e->page; 175 if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF)) 176 return (1); 177 178 bigkey = NULL; 179 if (h->flags & P_BLEAF) { 180 bl = GETBLEAF(h, e->index); 181 if (bl->flags & P_BIGKEY) 182 bigkey = bl->bytes; 183 else { 184 k2.data = bl->bytes; 185 k2.size = bl->ksize; 186 } 187 } else { 188 bi = GETBINTERNAL(h, e->index); 189 if (bi->flags & P_BIGKEY) 190 bigkey = bi->bytes; 191 else { 192 k2.data = bi->bytes; 193 k2.size = bi->ksize; 194 } 195 } 196 197 if (bigkey) { 198 if (__ovfl_get(t, bigkey, 199 &k2.size, &t->bt_rdata.data, &t->bt_rdata.size)) 200 return (RET_ERROR); 201 k2.data = t->bt_rdata.data; 202 } 203 return ((*t->bt_cmp)(k1, &k2)); 204 } 205 206 /* 207 * __BT_DEFCMP -- Default comparison routine. 208 * 209 * Parameters: 210 * a: DBT #1 211 * b: DBT #2 212 * 213 * Returns: 214 * < 0 if a is < b 215 * = 0 if a is = b 216 * > 0 if a is > b 217 */ 218 int 219 __bt_defcmp(a, b) 220 const DBT *a, *b; 221 { 222 register size_t len; 223 register u_char *p1, *p2; 224 225 /* 226 * XXX 227 * If a size_t doesn't fit in an int, this routine can lose. 228 * What we need is a integral type which is guaranteed to be 229 * larger than a size_t, and there is no such thing. 230 */ 231 len = MIN(a->size, b->size); 232 for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2) 233 if (*p1 != *p2) 234 return ((int)*p1 - (int)*p2); 235 return ((int)a->size - (int)b->size); 236 } 237 238 /* 239 * __BT_DEFPFX -- Default prefix routine. 240 * 241 * Parameters: 242 * a: DBT #1 243 * b: DBT #2 244 * 245 * Returns: 246 * Number of bytes needed to distinguish b from a. 247 */ 248 size_t 249 __bt_defpfx(a, b) 250 const DBT *a, *b; 251 { 252 register u_char *p1, *p2; 253 register size_t cnt, len; 254 255 cnt = 1; 256 len = MIN(a->size, b->size); 257 for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt) 258 if (*p1 != *p2) 259 return (cnt); 260 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); 263 } 264