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