bt_put.c revision 115411
11573Srgrimes/*-
214272Spst * Copyright (c) 1990, 1993, 1994
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Mike Olson.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3814272Spststatic char sccsid[] = "@(#)bt_put.c	8.8 (Berkeley) 7/26/94";
391573Srgrimes#endif /* LIBC_SCCS and not lint */
4092905Sobrien#include <sys/cdefs.h>
4192905Sobrien__FBSDID("$FreeBSD: head/lib/libc/db/btree/bt_put.c 115411 2003-05-30 11:05:08Z tmm $");
421573Srgrimes
431573Srgrimes#include <sys/types.h>
441573Srgrimes
451573Srgrimes#include <errno.h>
461573Srgrimes#include <stdio.h>
471573Srgrimes#include <stdlib.h>
481573Srgrimes#include <string.h>
491573Srgrimes
501573Srgrimes#include <db.h>
511573Srgrimes#include "btree.h"
521573Srgrimes
5392905Sobrienstatic EPG *bt_fast(BTREE *, const DBT *, const DBT *, int *);
541573Srgrimes
551573Srgrimes/*
561573Srgrimes * __BT_PUT -- Add a btree item to the tree.
571573Srgrimes *
581573Srgrimes * Parameters:
591573Srgrimes *	dbp:	pointer to access method
601573Srgrimes *	key:	key
611573Srgrimes *	data:	data
621573Srgrimes *	flag:	R_NOOVERWRITE
631573Srgrimes *
641573Srgrimes * Returns:
651573Srgrimes *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
661573Srgrimes *	tree and R_NOOVERWRITE specified.
671573Srgrimes */
681573Srgrimesint
691573Srgrimes__bt_put(dbp, key, data, flags)
701573Srgrimes	const DB *dbp;
711573Srgrimes	DBT *key;
721573Srgrimes	const DBT *data;
731573Srgrimes	u_int flags;
741573Srgrimes{
751573Srgrimes	BTREE *t;
761573Srgrimes	DBT tkey, tdata;
771573Srgrimes	EPG *e;
781573Srgrimes	PAGE *h;
791573Srgrimes	indx_t index, nxtindex;
801573Srgrimes	pgno_t pg;
81115411Stmm	u_int32_t nbytes, tmp;
821573Srgrimes	int dflags, exact, status;
831573Srgrimes	char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
841573Srgrimes
851573Srgrimes	t = dbp->internal;
861573Srgrimes
871573Srgrimes	/* Toss any page pinned across calls. */
881573Srgrimes	if (t->bt_pinned != NULL) {
891573Srgrimes		mpool_put(t->bt_mp, t->bt_pinned, 0);
901573Srgrimes		t->bt_pinned = NULL;
911573Srgrimes	}
921573Srgrimes
9314272Spst	/* Check for change to a read-only tree. */
9414272Spst	if (F_ISSET(t, B_RDONLY)) {
9514272Spst		errno = EPERM;
9614272Spst		return (RET_ERROR);
9714272Spst	}
9814272Spst
991573Srgrimes	switch (flags) {
1001573Srgrimes	case 0:
1011573Srgrimes	case R_NOOVERWRITE:
1021573Srgrimes		break;
10314272Spst	case R_CURSOR:
10414272Spst		/*
10514272Spst		 * If flags is R_CURSOR, put the cursor.  Must already
10614272Spst		 * have started a scan and not have already deleted it.
10714272Spst		 */
10814272Spst		if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
10914272Spst		    !F_ISSET(&t->bt_cursor,
11014272Spst		        CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
11114272Spst			break;
11214272Spst		/* FALLTHROUGH */
1131573Srgrimes	default:
11414272Spst		errno = EINVAL;
1151573Srgrimes		return (RET_ERROR);
1161573Srgrimes	}
1171573Srgrimes
1181573Srgrimes	/*
11914272Spst	 * If the key/data pair won't fit on a page, store it on overflow
12014272Spst	 * pages.  Only put the key on the overflow page if the pair are
12114272Spst	 * still too big after moving the data to an overflow page.
1221573Srgrimes	 *
1231573Srgrimes	 * XXX
12414272Spst	 * If the insert fails later on, the overflow pages aren't recovered.
1251573Srgrimes	 */
1261573Srgrimes	dflags = 0;
1271573Srgrimes	if (key->size + data->size > t->bt_ovflsize) {
1281573Srgrimes		if (key->size > t->bt_ovflsize) {
1291573Srgrimesstorekey:		if (__ovfl_put(t, key, &pg) == RET_ERROR)
1301573Srgrimes				return (RET_ERROR);
1311573Srgrimes			tkey.data = kb;
1321573Srgrimes			tkey.size = NOVFLSIZE;
1331573Srgrimes			memmove(kb, &pg, sizeof(pgno_t));
134115411Stmm			tmp = key->size;
1351573Srgrimes			memmove(kb + sizeof(pgno_t),
136115411Stmm			    &tmp, sizeof(u_int32_t));
1371573Srgrimes			dflags |= P_BIGKEY;
1381573Srgrimes			key = &tkey;
1391573Srgrimes		}
1401573Srgrimes		if (key->size + data->size > t->bt_ovflsize) {
1411573Srgrimes			if (__ovfl_put(t, data, &pg) == RET_ERROR)
1421573Srgrimes				return (RET_ERROR);
1431573Srgrimes			tdata.data = db;
1441573Srgrimes			tdata.size = NOVFLSIZE;
1451573Srgrimes			memmove(db, &pg, sizeof(pgno_t));
146115411Stmm			tmp = data->size;
1471573Srgrimes			memmove(db + sizeof(pgno_t),
148115411Stmm			    &tmp, sizeof(u_int32_t));
1491573Srgrimes			dflags |= P_BIGDATA;
1501573Srgrimes			data = &tdata;
1511573Srgrimes		}
1521573Srgrimes		if (key->size + data->size > t->bt_ovflsize)
1531573Srgrimes			goto storekey;
1541573Srgrimes	}
1551573Srgrimes
1561573Srgrimes	/* Replace the cursor. */
1571573Srgrimes	if (flags == R_CURSOR) {
15814272Spst		if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL)
1591573Srgrimes			return (RET_ERROR);
16014272Spst		index = t->bt_cursor.pg.index;
1611573Srgrimes		goto delete;
1621573Srgrimes	}
1631573Srgrimes
1641573Srgrimes	/*
16514272Spst	 * Find the key to delete, or, the location at which to insert.
16614272Spst	 * Bt_fast and __bt_search both pin the returned page.
1671573Srgrimes	 */
1681573Srgrimes	if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
1691573Srgrimes		if ((e = __bt_search(t, key, &exact)) == NULL)
1701573Srgrimes			return (RET_ERROR);
1711573Srgrimes	h = e->page;
1721573Srgrimes	index = e->index;
1731573Srgrimes
1741573Srgrimes	/*
17514272Spst	 * Add the key/data pair to the tree.  If an identical key is already
17614272Spst	 * in the tree, and R_NOOVERWRITE is set, an error is returned.  If
17714272Spst	 * R_NOOVERWRITE is not set, the key is either added (if duplicates are
17814272Spst	 * permitted) or an error is returned.
1791573Srgrimes	 */
1801573Srgrimes	switch (flags) {
1811573Srgrimes	case R_NOOVERWRITE:
1821573Srgrimes		if (!exact)
1831573Srgrimes			break;
1841573Srgrimes		mpool_put(t->bt_mp, h, 0);
1851573Srgrimes		return (RET_SPECIAL);
1861573Srgrimes	default:
18714272Spst		if (!exact || !F_ISSET(t, B_NODUPS))
1881573Srgrimes			break;
18914272Spst		/*
19014272Spst		 * !!!
19114272Spst		 * Note, the delete may empty the page, so we need to put a
19214272Spst		 * new entry into the page immediately.
19314272Spst		 */
19414272Spstdelete:		if (__bt_dleaf(t, key, h, index) == RET_ERROR) {
1951573Srgrimes			mpool_put(t->bt_mp, h, 0);
1961573Srgrimes			return (RET_ERROR);
1971573Srgrimes		}
1981573Srgrimes		break;
1991573Srgrimes	}
2001573Srgrimes
2011573Srgrimes	/*
2021573Srgrimes	 * If not enough room, or the user has put a ceiling on the number of
2031573Srgrimes	 * keys permitted in the page, split the page.  The split code will
2041573Srgrimes	 * insert the key and data and unpin the current page.  If inserting
2051573Srgrimes	 * into the offset array, shift the pointers up.
2061573Srgrimes	 */
2071573Srgrimes	nbytes = NBLEAFDBT(key->size, data->size);
2081573Srgrimes	if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
2091573Srgrimes		if ((status = __bt_split(t, h, key,
2101573Srgrimes		    data, dflags, nbytes, index)) != RET_SUCCESS)
2111573Srgrimes			return (status);
2121573Srgrimes		goto success;
2131573Srgrimes	}
2141573Srgrimes
2151573Srgrimes	if (index < (nxtindex = NEXTINDEX(h)))
2161573Srgrimes		memmove(h->linp + index + 1, h->linp + index,
2171573Srgrimes		    (nxtindex - index) * sizeof(indx_t));
2181573Srgrimes	h->lower += sizeof(indx_t);
2191573Srgrimes
2201573Srgrimes	h->linp[index] = h->upper -= nbytes;
2211573Srgrimes	dest = (char *)h + h->upper;
2221573Srgrimes	WR_BLEAF(dest, key, data, dflags);
2231573Srgrimes
22414272Spst	/* If the cursor is on this page, adjust it as necessary. */
22514272Spst	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
22614272Spst	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
22714272Spst	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= index)
22814272Spst		++t->bt_cursor.pg.index;
22914272Spst
230111010Snectar	if (t->bt_order == NOT) {
2311573Srgrimes		if (h->nextpg == P_INVALID) {
2321573Srgrimes			if (index == NEXTINDEX(h) - 1) {
2331573Srgrimes				t->bt_order = FORWARD;
2341573Srgrimes				t->bt_last.index = index;
2351573Srgrimes				t->bt_last.pgno = h->pgno;
2361573Srgrimes			}
2371573Srgrimes		} else if (h->prevpg == P_INVALID) {
2381573Srgrimes			if (index == 0) {
2391573Srgrimes				t->bt_order = BACK;
2401573Srgrimes				t->bt_last.index = 0;
2411573Srgrimes				t->bt_last.pgno = h->pgno;
2421573Srgrimes			}
2431573Srgrimes		}
244111010Snectar	}
2451573Srgrimes
2461573Srgrimes	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
2471573Srgrimes
2481573Srgrimessuccess:
24914272Spst	if (flags == R_SETCURSOR)
25014272Spst		__bt_setcur(t, e->page->pgno, e->index);
25114272Spst
25214272Spst	F_SET(t, B_MODIFIED);
2531573Srgrimes	return (RET_SUCCESS);
2541573Srgrimes}
2551573Srgrimes
2561573Srgrimes#ifdef STATISTICS
2571573Srgrimesu_long bt_cache_hit, bt_cache_miss;
2581573Srgrimes#endif
2591573Srgrimes
2601573Srgrimes/*
2611573Srgrimes * BT_FAST -- Do a quick check for sorted data.
2621573Srgrimes *
2631573Srgrimes * Parameters:
2641573Srgrimes *	t:	tree
2651573Srgrimes *	key:	key to insert
2661573Srgrimes *
2671573Srgrimes * Returns:
2681573Srgrimes * 	EPG for new record or NULL if not found.
2691573Srgrimes */
2701573Srgrimesstatic EPG *
2711573Srgrimesbt_fast(t, key, data, exactp)
2721573Srgrimes	BTREE *t;
2731573Srgrimes	const DBT *key, *data;
2741573Srgrimes	int *exactp;
2751573Srgrimes{
2761573Srgrimes	PAGE *h;
27714272Spst	u_int32_t nbytes;
2781573Srgrimes	int cmp;
2791573Srgrimes
2801573Srgrimes	if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
2811573Srgrimes		t->bt_order = NOT;
2821573Srgrimes		return (NULL);
2831573Srgrimes	}
2841573Srgrimes	t->bt_cur.page = h;
2851573Srgrimes	t->bt_cur.index = t->bt_last.index;
2861573Srgrimes
2871573Srgrimes	/*
28814272Spst	 * If won't fit in this page or have too many keys in this page,
28914272Spst	 * have to search to get split stack.
2901573Srgrimes	 */
2911573Srgrimes	nbytes = NBLEAFDBT(key->size, data->size);
2921573Srgrimes	if (h->upper - h->lower < nbytes + sizeof(indx_t))
2931573Srgrimes		goto miss;
2941573Srgrimes
2951573Srgrimes	if (t->bt_order == FORWARD) {
2961573Srgrimes		if (t->bt_cur.page->nextpg != P_INVALID)
2971573Srgrimes			goto miss;
2981573Srgrimes		if (t->bt_cur.index != NEXTINDEX(h) - 1)
2991573Srgrimes			goto miss;
3001573Srgrimes		if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
3011573Srgrimes			goto miss;
3021573Srgrimes		t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
3031573Srgrimes	} else {
3041573Srgrimes		if (t->bt_cur.page->prevpg != P_INVALID)
3051573Srgrimes			goto miss;
3061573Srgrimes		if (t->bt_cur.index != 0)
3071573Srgrimes			goto miss;
3081573Srgrimes		if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
3091573Srgrimes			goto miss;
3101573Srgrimes		t->bt_last.index = 0;
3111573Srgrimes	}
3121573Srgrimes	*exactp = cmp == 0;
3131573Srgrimes#ifdef STATISTICS
3141573Srgrimes	++bt_cache_hit;
3151573Srgrimes#endif
3161573Srgrimes	return (&t->bt_cur);
3171573Srgrimes
3181573Srgrimesmiss:
3191573Srgrimes#ifdef STATISTICS
3201573Srgrimes	++bt_cache_miss;
3211573Srgrimes#endif
3221573Srgrimes	t->bt_order = NOT;
3231573Srgrimes	mpool_put(t->bt_mp, h, 0);
3241573Srgrimes	return (NULL);
3251573Srgrimes}
326