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