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