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