xref: /minix3/lib/libc/db/btree/bt_overflow.c (revision de231a713e56cb204b58654702e4cbf40ff52904)
1 /*	$NetBSD: bt_overflow.c,v 1.16 2008/09/11 12:58:00 joerg 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 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38 
39 #include <sys/cdefs.h>
40 #ifndef __minix
41 __RCSID("$NetBSD: bt_overflow.c,v 1.16 2008/09/11 12:58:00 joerg Exp $");
42 #endif
43 
44 #ifndef __minix
45 #include "namespace.h"
46 #endif
47 #include <sys/param.h>
48 
49 #include <assert.h>
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, uint32_t }
80  *	buf:	storage address
81  *	bufsz:	storage size
82  *
83  * Returns:
84  *	RET_ERROR, RET_SUCCESS
85  */
86 int
87 __ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz)
88 {
89 	PAGE *h;
90 	pgno_t pg;
91 	uint32_t sz, nb, plen;
92 	size_t temp;
93 
94 	memmove(&pg, p, sizeof(pgno_t));
95 	memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(uint32_t));
96 	*ssz = sz;
97 
98 #ifdef DEBUG
99 	if (pg == P_INVALID || sz == 0)
100 		abort();
101 #endif
102 	/* Make the buffer bigger as necessary. */
103 	if (*bufsz < sz) {
104 		*buf = (char *)(*buf == NULL ? malloc(sz) : realloc(*buf, sz));
105 		if (*buf == NULL)
106 			return (RET_ERROR);
107 		*bufsz = sz;
108 	}
109 
110 	/*
111 	 * Step through the linked list of pages, copying the data on each one
112 	 * into the buffer.  Never copy more than the data's length.
113 	 */
114 	temp = t->bt_psize - BTDATAOFF;
115 	_DBFIT(temp, uint32_t);
116 	plen = (uint32_t)temp;
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(BTREE *t, const DBT *dbt, pgno_t *pg)
144 {
145 	PAGE *h, *last;
146 	void *p;
147 	pgno_t npg;
148 	uint32_t sz, nb, plen;
149 	size_t temp;
150 
151 	/*
152 	 * Allocate pages and copy the key/data record into them.  Store the
153 	 * number of the first page in the chain.
154 	 */
155 	temp = t->bt_psize - BTDATAOFF;
156 	_DBFIT(temp, uint32_t);
157 	plen = (uint32_t)temp;
158 	last = NULL;
159 	p = dbt->data;
160 	temp = dbt->size;
161 	_DBFIT(temp, uint32_t);
162 	sz = temp;
163 	for (;; 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 		(void)memmove((char *)(void *)h + BTDATAOFF, p, (size_t)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, uint32_t }
195  *
196  * Returns:
197  *	RET_ERROR, RET_SUCCESS
198  */
199 int
200 __ovfl_delete(BTREE *t, void *p)
201 {
202 	PAGE *h;
203 	pgno_t pg;
204 	uint32_t sz, plen;
205 	size_t temp;
206 
207 	(void)memmove(&pg, p, sizeof(pgno_t));
208 	(void)memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(uint32_t));
209 
210 #ifdef DEBUG
211 	if (pg == P_INVALID || sz == 0)
212 		abort();
213 #endif
214 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
215 		return (RET_ERROR);
216 
217 	/* Don't delete chains used by internal pages. */
218 	if (h->flags & P_PRESERVE) {
219 		mpool_put(t->bt_mp, h, 0);
220 		return (RET_SUCCESS);
221 	}
222 
223 	/* Step through the chain, calling the free routine for each page. */
224 	temp = t->bt_psize - BTDATAOFF;
225 	_DBFIT(temp, uint32_t);
226 	plen = (uint32_t)temp;
227 	for (;; sz -= plen) {
228 		pg = h->nextpg;
229 		__bt_free(t, h);
230 		if (sz <= plen)
231 			break;
232 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
233 			return (RET_ERROR);
234 	}
235 	return (RET_SUCCESS);
236 }
237