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