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
1N/A * Margo Seltzer. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1990, 1993, 1994
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 * Margo Seltzer.
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[] = "@(#)hash_page.c 10.55 (Sleepycat) 1/3/99";
1N/A#endif /* not lint */
1N/A
1N/A/*
1N/A * PACKAGE: hashing
1N/A *
1N/A * DESCRIPTION:
1N/A * Page manipulation for hashing package.
1N/A *
1N/A * ROUTINES:
1N/A *
1N/A * External
1N/A * __get_page
1N/A * __add_ovflpage
1N/A * __overflow_page
1N/A * Internal
1N/A * open_temp
1N/A */
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 "hash.h"
1N/A
1N/Astatic int __ham_lock_bucket __P((DBC *, db_lockmode_t));
1N/A
1N/A#ifdef DEBUG_SLOW
1N/Astatic void __account_page(DB *, db_pgno_t, int);
1N/A#endif
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_item __P((DBC *, db_lockmode_t));
1N/A */
1N/Aint
1N/A__ham_item(dbc, mode)
1N/A DBC *dbc;
1N/A db_lockmode_t mode;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A db_pgno_t next_pgno;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A
1N/A if (F_ISSET(hcp, H_DELETED))
1N/A return (EINVAL);
1N/A F_CLR(hcp, H_OK | H_NOMORE);
1N/A
1N/A /* Check if we need to get a page for this cursor. */
1N/A if ((ret = __ham_get_cpage(dbc, mode)) != 0)
1N/A return (ret);
1N/A
1N/A /* Check if we are looking for space in which to insert an item. */
1N/A if (hcp->seek_size && hcp->seek_found_page == PGNO_INVALID
1N/A && hcp->seek_size < P_FREESPACE(hcp->pagep))
1N/A hcp->seek_found_page = hcp->pgno;
1N/A
1N/A /* Check if we need to go on to the next page. */
1N/A if (F_ISSET(hcp, H_ISDUP) && hcp->dpgno == PGNO_INVALID)
1N/A /*
1N/A * ISDUP is set, and offset is at the beginning of the datum.
1N/A * We need to grab the length of the datum, then set the datum
1N/A * pointer to be the beginning of the datum.
1N/A */
1N/A memcpy(&hcp->dup_len,
1N/A HKEYDATA_DATA(H_PAIRDATA(hcp->pagep, hcp->bndx)) +
1N/A hcp->dup_off, sizeof(db_indx_t));
1N/A else if (F_ISSET(hcp, H_ISDUP)) {
1N/A /* Make sure we're not about to run off the page. */
1N/A if (hcp->dpagep == NULL && (ret = __ham_get_page(dbp,
1N/A hcp->dpgno, &hcp->dpagep)) != 0)
1N/A return (ret);
1N/A
1N/A if (hcp->dndx >= NUM_ENT(hcp->dpagep)) {
1N/A if (NEXT_PGNO(hcp->dpagep) == PGNO_INVALID) {
1N/A if (F_ISSET(hcp, H_DUPONLY)) {
1N/A F_CLR(hcp, H_OK);
1N/A F_SET(hcp, H_NOMORE);
1N/A return (0);
1N/A }
1N/A if ((ret = __ham_put_page(dbp,
1N/A hcp->dpagep, 0)) != 0)
1N/A return (ret);
1N/A F_CLR(hcp, H_ISDUP);
1N/A hcp->dpagep = NULL;
1N/A hcp->dpgno = PGNO_INVALID;
1N/A hcp->dndx = NDX_INVALID;
1N/A hcp->bndx++;
1N/A } else if ((ret = __ham_next_cpage(dbc,
1N/A NEXT_PGNO(hcp->dpagep), 0, H_ISDUP)) != 0)
1N/A return (ret);
1N/A }
1N/A }
1N/A
1N/A if (hcp->bndx >= (db_indx_t)H_NUMPAIRS(hcp->pagep)) {
1N/A /* Fetch next page. */
1N/A if (NEXT_PGNO(hcp->pagep) == PGNO_INVALID) {
1N/A F_SET(hcp, H_NOMORE);
1N/A if (hcp->dpagep != NULL &&
1N/A (ret = __ham_put_page(dbp, hcp->dpagep, 0)) != 0)
1N/A return (ret);
1N/A hcp->dpgno = PGNO_INVALID;
1N/A return (DB_NOTFOUND);
1N/A }
1N/A next_pgno = NEXT_PGNO(hcp->pagep);
1N/A hcp->bndx = 0;
1N/A if ((ret = __ham_next_cpage(dbc, next_pgno, 0, 0)) != 0)
1N/A return (ret);
1N/A }
1N/A
1N/A F_SET(hcp, H_OK);
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_item_reset __P((DBC *));
1N/A */
1N/Aint
1N/A__ham_item_reset(dbc)
1N/A DBC *dbc;
1N/A{
1N/A HASH_CURSOR *hcp;
1N/A DB *dbp;
1N/A int ret;
1N/A
1N/A ret = 0;
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A if (hcp->pagep != NULL)
1N/A ret = __ham_put_page(dbp, hcp->pagep, 0);
1N/A if (ret == 0 && hcp->dpagep != NULL)
1N/A ret = __ham_put_page(dbp, hcp->dpagep, 0);
1N/A
1N/A __ham_item_init(hcp);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: void __ham_item_init __P((HASH_CURSOR *));
1N/A */
1N/Avoid
1N/A__ham_item_init(hcp)
1N/A HASH_CURSOR *hcp;
1N/A{
1N/A /*
1N/A * If this cursor still holds any locks, we must
1N/A * release them if we are not running with transactions.
1N/A */
1N/A if (hcp->lock && hcp->dbc->txn == NULL)
1N/A (void)lock_put(hcp->dbc->dbp->dbenv->lk_info, hcp->lock);
1N/A
1N/A /*
1N/A * The following fields must *not* be initialized here
1N/A * because they may have meaning across inits.
1N/A * hlock, hdr, split_buf, stats
1N/A */
1N/A hcp->bucket = BUCKET_INVALID;
1N/A hcp->lbucket = BUCKET_INVALID;
1N/A hcp->lock = 0;
1N/A hcp->pagep = NULL;
1N/A hcp->pgno = PGNO_INVALID;
1N/A hcp->bndx = NDX_INVALID;
1N/A hcp->dpagep = NULL;
1N/A hcp->dpgno = PGNO_INVALID;
1N/A hcp->dndx = NDX_INVALID;
1N/A hcp->dup_off = 0;
1N/A hcp->dup_len = 0;
1N/A hcp->dup_tlen = 0;
1N/A hcp->seek_size = 0;
1N/A hcp->seek_found_page = PGNO_INVALID;
1N/A hcp->flags = 0;
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_item_done __P((DBC *, int));
1N/A */
1N/Aint
1N/A__ham_item_done(dbc, dirty)
1N/A DBC *dbc;
1N/A int dirty;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A int ret, t_ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A t_ret = ret = 0;
1N/A
1N/A if (hcp->pagep)
1N/A ret = __ham_put_page(dbp, hcp->pagep,
1N/A dirty && hcp->dpagep == NULL);
1N/A hcp->pagep = NULL;
1N/A
1N/A if (hcp->dpagep)
1N/A t_ret = __ham_put_page(dbp, hcp->dpagep, dirty);
1N/A hcp->dpagep = NULL;
1N/A
1N/A if (ret == 0 && t_ret != 0)
1N/A ret = t_ret;
1N/A
1N/A /*
1N/A * We don't throw out the page number since we might want to
1N/A * continue getting on this page.
1N/A */
1N/A return (ret != 0 ? ret : t_ret);
1N/A}
1N/A
1N/A/*
1N/A * Returns the last item in a bucket.
1N/A *
1N/A * PUBLIC: int __ham_item_last __P((DBC *, db_lockmode_t));
1N/A */
1N/Aint
1N/A__ham_item_last(dbc, mode)
1N/A DBC *dbc;
1N/A db_lockmode_t mode;
1N/A{
1N/A HASH_CURSOR *hcp;
1N/A int ret;
1N/A
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A if ((ret = __ham_item_reset(dbc)) != 0)
1N/A return (ret);
1N/A
1N/A hcp->bucket = hcp->hdr->max_bucket;
1N/A F_SET(hcp, H_OK);
1N/A return (__ham_item_prev(dbc, mode));
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_item_first __P((DBC *, db_lockmode_t));
1N/A */
1N/Aint
1N/A__ham_item_first(dbc, mode)
1N/A DBC *dbc;
1N/A db_lockmode_t mode;
1N/A{
1N/A HASH_CURSOR *hcp;
1N/A int ret;
1N/A
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A if ((ret = __ham_item_reset(dbc)) != 0)
1N/A return (ret);
1N/A F_SET(hcp, H_OK);
1N/A hcp->bucket = 0;
1N/A return (__ham_item_next(dbc, mode));
1N/A}
1N/A
1N/A/*
1N/A * __ham_item_prev --
1N/A * Returns a pointer to key/data pair on a page. In the case of
1N/A * bigkeys, just returns the page number and index of the bigkey
1N/A * pointer pair.
1N/A *
1N/A * PUBLIC: int __ham_item_prev __P((DBC *, db_lockmode_t));
1N/A */
1N/Aint
1N/A__ham_item_prev(dbc, mode)
1N/A DBC *dbc;
1N/A db_lockmode_t mode;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A db_pgno_t next_pgno;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A /*
1N/A * There are N cases for backing up in a hash file.
1N/A * Case 1: In the middle of a page, no duplicates, just dec the index.
1N/A * Case 2: In the middle of a duplicate set, back up one.
1N/A * Case 3: At the beginning of a duplicate set, get out of set and
1N/A * back up to next key.
1N/A * Case 4: At the beginning of a page; go to previous page.
1N/A * Case 5: At the beginning of a bucket; go to prev bucket.
1N/A */
1N/A F_CLR(hcp, H_OK | H_NOMORE | H_DELETED);
1N/A
1N/A /*
1N/A * First handle the duplicates. Either you'll get the key here
1N/A * or you'll exit the duplicate set and drop into the code below
1N/A * to handle backing up through keys.
1N/A */
1N/A if (F_ISSET(hcp, H_ISDUP)) {
1N/A if (hcp->dpgno == PGNO_INVALID) {
1N/A /* Duplicates are on-page. */
1N/A if (hcp->dup_off != 0)
1N/A if ((ret = __ham_get_cpage(dbc, mode)) != 0)
1N/A return (ret);
1N/A else {
1N/A HASH_CURSOR *h;
1N/A h = hcp;
1N/A memcpy(&h->dup_len, HKEYDATA_DATA(
1N/A H_PAIRDATA(h->pagep, h->bndx))
1N/A + h->dup_off - sizeof(db_indx_t),
1N/A sizeof(db_indx_t));
1N/A hcp->dup_off -=
1N/A DUP_SIZE(hcp->dup_len);
1N/A hcp->dndx--;
1N/A return (__ham_item(dbc, mode));
1N/A }
1N/A } else if (hcp->dndx > 0) { /* Duplicates are off-page. */
1N/A hcp->dndx--;
1N/A return (__ham_item(dbc, mode));
1N/A } else if ((ret = __ham_get_cpage(dbc, mode)) != 0)
1N/A return (ret);
1N/A else if (PREV_PGNO(hcp->dpagep) == PGNO_INVALID) {
1N/A if (F_ISSET(hcp, H_DUPONLY)) {
1N/A F_CLR(hcp, H_OK);
1N/A F_SET(hcp, H_NOMORE);
1N/A return (0);
1N/A } else {
1N/A F_CLR(hcp, H_ISDUP); /* End of dups */
1N/A hcp->dpgno = PGNO_INVALID;
1N/A if (hcp->dpagep != NULL)
1N/A (void)__ham_put_page(dbp,
1N/A hcp->dpagep, 0);
1N/A hcp->dpagep = NULL;
1N/A }
1N/A } else if ((ret = __ham_next_cpage(dbc,
1N/A PREV_PGNO(hcp->dpagep), 0, H_ISDUP)) != 0)
1N/A return (ret);
1N/A else {
1N/A hcp->dndx = NUM_ENT(hcp->pagep) - 1;
1N/A return (__ham_item(dbc, mode));
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * If we get here, we are not in a duplicate set, and just need
1N/A * to back up the cursor. There are still three cases:
1N/A * midpage, beginning of page, beginning of bucket.
1N/A */
1N/A
1N/A if (F_ISSET(hcp, H_DUPONLY)) {
1N/A F_CLR(hcp, H_OK);
1N/A F_SET(hcp, H_NOMORE);
1N/A return (0);
1N/A }
1N/A
1N/A if (hcp->bndx == 0) { /* Beginning of page. */
1N/A if ((ret = __ham_get_cpage(dbc, mode)) != 0)
1N/A return (ret);
1N/A hcp->pgno = PREV_PGNO(hcp->pagep);
1N/A if (hcp->pgno == PGNO_INVALID) {
1N/A /* Beginning of bucket. */
1N/A F_SET(hcp, H_NOMORE);
1N/A return (DB_NOTFOUND);
1N/A } else if ((ret =
1N/A __ham_next_cpage(dbc, hcp->pgno, 0, 0)) != 0)
1N/A return (ret);
1N/A else
1N/A hcp->bndx = H_NUMPAIRS(hcp->pagep);
1N/A }
1N/A
1N/A /*
1N/A * Either we've got the cursor set up to be decremented, or we
1N/A * have to find the end of a bucket.
1N/A */
1N/A if (hcp->bndx == NDX_INVALID) {
1N/A if (hcp->pagep == NULL)
1N/A next_pgno = BUCKET_TO_PAGE(hcp, hcp->bucket);
1N/A else
1N/A goto got_page;
1N/A
1N/A do {
1N/A if ((ret = __ham_next_cpage(dbc, next_pgno, 0, 0)) != 0)
1N/A return (ret);
1N/Agot_page: next_pgno = NEXT_PGNO(hcp->pagep);
1N/A hcp->bndx = H_NUMPAIRS(hcp->pagep);
1N/A } while (next_pgno != PGNO_INVALID);
1N/A
1N/A if (hcp->bndx == 0) {
1N/A /* Bucket was empty. */
1N/A F_SET(hcp, H_NOMORE);
1N/A return (DB_NOTFOUND);
1N/A }
1N/A }
1N/A
1N/A hcp->bndx--;
1N/A
1N/A return (__ham_item(dbc, mode));
1N/A}
1N/A
1N/A/*
1N/A * Sets the cursor to the next key/data pair on a page.
1N/A *
1N/A * PUBLIC: int __ham_item_next __P((DBC *, db_lockmode_t));
1N/A */
1N/Aint
1N/A__ham_item_next(dbc, mode)
1N/A DBC *dbc;
1N/A db_lockmode_t mode;
1N/A{
1N/A HASH_CURSOR *hcp;
1N/A
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A /*
1N/A * Deleted on-page duplicates are a weird case. If we delete the last
1N/A * one, then our cursor is at the very end of a duplicate set and
1N/A * we actually need to go on to the next key.
1N/A */
1N/A if (F_ISSET(hcp, H_DELETED)) {
1N/A if (hcp->bndx != NDX_INVALID &&
1N/A F_ISSET(hcp, H_ISDUP) &&
1N/A hcp->dpgno == PGNO_INVALID &&
1N/A hcp->dup_tlen == hcp->dup_off) {
1N/A if (F_ISSET(hcp, H_DUPONLY)) {
1N/A F_CLR(hcp, H_OK);
1N/A F_SET(hcp, H_NOMORE);
1N/A return (0);
1N/A } else {
1N/A F_CLR(hcp, H_ISDUP);
1N/A hcp->dpgno = PGNO_INVALID;
1N/A hcp->bndx++;
1N/A }
1N/A } else if (!F_ISSET(hcp, H_ISDUP) &&
1N/A F_ISSET(hcp, H_DUPONLY)) {
1N/A F_CLR(hcp, H_OK);
1N/A F_SET(hcp, H_NOMORE);
1N/A return (0);
1N/A }
1N/A F_CLR(hcp, H_DELETED);
1N/A } else if (hcp->bndx == NDX_INVALID) {
1N/A hcp->bndx = 0;
1N/A hcp->dpgno = PGNO_INVALID;
1N/A F_CLR(hcp, H_ISDUP);
1N/A } else if (F_ISSET(hcp, H_ISDUP) && hcp->dpgno != PGNO_INVALID)
1N/A hcp->dndx++;
1N/A else if (F_ISSET(hcp, H_ISDUP)) {
1N/A if (hcp->dup_off + DUP_SIZE(hcp->dup_len) >=
1N/A hcp->dup_tlen && F_ISSET(hcp, H_DUPONLY)) {
1N/A F_CLR(hcp, H_OK);
1N/A F_SET(hcp, H_NOMORE);
1N/A return (0);
1N/A }
1N/A hcp->dndx++;
1N/A hcp->dup_off += DUP_SIZE(hcp->dup_len);
1N/A if (hcp->dup_off >= hcp->dup_tlen) {
1N/A F_CLR(hcp, H_ISDUP);
1N/A hcp->dpgno = PGNO_INVALID;
1N/A hcp->bndx++;
1N/A }
1N/A } else if (F_ISSET(hcp, H_DUPONLY)) {
1N/A F_CLR(hcp, H_OK);
1N/A F_SET(hcp, H_NOMORE);
1N/A return (0);
1N/A } else
1N/A hcp->bndx++;
1N/A
1N/A return (__ham_item(dbc, mode));
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: void __ham_putitem __P((PAGE *p, const DBT *, int));
1N/A *
1N/A * This is a little bit sleazy in that we're overloading the meaning
1N/A * of the H_OFFPAGE type here. When we recover deletes, we have the
1N/A * entire entry instead of having only the DBT, so we'll pass type
1N/A * H_OFFPAGE to mean, "copy the whole entry" as opposed to constructing
1N/A * an H_KEYDATA around it.
1N/A */
1N/Avoid
1N/A__ham_putitem(p, dbt, type)
1N/A PAGE *p;
1N/A const DBT *dbt;
1N/A int type;
1N/A{
1N/A u_int16_t n, off;
1N/A
1N/A n = NUM_ENT(p);
1N/A
1N/A /* Put the item element on the page. */
1N/A if (type == H_OFFPAGE) {
1N/A off = HOFFSET(p) - dbt->size;
1N/A HOFFSET(p) = p->inp[n] = off;
1N/A memcpy(P_ENTRY(p, n), dbt->data, dbt->size);
1N/A } else {
1N/A off = HOFFSET(p) - HKEYDATA_SIZE(dbt->size);
1N/A HOFFSET(p) = p->inp[n] = off;
1N/A PUT_HKEYDATA(P_ENTRY(p, n), dbt->data, dbt->size, type);
1N/A }
1N/A
1N/A /* Adjust page info. */
1N/A NUM_ENT(p) += 1;
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: void __ham_reputpair
1N/A * PUBLIC: __P((PAGE *p, u_int32_t, u_int32_t, const DBT *, const DBT *));
1N/A *
1N/A * This is a special case to restore a key/data pair to its original
1N/A * location during recovery. We are guaranteed that the pair fits
1N/A * on the page and is not the last pair on the page (because if it's
1N/A * the last pair, the normal insert works).
1N/A */
1N/Avoid
1N/A__ham_reputpair(p, psize, ndx, key, data)
1N/A PAGE *p;
1N/A u_int32_t psize, ndx;
1N/A const DBT *key, *data;
1N/A{
1N/A db_indx_t i, movebytes, newbytes;
1N/A u_int8_t *from;
1N/A
1N/A /* First shuffle the existing items up on the page. */
1N/A movebytes =
1N/A (ndx == 0 ? psize : p->inp[H_DATAINDEX(ndx - 1)]) - HOFFSET(p);
1N/A newbytes = key->size + data->size;
1N/A from = (u_int8_t *)p + HOFFSET(p);
1N/A memmove(from - newbytes, from, movebytes);
1N/A
1N/A /*
1N/A * Adjust the indices and move them up 2 spaces. Note that we
1N/A * have to check the exit condition inside the loop just in case
1N/A * we are dealing with index 0 (db_indx_t's are unsigned).
1N/A */
1N/A for (i = NUM_ENT(p) - 1; ; i-- ) {
1N/A p->inp[i + 2] = p->inp[i] - newbytes;
1N/A if (i == H_KEYINDEX(ndx))
1N/A break;
1N/A }
1N/A
1N/A /* Put the key and data on the page. */
1N/A p->inp[H_KEYINDEX(ndx)] =
1N/A (ndx == 0 ? psize : p->inp[H_DATAINDEX(ndx - 1)]) - key->size;
1N/A p->inp[H_DATAINDEX(ndx)] = p->inp[H_KEYINDEX(ndx)] - data->size;
1N/A memcpy(P_ENTRY(p, H_KEYINDEX(ndx)), key->data, key->size);
1N/A memcpy(P_ENTRY(p, H_DATAINDEX(ndx)), data->data, data->size);
1N/A
1N/A /* Adjust page info. */
1N/A HOFFSET(p) -= newbytes;
1N/A NUM_ENT(p) += 2;
1N/A}
1N/A
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_del_pair __P((DBC *, int));
1N/A */
1N/Aint
1N/A__ham_del_pair(dbc, reclaim_page)
1N/A DBC *dbc;
1N/A int reclaim_page;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A DBT data_dbt, key_dbt;
1N/A DB_ENV *dbenv;
1N/A DB_LSN new_lsn, *n_lsn, tmp_lsn;
1N/A PAGE *p;
1N/A db_indx_t ndx;
1N/A db_pgno_t chg_pgno, pgno;
1N/A int ret, tret;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A
1N/A dbenv = dbp->dbenv;
1N/A ndx = hcp->bndx;
1N/A if (hcp->pagep == NULL &&
1N/A (ret = __ham_get_page(dbp, hcp->pgno, &hcp->pagep)) != 0)
1N/A return (ret);
1N/A
1N/A p = hcp->pagep;
1N/A
1N/A /*
1N/A * We optimize for the normal case which is when neither the key nor
1N/A * the data are large. In this case, we write a single log record
1N/A * and do the delete. If either is large, we'll call __big_delete
1N/A * to remove the big item and then update the page to remove the
1N/A * entry referring to the big item.
1N/A */
1N/A ret = 0;
1N/A if (HPAGE_PTYPE(H_PAIRKEY(p, ndx)) == H_OFFPAGE) {
1N/A memcpy(&pgno, HOFFPAGE_PGNO(P_ENTRY(p, H_KEYINDEX(ndx))),
1N/A sizeof(db_pgno_t));
1N/A ret = __db_doff(dbc, pgno, __ham_del_page);
1N/A }
1N/A
1N/A if (ret == 0)
1N/A switch (HPAGE_PTYPE(H_PAIRDATA(p, ndx))) {
1N/A case H_OFFPAGE:
1N/A memcpy(&pgno,
1N/A HOFFPAGE_PGNO(P_ENTRY(p, H_DATAINDEX(ndx))),
1N/A sizeof(db_pgno_t));
1N/A ret = __db_doff(dbc, pgno, __ham_del_page);
1N/A break;
1N/A case H_OFFDUP:
1N/A memcpy(&pgno,
1N/A HOFFDUP_PGNO(P_ENTRY(p, H_DATAINDEX(ndx))),
1N/A sizeof(db_pgno_t));
1N/A ret = __db_ddup(dbc, pgno, __ham_del_page);
1N/A F_CLR(hcp, H_ISDUP);
1N/A break;
1N/A case H_DUPLICATE:
1N/A /*
1N/A * If we delete a pair that is/was a duplicate, then
1N/A * we had better clear the flag so that we update the
1N/A * cursor appropriately.
1N/A */
1N/A F_CLR(hcp, H_ISDUP);
1N/A break;
1N/A }
1N/A
1N/A if (ret)
1N/A return (ret);
1N/A
1N/A /* Now log the delete off this page. */
1N/A if (DB_LOGGING(dbc)) {
1N/A key_dbt.data = P_ENTRY(p, H_KEYINDEX(ndx));
1N/A key_dbt.size =
1N/A LEN_HITEM(p, hcp->hdr->pagesize, H_KEYINDEX(ndx));
1N/A data_dbt.data = P_ENTRY(p, H_DATAINDEX(ndx));
1N/A data_dbt.size =
1N/A LEN_HITEM(p, hcp->hdr->pagesize, H_DATAINDEX(ndx));
1N/A
1N/A if ((ret = __ham_insdel_log(dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, DELPAIR,
1N/A dbp->log_fileid, PGNO(p), (u_int32_t)ndx,
1N/A &LSN(p), &key_dbt, &data_dbt)) != 0)
1N/A return (ret);
1N/A
1N/A /* Move lsn onto page. */
1N/A LSN(p) = new_lsn;
1N/A }
1N/A
1N/A __ham_dpair(dbp, p, ndx);
1N/A
1N/A /*
1N/A * If we are locking, we will not maintain this, because it is
1N/A * a hot spot.
1N/A * XXX perhaps we can retain incremental numbers and apply them
1N/A * later.
1N/A */
1N/A if (!F_ISSET(dbp, DB_AM_LOCKING))
1N/A --hcp->hdr->nelem;
1N/A
1N/A /*
1N/A * If we need to reclaim the page, then check if the page is empty.
1N/A * There are two cases. If it's empty and it's not the first page
1N/A * in the bucket (i.e., the bucket page) then we can simply remove
1N/A * it. If it is the first chain in the bucket, then we need to copy
1N/A * the second page into it and remove the second page.
1N/A */
1N/A if (reclaim_page && NUM_ENT(p) == 0 && PREV_PGNO(p) == PGNO_INVALID &&
1N/A NEXT_PGNO(p) != PGNO_INVALID) {
1N/A PAGE *n_pagep, *nn_pagep;
1N/A db_pgno_t tmp_pgno;
1N/A
1N/A /*
1N/A * First page in chain is empty and we know that there
1N/A * are more pages in the chain.
1N/A */
1N/A if ((ret =
1N/A __ham_get_page(dbp, NEXT_PGNO(p), &n_pagep)) != 0)
1N/A return (ret);
1N/A
1N/A if (NEXT_PGNO(n_pagep) != PGNO_INVALID) {
1N/A if ((ret =
1N/A __ham_get_page(dbp, NEXT_PGNO(n_pagep),
1N/A &nn_pagep)) != 0) {
1N/A (void) __ham_put_page(dbp, n_pagep, 0);
1N/A return (ret);
1N/A }
1N/A }
1N/A
1N/A if (DB_LOGGING(dbc)) {
1N/A key_dbt.data = n_pagep;
1N/A key_dbt.size = hcp->hdr->pagesize;
1N/A if ((ret = __ham_copypage_log(dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, dbp->log_fileid, PGNO(p),
1N/A &LSN(p), PGNO(n_pagep), &LSN(n_pagep),
1N/A NEXT_PGNO(n_pagep),
1N/A NEXT_PGNO(n_pagep) == PGNO_INVALID ? NULL :
1N/A &LSN(nn_pagep), &key_dbt)) != 0)
1N/A return (ret);
1N/A
1N/A /* Move lsn onto page. */
1N/A LSN(p) = new_lsn; /* Structure assignment. */
1N/A LSN(n_pagep) = new_lsn;
1N/A if (NEXT_PGNO(n_pagep) != PGNO_INVALID)
1N/A LSN(nn_pagep) = new_lsn;
1N/A }
1N/A if (NEXT_PGNO(n_pagep) != PGNO_INVALID) {
1N/A PREV_PGNO(nn_pagep) = PGNO(p);
1N/A (void)__ham_put_page(dbp, nn_pagep, 1);
1N/A }
1N/A
1N/A tmp_pgno = PGNO(p);
1N/A tmp_lsn = LSN(p);
1N/A memcpy(p, n_pagep, hcp->hdr->pagesize);
1N/A PGNO(p) = tmp_pgno;
1N/A LSN(p) = tmp_lsn;
1N/A PREV_PGNO(p) = PGNO_INVALID;
1N/A
1N/A /*
1N/A * Cursor is advanced to the beginning of the next page.
1N/A */
1N/A hcp->bndx = 0;
1N/A hcp->pgno = PGNO(p);
1N/A F_SET(hcp, H_DELETED);
1N/A chg_pgno = PGNO(p);
1N/A if ((ret = __ham_dirty_page(dbp, p)) != 0 ||
1N/A (ret = __ham_del_page(dbc, n_pagep)) != 0)
1N/A return (ret);
1N/A } else if (reclaim_page &&
1N/A NUM_ENT(p) == 0 && PREV_PGNO(p) != PGNO_INVALID) {
1N/A PAGE *n_pagep, *p_pagep;
1N/A
1N/A if ((ret =
1N/A __ham_get_page(dbp, PREV_PGNO(p), &p_pagep)) != 0)
1N/A return (ret);
1N/A
1N/A if (NEXT_PGNO(p) != PGNO_INVALID) {
1N/A if ((ret = __ham_get_page(dbp,
1N/A NEXT_PGNO(p), &n_pagep)) != 0) {
1N/A (void)__ham_put_page(dbp, p_pagep, 0);
1N/A return (ret);
1N/A }
1N/A n_lsn = &LSN(n_pagep);
1N/A } else {
1N/A n_pagep = NULL;
1N/A n_lsn = NULL;
1N/A }
1N/A
1N/A NEXT_PGNO(p_pagep) = NEXT_PGNO(p);
1N/A if (n_pagep != NULL)
1N/A PREV_PGNO(n_pagep) = PGNO(p_pagep);
1N/A
1N/A if (DB_LOGGING(dbc)) {
1N/A if ((ret = __ham_newpage_log(dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, DELOVFL,
1N/A dbp->log_fileid, PREV_PGNO(p), &LSN(p_pagep),
1N/A PGNO(p), &LSN(p), NEXT_PGNO(p), n_lsn)) != 0)
1N/A return (ret);
1N/A
1N/A /* Move lsn onto page. */
1N/A LSN(p_pagep) = new_lsn; /* Structure assignment. */
1N/A if (n_pagep)
1N/A LSN(n_pagep) = new_lsn;
1N/A LSN(p) = new_lsn;
1N/A }
1N/A hcp->pgno = NEXT_PGNO(p);
1N/A hcp->bndx = 0;
1N/A /*
1N/A * Since we are about to delete the cursor page and we have
1N/A * just moved the cursor, we need to make sure that the
1N/A * old page pointer isn't left hanging around in the cursor.
1N/A */
1N/A hcp->pagep = NULL;
1N/A chg_pgno = PGNO(p);
1N/A ret = __ham_del_page(dbc, p);
1N/A if ((tret = __ham_put_page(dbp, p_pagep, 1)) != 0 &&
1N/A ret == 0)
1N/A ret = tret;
1N/A if (n_pagep != NULL &&
1N/A (tret = __ham_put_page(dbp, n_pagep, 1)) != 0 &&
1N/A ret == 0)
1N/A ret = tret;
1N/A if (ret != 0)
1N/A return (ret);
1N/A } else {
1N/A /*
1N/A * Mark item deleted so that we don't try to return it, and
1N/A * so that we update the cursor correctly on the next call
1N/A * to next.
1N/A */
1N/A F_SET(hcp, H_DELETED);
1N/A chg_pgno = hcp->pgno;
1N/A ret = __ham_dirty_page(dbp, p);
1N/A }
1N/A __ham_c_update(hcp, chg_pgno, 0, 0, 0);
1N/A
1N/A /*
1N/A * Since we just deleted a pair from the master page, anything
1N/A * in hcp->dpgno should be cleared.
1N/A */
1N/A hcp->dpgno = PGNO_INVALID;
1N/A
1N/A F_CLR(hcp, H_OK);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __ham_replpair --
1N/A * Given the key data indicated by the cursor, replace part/all of it
1N/A * according to the fields in the dbt.
1N/A *
1N/A * PUBLIC: int __ham_replpair __P((DBC *, DBT *, u_int32_t));
1N/A */
1N/Aint
1N/A__ham_replpair(dbc, dbt, make_dup)
1N/A DBC *dbc;
1N/A DBT *dbt;
1N/A u_int32_t make_dup;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A DBT old_dbt, tdata, tmp;
1N/A DB_LSN new_lsn;
1N/A int32_t change; /* XXX: Possible overflow. */
1N/A u_int32_t len;
1N/A int is_big, ret, type;
1N/A u_int8_t *beg, *dest, *end, *hk, *src;
1N/A
1N/A /*
1N/A * Big item replacements are handled in generic code.
1N/A * Items that fit on the current page fall into 4 classes.
1N/A * 1. On-page element, same size
1N/A * 2. On-page element, new is bigger (fits)
1N/A * 3. On-page element, new is bigger (does not fit)
1N/A * 4. On-page element, old is bigger
1N/A * Numbers 1, 2, and 4 are essentially the same (and should
1N/A * be the common case). We handle case 3 as a delete and
1N/A * add.
1N/A */
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A
1N/A /*
1N/A * We need to compute the number of bytes that we are adding or
1N/A * removing from the entry. Normally, we can simply substract
1N/A * the number of bytes we are replacing (dbt->dlen) from the
1N/A * number of bytes we are inserting (dbt->size). However, if
1N/A * we are doing a partial put off the end of a record, then this
1N/A * formula doesn't work, because we are essentially adding
1N/A * new bytes.
1N/A */
1N/A change = dbt->size - dbt->dlen;
1N/A
1N/A hk = H_PAIRDATA(hcp->pagep, hcp->bndx);
1N/A is_big = HPAGE_PTYPE(hk) == H_OFFPAGE;
1N/A
1N/A if (is_big)
1N/A memcpy(&len, HOFFPAGE_TLEN(hk), sizeof(u_int32_t));
1N/A else
1N/A len = LEN_HKEYDATA(hcp->pagep,
1N/A dbp->pgsize, H_DATAINDEX(hcp->bndx));
1N/A
1N/A if (dbt->doff + dbt->dlen > len)
1N/A change += dbt->doff + dbt->dlen - len;
1N/A
1N/A
1N/A if (change > (int32_t)P_FREESPACE(hcp->pagep) || is_big) {
1N/A /*
1N/A * Case 3 -- two subcases.
1N/A * A. This is not really a partial operation, but an overwrite.
1N/A * Simple del and add works.
1N/A * B. This is a partial and we need to construct the data that
1N/A * we are really inserting (yuck).
1N/A * In both cases, we need to grab the key off the page (in
1N/A * some cases we could do this outside of this routine; for
1N/A * cleanliness we do it here. If you happen to be on a big
1N/A * key, this could be a performance hit).
1N/A */
1N/A tmp.flags = 0;
1N/A F_SET(&tmp, DB_DBT_MALLOC | DB_DBT_INTERNAL);
1N/A if ((ret =
1N/A __db_ret(dbp, hcp->pagep, H_KEYINDEX(hcp->bndx),
1N/A &tmp, &dbc->rkey.data, &dbc->rkey.size)) != 0)
1N/A return (ret);
1N/A
1N/A if (dbt->doff == 0 && dbt->dlen == len) {
1N/A ret = __ham_del_pair(dbc, 0);
1N/A if (ret == 0)
1N/A ret = __ham_add_el(dbc, &tmp, dbt, H_KEYDATA);
1N/A } else { /* Case B */
1N/A type = HPAGE_PTYPE(hk) != H_OFFPAGE ?
1N/A HPAGE_PTYPE(hk) : H_KEYDATA;
1N/A tdata.flags = 0;
1N/A F_SET(&tdata, DB_DBT_MALLOC | DB_DBT_INTERNAL);
1N/A
1N/A if ((ret = __db_ret(dbp, hcp->pagep,
1N/A H_DATAINDEX(hcp->bndx), &tdata, &dbc->rdata.data,
1N/A &dbc->rdata.size)) != 0)
1N/A goto err;
1N/A
1N/A /* Now we can delete the item. */
1N/A if ((ret = __ham_del_pair(dbc, 0)) != 0) {
1N/A __os_free(tdata.data, tdata.size);
1N/A goto err;
1N/A }
1N/A
1N/A /* Now shift old data around to make room for new. */
1N/A if (change > 0) {
1N/A if ((ret = __os_realloc(&tdata.data,
1N/A tdata.size + change)) != 0)
1N/A return (ret);
1N/A memset((u_int8_t *)tdata.data + tdata.size,
1N/A 0, change);
1N/A }
1N/A end = (u_int8_t *)tdata.data + tdata.size;
1N/A
1N/A src = (u_int8_t *)tdata.data + dbt->doff + dbt->dlen;
1N/A if (src < end && tdata.size > dbt->doff + dbt->dlen) {
1N/A len = tdata.size - dbt->doff - dbt->dlen;
1N/A dest = src + change;
1N/A memmove(dest, src, len);
1N/A }
1N/A memcpy((u_int8_t *)tdata.data + dbt->doff,
1N/A dbt->data, dbt->size);
1N/A tdata.size += change;
1N/A
1N/A /* Now add the pair. */
1N/A ret = __ham_add_el(dbc, &tmp, &tdata, type);
1N/A __os_free(tdata.data, tdata.size);
1N/A }
1N/Aerr: __os_free(tmp.data, tmp.size);
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * Set up pointer into existing data. Do it before the log
1N/A * message so we can use it inside of the log setup.
1N/A */
1N/A beg = HKEYDATA_DATA(H_PAIRDATA(hcp->pagep, hcp->bndx));
1N/A beg += dbt->doff;
1N/A
1N/A /*
1N/A * If we are going to have to move bytes at all, figure out
1N/A * all the parameters here. Then log the call before moving
1N/A * anything around.
1N/A */
1N/A if (DB_LOGGING(dbc)) {
1N/A old_dbt.data = beg;
1N/A old_dbt.size = dbt->dlen;
1N/A if ((ret = __ham_replace_log(dbp->dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, dbp->log_fileid, PGNO(hcp->pagep),
1N/A (u_int32_t)H_DATAINDEX(hcp->bndx), &LSN(hcp->pagep),
1N/A (u_int32_t)dbt->doff, &old_dbt, dbt, make_dup)) != 0)
1N/A return (ret);
1N/A
1N/A LSN(hcp->pagep) = new_lsn; /* Structure assignment. */
1N/A }
1N/A
1N/A __ham_onpage_replace(hcp->pagep, dbp->pgsize,
1N/A (u_int32_t)H_DATAINDEX(hcp->bndx), (int32_t)dbt->doff, change, dbt);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Replace data on a page with new data, possibly growing or shrinking what's
1N/A * there. This is called on two different occasions. On one (from replpair)
1N/A * we are interested in changing only the data. On the other (from recovery)
1N/A * we are replacing the entire data (header and all) with a new element. In
1N/A * the latter case, the off argument is negative.
1N/A * pagep: the page that we're changing
1N/A * ndx: page index of the element that is growing/shrinking.
1N/A * off: Offset at which we are beginning the replacement.
1N/A * change: the number of bytes (+ or -) that the element is growing/shrinking.
1N/A * dbt: the new data that gets written at beg.
1N/A * PUBLIC: void __ham_onpage_replace __P((PAGE *, size_t, u_int32_t, int32_t,
1N/A * PUBLIC: int32_t, DBT *));
1N/A */
1N/Avoid
1N/A__ham_onpage_replace(pagep, pgsize, ndx, off, change, dbt)
1N/A PAGE *pagep;
1N/A size_t pgsize;
1N/A u_int32_t ndx;
1N/A int32_t off;
1N/A int32_t change;
1N/A DBT *dbt;
1N/A{
1N/A db_indx_t i;
1N/A int32_t len;
1N/A u_int8_t *src, *dest;
1N/A int zero_me;
1N/A
1N/A if (change != 0) {
1N/A zero_me = 0;
1N/A src = (u_int8_t *)(pagep) + HOFFSET(pagep);
1N/A if (off < 0)
1N/A len = pagep->inp[ndx] - HOFFSET(pagep);
1N/A else if ((u_int32_t)off >= LEN_HKEYDATA(pagep, pgsize, ndx)) {
1N/A len = HKEYDATA_DATA(P_ENTRY(pagep, ndx)) +
1N/A LEN_HKEYDATA(pagep, pgsize, ndx) - src;
1N/A zero_me = 1;
1N/A } else
1N/A len = (HKEYDATA_DATA(P_ENTRY(pagep, ndx)) + off) - src;
1N/A dest = src - change;
1N/A memmove(dest, src, len);
1N/A if (zero_me)
1N/A memset(dest + len, 0, change);
1N/A
1N/A /* Now update the indices. */
1N/A for (i = ndx; i < NUM_ENT(pagep); i++)
1N/A pagep->inp[i] -= change;
1N/A HOFFSET(pagep) -= change;
1N/A }
1N/A if (off >= 0)
1N/A memcpy(HKEYDATA_DATA(P_ENTRY(pagep, ndx)) + off,
1N/A dbt->data, dbt->size);
1N/A else
1N/A memcpy(P_ENTRY(pagep, ndx), dbt->data, dbt->size);
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_split_page __P((DBC *, u_int32_t, u_int32_t));
1N/A */
1N/Aint
1N/A__ham_split_page(dbc, obucket, nbucket)
1N/A DBC *dbc;
1N/A u_int32_t obucket, nbucket;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A DBT key, page_dbt;
1N/A DB_ENV *dbenv;
1N/A DB_LSN new_lsn;
1N/A PAGE **pp, *old_pagep, *temp_pagep, *new_pagep;
1N/A db_indx_t n;
1N/A db_pgno_t bucket_pgno, next_pgno;
1N/A u_int32_t big_len, len;
1N/A int ret, tret;
1N/A void *big_buf;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A dbenv = dbp->dbenv;
1N/A temp_pagep = old_pagep = new_pagep = NULL;
1N/A
1N/A bucket_pgno = BUCKET_TO_PAGE(hcp, obucket);
1N/A if ((ret = __ham_get_page(dbp, bucket_pgno, &old_pagep)) != 0)
1N/A return (ret);
1N/A if ((ret = __ham_new_page(dbp, BUCKET_TO_PAGE(hcp, nbucket), P_HASH,
1N/A &new_pagep)) != 0)
1N/A goto err;
1N/A
1N/A temp_pagep = hcp->split_buf;
1N/A memcpy(temp_pagep, old_pagep, hcp->hdr->pagesize);
1N/A
1N/A if (DB_LOGGING(dbc)) {
1N/A page_dbt.size = hcp->hdr->pagesize;
1N/A page_dbt.data = old_pagep;
1N/A if ((ret = __ham_splitdata_log(dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, dbp->log_fileid, SPLITOLD,
1N/A PGNO(old_pagep), &page_dbt, &LSN(old_pagep))) != 0)
1N/A goto err;
1N/A }
1N/A
1N/A P_INIT(old_pagep, hcp->hdr->pagesize, PGNO(old_pagep), PGNO_INVALID,
1N/A PGNO_INVALID, 0, P_HASH);
1N/A
1N/A if (DB_LOGGING(dbc))
1N/A LSN(old_pagep) = new_lsn; /* Structure assignment. */
1N/A
1N/A big_len = 0;
1N/A big_buf = NULL;
1N/A key.flags = 0;
1N/A while (temp_pagep != NULL) {
1N/A for (n = 0; n < (db_indx_t)H_NUMPAIRS(temp_pagep); n++) {
1N/A if ((ret =
1N/A __db_ret(dbp, temp_pagep, H_KEYINDEX(n),
1N/A &key, &big_buf, &big_len)) != 0)
1N/A goto err;
1N/A
1N/A if (__ham_call_hash(hcp, key.data, key.size)
1N/A == obucket)
1N/A pp = &old_pagep;
1N/A else
1N/A pp = &new_pagep;
1N/A
1N/A /*
1N/A * Figure out how many bytes we need on the new
1N/A * page to store the key/data pair.
1N/A */
1N/A
1N/A len = LEN_HITEM(temp_pagep, hcp->hdr->pagesize,
1N/A H_DATAINDEX(n)) +
1N/A LEN_HITEM(temp_pagep, hcp->hdr->pagesize,
1N/A H_KEYINDEX(n)) +
1N/A 2 * sizeof(db_indx_t);
1N/A
1N/A if (P_FREESPACE(*pp) < len) {
1N/A if (DB_LOGGING(dbc)) {
1N/A page_dbt.size = hcp->hdr->pagesize;
1N/A page_dbt.data = *pp;
1N/A if ((ret = __ham_splitdata_log(
1N/A dbenv->lg_info, dbc->txn,
1N/A &new_lsn, 0, dbp->log_fileid,
1N/A SPLITNEW, PGNO(*pp), &page_dbt,
1N/A &LSN(*pp))) != 0)
1N/A goto err;
1N/A LSN(*pp) = new_lsn;
1N/A }
1N/A if ((ret =
1N/A __ham_add_ovflpage(dbc, *pp, 1, pp)) != 0)
1N/A goto err;
1N/A }
1N/A __ham_copy_item(dbp->pgsize,
1N/A temp_pagep, H_KEYINDEX(n), *pp);
1N/A __ham_copy_item(dbp->pgsize,
1N/A temp_pagep, H_DATAINDEX(n), *pp);
1N/A }
1N/A next_pgno = NEXT_PGNO(temp_pagep);
1N/A
1N/A /* Clear temp_page; if it's a link overflow page, free it. */
1N/A if (PGNO(temp_pagep) != bucket_pgno && (ret =
1N/A __ham_del_page(dbc, temp_pagep)) != 0)
1N/A goto err;
1N/A
1N/A if (next_pgno == PGNO_INVALID)
1N/A temp_pagep = NULL;
1N/A else if ((ret =
1N/A __ham_get_page(dbp, next_pgno, &temp_pagep)) != 0)
1N/A goto err;
1N/A
1N/A if (temp_pagep != NULL && DB_LOGGING(dbc)) {
1N/A page_dbt.size = hcp->hdr->pagesize;
1N/A page_dbt.data = temp_pagep;
1N/A if ((ret = __ham_splitdata_log(dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, dbp->log_fileid,
1N/A SPLITOLD, PGNO(temp_pagep),
1N/A &page_dbt, &LSN(temp_pagep))) != 0)
1N/A goto err;
1N/A LSN(temp_pagep) = new_lsn;
1N/A }
1N/A }
1N/A if (big_buf != NULL)
1N/A __os_free(big_buf, big_len);
1N/A
1N/A /*
1N/A * If the original bucket spanned multiple pages, then we've got
1N/A * a pointer to a page that used to be on the bucket chain. It
1N/A * should be deleted.
1N/A */
1N/A if (temp_pagep != NULL && PGNO(temp_pagep) != bucket_pgno &&
1N/A (ret = __ham_del_page(dbc, temp_pagep)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * Write new buckets out.
1N/A */
1N/A if (DB_LOGGING(dbc)) {
1N/A page_dbt.size = hcp->hdr->pagesize;
1N/A page_dbt.data = old_pagep;
1N/A if ((ret = __ham_splitdata_log(dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, dbp->log_fileid,
1N/A SPLITNEW, PGNO(old_pagep),
1N/A &page_dbt, &LSN(old_pagep))) != 0)
1N/A goto err;
1N/A LSN(old_pagep) = new_lsn;
1N/A
1N/A page_dbt.data = new_pagep;
1N/A if ((ret = __ham_splitdata_log(dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, dbp->log_fileid,
1N/A SPLITNEW, PGNO(new_pagep), &page_dbt, &LSN(new_pagep))) != 0)
1N/A goto err;
1N/A LSN(new_pagep) = new_lsn;
1N/A }
1N/A ret = __ham_put_page(dbp, old_pagep, 1);
1N/A if ((tret = __ham_put_page(dbp, new_pagep, 1)) != 0 &&
1N/A ret == 0)
1N/A ret = tret;
1N/A
1N/A if (0) {
1N/Aerr: if (old_pagep != NULL)
1N/A (void)__ham_put_page(dbp, old_pagep, 1);
1N/A if (new_pagep != NULL)
1N/A (void)__ham_put_page(dbp, new_pagep, 1);
1N/A if (temp_pagep != NULL && PGNO(temp_pagep) != bucket_pgno)
1N/A (void)__ham_put_page(dbp, temp_pagep, 1);
1N/A }
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * Add the given pair to the page. The page in question may already be
1N/A * held (i.e. it was already gotten). If it is, then the page is passed
1N/A * in via the pagep parameter. On return, pagep will contain the page
1N/A * to which we just added something. This allows us to link overflow
1N/A * pages and return the new page having correctly put the last page.
1N/A *
1N/A * PUBLIC: int __ham_add_el __P((DBC *, const DBT *, const DBT *, int));
1N/A */
1N/Aint
1N/A__ham_add_el(dbc, key, val, type)
1N/A DBC *dbc;
1N/A const DBT *key, *val;
1N/A int type;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A const DBT *pkey, *pdata;
1N/A DBT key_dbt, data_dbt;
1N/A DB_LSN new_lsn;
1N/A HOFFPAGE doff, koff;
1N/A db_pgno_t next_pgno;
1N/A u_int32_t data_size, key_size, pairsize, rectype;
1N/A int do_expand, is_keybig, is_databig, ret;
1N/A int key_type, data_type;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A do_expand = 0;
1N/A
1N/A if (hcp->pagep == NULL && (ret = __ham_get_page(dbp,
1N/A hcp->seek_found_page != PGNO_INVALID ? hcp->seek_found_page :
1N/A hcp->pgno, &hcp->pagep)) != 0)
1N/A return (ret);
1N/A
1N/A key_size = HKEYDATA_PSIZE(key->size);
1N/A data_size = HKEYDATA_PSIZE(val->size);
1N/A is_keybig = ISBIG(hcp, key->size);
1N/A is_databig = ISBIG(hcp, val->size);
1N/A if (is_keybig)
1N/A key_size = HOFFPAGE_PSIZE;
1N/A if (is_databig)
1N/A data_size = HOFFPAGE_PSIZE;
1N/A
1N/A pairsize = key_size + data_size;
1N/A
1N/A /* Advance to first page in chain with room for item. */
1N/A while (H_NUMPAIRS(hcp->pagep) && NEXT_PGNO(hcp->pagep) !=
1N/A PGNO_INVALID) {
1N/A /*
1N/A * This may not be the end of the chain, but the pair may fit
1N/A * anyway. Check if it's a bigpair that fits or a regular
1N/A * pair that fits.
1N/A */
1N/A if (P_FREESPACE(hcp->pagep) >= pairsize)
1N/A break;
1N/A next_pgno = NEXT_PGNO(hcp->pagep);
1N/A if ((ret =
1N/A __ham_next_cpage(dbc, next_pgno, 0, 0)) != 0)
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * Check if we need to allocate a new page.
1N/A */
1N/A if (P_FREESPACE(hcp->pagep) < pairsize) {
1N/A do_expand = 1;
1N/A if ((ret = __ham_add_ovflpage(dbc,
1N/A hcp->pagep, 1, &hcp->pagep)) != 0)
1N/A return (ret);
1N/A hcp->pgno = PGNO(hcp->pagep);
1N/A }
1N/A
1N/A /*
1N/A * Update cursor.
1N/A */
1N/A hcp->bndx = H_NUMPAIRS(hcp->pagep);
1N/A F_CLR(hcp, H_DELETED);
1N/A if (is_keybig) {
1N/A koff.type = H_OFFPAGE;
1N/A UMRW(koff.unused[0]);
1N/A UMRW(koff.unused[1]);
1N/A UMRW(koff.unused[2]);
1N/A if ((ret = __db_poff(dbc,
1N/A key, &koff.pgno, __ham_overflow_page)) != 0)
1N/A return (ret);
1N/A koff.tlen = key->size;
1N/A key_dbt.data = &koff;
1N/A key_dbt.size = sizeof(koff);
1N/A pkey = &key_dbt;
1N/A key_type = H_OFFPAGE;
1N/A } else {
1N/A pkey = key;
1N/A key_type = H_KEYDATA;
1N/A }
1N/A
1N/A if (is_databig) {
1N/A doff.type = H_OFFPAGE;
1N/A UMRW(doff.unused[0]);
1N/A UMRW(doff.unused[1]);
1N/A UMRW(doff.unused[2]);
1N/A if ((ret = __db_poff(dbc,
1N/A val, &doff.pgno, __ham_overflow_page)) != 0)
1N/A return (ret);
1N/A doff.tlen = val->size;
1N/A data_dbt.data = &doff;
1N/A data_dbt.size = sizeof(doff);
1N/A pdata = &data_dbt;
1N/A data_type = H_OFFPAGE;
1N/A } else {
1N/A pdata = val;
1N/A data_type = type;
1N/A }
1N/A
1N/A if (DB_LOGGING(dbc)) {
1N/A rectype = PUTPAIR;
1N/A if (is_databig)
1N/A rectype |= PAIR_DATAMASK;
1N/A if (is_keybig)
1N/A rectype |= PAIR_KEYMASK;
1N/A
1N/A if ((ret = __ham_insdel_log(dbp->dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, rectype,
1N/A dbp->log_fileid, PGNO(hcp->pagep),
1N/A (u_int32_t)H_NUMPAIRS(hcp->pagep),
1N/A &LSN(hcp->pagep), pkey, pdata)) != 0)
1N/A return (ret);
1N/A
1N/A /* Move lsn onto page. */
1N/A LSN(hcp->pagep) = new_lsn; /* Structure assignment. */
1N/A }
1N/A
1N/A __ham_putitem(hcp->pagep, pkey, key_type);
1N/A __ham_putitem(hcp->pagep, pdata, data_type);
1N/A
1N/A /*
1N/A * For splits, we are going to update item_info's page number
1N/A * field, so that we can easily return to the same page the
1N/A * next time we come in here. For other operations, this shouldn't
1N/A * matter, since odds are this is the last thing that happens before
1N/A * we return to the user program.
1N/A */
1N/A hcp->pgno = PGNO(hcp->pagep);
1N/A
1N/A /*
1N/A * XXX Maybe keep incremental numbers here
1N/A */
1N/A if (!F_ISSET(dbp, DB_AM_LOCKING))
1N/A hcp->hdr->nelem++;
1N/A
1N/A if (do_expand || (hcp->hdr->ffactor != 0 &&
1N/A (u_int32_t)H_NUMPAIRS(hcp->pagep) > hcp->hdr->ffactor))
1N/A F_SET(hcp, H_EXPAND);
1N/A return (0);
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Special __putitem call used in splitting -- copies one entry to
1N/A * another. Works for all types of hash entries (H_OFFPAGE, H_KEYDATA,
1N/A * H_DUPLICATE, H_OFFDUP). Since we log splits at a high level, we
1N/A * do not need to do any logging here.
1N/A *
1N/A * PUBLIC: void __ham_copy_item __P((size_t, PAGE *, u_int32_t, PAGE *));
1N/A */
1N/Avoid
1N/A__ham_copy_item(pgsize, src_page, src_ndx, dest_page)
1N/A size_t pgsize;
1N/A PAGE *src_page;
1N/A u_int32_t src_ndx;
1N/A PAGE *dest_page;
1N/A{
1N/A u_int32_t len;
1N/A void *src, *dest;
1N/A
1N/A /*
1N/A * Copy the key and data entries onto this new page.
1N/A */
1N/A src = P_ENTRY(src_page, src_ndx);
1N/A
1N/A /* Set up space on dest. */
1N/A len = LEN_HITEM(src_page, pgsize, src_ndx);
1N/A HOFFSET(dest_page) -= len;
1N/A dest_page->inp[NUM_ENT(dest_page)] = HOFFSET(dest_page);
1N/A dest = P_ENTRY(dest_page, NUM_ENT(dest_page));
1N/A NUM_ENT(dest_page)++;
1N/A
1N/A memcpy(dest, src, len);
1N/A}
1N/A
1N/A/*
1N/A *
1N/A * Returns:
1N/A * pointer on success
1N/A * NULL on error
1N/A *
1N/A * PUBLIC: int __ham_add_ovflpage __P((DBC *, PAGE *, int, PAGE **));
1N/A */
1N/Aint
1N/A__ham_add_ovflpage(dbc, pagep, release, pp)
1N/A DBC *dbc;
1N/A PAGE *pagep;
1N/A int release;
1N/A PAGE **pp;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A DB_LSN new_lsn;
1N/A PAGE *new_pagep;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A
1N/A if ((ret = __ham_overflow_page(dbc, P_HASH, &new_pagep)) != 0)
1N/A return (ret);
1N/A
1N/A if (DB_LOGGING(dbc)) {
1N/A if ((ret = __ham_newpage_log(dbp->dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, PUTOVFL,
1N/A dbp->log_fileid, PGNO(pagep), &LSN(pagep),
1N/A PGNO(new_pagep), &LSN(new_pagep), PGNO_INVALID, NULL)) != 0)
1N/A return (ret);
1N/A
1N/A /* Move lsn onto page. */
1N/A LSN(pagep) = LSN(new_pagep) = new_lsn;
1N/A }
1N/A NEXT_PGNO(pagep) = PGNO(new_pagep);
1N/A PREV_PGNO(new_pagep) = PGNO(pagep);
1N/A
1N/A if (release)
1N/A ret = __ham_put_page(dbp, pagep, 1);
1N/A
1N/A hcp->stats.hash_overflows++;
1N/A *pp = new_pagep;
1N/A return (ret);
1N/A}
1N/A
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_new_page __P((DB *, u_int32_t, u_int32_t, PAGE **));
1N/A */
1N/Aint
1N/A__ham_new_page(dbp, addr, type, pp)
1N/A DB *dbp;
1N/A u_int32_t addr, type;
1N/A PAGE **pp;
1N/A{
1N/A PAGE *pagep;
1N/A int ret;
1N/A
1N/A if ((ret = memp_fget(dbp->mpf,
1N/A &addr, DB_MPOOL_CREATE, &pagep)) != 0)
1N/A return (ret);
1N/A
1N/A /* This should not be necessary because page-in should do it. */
1N/A P_INIT(pagep, dbp->pgsize, addr, PGNO_INVALID, PGNO_INVALID, 0, type);
1N/A
1N/A *pp = pagep;
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_del_page __P((DBC *, PAGE *));
1N/A */
1N/Aint
1N/A__ham_del_page(dbc, pagep)
1N/A DBC *dbc;
1N/A PAGE *pagep;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A DB_LSN new_lsn;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A ret = 0;
1N/A DIRTY_META(dbp, hcp, ret);
1N/A if (ret != 0) {
1N/A if (ret != EAGAIN)
1N/A __db_err(dbp->dbenv,
1N/A "free_ovflpage: unable to lock meta data page %s\n",
1N/A strerror(ret));
1N/A /*
1N/A * If we are going to return an error, then we should free
1N/A * the page, so it doesn't stay pinned forever.
1N/A */
1N/A (void)__ham_put_page(dbp, pagep, 0);
1N/A return (ret);
1N/A }
1N/A
1N/A if (DB_LOGGING(dbc)) {
1N/A if ((ret = __ham_newpgno_log(dbp->dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, DELPGNO,
1N/A dbp->log_fileid, PGNO(pagep), hcp->hdr->last_freed,
1N/A (u_int32_t)TYPE(pagep), NEXT_PGNO(pagep), P_INVALID,
1N/A &LSN(pagep), &hcp->hdr->lsn)) != 0)
1N/A return (ret);
1N/A
1N/A hcp->hdr->lsn = new_lsn;
1N/A LSN(pagep) = new_lsn;
1N/A }
1N/A
1N/A#ifdef DIAGNOSTIC
1N/A {
1N/A db_pgno_t __pgno;
1N/A DB_LSN __lsn;
1N/A __pgno = pagep->pgno;
1N/A __lsn = pagep->lsn;
1N/A memset(pagep, 0xdb, dbp->pgsize);
1N/A pagep->pgno = __pgno;
1N/A pagep->lsn = __lsn;
1N/A }
1N/A#endif
1N/A TYPE(pagep) = P_INVALID;
1N/A NEXT_PGNO(pagep) = hcp->hdr->last_freed;
1N/A hcp->hdr->last_freed = PGNO(pagep);
1N/A
1N/A return (__ham_put_page(dbp, pagep, 1));
1N/A}
1N/A
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_put_page __P((DB *, PAGE *, int32_t));
1N/A */
1N/Aint
1N/A__ham_put_page(dbp, pagep, is_dirty)
1N/A DB *dbp;
1N/A PAGE *pagep;
1N/A int32_t is_dirty;
1N/A{
1N/A#ifdef DEBUG_SLOW
1N/A __account_page(dbp, ((BKT *)((char *)pagep - sizeof(BKT)))->pgno, -1);
1N/A#endif
1N/A return (memp_fput(dbp->mpf, pagep, (is_dirty ? DB_MPOOL_DIRTY : 0)));
1N/A}
1N/A
1N/A/*
1N/A * __ham_dirty_page --
1N/A * Mark a page dirty.
1N/A *
1N/A * PUBLIC: int __ham_dirty_page __P((DB *, PAGE *));
1N/A */
1N/Aint
1N/A__ham_dirty_page(dbp, pagep)
1N/A DB *dbp;
1N/A PAGE *pagep;
1N/A{
1N/A return (memp_fset(dbp->mpf, pagep, DB_MPOOL_DIRTY));
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_get_page __P((DB *, db_pgno_t, PAGE **));
1N/A */
1N/Aint
1N/A__ham_get_page(dbp, addr, pagep)
1N/A DB *dbp;
1N/A db_pgno_t addr;
1N/A PAGE **pagep;
1N/A{
1N/A int ret;
1N/A
1N/A ret = memp_fget(dbp->mpf, &addr, DB_MPOOL_CREATE, pagep);
1N/A#ifdef DEBUG_SLOW
1N/A if (*pagep != NULL)
1N/A __account_page(dbp, addr, 1);
1N/A#endif
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_overflow_page
1N/A * PUBLIC: __P((DBC *, u_int32_t, PAGE **));
1N/A */
1N/Aint
1N/A__ham_overflow_page(dbc, type, pp)
1N/A DBC *dbc;
1N/A u_int32_t type;
1N/A PAGE **pp;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A DB_LSN *lsnp, new_lsn;
1N/A PAGE *p;
1N/A db_pgno_t new_addr, next_free, newalloc_flag;
1N/A u_int32_t offset, splitnum;
1N/A int ret;
1N/A
1N/A ret = 0;
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A DIRTY_META(dbp, hcp, ret);
1N/A if (ret != 0)
1N/A return (ret);
1N/A
1N/A /*
1N/A * This routine is split up into two parts. First we have
1N/A * to figure out the address of the new page that we are
1N/A * allocating. Then we have to log the allocation. Only
1N/A * after the log do we get to complete allocation of the
1N/A * new page.
1N/A */
1N/A new_addr = hcp->hdr->last_freed;
1N/A if (new_addr != PGNO_INVALID) {
1N/A if ((ret = __ham_get_page(dbp, new_addr, &p)) != 0)
1N/A return (ret);
1N/A next_free = NEXT_PGNO(p);
1N/A lsnp = &LSN(p);
1N/A newalloc_flag = 0;
1N/A } else {
1N/A splitnum = hcp->hdr->ovfl_point;
1N/A hcp->hdr->spares[splitnum]++;
1N/A offset = hcp->hdr->spares[splitnum] -
1N/A (splitnum ? hcp->hdr->spares[splitnum - 1] : 0);
1N/A new_addr = PGNO_OF(hcp, hcp->hdr->ovfl_point, offset);
1N/A if (new_addr > MAX_PAGES(hcp)) {
1N/A __db_err(dbp->dbenv, "hash: out of file pages");
1N/A hcp->hdr->spares[splitnum]--;
1N/A return (ENOMEM);
1N/A }
1N/A next_free = PGNO_INVALID;
1N/A p = NULL;
1N/A lsnp = NULL;
1N/A newalloc_flag = 1;
1N/A }
1N/A
1N/A if (DB_LOGGING(dbc)) {
1N/A if ((ret = __ham_newpgno_log(dbp->dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, ALLOCPGNO,
1N/A dbp->log_fileid, new_addr, next_free,
1N/A 0, newalloc_flag, type, lsnp, &hcp->hdr->lsn)) != 0)
1N/A return (ret);
1N/A
1N/A hcp->hdr->lsn = new_lsn;
1N/A if (lsnp != NULL)
1N/A *lsnp = new_lsn;
1N/A }
1N/A
1N/A if (p != NULL) {
1N/A /* We just took something off the free list, initialize it. */
1N/A hcp->hdr->last_freed = next_free;
1N/A P_INIT(p, hcp->hdr->pagesize, PGNO(p), PGNO_INVALID,
1N/A PGNO_INVALID, 0, (u_int8_t)type);
1N/A } else {
1N/A /* Get the new page. */
1N/A if ((ret = __ham_new_page(dbp, new_addr, type, &p)) != 0)
1N/A return (ret);
1N/A }
1N/A if (DB_LOGGING(dbc))
1N/A LSN(p) = new_lsn;
1N/A
1N/A *pp = p;
1N/A return (0);
1N/A}
1N/A
1N/A#ifdef DEBUG
1N/A/*
1N/A * PUBLIC: #ifdef DEBUG
1N/A * PUBLIC: db_pgno_t __bucket_to_page __P((HASH_CURSOR *, db_pgno_t));
1N/A * PUBLIC: #endif
1N/A */
1N/Adb_pgno_t
1N/A__bucket_to_page(hcp, n)
1N/A HASH_CURSOR *hcp;
1N/A db_pgno_t n;
1N/A{
1N/A int ret_val;
1N/A
1N/A ret_val = n + 1;
1N/A if (n != 0)
1N/A ret_val += hcp->hdr->spares[__db_log2(n + 1) - 1];
1N/A return (ret_val);
1N/A}
1N/A#endif
1N/A
1N/A/*
1N/A * Create a bunch of overflow pages at the current split point.
1N/A * PUBLIC: void __ham_init_ovflpages __P((DBC *));
1N/A */
1N/Avoid
1N/A__ham_init_ovflpages(dbc)
1N/A DBC *dbc;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A DB_LSN new_lsn;
1N/A PAGE *p;
1N/A db_pgno_t last_pgno, new_pgno;
1N/A u_int32_t i, curpages, numpages;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A
1N/A curpages = hcp->hdr->spares[hcp->hdr->ovfl_point] -
1N/A hcp->hdr->spares[hcp->hdr->ovfl_point - 1];
1N/A numpages = hcp->hdr->ovfl_point + 1 - curpages;
1N/A
1N/A last_pgno = hcp->hdr->last_freed;
1N/A new_pgno = PGNO_OF(hcp, hcp->hdr->ovfl_point, curpages + 1);
1N/A if (DB_LOGGING(dbc)) {
1N/A (void)__ham_ovfl_log(dbp->dbenv->lg_info,
1N/A dbc->txn, &new_lsn, 0, dbp->log_fileid, new_pgno,
1N/A numpages, last_pgno, hcp->hdr->ovfl_point, &hcp->hdr->lsn);
1N/A hcp->hdr->lsn = new_lsn;
1N/A } else
1N/A ZERO_LSN(new_lsn);
1N/A
1N/A hcp->hdr->spares[hcp->hdr->ovfl_point] += numpages;
1N/A for (i = numpages; i > 0; i--) {
1N/A if (__ham_new_page(dbp,
1N/A PGNO_OF(hcp, hcp->hdr->ovfl_point, curpages + i),
1N/A P_INVALID, &p) != 0)
1N/A break;
1N/A LSN(p) = new_lsn;
1N/A NEXT_PGNO(p) = last_pgno;
1N/A last_pgno = PGNO(p);
1N/A (void)__ham_put_page(dbp, p, 1);
1N/A }
1N/A hcp->hdr->last_freed = last_pgno;
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __ham_get_cpage __P((DBC *, db_lockmode_t));
1N/A */
1N/Aint
1N/A__ham_get_cpage(dbc, mode)
1N/A DBC *dbc;
1N/A db_lockmode_t mode;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A
1N/A /*
1N/A * There are three cases with respect to buckets and locks. If there
1N/A * is no lock held, then if we are locking, we should get the lock.
1N/A * If there is a lock held and it's for the current bucket, we don't
1N/A * need to do anything. If there is a lock, but it's for a different
1N/A * bucket, then we need to release and get.
1N/A */
1N/A if (F_ISSET(dbp, DB_AM_LOCKING)) {
1N/A if (hcp->lock != 0 && hcp->lbucket != hcp->bucket) {
1N/A /*
1N/A * If this is the original lock, don't release it,
1N/A * because we may need to restore it upon exit.
1N/A */
1N/A if (dbc->txn == NULL &&
1N/A !F_ISSET(hcp, H_ORIGINAL) && (ret =
1N/A lock_put(dbp->dbenv->lk_info, hcp->lock)) != 0)
1N/A return (ret);
1N/A F_CLR(hcp, H_ORIGINAL);
1N/A hcp->lock = 0;
1N/A }
1N/A if (hcp->lock == 0 && (ret = __ham_lock_bucket(dbc, mode)) != 0)
1N/A return (ret);
1N/A hcp->lbucket = hcp->bucket;
1N/A }
1N/A
1N/A if (hcp->pagep == NULL) {
1N/A if (hcp->pgno == PGNO_INVALID) {
1N/A hcp->pgno = BUCKET_TO_PAGE(hcp, hcp->bucket);
1N/A hcp->bndx = 0;
1N/A }
1N/A
1N/A if ((ret =
1N/A __ham_get_page(dbp, hcp->pgno, &hcp->pagep)) != 0)
1N/A return (ret);
1N/A }
1N/A
1N/A if (hcp->dpgno != PGNO_INVALID && hcp->dpagep == NULL)
1N/A if ((ret =
1N/A __ham_get_page(dbp, hcp->dpgno, &hcp->dpagep)) != 0)
1N/A return (ret);
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Get a new page at the cursor, putting the last page if necessary.
1N/A * If the flag is set to H_ISDUP, then we are talking about the
1N/A * duplicate page, not the main page.
1N/A *
1N/A * PUBLIC: int __ham_next_cpage __P((DBC *, db_pgno_t, int, u_int32_t));
1N/A */
1N/Aint
1N/A__ham_next_cpage(dbc, pgno, dirty, flags)
1N/A DBC *dbc;
1N/A db_pgno_t pgno;
1N/A int dirty;
1N/A u_int32_t flags;
1N/A{
1N/A DB *dbp;
1N/A HASH_CURSOR *hcp;
1N/A PAGE *p;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A if (LF_ISSET(H_ISDUP) && hcp->dpagep != NULL &&
1N/A (ret = __ham_put_page(dbp, hcp->dpagep, dirty)) != 0)
1N/A return (ret);
1N/A else if (!LF_ISSET(H_ISDUP) && hcp->pagep != NULL &&
1N/A (ret = __ham_put_page(dbp, hcp->pagep, dirty)) != 0)
1N/A return (ret);
1N/A
1N/A if ((ret = __ham_get_page(dbp, pgno, &p)) != 0)
1N/A return (ret);
1N/A
1N/A if (LF_ISSET(H_ISDUP)) {
1N/A hcp->dpagep = p;
1N/A hcp->dpgno = pgno;
1N/A hcp->dndx = 0;
1N/A } else {
1N/A hcp->pagep = p;
1N/A hcp->pgno = pgno;
1N/A hcp->bndx = 0;
1N/A }
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __ham_lock_bucket --
1N/A * Get the lock on a particular bucket.
1N/A */
1N/Astatic int
1N/A__ham_lock_bucket(dbc, mode)
1N/A DBC *dbc;
1N/A db_lockmode_t mode;
1N/A{
1N/A HASH_CURSOR *hcp;
1N/A int ret;
1N/A
1N/A hcp = (HASH_CURSOR *)dbc->internal;
1N/A dbc->lock.pgno = (db_pgno_t)(hcp->bucket);
1N/A if (dbc->txn == NULL)
1N/A ret = lock_get(dbc->dbp->dbenv->lk_info, dbc->locker, 0,
1N/A &dbc->lock_dbt, mode, &hcp->lock);
1N/A else
1N/A ret = lock_tget(dbc->dbp->dbenv->lk_info, dbc->txn, 0,
1N/A &dbc->lock_dbt, mode, &hcp->lock);
1N/A
1N/A return (ret < 0 ? EAGAIN : ret);
1N/A}
1N/A
1N/A/*
1N/A * __ham_dpair --
1N/A * Delete a pair on a page, paying no attention to what the pair
1N/A * represents. The caller is responsible for freeing up duplicates
1N/A * or offpage entries that might be referenced by this pair.
1N/A *
1N/A * PUBLIC: void __ham_dpair __P((DB *, PAGE *, u_int32_t));
1N/A */
1N/Avoid
1N/A__ham_dpair(dbp, p, pndx)
1N/A DB *dbp;
1N/A PAGE *p;
1N/A u_int32_t pndx;
1N/A{
1N/A db_indx_t delta, n;
1N/A u_int8_t *dest, *src;
1N/A
1N/A /*
1N/A * Compute "delta", the amount we have to shift all of the
1N/A * offsets. To find the delta, we just need to calculate
1N/A * the size of the pair of elements we are removing.
1N/A */
1N/A delta = H_PAIRSIZE(p, dbp->pgsize, pndx);
1N/A
1N/A /*
1N/A * The hard case: we want to remove something other than
1N/A * the last item on the page. We need to shift data and
1N/A * offsets down.
1N/A */
1N/A if ((db_indx_t)pndx != H_NUMPAIRS(p) - 1) {
1N/A /*
1N/A * Move the data: src is the first occupied byte on
1N/A * the page. (Length is delta.)
1N/A */
1N/A src = (u_int8_t *)p + HOFFSET(p);
1N/A
1N/A /*
1N/A * Destination is delta bytes beyond src. This might
1N/A * be an overlapping copy, so we have to use memmove.
1N/A */
1N/A dest = src + delta;
1N/A memmove(dest, src, p->inp[H_DATAINDEX(pndx)] - HOFFSET(p));
1N/A }
1N/A
1N/A /* Adjust the offsets. */
1N/A for (n = (db_indx_t)pndx; n < (db_indx_t)(H_NUMPAIRS(p) - 1); n++) {
1N/A p->inp[H_KEYINDEX(n)] = p->inp[H_KEYINDEX(n+1)] + delta;
1N/A p->inp[H_DATAINDEX(n)] = p->inp[H_DATAINDEX(n+1)] + delta;
1N/A }
1N/A
1N/A /* Adjust page metadata. */
1N/A HOFFSET(p) = HOFFSET(p) + delta;
1N/A NUM_ENT(p) = NUM_ENT(p) - 2;
1N/A}
1N/A