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