Home | History | Annotate | Line # | Download | only in recno
rec_put.c revision 1.17.6.2
      1  1.17.6.2  joerg /*	$NetBSD: rec_put.c,v 1.17.6.2 2008/09/11 12:58:01 joerg Exp $	*/
      2  1.17.6.2  joerg 
      3  1.17.6.2  joerg /*-
      4  1.17.6.2  joerg  * Copyright (c) 1990, 1993, 1994
      5  1.17.6.2  joerg  *	The Regents of the University of California.  All rights reserved.
      6  1.17.6.2  joerg  *
      7  1.17.6.2  joerg  * Redistribution and use in source and binary forms, with or without
      8  1.17.6.2  joerg  * modification, are permitted provided that the following conditions
      9  1.17.6.2  joerg  * are met:
     10  1.17.6.2  joerg  * 1. Redistributions of source code must retain the above copyright
     11  1.17.6.2  joerg  *    notice, this list of conditions and the following disclaimer.
     12  1.17.6.2  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.17.6.2  joerg  *    notice, this list of conditions and the following disclaimer in the
     14  1.17.6.2  joerg  *    documentation and/or other materials provided with the distribution.
     15  1.17.6.2  joerg  * 3. Neither the name of the University nor the names of its contributors
     16  1.17.6.2  joerg  *    may be used to endorse or promote products derived from this software
     17  1.17.6.2  joerg  *    without specific prior written permission.
     18  1.17.6.2  joerg  *
     19  1.17.6.2  joerg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.17.6.2  joerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.17.6.2  joerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.17.6.2  joerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.17.6.2  joerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.17.6.2  joerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.17.6.2  joerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.17.6.2  joerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.17.6.2  joerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.17.6.2  joerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.17.6.2  joerg  * SUCH DAMAGE.
     30  1.17.6.2  joerg  */
     31  1.17.6.2  joerg 
     32  1.17.6.2  joerg #if HAVE_NBTOOL_CONFIG_H
     33  1.17.6.2  joerg #include "nbtool_config.h"
     34  1.17.6.2  joerg #endif
     35  1.17.6.2  joerg 
     36  1.17.6.2  joerg #include <sys/cdefs.h>
     37  1.17.6.2  joerg __RCSID("$NetBSD: rec_put.c,v 1.17.6.2 2008/09/11 12:58:01 joerg Exp $");
     38  1.17.6.2  joerg 
     39  1.17.6.2  joerg #include "namespace.h"
     40  1.17.6.2  joerg #include <sys/types.h>
     41  1.17.6.2  joerg 
     42  1.17.6.2  joerg #include <assert.h>
     43  1.17.6.2  joerg #include <errno.h>
     44  1.17.6.2  joerg #include <stdio.h>
     45  1.17.6.2  joerg #include <stdlib.h>
     46  1.17.6.2  joerg #include <string.h>
     47  1.17.6.2  joerg 
     48  1.17.6.2  joerg #include <db.h>
     49  1.17.6.2  joerg #include "recno.h"
     50  1.17.6.2  joerg 
     51  1.17.6.2  joerg /*
     52  1.17.6.2  joerg  * __REC_PUT -- Add a recno item to the tree.
     53  1.17.6.2  joerg  *
     54  1.17.6.2  joerg  * Parameters:
     55  1.17.6.2  joerg  *	dbp:	pointer to access method
     56  1.17.6.2  joerg  *	key:	key
     57  1.17.6.2  joerg  *	data:	data
     58  1.17.6.2  joerg  *	flag:	R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
     59  1.17.6.2  joerg  *
     60  1.17.6.2  joerg  * Returns:
     61  1.17.6.2  joerg  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
     62  1.17.6.2  joerg  *	already in the tree and R_NOOVERWRITE specified.
     63  1.17.6.2  joerg  */
     64  1.17.6.2  joerg int
     65  1.17.6.2  joerg __rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
     66  1.17.6.2  joerg {
     67  1.17.6.2  joerg 	BTREE *t;
     68  1.17.6.2  joerg 	DBT fdata, tdata;
     69  1.17.6.2  joerg 	recno_t nrec;
     70  1.17.6.2  joerg 	int status;
     71  1.17.6.2  joerg 
     72  1.17.6.2  joerg 	t = dbp->internal;
     73  1.17.6.2  joerg 
     74  1.17.6.2  joerg 	/* Toss any page pinned across calls. */
     75  1.17.6.2  joerg 	if (t->bt_pinned != NULL) {
     76  1.17.6.2  joerg 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     77  1.17.6.2  joerg 		t->bt_pinned = NULL;
     78  1.17.6.2  joerg 	}
     79  1.17.6.2  joerg 
     80  1.17.6.2  joerg 	/*
     81  1.17.6.2  joerg 	 * If using fixed-length records, and the record is long, return
     82  1.17.6.2  joerg 	 * EINVAL.  If it's short, pad it out.  Use the record data return
     83  1.17.6.2  joerg 	 * memory, it's only short-term.
     84  1.17.6.2  joerg 	 */
     85  1.17.6.2  joerg 	if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
     86  1.17.6.2  joerg 		if (data->size > t->bt_reclen)
     87  1.17.6.2  joerg 			goto einval;
     88  1.17.6.2  joerg 
     89  1.17.6.2  joerg 		if (t->bt_rdata.size < t->bt_reclen) {
     90  1.17.6.2  joerg 			t->bt_rdata.data = t->bt_rdata.data == NULL ?
     91  1.17.6.2  joerg 			    malloc(t->bt_reclen) :
     92  1.17.6.2  joerg 			    realloc(t->bt_rdata.data, t->bt_reclen);
     93  1.17.6.2  joerg 			if (t->bt_rdata.data == NULL)
     94  1.17.6.2  joerg 				return (RET_ERROR);
     95  1.17.6.2  joerg 			t->bt_rdata.size = t->bt_reclen;
     96  1.17.6.2  joerg 		}
     97  1.17.6.2  joerg 		memmove(t->bt_rdata.data, data->data, data->size);
     98  1.17.6.2  joerg 		memset((char *)t->bt_rdata.data + data->size,
     99  1.17.6.2  joerg 		    t->bt_bval, t->bt_reclen - data->size);
    100  1.17.6.2  joerg 		fdata.data = t->bt_rdata.data;
    101  1.17.6.2  joerg 		fdata.size = t->bt_reclen;
    102  1.17.6.2  joerg 	} else {
    103  1.17.6.2  joerg 		fdata.data = data->data;
    104  1.17.6.2  joerg 		fdata.size = data->size;
    105  1.17.6.2  joerg 	}
    106  1.17.6.2  joerg 
    107  1.17.6.2  joerg 	switch (flags) {
    108  1.17.6.2  joerg 	case R_CURSOR:
    109  1.17.6.2  joerg 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
    110  1.17.6.2  joerg 			goto einval;
    111  1.17.6.2  joerg 		nrec = t->bt_cursor.rcursor;
    112  1.17.6.2  joerg 		break;
    113  1.17.6.2  joerg 	case R_SETCURSOR:
    114  1.17.6.2  joerg 		if ((nrec = *(recno_t *)key->data) == 0)
    115  1.17.6.2  joerg 			goto einval;
    116  1.17.6.2  joerg 		break;
    117  1.17.6.2  joerg 	case R_IAFTER:
    118  1.17.6.2  joerg 		if ((nrec = *(recno_t *)key->data) == 0) {
    119  1.17.6.2  joerg 			nrec = 1;
    120  1.17.6.2  joerg 			flags = R_IBEFORE;
    121  1.17.6.2  joerg 		}
    122  1.17.6.2  joerg 		break;
    123  1.17.6.2  joerg 	case 0:
    124  1.17.6.2  joerg 	case R_IBEFORE:
    125  1.17.6.2  joerg 		if ((nrec = *(recno_t *)key->data) == 0)
    126  1.17.6.2  joerg 			goto einval;
    127  1.17.6.2  joerg 		break;
    128  1.17.6.2  joerg 	case R_NOOVERWRITE:
    129  1.17.6.2  joerg 		if ((nrec = *(recno_t *)key->data) == 0)
    130  1.17.6.2  joerg 			goto einval;
    131  1.17.6.2  joerg 		if (nrec <= t->bt_nrecs)
    132  1.17.6.2  joerg 			return (RET_SPECIAL);
    133  1.17.6.2  joerg 		break;
    134  1.17.6.2  joerg 	default:
    135  1.17.6.2  joerg einval:		errno = EINVAL;
    136  1.17.6.2  joerg 		return (RET_ERROR);
    137  1.17.6.2  joerg 	}
    138  1.17.6.2  joerg 
    139  1.17.6.2  joerg 	/*
    140  1.17.6.2  joerg 	 * Make sure that records up to and including the put record are
    141  1.17.6.2  joerg 	 * already in the database.  If skipping records, create empty ones.
    142  1.17.6.2  joerg 	 */
    143  1.17.6.2  joerg 	if (nrec > t->bt_nrecs) {
    144  1.17.6.2  joerg 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
    145  1.17.6.2  joerg 		    t->bt_irec(t, nrec) == RET_ERROR)
    146  1.17.6.2  joerg 			return (RET_ERROR);
    147  1.17.6.2  joerg 		if (nrec > t->bt_nrecs + 1) {
    148  1.17.6.2  joerg 			if (F_ISSET(t, R_FIXLEN)) {
    149  1.17.6.2  joerg 				if ((tdata.data =
    150  1.17.6.2  joerg 				    (void *)malloc(t->bt_reclen)) == NULL)
    151  1.17.6.2  joerg 					return (RET_ERROR);
    152  1.17.6.2  joerg 				tdata.size = t->bt_reclen;
    153  1.17.6.2  joerg 				memset(tdata.data, t->bt_bval, tdata.size);
    154  1.17.6.2  joerg 			} else {
    155  1.17.6.2  joerg 				tdata.data = NULL;
    156  1.17.6.2  joerg 				tdata.size = 0;
    157  1.17.6.2  joerg 			}
    158  1.17.6.2  joerg 			while (nrec > t->bt_nrecs + 1)
    159  1.17.6.2  joerg 				if (__rec_iput(t,
    160  1.17.6.2  joerg 				    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
    161  1.17.6.2  joerg 					return (RET_ERROR);
    162  1.17.6.2  joerg 			if (F_ISSET(t, R_FIXLEN))
    163  1.17.6.2  joerg 				free(tdata.data);
    164  1.17.6.2  joerg 		}
    165  1.17.6.2  joerg 	}
    166  1.17.6.2  joerg 
    167  1.17.6.2  joerg 	if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
    168  1.17.6.2  joerg 		return (status);
    169  1.17.6.2  joerg 
    170  1.17.6.2  joerg 	if (flags == R_SETCURSOR)
    171  1.17.6.2  joerg 		t->bt_cursor.rcursor = nrec;
    172  1.17.6.2  joerg 
    173  1.17.6.2  joerg 	F_SET(t, R_MODIFIED);
    174  1.17.6.2  joerg 	return (__rec_ret(t, NULL, nrec, key, NULL));
    175  1.17.6.2  joerg }
    176  1.17.6.2  joerg 
    177  1.17.6.2  joerg /*
    178  1.17.6.2  joerg  * __REC_IPUT -- Add a recno item to the tree.
    179  1.17.6.2  joerg  *
    180  1.17.6.2  joerg  * Parameters:
    181  1.17.6.2  joerg  *	t:	tree
    182  1.17.6.2  joerg  *	nrec:	record number
    183  1.17.6.2  joerg  *	data:	data
    184  1.17.6.2  joerg  *
    185  1.17.6.2  joerg  * Returns:
    186  1.17.6.2  joerg  *	RET_ERROR, RET_SUCCESS
    187  1.17.6.2  joerg  */
    188  1.17.6.2  joerg int
    189  1.17.6.2  joerg __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
    190  1.17.6.2  joerg {
    191  1.17.6.2  joerg 	DBT tdata;
    192  1.17.6.2  joerg 	EPG *e;
    193  1.17.6.2  joerg 	PAGE *h;
    194  1.17.6.2  joerg 	indx_t idx, nxtindex;
    195  1.17.6.2  joerg 	pgno_t pg;
    196  1.17.6.2  joerg 	uint32_t nbytes;
    197  1.17.6.2  joerg 	int dflags, status;
    198  1.17.6.2  joerg 	char *dest, db[NOVFLSIZE];
    199  1.17.6.2  joerg 
    200  1.17.6.2  joerg 	/*
    201  1.17.6.2  joerg 	 * If the data won't fit on a page, store it on indirect pages.
    202  1.17.6.2  joerg 	 *
    203  1.17.6.2  joerg 	 * XXX
    204  1.17.6.2  joerg 	 * If the insert fails later on, these pages aren't recovered.
    205  1.17.6.2  joerg 	 */
    206  1.17.6.2  joerg 	if (data->size > t->bt_ovflsize) {
    207  1.17.6.2  joerg 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
    208  1.17.6.2  joerg 			return (RET_ERROR);
    209  1.17.6.2  joerg 		tdata.data = db;
    210  1.17.6.2  joerg 		tdata.size = NOVFLSIZE;
    211  1.17.6.2  joerg 		*(pgno_t *)(void *)db = pg;
    212  1.17.6.2  joerg 		_DBFIT(data->size, uint32_t);
    213  1.17.6.2  joerg 		*(uint32_t *)(void *)(db + sizeof(pgno_t)) =
    214  1.17.6.2  joerg 		    (uint32_t)data->size;
    215  1.17.6.2  joerg 		dflags = P_BIGDATA;
    216  1.17.6.2  joerg 		data = &tdata;
    217  1.17.6.2  joerg 	} else
    218  1.17.6.2  joerg 		dflags = 0;
    219  1.17.6.2  joerg 
    220  1.17.6.2  joerg 	/* __rec_search pins the returned page. */
    221  1.17.6.2  joerg 	if ((e = __rec_search(t, nrec,
    222  1.17.6.2  joerg 	    nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
    223  1.17.6.2  joerg 	    SINSERT : SEARCH)) == NULL)
    224  1.17.6.2  joerg 		return (RET_ERROR);
    225  1.17.6.2  joerg 
    226  1.17.6.2  joerg 	h = e->page;
    227  1.17.6.2  joerg 	idx = e->index;
    228  1.17.6.2  joerg 
    229  1.17.6.2  joerg 	/*
    230  1.17.6.2  joerg 	 * Add the specified key/data pair to the tree.  The R_IAFTER and
    231  1.17.6.2  joerg 	 * R_IBEFORE flags insert the key after/before the specified key.
    232  1.17.6.2  joerg 	 *
    233  1.17.6.2  joerg 	 * Pages are split as required.
    234  1.17.6.2  joerg 	 */
    235  1.17.6.2  joerg 	switch (flags) {
    236  1.17.6.2  joerg 	case R_IAFTER:
    237  1.17.6.2  joerg 		++idx;
    238  1.17.6.2  joerg 		break;
    239  1.17.6.2  joerg 	case R_IBEFORE:
    240  1.17.6.2  joerg 		break;
    241  1.17.6.2  joerg 	default:
    242  1.17.6.2  joerg 		if (nrec < t->bt_nrecs &&
    243  1.17.6.2  joerg 		    __rec_dleaf(t, h, (uint32_t)idx) == RET_ERROR) {
    244  1.17.6.2  joerg 			mpool_put(t->bt_mp, h, 0);
    245  1.17.6.2  joerg 			return (RET_ERROR);
    246  1.17.6.2  joerg 		}
    247  1.17.6.2  joerg 		break;
    248  1.17.6.2  joerg 	}
    249  1.17.6.2  joerg 
    250  1.17.6.2  joerg 	/*
    251  1.17.6.2  joerg 	 * If not enough room, split the page.  The split code will insert
    252  1.17.6.2  joerg 	 * the key and data and unpin the current page.  If inserting into
    253  1.17.6.2  joerg 	 * the offset array, shift the pointers up.
    254  1.17.6.2  joerg 	 */
    255  1.17.6.2  joerg 	nbytes = NRLEAFDBT(data->size);
    256  1.17.6.2  joerg 	if ((uint32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) {
    257  1.17.6.2  joerg 		status = __bt_split(t, h, NULL, data, dflags, nbytes,
    258  1.17.6.2  joerg 		    (uint32_t)idx);
    259  1.17.6.2  joerg 		if (status == RET_SUCCESS)
    260  1.17.6.2  joerg 			++t->bt_nrecs;
    261  1.17.6.2  joerg 		return (status);
    262  1.17.6.2  joerg 	}
    263  1.17.6.2  joerg 
    264  1.17.6.2  joerg 	if (idx < (nxtindex = NEXTINDEX(h)))
    265  1.17.6.2  joerg 		memmove(h->linp + idx + 1, h->linp + idx,
    266  1.17.6.2  joerg 		    (nxtindex - idx) * sizeof(indx_t));
    267  1.17.6.2  joerg 	h->lower += sizeof(indx_t);
    268  1.17.6.2  joerg 
    269  1.17.6.2  joerg 	h->linp[idx] = h->upper -= nbytes;
    270  1.17.6.2  joerg 	dest = (char *)(void *)h + h->upper;
    271  1.17.6.2  joerg 	WR_RLEAF(dest, data, dflags);
    272  1.17.6.2  joerg 
    273  1.17.6.2  joerg 	++t->bt_nrecs;
    274  1.17.6.2  joerg 	F_SET(t, B_MODIFIED);
    275  1.17.6.2  joerg 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    276  1.17.6.2  joerg 
    277  1.17.6.2  joerg 	return (RET_SUCCESS);
    278  1.17.6.2  joerg }
    279