17c478bd9Sstevel@tonic-gate /*-
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994
37c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate  * are met:
87c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
147c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
157c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
167c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
177c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
187c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
197c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
357c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#)rec_put.c	8.7 (Berkeley) 8/18/94";
367c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "db-int.h"
467c478bd9Sstevel@tonic-gate #include "recno.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * __REC_PUT -- Add a recno item to the tree.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * Parameters:
527c478bd9Sstevel@tonic-gate  *	dbp:	pointer to access method
537c478bd9Sstevel@tonic-gate  *	key:	key
547c478bd9Sstevel@tonic-gate  *	data:	data
557c478bd9Sstevel@tonic-gate  *	flag:	R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * Returns:
587c478bd9Sstevel@tonic-gate  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
597c478bd9Sstevel@tonic-gate  *	already in the tree and R_NOOVERWRITE specified.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate int
__rec_put(dbp,key,data,flags)627c478bd9Sstevel@tonic-gate __rec_put(dbp, key, data, flags)
637c478bd9Sstevel@tonic-gate 	const DB *dbp;
647c478bd9Sstevel@tonic-gate 	DBT *key;
657c478bd9Sstevel@tonic-gate 	const DBT *data;
667c478bd9Sstevel@tonic-gate 	u_int flags;
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	BTREE *t;
697c478bd9Sstevel@tonic-gate 	DBT fdata, tdata;
707c478bd9Sstevel@tonic-gate 	recno_t nrec;
717c478bd9Sstevel@tonic-gate 	int status;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	t = dbp->internal;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/* Toss any page pinned across calls. */
767c478bd9Sstevel@tonic-gate 	if (t->bt_pinned != NULL) {
777c478bd9Sstevel@tonic-gate 		mpool_put(t->bt_mp, t->bt_pinned, 0);
787c478bd9Sstevel@tonic-gate 		t->bt_pinned = NULL;
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	/*
827c478bd9Sstevel@tonic-gate 	 * If using fixed-length records, and the record is long, return
837c478bd9Sstevel@tonic-gate 	 * EINVAL.  If it's short, pad it out.  Use the record data return
847c478bd9Sstevel@tonic-gate 	 * memory, it's only short-term.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate 	if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
877c478bd9Sstevel@tonic-gate 		if (data->size > t->bt_reclen)
887c478bd9Sstevel@tonic-gate 			goto einval;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		if (t->bt_rdata.size < t->bt_reclen) {
917c478bd9Sstevel@tonic-gate 			t->bt_rdata.data = t->bt_rdata.data == NULL ?
927c478bd9Sstevel@tonic-gate 			    malloc(t->bt_reclen) :
937c478bd9Sstevel@tonic-gate 			    realloc(t->bt_rdata.data, t->bt_reclen);
947c478bd9Sstevel@tonic-gate 			if (t->bt_rdata.data == NULL)
957c478bd9Sstevel@tonic-gate 				return (RET_ERROR);
967c478bd9Sstevel@tonic-gate 			t->bt_rdata.size = t->bt_reclen;
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 		memmove(t->bt_rdata.data, data->data, data->size);
997c478bd9Sstevel@tonic-gate 		memset((char *)t->bt_rdata.data + data->size,
1007c478bd9Sstevel@tonic-gate 		    t->bt_bval, t->bt_reclen - data->size);
1017c478bd9Sstevel@tonic-gate 		fdata.data = t->bt_rdata.data;
1027c478bd9Sstevel@tonic-gate 		fdata.size = t->bt_reclen;
1037c478bd9Sstevel@tonic-gate 	} else {
1047c478bd9Sstevel@tonic-gate 		fdata.data = data->data;
1057c478bd9Sstevel@tonic-gate 		fdata.size = data->size;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	switch (flags) {
1097c478bd9Sstevel@tonic-gate 	case R_CURSOR:
1107c478bd9Sstevel@tonic-gate 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
1117c478bd9Sstevel@tonic-gate 			goto einval;
1127c478bd9Sstevel@tonic-gate 		nrec = t->bt_cursor.rcursor;
1137c478bd9Sstevel@tonic-gate 		break;
1147c478bd9Sstevel@tonic-gate 	case R_SETCURSOR:
1157c478bd9Sstevel@tonic-gate 		if ((nrec = *(recno_t *)key->data) == 0)
1167c478bd9Sstevel@tonic-gate 			goto einval;
1177c478bd9Sstevel@tonic-gate 		break;
1187c478bd9Sstevel@tonic-gate 	case R_IAFTER:
1197c478bd9Sstevel@tonic-gate 		if ((nrec = *(recno_t *)key->data) == 0) {
1207c478bd9Sstevel@tonic-gate 			nrec = 1;
1217c478bd9Sstevel@tonic-gate 			flags = R_IBEFORE;
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 		break;
1247c478bd9Sstevel@tonic-gate 	case 0:
1257c478bd9Sstevel@tonic-gate 	case R_IBEFORE:
1267c478bd9Sstevel@tonic-gate 		if ((nrec = *(recno_t *)key->data) == 0)
1277c478bd9Sstevel@tonic-gate 			goto einval;
1287c478bd9Sstevel@tonic-gate 		break;
1297c478bd9Sstevel@tonic-gate 	case R_NOOVERWRITE:
1307c478bd9Sstevel@tonic-gate 		if ((nrec = *(recno_t *)key->data) == 0)
1317c478bd9Sstevel@tonic-gate 			goto einval;
1327c478bd9Sstevel@tonic-gate 		if (nrec <= t->bt_nrecs)
1337c478bd9Sstevel@tonic-gate 			return (RET_SPECIAL);
1347c478bd9Sstevel@tonic-gate 		break;
1357c478bd9Sstevel@tonic-gate 	default:
1367c478bd9Sstevel@tonic-gate einval:		errno = EINVAL;
1377c478bd9Sstevel@tonic-gate 		return (RET_ERROR);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * Make sure that records up to and including the put record are
1427c478bd9Sstevel@tonic-gate 	 * already in the database.  If skipping records, create empty ones.
1437c478bd9Sstevel@tonic-gate 	 */
1447c478bd9Sstevel@tonic-gate 	if (nrec > t->bt_nrecs) {
1457c478bd9Sstevel@tonic-gate 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
1467c478bd9Sstevel@tonic-gate 		    t->bt_irec(t, nrec) == RET_ERROR)
1477c478bd9Sstevel@tonic-gate 			return (RET_ERROR);
1487c478bd9Sstevel@tonic-gate 		if (nrec > t->bt_nrecs + 1) {
1497c478bd9Sstevel@tonic-gate 			if (F_ISSET(t, R_FIXLEN)) {
1507c478bd9Sstevel@tonic-gate 				if ((tdata.data =
1517c478bd9Sstevel@tonic-gate 				    (void *)malloc(t->bt_reclen)) == NULL)
1527c478bd9Sstevel@tonic-gate 					return (RET_ERROR);
1537c478bd9Sstevel@tonic-gate 				tdata.size = t->bt_reclen;
1547c478bd9Sstevel@tonic-gate 				memset(tdata.data, t->bt_bval, tdata.size);
1557c478bd9Sstevel@tonic-gate 			} else {
1567c478bd9Sstevel@tonic-gate 				tdata.data = NULL;
1577c478bd9Sstevel@tonic-gate 				tdata.size = 0;
1587c478bd9Sstevel@tonic-gate 			}
1597c478bd9Sstevel@tonic-gate 			while (nrec > t->bt_nrecs + 1)
1607c478bd9Sstevel@tonic-gate 				if (__rec_iput(t,
1617c478bd9Sstevel@tonic-gate 				    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
1627c478bd9Sstevel@tonic-gate 					return (RET_ERROR);
1637c478bd9Sstevel@tonic-gate 			if (F_ISSET(t, R_FIXLEN))
1647c478bd9Sstevel@tonic-gate 				free(tdata.data);
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
1697c478bd9Sstevel@tonic-gate 		return (status);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (flags == R_SETCURSOR)
1727c478bd9Sstevel@tonic-gate 		t->bt_cursor.rcursor = nrec;
173*1da57d55SToomas Soome 
1747c478bd9Sstevel@tonic-gate 	F_SET(t, R_MODIFIED);
1757c478bd9Sstevel@tonic-gate 	return (__rec_ret(t, NULL, nrec, key, NULL));
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * __REC_IPUT -- Add a recno item to the tree.
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  * Parameters:
1827c478bd9Sstevel@tonic-gate  *	t:	tree
1837c478bd9Sstevel@tonic-gate  *	nrec:	record number
1847c478bd9Sstevel@tonic-gate  *	data:	data
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * Returns:
1877c478bd9Sstevel@tonic-gate  *	RET_ERROR, RET_SUCCESS
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate int
__rec_iput(t,nrec,data,flags)1907c478bd9Sstevel@tonic-gate __rec_iput(t, nrec, data, flags)
1917c478bd9Sstevel@tonic-gate 	BTREE *t;
1927c478bd9Sstevel@tonic-gate 	recno_t nrec;
1937c478bd9Sstevel@tonic-gate 	const DBT *data;
1947c478bd9Sstevel@tonic-gate 	u_int flags;
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	DBT tdata;
1977c478bd9Sstevel@tonic-gate 	EPG *e;
1987c478bd9Sstevel@tonic-gate 	PAGE *h;
19956a424ccSmp 	indx_t idx, nxtindex;
2007c478bd9Sstevel@tonic-gate 	db_pgno_t pg;
2017c478bd9Sstevel@tonic-gate 	u_int32_t nbytes;
2027c478bd9Sstevel@tonic-gate 	int dflags, status;
2037c478bd9Sstevel@tonic-gate 	char *dest, db[NOVFLSIZE];
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * If the data won't fit on a page, store it on indirect pages.
2077c478bd9Sstevel@tonic-gate 	 *
2087c478bd9Sstevel@tonic-gate 	 * XXX
2097c478bd9Sstevel@tonic-gate 	 * If the insert fails later on, these pages aren't recovered.
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	if (data->size > t->bt_ovflsize) {
2127c478bd9Sstevel@tonic-gate 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
2137c478bd9Sstevel@tonic-gate 			return (RET_ERROR);
2147c478bd9Sstevel@tonic-gate 		tdata.data = db;
2157c478bd9Sstevel@tonic-gate 		tdata.size = NOVFLSIZE;
2167c478bd9Sstevel@tonic-gate 		*(db_pgno_t *)db = pg;
2177c478bd9Sstevel@tonic-gate 		*(u_int32_t *)(db + sizeof(db_pgno_t)) = data->size;
2187c478bd9Sstevel@tonic-gate 		dflags = P_BIGDATA;
2197c478bd9Sstevel@tonic-gate 		data = &tdata;
2207c478bd9Sstevel@tonic-gate 	} else
2217c478bd9Sstevel@tonic-gate 		dflags = 0;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* __rec_search pins the returned page. */
2247c478bd9Sstevel@tonic-gate 	if ((e = __rec_search(t, nrec,
2257c478bd9Sstevel@tonic-gate 	    nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
2267c478bd9Sstevel@tonic-gate 	    SINSERT : SEARCH)) == NULL)
2277c478bd9Sstevel@tonic-gate 		return (RET_ERROR);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	h = e->page;
23056a424ccSmp 	idx = e->index;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * Add the specified key/data pair to the tree.  The R_IAFTER and
2347c478bd9Sstevel@tonic-gate 	 * R_IBEFORE flags insert the key after/before the specified key.
2357c478bd9Sstevel@tonic-gate 	 *
2367c478bd9Sstevel@tonic-gate 	 * Pages are split as required.
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	switch (flags) {
2397c478bd9Sstevel@tonic-gate 	case R_IAFTER:
24056a424ccSmp 		++idx;
2417c478bd9Sstevel@tonic-gate 		break;
2427c478bd9Sstevel@tonic-gate 	case R_IBEFORE:
2437c478bd9Sstevel@tonic-gate 		break;
2447c478bd9Sstevel@tonic-gate 	default:
2457c478bd9Sstevel@tonic-gate 		if (nrec < t->bt_nrecs &&
24656a424ccSmp 		    __rec_dleaf(t, h, idx) == RET_ERROR) {
2477c478bd9Sstevel@tonic-gate 			mpool_put(t->bt_mp, h, 0);
2487c478bd9Sstevel@tonic-gate 			return (RET_ERROR);
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 		break;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * If not enough room, split the page.  The split code will insert
2557c478bd9Sstevel@tonic-gate 	 * the key and data and unpin the current page.  If inserting into
2567c478bd9Sstevel@tonic-gate 	 * the offset array, shift the pointers up.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	nbytes = NRLEAFDBT(data->size);
2597c478bd9Sstevel@tonic-gate 	if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
26056a424ccSmp 		status = __bt_split(t, h, NULL, data, dflags, nbytes, idx);
2617c478bd9Sstevel@tonic-gate 		if (status == RET_SUCCESS)
2627c478bd9Sstevel@tonic-gate 			++t->bt_nrecs;
2637c478bd9Sstevel@tonic-gate 		return (status);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
26656a424ccSmp 	if (idx < (nxtindex = NEXTINDEX(h)))
26756a424ccSmp 		memmove(h->linp + idx + 1, h->linp + idx,
26856a424ccSmp 		    (nxtindex - idx) * sizeof(indx_t));
2697c478bd9Sstevel@tonic-gate 	h->lower += sizeof(indx_t);
2707c478bd9Sstevel@tonic-gate 
27156a424ccSmp 	h->linp[idx] = h->upper -= nbytes;
2727c478bd9Sstevel@tonic-gate 	dest = (char *)h + h->upper;
2737c478bd9Sstevel@tonic-gate 	WR_RLEAF(dest, data, dflags);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	++t->bt_nrecs;
2767c478bd9Sstevel@tonic-gate 	F_SET(t, B_MODIFIED);
2777c478bd9Sstevel@tonic-gate 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	return (RET_SUCCESS);
2807c478bd9Sstevel@tonic-gate }
281