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