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