xref: /csrg-svn/lib/libc/db/btree/bt_put.c (revision 57932)
146131Smao /*-
246131Smao  * Copyright (c) 1990 The Regents of the University of California.
346131Smao  * All rights reserved.
446131Smao  *
546131Smao  * This code is derived from software contributed to Berkeley by
646131Smao  * Mike Olson.
746131Smao  *
846131Smao  * %sccs.include.redist.c%
946131Smao  */
1046131Smao 
1146131Smao #if defined(LIBC_SCCS) && !defined(lint)
12*57932Sbostic static char sccsid[] = "@(#)bt_put.c	5.12 (Berkeley) 02/11/93";
1346131Smao #endif /* LIBC_SCCS and not lint */
1446131Smao 
1546131Smao #include <sys/types.h>
1656751Sbostic 
1750989Sbostic #include <errno.h>
1850989Sbostic #include <stdio.h>
1946561Sbostic #include <stdlib.h>
2046561Sbostic #include <string.h>
2156751Sbostic 
22*57932Sbostic #include <db.h>
2346131Smao #include "btree.h"
2446131Smao 
2550989Sbostic static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *));
2650989Sbostic 
2746131Smao /*
2850989Sbostic  * __BT_PUT -- Add a btree item to the tree.
2946131Smao  *
3050989Sbostic  * Parameters:
3150989Sbostic  *	dbp:	pointer to access method
3250989Sbostic  *	key:	key
3350989Sbostic  *	data:	data
3450989Sbostic  *	flag:	R_NOOVERWRITE
3546131Smao  *
3650989Sbostic  * Returns:
3750989Sbostic  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
3850989Sbostic  *	tree and R_NOOVERWRITE specified.
3946131Smao  */
4046131Smao int
4150989Sbostic __bt_put(dbp, key, data, flags)
4250989Sbostic 	const DB *dbp;
4356751Sbostic 	DBT *key;
4456751Sbostic 	const DBT *data;
4550989Sbostic 	u_int flags;
4646131Smao {
4750989Sbostic 	BTREE *t;
4850989Sbostic 	DBT tkey, tdata;
4950989Sbostic 	EPG *e;
5050989Sbostic 	PAGE *h;
5150989Sbostic 	index_t index, nxtindex;
5250989Sbostic 	pgno_t pg;
5350989Sbostic 	size_t nbytes;
5451099Sbostic 	int dflags, exact, status;
5550989Sbostic 	char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
5646131Smao 
5756751Sbostic 	t = dbp->internal;
5856751Sbostic 
5951099Sbostic 	switch (flags) {
6051099Sbostic 	case R_CURSOR:
6156995Sbostic 		if (!ISSET(t, BTF_SEQINIT))
6256995Sbostic 			goto einval;
6356995Sbostic 		if (ISSET(t, BTF_DELCRSR))
6456995Sbostic 			goto einval;
6551099Sbostic 		break;
6651099Sbostic 	case 0:
6751099Sbostic 	case R_NOOVERWRITE:
6851099Sbostic 		break;
6951099Sbostic 	default:
7056995Sbostic einval:		errno = EINVAL;
7146131Smao 		return (RET_ERROR);
7246131Smao 	}
7351099Sbostic 
7450989Sbostic 	if (ISSET(t, BTF_RDONLY)) {
7550989Sbostic 		errno = EPERM;
7650989Sbostic 		return (RET_ERROR);
7746131Smao 	}
7851099Sbostic 
7950989Sbostic 	/*
8050989Sbostic 	 * If the key/data won't fit on a page, store it on indirect pages.
8151099Sbostic 	 * Only store the key on the overflow page if it's too big after the
8251099Sbostic 	 * data is on an overflow page.
8350989Sbostic 	 *
8450989Sbostic 	 * XXX
8550989Sbostic 	 * If the insert fails later on, these pages aren't recovered.
8650989Sbostic 	 */
8750989Sbostic 	dflags = 0;
8851099Sbostic 	if (key->size + data->size > t->bt_ovflsize) {
8951099Sbostic 		if (key->size > t->bt_ovflsize) {
9051099Sbostic storekey:		if (__ovfl_put(t, key, &pg) == RET_ERROR)
9151099Sbostic 				return (RET_ERROR);
9251099Sbostic 			tkey.data = kb;
9351099Sbostic 			tkey.size = NOVFLSIZE;
9456558Sbostic 			bcopy(&pg, kb, sizeof(pgno_t));
9556558Sbostic 			bcopy(&key->size, kb + sizeof(pgno_t), sizeof(size_t));
9651099Sbostic 			dflags |= P_BIGKEY;
9751099Sbostic 			key = &tkey;
9851099Sbostic 		}
9951099Sbostic 		if (key->size + data->size > t->bt_ovflsize) {
10051099Sbostic 			if (__ovfl_put(t, data, &pg) == RET_ERROR)
10151099Sbostic 				return (RET_ERROR);
10251099Sbostic 			tdata.data = db;
10351099Sbostic 			tdata.size = NOVFLSIZE;
10456558Sbostic 			bcopy(&pg, db, sizeof(pgno_t));
10556558Sbostic 			bcopy(&data->size, db + sizeof(pgno_t), sizeof(size_t));
10651099Sbostic 			dflags |= P_BIGDATA;
10751099Sbostic 			data = &tdata;
10851099Sbostic 		}
10951099Sbostic 		if (key->size + data->size > t->bt_ovflsize)
11051099Sbostic 			goto storekey;
11146131Smao 	}
11251099Sbostic 
11351099Sbostic 	/* Replace the cursor. */
11451099Sbostic 	if (flags == R_CURSOR) {
11551099Sbostic 		if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
11650989Sbostic 			return (RET_ERROR);
11751099Sbostic 		index = t->bt_bcursor.index;
11851099Sbostic 		goto delete;
11950989Sbostic 	}
12046131Smao 
12151099Sbostic 	/*
12251099Sbostic 	 * Find the key to delete, or, the location at which to insert.  Bt_fast
12351099Sbostic 	 * and __bt_search pin the returned page.
12451099Sbostic 	 */
12550989Sbostic 	if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
12650989Sbostic 		if ((e = __bt_search(t, key, &exact)) == NULL)
12750989Sbostic 			return (RET_ERROR);
12850989Sbostic 	h = e->page;
12950989Sbostic 	index = e->index;
13046131Smao 
13150989Sbostic 	/*
13250989Sbostic 	 * Add the specified key/data pair to the tree.  If an identical key
13350989Sbostic 	 * is already in the tree, and R_NOOVERWRITE is set, an error is
13450989Sbostic 	 * returned.  If R_NOOVERWRITE is not set, the key is either added (if
13550989Sbostic 	 * duplicates are permitted) or an error is returned.
13650989Sbostic 	 *
13750989Sbostic 	 * Pages are split as required.
13850989Sbostic 	 */
13950989Sbostic 	switch (flags) {
14050989Sbostic 	case R_NOOVERWRITE:
14150989Sbostic 		if (!exact)
14250989Sbostic 			break;
14350989Sbostic 		/*
14450989Sbostic 		 * One special case is if the cursor references the record and
14551099Sbostic 		 * it's been flagged for deletion.  Then, we delete the record,
14651099Sbostic 		 * leaving the cursor there -- this means that the inserted
14751099Sbostic 		 * record will not be seen in a cursor scan.
14850989Sbostic 		 */
14950989Sbostic 		if (ISSET(t, BTF_DELCRSR) && t->bt_bcursor.pgno == h->pgno &&
15050989Sbostic 		    t->bt_bcursor.index == index) {
15156751Sbostic 			CLR(t, BTF_DELCRSR);
15250989Sbostic 			goto delete;
15346131Smao 		}
15450989Sbostic 		mpool_put(t->bt_mp, h, 0);
15550989Sbostic 		return (RET_SPECIAL);
15650989Sbostic 	default:
15756751Sbostic 		if (!exact || !ISSET(t, BTF_NODUPS))
15850989Sbostic 			break;
15950989Sbostic delete:		if (__bt_dleaf(t, h, index) == RET_ERROR) {
16050989Sbostic 			mpool_put(t->bt_mp, h, 0);
16146131Smao 			return (RET_ERROR);
16246131Smao 		}
16350989Sbostic 		break;
16446131Smao 	}
16546131Smao 
16650989Sbostic 	/*
16750989Sbostic 	 * If not enough room, or the user has put a ceiling on the number of
16850989Sbostic 	 * keys permitted in the page, split the page.  The split code will
16950989Sbostic 	 * insert the key and data and unpin the current page.  If inserting
17050989Sbostic 	 * into the offset array, shift the pointers up.
17150989Sbostic 	 */
17250989Sbostic 	nbytes = NBLEAFDBT(key->size, data->size);
17351099Sbostic 	if (h->upper - h->lower < nbytes + sizeof(index_t)) {
17456751Sbostic 		if ((status = __bt_split(t, h, key,
17556751Sbostic 		    data, dflags, nbytes, index)) != RET_SUCCESS)
17656751Sbostic 			return (status);
17756751Sbostic 		goto success;
17851099Sbostic 	}
17946131Smao 
18050989Sbostic 	if (index < (nxtindex = NEXTINDEX(h)))
18150989Sbostic 		bcopy(h->linp + index, h->linp + index + 1,
18250989Sbostic 		    (nxtindex - index) * sizeof(index_t));
18350989Sbostic 	h->lower += sizeof(index_t);
18446131Smao 
18550989Sbostic 	h->linp[index] = h->upper -= nbytes;
18650989Sbostic 	dest = (char *)h + h->upper;
18750989Sbostic 	WR_BLEAF(dest, key, data, dflags);
18846131Smao 
18950989Sbostic 	if (t->bt_order == NOT)
19050989Sbostic 		if (h->nextpg == P_INVALID) {
19150989Sbostic 			if (index == NEXTINDEX(h) - 1) {
19250989Sbostic 				t->bt_order = FORWARD;
19350989Sbostic 				t->bt_last.index = index;
19450989Sbostic 				t->bt_last.pgno = h->pgno;
19550989Sbostic 			}
19650989Sbostic 		} else if (h->prevpg == P_INVALID) {
19750989Sbostic 			if (index == 0) {
19850989Sbostic 				t->bt_order = BACK;
19950989Sbostic 				t->bt_last.index = 0;
20050989Sbostic 				t->bt_last.pgno = h->pgno;
20150989Sbostic 			}
20246131Smao 		}
20346131Smao 
20451099Sbostic 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
20556751Sbostic 
20656751Sbostic success:
20756751Sbostic 	if (flags == R_SETCURSOR) {
20856751Sbostic 		t->bt_bcursor.pgno = e->page->pgno;
20956751Sbostic 		t->bt_bcursor.index = e->index;
21056751Sbostic 	}
21150989Sbostic 	SET(t, BTF_MODIFIED);
21250989Sbostic 	return (RET_SUCCESS);
21346131Smao }
21446131Smao 
21550989Sbostic #ifdef STATISTICS
21650989Sbostic u_long bt_cache_hit, bt_cache_miss;
21750989Sbostic #endif
21850989Sbostic 
21946131Smao /*
22050989Sbostic  * BT_FAST -- Do a quick check for sorted data.
22146131Smao  *
22250989Sbostic  * Parameters:
22350989Sbostic  *	t:	tree
22450989Sbostic  *	key:	key to insert
22546131Smao  *
22650989Sbostic  * Returns:
22750989Sbostic  * 	EPG for new record or NULL if not found.
22846131Smao  */
22950989Sbostic static EPG *
23050989Sbostic bt_fast(t, key, data, exactp)
23150989Sbostic 	BTREE *t;
23250989Sbostic 	const DBT *key, *data;
23350989Sbostic 	int *exactp;
23446131Smao {
23550989Sbostic 	EPG e;
23650989Sbostic 	PAGE *h;
23750989Sbostic 	size_t nbytes;
23850989Sbostic 	int cmp;
23946131Smao 
24050989Sbostic 	if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
24150989Sbostic 		t->bt_order = NOT;
24250989Sbostic 		return (NULL);
24350989Sbostic 	}
24450989Sbostic 	e.page = h;
24550989Sbostic 	e.index = t->bt_last.index;
24646131Smao 
24746131Smao 	/*
24850989Sbostic 	 * If won't fit in this page or have too many keys in this page, have
24950989Sbostic 	 * to search to get split stack.
25046131Smao 	 */
25151099Sbostic 	nbytes = NBLEAFDBT(key->size, data->size);
25251099Sbostic 	if (h->upper - h->lower < nbytes + sizeof(index_t))
25350989Sbostic 		goto miss;
25446131Smao 
25550989Sbostic 	if (t->bt_order == FORWARD) {
25650989Sbostic 		if (e.page->nextpg != P_INVALID)
25750989Sbostic 			goto miss;
25850989Sbostic 		if (e.index != NEXTINDEX(h) - 1)
25950989Sbostic 			goto miss;
26050989Sbostic 		if ((cmp = __bt_cmp(t, key, &e)) < 0)
26150989Sbostic 			goto miss;
26251880Sbostic 		t->bt_last.index = cmp ? ++e.index : e.index;
26346131Smao 	} else {
26450989Sbostic 		if (e.page->prevpg != P_INVALID)
26550989Sbostic 			goto miss;
26650989Sbostic 		if (e.index != 0)
26750989Sbostic 			goto miss;
26850989Sbostic 		if ((cmp = __bt_cmp(t, key, &e)) > 0)
26950989Sbostic 			goto miss;
27050989Sbostic 		t->bt_last.index = 0;
27146131Smao 	}
27250989Sbostic 	*exactp = cmp == 0;
27350989Sbostic #ifdef STATISTICS
27450989Sbostic 	++bt_cache_hit;
27550989Sbostic #endif
27650989Sbostic 	return (&e);
27746131Smao 
27851099Sbostic miss:
27950989Sbostic #ifdef STATISTICS
28050989Sbostic 	++bt_cache_miss;
28150989Sbostic #endif
28250989Sbostic 	t->bt_order = NOT;
28350989Sbostic 	mpool_put(t->bt_mp, h, 0);
28450989Sbostic 	return (NULL);
28546131Smao }
286