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