Home | History | Annotate | Line # | Download | only in btree
bt_overflow.c revision 1.17
      1 /*	$NetBSD: bt_overflow.c,v 1.17 2011/06/26 22:20:31 christos 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 __RCSID("$NetBSD: bt_overflow.c,v 1.17 2011/06/26 22:20:31 christos Exp $");
     41 
     42 #include "namespace.h"
     43 #include <sys/param.h>
     44 
     45 #include <assert.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 
     50 #include <db.h>
     51 #include "btree.h"
     52 
     53 /*
     54  * Big key/data code.
     55  *
     56  * Big key and data entries are stored on linked lists of pages.  The initial
     57  * reference is byte string stored with the key or data and is the page number
     58  * and size.  The actual record is stored in a chain of pages linked by the
     59  * nextpg field of the PAGE header.
     60  *
     61  * The first page of the chain has a special property.  If the record is used
     62  * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
     63  * in the header.
     64  *
     65  * XXX
     66  * A single DBT is written to each chain, so a lot of space on the last page
     67  * is wasted.  This is a fairly major bug for some data sets.
     68  */
     69 
     70 /*
     71  * __OVFL_GET -- Get an overflow key/data item.
     72  *
     73  * Parameters:
     74  *	t:	tree
     75  *	p:	pointer to { pgno_t, uint32_t }
     76  *	buf:	storage address
     77  *	bufsz:	storage size
     78  *
     79  * Returns:
     80  *	RET_ERROR, RET_SUCCESS
     81  */
     82 int
     83 __ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz)
     84 {
     85 	PAGE *h;
     86 	pgno_t pg;
     87 	uint32_t sz, nb, plen;
     88 	size_t temp;
     89 
     90 	memmove(&pg, p, sizeof(pg));
     91 	memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(uint32_t));
     92 	*ssz = sz;
     93 
     94 #ifdef DEBUG
     95 	if (pg == P_INVALID || sz == 0)
     96 		abort();
     97 #endif
     98 	/* Make the buffer bigger as necessary. */
     99 	if (*bufsz < sz) {
    100 		*buf = (*buf == NULL ? malloc(sz) : realloc(*buf, sz));
    101 		if (*buf == NULL)
    102 			return (RET_ERROR);
    103 		*bufsz = sz;
    104 	}
    105 
    106 	/*
    107 	 * Step through the linked list of pages, copying the data on each one
    108 	 * into the buffer.  Never copy more than the data's length.
    109 	 */
    110 	temp = t->bt_psize - BTDATAOFF;
    111 	_DBFIT(temp, uint32_t);
    112 	plen = (uint32_t)temp;
    113 	for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {
    114 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    115 			return (RET_ERROR);
    116 
    117 		nb = MIN(sz, plen);
    118 		memmove(p, (char *)(void *)h + BTDATAOFF, nb);
    119 		mpool_put(t->bt_mp, h, 0);
    120 
    121 		if ((sz -= nb) == 0)
    122 			break;
    123 	}
    124 	return (RET_SUCCESS);
    125 }
    126 
    127 /*
    128  * __OVFL_PUT -- Store an overflow key/data item.
    129  *
    130  * Parameters:
    131  *	t:	tree
    132  *	data:	DBT to store
    133  *	pgno:	storage page number
    134  *
    135  * Returns:
    136  *	RET_ERROR, RET_SUCCESS
    137  */
    138 int
    139 __ovfl_put(BTREE *t, const DBT *dbt, pgno_t *pg)
    140 {
    141 	PAGE *h, *last;
    142 	void *p;
    143 	pgno_t npg;
    144 	uint32_t sz, nb, plen;
    145 	size_t temp;
    146 
    147 	/*
    148 	 * Allocate pages and copy the key/data record into them.  Store the
    149 	 * number of the first page in the chain.
    150 	 */
    151 	temp = t->bt_psize - BTDATAOFF;
    152 	_DBFIT(temp, uint32_t);
    153 	plen = (uint32_t)temp;
    154 	last = NULL;
    155 	p = dbt->data;
    156 	temp = dbt->size;
    157 	_DBFIT(temp, uint32_t);
    158 	sz = temp;
    159 	for (;; p = (char *)p + plen, last = h) {
    160 		if ((h = __bt_new(t, &npg)) == NULL)
    161 			return (RET_ERROR);
    162 
    163 		h->pgno = npg;
    164 		h->nextpg = h->prevpg = P_INVALID;
    165 		h->flags = P_OVERFLOW;
    166 		h->lower = h->upper = 0;
    167 
    168 		nb = MIN(sz, plen);
    169 		(void)memmove((char *)(void *)h + BTDATAOFF, p, (size_t)nb);
    170 
    171 		if (last) {
    172 			last->nextpg = h->pgno;
    173 			mpool_put(t->bt_mp, last, MPOOL_DIRTY);
    174 		} else
    175 			*pg = h->pgno;
    176 
    177 		if ((sz -= nb) == 0) {
    178 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    179 			break;
    180 		}
    181 	}
    182 	return (RET_SUCCESS);
    183 }
    184 
    185 /*
    186  * __OVFL_DELETE -- Delete an overflow chain.
    187  *
    188  * Parameters:
    189  *	t:	tree
    190  *	p:	pointer to { pgno_t, uint32_t }
    191  *
    192  * Returns:
    193  *	RET_ERROR, RET_SUCCESS
    194  */
    195 int
    196 __ovfl_delete(BTREE *t, void *p)
    197 {
    198 	PAGE *h;
    199 	pgno_t pg;
    200 	uint32_t sz, plen;
    201 	size_t temp;
    202 
    203 	(void)memmove(&pg, p, sizeof(pg));
    204 	(void)memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(uint32_t));
    205 
    206 #ifdef DEBUG
    207 	if (pg == P_INVALID || sz == 0)
    208 		abort();
    209 #endif
    210 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    211 		return (RET_ERROR);
    212 
    213 	/* Don't delete chains used by internal pages. */
    214 	if (h->flags & P_PRESERVE) {
    215 		mpool_put(t->bt_mp, h, 0);
    216 		return (RET_SUCCESS);
    217 	}
    218 
    219 	/* Step through the chain, calling the free routine for each page. */
    220 	temp = t->bt_psize - BTDATAOFF;
    221 	_DBFIT(temp, uint32_t);
    222 	plen = (uint32_t)temp;
    223 	for (;; sz -= plen) {
    224 		pg = h->nextpg;
    225 		__bt_free(t, h);
    226 		if (sz <= plen)
    227 			break;
    228 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    229 			return (RET_ERROR);
    230 	}
    231 	return (RET_SUCCESS);
    232 }
    233