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