xref: /minix3/lib/libc/db/btree/bt_overflow.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: bt_overflow.c,v 1.20 2013/12/14 18:04:56 christos Exp $	*/
22639ae9bSBen Gras 
32639ae9bSBen Gras /*-
42639ae9bSBen Gras  * Copyright (c) 1990, 1993, 1994
52639ae9bSBen Gras  *	The Regents of the University of California.  All rights reserved.
62639ae9bSBen Gras  *
72639ae9bSBen Gras  * This code is derived from software contributed to Berkeley by
82639ae9bSBen Gras  * Mike Olson.
92639ae9bSBen Gras  *
102639ae9bSBen Gras  * Redistribution and use in source and binary forms, with or without
112639ae9bSBen Gras  * modification, are permitted provided that the following conditions
122639ae9bSBen Gras  * are met:
132639ae9bSBen Gras  * 1. Redistributions of source code must retain the above copyright
142639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer.
152639ae9bSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer in the
172639ae9bSBen Gras  *    documentation and/or other materials provided with the distribution.
182639ae9bSBen Gras  * 3. Neither the name of the University nor the names of its contributors
192639ae9bSBen Gras  *    may be used to endorse or promote products derived from this software
202639ae9bSBen Gras  *    without specific prior written permission.
212639ae9bSBen Gras  *
222639ae9bSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232639ae9bSBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242639ae9bSBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252639ae9bSBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262639ae9bSBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272639ae9bSBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282639ae9bSBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292639ae9bSBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302639ae9bSBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312639ae9bSBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322639ae9bSBen Gras  * SUCH DAMAGE.
332639ae9bSBen Gras  */
342639ae9bSBen Gras 
352639ae9bSBen Gras #if HAVE_NBTOOL_CONFIG_H
362639ae9bSBen Gras #include "nbtool_config.h"
372639ae9bSBen Gras #endif
382639ae9bSBen Gras 
392639ae9bSBen Gras #include <sys/cdefs.h>
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: bt_overflow.c,v 1.20 2013/12/14 18:04:56 christos Exp $");
412639ae9bSBen Gras 
422639ae9bSBen Gras #include "namespace.h"
432639ae9bSBen Gras #include <sys/param.h>
442639ae9bSBen Gras 
452639ae9bSBen Gras #include <assert.h>
462639ae9bSBen Gras #include <stdio.h>
472639ae9bSBen Gras #include <stdlib.h>
482639ae9bSBen Gras #include <string.h>
492639ae9bSBen Gras 
502639ae9bSBen Gras #include <db.h>
512639ae9bSBen Gras #include "btree.h"
522639ae9bSBen Gras 
532639ae9bSBen Gras /*
542639ae9bSBen Gras  * Big key/data code.
552639ae9bSBen Gras  *
562639ae9bSBen Gras  * Big key and data entries are stored on linked lists of pages.  The initial
572639ae9bSBen Gras  * reference is byte string stored with the key or data and is the page number
582639ae9bSBen Gras  * and size.  The actual record is stored in a chain of pages linked by the
592639ae9bSBen Gras  * nextpg field of the PAGE header.
602639ae9bSBen Gras  *
612639ae9bSBen Gras  * The first page of the chain has a special property.  If the record is used
622639ae9bSBen Gras  * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
632639ae9bSBen Gras  * in the header.
642639ae9bSBen Gras  *
652639ae9bSBen Gras  * XXX
662639ae9bSBen Gras  * A single DBT is written to each chain, so a lot of space on the last page
672639ae9bSBen Gras  * is wasted.  This is a fairly major bug for some data sets.
682639ae9bSBen Gras  */
692639ae9bSBen Gras 
702639ae9bSBen Gras /*
712639ae9bSBen Gras  * __OVFL_GET -- Get an overflow key/data item.
722639ae9bSBen Gras  *
732639ae9bSBen Gras  * Parameters:
742639ae9bSBen Gras  *	t:	tree
752639ae9bSBen Gras  *	p:	pointer to { pgno_t, uint32_t }
762639ae9bSBen Gras  *	buf:	storage address
772639ae9bSBen Gras  *	bufsz:	storage size
782639ae9bSBen Gras  *
792639ae9bSBen Gras  * Returns:
802639ae9bSBen Gras  *	RET_ERROR, RET_SUCCESS
812639ae9bSBen Gras  */
822639ae9bSBen Gras int
__ovfl_get(BTREE * t,void * p,size_t * ssz,void ** buf,size_t * bufsz)832639ae9bSBen Gras __ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz)
842639ae9bSBen Gras {
852639ae9bSBen Gras 	PAGE *h;
862639ae9bSBen Gras 	pgno_t pg;
872639ae9bSBen Gras 	uint32_t sz, nb, plen;
882639ae9bSBen Gras 	size_t temp;
892639ae9bSBen Gras 
90f14fb602SLionel Sambuc 	memmove(&pg, p, sizeof(pg));
912639ae9bSBen Gras 	memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(uint32_t));
922639ae9bSBen Gras 	*ssz = sz;
932639ae9bSBen Gras 
942639ae9bSBen Gras #ifdef DEBUG
952639ae9bSBen Gras 	if (pg == P_INVALID || sz == 0)
962639ae9bSBen Gras 		abort();
972639ae9bSBen Gras #endif
982639ae9bSBen Gras 	/* Make the buffer bigger as necessary. */
992639ae9bSBen Gras 	if (*bufsz < sz) {
100*0a6a1f1dSLionel Sambuc 		void *nbuf = realloc(*buf, sz);
101*0a6a1f1dSLionel Sambuc 		if (nbuf == NULL)
1022639ae9bSBen Gras 			return (RET_ERROR);
103*0a6a1f1dSLionel Sambuc 		*buf = nbuf;
1042639ae9bSBen Gras 		*bufsz = sz;
1052639ae9bSBen Gras 	}
1062639ae9bSBen Gras 
1072639ae9bSBen Gras 	/*
1082639ae9bSBen Gras 	 * Step through the linked list of pages, copying the data on each one
1092639ae9bSBen Gras 	 * into the buffer.  Never copy more than the data's length.
1102639ae9bSBen Gras 	 */
1112639ae9bSBen Gras 	temp = t->bt_psize - BTDATAOFF;
1122639ae9bSBen Gras 	_DBFIT(temp, uint32_t);
1132639ae9bSBen Gras 	plen = (uint32_t)temp;
1142639ae9bSBen Gras 	for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {
1152639ae9bSBen Gras 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
1162639ae9bSBen Gras 			return (RET_ERROR);
1172639ae9bSBen Gras 
1182639ae9bSBen Gras 		nb = MIN(sz, plen);
1192639ae9bSBen Gras 		memmove(p, (char *)(void *)h + BTDATAOFF, nb);
1202639ae9bSBen Gras 		mpool_put(t->bt_mp, h, 0);
1212639ae9bSBen Gras 
1222639ae9bSBen Gras 		if ((sz -= nb) == 0)
1232639ae9bSBen Gras 			break;
1242639ae9bSBen Gras 	}
1252639ae9bSBen Gras 	return (RET_SUCCESS);
1262639ae9bSBen Gras }
1272639ae9bSBen Gras 
1282639ae9bSBen Gras /*
1292639ae9bSBen Gras  * __OVFL_PUT -- Store an overflow key/data item.
1302639ae9bSBen Gras  *
1312639ae9bSBen Gras  * Parameters:
1322639ae9bSBen Gras  *	t:	tree
1332639ae9bSBen Gras  *	data:	DBT to store
1342639ae9bSBen Gras  *	pgno:	storage page number
1352639ae9bSBen Gras  *
1362639ae9bSBen Gras  * Returns:
1372639ae9bSBen Gras  *	RET_ERROR, RET_SUCCESS
1382639ae9bSBen Gras  */
1392639ae9bSBen Gras int
__ovfl_put(BTREE * t,const DBT * dbt,pgno_t * pg)1402639ae9bSBen Gras __ovfl_put(BTREE *t, const DBT *dbt, pgno_t *pg)
1412639ae9bSBen Gras {
1422639ae9bSBen Gras 	PAGE *h, *last;
1432639ae9bSBen Gras 	void *p;
1442639ae9bSBen Gras 	pgno_t npg;
1452639ae9bSBen Gras 	uint32_t sz, nb, plen;
1462639ae9bSBen Gras 	size_t temp;
1472639ae9bSBen Gras 
1482639ae9bSBen Gras 	/*
1492639ae9bSBen Gras 	 * Allocate pages and copy the key/data record into them.  Store the
1502639ae9bSBen Gras 	 * number of the first page in the chain.
1512639ae9bSBen Gras 	 */
1522639ae9bSBen Gras 	temp = t->bt_psize - BTDATAOFF;
1532639ae9bSBen Gras 	_DBFIT(temp, uint32_t);
1542639ae9bSBen Gras 	plen = (uint32_t)temp;
1552639ae9bSBen Gras 	last = NULL;
1562639ae9bSBen Gras 	p = dbt->data;
1572639ae9bSBen Gras 	temp = dbt->size;
1582639ae9bSBen Gras 	_DBFIT(temp, uint32_t);
159f14fb602SLionel Sambuc 	sz = (uint32_t)temp;
1602639ae9bSBen Gras 	for (;; p = (char *)p + plen, last = h) {
1612639ae9bSBen Gras 		if ((h = __bt_new(t, &npg)) == NULL)
1622639ae9bSBen Gras 			return (RET_ERROR);
1632639ae9bSBen Gras 
1642639ae9bSBen Gras 		h->pgno = npg;
1652639ae9bSBen Gras 		h->nextpg = h->prevpg = P_INVALID;
1662639ae9bSBen Gras 		h->flags = P_OVERFLOW;
1672639ae9bSBen Gras 		h->lower = h->upper = 0;
1682639ae9bSBen Gras 
1692639ae9bSBen Gras 		nb = MIN(sz, plen);
1702639ae9bSBen Gras 		(void)memmove((char *)(void *)h + BTDATAOFF, p, (size_t)nb);
1712639ae9bSBen Gras 
1722639ae9bSBen Gras 		if (last) {
1732639ae9bSBen Gras 			last->nextpg = h->pgno;
1742639ae9bSBen Gras 			mpool_put(t->bt_mp, last, MPOOL_DIRTY);
1752639ae9bSBen Gras 		} else
1762639ae9bSBen Gras 			*pg = h->pgno;
1772639ae9bSBen Gras 
1782639ae9bSBen Gras 		if ((sz -= nb) == 0) {
1792639ae9bSBen Gras 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
1802639ae9bSBen Gras 			break;
1812639ae9bSBen Gras 		}
1822639ae9bSBen Gras 	}
1832639ae9bSBen Gras 	return (RET_SUCCESS);
1842639ae9bSBen Gras }
1852639ae9bSBen Gras 
1862639ae9bSBen Gras /*
1872639ae9bSBen Gras  * __OVFL_DELETE -- Delete an overflow chain.
1882639ae9bSBen Gras  *
1892639ae9bSBen Gras  * Parameters:
1902639ae9bSBen Gras  *	t:	tree
1912639ae9bSBen Gras  *	p:	pointer to { pgno_t, uint32_t }
1922639ae9bSBen Gras  *
1932639ae9bSBen Gras  * Returns:
1942639ae9bSBen Gras  *	RET_ERROR, RET_SUCCESS
1952639ae9bSBen Gras  */
1962639ae9bSBen Gras int
__ovfl_delete(BTREE * t,void * p)1972639ae9bSBen Gras __ovfl_delete(BTREE *t, void *p)
1982639ae9bSBen Gras {
1992639ae9bSBen Gras 	PAGE *h;
2002639ae9bSBen Gras 	pgno_t pg;
2012639ae9bSBen Gras 	uint32_t sz, plen;
2022639ae9bSBen Gras 	size_t temp;
2032639ae9bSBen Gras 
204f14fb602SLionel Sambuc 	(void)memmove(&pg, p, sizeof(pg));
2052639ae9bSBen Gras 	(void)memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(uint32_t));
2062639ae9bSBen Gras 
2072639ae9bSBen Gras #ifdef DEBUG
2082639ae9bSBen Gras 	if (pg == P_INVALID || sz == 0)
2092639ae9bSBen Gras 		abort();
2102639ae9bSBen Gras #endif
2112639ae9bSBen Gras 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
2122639ae9bSBen Gras 		return (RET_ERROR);
2132639ae9bSBen Gras 
2142639ae9bSBen Gras 	/* Don't delete chains used by internal pages. */
2152639ae9bSBen Gras 	if (h->flags & P_PRESERVE) {
2162639ae9bSBen Gras 		mpool_put(t->bt_mp, h, 0);
2172639ae9bSBen Gras 		return (RET_SUCCESS);
2182639ae9bSBen Gras 	}
2192639ae9bSBen Gras 
2202639ae9bSBen Gras 	/* Step through the chain, calling the free routine for each page. */
2212639ae9bSBen Gras 	temp = t->bt_psize - BTDATAOFF;
2222639ae9bSBen Gras 	_DBFIT(temp, uint32_t);
2232639ae9bSBen Gras 	plen = (uint32_t)temp;
2242639ae9bSBen Gras 	for (;; sz -= plen) {
2252639ae9bSBen Gras 		pg = h->nextpg;
2262639ae9bSBen Gras 		__bt_free(t, h);
2272639ae9bSBen Gras 		if (sz <= plen)
2282639ae9bSBen Gras 			break;
2292639ae9bSBen Gras 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
2302639ae9bSBen Gras 			return (RET_ERROR);
2312639ae9bSBen Gras 	}
2322639ae9bSBen Gras 	return (RET_SUCCESS);
2332639ae9bSBen Gras }
234