xref: /minix3/lib/libc/db/btree/bt_put.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: bt_put.c,v 1.20 2011/06/26 22:20:31 christos Exp $	*/
22639ae9bSBen Gras 
32639ae9bSBen Gras /*-
42639ae9bSBen Gras  * Copyright (c) 1990, 1993, 1994
52639ae9bSBen Gras  *	The Regents of the University of California.  All rights reserved.
62639ae9bSBen Gras  *
72639ae9bSBen Gras  * This code is derived from software contributed to Berkeley by
82639ae9bSBen Gras  * Mike Olson.
92639ae9bSBen Gras  *
102639ae9bSBen Gras  * Redistribution and use in source and binary forms, with or without
112639ae9bSBen Gras  * modification, are permitted provided that the following conditions
122639ae9bSBen Gras  * are met:
132639ae9bSBen Gras  * 1. Redistributions of source code must retain the above copyright
142639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer.
152639ae9bSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer in the
172639ae9bSBen Gras  *    documentation and/or other materials provided with the distribution.
182639ae9bSBen Gras  * 3. Neither the name of the University nor the names of its contributors
192639ae9bSBen Gras  *    may be used to endorse or promote products derived from this software
202639ae9bSBen Gras  *    without specific prior written permission.
212639ae9bSBen Gras  *
222639ae9bSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232639ae9bSBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242639ae9bSBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252639ae9bSBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262639ae9bSBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272639ae9bSBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282639ae9bSBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292639ae9bSBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302639ae9bSBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312639ae9bSBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322639ae9bSBen Gras  * SUCH DAMAGE.
332639ae9bSBen Gras  */
342639ae9bSBen Gras 
352639ae9bSBen Gras #if HAVE_NBTOOL_CONFIG_H
362639ae9bSBen Gras #include "nbtool_config.h"
372639ae9bSBen Gras #endif
382639ae9bSBen Gras 
392639ae9bSBen Gras #include <sys/cdefs.h>
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: bt_put.c,v 1.20 2011/06/26 22:20:31 christos Exp $");
412639ae9bSBen Gras 
422639ae9bSBen Gras #include "namespace.h"
432639ae9bSBen Gras #include <sys/types.h>
442639ae9bSBen Gras 
452639ae9bSBen Gras #include <assert.h>
462639ae9bSBen Gras #include <errno.h>
472639ae9bSBen Gras #include <stdio.h>
482639ae9bSBen Gras #include <stdlib.h>
492639ae9bSBen Gras #include <string.h>
502639ae9bSBen Gras 
512639ae9bSBen Gras #include <db.h>
522639ae9bSBen Gras #include "btree.h"
532639ae9bSBen Gras 
542639ae9bSBen Gras static EPG *bt_fast(BTREE *, const DBT *, const DBT *, int *);
552639ae9bSBen Gras 
562639ae9bSBen Gras /*
572639ae9bSBen Gras  * __BT_PUT -- Add a btree item to the tree.
582639ae9bSBen Gras  *
592639ae9bSBen Gras  * Parameters:
602639ae9bSBen Gras  *	dbp:	pointer to access method
612639ae9bSBen Gras  *	key:	key
622639ae9bSBen Gras  *	data:	data
632639ae9bSBen Gras  *	flag:	R_NOOVERWRITE
642639ae9bSBen Gras  *
652639ae9bSBen Gras  * Returns:
662639ae9bSBen Gras  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
672639ae9bSBen Gras  *	tree and R_NOOVERWRITE specified.
682639ae9bSBen Gras  */
692639ae9bSBen Gras int
__bt_put(const DB * dbp,DBT * key,const DBT * data,u_int flags)702639ae9bSBen Gras __bt_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
712639ae9bSBen Gras {
722639ae9bSBen Gras 	BTREE *t;
732639ae9bSBen Gras 	DBT tkey, tdata;
742639ae9bSBen Gras 	EPG *e = NULL; /* pacify gcc */
752639ae9bSBen Gras 	PAGE *h;
762639ae9bSBen Gras 	indx_t idx, nxtindex;
772639ae9bSBen Gras 	pgno_t pg;
782639ae9bSBen Gras 	uint32_t nbytes, temp;
792639ae9bSBen Gras 	int dflags, exact, status;
802639ae9bSBen Gras 	char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
812639ae9bSBen Gras 
822639ae9bSBen Gras 	t = dbp->internal;
832639ae9bSBen Gras 
842639ae9bSBen Gras 	/* Toss any page pinned across calls. */
852639ae9bSBen Gras 	if (t->bt_pinned != NULL) {
862639ae9bSBen Gras 		mpool_put(t->bt_mp, t->bt_pinned, 0);
872639ae9bSBen Gras 		t->bt_pinned = NULL;
882639ae9bSBen Gras 	}
892639ae9bSBen Gras 
902639ae9bSBen Gras 	/* Check for change to a read-only tree. */
912639ae9bSBen Gras 	if (F_ISSET(t, B_RDONLY)) {
922639ae9bSBen Gras 		errno = EPERM;
932639ae9bSBen Gras 		return (RET_ERROR);
942639ae9bSBen Gras 	}
952639ae9bSBen Gras 
962639ae9bSBen Gras 	switch (flags) {
972639ae9bSBen Gras 	case 0:
982639ae9bSBen Gras 	case R_NOOVERWRITE:
992639ae9bSBen Gras 		break;
1002639ae9bSBen Gras 	case R_CURSOR:
1012639ae9bSBen Gras 		/*
1022639ae9bSBen Gras 		 * If flags is R_CURSOR, put the cursor.  Must already
1032639ae9bSBen Gras 		 * have started a scan and not have already deleted it.
1042639ae9bSBen Gras 		 */
1052639ae9bSBen Gras 		if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
1062639ae9bSBen Gras 		    !F_ISSET(&t->bt_cursor,
1072639ae9bSBen Gras 		        CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
1082639ae9bSBen Gras 			break;
1092639ae9bSBen Gras 		/* FALLTHROUGH */
1102639ae9bSBen Gras 	default:
1112639ae9bSBen Gras 		errno = EINVAL;
1122639ae9bSBen Gras 		return (RET_ERROR);
1132639ae9bSBen Gras 	}
1142639ae9bSBen Gras 
1152639ae9bSBen Gras 	/*
1162639ae9bSBen Gras 	 * If the key/data pair won't fit on a page, store it on overflow
1172639ae9bSBen Gras 	 * pages.  Only put the key on the overflow page if the pair are
1182639ae9bSBen Gras 	 * still too big after moving the data to an overflow page.
1192639ae9bSBen Gras 	 *
1202639ae9bSBen Gras 	 * XXX
1212639ae9bSBen Gras 	 * If the insert fails later on, the overflow pages aren't recovered.
1222639ae9bSBen Gras 	 */
1232639ae9bSBen Gras 	dflags = 0;
1242639ae9bSBen Gras 	if (key->size + data->size > t->bt_ovflsize) {
1252639ae9bSBen Gras 		if (key->size > t->bt_ovflsize) {
1262639ae9bSBen Gras storekey:		if (__ovfl_put(t, key, &pg) == RET_ERROR)
1272639ae9bSBen Gras 				return (RET_ERROR);
1282639ae9bSBen Gras 			tkey.data = kb;
1292639ae9bSBen Gras 			tkey.size = NOVFLSIZE;
130*f14fb602SLionel Sambuc 			memmove(kb, &pg, sizeof(pg));
1312639ae9bSBen Gras 			memmove(kb + sizeof(pgno_t),
1322639ae9bSBen Gras 			    &key->size, sizeof(uint32_t));
1332639ae9bSBen Gras 			dflags |= P_BIGKEY;
1342639ae9bSBen Gras 			key = &tkey;
1352639ae9bSBen Gras 		}
1362639ae9bSBen Gras 		if (key->size + data->size > t->bt_ovflsize) {
1372639ae9bSBen Gras 			if (__ovfl_put(t, data, &pg) == RET_ERROR)
1382639ae9bSBen Gras 				return (RET_ERROR);
1392639ae9bSBen Gras 			tdata.data = db;
1402639ae9bSBen Gras 			tdata.size = NOVFLSIZE;
141*f14fb602SLionel Sambuc 			memmove(db, &pg, sizeof(pg));
1422639ae9bSBen Gras 			_DBFIT(data->size, uint32_t);
1432639ae9bSBen Gras 			temp = (uint32_t)data->size;
1442639ae9bSBen Gras 			(void)memmove(db + sizeof(pgno_t),
1452639ae9bSBen Gras 			    &temp, sizeof(uint32_t));
1462639ae9bSBen Gras 			dflags |= P_BIGDATA;
1472639ae9bSBen Gras 			data = &tdata;
1482639ae9bSBen Gras 		}
1492639ae9bSBen Gras 		if (key->size + data->size > t->bt_ovflsize)
1502639ae9bSBen Gras 			goto storekey;
1512639ae9bSBen Gras 	}
1522639ae9bSBen Gras 
1532639ae9bSBen Gras 	/* Replace the cursor. */
1542639ae9bSBen Gras 	if (flags == R_CURSOR) {
1552639ae9bSBen Gras 		if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL)
1562639ae9bSBen Gras 			return (RET_ERROR);
1572639ae9bSBen Gras 		idx = t->bt_cursor.pg.index;
1582639ae9bSBen Gras 		goto delete;
1592639ae9bSBen Gras 	}
1602639ae9bSBen Gras 
1612639ae9bSBen Gras 	/*
1622639ae9bSBen Gras 	 * Find the key to delete, or, the location at which to insert.
1632639ae9bSBen Gras 	 * Bt_fast and __bt_search both pin the returned page.
1642639ae9bSBen Gras 	 */
1652639ae9bSBen Gras 	if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
1662639ae9bSBen Gras 		if ((e = __bt_search(t, key, &exact)) == NULL)
1672639ae9bSBen Gras 			return (RET_ERROR);
1682639ae9bSBen Gras 	h = e->page;
1692639ae9bSBen Gras 	idx = e->index;
1702639ae9bSBen Gras 
1712639ae9bSBen Gras 	/*
1722639ae9bSBen Gras 	 * Add the key/data pair to the tree.  If an identical key is already
1732639ae9bSBen Gras 	 * in the tree, and R_NOOVERWRITE is set, an error is returned.  If
1742639ae9bSBen Gras 	 * R_NOOVERWRITE is not set, the key is either added (if duplicates are
1752639ae9bSBen Gras 	 * permitted) or an error is returned.
1762639ae9bSBen Gras 	 */
1772639ae9bSBen Gras 	switch (flags) {
1782639ae9bSBen Gras 	case R_NOOVERWRITE:
1792639ae9bSBen Gras 		if (!exact)
1802639ae9bSBen Gras 			break;
1812639ae9bSBen Gras 		mpool_put(t->bt_mp, h, 0);
1822639ae9bSBen Gras 		return (RET_SPECIAL);
1832639ae9bSBen Gras 	default:
1842639ae9bSBen Gras 		if (!exact || !F_ISSET(t, B_NODUPS))
1852639ae9bSBen Gras 			break;
1862639ae9bSBen Gras 		/*
1872639ae9bSBen Gras 		 * !!!
1882639ae9bSBen Gras 		 * Note, the delete may empty the page, so we need to put a
1892639ae9bSBen Gras 		 * new entry into the page immediately.
1902639ae9bSBen Gras 		 */
1912639ae9bSBen Gras delete:		if (__bt_dleaf(t, key, h, (u_int)idx) == RET_ERROR) {
1922639ae9bSBen Gras 			mpool_put(t->bt_mp, h, 0);
1932639ae9bSBen Gras 			return (RET_ERROR);
1942639ae9bSBen Gras 		}
1952639ae9bSBen Gras 		break;
1962639ae9bSBen Gras 	}
1972639ae9bSBen Gras 
1982639ae9bSBen Gras 	/*
1992639ae9bSBen Gras 	 * If not enough room, or the user has put a ceiling on the number of
2002639ae9bSBen Gras 	 * keys permitted in the page, split the page.  The split code will
2012639ae9bSBen Gras 	 * insert the key and data and unpin the current page.  If inserting
2022639ae9bSBen Gras 	 * into the offset array, shift the pointers up.
2032639ae9bSBen Gras 	 */
2042639ae9bSBen Gras 	nbytes = NBLEAFDBT(key->size, data->size);
2052639ae9bSBen Gras 	if ((uint32_t)h->upper - (uint32_t)h->lower < nbytes + sizeof(indx_t)) {
2062639ae9bSBen Gras 		if ((status = __bt_split(t, h, key,
2072639ae9bSBen Gras 		    data, dflags, nbytes, (u_int)idx)) != RET_SUCCESS)
2082639ae9bSBen Gras 			return (status);
2092639ae9bSBen Gras 		goto success;
2102639ae9bSBen Gras 	}
2112639ae9bSBen Gras 
2122639ae9bSBen Gras 	if (idx < (nxtindex = NEXTINDEX(h)))
2132639ae9bSBen Gras 		memmove(h->linp + idx + 1, h->linp + idx,
2142639ae9bSBen Gras 		    (nxtindex - idx) * sizeof(indx_t));
2152639ae9bSBen Gras 	h->lower += sizeof(indx_t);
2162639ae9bSBen Gras 
2172639ae9bSBen Gras 	h->linp[idx] = h->upper -= nbytes;
2182639ae9bSBen Gras 	dest = (char *)(void *)h + h->upper;
2192639ae9bSBen Gras 	WR_BLEAF(dest, key, data, dflags);
2202639ae9bSBen Gras 
2212639ae9bSBen Gras 	/* If the cursor is on this page, adjust it as necessary. */
2222639ae9bSBen Gras 	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
2232639ae9bSBen Gras 	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
2242639ae9bSBen Gras 	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= idx)
2252639ae9bSBen Gras 		++t->bt_cursor.pg.index;
2262639ae9bSBen Gras 
2272639ae9bSBen Gras 	if (t->bt_order == NOT) {
2282639ae9bSBen Gras 		if (h->nextpg == P_INVALID) {
2292639ae9bSBen Gras 			if (idx == NEXTINDEX(h) - 1) {
2302639ae9bSBen Gras 				t->bt_order = FORWARD;
2312639ae9bSBen Gras 				t->bt_last.index = idx;
2322639ae9bSBen Gras 				t->bt_last.pgno = h->pgno;
2332639ae9bSBen Gras 			}
2342639ae9bSBen Gras 		} else if (h->prevpg == P_INVALID) {
2352639ae9bSBen Gras 			if (idx == 0) {
2362639ae9bSBen Gras 				t->bt_order = BACK;
2372639ae9bSBen Gras 				t->bt_last.index = 0;
2382639ae9bSBen Gras 				t->bt_last.pgno = h->pgno;
2392639ae9bSBen Gras 			}
2402639ae9bSBen Gras 		}
2412639ae9bSBen Gras 	}
2422639ae9bSBen Gras 
2432639ae9bSBen Gras 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
2442639ae9bSBen Gras 
2452639ae9bSBen Gras success:
2462639ae9bSBen Gras 	if (flags == R_SETCURSOR)
2472639ae9bSBen Gras 		__bt_setcur(t, e->page->pgno, (u_int)e->index);
2482639ae9bSBen Gras 
2492639ae9bSBen Gras 	F_SET(t, B_MODIFIED);
2502639ae9bSBen Gras 	return (RET_SUCCESS);
2512639ae9bSBen Gras }
2522639ae9bSBen Gras 
2532639ae9bSBen Gras #ifdef STATISTICS
2542639ae9bSBen Gras unsigned long bt_cache_hit, bt_cache_miss;
2552639ae9bSBen Gras #endif
2562639ae9bSBen Gras 
2572639ae9bSBen Gras /*
2582639ae9bSBen Gras  * BT_FAST -- Do a quick check for sorted data.
2592639ae9bSBen Gras  *
2602639ae9bSBen Gras  * Parameters:
2612639ae9bSBen Gras  *	t:	tree
2622639ae9bSBen Gras  *	key:	key to insert
2632639ae9bSBen Gras  *
2642639ae9bSBen Gras  * Returns:
2652639ae9bSBen Gras  * 	EPG for new record or NULL if not found.
2662639ae9bSBen Gras  */
2672639ae9bSBen Gras static EPG *
bt_fast(BTREE * t,const DBT * key,const DBT * data,int * exactp)2682639ae9bSBen Gras bt_fast(BTREE *t, const DBT *key, const DBT *data, int *exactp)
2692639ae9bSBen Gras {
2702639ae9bSBen Gras 	PAGE *h;
2712639ae9bSBen Gras 	uint32_t nbytes;
2722639ae9bSBen Gras 	int cmp;
2732639ae9bSBen Gras 
2742639ae9bSBen Gras 	if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
2752639ae9bSBen Gras 		t->bt_order = NOT;
2762639ae9bSBen Gras 		return (NULL);
2772639ae9bSBen Gras 	}
2782639ae9bSBen Gras 	t->bt_cur.page = h;
2792639ae9bSBen Gras 	t->bt_cur.index = t->bt_last.index;
2802639ae9bSBen Gras 
2812639ae9bSBen Gras 	/*
2822639ae9bSBen Gras 	 * If won't fit in this page or have too many keys in this page,
2832639ae9bSBen Gras 	 * have to search to get split stack.
2842639ae9bSBen Gras 	 */
2852639ae9bSBen Gras 	nbytes = NBLEAFDBT(key->size, data->size);
2862639ae9bSBen Gras 	if ((uint32_t)h->upper - (uint32_t)h->lower < nbytes + sizeof(indx_t))
2872639ae9bSBen Gras 		goto miss;
2882639ae9bSBen Gras 
2892639ae9bSBen Gras 	if (t->bt_order == FORWARD) {
2902639ae9bSBen Gras 		if (t->bt_cur.page->nextpg != P_INVALID)
2912639ae9bSBen Gras 			goto miss;
2922639ae9bSBen Gras 		if (t->bt_cur.index != NEXTINDEX(h) - 1)
2932639ae9bSBen Gras 			goto miss;
2942639ae9bSBen Gras 		if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
2952639ae9bSBen Gras 			goto miss;
2962639ae9bSBen Gras 		t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
2972639ae9bSBen Gras 	} else {
2982639ae9bSBen Gras 		if (t->bt_cur.page->prevpg != P_INVALID)
2992639ae9bSBen Gras 			goto miss;
3002639ae9bSBen Gras 		if (t->bt_cur.index != 0)
3012639ae9bSBen Gras 			goto miss;
3022639ae9bSBen Gras 		if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
3032639ae9bSBen Gras 			goto miss;
3042639ae9bSBen Gras 		t->bt_last.index = 0;
3052639ae9bSBen Gras 	}
3062639ae9bSBen Gras 	*exactp = cmp == 0;
3072639ae9bSBen Gras #ifdef STATISTICS
3082639ae9bSBen Gras 	++bt_cache_hit;
3092639ae9bSBen Gras #endif
3102639ae9bSBen Gras 	return (&t->bt_cur);
3112639ae9bSBen Gras 
3122639ae9bSBen Gras miss:
3132639ae9bSBen Gras #ifdef STATISTICS
3142639ae9bSBen Gras 	++bt_cache_miss;
3152639ae9bSBen Gras #endif
3162639ae9bSBen Gras 	t->bt_order = NOT;
3172639ae9bSBen Gras 	mpool_put(t->bt_mp, h, 0);
3182639ae9bSBen Gras 	return (NULL);
3192639ae9bSBen Gras }
320