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