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