xref: /freebsd/lib/libc/db/recno/rec_put.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
158f0484fSRodney W. Grimes /*-
2f1e396bcSPaul Traina  * Copyright (c) 1990, 1993, 1994
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1458f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1558f0484fSRodney W. Grimes  *    without specific prior written permission.
1658f0484fSRodney W. Grimes  *
1758f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1858f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1958f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2058f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2158f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2258f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2358f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2458f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2558f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2658f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2758f0484fSRodney W. Grimes  * SUCH DAMAGE.
2858f0484fSRodney W. Grimes  */
2958f0484fSRodney W. Grimes 
3058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
31f1e396bcSPaul Traina static char sccsid[] = "@(#)rec_put.c	8.7 (Berkeley) 8/18/94";
3258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
33333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
34333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #include <sys/types.h>
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes #include <errno.h>
3958f0484fSRodney W. Grimes #include <stdio.h>
4058f0484fSRodney W. Grimes #include <stdlib.h>
4158f0484fSRodney W. Grimes #include <string.h>
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <db.h>
4458f0484fSRodney W. Grimes #include "recno.h"
4558f0484fSRodney W. Grimes 
4658f0484fSRodney W. Grimes /*
4758f0484fSRodney W. Grimes  * __REC_PUT -- Add a recno item to the tree.
4858f0484fSRodney W. Grimes  *
4958f0484fSRodney W. Grimes  * Parameters:
5058f0484fSRodney W. Grimes  *	dbp:	pointer to access method
5158f0484fSRodney W. Grimes  *	key:	key
5258f0484fSRodney W. Grimes  *	data:	data
5358f0484fSRodney W. Grimes  *	flag:	R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
5458f0484fSRodney W. Grimes  *
5558f0484fSRodney W. Grimes  * Returns:
5658f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
5758f0484fSRodney W. Grimes  *	already in the tree and R_NOOVERWRITE specified.
5858f0484fSRodney W. Grimes  */
5958f0484fSRodney W. Grimes int
600ac22237SXin LI __rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
6158f0484fSRodney W. Grimes {
6258f0484fSRodney W. Grimes 	BTREE *t;
63f1e396bcSPaul Traina 	DBT fdata, tdata;
6458f0484fSRodney W. Grimes 	recno_t nrec;
6558f0484fSRodney W. Grimes 	int status;
6658f0484fSRodney W. Grimes 
6758f0484fSRodney W. Grimes 	t = dbp->internal;
6858f0484fSRodney W. Grimes 
6958f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
7058f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
7158f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
7258f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
7358f0484fSRodney W. Grimes 	}
7458f0484fSRodney W. Grimes 
75f1e396bcSPaul Traina 	/*
76f1e396bcSPaul Traina 	 * If using fixed-length records, and the record is long, return
77f1e396bcSPaul Traina 	 * EINVAL.  If it's short, pad it out.  Use the record data return
78f1e396bcSPaul Traina 	 * memory, it's only short-term.
79f1e396bcSPaul Traina 	 */
80f1e396bcSPaul Traina 	if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
81f1e396bcSPaul Traina 		if (data->size > t->bt_reclen)
82f1e396bcSPaul Traina 			goto einval;
83f1e396bcSPaul Traina 
84f1e396bcSPaul Traina 		if (t->bt_rdata.size < t->bt_reclen) {
85e8420087SWarner Losh 			t->bt_rdata.data =
86e8420087SWarner Losh 			    reallocf(t->bt_rdata.data, t->bt_reclen);
87f1e396bcSPaul Traina 			if (t->bt_rdata.data == NULL)
88f1e396bcSPaul Traina 				return (RET_ERROR);
89f1e396bcSPaul Traina 			t->bt_rdata.size = t->bt_reclen;
90f1e396bcSPaul Traina 		}
91f1e396bcSPaul Traina 		memmove(t->bt_rdata.data, data->data, data->size);
92f1e396bcSPaul Traina 		memset((char *)t->bt_rdata.data + data->size,
93f1e396bcSPaul Traina 		    t->bt_bval, t->bt_reclen - data->size);
94f1e396bcSPaul Traina 		fdata.data = t->bt_rdata.data;
95f1e396bcSPaul Traina 		fdata.size = t->bt_reclen;
96f1e396bcSPaul Traina 	} else {
97f1e396bcSPaul Traina 		fdata.data = data->data;
98f1e396bcSPaul Traina 		fdata.size = data->size;
99f1e396bcSPaul Traina 	}
100f1e396bcSPaul Traina 
10158f0484fSRodney W. Grimes 	switch (flags) {
10258f0484fSRodney W. Grimes 	case R_CURSOR:
103f1e396bcSPaul Traina 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
10458f0484fSRodney W. Grimes 			goto einval;
105f1e396bcSPaul Traina 		nrec = t->bt_cursor.rcursor;
10658f0484fSRodney W. Grimes 		break;
10758f0484fSRodney W. Grimes 	case R_SETCURSOR:
10858f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0)
10958f0484fSRodney W. Grimes 			goto einval;
11058f0484fSRodney W. Grimes 		break;
11158f0484fSRodney W. Grimes 	case R_IAFTER:
11258f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0) {
11358f0484fSRodney W. Grimes 			nrec = 1;
11458f0484fSRodney W. Grimes 			flags = R_IBEFORE;
11558f0484fSRodney W. Grimes 		}
11658f0484fSRodney W. Grimes 		break;
11758f0484fSRodney W. Grimes 	case 0:
11858f0484fSRodney W. Grimes 	case R_IBEFORE:
11958f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0)
12058f0484fSRodney W. Grimes 			goto einval;
12158f0484fSRodney W. Grimes 		break;
12258f0484fSRodney W. Grimes 	case R_NOOVERWRITE:
12358f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0)
12458f0484fSRodney W. Grimes 			goto einval;
12558f0484fSRodney W. Grimes 		if (nrec <= t->bt_nrecs)
12658f0484fSRodney W. Grimes 			return (RET_SPECIAL);
12758f0484fSRodney W. Grimes 		break;
12858f0484fSRodney W. Grimes 	default:
12958f0484fSRodney W. Grimes einval:		errno = EINVAL;
13058f0484fSRodney W. Grimes 		return (RET_ERROR);
13158f0484fSRodney W. Grimes 	}
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes 	/*
13458f0484fSRodney W. Grimes 	 * Make sure that records up to and including the put record are
13558f0484fSRodney W. Grimes 	 * already in the database.  If skipping records, create empty ones.
13658f0484fSRodney W. Grimes 	 */
13758f0484fSRodney W. Grimes 	if (nrec > t->bt_nrecs) {
138f1e396bcSPaul Traina 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
13958f0484fSRodney W. Grimes 		    t->bt_irec(t, nrec) == RET_ERROR)
14058f0484fSRodney W. Grimes 			return (RET_ERROR);
14158f0484fSRodney W. Grimes 		if (nrec > t->bt_nrecs + 1) {
142f1e396bcSPaul Traina 			if (F_ISSET(t, R_FIXLEN)) {
143e1ea7827SPedro F. Giffuni 				if ((tdata.data = malloc(t->bt_reclen)) == NULL)
14458f0484fSRodney W. Grimes 					return (RET_ERROR);
14558f0484fSRodney W. Grimes 				tdata.size = t->bt_reclen;
14658f0484fSRodney W. Grimes 				memset(tdata.data, t->bt_bval, tdata.size);
14758f0484fSRodney W. Grimes 			} else {
14858f0484fSRodney W. Grimes 				tdata.data = NULL;
14958f0484fSRodney W. Grimes 				tdata.size = 0;
15058f0484fSRodney W. Grimes 			}
15158f0484fSRodney W. Grimes 			while (nrec > t->bt_nrecs + 1)
15258f0484fSRodney W. Grimes 				if (__rec_iput(t,
15358f0484fSRodney W. Grimes 				    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
15458f0484fSRodney W. Grimes 					return (RET_ERROR);
155f1e396bcSPaul Traina 			if (F_ISSET(t, R_FIXLEN))
15658f0484fSRodney W. Grimes 				free(tdata.data);
15758f0484fSRodney W. Grimes 		}
15858f0484fSRodney W. Grimes 	}
15958f0484fSRodney W. Grimes 
160f1e396bcSPaul Traina 	if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
16158f0484fSRodney W. Grimes 		return (status);
16258f0484fSRodney W. Grimes 
1634f0682b0SBrian Feldman 	switch (flags) {
1644f0682b0SBrian Feldman 	case R_IAFTER:
1654f0682b0SBrian Feldman 		nrec++;
1664f0682b0SBrian Feldman 		break;
1674f0682b0SBrian Feldman 	case R_SETCURSOR:
168f1e396bcSPaul Traina 		t->bt_cursor.rcursor = nrec;
1694f0682b0SBrian Feldman 		break;
1704f0682b0SBrian Feldman 	}
17158f0484fSRodney W. Grimes 
172f1e396bcSPaul Traina 	F_SET(t, R_MODIFIED);
17358f0484fSRodney W. Grimes 	return (__rec_ret(t, NULL, nrec, key, NULL));
17458f0484fSRodney W. Grimes }
17558f0484fSRodney W. Grimes 
17658f0484fSRodney W. Grimes /*
17758f0484fSRodney W. Grimes  * __REC_IPUT -- Add a recno item to the tree.
17858f0484fSRodney W. Grimes  *
17958f0484fSRodney W. Grimes  * Parameters:
18058f0484fSRodney W. Grimes  *	t:	tree
18158f0484fSRodney W. Grimes  *	nrec:	record number
18258f0484fSRodney W. Grimes  *	data:	data
18358f0484fSRodney W. Grimes  *
18458f0484fSRodney W. Grimes  * Returns:
18558f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
18658f0484fSRodney W. Grimes  */
18758f0484fSRodney W. Grimes int
1880ac22237SXin LI __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
18958f0484fSRodney W. Grimes {
19058f0484fSRodney W. Grimes 	DBT tdata;
19158f0484fSRodney W. Grimes 	EPG *e;
19258f0484fSRodney W. Grimes 	PAGE *h;
1934c66e4b6SXin LI 	indx_t idx, nxtindex;
19458f0484fSRodney W. Grimes 	pgno_t pg;
195f1e396bcSPaul Traina 	u_int32_t nbytes;
19658f0484fSRodney W. Grimes 	int dflags, status;
19758f0484fSRodney W. Grimes 	char *dest, db[NOVFLSIZE];
19858f0484fSRodney W. Grimes 
19958f0484fSRodney W. Grimes 	/*
20058f0484fSRodney W. Grimes 	 * If the data won't fit on a page, store it on indirect pages.
20158f0484fSRodney W. Grimes 	 *
20258f0484fSRodney W. Grimes 	 * XXX
20358f0484fSRodney W. Grimes 	 * If the insert fails later on, these pages aren't recovered.
20458f0484fSRodney W. Grimes 	 */
20558f0484fSRodney W. Grimes 	if (data->size > t->bt_ovflsize) {
20658f0484fSRodney W. Grimes 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
20758f0484fSRodney W. Grimes 			return (RET_ERROR);
20858f0484fSRodney W. Grimes 		tdata.data = db;
20958f0484fSRodney W. Grimes 		tdata.size = NOVFLSIZE;
210e1ea7827SPedro F. Giffuni 		memcpy(db, &pg, sizeof(pg));
211f1e396bcSPaul Traina 		*(u_int32_t *)(db + sizeof(pgno_t)) = data->size;
21258f0484fSRodney W. Grimes 		dflags = P_BIGDATA;
21358f0484fSRodney W. Grimes 		data = &tdata;
21458f0484fSRodney W. Grimes 	} else
21558f0484fSRodney W. Grimes 		dflags = 0;
21658f0484fSRodney W. Grimes 
21758f0484fSRodney W. Grimes 	/* __rec_search pins the returned page. */
21858f0484fSRodney W. Grimes 	if ((e = __rec_search(t, nrec,
21958f0484fSRodney W. Grimes 	    nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
22058f0484fSRodney W. Grimes 	    SINSERT : SEARCH)) == NULL)
22158f0484fSRodney W. Grimes 		return (RET_ERROR);
22258f0484fSRodney W. Grimes 
22358f0484fSRodney W. Grimes 	h = e->page;
2244c66e4b6SXin LI 	idx = e->index;
22558f0484fSRodney W. Grimes 
22658f0484fSRodney W. Grimes 	/*
22758f0484fSRodney W. Grimes 	 * Add the specified key/data pair to the tree.  The R_IAFTER and
22858f0484fSRodney W. Grimes 	 * R_IBEFORE flags insert the key after/before the specified key.
22958f0484fSRodney W. Grimes 	 *
23058f0484fSRodney W. Grimes 	 * Pages are split as required.
23158f0484fSRodney W. Grimes 	 */
23258f0484fSRodney W. Grimes 	switch (flags) {
23358f0484fSRodney W. Grimes 	case R_IAFTER:
2344c66e4b6SXin LI 		++idx;
23558f0484fSRodney W. Grimes 		break;
23658f0484fSRodney W. Grimes 	case R_IBEFORE:
23758f0484fSRodney W. Grimes 		break;
23858f0484fSRodney W. Grimes 	default:
23958f0484fSRodney W. Grimes 		if (nrec < t->bt_nrecs &&
2404c66e4b6SXin LI 		    __rec_dleaf(t, h, idx) == RET_ERROR) {
24158f0484fSRodney W. Grimes 			mpool_put(t->bt_mp, h, 0);
24258f0484fSRodney W. Grimes 			return (RET_ERROR);
24358f0484fSRodney W. Grimes 		}
24458f0484fSRodney W. Grimes 		break;
24558f0484fSRodney W. Grimes 	}
24658f0484fSRodney W. Grimes 
24758f0484fSRodney W. Grimes 	/*
24858f0484fSRodney W. Grimes 	 * If not enough room, split the page.  The split code will insert
24958f0484fSRodney W. Grimes 	 * the key and data and unpin the current page.  If inserting into
25058f0484fSRodney W. Grimes 	 * the offset array, shift the pointers up.
25158f0484fSRodney W. Grimes 	 */
25258f0484fSRodney W. Grimes 	nbytes = NRLEAFDBT(data->size);
253f60486b3SXin LI 	if ((u_int32_t)(h->upper - h->lower) < nbytes + sizeof(indx_t)) {
2544c66e4b6SXin LI 		status = __bt_split(t, h, NULL, data, dflags, nbytes, idx);
25558f0484fSRodney W. Grimes 		if (status == RET_SUCCESS)
25658f0484fSRodney W. Grimes 			++t->bt_nrecs;
25758f0484fSRodney W. Grimes 		return (status);
25858f0484fSRodney W. Grimes 	}
25958f0484fSRodney W. Grimes 
2604c66e4b6SXin LI 	if (idx < (nxtindex = NEXTINDEX(h)))
2614c66e4b6SXin LI 		memmove(h->linp + idx + 1, h->linp + idx,
2624c66e4b6SXin LI 		    (nxtindex - idx) * sizeof(indx_t));
26358f0484fSRodney W. Grimes 	h->lower += sizeof(indx_t);
26458f0484fSRodney W. Grimes 
2654c66e4b6SXin LI 	h->linp[idx] = h->upper -= nbytes;
26658f0484fSRodney W. Grimes 	dest = (char *)h + h->upper;
26758f0484fSRodney W. Grimes 	WR_RLEAF(dest, data, dflags);
26858f0484fSRodney W. Grimes 
26958f0484fSRodney W. Grimes 	++t->bt_nrecs;
270f1e396bcSPaul Traina 	F_SET(t, B_MODIFIED);
27158f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
27258f0484fSRodney W. Grimes 
27358f0484fSRodney W. Grimes 	return (RET_SUCCESS);
27458f0484fSRodney W. Grimes }
275