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