2N/A/*-
2N/A * Copyright (c) 1990, 1993, 1994
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * This code is derived from software contributed to Berkeley by
2N/A * Mike Olson.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by the University of
2N/A * California, Berkeley and its contributors.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A */
2N/A
2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic char sccsid[] = "@(#)bt_put.c 8.8 (Berkeley) 7/26/94";
2N/A#endif /* LIBC_SCCS and not lint */
2N/A
2N/A#include <sys/types.h>
2N/A
2N/A#include <errno.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/A#include "db-int.h"
2N/A#include "btree.h"
2N/A
2N/Astatic EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *));
2N/A
2N/A/*
2N/A * __BT_PUT -- Add a btree item to the tree.
2N/A *
2N/A * Parameters:
2N/A * dbp: pointer to access method
2N/A * key: key
2N/A * data: data
2N/A * flag: R_NOOVERWRITE
2N/A *
2N/A * Returns:
2N/A * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
2N/A * tree and R_NOOVERWRITE specified.
2N/A */
2N/Aint
2N/A__bt_put(dbp, key, data, flags)
2N/A const DB *dbp;
2N/A DBT *key;
2N/A const DBT *data;
2N/A u_int flags;
2N/A{
2N/A BTREE *t;
2N/A DBT tkey, tdata;
2N/A EPG *e = 0;
2N/A PAGE *h;
2N/A indx_t idx, nxtindex;
2N/A db_pgno_t pg;
2N/A u_int32_t nbytes;
2N/A int dflags, exact, status;
2N/A char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
2N/A
2N/A t = dbp->internal;
2N/A
2N/A /* Toss any page pinned across calls. */
2N/A if (t->bt_pinned != NULL) {
2N/A mpool_put(t->bt_mp, t->bt_pinned, 0);
2N/A t->bt_pinned = NULL;
2N/A }
2N/A
2N/A /* Check for change to a read-only tree. */
2N/A if (F_ISSET(t, B_RDONLY)) {
2N/A errno = EPERM;
2N/A return (RET_ERROR);
2N/A }
2N/A
2N/A switch (flags) {
2N/A case 0:
2N/A case R_NOOVERWRITE:
2N/A break;
2N/A case R_CURSOR:
2N/A /*
2N/A * If flags is R_CURSOR, put the cursor. Must already
2N/A * have started a scan and not have already deleted it.
2N/A */
2N/A if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
2N/A !F_ISSET(&t->bt_cursor,
2N/A CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
2N/A break;
2N/A /* FALLTHROUGH */
2N/A default:
2N/A errno = EINVAL;
2N/A return (RET_ERROR);
2N/A }
2N/A
2N/A /*
2N/A * If the key/data pair won't fit on a page, store it on overflow
2N/A * pages. Only put the key on the overflow page if the pair are
2N/A * still too big after moving the data to an overflow page.
2N/A *
2N/A * XXX
2N/A * If the insert fails later on, the overflow pages aren't recovered.
2N/A */
2N/A dflags = 0;
2N/A if (key->size + data->size > t->bt_ovflsize) {
2N/A if (key->size > t->bt_ovflsize) {
2N/A u_int32_t yuck_this_is_gross_code;
2N/Astorekey: if (__ovfl_put(t, key, &pg) == RET_ERROR)
2N/A return (RET_ERROR);
2N/A tkey.data = kb;
2N/A tkey.size = NOVFLSIZE;
2N/A memmove(kb, &pg, sizeof(db_pgno_t));
2N/A yuck_this_is_gross_code = key->size;
2N/A if (yuck_this_is_gross_code != key->size)
2N/A abort ();
2N/A memmove(kb + sizeof(db_pgno_t),
2N/A &yuck_this_is_gross_code, sizeof(u_int32_t));
2N/A dflags |= P_BIGKEY;
2N/A key = &tkey;
2N/A }
2N/A if (key->size + data->size > t->bt_ovflsize) {
2N/A u_int32_t yuck_this_is_gross_code = data->size;
2N/A if (__ovfl_put(t, data, &pg) == RET_ERROR)
2N/A return (RET_ERROR);
2N/A tdata.data = db;
2N/A tdata.size = NOVFLSIZE;
2N/A memmove(db, &pg, sizeof(db_pgno_t));
2N/A if (yuck_this_is_gross_code != data->size)
2N/A abort ();
2N/A memmove(db + sizeof(db_pgno_t),
2N/A &yuck_this_is_gross_code, sizeof(u_int32_t));
2N/A dflags |= P_BIGDATA;
2N/A data = &tdata;
2N/A }
2N/A if (key->size + data->size > t->bt_ovflsize)
2N/A goto storekey;
2N/A }
2N/A
2N/A /* Replace the cursor. */
2N/A if (flags == R_CURSOR) {
2N/A if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL)
2N/A return (RET_ERROR);
2N/A idx = t->bt_cursor.pg.index;
2N/A goto delete;
2N/A }
2N/A
2N/A /*
2N/A * Find the key to delete, or, the location at which to insert.
2N/A * Bt_fast and __bt_search both pin the returned page.
2N/A */
2N/A if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
2N/A if ((e = __bt_search(t, key, &exact)) == NULL)
2N/A return (RET_ERROR);
2N/A h = e->page;
2N/A idx = e->index;
2N/A
2N/A /*
2N/A * Add the key/data pair to the tree. If an identical key is already
2N/A * in the tree, and R_NOOVERWRITE is set, an error is returned. If
2N/A * R_NOOVERWRITE is not set, the key is either added (if duplicates are
2N/A * permitted) or an error is returned.
2N/A */
2N/A switch (flags) {
2N/A case R_NOOVERWRITE:
2N/A if (!exact)
2N/A break;
2N/A mpool_put(t->bt_mp, h, 0);
2N/A return (RET_SPECIAL);
2N/A default:
2N/A if (!exact || !F_ISSET(t, B_NODUPS))
2N/A break;
2N/A /*
2N/A * !!!
2N/A * Note, the delete may empty the page, so we need to put a
2N/A * new entry into the page immediately.
2N/A */
2N/Adelete: if (__bt_dleaf(t, key, h, idx) == RET_ERROR) {
2N/A mpool_put(t->bt_mp, h, 0);
2N/A return (RET_ERROR);
2N/A }
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A * If not enough room, or the user has put a ceiling on the number of
2N/A * keys permitted in the page, split the page. The split code will
2N/A * insert the key and data and unpin the current page. If inserting
2N/A * into the offset array, shift the pointers up.
2N/A */
2N/A nbytes = NBLEAFDBT(key->size, data->size);
2N/A if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
2N/A if ((status = __bt_split(t, h, key,
2N/A data, dflags, nbytes, idx)) != RET_SUCCESS)
2N/A return (status);
2N/A goto success;
2N/A }
2N/A
2N/A if (idx < (nxtindex = NEXTINDEX(h)))
2N/A memmove(h->linp + idx + 1, h->linp + idx,
2N/A (nxtindex - idx) * sizeof(indx_t));
2N/A h->lower += sizeof(indx_t);
2N/A
2N/A h->linp[idx] = h->upper -= nbytes;
2N/A dest = (char *)h + h->upper;
2N/A WR_BLEAF(dest, key, data, dflags);
2N/A
2N/A /* If the cursor is on this page, adjust it as necessary. */
2N/A if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
2N/A !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
2N/A t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= idx)
2N/A ++t->bt_cursor.pg.index;
2N/A
2N/A if (t->bt_order == NOT) {
2N/A if (h->nextpg == P_INVALID) {
2N/A if (idx == NEXTINDEX(h) - 1) {
2N/A t->bt_order = FORWARD;
2N/A t->bt_last.index = idx;
2N/A t->bt_last.pgno = h->pgno;
2N/A }
2N/A } else if (h->prevpg == P_INVALID) {
2N/A if (idx == 0) {
2N/A t->bt_order = BACK;
2N/A t->bt_last.index = 0;
2N/A t->bt_last.pgno = h->pgno;
2N/A }
2N/A }
2N/A }
2N/A
2N/A mpool_put(t->bt_mp, h, MPOOL_DIRTY);
2N/A
2N/Asuccess:
2N/A if (flags == R_SETCURSOR)
2N/A __bt_setcur(t, e->page->pgno, e->index);
2N/A
2N/A F_SET(t, B_MODIFIED);
2N/A return (RET_SUCCESS);
2N/A}
2N/A
2N/A#ifdef STATISTICS
2N/Au_long bt_cache_hit, bt_cache_miss;
2N/A#endif
2N/A
2N/A/*
2N/A * BT_FAST -- Do a quick check for sorted data.
2N/A *
2N/A * Parameters:
2N/A * t: tree
2N/A * key: key to insert
2N/A *
2N/A * Returns:
2N/A * EPG for new record or NULL if not found.
2N/A */
2N/Astatic EPG *
2N/Abt_fast(t, key, data, exactp)
2N/A BTREE *t;
2N/A const DBT *key, *data;
2N/A int *exactp;
2N/A{
2N/A PAGE *h;
2N/A u_int32_t nbytes;
2N/A int cmp;
2N/A
2N/A if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
2N/A t->bt_order = NOT;
2N/A return (NULL);
2N/A }
2N/A t->bt_cur.page = h;
2N/A t->bt_cur.index = t->bt_last.index;
2N/A
2N/A /*
2N/A * If won't fit in this page or have too many keys in this page,
2N/A * have to search to get split stack.
2N/A */
2N/A nbytes = NBLEAFDBT(key->size, data->size);
2N/A if (h->upper - h->lower < nbytes + sizeof(indx_t))
2N/A goto miss;
2N/A
2N/A if (t->bt_order == FORWARD) {
2N/A if (t->bt_cur.page->nextpg != P_INVALID)
2N/A goto miss;
2N/A if (t->bt_cur.index != NEXTINDEX(h) - 1)
2N/A goto miss;
2N/A if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
2N/A goto miss;
2N/A t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
2N/A } else {
2N/A if (t->bt_cur.page->prevpg != P_INVALID)
2N/A goto miss;
2N/A if (t->bt_cur.index != 0)
2N/A goto miss;
2N/A if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
2N/A goto miss;
2N/A t->bt_last.index = 0;
2N/A }
2N/A *exactp = cmp == 0;
2N/A#ifdef STATISTICS
2N/A ++bt_cache_hit;
2N/A#endif
2N/A return (&t->bt_cur);
2N/A
2N/Amiss:
2N/A#ifdef STATISTICS
2N/A ++bt_cache_miss;
2N/A#endif
2N/A t->bt_order = NOT;
2N/A mpool_put(t->bt_mp, h, 0);
2N/A return (NULL);
2N/A}