2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
8 * Copyright (c) 1990, 1993, 1994, 1995, 1996
9 * Keith Bostic. All rights reserved.
12 * Copyright (c) 1990, 1993, 1994, 1995
13 * The Regents of the University of California. All rights reserved.
15 * This code is derived from software contributed to Berkeley by
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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.
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
50 static const char sccsid[] = "@(#)db_overflow.c 10.4 (Sleepycat) 7/2/97";
53 #ifndef NO_SYSTEM_INCLUDES
54 #include <sys/types.h>
65 #include "common_ext.h"
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.
78 * Get an offpage item.
80 * PUBLIC: int __db_goff __P((DB *, DBT *,
81 * PUBLIC: u_int32_t, db_pgno_t, void **, u_int32_t *));
84 __db_goff(dbp, dbt, tlen, pgno, bpp, bpsz)
95 u_int32_t curoff, needed, start;
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
104 if (F_ISSET(dbt, DB_DBT_PARTIAL)) {
113 * Allocate any necessary memory.
115 * XXX: Never allocate 0 bytes;
117 if (F_ISSET(dbt, DB_DBT_USERMEM)) {
118 if (needed > dbt->ulen) {
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)
128 } else if (*bpsz == 0 || *bpsz < needed) {
129 *bpp = (*bpp == NULL ?
130 (void *)malloc(needed + 1) :
131 (void *)realloc(*bpp, needed + 1));
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.
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);
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;
153 if (start > curoff) {
154 src += start - curoff;
155 bytes -= start - curoff;
159 memcpy(p, src, bytes);
165 memp_fput(dbp->mpf, h, 0);
172 * Put an offpage item.
174 * PUBLIC: int __db_poff __P((DB *, const DBT *, db_pgno_t *,
175 * PUBLIC: int (*)(DB *, u_int32_t, PAGE **)));
178 __db_poff(dbp, dbt, pgnop, newfunc)
182 int (*newfunc) __P((DB *, u_int32_t, PAGE **));
185 DB_LSN new_lsn, null_lsn;
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
197 pagespace = P_MAXSPACE(dbp->pgsize);
201 sz = dbt->size; sz > 0; p += pagespace, sz -= pagespace) {
203 * Reduce pagespace so we terminate the loop correctly and
204 * don't copy too much data.
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.
214 if ((ret = newfunc(dbp, P_OVERFLOW, &pagep)) != 0)
216 if (DB_LOGGING(dbp)) {
218 tmp_dbt.size = pagespace;
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),
228 /* Move lsn onto page. */
230 LSN(lastp) = new_lsn;
231 LSN(pagep) = new_lsn;
234 P_INIT(pagep, dbp->pgsize,
235 PGNO(pagep), PGNO_INVALID, PGNO_INVALID, 0, P_OVERFLOW);
236 OV_LEN(pagep) = pagespace;
238 memcpy((u_int8_t *)pagep + P_OVERHEAD, p, pagespace);
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.
246 *pgnop = PGNO(pagep);
248 lastp->next_pgno = PGNO(pagep);
249 pagep->prev_pgno = PGNO(lastp);
250 (void)memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
254 (void)memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
260 * Increment the reference count on an overflow page.
262 * PUBLIC: int __db_ioff __P((DB *, db_pgno_t));
272 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
273 (void)__db_pgerr(dbp, pgno);
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)
282 (void)memp_fput(dbp->mpf, h, DB_MPOOL_DIRTY);
288 * Delete an offpage chain of overflow pages.
290 * PUBLIC: int __db_doff __P((DB *, db_pgno_t, int (*)(DB *, PAGE *)));
293 __db_doff(dbp, pgno, freefunc)
296 int (*freefunc) __P((DB *, PAGE *));
304 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0) {
305 (void)__db_pgerr(dbp, pgno);
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.
313 if (TYPE(pagep) == P_OVERFLOW && OV_REF(pagep) > 1) {
315 (void)memp_fput(dbp->mpf, pagep, DB_MPOOL_DIRTY);
319 if (DB_LOGGING(dbp)) {
320 tmp_dbt.data = (u_int8_t *)pagep + P_OVERHEAD;
321 tmp_dbt.size = OV_LEN(pagep);
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)
329 pgno = pagep->next_pgno;
330 if ((ret = freefunc(dbp, pagep)) != 0)
332 } while (pgno != PGNO_INVALID);
339 * Match on overflow pages.
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.
344 * PUBLIC: int __db_moff __P((DB *, const DBT *, db_pgno_t));
347 __db_moff(dbp, dbt, pgno)
353 u_int32_t cmp_bytes, key_left;
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. */
365 cmp_bytes = OV_LEN(pagep) < key_left ? OV_LEN(pagep) : key_left;
366 key_left -= cmp_bytes;
368 (u_int8_t *)pagep + P_OVERHEAD; cmp_bytes-- > 0; ++p1, ++p2)
370 ret = (long)*p1 - (long)*p2;
373 pgno = NEXT_PGNO(pagep);
374 (void)memp_fput(dbp->mpf, pagep, 0);
378 if (key_left > 0) /* DBT is longer than page key. */
380 if (pgno != PGNO_INVALID) /* DBT is shorter than page key. */