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#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)bt_cursor.c 10.81 (Sleepycat) 12/16/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 <stdlib.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 "btree.h"
1N/A#include "shqueue.h"
1N/A#include "db_shash.h"
1N/A#include "lock.h"
1N/A#include "lock_ext.h"
1N/A
1N/Astatic int __bam_c_close __P((DBC *));
1N/Astatic int __bam_c_del __P((DBC *, u_int32_t));
1N/Astatic int __bam_c_destroy __P((DBC *));
1N/Astatic int __bam_c_first __P((DBC *, CURSOR *));
1N/Astatic int __bam_c_get __P((DBC *, DBT *, DBT *, u_int32_t));
1N/Astatic int __bam_c_getstack __P((DBC *, CURSOR *));
1N/Astatic int __bam_c_last __P((DBC *, CURSOR *));
1N/Astatic int __bam_c_next __P((DBC *, CURSOR *, int));
1N/Astatic int __bam_c_physdel __P((DBC *, CURSOR *, PAGE *));
1N/Astatic int __bam_c_prev __P((DBC *, CURSOR *));
1N/Astatic int __bam_c_put __P((DBC *, DBT *, DBT *, u_int32_t));
1N/Astatic void __bam_c_reset __P((CURSOR *));
1N/Astatic int __bam_c_rget __P((DBC *, DBT *, u_int32_t));
1N/Astatic int __bam_c_search __P((DBC *, CURSOR *, const DBT *, u_int32_t, int *));
1N/Astatic int __bam_dsearch __P((DBC *, CURSOR *, DBT *, u_int32_t *));
1N/A
1N/A/* Discard the current page/lock held by a cursor. */
1N/A#undef DISCARD
1N/A#define DISCARD(dbc, cp) { \
1N/A if ((cp)->page != NULL) { \
1N/A (void)memp_fput((dbc)->dbp->mpf, (cp)->page, 0); \
1N/A (cp)->page = NULL; \
1N/A } \
1N/A if ((cp)->lock != LOCK_INVALID) { \
1N/A (void)__BT_TLPUT((dbc), (cp)->lock); \
1N/A (cp)->lock = LOCK_INVALID; \
1N/A } \
1N/A}
1N/A
1N/A/* If the cursor references a deleted record. */
1N/A#undef IS_CUR_DELETED
1N/A#define IS_CUR_DELETED(cp) \
1N/A (((cp)->dpgno == PGNO_INVALID && \
1N/A B_DISSET(GET_BKEYDATA((cp)->page, \
1N/A (cp)->indx + O_INDX)->type)) || \
1N/A ((cp)->dpgno != PGNO_INVALID && \
1N/A B_DISSET(GET_BKEYDATA((cp)->page, (cp)->dindx)->type)))
1N/A
1N/A/* If the cursor and index combination references a deleted record. */
1N/A#undef IS_DELETED
1N/A#define IS_DELETED(cp, indx) \
1N/A (((cp)->dpgno == PGNO_INVALID && \
1N/A B_DISSET(GET_BKEYDATA((cp)->page, (indx) + O_INDX)->type)) || \
1N/A ((cp)->dpgno != PGNO_INVALID && \
1N/A B_DISSET(GET_BKEYDATA((cp)->page, (indx))->type)))
1N/A
1N/A/*
1N/A * Test to see if two cursors could point to duplicates of the same key,
1N/A * whether on-page or off-page. The leaf page numbers must be the same
1N/A * in both cases. In the case of off-page duplicates, the key indices
1N/A * on the leaf page will be the same. In the case of on-page duplicates,
1N/A * the duplicate page number must not be set, and the key index offsets
1N/A * must be the same. For the last test, as the saved copy of the cursor
1N/A * will not have a valid page pointer, we use the cursor's.
1N/A */
1N/A#undef POSSIBLE_DUPLICATE
1N/A#define POSSIBLE_DUPLICATE(cursor, saved_copy) \
1N/A ((cursor)->pgno == (saved_copy).pgno && \
1N/A ((cursor)->indx == (saved_copy).indx || \
1N/A ((cursor)->dpgno == PGNO_INVALID && \
1N/A (saved_copy).dpgno == PGNO_INVALID && \
1N/A (cursor)->page->inp[(cursor)->indx] == \
1N/A (cursor)->page->inp[(saved_copy).indx])))
1N/A
1N/A/*
1N/A * __bam_c_reset --
1N/A * Initialize internal cursor structure.
1N/A */
1N/Astatic void
1N/A__bam_c_reset(cp)
1N/A CURSOR *cp;
1N/A{
1N/A cp->sp = cp->csp = cp->stack;
1N/A cp->esp = cp->stack + sizeof(cp->stack) / sizeof(cp->stack[0]);
1N/A cp->page = NULL;
1N/A cp->pgno = PGNO_INVALID;
1N/A cp->indx = 0;
1N/A cp->dpgno = PGNO_INVALID;
1N/A cp->dindx = 0;
1N/A cp->lock = LOCK_INVALID;
1N/A cp->mode = DB_LOCK_NG;
1N/A cp->recno = RECNO_OOB;
1N/A cp->flags = 0;
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_init --
1N/A * Initialize the access private portion of a cursor
1N/A *
1N/A * PUBLIC: int __bam_c_init __P((DBC *));
1N/A */
1N/Aint
1N/A__bam_c_init(dbc)
1N/A DBC *dbc;
1N/A{
1N/A DB *dbp;
1N/A CURSOR *cp;
1N/A int ret;
1N/A
1N/A if ((ret = __os_calloc(1, sizeof(CURSOR), &cp)) != 0)
1N/A return (ret);
1N/A
1N/A dbp = dbc->dbp;
1N/A cp->dbc = dbc;
1N/A
1N/A /*
1N/A * Logical record numbers are always the same size, and we don't want
1N/A * to have to check for space every time we return one. Allocate it
1N/A * in advance.
1N/A */
1N/A if (dbp->type == DB_RECNO || F_ISSET(dbp, DB_BT_RECNUM)) {
1N/A if ((ret = __os_malloc(sizeof(db_recno_t),
1N/A NULL, &dbc->rkey.data)) != 0) {
1N/A __os_free(cp, sizeof(CURSOR));
1N/A return (ret);
1N/A }
1N/A dbc->rkey.ulen = sizeof(db_recno_t);
1N/A }
1N/A
1N/A /* Initialize methods. */
1N/A dbc->internal = cp;
1N/A if (dbp->type == DB_BTREE) {
1N/A dbc->c_am_close = __bam_c_close;
1N/A dbc->c_am_destroy = __bam_c_destroy;
1N/A dbc->c_del = __bam_c_del;
1N/A dbc->c_get = __bam_c_get;
1N/A dbc->c_put = __bam_c_put;
1N/A } else {
1N/A dbc->c_am_close = __bam_c_close;
1N/A dbc->c_am_destroy = __bam_c_destroy;
1N/A dbc->c_del = __ram_c_del;
1N/A dbc->c_get = __ram_c_get;
1N/A dbc->c_put = __ram_c_put;
1N/A }
1N/A
1N/A /* Initialize dynamic information. */
1N/A __bam_c_reset(cp);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_close --
1N/A * Close down the cursor from a single use.
1N/A */
1N/Astatic int
1N/A__bam_c_close(dbc)
1N/A DBC *dbc;
1N/A{
1N/A CURSOR *cp;
1N/A DB *dbp;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A cp = dbc->internal;
1N/A ret = 0;
1N/A
1N/A /*
1N/A * If a cursor deleted a btree key, perform the actual deletion.
1N/A * (Recno keys are either deleted immediately or never deleted.)
1N/A */
1N/A if (dbp->type == DB_BTREE && F_ISSET(cp, C_DELETED))
1N/A ret = __bam_c_physdel(dbc, cp, NULL);
1N/A
1N/A /* Discard any locks not acquired inside of a transaction. */
1N/A if (cp->lock != LOCK_INVALID) {
1N/A (void)__BT_TLPUT(dbc, cp->lock);
1N/A cp->lock = LOCK_INVALID;
1N/A }
1N/A
1N/A /* Sanity checks. */
1N/A#ifdef DIAGNOSTIC
1N/A if (cp->csp != cp->stack)
1N/A __db_err(dbp->dbenv, "btree cursor close: stack not empty");
1N/A#endif
1N/A
1N/A /* Initialize dynamic information. */
1N/A __bam_c_reset(cp);
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_destroy --
1N/A * Close a single cursor -- internal version.
1N/A */
1N/Astatic int
1N/A__bam_c_destroy(dbc)
1N/A DBC *dbc;
1N/A{
1N/A /* Discard the structures. */
1N/A __os_free(dbc->internal, sizeof(CURSOR));
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_del --
1N/A * Delete using a cursor.
1N/A */
1N/Astatic int
1N/A__bam_c_del(dbc, flags)
1N/A DBC *dbc;
1N/A u_int32_t flags;
1N/A{
1N/A CURSOR *cp;
1N/A DB *dbp;
1N/A DB_LOCK lock;
1N/A PAGE *h;
1N/A db_pgno_t pgno;
1N/A db_indx_t indx;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A cp = dbc->internal;
1N/A h = NULL;
1N/A
1N/A DB_PANIC_CHECK(dbp);
1N/A
1N/A /* Check for invalid flags. */
1N/A if ((ret = __db_cdelchk(dbp, flags,
1N/A F_ISSET(dbp, DB_AM_RDONLY), cp->pgno != PGNO_INVALID)) != 0)
1N/A return (ret);
1N/A
1N/A /*
1N/A * If we are running CDB, this had better be either a write
1N/A * cursor or an immediate writer.
1N/A */
1N/A if (F_ISSET(dbp, DB_AM_CDB))
1N/A if (!F_ISSET(dbc, DBC_RMW | DBC_WRITER))
1N/A return (EINVAL);
1N/A
1N/A DEBUG_LWRITE(dbc, dbc->txn, "bam_c_del", NULL, NULL, flags);
1N/A
1N/A /* If already deleted, return failure. */
1N/A if (F_ISSET(cp, C_DELETED))
1N/A return (DB_KEYEMPTY);
1N/A
1N/A /*
1N/A * We don't physically delete the record until the cursor moves,
1N/A * so we have to have a long-lived write lock on the page instead
1N/A * of a long-lived read lock. Note, we have to have a read lock
1N/A * to even get here, so we simply discard it.
1N/A */
1N/A if (F_ISSET(dbp, DB_AM_LOCKING) && cp->mode != DB_LOCK_WRITE) {
1N/A if ((ret = __bam_lget(dbc,
1N/A 0, cp->pgno, DB_LOCK_WRITE, &lock)) != 0)
1N/A goto err;
1N/A (void)__BT_TLPUT(dbc, cp->lock);
1N/A cp->lock = lock;
1N/A cp->mode = DB_LOCK_WRITE;
1N/A }
1N/A
1N/A /*
1N/A * Acquire the underlying page (which may be different from the above
1N/A * page because it may be a duplicate page), and set the on-page and
1N/A * in-cursor delete flags. We don't need to lock it as we've already
1N/A * write-locked the page leading to it.
1N/A */
1N/A if (cp->dpgno == PGNO_INVALID) {
1N/A pgno = cp->pgno;
1N/A indx = cp->indx;
1N/A } else {
1N/A pgno = cp->dpgno;
1N/A indx = cp->dindx;
1N/A }
1N/A
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
1N/A goto err;
1N/A
1N/A /* Log the change. */
1N/A if (DB_LOGGING(dbc) &&
1N/A (ret = __bam_cdel_log(dbp->dbenv->lg_info, dbc->txn, &LSN(h),
1N/A 0, dbp->log_fileid, PGNO(h), &LSN(h), indx)) != 0) {
1N/A (void)memp_fput(dbp->mpf, h, 0);
1N/A goto err;
1N/A }
1N/A
1N/A /*
1N/A * Set the intent-to-delete flag on the page and update all cursors. */
1N/A if (cp->dpgno == PGNO_INVALID)
1N/A B_DSET(GET_BKEYDATA(h, indx + O_INDX)->type);
1N/A else
1N/A B_DSET(GET_BKEYDATA(h, indx)->type);
1N/A (void)__bam_ca_delete(dbp, pgno, indx, 1);
1N/A
1N/A ret = memp_fput(dbp->mpf, h, DB_MPOOL_DIRTY);
1N/A h = NULL;
1N/A
1N/A /*
1N/A * If the tree has record numbers, we have to adjust the counts.
1N/A *
1N/A * !!!
1N/A * This test is right -- we don't yet support duplicates and record
1N/A * numbers in the same tree, so ignore duplicates if DB_BT_RECNUM
1N/A * set.
1N/A */
1N/A if (F_ISSET(dbp, DB_BT_RECNUM)) {
1N/A if ((ret = __bam_c_getstack(dbc, cp)) != 0)
1N/A goto err;
1N/A if ((ret = __bam_adjust(dbc, -1)) != 0)
1N/A goto err;
1N/A (void)__bam_stkrel(dbc, 0);
1N/A }
1N/A
1N/Aerr: if (h != NULL)
1N/A (void)memp_fput(dbp->mpf, h, 0);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_get --
1N/A * Get using a cursor (btree).
1N/A */
1N/Astatic int
1N/A__bam_c_get(dbc, key, data, flags)
1N/A DBC *dbc;
1N/A DBT *key, *data;
1N/A u_int32_t flags;
1N/A{
1N/A CURSOR *cp, copy, start;
1N/A DB *dbp;
1N/A PAGE *h;
1N/A int exact, ret, tmp_rmw;
1N/A
1N/A dbp = dbc->dbp;
1N/A cp = dbc->internal;
1N/A
1N/A DB_PANIC_CHECK(dbp);
1N/A
1N/A /* Check for invalid flags. */
1N/A if ((ret = __db_cgetchk(dbp,
1N/A key, data, flags, cp->pgno != PGNO_INVALID)) != 0)
1N/A return (ret);
1N/A
1N/A /* Clear OR'd in additional bits so we can check for flag equality. */
1N/A tmp_rmw = 0;
1N/A if (LF_ISSET(DB_RMW)) {
1N/A if (!F_ISSET(dbp, DB_AM_CDB)) {
1N/A tmp_rmw = 1;
1N/A F_SET(dbc, DBC_RMW);
1N/A }
1N/A LF_CLR(DB_RMW);
1N/A }
1N/A
1N/A DEBUG_LREAD(dbc, dbc->txn, "bam_c_get",
1N/A flags == DB_SET || flags == DB_SET_RANGE ? key : NULL, NULL, flags);
1N/A
1N/A /*
1N/A * Return a cursor's record number. It has nothing to do with the
1N/A * cursor get code except that it's been rammed into the interface.
1N/A */
1N/A if (flags == DB_GET_RECNO) {
1N/A ret = __bam_c_rget(dbc, data, flags);
1N/A if (tmp_rmw)
1N/A F_CLR(dbc, DBC_RMW);
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * Initialize the cursor for a new retrieval. Clear the cursor's
1N/A * page pointer, it was set before this operation, and no longer
1N/A * has any meaning.
1N/A */
1N/A cp->page = NULL;
1N/A copy = *cp;
1N/A cp->lock = LOCK_INVALID;
1N/A
1N/A switch (flags) {
1N/A case DB_CURRENT:
1N/A /* It's not possible to return a deleted record. */
1N/A if (F_ISSET(cp, C_DELETED)) {
1N/A ret = DB_KEYEMPTY;
1N/A goto err;
1N/A }
1N/A
1N/A /* Acquire the current page. */
1N/A if ((ret = __bam_lget(dbc,
1N/A 0, cp->pgno, DB_LOCK_READ, &cp->lock)) == 0)
1N/A ret = memp_fget(dbp->mpf,
1N/A cp->dpgno == PGNO_INVALID ? &cp->pgno : &cp->dpgno,
1N/A 0, &cp->page);
1N/A if (ret != 0)
1N/A goto err;
1N/A break;
1N/A case DB_NEXT_DUP:
1N/A if (cp->pgno == PGNO_INVALID) {
1N/A ret = EINVAL;
1N/A goto err;
1N/A }
1N/A if ((ret = __bam_c_next(dbc, cp, 1)) != 0)
1N/A goto err;
1N/A
1N/A /* Make sure we didn't go past the end of the duplicates. */
1N/A if (!POSSIBLE_DUPLICATE(cp, copy)) {
1N/A ret = DB_NOTFOUND;
1N/A goto err;
1N/A }
1N/A break;
1N/A case DB_NEXT:
1N/A if (cp->pgno != PGNO_INVALID) {
1N/A if ((ret = __bam_c_next(dbc, cp, 1)) != 0)
1N/A goto err;
1N/A break;
1N/A }
1N/A /* FALLTHROUGH */
1N/A case DB_FIRST:
1N/A if ((ret = __bam_c_first(dbc, cp)) != 0)
1N/A goto err;
1N/A break;
1N/A case DB_PREV:
1N/A if (cp->pgno != PGNO_INVALID) {
1N/A if ((ret = __bam_c_prev(dbc, cp)) != 0)
1N/A goto err;
1N/A break;
1N/A }
1N/A /* FALLTHROUGH */
1N/A case DB_LAST:
1N/A if ((ret = __bam_c_last(dbc, cp)) != 0)
1N/A goto err;
1N/A break;
1N/A case DB_SET:
1N/A if ((ret = __bam_c_search(dbc, cp, key, flags, &exact)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * We cannot currently be referencing a deleted record, but we
1N/A * may be referencing off-page duplicates.
1N/A *
1N/A * If we're referencing off-page duplicates, move off-page.
1N/A * If we moved off-page, move to the next non-deleted record.
1N/A * If we moved to the next non-deleted record, check to make
1N/A * sure we didn't switch records because our current record
1N/A * had no non-deleted data items.
1N/A */
1N/A start = *cp;
1N/A if ((ret = __bam_dup(dbc, cp, cp->indx, 0)) != 0)
1N/A goto err;
1N/A if (cp->dpgno != PGNO_INVALID && IS_CUR_DELETED(cp)) {
1N/A if ((ret = __bam_c_next(dbc, cp, 0)) != 0)
1N/A goto err;
1N/A if (!POSSIBLE_DUPLICATE(cp, start)) {
1N/A ret = DB_NOTFOUND;
1N/A goto err;
1N/A }
1N/A }
1N/A break;
1N/A case DB_SET_RECNO:
1N/A if ((ret = __bam_c_search(dbc, cp, key, flags, &exact)) != 0)
1N/A goto err;
1N/A break;
1N/A case DB_GET_BOTH:
1N/A if (F_ISSET(dbc, DBC_CONTINUE | DBC_KEYSET)) {
1N/A /* Acquire the current page. */
1N/A if ((ret = memp_fget(dbp->mpf,
1N/A cp->dpgno == PGNO_INVALID ? &cp->pgno : &cp->dpgno,
1N/A 0, &cp->page)) != 0)
1N/A goto err;
1N/A
1N/A /* If DBC_CONTINUE, move to the next item. */
1N/A if (F_ISSET(dbc, DBC_CONTINUE) &&
1N/A (ret = __bam_c_next(dbc, cp, 1)) != 0)
1N/A goto err;
1N/A } else {
1N/A if ((ret =
1N/A __bam_c_search(dbc, cp, key, flags, &exact)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * We may be referencing a duplicates page. Move to
1N/A * the first duplicate.
1N/A */
1N/A if ((ret = __bam_dup(dbc, cp, cp->indx, 0)) != 0)
1N/A goto err;
1N/A }
1N/A
1N/A /* Search for a matching entry. */
1N/A if ((ret = __bam_dsearch(dbc, cp, data, NULL)) != 0)
1N/A goto err;
1N/A
1N/A /* Ignore deleted entries. */
1N/A if (IS_CUR_DELETED(cp)) {
1N/A ret = DB_NOTFOUND;
1N/A goto err;
1N/A }
1N/A break;
1N/A case DB_SET_RANGE:
1N/A if ((ret = __bam_c_search(dbc, cp, key, flags, &exact)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * As we didn't require an exact match, the search function
1N/A * may have returned an entry past the end of the page. If
1N/A * so, move to the next entry.
1N/A */
1N/A if (cp->indx == NUM_ENT(cp->page) &&
1N/A (ret = __bam_c_next(dbc, cp, 0)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * We may be referencing off-page duplicates, if so, move
1N/A * off-page.
1N/A */
1N/A if ((ret = __bam_dup(dbc, cp, cp->indx, 0)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * We may be referencing a deleted record, if so, move to
1N/A * the next non-deleted record.
1N/A */
1N/A if (IS_CUR_DELETED(cp) && (ret = __bam_c_next(dbc, cp, 0)) != 0)
1N/A goto err;
1N/A break;
1N/A }
1N/A
1N/A /*
1N/A * Return the key if the user didn't give us one. If we've moved to
1N/A * a duplicate page, we may no longer have a pointer to the main page,
1N/A * so we have to go get it. We know that it's already read-locked,
1N/A * however, so we don't have to acquire a new lock.
1N/A */
1N/A if (flags != DB_SET) {
1N/A if (cp->dpgno != PGNO_INVALID) {
1N/A if ((ret = memp_fget(dbp->mpf, &cp->pgno, 0, &h)) != 0)
1N/A goto err;
1N/A } else
1N/A h = cp->page;
1N/A ret = __db_ret(dbp,
1N/A h, cp->indx, key, &dbc->rkey.data, &dbc->rkey.ulen);
1N/A if (cp->dpgno != PGNO_INVALID)
1N/A (void)memp_fput(dbp->mpf, h, 0);
1N/A if (ret)
1N/A goto err;
1N/A }
1N/A
1N/A /* Return the data. */
1N/A if ((ret = __db_ret(dbp, cp->page,
1N/A cp->dpgno == PGNO_INVALID ? cp->indx + O_INDX : cp->dindx,
1N/A data, &dbc->rdata.data, &dbc->rdata.ulen)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * If the previous cursor record has been deleted, physically delete
1N/A * the entry from the page. We clear the deleted flag before we call
1N/A * the underlying delete routine so that, if an error occurs, and we
1N/A * restore the cursor, the deleted flag is cleared. This is because,
1N/A * if we manage to physically modify the page, and then restore the
1N/A * cursor, we might try to repeat the page modification when closing
1N/A * the cursor.
1N/A */
1N/A if (F_ISSET(&copy, C_DELETED)) {
1N/A F_CLR(&copy, C_DELETED);
1N/A if ((ret = __bam_c_physdel(dbc, &copy, cp->page)) != 0)
1N/A goto err;
1N/A }
1N/A F_CLR(cp, C_DELETED);
1N/A
1N/A /* Release the previous lock, if any; the current lock is retained. */
1N/A if (copy.lock != LOCK_INVALID)
1N/A (void)__BT_TLPUT(dbc, copy.lock);
1N/A
1N/A /* Release the current page. */
1N/A if ((ret = memp_fput(dbp->mpf, cp->page, 0)) != 0)
1N/A goto err;
1N/A
1N/A if (0) {
1N/Aerr: if (cp->page != NULL)
1N/A (void)memp_fput(dbp->mpf, cp->page, 0);
1N/A if (cp->lock != LOCK_INVALID)
1N/A (void)__BT_TLPUT(dbc, cp->lock);
1N/A *cp = copy;
1N/A }
1N/A
1N/A /* Release temporary lock upgrade. */
1N/A if (tmp_rmw)
1N/A F_CLR(dbc, DBC_RMW);
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __bam_dsearch --
1N/A * Search for a matching data item (or the first data item that's
1N/A * equal to or greater than the one we're searching for).
1N/A */
1N/Astatic int
1N/A__bam_dsearch(dbc, cp, data, iflagp)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A DBT *data;
1N/A u_int32_t *iflagp;
1N/A{
1N/A DB *dbp;
1N/A CURSOR copy, last;
1N/A int cmp, ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A
1N/A /*
1N/A * If iflagp is non-NULL, we're doing an insert.
1N/A *
1N/A * If the duplicates are off-page, use the duplicate search routine.
1N/A */
1N/A if (cp->dpgno != PGNO_INVALID) {
1N/A if ((ret = __db_dsearch(dbc, iflagp != NULL,
1N/A data, cp->dpgno, &cp->dindx, &cp->page, &cmp)) != 0)
1N/A return (ret);
1N/A cp->dpgno = cp->page->pgno;
1N/A
1N/A if (iflagp == NULL) {
1N/A if (cmp != 0)
1N/A return (DB_NOTFOUND);
1N/A return (0);
1N/A }
1N/A *iflagp = DB_BEFORE;
1N/A return (0);
1N/A }
1N/A
1N/A /* Otherwise, do the search ourselves. */
1N/A copy = *cp;
1N/A for (;;) {
1N/A /* Save the last interesting cursor position. */
1N/A last = *cp;
1N/A
1N/A /* See if the data item matches the one we're looking for. */
1N/A if ((cmp = __bam_cmp(dbp, data, cp->page, cp->indx + O_INDX,
1N/A dbp->dup_compare == NULL ?
1N/A __bam_defcmp : dbp->dup_compare)) == 0) {
1N/A if (iflagp != NULL)
1N/A *iflagp = DB_AFTER;
1N/A return (0);
1N/A }
1N/A
1N/A /*
1N/A * If duplicate entries are sorted, we're done if we find a
1N/A * page entry that sorts greater than the application item.
1N/A * If doing an insert, return success, otherwise DB_NOTFOUND.
1N/A */
1N/A if (dbp->dup_compare != NULL && cmp < 0) {
1N/A if (iflagp == NULL)
1N/A return (DB_NOTFOUND);
1N/A *iflagp = DB_BEFORE;
1N/A return (0);
1N/A }
1N/A
1N/A /*
1N/A * Move to the next item. If we reach the end of the page and
1N/A * we're doing an insert, set the cursor to the last item and
1N/A * set the referenced memory location so callers know to insert
1N/A * after the item, instead of before it. If not inserting, we
1N/A * return DB_NOTFOUND.
1N/A */
1N/A if ((cp->indx += P_INDX) >= NUM_ENT(cp->page)) {
1N/A if (iflagp == NULL)
1N/A return (DB_NOTFOUND);
1N/A goto use_last;
1N/A }
1N/A
1N/A /*
1N/A * Make sure we didn't go past the end of the duplicates. The
1N/A * error conditions are the same as above.
1N/A */
1N/A if (!POSSIBLE_DUPLICATE(cp, copy)) {
1N/A if (iflagp == NULL)
1N/A return (DB_NOTFOUND);
1N/Ause_last: *cp = last;
1N/A *iflagp = DB_AFTER;
1N/A return (0);
1N/A }
1N/A }
1N/A /* NOTREACHED */
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_rget --
1N/A * Return the record number for a cursor.
1N/A */
1N/Astatic int
1N/A__bam_c_rget(dbc, data, flags)
1N/A DBC *dbc;
1N/A DBT *data;
1N/A u_int32_t flags;
1N/A{
1N/A CURSOR *cp;
1N/A DB *dbp;
1N/A DBT dbt;
1N/A db_recno_t recno;
1N/A int exact, ret;
1N/A
1N/A COMPQUIET(flags, 0);
1N/A dbp = dbc->dbp;
1N/A cp = dbc->internal;
1N/A
1N/A /* Get the page with the current item on it. */
1N/A if ((ret = memp_fget(dbp->mpf, &cp->pgno, 0, &cp->page)) != 0)
1N/A return (ret);
1N/A
1N/A /* Get a copy of the key. */
1N/A memset(&dbt, 0, sizeof(DBT));
1N/A dbt.flags = DB_DBT_MALLOC | DB_DBT_INTERNAL;
1N/A if ((ret = __db_ret(dbp, cp->page, cp->indx, &dbt, NULL, NULL)) != 0)
1N/A goto err;
1N/A
1N/A exact = 1;
1N/A if ((ret = __bam_search(dbc, &dbt,
1N/A F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND,
1N/A 1, &recno, &exact)) != 0)
1N/A goto err;
1N/A
1N/A ret = __db_retcopy(data, &recno, sizeof(recno),
1N/A &dbc->rdata.data, &dbc->rdata.ulen, dbp->db_malloc);
1N/A
1N/A /* Release the stack. */
1N/A __bam_stkrel(dbc, 0);
1N/A
1N/Aerr: (void)memp_fput(dbp->mpf, cp->page, 0);
1N/A __os_free(dbt.data, dbt.size);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_put --
1N/A * Put using a cursor.
1N/A */
1N/Astatic int
1N/A__bam_c_put(dbc, key, data, flags)
1N/A DBC *dbc;
1N/A DBT *key, *data;
1N/A u_int32_t flags;
1N/A{
1N/A CURSOR *cp, copy;
1N/A DB *dbp;
1N/A DBT dbt;
1N/A db_indx_t indx;
1N/A db_pgno_t pgno;
1N/A u_int32_t iiflags, iiop;
1N/A int exact, needkey, ret, stack;
1N/A void *arg;
1N/A
1N/A dbp = dbc->dbp;
1N/A cp = dbc->internal;
1N/A
1N/A DB_PANIC_CHECK(dbp);
1N/A
1N/A DEBUG_LWRITE(dbc, dbc->txn, "bam_c_put",
1N/A flags == DB_KEYFIRST || flags == DB_KEYLAST ? key : NULL,
1N/A data, flags);
1N/A
1N/A if ((ret = __db_cputchk(dbp, key, data, flags,
1N/A F_ISSET(dbp, DB_AM_RDONLY), cp->pgno != PGNO_INVALID)) != 0)
1N/A return (ret);
1N/A
1N/A /*
1N/A * If we are running CDB, this had better be either a write
1N/A * cursor or an immediate writer. If it's a regular writer,
1N/A * that means we have an IWRITE lock and we need to upgrade
1N/A * it to a write lock.
1N/A */
1N/A if (F_ISSET(dbp, DB_AM_CDB)) {
1N/A if (!F_ISSET(dbc, DBC_RMW | DBC_WRITER))
1N/A return (EINVAL);
1N/A
1N/A if (F_ISSET(dbc, DBC_RMW) &&
1N/A (ret = lock_get(dbp->dbenv->lk_info, dbc->locker,
1N/A DB_LOCK_UPGRADE, &dbc->lock_dbt, DB_LOCK_WRITE,
1N/A &dbc->mylock)) != 0)
1N/A return (EAGAIN);
1N/A }
1N/A
1N/A if (0) {
1N/Asplit: /*
1N/A * To split, we need a valid key for the page. Since it's a
1N/A * cursor, we have to build one.
1N/A *
1N/A * Acquire a copy of a key from the page.
1N/A */
1N/A if (needkey) {
1N/A memset(&dbt, 0, sizeof(DBT));
1N/A if ((ret = __db_ret(dbp, cp->page, indx,
1N/A &dbt, &dbc->rkey.data, &dbc->rkey.ulen)) != 0)
1N/A goto err;
1N/A arg = &dbt;
1N/A } else
1N/A arg = key;
1N/A
1N/A /*
1N/A * Discard any locks and pinned pages (the locks are discarded
1N/A * even if we're running with transactions, as they lock pages
1N/A * that we're sorry we ever acquired). If stack is set and the
1N/A * cursor entries are valid, they point to the same entries as
1N/A * the stack, don't free them twice.
1N/A */
1N/A if (stack) {
1N/A (void)__bam_stkrel(dbc, 1);
1N/A stack = 0;
1N/A } else
1N/A DISCARD(dbc, cp);
1N/A
1N/A /*
1N/A * Restore the cursor to its original value. This is necessary
1N/A * for two reasons. First, we are about to copy it in case of
1N/A * error, again. Second, we adjust cursors during the split,
1N/A * and we have to ensure this cursor is adjusted appropriately,
1N/A * along with all the other cursors.
1N/A */
1N/A *cp = copy;
1N/A
1N/A if ((ret = __bam_split(dbc, arg)) != 0)
1N/A goto err;
1N/A }
1N/A
1N/A /*
1N/A * Initialize the cursor for a new retrieval. Clear the cursor's
1N/A * page pointer, it was set before this operation, and no longer
1N/A * has any meaning.
1N/A */
1N/A cp->page = NULL;
1N/A copy = *cp;
1N/A cp->lock = LOCK_INVALID;
1N/A
1N/A iiflags = needkey = ret = stack = 0;
1N/A switch (flags) {
1N/A case DB_AFTER:
1N/A case DB_BEFORE:
1N/A case DB_CURRENT:
1N/A needkey = 1;
1N/A if (cp->dpgno == PGNO_INVALID) {
1N/A pgno = cp->pgno;
1N/A indx = cp->indx;
1N/A } else {
1N/A pgno = cp->dpgno;
1N/A indx = cp->dindx;
1N/A }
1N/A
1N/A /*
1N/A * !!!
1N/A * This test is right -- we don't yet support duplicates and
1N/A * record numbers in the same tree, so ignore duplicates if
1N/A * DB_BT_RECNUM set.
1N/A */
1N/A if (F_ISSET(dbp, DB_BT_RECNUM) &&
1N/A (flags != DB_CURRENT || F_ISSET(cp, C_DELETED))) {
1N/A /* Acquire a complete stack. */
1N/A if ((ret = __bam_c_getstack(dbc, cp)) != 0)
1N/A goto err;
1N/A cp->page = cp->csp->page;
1N/A
1N/A stack = 1;
1N/A iiflags = BI_DOINCR;
1N/A } else {
1N/A /* Acquire the current page. */
1N/A if ((ret = __bam_lget(dbc,
1N/A 0, cp->pgno, DB_LOCK_WRITE, &cp->lock)) == 0)
1N/A ret = memp_fget(dbp->mpf, &pgno, 0, &cp->page);
1N/A if (ret != 0)
1N/A goto err;
1N/A
1N/A iiflags = 0;
1N/A }
1N/A
1N/A /*
1N/A * If the user has specified a duplicate comparison function,
1N/A * we return an error if DB_CURRENT was specified and the
1N/A * replacement data doesn't compare equal to the current data.
1N/A * This stops apps from screwing up the duplicate sort order.
1N/A */
1N/A if (flags == DB_CURRENT && dbp->dup_compare != NULL)
1N/A if (__bam_cmp(dbp, data,
1N/A cp->page, indx, dbp->dup_compare) != 0) {
1N/A ret = EINVAL;
1N/A goto err;
1N/A }
1N/A
1N/A iiop = flags;
1N/A break;
1N/A case DB_KEYFIRST:
1N/A case DB_KEYLAST:
1N/A /*
1N/A * If we have a duplicate comparison function, we position to
1N/A * the first of any on-page duplicates, and use __bam_dsearch
1N/A * to search for the right slot. Otherwise, we position to
1N/A * the first/last of any on-page duplicates based on the flag
1N/A * value.
1N/A */
1N/A if ((ret = __bam_c_search(dbc, cp, key,
1N/A flags == DB_KEYFIRST || dbp->dup_compare != NULL ?
1N/A DB_KEYFIRST : DB_KEYLAST, &exact)) != 0)
1N/A goto err;
1N/A stack = 1;
1N/A
1N/A /*
1N/A * If an exact match:
1N/A * If duplicates aren't supported, replace the current
1N/A * item. (When implementing the DB->put function, our
1N/A * caller has already checked the DB_NOOVERWRITE flag.)
1N/A *
1N/A * If there's a duplicate comparison function, find the
1N/A * correct slot for this duplicate item.
1N/A *
1N/A * If there's no duplicate comparison function, set the
1N/A * insert flag based on the argument flags.
1N/A *
1N/A * If there's no match, the search function returned the
1N/A * smallest slot greater than the key, use it.
1N/A */
1N/A if (exact) {
1N/A if (F_ISSET(dbp, DB_AM_DUP)) {
1N/A /*
1N/A * If at off-page duplicate page, move to the
1N/A * first or last entry -- if a comparison
1N/A * function was specified, start searching at
1N/A * the first entry. Otherwise, move based on
1N/A * the DB_KEYFIRST/DB_KEYLAST flags.
1N/A */
1N/A if ((ret = __bam_dup(dbc, cp, cp->indx,
1N/A dbp->dup_compare == NULL &&
1N/A flags != DB_KEYFIRST)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * If there's a comparison function, search for
1N/A * the correct slot. Otherwise, set the insert
1N/A * flag based on the argment flag.
1N/A */
1N/A if (dbp->dup_compare == NULL)
1N/A iiop = flags == DB_KEYFIRST ?
1N/A DB_BEFORE : DB_AFTER;
1N/A else
1N/A if ((ret = __bam_dsearch(dbc,
1N/A cp, data, &iiop)) != 0)
1N/A goto err;
1N/A } else
1N/A iiop = DB_CURRENT;
1N/A iiflags = 0;
1N/A } else {
1N/A iiop = DB_BEFORE;
1N/A iiflags = BI_NEWKEY;
1N/A }
1N/A
1N/A if (cp->dpgno == PGNO_INVALID) {
1N/A pgno = cp->pgno;
1N/A indx = cp->indx;
1N/A } else {
1N/A pgno = cp->dpgno;
1N/A indx = cp->dindx;
1N/A }
1N/A break;
1N/A }
1N/A
1N/A ret = __bam_iitem(dbc, &cp->page, &indx, key, data, iiop, iiflags);
1N/A
1N/A if (ret == DB_NEEDSPLIT)
1N/A goto split;
1N/A if (ret != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * Reset any cursors referencing this item that might have the item
1N/A * marked for deletion.
1N/A */
1N/A if (iiop == DB_CURRENT) {
1N/A (void)__bam_ca_delete(dbp, pgno, indx, 0);
1N/A
1N/A /*
1N/A * It's also possible that we are the cursor that had the
1N/A * item marked for deletion, in which case we want to make
1N/A * sure that we don't delete it because we had the delete
1N/A * flag set already.
1N/A */
1N/A if (cp->pgno == copy.pgno && cp->indx == copy.indx &&
1N/A cp->dpgno == copy.dpgno && cp->dindx == copy.dindx)
1N/A F_CLR(&copy, C_DELETED);
1N/A }
1N/A
1N/A /*
1N/A * Update the cursor to point to the new entry. The new entry was
1N/A * stored on the current page, because we split pages until it was
1N/A * possible.
1N/A */
1N/A if (cp->dpgno == PGNO_INVALID)
1N/A cp->indx = indx;
1N/A else
1N/A cp->dindx = indx;
1N/A
1N/A /*
1N/A * If the previous cursor record has been deleted, physically delete
1N/A * the entry from the page. We clear the deleted flag before we call
1N/A * the underlying delete routine so that, if an error occurs, and we
1N/A * restore the cursor, the deleted flag is cleared. This is because,
1N/A * if we manage to physically modify the page, and then restore the
1N/A * cursor, we might try to repeat the page modification when closing
1N/A * the cursor.
1N/A */
1N/A if (F_ISSET(&copy, C_DELETED)) {
1N/A F_CLR(&copy, C_DELETED);
1N/A if ((ret = __bam_c_physdel(dbc, &copy, cp->page)) != 0)
1N/A goto err;
1N/A }
1N/A F_CLR(cp, C_DELETED);
1N/A
1N/A /* Release the previous lock, if any; the current lock is retained. */
1N/A if (copy.lock != LOCK_INVALID)
1N/A (void)__BT_TLPUT(dbc, copy.lock);
1N/A
1N/A /*
1N/A * Discard any pages pinned in the tree and their locks, except for
1N/A * the leaf page, for which we only discard the pin, not the lock.
1N/A *
1N/A * Note, the leaf page participated in the stack we acquired, and so
1N/A * we have to adjust the stack as necessary. If there was only a
1N/A * single page on the stack, we don't have to free further stack pages.
1N/A */
1N/A if (stack && BT_STK_POP(cp) != NULL)
1N/A (void)__bam_stkrel(dbc, 0);
1N/A
1N/A /* Release the current page. */
1N/A if ((ret = memp_fput(dbp->mpf, cp->page, 0)) != 0)
1N/A goto err;
1N/A
1N/A if (0) {
1N/Aerr: /* Discard any pinned pages. */
1N/A if (stack)
1N/A (void)__bam_stkrel(dbc, 0);
1N/A else
1N/A DISCARD(dbc, cp);
1N/A *cp = copy;
1N/A }
1N/A
1N/A if (F_ISSET(dbp, DB_AM_CDB) && F_ISSET(dbc, DBC_RMW))
1N/A (void)__lock_downgrade(dbp->dbenv->lk_info, dbc->mylock,
1N/A DB_LOCK_IWRITE, 0);
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_first --
1N/A * Return the first record.
1N/A */
1N/Astatic int
1N/A__bam_c_first(dbc, cp)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A{
1N/A DB *dbp;
1N/A db_pgno_t pgno;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A
1N/A /* Walk down the left-hand side of the tree. */
1N/A for (pgno = PGNO_ROOT;;) {
1N/A if ((ret =
1N/A __bam_lget(dbc, 0, pgno, DB_LOCK_READ, &cp->lock)) != 0)
1N/A return (ret);
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &cp->page)) != 0)
1N/A return (ret);
1N/A
1N/A /* If we find a leaf page, we're done. */
1N/A if (ISLEAF(cp->page))
1N/A break;
1N/A
1N/A pgno = GET_BINTERNAL(cp->page, 0)->pgno;
1N/A DISCARD(dbc, cp);
1N/A }
1N/A
1N/A cp->pgno = cp->page->pgno;
1N/A cp->indx = 0;
1N/A cp->dpgno = PGNO_INVALID;
1N/A
1N/A /* Check for duplicates. */
1N/A if ((ret = __bam_dup(dbc, cp, cp->indx, 0)) != 0)
1N/A return (ret);
1N/A
1N/A /* If on an empty page or a deleted record, move to the next one. */
1N/A if (NUM_ENT(cp->page) == 0 || IS_CUR_DELETED(cp))
1N/A if ((ret = __bam_c_next(dbc, cp, 0)) != 0)
1N/A return (ret);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_last --
1N/A * Return the last record.
1N/A */
1N/Astatic int
1N/A__bam_c_last(dbc, cp)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A{
1N/A DB *dbp;
1N/A db_pgno_t pgno;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A
1N/A /* Walk down the right-hand side of the tree. */
1N/A for (pgno = PGNO_ROOT;;) {
1N/A if ((ret =
1N/A __bam_lget(dbc, 0, pgno, DB_LOCK_READ, &cp->lock)) != 0)
1N/A return (ret);
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &cp->page)) != 0)
1N/A return (ret);
1N/A
1N/A /* If we find a leaf page, we're done. */
1N/A if (ISLEAF(cp->page))
1N/A break;
1N/A
1N/A pgno =
1N/A GET_BINTERNAL(cp->page, NUM_ENT(cp->page) - O_INDX)->pgno;
1N/A DISCARD(dbc, cp);
1N/A }
1N/A
1N/A cp->pgno = cp->page->pgno;
1N/A cp->indx = NUM_ENT(cp->page) == 0 ? 0 : NUM_ENT(cp->page) - P_INDX;
1N/A cp->dpgno = PGNO_INVALID;
1N/A
1N/A /* Check for duplicates. */
1N/A if ((ret = __bam_dup(dbc, cp, cp->indx, 1)) != 0)
1N/A return (ret);
1N/A
1N/A /* If on an empty page or a deleted record, move to the next one. */
1N/A if (NUM_ENT(cp->page) == 0 || IS_CUR_DELETED(cp))
1N/A if ((ret = __bam_c_prev(dbc, cp)) != 0)
1N/A return (ret);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_next --
1N/A * Move to the next record.
1N/A */
1N/Astatic int
1N/A__bam_c_next(dbc, cp, initial_move)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A int initial_move;
1N/A{
1N/A DB *dbp;
1N/A db_indx_t adjust, indx;
1N/A db_pgno_t pgno;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A
1N/A /*
1N/A * We're either moving through a page of duplicates or a btree leaf
1N/A * page.
1N/A */
1N/A if (cp->dpgno == PGNO_INVALID) {
1N/A adjust = dbp->type == DB_BTREE ? P_INDX : O_INDX;
1N/A pgno = cp->pgno;
1N/A indx = cp->indx;
1N/A } else {
1N/A adjust = O_INDX;
1N/A pgno = cp->dpgno;
1N/A indx = cp->dindx;
1N/A }
1N/A if (cp->page == NULL) {
1N/A if ((ret =
1N/A __bam_lget(dbc, 0, pgno, DB_LOCK_READ, &cp->lock)) != 0)
1N/A return (ret);
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &cp->page)) != 0)
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * If at the end of the page, move to a subsequent page.
1N/A *
1N/A * !!!
1N/A * Check for >= NUM_ENT. If we're here as the result of a search that
1N/A * landed us on NUM_ENT, we'll increment indx before we test.
1N/A *
1N/A * !!!
1N/A * This code handles empty pages and pages with only deleted entries.
1N/A */
1N/A if (initial_move)
1N/A indx += adjust;
1N/A for (;;) {
1N/A if (indx >= NUM_ENT(cp->page)) {
1N/A /*
1N/A * If we're in a btree leaf page, we've reached the end
1N/A * of the tree. If we've reached the end of a page of
1N/A * duplicates, continue from the btree leaf page where
1N/A * we found this page of duplicates.
1N/A */
1N/A pgno = cp->page->next_pgno;
1N/A if (pgno == PGNO_INVALID) {
1N/A /* If in a btree leaf page, it's EOF. */
1N/A if (cp->dpgno == PGNO_INVALID)
1N/A return (DB_NOTFOUND);
1N/A
1N/A /* Continue from the last btree leaf page. */
1N/A cp->dpgno = PGNO_INVALID;
1N/A
1N/A adjust = P_INDX;
1N/A pgno = cp->pgno;
1N/A indx = cp->indx + P_INDX;
1N/A } else
1N/A indx = 0;
1N/A
1N/A DISCARD(dbc, cp);
1N/A if ((ret = __bam_lget(dbc,
1N/A 0, pgno, DB_LOCK_READ, &cp->lock)) != 0)
1N/A return (ret);
1N/A if ((ret =
1N/A memp_fget(dbp->mpf, &pgno, 0, &cp->page)) != 0)
1N/A return (ret);
1N/A continue;
1N/A }
1N/A
1N/A /* Ignore deleted records. */
1N/A if (IS_DELETED(cp, indx)) {
1N/A indx += adjust;
1N/A continue;
1N/A }
1N/A
1N/A /*
1N/A * If we're not in a duplicates page, check to see if we've
1N/A * found a page of duplicates, in which case we move to the
1N/A * first entry.
1N/A */
1N/A if (cp->dpgno == PGNO_INVALID) {
1N/A cp->pgno = cp->page->pgno;
1N/A cp->indx = indx;
1N/A
1N/A if ((ret = __bam_dup(dbc, cp, indx, 0)) != 0)
1N/A return (ret);
1N/A if (cp->dpgno != PGNO_INVALID) {
1N/A indx = cp->dindx;
1N/A adjust = O_INDX;
1N/A continue;
1N/A }
1N/A } else {
1N/A cp->dpgno = cp->page->pgno;
1N/A cp->dindx = indx;
1N/A }
1N/A break;
1N/A }
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_prev --
1N/A * Move to the previous record.
1N/A */
1N/Astatic int
1N/A__bam_c_prev(dbc, cp)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A{
1N/A DB *dbp;
1N/A db_indx_t indx, adjust;
1N/A db_pgno_t pgno;
1N/A int ret, set_indx;
1N/A
1N/A dbp = dbc->dbp;
1N/A
1N/A /*
1N/A * We're either moving through a page of duplicates or a btree leaf
1N/A * page.
1N/A */
1N/A if (cp->dpgno == PGNO_INVALID) {
1N/A adjust = dbp->type == DB_BTREE ? P_INDX : O_INDX;
1N/A pgno = cp->pgno;
1N/A indx = cp->indx;
1N/A } else {
1N/A adjust = O_INDX;
1N/A pgno = cp->dpgno;
1N/A indx = cp->dindx;
1N/A }
1N/A if (cp->page == NULL) {
1N/A if ((ret =
1N/A __bam_lget(dbc, 0, pgno, DB_LOCK_READ, &cp->lock)) != 0)
1N/A return (ret);
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &cp->page)) != 0)
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * If at the beginning of the page, move to any previous one.
1N/A *
1N/A * !!!
1N/A * This code handles empty pages and pages with only deleted entries.
1N/A */
1N/A for (;;) {
1N/A if (indx == 0) {
1N/A /*
1N/A * If we're in a btree leaf page, we've reached the
1N/A * beginning of the tree. If we've reached the first
1N/A * of a page of duplicates, continue from the btree
1N/A * leaf page where we found this page of duplicates.
1N/A */
1N/A pgno = cp->page->prev_pgno;
1N/A if (pgno == PGNO_INVALID) {
1N/A /* If in a btree leaf page, it's SOF. */
1N/A if (cp->dpgno == PGNO_INVALID)
1N/A return (DB_NOTFOUND);
1N/A
1N/A /* Continue from the last btree leaf page. */
1N/A cp->dpgno = PGNO_INVALID;
1N/A
1N/A adjust = P_INDX;
1N/A pgno = cp->pgno;
1N/A indx = cp->indx;
1N/A set_indx = 0;
1N/A } else
1N/A set_indx = 1;
1N/A
1N/A DISCARD(dbc, cp);
1N/A if ((ret = __bam_lget(dbc,
1N/A 0, pgno, DB_LOCK_READ, &cp->lock)) != 0)
1N/A return (ret);
1N/A if ((ret =
1N/A memp_fget(dbp->mpf, &pgno, 0, &cp->page)) != 0)
1N/A return (ret);
1N/A
1N/A if (set_indx)
1N/A indx = NUM_ENT(cp->page);
1N/A if (indx == 0)
1N/A continue;
1N/A }
1N/A
1N/A /* Ignore deleted records. */
1N/A indx -= adjust;
1N/A if (IS_DELETED(cp, indx))
1N/A continue;
1N/A
1N/A /*
1N/A * If we're not in a duplicates page, check to see if we've
1N/A * found a page of duplicates, in which case we move to the
1N/A * last entry.
1N/A */
1N/A if (cp->dpgno == PGNO_INVALID) {
1N/A cp->pgno = cp->page->pgno;
1N/A cp->indx = indx;
1N/A
1N/A if ((ret = __bam_dup(dbc, cp, indx, 1)) != 0)
1N/A return (ret);
1N/A if (cp->dpgno != PGNO_INVALID) {
1N/A indx = cp->dindx + O_INDX;
1N/A adjust = O_INDX;
1N/A continue;
1N/A }
1N/A } else {
1N/A cp->dpgno = cp->page->pgno;
1N/A cp->dindx = indx;
1N/A }
1N/A break;
1N/A }
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_search --
1N/A * Move to a specified record.
1N/A */
1N/Astatic int
1N/A__bam_c_search(dbc, cp, key, flags, exactp)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A const DBT *key;
1N/A u_int32_t flags;
1N/A int *exactp;
1N/A{
1N/A BTREE *t;
1N/A DB *dbp;
1N/A DB_LOCK lock;
1N/A PAGE *h;
1N/A db_recno_t recno;
1N/A db_indx_t indx;
1N/A u_int32_t sflags;
1N/A int cmp, needexact, ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A t = dbp->internal;
1N/A
1N/A /* Find an entry in the database. */
1N/A switch (flags) {
1N/A case DB_SET_RECNO:
1N/A if ((ret = __ram_getno(dbc, key, &recno, 0)) != 0)
1N/A return (ret);
1N/A sflags = F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND;
1N/A needexact = *exactp = 1;
1N/A ret = __bam_rsearch(dbc, &recno, sflags, 1, exactp);
1N/A break;
1N/A case DB_SET:
1N/A case DB_GET_BOTH:
1N/A sflags = F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND;
1N/A needexact = *exactp = 1;
1N/A goto search;
1N/A case DB_SET_RANGE:
1N/A sflags = F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND;
1N/A needexact = *exactp = 0;
1N/A goto search;
1N/A case DB_KEYFIRST:
1N/A sflags = S_KEYFIRST;
1N/A goto fast_search;
1N/A case DB_KEYLAST:
1N/A sflags = S_KEYLAST;
1N/Afast_search: needexact = *exactp = 0;
1N/A /*
1N/A * If the application has a history of inserting into the first
1N/A * or last pages of the database, we check those pages first to
1N/A * avoid doing a full search.
1N/A *
1N/A * Record numbers can't be fast-tracked, the entire tree has to
1N/A * be locked.
1N/A */
1N/A h = NULL;
1N/A lock = LOCK_INVALID;
1N/A if (F_ISSET(dbp, DB_BT_RECNUM))
1N/A goto search;
1N/A
1N/A /* Check if the application has a history of sorted input. */
1N/A if (t->bt_lpgno == PGNO_INVALID)
1N/A goto search;
1N/A
1N/A /*
1N/A * Lock and retrieve the page on which we did the last insert.
1N/A * It's okay if it doesn't exist, or if it's not the page type
1N/A * we expected, it just means that the world changed.
1N/A */
1N/A if (__bam_lget(dbc, 0, t->bt_lpgno, DB_LOCK_WRITE, &lock))
1N/A goto fast_miss;
1N/A if (memp_fget(dbp->mpf, &t->bt_lpgno, 0, &h))
1N/A goto fast_miss;
1N/A if (TYPE(h) != P_LBTREE)
1N/A goto fast_miss;
1N/A if (NUM_ENT(h) == 0)
1N/A goto fast_miss;
1N/A
1N/A /*
1N/A * What we do here is test to see if we're at the beginning or
1N/A * end of the tree and if the new item sorts before/after the
1N/A * first/last page entry. We don't try and catch inserts into
1N/A * the middle of the tree (although we could, as long as there
1N/A * were two keys on the page and we saved both the index and
1N/A * the page number of the last insert).
1N/A */
1N/A if (h->next_pgno == PGNO_INVALID) {
1N/A indx = NUM_ENT(h) - P_INDX;
1N/A if ((cmp =
1N/A __bam_cmp(dbp, key, h, indx, t->bt_compare)) < 0)
1N/A goto try_begin;
1N/A if (cmp > 0) {
1N/A indx += P_INDX;
1N/A goto fast_hit;
1N/A }
1N/A
1N/A /*
1N/A * Found a duplicate. If doing DB_KEYLAST, we're at
1N/A * the correct position, otherwise, move to the first
1N/A * of the duplicates.
1N/A */
1N/A if (flags == DB_KEYLAST)
1N/A goto fast_hit;
1N/A for (;
1N/A indx > 0 && h->inp[indx - P_INDX] == h->inp[indx];
1N/A indx -= P_INDX)
1N/A ;
1N/A goto fast_hit;
1N/A }
1N/Atry_begin: if (h->prev_pgno == PGNO_INVALID) {
1N/A indx = 0;
1N/A if ((cmp =
1N/A __bam_cmp(dbp, key, h, indx, t->bt_compare)) > 0)
1N/A goto fast_miss;
1N/A if (cmp < 0)
1N/A goto fast_hit;
1N/A /*
1N/A * Found a duplicate. If doing DB_KEYFIRST, we're at
1N/A * the correct position, otherwise, move to the last
1N/A * of the duplicates.
1N/A */
1N/A if (flags == DB_KEYFIRST)
1N/A goto fast_hit;
1N/A for (;
1N/A indx < (db_indx_t)(NUM_ENT(h) - P_INDX) &&
1N/A h->inp[indx] == h->inp[indx + P_INDX];
1N/A indx += P_INDX)
1N/A ;
1N/A goto fast_hit;
1N/A }
1N/A goto fast_miss;
1N/A
1N/Afast_hit: /* Set the exact match flag, we may have found a duplicate. */
1N/A *exactp = cmp == 0;
1N/A
1N/A /* Enter the entry in the stack. */
1N/A BT_STK_CLR(cp);
1N/A BT_STK_ENTER(cp, h, indx, lock, ret);
1N/A break;
1N/A
1N/Afast_miss: if (h != NULL)
1N/A (void)memp_fput(dbp->mpf, h, 0);
1N/A if (lock != LOCK_INVALID)
1N/A (void)__BT_LPUT(dbc, lock);
1N/A
1N/Asearch: ret = __bam_search(dbc, key, sflags, 1, NULL, exactp);
1N/A break;
1N/A default: /* XXX: Impossible. */
1N/A abort();
1N/A /* NOTREACHED */
1N/A }
1N/A if (ret != 0)
1N/A return (ret);
1N/A
1N/A /*
1N/A * Initialize the cursor to reference it. This has to be done
1N/A * before we return (even with DB_NOTFOUND) because we have to
1N/A * free the page(s) we locked in __bam_search.
1N/A */
1N/A cp->page = cp->csp->page;
1N/A cp->pgno = cp->csp->page->pgno;
1N/A cp->indx = cp->csp->indx;
1N/A cp->lock = cp->csp->lock;
1N/A cp->dpgno = PGNO_INVALID;
1N/A
1N/A /*
1N/A * If we inserted a key into the first or last slot of the tree,
1N/A * remember where it was so we can do it more quickly next time.
1N/A */
1N/A if (flags == DB_KEYFIRST || flags == DB_KEYLAST)
1N/A t->bt_lpgno =
1N/A ((cp->page->next_pgno == PGNO_INVALID &&
1N/A cp->indx >= NUM_ENT(cp->page)) ||
1N/A (cp->page->prev_pgno == PGNO_INVALID && cp->indx == 0)) ?
1N/A cp->pgno : PGNO_INVALID;
1N/A
1N/A /* If we need an exact match and didn't find one, we're done. */
1N/A if (needexact && *exactp == 0)
1N/A return (DB_NOTFOUND);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_dup --
1N/A * Check for an off-page duplicates entry, and if found, move to the
1N/A * first or last entry.
1N/A *
1N/A * PUBLIC: int __bam_dup __P((DBC *, CURSOR *, u_int32_t, int));
1N/A */
1N/Aint
1N/A__bam_dup(dbc, cp, indx, last_dup)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A u_int32_t indx;
1N/A int last_dup;
1N/A{
1N/A BOVERFLOW *bo;
1N/A DB *dbp;
1N/A db_pgno_t pgno;
1N/A int ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A
1N/A /*
1N/A * Check for an overflow entry. If we find one, move to the
1N/A * duplicates page, and optionally move to the last record on
1N/A * that page.
1N/A *
1N/A * !!!
1N/A * We don't lock duplicates pages, we've already got the correct
1N/A * lock on the main page.
1N/A */
1N/A bo = GET_BOVERFLOW(cp->page, indx + O_INDX);
1N/A if (B_TYPE(bo->type) != B_DUPLICATE)
1N/A return (0);
1N/A
1N/A pgno = bo->pgno;
1N/A if ((ret = memp_fput(dbp->mpf, cp->page, 0)) != 0)
1N/A return (ret);
1N/A cp->page = NULL;
1N/A if (last_dup) {
1N/A if ((ret = __db_dend(dbc, pgno, &cp->page)) != 0)
1N/A return (ret);
1N/A indx = NUM_ENT(cp->page) - O_INDX;
1N/A } else {
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &cp->page)) != 0)
1N/A return (ret);
1N/A indx = 0;
1N/A }
1N/A
1N/A /* Update the cursor's duplicate information. */
1N/A cp->dpgno = cp->page->pgno;
1N/A cp->dindx = indx;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_physdel --
1N/A * Actually do the cursor deletion.
1N/A */
1N/Astatic int
1N/A__bam_c_physdel(dbc, cp, h)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A PAGE *h;
1N/A{
1N/A enum { DELETE_ITEM, DELETE_PAGE, NOTHING_FURTHER } cmd;
1N/A BOVERFLOW bo;
1N/A DB *dbp;
1N/A DBT dbt;
1N/A DB_LOCK lock;
1N/A db_indx_t indx;
1N/A db_pgno_t pgno, next_pgno, prev_pgno;
1N/A int delete_page, local_page, ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A
1N/A delete_page = ret = 0;
1N/A
1N/A /* Figure out what we're deleting. */
1N/A if (cp->dpgno == PGNO_INVALID) {
1N/A pgno = cp->pgno;
1N/A indx = cp->indx;
1N/A } else {
1N/A pgno = cp->dpgno;
1N/A indx = cp->dindx;
1N/A }
1N/A
1N/A /*
1N/A * If the item is referenced by another cursor, set that cursor's
1N/A * delete flag and leave it up to it to do the delete.
1N/A *
1N/A * !!!
1N/A * This test for > 0 is a tricky. There are two ways that we can
1N/A * be called here. Either we are closing the cursor or we've moved
1N/A * off the page with the deleted entry. In the first case, we've
1N/A * already removed the cursor from the active queue, so we won't see
1N/A * it in __bam_ca_delete. In the second case, it will be on a different
1N/A * item, so we won't bother with it in __bam_ca_delete.
1N/A */
1N/A if (__bam_ca_delete(dbp, pgno, indx, 1) > 0)
1N/A return (0);
1N/A
1N/A /*
1N/A * If this is concurrent DB, upgrade the lock if necessary.
1N/A */
1N/A if (F_ISSET(dbp, DB_AM_CDB) && F_ISSET(dbc, DBC_RMW) &&
1N/A (ret = lock_get(dbp->dbenv->lk_info,
1N/A dbc->locker, DB_LOCK_UPGRADE, &dbc->lock_dbt, DB_LOCK_WRITE,
1N/A &dbc->mylock)) != 0)
1N/A return (EAGAIN);
1N/A
1N/A /*
1N/A * If we don't already have the page locked, get it and delete the
1N/A * items.
1N/A */
1N/A if ((h == NULL || h->pgno != pgno)) {
1N/A if ((ret = __bam_lget(dbc, 0, pgno, DB_LOCK_WRITE, &lock)) != 0)
1N/A return (ret);
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
1N/A return (ret);
1N/A local_page = 1;
1N/A } else
1N/A local_page = 0;
1N/A
1N/A /*
1N/A * If we're deleting a duplicate entry and there are other duplicate
1N/A * entries remaining, call the common code to do the work and fix up
1N/A * the parent page as necessary. Otherwise, do a normal btree delete.
1N/A *
1N/A * There are 5 possible cases:
1N/A *
1N/A * 1. It's not a duplicate item: do a normal btree delete.
1N/A * 2. It's a duplicate item:
1N/A * 2a: We delete an item from a page of duplicates, but there are
1N/A * more items on the page.
1N/A * 2b: We delete the last item from a page of duplicates, deleting
1N/A * the last duplicate.
1N/A * 2c: We delete the last item from a page of duplicates, but there
1N/A * is a previous page of duplicates.
1N/A * 2d: We delete the last item from a page of duplicates, but there
1N/A * is a following page of duplicates.
1N/A *
1N/A * In the case of:
1N/A *
1N/A * 1: There's nothing further to do.
1N/A * 2a: There's nothing further to do.
1N/A * 2b: Do the normal btree delete instead of a duplicate delete, as
1N/A * that deletes both the duplicate chain and the parent page's
1N/A * entry.
1N/A * 2c: There's nothing further to do.
1N/A * 2d: Delete the duplicate, and update the parent page's entry.
1N/A */
1N/A if (TYPE(h) == P_DUPLICATE) {
1N/A pgno = PGNO(h);
1N/A prev_pgno = PREV_PGNO(h);
1N/A next_pgno = NEXT_PGNO(h);
1N/A
1N/A if (NUM_ENT(h) == 1 &&
1N/A prev_pgno == PGNO_INVALID && next_pgno == PGNO_INVALID)
1N/A cmd = DELETE_PAGE;
1N/A else {
1N/A cmd = DELETE_ITEM;
1N/A
1N/A /* Delete the duplicate. */
1N/A if ((ret = __db_drem(dbc, &h, indx, __bam_free)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * 2a: h != NULL, h->pgno == pgno
1N/A * 2b: We don't reach this clause, as the above test
1N/A * was true.
1N/A * 2c: h == NULL, prev_pgno != PGNO_INVALID
1N/A * 2d: h != NULL, next_pgno != PGNO_INVALID
1N/A *
1N/A * Test for 2a and 2c: if we didn't empty the current
1N/A * page or there was a previous page of duplicates, we
1N/A * don't need to touch the parent page.
1N/A */
1N/A if ((h != NULL && pgno == h->pgno) ||
1N/A prev_pgno != PGNO_INVALID)
1N/A cmd = NOTHING_FURTHER;
1N/A }
1N/A
1N/A /*
1N/A * Release any page we're holding and its lock.
1N/A *
1N/A * !!!
1N/A * If there is no subsequent page in the duplicate chain, then
1N/A * __db_drem will have put page "h" and set it to NULL.
1N/A */
1N/A if (local_page) {
1N/A if (h != NULL)
1N/A (void)memp_fput(dbp->mpf, h, 0);
1N/A (void)__BT_TLPUT(dbc, lock);
1N/A local_page = 0;
1N/A }
1N/A
1N/A if (cmd == NOTHING_FURTHER)
1N/A goto done;
1N/A
1N/A /* Acquire the parent page and switch the index to its entry. */
1N/A if ((ret =
1N/A __bam_lget(dbc, 0, cp->pgno, DB_LOCK_WRITE, &lock)) != 0)
1N/A goto err;
1N/A if ((ret = memp_fget(dbp->mpf, &cp->pgno, 0, &h)) != 0) {
1N/A (void)__BT_TLPUT(dbc, lock);
1N/A goto err;
1N/A }
1N/A local_page = 1;
1N/A indx = cp->indx;
1N/A
1N/A if (cmd == DELETE_PAGE)
1N/A goto btd;
1N/A
1N/A /*
1N/A * Copy, delete, update, add-back the parent page's data entry.
1N/A *
1N/A * XXX
1N/A * This may be a performance/logging problem. We should add a
1N/A * log message which simply logs/updates a random set of bytes
1N/A * on a page, and use it instead of doing a delete/add pair.
1N/A */
1N/A indx += O_INDX;
1N/A bo = *GET_BOVERFLOW(h, indx);
1N/A (void)__db_ditem(dbc, h, indx, BOVERFLOW_SIZE);
1N/A bo.pgno = next_pgno;
1N/A memset(&dbt, 0, sizeof(dbt));
1N/A dbt.data = &bo;
1N/A dbt.size = BOVERFLOW_SIZE;
1N/A (void)__db_pitem(dbc, h, indx, BOVERFLOW_SIZE, &dbt, NULL);
1N/A (void)memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY);
1N/A goto done;
1N/A }
1N/A
1N/Abtd: /*
1N/A * If the page is going to be emptied, delete it. To delete a leaf
1N/A * page we need a copy of a key from the page. We use the 0th page
1N/A * index since it's the last key that the page held.
1N/A *
1N/A * We malloc the page information instead of using the return key/data
1N/A * memory because we've already set them -- the reason we've already
1N/A * set them is because we're (potentially) about to do a reverse split,
1N/A * which would make our saved page information useless.
1N/A *
1N/A * !!!
1N/A * The following operations to delete a page might deadlock. I think
1N/A * that's OK. The problem is if we're deleting an item because we're
1N/A * closing cursors because we've already deadlocked and want to call
1N/A * txn_abort(). If we fail due to deadlock, we leave a locked empty
1N/A * page in the tree, which won't be empty long because we're going to
1N/A * undo the delete.
1N/A */
1N/A if (NUM_ENT(h) == 2 && h->pgno != PGNO_ROOT) {
1N/A memset(&dbt, 0, sizeof(DBT));
1N/A dbt.flags = DB_DBT_MALLOC | DB_DBT_INTERNAL;
1N/A if ((ret = __db_ret(dbp, h, 0, &dbt, NULL, NULL)) != 0)
1N/A goto err;
1N/A delete_page = 1;
1N/A }
1N/A
1N/A /*
1N/A * Do a normal btree delete.
1N/A *
1N/A * !!!
1N/A * Delete the key item first, otherwise the duplicate checks in
1N/A * __bam_ditem() won't work!
1N/A */
1N/A if ((ret = __bam_ditem(dbc, h, indx)) != 0)
1N/A goto err;
1N/A if ((ret = __bam_ditem(dbc, h, indx)) != 0)
1N/A goto err;
1N/A
1N/A /* Discard any remaining locks/pages. */
1N/A if (local_page) {
1N/A (void)memp_fput(dbp->mpf, h, 0);
1N/A (void)__BT_TLPUT(dbc, lock);
1N/A local_page = 0;
1N/A }
1N/A
1N/A /* Delete the page if it was emptied. */
1N/A if (delete_page)
1N/A ret = __bam_dpage(dbc, &dbt);
1N/A
1N/Aerr:
1N/Adone: if (delete_page)
1N/A __os_free(dbt.data, dbt.size);
1N/A
1N/A if (local_page) {
1N/A /*
1N/A * It's possible for h to be NULL, as __db_drem may have
1N/A * been relinking pages by the time that it deadlocked.
1N/A */
1N/A if (h != NULL)
1N/A (void)memp_fput(dbp->mpf, h, 0);
1N/A (void)__BT_TLPUT(dbc, lock);
1N/A }
1N/A
1N/A if (F_ISSET(dbp, DB_AM_CDB) && F_ISSET(dbc, DBC_RMW))
1N/A (void)__lock_downgrade(dbp->dbenv->lk_info, dbc->mylock,
1N/A DB_LOCK_IWRITE, 0);
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __bam_c_getstack --
1N/A * Acquire a full stack for a cursor.
1N/A */
1N/Astatic int
1N/A__bam_c_getstack(dbc, cp)
1N/A DBC *dbc;
1N/A CURSOR *cp;
1N/A{
1N/A DB *dbp;
1N/A DBT dbt;
1N/A PAGE *h;
1N/A db_pgno_t pgno;
1N/A int exact, ret;
1N/A
1N/A dbp = dbc->dbp;
1N/A h = NULL;
1N/A memset(&dbt, 0, sizeof(DBT));
1N/A ret = 0;
1N/A
1N/A /* Get the page with the current item on it. */
1N/A pgno = cp->pgno;
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
1N/A return (ret);
1N/A
1N/A /* Get a copy of a key from the page. */
1N/A dbt.flags = DB_DBT_MALLOC | DB_DBT_INTERNAL;
1N/A if ((ret = __db_ret(dbp, h, 0, &dbt, NULL, NULL)) != 0)
1N/A goto err;
1N/A
1N/A /* Get a write-locked stack for that page. */
1N/A exact = 0;
1N/A ret = __bam_search(dbc, &dbt, S_KEYFIRST, 1, NULL, &exact);
1N/A
1N/A /* We no longer need the key or the page. */
1N/Aerr: if (h != NULL)
1N/A (void)memp_fput(dbp->mpf, h, 0);
1N/A if (dbt.data != NULL)
1N/A __os_free(dbt.data, dbt.size);
1N/A return (ret);
1N/A}