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