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