1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Olson. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)bt_utils.c 5.5 (Berkeley) 09/12/91"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/param.h> 16 #include <db.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 #include "btree.h" 21 22 /* 23 * __BT_RET -- Build return key/data pair as a result of search or scan. 24 * 25 * Parameters: 26 * t: tree 27 * d: LEAF to be returned to the user. 28 * key: user's key structure (NULL if not to be filled in) 29 * data: user's data structure 30 * 31 * Returns: 32 * RET_SUCCESS, RET_ERROR. 33 */ 34 int 35 __bt_ret(t, e, key, data) 36 BTREE *t; 37 EPG *e; 38 DBT *key, *data; 39 { 40 register BLEAF *bl; 41 register void *p; 42 43 bl = GETBLEAF(e->page, e->index); 44 45 if (bl->flags & P_BIGDATA) { 46 if (__ovfl_get(t, bl->bytes + bl->ksize, 47 &data->size, &t->bt_dbuf, &t->bt_dbufsz)) 48 return (RET_ERROR); 49 } else { 50 if (bl->dsize > t->bt_dbufsz) { 51 if ((p = realloc(t->bt_dbuf, bl->dsize)) == NULL) 52 return (RET_ERROR); 53 t->bt_dbuf = p; 54 t->bt_dbufsz = bl->dsize; 55 } 56 bcopy(bl->bytes + bl->ksize, t->bt_dbuf, t->bt_dbufsz); 57 data->size = bl->dsize; 58 } 59 data->data = t->bt_dbuf; 60 61 if (key == NULL) 62 return (RET_SUCCESS); 63 64 if (bl->flags & P_BIGKEY) { 65 if (__ovfl_get(t, bl->bytes, 66 &key->size, &t->bt_kbuf, &t->bt_kbufsz)) 67 return (RET_ERROR); 68 } else { 69 if (bl->ksize > t->bt_kbufsz) { 70 if ((p = realloc(t->bt_kbuf, bl->ksize)) == NULL) 71 return (RET_ERROR); 72 t->bt_kbuf = p; 73 t->bt_kbufsz = bl->ksize; 74 } 75 bcopy(bl->bytes, t->bt_kbuf, t->bt_kbufsz); 76 key->size = bl->ksize; 77 } 78 key->data = t->bt_kbuf; 79 return (RET_SUCCESS); 80 } 81 82 /* 83 * __BT_CMP -- Compare a key to a given record. 84 * 85 * Parameters: 86 * t: tree 87 * k1: DBT pointer of first arg to comparison 88 * e: pointer to EPG for comparison 89 * 90 * Returns: 91 * < 0 if k1 is < record 92 * = 0 if k1 is = record 93 * > 0 if k1 is > record 94 */ 95 int 96 __bt_cmp(t, k1, e) 97 BTREE *t; 98 const DBT *k1; 99 EPG *e; 100 { 101 BINTERNAL *bi; 102 BLEAF *bl; 103 DBT k2; 104 PAGE *h; 105 void *bigkey; 106 107 /* 108 * The left-most key on internal pages, at any level of the tree, is 109 * guaranteed by the following code to be less than any user key. 110 * This saves us from having to update the leftmost key on an internal 111 * page when the user inserts a new key in the tree smaller than 112 * anything we've yet seen. 113 */ 114 h = e->page; 115 if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF)) 116 return (1); 117 118 bigkey = NULL; 119 if (h->flags & P_BLEAF) { 120 bl = GETBLEAF(h, e->index); 121 if (bl->flags & P_BIGKEY) 122 bigkey = bl->bytes; 123 else { 124 k2.data = bl->bytes; 125 k2.size = bl->ksize; 126 } 127 } else { 128 bi = GETBINTERNAL(h, e->index); 129 if (bi->flags & P_BIGKEY) 130 bigkey = bi->bytes; 131 else { 132 k2.data = bi->bytes; 133 k2.size = bi->ksize; 134 } 135 } 136 137 if (bigkey) { 138 if (__ovfl_get(t, bigkey, 139 &k2.size, &t->bt_dbuf, &t->bt_dbufsz)) 140 return (RET_ERROR); 141 k2.data = t->bt_dbuf; 142 } 143 return((*t->bt_cmp)(k1, &k2)); 144 } 145 146 /* 147 * __BT_DEFCMP -- Default comparison routine. 148 * 149 * Parameters: 150 * a: DBT #1 151 * b: DBT #2 152 * 153 * Returns: 154 * < 0 if a is < b 155 * = 0 if a is = b 156 * > 0 if a is > b 157 */ 158 int 159 __bt_defcmp(a, b) 160 const DBT *a, *b; 161 { 162 register u_char *p1, *p2; 163 register int diff, len; 164 165 len = MIN(a->size, b->size); 166 for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2) 167 if (diff = *p1 - *p2) 168 return(diff); 169 return(a->size - b->size); 170 } 171 172 /* 173 * __BT_DEFPFX -- Default prefix routine. 174 * 175 * Parameters: 176 * a: DBT #1 177 * b: DBT #2 178 * 179 * Returns: 180 * Number of bytes needed to distinguish b from a. 181 */ 182 int 183 __bt_defpfx(a, b) 184 const DBT *a, *b; 185 { 186 register u_char *p1, *p2; 187 register int len; 188 int cnt; 189 190 cnt = 1; 191 len = MIN(a->size, b->size); 192 for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt) 193 if (*p1 != *p2) 194 return(cnt); 195 196 /* a->size must be <= b->size, or they wouldn't be in this order. */ 197 return (a->size < b->size ? a->size + 1 : a->size); 198 } 199