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