1*54925bf6Swillf /*-
2*54925bf6Swillf  * Copyright (c) 1990, 1993, 1994
3*54925bf6Swillf  *	The Regents of the University of California.  All rights reserved.
4*54925bf6Swillf  *
5*54925bf6Swillf  * This code is derived from software contributed to Berkeley by
6*54925bf6Swillf  * Mike Olson.
7*54925bf6Swillf  *
8*54925bf6Swillf  * Redistribution and use in source and binary forms, with or without
9*54925bf6Swillf  * modification, are permitted provided that the following conditions
10*54925bf6Swillf  * are met:
11*54925bf6Swillf  * 1. Redistributions of source code must retain the above copyright
12*54925bf6Swillf  *    notice, this list of conditions and the following disclaimer.
13*54925bf6Swillf  * 2. Redistributions in binary form must reproduce the above copyright
14*54925bf6Swillf  *    notice, this list of conditions and the following disclaimer in the
15*54925bf6Swillf  *    documentation and/or other materials provided with the distribution.
16*54925bf6Swillf  * 3. All advertising materials mentioning features or use of this software
17*54925bf6Swillf  *    must display the following acknowledgement:
18*54925bf6Swillf  *	This product includes software developed by the University of
19*54925bf6Swillf  *	California, Berkeley and its contributors.
20*54925bf6Swillf  * 4. Neither the name of the University nor the names of its contributors
21*54925bf6Swillf  *    may be used to endorse or promote products derived from this software
22*54925bf6Swillf  *    without specific prior written permission.
23*54925bf6Swillf  *
24*54925bf6Swillf  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25*54925bf6Swillf  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*54925bf6Swillf  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*54925bf6Swillf  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*54925bf6Swillf  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*54925bf6Swillf  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*54925bf6Swillf  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*54925bf6Swillf  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*54925bf6Swillf  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*54925bf6Swillf  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*54925bf6Swillf  * SUCH DAMAGE.
35*54925bf6Swillf  */
36*54925bf6Swillf 
37*54925bf6Swillf #if defined(LIBC_SCCS) && !defined(lint)
38*54925bf6Swillf static char sccsid[] = "@(#)bt_overflow.c	8.5 (Berkeley) 7/16/94";
39*54925bf6Swillf #endif /* LIBC_SCCS and not lint */
40*54925bf6Swillf 
41*54925bf6Swillf #include <sys/param.h>
42*54925bf6Swillf 
43*54925bf6Swillf #include <stdio.h>
44*54925bf6Swillf #include <stdlib.h>
45*54925bf6Swillf #include <string.h>
46*54925bf6Swillf 
47*54925bf6Swillf #include "db-int.h"
48*54925bf6Swillf #include "btree.h"
49*54925bf6Swillf 
50*54925bf6Swillf /*
51*54925bf6Swillf  * Big key/data code.
52*54925bf6Swillf  *
53*54925bf6Swillf  * Big key and data entries are stored on linked lists of pages.  The initial
54*54925bf6Swillf  * reference is byte string stored with the key or data and is the page number
55*54925bf6Swillf  * and size.  The actual record is stored in a chain of pages linked by the
56*54925bf6Swillf  * nextpg field of the PAGE header.
57*54925bf6Swillf  *
58*54925bf6Swillf  * The first page of the chain has a special property.  If the record is used
59*54925bf6Swillf  * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
60*54925bf6Swillf  * in the header.
61*54925bf6Swillf  *
62*54925bf6Swillf  * XXX
63*54925bf6Swillf  * A single DBT is written to each chain, so a lot of space on the last page
64*54925bf6Swillf  * is wasted.  This is a fairly major bug for some data sets.
65*54925bf6Swillf  */
66*54925bf6Swillf 
67*54925bf6Swillf /*
68*54925bf6Swillf  * __OVFL_GET -- Get an overflow key/data item.
69*54925bf6Swillf  *
70*54925bf6Swillf  * Parameters:
71*54925bf6Swillf  *	t:	tree
72*54925bf6Swillf  *	p:	pointer to { db_pgno_t, u_int32_t }
73*54925bf6Swillf  *	buf:	storage address
74*54925bf6Swillf  *	bufsz:	storage size
75*54925bf6Swillf  *
76*54925bf6Swillf  * Returns:
77*54925bf6Swillf  *	RET_ERROR, RET_SUCCESS
78*54925bf6Swillf  */
79*54925bf6Swillf int
__ovfl_get(t,p,ssz,buf,bufsz)80*54925bf6Swillf __ovfl_get(t, p, ssz, buf, bufsz)
81*54925bf6Swillf 	BTREE *t;
82*54925bf6Swillf 	void *p;
83*54925bf6Swillf 	size_t *ssz;
84*54925bf6Swillf 	void **buf;
85*54925bf6Swillf 	size_t *bufsz;
86*54925bf6Swillf {
87*54925bf6Swillf 	PAGE *h;
88*54925bf6Swillf 	db_pgno_t pg;
89*54925bf6Swillf 	size_t nb, plen;
90*54925bf6Swillf 	u_int32_t sz;
91*54925bf6Swillf 
92*54925bf6Swillf 	memmove(&pg, p, sizeof(db_pgno_t));
93*54925bf6Swillf 	memmove(&sz, (char *)p + sizeof(db_pgno_t), sizeof(u_int32_t));
94*54925bf6Swillf 	*ssz = sz;
95*54925bf6Swillf 
96*54925bf6Swillf #ifdef DEBUG
97*54925bf6Swillf 	if (pg == P_INVALID || sz == 0)
98*54925bf6Swillf 		abort();
99*54925bf6Swillf #endif
100*54925bf6Swillf 	/* Make the buffer bigger as necessary. */
101*54925bf6Swillf 	if (*bufsz < sz) {
102*54925bf6Swillf 		*buf = (char *)(*buf == NULL ? malloc(sz) : realloc(*buf, sz));
103*54925bf6Swillf 		if (*buf == NULL)
104*54925bf6Swillf 			return (RET_ERROR);
105*54925bf6Swillf 		*bufsz = sz;
106*54925bf6Swillf 	}
107*54925bf6Swillf 
108*54925bf6Swillf 	/*
109*54925bf6Swillf 	 * Step through the linked list of pages, copying the data on each one
110*54925bf6Swillf 	 * into the buffer.  Never copy more than the data's length.
111*54925bf6Swillf 	 */
112*54925bf6Swillf 	plen = t->bt_psize - BTDATAOFF;
113*54925bf6Swillf 	for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {
114*54925bf6Swillf 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
115*54925bf6Swillf 			return (RET_ERROR);
116*54925bf6Swillf 
117*54925bf6Swillf 		nb = MIN(sz, plen);
118*54925bf6Swillf 		memmove(p, (char *)h + BTDATAOFF, nb);
119*54925bf6Swillf 		mpool_put(t->bt_mp, h, 0);
120*54925bf6Swillf 
121*54925bf6Swillf 		if ((sz -= nb) == 0)
122*54925bf6Swillf 			break;
123*54925bf6Swillf 	}
124*54925bf6Swillf 	return (RET_SUCCESS);
125*54925bf6Swillf }
126*54925bf6Swillf 
127*54925bf6Swillf /*
128*54925bf6Swillf  * __OVFL_PUT -- Store an overflow key/data item.
129*54925bf6Swillf  *
130*54925bf6Swillf  * Parameters:
131*54925bf6Swillf  *	t:	tree
132*54925bf6Swillf  *	data:	DBT to store
133*54925bf6Swillf  *	pgno:	storage page number
134*54925bf6Swillf  *
135*54925bf6Swillf  * Returns:
136*54925bf6Swillf  *	RET_ERROR, RET_SUCCESS
137*54925bf6Swillf  */
138*54925bf6Swillf int
__ovfl_put(t,dbt,pg)139*54925bf6Swillf __ovfl_put(t, dbt, pg)
140*54925bf6Swillf 	BTREE *t;
141*54925bf6Swillf 	const DBT *dbt;
142*54925bf6Swillf 	db_pgno_t *pg;
143*54925bf6Swillf {
144*54925bf6Swillf 	PAGE *h, *last;
145*54925bf6Swillf 	void *p;
146*54925bf6Swillf 	db_pgno_t npg;
147*54925bf6Swillf 	size_t nb, plen;
148*54925bf6Swillf 	u_int32_t sz;
149*54925bf6Swillf 
150*54925bf6Swillf 	/*
151*54925bf6Swillf 	 * Allocate pages and copy the key/data record into them.  Store the
152*54925bf6Swillf 	 * number of the first page in the chain.
153*54925bf6Swillf 	 */
154*54925bf6Swillf 	plen = t->bt_psize - BTDATAOFF;
155*54925bf6Swillf 	for (last = NULL, p = dbt->data, sz = dbt->size;;
156*54925bf6Swillf 	    p = (char *)p + plen, last = h) {
157*54925bf6Swillf 		if ((h = __bt_new(t, &npg)) == NULL)
158*54925bf6Swillf 			return (RET_ERROR);
159*54925bf6Swillf 
160*54925bf6Swillf 		h->pgno = npg;
161*54925bf6Swillf 		h->nextpg = h->prevpg = P_INVALID;
162*54925bf6Swillf 		h->flags = P_OVERFLOW;
163*54925bf6Swillf 		h->lower = h->upper = 0;
164*54925bf6Swillf 
165*54925bf6Swillf 		nb = MIN(sz, plen);
166*54925bf6Swillf 		memmove((char *)h + BTDATAOFF, p, nb);
167*54925bf6Swillf 
168*54925bf6Swillf 		if (last) {
169*54925bf6Swillf 			last->nextpg = h->pgno;
170*54925bf6Swillf 			mpool_put(t->bt_mp, last, MPOOL_DIRTY);
171*54925bf6Swillf 		} else
172*54925bf6Swillf 			*pg = h->pgno;
173*54925bf6Swillf 
174*54925bf6Swillf 		if ((sz -= nb) == 0) {
175*54925bf6Swillf 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
176*54925bf6Swillf 			break;
177*54925bf6Swillf 		}
178*54925bf6Swillf 	}
179*54925bf6Swillf 	return (RET_SUCCESS);
180*54925bf6Swillf }
181*54925bf6Swillf 
182*54925bf6Swillf /*
183*54925bf6Swillf  * __OVFL_DELETE -- Delete an overflow chain.
184*54925bf6Swillf  *
185*54925bf6Swillf  * Parameters:
186*54925bf6Swillf  *	t:	tree
187*54925bf6Swillf  *	p:	pointer to { db_pgno_t, u_int32_t }
188*54925bf6Swillf  *
189*54925bf6Swillf  * Returns:
190*54925bf6Swillf  *	RET_ERROR, RET_SUCCESS
191*54925bf6Swillf  */
192*54925bf6Swillf int
__ovfl_delete(t,p)193*54925bf6Swillf __ovfl_delete(t, p)
194*54925bf6Swillf 	BTREE *t;
195*54925bf6Swillf 	void *p;
196*54925bf6Swillf {
197*54925bf6Swillf 	PAGE *h;
198*54925bf6Swillf 	db_pgno_t pg;
199*54925bf6Swillf 	size_t plen;
200*54925bf6Swillf 	u_int32_t sz;
201*54925bf6Swillf 
202*54925bf6Swillf 	memmove(&pg, p, sizeof(db_pgno_t));
203*54925bf6Swillf 	memmove(&sz, (char *)p + sizeof(db_pgno_t), sizeof(u_int32_t));
204*54925bf6Swillf 
205*54925bf6Swillf #ifdef DEBUG
206*54925bf6Swillf 	if (pg == P_INVALID || sz == 0)
207*54925bf6Swillf 		abort();
208*54925bf6Swillf #endif
209*54925bf6Swillf 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
210*54925bf6Swillf 		return (RET_ERROR);
211*54925bf6Swillf 
212*54925bf6Swillf 	/* Don't delete chains used by internal pages. */
213*54925bf6Swillf 	if (h->flags & P_PRESERVE) {
214*54925bf6Swillf 		mpool_put(t->bt_mp, h, 0);
215*54925bf6Swillf 		return (RET_SUCCESS);
216*54925bf6Swillf 	}
217*54925bf6Swillf 
218*54925bf6Swillf 	/* Step through the chain, calling the free routine for each page. */
219*54925bf6Swillf 	for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) {
220*54925bf6Swillf 		pg = h->nextpg;
221*54925bf6Swillf 		__bt_free(t, h);
222*54925bf6Swillf 		if (sz <= plen)
223*54925bf6Swillf 			break;
224*54925bf6Swillf 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
225*54925bf6Swillf 			return (RET_ERROR);
226*54925bf6Swillf 	}
227*54925bf6Swillf 	return (RET_SUCCESS);
228*54925bf6Swillf }
229