Home | History | Annotate | Line # | Download | only in btree
bt_put.c revision 1.18.6.2
      1  1.18.6.2  joerg /*	$NetBSD: bt_put.c,v 1.18.6.2 2008/09/11 12:58:01 joerg Exp $	*/
      2  1.18.6.2  joerg 
      3  1.18.6.2  joerg /*-
      4  1.18.6.2  joerg  * Copyright (c) 1990, 1993, 1994
      5  1.18.6.2  joerg  *	The Regents of the University of California.  All rights reserved.
      6  1.18.6.2  joerg  *
      7  1.18.6.2  joerg  * This code is derived from software contributed to Berkeley by
      8  1.18.6.2  joerg  * Mike Olson.
      9  1.18.6.2  joerg  *
     10  1.18.6.2  joerg  * Redistribution and use in source and binary forms, with or without
     11  1.18.6.2  joerg  * modification, are permitted provided that the following conditions
     12  1.18.6.2  joerg  * are met:
     13  1.18.6.2  joerg  * 1. Redistributions of source code must retain the above copyright
     14  1.18.6.2  joerg  *    notice, this list of conditions and the following disclaimer.
     15  1.18.6.2  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.18.6.2  joerg  *    notice, this list of conditions and the following disclaimer in the
     17  1.18.6.2  joerg  *    documentation and/or other materials provided with the distribution.
     18  1.18.6.2  joerg  * 3. Neither the name of the University nor the names of its contributors
     19  1.18.6.2  joerg  *    may be used to endorse or promote products derived from this software
     20  1.18.6.2  joerg  *    without specific prior written permission.
     21  1.18.6.2  joerg  *
     22  1.18.6.2  joerg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.18.6.2  joerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.18.6.2  joerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.18.6.2  joerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.18.6.2  joerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.18.6.2  joerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.18.6.2  joerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.18.6.2  joerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.18.6.2  joerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.18.6.2  joerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.18.6.2  joerg  * SUCH DAMAGE.
     33  1.18.6.2  joerg  */
     34  1.18.6.2  joerg 
     35  1.18.6.2  joerg #if HAVE_NBTOOL_CONFIG_H
     36  1.18.6.2  joerg #include "nbtool_config.h"
     37  1.18.6.2  joerg #endif
     38  1.18.6.2  joerg 
     39  1.18.6.2  joerg #include <sys/cdefs.h>
     40  1.18.6.2  joerg __RCSID("$NetBSD: bt_put.c,v 1.18.6.2 2008/09/11 12:58:01 joerg Exp $");
     41  1.18.6.2  joerg 
     42  1.18.6.2  joerg #include "namespace.h"
     43  1.18.6.2  joerg #include <sys/types.h>
     44  1.18.6.2  joerg 
     45  1.18.6.2  joerg #include <assert.h>
     46  1.18.6.2  joerg #include <errno.h>
     47  1.18.6.2  joerg #include <stdio.h>
     48  1.18.6.2  joerg #include <stdlib.h>
     49  1.18.6.2  joerg #include <string.h>
     50  1.18.6.2  joerg 
     51  1.18.6.2  joerg #include <db.h>
     52  1.18.6.2  joerg #include "btree.h"
     53  1.18.6.2  joerg 
     54  1.18.6.2  joerg static EPG *bt_fast(BTREE *, const DBT *, const DBT *, int *);
     55  1.18.6.2  joerg 
     56  1.18.6.2  joerg /*
     57  1.18.6.2  joerg  * __BT_PUT -- Add a btree item to the tree.
     58  1.18.6.2  joerg  *
     59  1.18.6.2  joerg  * Parameters:
     60  1.18.6.2  joerg  *	dbp:	pointer to access method
     61  1.18.6.2  joerg  *	key:	key
     62  1.18.6.2  joerg  *	data:	data
     63  1.18.6.2  joerg  *	flag:	R_NOOVERWRITE
     64  1.18.6.2  joerg  *
     65  1.18.6.2  joerg  * Returns:
     66  1.18.6.2  joerg  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
     67  1.18.6.2  joerg  *	tree and R_NOOVERWRITE specified.
     68  1.18.6.2  joerg  */
     69  1.18.6.2  joerg int
     70  1.18.6.2  joerg __bt_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
     71  1.18.6.2  joerg {
     72  1.18.6.2  joerg 	BTREE *t;
     73  1.18.6.2  joerg 	DBT tkey, tdata;
     74  1.18.6.2  joerg 	EPG *e = NULL; /* pacify gcc */
     75  1.18.6.2  joerg 	PAGE *h;
     76  1.18.6.2  joerg 	indx_t idx, nxtindex;
     77  1.18.6.2  joerg 	pgno_t pg;
     78  1.18.6.2  joerg 	uint32_t nbytes, temp;
     79  1.18.6.2  joerg 	int dflags, exact, status;
     80  1.18.6.2  joerg 	char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
     81  1.18.6.2  joerg 
     82  1.18.6.2  joerg 	t = dbp->internal;
     83  1.18.6.2  joerg 
     84  1.18.6.2  joerg 	/* Toss any page pinned across calls. */
     85  1.18.6.2  joerg 	if (t->bt_pinned != NULL) {
     86  1.18.6.2  joerg 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     87  1.18.6.2  joerg 		t->bt_pinned = NULL;
     88  1.18.6.2  joerg 	}
     89  1.18.6.2  joerg 
     90  1.18.6.2  joerg 	/* Check for change to a read-only tree. */
     91  1.18.6.2  joerg 	if (F_ISSET(t, B_RDONLY)) {
     92  1.18.6.2  joerg 		errno = EPERM;
     93  1.18.6.2  joerg 		return (RET_ERROR);
     94  1.18.6.2  joerg 	}
     95  1.18.6.2  joerg 
     96  1.18.6.2  joerg 	switch (flags) {
     97  1.18.6.2  joerg 	case 0:
     98  1.18.6.2  joerg 	case R_NOOVERWRITE:
     99  1.18.6.2  joerg 		break;
    100  1.18.6.2  joerg 	case R_CURSOR:
    101  1.18.6.2  joerg 		/*
    102  1.18.6.2  joerg 		 * If flags is R_CURSOR, put the cursor.  Must already
    103  1.18.6.2  joerg 		 * have started a scan and not have already deleted it.
    104  1.18.6.2  joerg 		 */
    105  1.18.6.2  joerg 		if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
    106  1.18.6.2  joerg 		    !F_ISSET(&t->bt_cursor,
    107  1.18.6.2  joerg 		        CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
    108  1.18.6.2  joerg 			break;
    109  1.18.6.2  joerg 		/* FALLTHROUGH */
    110  1.18.6.2  joerg 	default:
    111  1.18.6.2  joerg 		errno = EINVAL;
    112  1.18.6.2  joerg 		return (RET_ERROR);
    113  1.18.6.2  joerg 	}
    114  1.18.6.2  joerg 
    115  1.18.6.2  joerg 	/*
    116  1.18.6.2  joerg 	 * If the key/data pair won't fit on a page, store it on overflow
    117  1.18.6.2  joerg 	 * pages.  Only put the key on the overflow page if the pair are
    118  1.18.6.2  joerg 	 * still too big after moving the data to an overflow page.
    119  1.18.6.2  joerg 	 *
    120  1.18.6.2  joerg 	 * XXX
    121  1.18.6.2  joerg 	 * If the insert fails later on, the overflow pages aren't recovered.
    122  1.18.6.2  joerg 	 */
    123  1.18.6.2  joerg 	dflags = 0;
    124  1.18.6.2  joerg 	if (key->size + data->size > t->bt_ovflsize) {
    125  1.18.6.2  joerg 		if (key->size > t->bt_ovflsize) {
    126  1.18.6.2  joerg storekey:		if (__ovfl_put(t, key, &pg) == RET_ERROR)
    127  1.18.6.2  joerg 				return (RET_ERROR);
    128  1.18.6.2  joerg 			tkey.data = kb;
    129  1.18.6.2  joerg 			tkey.size = NOVFLSIZE;
    130  1.18.6.2  joerg 			memmove(kb, &pg, sizeof(pgno_t));
    131  1.18.6.2  joerg 			memmove(kb + sizeof(pgno_t),
    132  1.18.6.2  joerg 			    &key->size, sizeof(uint32_t));
    133  1.18.6.2  joerg 			dflags |= P_BIGKEY;
    134  1.18.6.2  joerg 			key = &tkey;
    135  1.18.6.2  joerg 		}
    136  1.18.6.2  joerg 		if (key->size + data->size > t->bt_ovflsize) {
    137  1.18.6.2  joerg 			if (__ovfl_put(t, data, &pg) == RET_ERROR)
    138  1.18.6.2  joerg 				return (RET_ERROR);
    139  1.18.6.2  joerg 			tdata.data = db;
    140  1.18.6.2  joerg 			tdata.size = NOVFLSIZE;
    141  1.18.6.2  joerg 			memmove(db, &pg, sizeof(pgno_t));
    142  1.18.6.2  joerg 			_DBFIT(data->size, uint32_t);
    143  1.18.6.2  joerg 			temp = (uint32_t)data->size;
    144  1.18.6.2  joerg 			(void)memmove(db + sizeof(pgno_t),
    145  1.18.6.2  joerg 			    &temp, sizeof(uint32_t));
    146  1.18.6.2  joerg 			dflags |= P_BIGDATA;
    147  1.18.6.2  joerg 			data = &tdata;
    148  1.18.6.2  joerg 		}
    149  1.18.6.2  joerg 		if (key->size + data->size > t->bt_ovflsize)
    150  1.18.6.2  joerg 			goto storekey;
    151  1.18.6.2  joerg 	}
    152  1.18.6.2  joerg 
    153  1.18.6.2  joerg 	/* Replace the cursor. */
    154  1.18.6.2  joerg 	if (flags == R_CURSOR) {
    155  1.18.6.2  joerg 		if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL)
    156  1.18.6.2  joerg 			return (RET_ERROR);
    157  1.18.6.2  joerg 		idx = t->bt_cursor.pg.index;
    158  1.18.6.2  joerg 		goto delete;
    159  1.18.6.2  joerg 	}
    160  1.18.6.2  joerg 
    161  1.18.6.2  joerg 	/*
    162  1.18.6.2  joerg 	 * Find the key to delete, or, the location at which to insert.
    163  1.18.6.2  joerg 	 * Bt_fast and __bt_search both pin the returned page.
    164  1.18.6.2  joerg 	 */
    165  1.18.6.2  joerg 	if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
    166  1.18.6.2  joerg 		if ((e = __bt_search(t, key, &exact)) == NULL)
    167  1.18.6.2  joerg 			return (RET_ERROR);
    168  1.18.6.2  joerg 	h = e->page;
    169  1.18.6.2  joerg 	idx = e->index;
    170  1.18.6.2  joerg 
    171  1.18.6.2  joerg 	/*
    172  1.18.6.2  joerg 	 * Add the key/data pair to the tree.  If an identical key is already
    173  1.18.6.2  joerg 	 * in the tree, and R_NOOVERWRITE is set, an error is returned.  If
    174  1.18.6.2  joerg 	 * R_NOOVERWRITE is not set, the key is either added (if duplicates are
    175  1.18.6.2  joerg 	 * permitted) or an error is returned.
    176  1.18.6.2  joerg 	 */
    177  1.18.6.2  joerg 	switch (flags) {
    178  1.18.6.2  joerg 	case R_NOOVERWRITE:
    179  1.18.6.2  joerg 		if (!exact)
    180  1.18.6.2  joerg 			break;
    181  1.18.6.2  joerg 		mpool_put(t->bt_mp, h, 0);
    182  1.18.6.2  joerg 		return (RET_SPECIAL);
    183  1.18.6.2  joerg 	default:
    184  1.18.6.2  joerg 		if (!exact || !F_ISSET(t, B_NODUPS))
    185  1.18.6.2  joerg 			break;
    186  1.18.6.2  joerg 		/*
    187  1.18.6.2  joerg 		 * !!!
    188  1.18.6.2  joerg 		 * Note, the delete may empty the page, so we need to put a
    189  1.18.6.2  joerg 		 * new entry into the page immediately.
    190  1.18.6.2  joerg 		 */
    191  1.18.6.2  joerg delete:		if (__bt_dleaf(t, key, h, (u_int)idx) == RET_ERROR) {
    192  1.18.6.2  joerg 			mpool_put(t->bt_mp, h, 0);
    193  1.18.6.2  joerg 			return (RET_ERROR);
    194  1.18.6.2  joerg 		}
    195  1.18.6.2  joerg 		break;
    196  1.18.6.2  joerg 	}
    197  1.18.6.2  joerg 
    198  1.18.6.2  joerg 	/*
    199  1.18.6.2  joerg 	 * If not enough room, or the user has put a ceiling on the number of
    200  1.18.6.2  joerg 	 * keys permitted in the page, split the page.  The split code will
    201  1.18.6.2  joerg 	 * insert the key and data and unpin the current page.  If inserting
    202  1.18.6.2  joerg 	 * into the offset array, shift the pointers up.
    203  1.18.6.2  joerg 	 */
    204  1.18.6.2  joerg 	nbytes = NBLEAFDBT(key->size, data->size);
    205  1.18.6.2  joerg 	if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
    206  1.18.6.2  joerg 		if ((status = __bt_split(t, h, key,
    207  1.18.6.2  joerg 		    data, dflags, nbytes, (u_int)idx)) != RET_SUCCESS)
    208  1.18.6.2  joerg 			return (status);
    209  1.18.6.2  joerg 		goto success;
    210  1.18.6.2  joerg 	}
    211  1.18.6.2  joerg 
    212  1.18.6.2  joerg 	if (idx < (nxtindex = NEXTINDEX(h)))
    213  1.18.6.2  joerg 		memmove(h->linp + idx + 1, h->linp + idx,
    214  1.18.6.2  joerg 		    (nxtindex - idx) * sizeof(indx_t));
    215  1.18.6.2  joerg 	h->lower += sizeof(indx_t);
    216  1.18.6.2  joerg 
    217  1.18.6.2  joerg 	h->linp[idx] = h->upper -= nbytes;
    218  1.18.6.2  joerg 	dest = (char *)(void *)h + h->upper;
    219  1.18.6.2  joerg 	WR_BLEAF(dest, key, data, dflags);
    220  1.18.6.2  joerg 
    221  1.18.6.2  joerg 	/* If the cursor is on this page, adjust it as necessary. */
    222  1.18.6.2  joerg 	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
    223  1.18.6.2  joerg 	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
    224  1.18.6.2  joerg 	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= idx)
    225  1.18.6.2  joerg 		++t->bt_cursor.pg.index;
    226  1.18.6.2  joerg 
    227  1.18.6.2  joerg 	if (t->bt_order == NOT) {
    228  1.18.6.2  joerg 		if (h->nextpg == P_INVALID) {
    229  1.18.6.2  joerg 			if (idx == NEXTINDEX(h) - 1) {
    230  1.18.6.2  joerg 				t->bt_order = FORWARD;
    231  1.18.6.2  joerg 				t->bt_last.index = idx;
    232  1.18.6.2  joerg 				t->bt_last.pgno = h->pgno;
    233  1.18.6.2  joerg 			}
    234  1.18.6.2  joerg 		} else if (h->prevpg == P_INVALID) {
    235  1.18.6.2  joerg 			if (idx == 0) {
    236  1.18.6.2  joerg 				t->bt_order = BACK;
    237  1.18.6.2  joerg 				t->bt_last.index = 0;
    238  1.18.6.2  joerg 				t->bt_last.pgno = h->pgno;
    239  1.18.6.2  joerg 			}
    240  1.18.6.2  joerg 		}
    241  1.18.6.2  joerg 	}
    242  1.18.6.2  joerg 
    243  1.18.6.2  joerg 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    244  1.18.6.2  joerg 
    245  1.18.6.2  joerg success:
    246  1.18.6.2  joerg 	if (flags == R_SETCURSOR)
    247  1.18.6.2  joerg 		__bt_setcur(t, e->page->pgno, (u_int)e->index);
    248  1.18.6.2  joerg 
    249  1.18.6.2  joerg 	F_SET(t, B_MODIFIED);
    250  1.18.6.2  joerg 	return (RET_SUCCESS);
    251  1.18.6.2  joerg }
    252  1.18.6.2  joerg 
    253  1.18.6.2  joerg #ifdef STATISTICS
    254  1.18.6.2  joerg unsigned long bt_cache_hit, bt_cache_miss;
    255  1.18.6.2  joerg #endif
    256  1.18.6.2  joerg 
    257  1.18.6.2  joerg /*
    258  1.18.6.2  joerg  * BT_FAST -- Do a quick check for sorted data.
    259  1.18.6.2  joerg  *
    260  1.18.6.2  joerg  * Parameters:
    261  1.18.6.2  joerg  *	t:	tree
    262  1.18.6.2  joerg  *	key:	key to insert
    263  1.18.6.2  joerg  *
    264  1.18.6.2  joerg  * Returns:
    265  1.18.6.2  joerg  * 	EPG for new record or NULL if not found.
    266  1.18.6.2  joerg  */
    267  1.18.6.2  joerg static EPG *
    268  1.18.6.2  joerg bt_fast(BTREE *t, const DBT *key, const DBT *data, int *exactp)
    269  1.18.6.2  joerg {
    270  1.18.6.2  joerg 	PAGE *h;
    271  1.18.6.2  joerg 	uint32_t nbytes;
    272  1.18.6.2  joerg 	int cmp;
    273  1.18.6.2  joerg 
    274  1.18.6.2  joerg 	if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
    275  1.18.6.2  joerg 		t->bt_order = NOT;
    276  1.18.6.2  joerg 		return (NULL);
    277  1.18.6.2  joerg 	}
    278  1.18.6.2  joerg 	t->bt_cur.page = h;
    279  1.18.6.2  joerg 	t->bt_cur.index = t->bt_last.index;
    280  1.18.6.2  joerg 
    281  1.18.6.2  joerg 	/*
    282  1.18.6.2  joerg 	 * If won't fit in this page or have too many keys in this page,
    283  1.18.6.2  joerg 	 * have to search to get split stack.
    284  1.18.6.2  joerg 	 */
    285  1.18.6.2  joerg 	nbytes = NBLEAFDBT(key->size, data->size);
    286  1.18.6.2  joerg 	if (h->upper - h->lower < nbytes + sizeof(indx_t))
    287  1.18.6.2  joerg 		goto miss;
    288  1.18.6.2  joerg 
    289  1.18.6.2  joerg 	if (t->bt_order == FORWARD) {
    290  1.18.6.2  joerg 		if (t->bt_cur.page->nextpg != P_INVALID)
    291  1.18.6.2  joerg 			goto miss;
    292  1.18.6.2  joerg 		if (t->bt_cur.index != NEXTINDEX(h) - 1)
    293  1.18.6.2  joerg 			goto miss;
    294  1.18.6.2  joerg 		if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
    295  1.18.6.2  joerg 			goto miss;
    296  1.18.6.2  joerg 		t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
    297  1.18.6.2  joerg 	} else {
    298  1.18.6.2  joerg 		if (t->bt_cur.page->prevpg != P_INVALID)
    299  1.18.6.2  joerg 			goto miss;
    300  1.18.6.2  joerg 		if (t->bt_cur.index != 0)
    301  1.18.6.2  joerg 			goto miss;
    302  1.18.6.2  joerg 		if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
    303  1.18.6.2  joerg 			goto miss;
    304  1.18.6.2  joerg 		t->bt_last.index = 0;
    305  1.18.6.2  joerg 	}
    306  1.18.6.2  joerg 	*exactp = cmp == 0;
    307  1.18.6.2  joerg #ifdef STATISTICS
    308  1.18.6.2  joerg 	++bt_cache_hit;
    309  1.18.6.2  joerg #endif
    310  1.18.6.2  joerg 	return (&t->bt_cur);
    311  1.18.6.2  joerg 
    312  1.18.6.2  joerg miss:
    313  1.18.6.2  joerg #ifdef STATISTICS
    314  1.18.6.2  joerg 	++bt_cache_miss;
    315  1.18.6.2  joerg #endif
    316  1.18.6.2  joerg 	t->bt_order = NOT;
    317  1.18.6.2  joerg 	mpool_put(t->bt_mp, h, 0);
    318  1.18.6.2  joerg 	return (NULL);
    319  1.18.6.2  joerg }
    320