1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1996, 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1990, 1993, 1994, 1995, 1996
1N/A * Keith Bostic. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1990, 1993, 1994, 1995
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * This code is derived from software contributed to Berkeley by
1N/A * Mike Olson.
1N/A *
1N/A * Redistribution and use in source and binary forms, with or without
1N/A * modification, are permitted provided that the following conditions
1N/A * are met:
1N/A * 1. Redistributions of source code must retain the above copyright
1N/A * notice, this list of conditions and the following disclaimer.
1N/A * 2. Redistributions in binary form must reproduce the above copyright
1N/A * notice, this list of conditions and the following disclaimer in the
1N/A * documentation and/or other materials provided with the distribution.
1N/A * 3. All advertising materials mentioning features or use of this software
1N/A * must display the following acknowledgement:
1N/A * This product includes software developed by the University of
1N/A * California, Berkeley and its contributors.
1N/A * 4. Neither the name of the University nor the names of its contributors
1N/A * may be used to endorse or promote products derived from this software
1N/A * without specific prior written permission.
1N/A *
1N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1N/A * SUCH DAMAGE.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)db_overflow.c 10.21 (Sleepycat) 9/27/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <string.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "db_page.h"
1N/A#include "db_am.h"
1N/A#include "common_ext.h"
1N/A
1N/A/*
1N/A * Big key/data code.
1N/A *
1N/A * Big key and data entries are stored on linked lists of pages. The initial
1N/A * reference is a structure with the total length of the item and the page
1N/A * number where it begins. Each entry in the linked list contains a pointer
1N/A * to the next page of data, and so on.
1N/A */
1N/A
1N/A/*
1N/A * __db_goff --
1N/A * Get an offpage item.
1N/A *
1N/A * PUBLIC: int __db_goff __P((DB *, DBT *,
1N/A * PUBLIC: u_int32_t, db_pgno_t, void **, u_int32_t *));
1N/A */
1N/Aint
1N/A__db_goff(dbp, dbt, tlen, pgno, bpp, bpsz)
1N/A DB *dbp;
1N/A DBT *dbt;
1N/A u_int32_t tlen;
1N/A db_pgno_t pgno;
1N/A void **bpp;
1N/A u_int32_t *bpsz;
1N/A{
1N/A PAGE *h;
1N/A db_indx_t bytes;
1N/A u_int32_t curoff, needed, start;
1N/A u_int8_t *p, *src;
1N/A int ret;
1N/A
1N/A /*
1N/A * Check if the buffer is big enough; if it is not and we are
1N/A * allowed to malloc space, then we'll malloc it. If we are
1N/A * not (DB_DBT_USERMEM), then we'll set the dbt and return
1N/A * appropriately.
1N/A */
1N/A if (F_ISSET(dbt, DB_DBT_PARTIAL)) {
1N/A start = dbt->doff;
1N/A needed = dbt->dlen;
1N/A } else {
1N/A start = 0;
1N/A needed = tlen;
1N/A }
1N/A
1N/A /* Allocate any necessary memory. */
1N/A if (F_ISSET(dbt, DB_DBT_USERMEM)) {
1N/A if (needed > dbt->ulen) {
1N/A dbt->size = needed;
1N/A return (ENOMEM);
1N/A }
1N/A } else if (F_ISSET(dbt, DB_DBT_MALLOC)) {
1N/A if ((ret =
1N/A __os_malloc(needed, dbp->db_malloc, &dbt->data)) != 0)
1N/A return (ret);
1N/A } else if (*bpsz == 0 || *bpsz < needed) {
1N/A if ((ret = __os_realloc(bpp, needed)) != 0)
1N/A return (ret);
1N/A *bpsz = needed;
1N/A dbt->data = *bpp;
1N/A } else
1N/A dbt->data = *bpp;
1N/A
1N/A /*
1N/A * Step through the linked list of pages, copying the data on each
1N/A * one into the buffer. Never copy more than the total data length.
1N/A */
1N/A dbt->size = needed;
1N/A for (curoff = 0, p = dbt->data; pgno != P_INVALID && needed > 0;) {
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
1N/A (void)__db_pgerr(dbp, pgno);
1N/A return (ret);
1N/A }
1N/A /* Check if we need any bytes from this page. */
1N/A if (curoff + OV_LEN(h) >= start) {
1N/A src = (u_int8_t *)h + P_OVERHEAD;
1N/A bytes = OV_LEN(h);
1N/A if (start > curoff) {
1N/A src += start - curoff;
1N/A bytes -= start - curoff;
1N/A }
1N/A if (bytes > needed)
1N/A bytes = needed;
1N/A memcpy(p, src, bytes);
1N/A p += bytes;
1N/A needed -= bytes;
1N/A }
1N/A curoff += OV_LEN(h);
1N/A pgno = h->next_pgno;
1N/A memp_fput(dbp->mpf, h, 0);
1N/A }
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_poff --
1N/A * Put an offpage item.
1N/A *
1N/A * PUBLIC: int __db_poff __P((DBC *, const DBT *, db_pgno_t *,
1N/A * PUBLIC: int (*)(DBC *, u_int32_t, PAGE **)));
1N/A */
1N/Aint
1N/A__db_poff(dbc, dbt, pgnop, newfunc)
1N/A DBC *dbc;
1N/A const DBT *dbt;
1N/A db_pgno_t *pgnop;
1N/A int (*newfunc) __P((DBC *, u_int32_t, PAGE **));
1N/A{
1N/A DB *dbp;
1N/A PAGE *pagep, *lastp;
1N/A DB_LSN new_lsn, null_lsn;
1N/A DBT tmp_dbt;
1N/A db_indx_t pagespace;
1N/A u_int32_t sz;
1N/A u_int8_t *p;
1N/A int ret;
1N/A
1N/A /*
1N/A * Allocate pages and copy the key/data item into them. Calculate the
1N/A * number of bytes we get for pages we fill completely with a single
1N/A * item.
1N/A */
1N/A dbp = dbc->dbp;
1N/A pagespace = P_MAXSPACE(dbp->pgsize);
1N/A
1N/A lastp = NULL;
1N/A for (p = dbt->data,
1N/A sz = dbt->size; sz > 0; p += pagespace, sz -= pagespace) {
1N/A /*
1N/A * Reduce pagespace so we terminate the loop correctly and
1N/A * don't copy too much data.
1N/A */
1N/A if (sz < pagespace)
1N/A pagespace = sz;
1N/A
1N/A /*
1N/A * Allocate and initialize a new page and copy all or part of
1N/A * the item onto the page. If sz is less than pagespace, we
1N/A * have a partial record.
1N/A */
1N/A if ((ret = newfunc(dbc, P_OVERFLOW, &pagep)) != 0)
1N/A return (ret);
1N/A if (DB_LOGGING(dbc)) {
1N/A tmp_dbt.data = p;
1N/A tmp_dbt.size = pagespace;
1N/A ZERO_LSN(null_lsn);
1N/A if ((ret = __db_big_log(dbp->dbenv->lg_info, dbc->txn,
1N/A &new_lsn, 0, DB_ADD_BIG, dbp->log_fileid,
1N/A PGNO(pagep), lastp ? PGNO(lastp) : PGNO_INVALID,
1N/A PGNO_INVALID, &tmp_dbt, &LSN(pagep),
1N/A lastp == NULL ? &null_lsn : &LSN(lastp),
1N/A &null_lsn)) != 0)
1N/A return (ret);
1N/A
1N/A /* Move lsn onto page. */
1N/A if (lastp)
1N/A LSN(lastp) = new_lsn;
1N/A LSN(pagep) = new_lsn;
1N/A }
1N/A
1N/A P_INIT(pagep, dbp->pgsize,
1N/A PGNO(pagep), PGNO_INVALID, PGNO_INVALID, 0, P_OVERFLOW);
1N/A OV_LEN(pagep) = pagespace;
1N/A OV_REF(pagep) = 1;
1N/A memcpy((u_int8_t *)pagep + P_OVERHEAD, p, pagespace);
1N/A
1N/A /*
1N/A * If this is the first entry, update the user's info.
1N/A * Otherwise, update the entry on the last page filled
1N/A * in and release that page.
1N/A */
1N/A if (lastp == NULL)
1N/A *pgnop = PGNO(pagep);
1N/A else {
1N/A lastp->next_pgno = PGNO(pagep);
1N/A pagep->prev_pgno = PGNO(lastp);
1N/A (void)memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
1N/A }
1N/A lastp = pagep;
1N/A }
1N/A (void)memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_ovref --
1N/A * Increment/decrement the reference count on an overflow page.
1N/A *
1N/A * PUBLIC: int __db_ovref __P((DBC *, db_pgno_t, int32_t));
1N/A */
1N/Aint
1N/A__db_ovref(dbc, pgno, adjust)
1N/A DBC *dbc;
1N/A db_pgno_t pgno;
1N/A int32_t adjust;
1N/A{
1N/A DB *dbp;
1N/A PAGE *h;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
1N/A (void)__db_pgerr(dbp, pgno);
1N/A return (ret);
1N/A }
1N/A
1N/A if (DB_LOGGING(dbc))
1N/A if ((ret = __db_ovref_log(dbp->dbenv->lg_info, dbc->txn,
1N/A &LSN(h), 0, dbp->log_fileid, h->pgno, adjust,
1N/A &LSN(h))) != 0)
1N/A return (ret);
1N/A OV_REF(h) += adjust;
1N/A
1N/A (void)memp_fput(dbp->mpf, h, DB_MPOOL_DIRTY);
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_doff --
1N/A * Delete an offpage chain of overflow pages.
1N/A *
1N/A * PUBLIC: int __db_doff __P((DBC *, db_pgno_t, int (*)(DBC *, PAGE *)));
1N/A */
1N/Aint
1N/A__db_doff(dbc, pgno, freefunc)
1N/A DBC *dbc;
1N/A db_pgno_t pgno;
1N/A int (*freefunc) __P((DBC *, PAGE *));
1N/A{
1N/A DB *dbp;
1N/A PAGE *pagep;
1N/A DB_LSN null_lsn;
1N/A DBT tmp_dbt;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A do {
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0) {
1N/A (void)__db_pgerr(dbp, pgno);
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * If it's an overflow page and it's referenced by more than
1N/A * one key/data item, decrement the reference count and return.
1N/A */
1N/A if (TYPE(pagep) == P_OVERFLOW && OV_REF(pagep) > 1) {
1N/A (void)memp_fput(dbp->mpf, pagep, 0);
1N/A return (__db_ovref(dbc, pgno, -1));
1N/A }
1N/A
1N/A if (DB_LOGGING(dbc)) {
1N/A tmp_dbt.data = (u_int8_t *)pagep + P_OVERHEAD;
1N/A tmp_dbt.size = OV_LEN(pagep);
1N/A ZERO_LSN(null_lsn);
1N/A if ((ret = __db_big_log(dbp->dbenv->lg_info, dbc->txn,
1N/A &LSN(pagep), 0, DB_REM_BIG, dbp->log_fileid,
1N/A PGNO(pagep), PREV_PGNO(pagep), NEXT_PGNO(pagep),
1N/A &tmp_dbt, &LSN(pagep), &null_lsn, &null_lsn)) != 0)
1N/A return (ret);
1N/A }
1N/A pgno = pagep->next_pgno;
1N/A if ((ret = freefunc(dbc, pagep)) != 0)
1N/A return (ret);
1N/A } while (pgno != PGNO_INVALID);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_moff --
1N/A * Match on overflow pages.
1N/A *
1N/A * Given a starting page number and a key, return <0, 0, >0 to indicate if the
1N/A * key on the page is less than, equal to or greater than the key specified.
1N/A * We optimize this by doing chunk at a time comparison unless the user has
1N/A * specified a comparison function. In this case, we need to materialize
1N/A * the entire object and call their comparison routine.
1N/A *
1N/A * PUBLIC: int __db_moff __P((DB *, const DBT *, db_pgno_t, u_int32_t,
1N/A * PUBLIC: int (*)(const DBT *, const DBT *), int *));
1N/A */
1N/Aint
1N/A__db_moff(dbp, dbt, pgno, tlen, cmpfunc, cmpp)
1N/A DB *dbp;
1N/A const DBT *dbt;
1N/A db_pgno_t pgno;
1N/A u_int32_t tlen;
1N/A int (*cmpfunc) __P((const DBT *, const DBT *)), *cmpp;
1N/A{
1N/A PAGE *pagep;
1N/A DBT local_dbt;
1N/A void *buf;
1N/A u_int32_t bufsize, cmp_bytes, key_left;
1N/A u_int8_t *p1, *p2;
1N/A int ret;
1N/A
1N/A /*
1N/A * If there is a user-specified comparison function, build a
1N/A * contiguous copy of the key, and call it.
1N/A */
1N/A if (cmpfunc != NULL) {
1N/A memset(&local_dbt, 0, sizeof(local_dbt));
1N/A buf = NULL;
1N/A bufsize = 0;
1N/A
1N/A if ((ret = __db_goff(dbp,
1N/A &local_dbt, tlen, pgno, &buf, &bufsize)) != 0)
1N/A return (ret);
1N/A *cmpp = cmpfunc(&local_dbt, dbt);
1N/A __os_free(buf, bufsize);
1N/A return (0);
1N/A }
1N/A
1N/A /* While there are both keys to compare. */
1N/A for (*cmpp = 0, p1 = dbt->data,
1N/A key_left = dbt->size; key_left > 0 && pgno != PGNO_INVALID;) {
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0)
1N/A return (ret);
1N/A
1N/A cmp_bytes = OV_LEN(pagep) < key_left ? OV_LEN(pagep) : key_left;
1N/A key_left -= cmp_bytes;
1N/A for (p2 =
1N/A (u_int8_t *)pagep + P_OVERHEAD; cmp_bytes-- > 0; ++p1, ++p2)
1N/A if (*p1 != *p2) {
1N/A *cmpp = (long)*p1 - (long)*p2;
1N/A break;
1N/A }
1N/A pgno = NEXT_PGNO(pagep);
1N/A if ((ret = memp_fput(dbp->mpf, pagep, 0)) != 0)
1N/A return (ret);
1N/A if (*cmpp != 0)
1N/A return (0);
1N/A }
1N/A if (key_left > 0) /* DBT is longer than page key. */
1N/A *cmpp = -1;
1N/A else if (pgno != PGNO_INVALID) /* DBT is shorter than page key. */
1N/A *cmpp = 1;
1N/A else
1N/A *cmpp = 0;
1N/A
1N/A return (0);
1N/A}