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