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