]> sourceware.org Git - glibc.git/blob - db2/db/db_overflow.c
Update.
[glibc.git] / db2 / db / db_overflow.c
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
6 */
7 /*
8 * Copyright (c) 1990, 1993, 1994, 1995, 1996
9 * Keith Bostic. All rights reserved.
10 */
11 /*
12 * Copyright (c) 1990, 1993, 1994, 1995
13 * The Regents of the University of California. All rights reserved.
14 *
15 * This code is derived from software contributed to Berkeley by
16 * Mike Olson.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 */
46
47 #include "config.h"
48
49 #ifndef lint
50 static const char sccsid[] = "@(#)db_overflow.c 10.4 (Sleepycat) 7/2/97";
51 #endif /* not lint */
52
53 #ifndef NO_SYSTEM_INCLUDES
54 #include <sys/types.h>
55
56 #include <errno.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #endif
61
62 #include "db_int.h"
63 #include "db_page.h"
64 #include "db_am.h"
65 #include "common_ext.h"
66
67 /*
68 * Big key/data code.
69 *
70 * Big key and data entries are stored on linked lists of pages. The initial
71 * reference is a structure with the total length of the item and the page
72 * number where it begins. Each entry in the linked list contains a pointer
73 * to the next page of data, and so on.
74 */
75
76 /*
77 * __db_goff --
78 * Get an offpage item.
79 *
80 * PUBLIC: int __db_goff __P((DB *, DBT *,
81 * PUBLIC: u_int32_t, db_pgno_t, void **, u_int32_t *));
82 */
83 int
84 __db_goff(dbp, dbt, tlen, pgno, bpp, bpsz)
85 DB *dbp;
86 DBT *dbt;
87 u_int32_t tlen;
88 db_pgno_t pgno;
89 void **bpp;
90 u_int32_t *bpsz;
91 {
92 PAGE *h;
93 db_indx_t bytes;
94 int ret;
95 u_int32_t curoff, needed, start;
96 u_int8_t *p, *src;
97
98 /*
99 * Check if the buffer is big enough; if it is not and we are
100 * allowed to malloc space, then we'll malloc it. If we are
101 * not (DB_DBT_USERMEM), then we'll set the dbt and return
102 * appropriately.
103 */
104 if (F_ISSET(dbt, DB_DBT_PARTIAL)) {
105 start = dbt->doff;
106 needed = dbt->dlen;
107 } else {
108 start = 0;
109 needed = tlen;
110 }
111
112 /*
113 * Allocate any necessary memory.
114 *
115 * XXX: Never allocate 0 bytes;
116 */
117 if (F_ISSET(dbt, DB_DBT_USERMEM)) {
118 if (needed > dbt->ulen) {
119 dbt->size = needed;
120 return (ENOMEM);
121 }
122 } else if (F_ISSET(dbt, DB_DBT_MALLOC)) {
123 dbt->data = dbp->db_malloc == NULL ?
124 (void *)malloc(needed + 1) :
125 (void *)dbp->db_malloc(needed + 1);
126 if (dbt->data == NULL)
127 return (ENOMEM);
128 } else if (*bpsz == 0 || *bpsz < needed) {
129 *bpp = (*bpp == NULL ?
130 (void *)malloc(needed + 1) :
131 (void *)realloc(*bpp, needed + 1));
132 if (*bpp == NULL)
133 return (ENOMEM);
134 *bpsz = needed + 1;
135 dbt->data = *bpp;
136 } else
137 dbt->data = *bpp;
138
139 /*
140 * Step through the linked list of pages, copying the data on each
141 * one into the buffer. Never copy more than the total data length.
142 */
143 dbt->size = needed;
144 for (curoff = 0, p = dbt->data; pgno != P_INVALID && needed > 0;) {
145 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
146 (void)__db_pgerr(dbp, pgno);
147 return (ret);
148 }
149 /* Check if we need any bytes from this page. */
150 if (curoff + OV_LEN(h) >= start) {
151 src = (u_int8_t *)h + P_OVERHEAD;
152 bytes = OV_LEN(h);
153 if (start > curoff) {
154 src += start - curoff;
155 bytes -= start - curoff;
156 }
157 if (bytes > needed)
158 bytes = needed;
159 memcpy(p, src, bytes);
160 p += bytes;
161 needed -= bytes;
162 }
163 curoff += OV_LEN(h);
164 pgno = h->next_pgno;
165 memp_fput(dbp->mpf, h, 0);
166 }
167 return (0);
168 }
169
170 /*
171 * __db_poff --
172 * Put an offpage item.
173 *
174 * PUBLIC: int __db_poff __P((DB *, const DBT *, db_pgno_t *,
175 * PUBLIC: int (*)(DB *, u_int32_t, PAGE **)));
176 */
177 int
178 __db_poff(dbp, dbt, pgnop, newfunc)
179 DB *dbp;
180 const DBT *dbt;
181 db_pgno_t *pgnop;
182 int (*newfunc) __P((DB *, u_int32_t, PAGE **));
183 {
184 PAGE *pagep, *lastp;
185 DB_LSN new_lsn, null_lsn;
186 DBT tmp_dbt;
187 db_indx_t pagespace;
188 u_int32_t sz;
189 u_int8_t *p;
190 int ret;
191
192 /*
193 * Allocate pages and copy the key/data item into them. Calculate the
194 * number of bytes we get for pages we fill completely with a single
195 * item.
196 */
197 pagespace = P_MAXSPACE(dbp->pgsize);
198
199 lastp = NULL;
200 for (p = dbt->data,
201 sz = dbt->size; sz > 0; p += pagespace, sz -= pagespace) {
202 /*
203 * Reduce pagespace so we terminate the loop correctly and
204 * don't copy too much data.
205 */
206 if (sz < pagespace)
207 pagespace = sz;
208
209 /*
210 * Allocate and initialize a new page and copy all or part of
211 * the item onto the page. If sz is less than pagespace, we
212 * have a partial record.
213 */
214 if ((ret = newfunc(dbp, P_OVERFLOW, &pagep)) != 0)
215 return (ret);
216 if (DB_LOGGING(dbp)) {
217 tmp_dbt.data = p;
218 tmp_dbt.size = pagespace;
219 ZERO_LSN(null_lsn);
220 if ((ret = __db_big_log(dbp->dbenv->lg_info, dbp->txn,
221 &new_lsn, 0, DB_ADD_BIG, dbp->log_fileid,
222 PGNO(pagep), lastp ? PGNO(lastp) : PGNO_INVALID,
223 PGNO_INVALID, &tmp_dbt, &LSN(pagep),
224 lastp == NULL ? &null_lsn : &LSN(lastp),
225 &null_lsn)) != 0)
226 return (ret);
227
228 /* Move lsn onto page. */
229 if (lastp)
230 LSN(lastp) = new_lsn;
231 LSN(pagep) = new_lsn;
232 }
233
234 P_INIT(pagep, dbp->pgsize,
235 PGNO(pagep), PGNO_INVALID, PGNO_INVALID, 0, P_OVERFLOW);
236 OV_LEN(pagep) = pagespace;
237 OV_REF(pagep) = 1;
238 memcpy((u_int8_t *)pagep + P_OVERHEAD, p, pagespace);
239
240 /*
241 * If this is the first entry, update the user's info.
242 * Otherwise, update the entry on the last page filled
243 * in and release that page.
244 */
245 if (lastp == NULL)
246 *pgnop = PGNO(pagep);
247 else {
248 lastp->next_pgno = PGNO(pagep);
249 pagep->prev_pgno = PGNO(lastp);
250 (void)memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
251 }
252 lastp = pagep;
253 }
254 (void)memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
255 return (0);
256 }
257
258 /*
259 * __db_ioff --
260 * Increment the reference count on an overflow page.
261 *
262 * PUBLIC: int __db_ioff __P((DB *, db_pgno_t));
263 */
264 int
265 __db_ioff(dbp, pgno)
266 DB *dbp;
267 db_pgno_t pgno;
268 {
269 PAGE *h;
270 int ret;
271
272 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
273 (void)__db_pgerr(dbp, pgno);
274 return (ret);
275 }
276
277 ++OV_REF(h);
278 if (DB_LOGGING(dbp) && (ret = __db_ovref_log(dbp->dbenv->lg_info,
279 dbp->txn, &LSN(h), 0, dbp->log_fileid, h->pgno, &LSN(h))) != 0)
280 return (ret);
281
282 (void)memp_fput(dbp->mpf, h, DB_MPOOL_DIRTY);
283 return (0);
284 }
285
286 /*
287 * __db_doff --
288 * Delete an offpage chain of overflow pages.
289 *
290 * PUBLIC: int __db_doff __P((DB *, db_pgno_t, int (*)(DB *, PAGE *)));
291 */
292 int
293 __db_doff(dbp, pgno, freefunc)
294 DB *dbp;
295 db_pgno_t pgno;
296 int (*freefunc) __P((DB *, PAGE *));
297 {
298 PAGE *pagep;
299 DB_LSN null_lsn;
300 DBT tmp_dbt;
301 int ret;
302
303 do {
304 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0) {
305 (void)__db_pgerr(dbp, pgno);
306 return (ret);
307 }
308
309 /*
310 * If it's an overflow page and it's referenced by more than
311 * one key/data item, decrement the reference count and return.
312 */
313 if (TYPE(pagep) == P_OVERFLOW && OV_REF(pagep) > 1) {
314 --OV_REF(pagep);
315 (void)memp_fput(dbp->mpf, pagep, DB_MPOOL_DIRTY);
316 return (0);
317 }
318
319 if (DB_LOGGING(dbp)) {
320 tmp_dbt.data = (u_int8_t *)pagep + P_OVERHEAD;
321 tmp_dbt.size = OV_LEN(pagep);
322 ZERO_LSN(null_lsn);
323 if ((ret = __db_big_log(dbp->dbenv->lg_info, dbp->txn,
324 &LSN(pagep), 0, DB_REM_BIG, dbp->log_fileid,
325 PGNO(pagep), PREV_PGNO(pagep), NEXT_PGNO(pagep),
326 &tmp_dbt, &LSN(pagep), &null_lsn, &null_lsn)) != 0)
327 return (ret);
328 }
329 pgno = pagep->next_pgno;
330 if ((ret = freefunc(dbp, pagep)) != 0)
331 return (ret);
332 } while (pgno != PGNO_INVALID);
333
334 return (0);
335 }
336
337 /*
338 * __db_moff --
339 * Match on overflow pages.
340 *
341 * Given a starting page number and a key, return <0, 0, >0 to indicate if the
342 * key on the page is less than, equal to or greater than the key specified.
343 *
344 * PUBLIC: int __db_moff __P((DB *, const DBT *, db_pgno_t));
345 */
346 int
347 __db_moff(dbp, dbt, pgno)
348 DB *dbp;
349 const DBT *dbt;
350 db_pgno_t pgno;
351 {
352 PAGE *pagep;
353 u_int32_t cmp_bytes, key_left;
354 int ret;
355 u_int8_t *p1, *p2;
356
357 /* While there are both keys to compare. */
358 for (ret = 0, p1 = dbt->data,
359 key_left = dbt->size; key_left > 0 && pgno != PGNO_INVALID;) {
360 if (memp_fget(dbp->mpf, &pgno, 0, &pagep) != 0) {
361 (void)__db_pgerr(dbp, pgno);
362 return (0); /* No system error return. */
363 }
364
365 cmp_bytes = OV_LEN(pagep) < key_left ? OV_LEN(pagep) : key_left;
366 key_left -= cmp_bytes;
367 for (p2 =
368 (u_int8_t *)pagep + P_OVERHEAD; cmp_bytes-- > 0; ++p1, ++p2)
369 if (*p1 != *p2) {
370 ret = (long)*p1 - (long)*p2;
371 break;
372 }
373 pgno = NEXT_PGNO(pagep);
374 (void)memp_fput(dbp->mpf, pagep, 0);
375 if (ret != 0)
376 return (ret);
377 }
378 if (key_left > 0) /* DBT is longer than page key. */
379 return (-1);
380 if (pgno != PGNO_INVALID) /* DBT is shorter than page key. */
381 return (1);
382 return (0);
383 }
This page took 0.053697 seconds and 5 git commands to generate.