xref: /freebsd/lib/libc/db/recno/rec_put.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4f1e396bcSPaul Traina  * Copyright (c) 1990, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
3258f0484fSRodney W. Grimes #include <sys/types.h>
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #include <errno.h>
3558f0484fSRodney W. Grimes #include <stdio.h>
3658f0484fSRodney W. Grimes #include <stdlib.h>
3758f0484fSRodney W. Grimes #include <string.h>
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #include <db.h>
4058f0484fSRodney W. Grimes #include "recno.h"
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes /*
4358f0484fSRodney W. Grimes  * __REC_PUT -- Add a recno item to the tree.
4458f0484fSRodney W. Grimes  *
4558f0484fSRodney W. Grimes  * Parameters:
4658f0484fSRodney W. Grimes  *	dbp:	pointer to access method
4758f0484fSRodney W. Grimes  *	key:	key
4858f0484fSRodney W. Grimes  *	data:	data
4958f0484fSRodney W. Grimes  *	flag:	R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
5058f0484fSRodney W. Grimes  *
5158f0484fSRodney W. Grimes  * Returns:
5258f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
5358f0484fSRodney W. Grimes  *	already in the tree and R_NOOVERWRITE specified.
5458f0484fSRodney W. Grimes  */
5558f0484fSRodney W. Grimes int
__rec_put(const DB * dbp,DBT * key,const DBT * data,u_int flags)560ac22237SXin LI __rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
5758f0484fSRodney W. Grimes {
5858f0484fSRodney W. Grimes 	BTREE *t;
59f1e396bcSPaul Traina 	DBT fdata, tdata;
6058f0484fSRodney W. Grimes 	recno_t nrec;
6158f0484fSRodney W. Grimes 	int status;
6258f0484fSRodney W. Grimes 
6358f0484fSRodney W. Grimes 	t = dbp->internal;
6458f0484fSRodney W. Grimes 
6558f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
6658f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
6758f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
6858f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
6958f0484fSRodney W. Grimes 	}
7058f0484fSRodney W. Grimes 
71f1e396bcSPaul Traina 	/*
72f1e396bcSPaul Traina 	 * If using fixed-length records, and the record is long, return
73f1e396bcSPaul Traina 	 * EINVAL.  If it's short, pad it out.  Use the record data return
74f1e396bcSPaul Traina 	 * memory, it's only short-term.
75f1e396bcSPaul Traina 	 */
76f1e396bcSPaul Traina 	if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
77f1e396bcSPaul Traina 		if (data->size > t->bt_reclen)
78f1e396bcSPaul Traina 			goto einval;
79f1e396bcSPaul Traina 
80f1e396bcSPaul Traina 		if (t->bt_rdata.size < t->bt_reclen) {
81e8420087SWarner Losh 			t->bt_rdata.data =
82e8420087SWarner Losh 			    reallocf(t->bt_rdata.data, t->bt_reclen);
83f1e396bcSPaul Traina 			if (t->bt_rdata.data == NULL)
84f1e396bcSPaul Traina 				return (RET_ERROR);
85f1e396bcSPaul Traina 			t->bt_rdata.size = t->bt_reclen;
86f1e396bcSPaul Traina 		}
87f1e396bcSPaul Traina 		memmove(t->bt_rdata.data, data->data, data->size);
88f1e396bcSPaul Traina 		memset((char *)t->bt_rdata.data + data->size,
89f1e396bcSPaul Traina 		    t->bt_bval, t->bt_reclen - data->size);
90f1e396bcSPaul Traina 		fdata.data = t->bt_rdata.data;
91f1e396bcSPaul Traina 		fdata.size = t->bt_reclen;
92f1e396bcSPaul Traina 	} else {
93f1e396bcSPaul Traina 		fdata.data = data->data;
94f1e396bcSPaul Traina 		fdata.size = data->size;
95f1e396bcSPaul Traina 	}
96f1e396bcSPaul Traina 
9758f0484fSRodney W. Grimes 	switch (flags) {
9858f0484fSRodney W. Grimes 	case R_CURSOR:
99f1e396bcSPaul Traina 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
10058f0484fSRodney W. Grimes 			goto einval;
101f1e396bcSPaul Traina 		nrec = t->bt_cursor.rcursor;
10258f0484fSRodney W. Grimes 		break;
10358f0484fSRodney W. Grimes 	case R_SETCURSOR:
10458f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0)
10558f0484fSRodney W. Grimes 			goto einval;
10658f0484fSRodney W. Grimes 		break;
10758f0484fSRodney W. Grimes 	case R_IAFTER:
10858f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0) {
10958f0484fSRodney W. Grimes 			nrec = 1;
11058f0484fSRodney W. Grimes 			flags = R_IBEFORE;
11158f0484fSRodney W. Grimes 		}
11258f0484fSRodney W. Grimes 		break;
11358f0484fSRodney W. Grimes 	case 0:
11458f0484fSRodney W. Grimes 	case R_IBEFORE:
11558f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0)
11658f0484fSRodney W. Grimes 			goto einval;
11758f0484fSRodney W. Grimes 		break;
11858f0484fSRodney W. Grimes 	case R_NOOVERWRITE:
11958f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0)
12058f0484fSRodney W. Grimes 			goto einval;
12158f0484fSRodney W. Grimes 		if (nrec <= t->bt_nrecs)
12258f0484fSRodney W. Grimes 			return (RET_SPECIAL);
12358f0484fSRodney W. Grimes 		break;
12458f0484fSRodney W. Grimes 	default:
12558f0484fSRodney W. Grimes einval:		errno = EINVAL;
12658f0484fSRodney W. Grimes 		return (RET_ERROR);
12758f0484fSRodney W. Grimes 	}
12858f0484fSRodney W. Grimes 
12958f0484fSRodney W. Grimes 	/*
13058f0484fSRodney W. Grimes 	 * Make sure that records up to and including the put record are
13158f0484fSRodney W. Grimes 	 * already in the database.  If skipping records, create empty ones.
13258f0484fSRodney W. Grimes 	 */
13358f0484fSRodney W. Grimes 	if (nrec > t->bt_nrecs) {
134f1e396bcSPaul Traina 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
13558f0484fSRodney W. Grimes 		    t->bt_irec(t, nrec) == RET_ERROR)
13658f0484fSRodney W. Grimes 			return (RET_ERROR);
13758f0484fSRodney W. Grimes 		if (nrec > t->bt_nrecs + 1) {
138f1e396bcSPaul Traina 			if (F_ISSET(t, R_FIXLEN)) {
139e1ea7827SPedro F. Giffuni 				if ((tdata.data = malloc(t->bt_reclen)) == NULL)
14058f0484fSRodney W. Grimes 					return (RET_ERROR);
14158f0484fSRodney W. Grimes 				tdata.size = t->bt_reclen;
14258f0484fSRodney W. Grimes 				memset(tdata.data, t->bt_bval, tdata.size);
14358f0484fSRodney W. Grimes 			} else {
14458f0484fSRodney W. Grimes 				tdata.data = NULL;
14558f0484fSRodney W. Grimes 				tdata.size = 0;
14658f0484fSRodney W. Grimes 			}
14758f0484fSRodney W. Grimes 			while (nrec > t->bt_nrecs + 1)
14858f0484fSRodney W. Grimes 				if (__rec_iput(t,
14958f0484fSRodney W. Grimes 				    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
15058f0484fSRodney W. Grimes 					return (RET_ERROR);
151f1e396bcSPaul Traina 			if (F_ISSET(t, R_FIXLEN))
15258f0484fSRodney W. Grimes 				free(tdata.data);
15358f0484fSRodney W. Grimes 		}
15458f0484fSRodney W. Grimes 	}
15558f0484fSRodney W. Grimes 
156f1e396bcSPaul Traina 	if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
15758f0484fSRodney W. Grimes 		return (status);
15858f0484fSRodney W. Grimes 
1594f0682b0SBrian Feldman 	switch (flags) {
1604f0682b0SBrian Feldman 	case R_IAFTER:
1614f0682b0SBrian Feldman 		nrec++;
1624f0682b0SBrian Feldman 		break;
1634f0682b0SBrian Feldman 	case R_SETCURSOR:
164f1e396bcSPaul Traina 		t->bt_cursor.rcursor = nrec;
1654f0682b0SBrian Feldman 		break;
1664f0682b0SBrian Feldman 	}
16758f0484fSRodney W. Grimes 
168f1e396bcSPaul Traina 	F_SET(t, R_MODIFIED);
16958f0484fSRodney W. Grimes 	return (__rec_ret(t, NULL, nrec, key, NULL));
17058f0484fSRodney W. Grimes }
17158f0484fSRodney W. Grimes 
17258f0484fSRodney W. Grimes /*
17358f0484fSRodney W. Grimes  * __REC_IPUT -- Add a recno item to the tree.
17458f0484fSRodney W. Grimes  *
17558f0484fSRodney W. Grimes  * Parameters:
17658f0484fSRodney W. Grimes  *	t:	tree
17758f0484fSRodney W. Grimes  *	nrec:	record number
17858f0484fSRodney W. Grimes  *	data:	data
17958f0484fSRodney W. Grimes  *
18058f0484fSRodney W. Grimes  * Returns:
18158f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
18258f0484fSRodney W. Grimes  */
18358f0484fSRodney W. Grimes int
__rec_iput(BTREE * t,recno_t nrec,const DBT * data,u_int flags)1840ac22237SXin LI __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
18558f0484fSRodney W. Grimes {
18658f0484fSRodney W. Grimes 	DBT tdata;
18758f0484fSRodney W. Grimes 	EPG *e;
18858f0484fSRodney W. Grimes 	PAGE *h;
1894c66e4b6SXin LI 	indx_t idx, nxtindex;
19058f0484fSRodney W. Grimes 	pgno_t pg;
191f1e396bcSPaul Traina 	u_int32_t nbytes;
19258f0484fSRodney W. Grimes 	int dflags, status;
19358f0484fSRodney W. Grimes 	char *dest, db[NOVFLSIZE];
19458f0484fSRodney W. Grimes 
19558f0484fSRodney W. Grimes 	/*
19658f0484fSRodney W. Grimes 	 * If the data won't fit on a page, store it on indirect pages.
19758f0484fSRodney W. Grimes 	 *
19858f0484fSRodney W. Grimes 	 * XXX
19958f0484fSRodney W. Grimes 	 * If the insert fails later on, these pages aren't recovered.
20058f0484fSRodney W. Grimes 	 */
20158f0484fSRodney W. Grimes 	if (data->size > t->bt_ovflsize) {
20258f0484fSRodney W. Grimes 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
20358f0484fSRodney W. Grimes 			return (RET_ERROR);
20458f0484fSRodney W. Grimes 		tdata.data = db;
20558f0484fSRodney W. Grimes 		tdata.size = NOVFLSIZE;
206e1ea7827SPedro F. Giffuni 		memcpy(db, &pg, sizeof(pg));
207f1e396bcSPaul Traina 		*(u_int32_t *)(db + sizeof(pgno_t)) = data->size;
20858f0484fSRodney W. Grimes 		dflags = P_BIGDATA;
20958f0484fSRodney W. Grimes 		data = &tdata;
21058f0484fSRodney W. Grimes 	} else
21158f0484fSRodney W. Grimes 		dflags = 0;
21258f0484fSRodney W. Grimes 
21358f0484fSRodney W. Grimes 	/* __rec_search pins the returned page. */
21458f0484fSRodney W. Grimes 	if ((e = __rec_search(t, nrec,
21558f0484fSRodney W. Grimes 	    nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
21658f0484fSRodney W. Grimes 	    SINSERT : SEARCH)) == NULL)
21758f0484fSRodney W. Grimes 		return (RET_ERROR);
21858f0484fSRodney W. Grimes 
21958f0484fSRodney W. Grimes 	h = e->page;
2204c66e4b6SXin LI 	idx = e->index;
22158f0484fSRodney W. Grimes 
22258f0484fSRodney W. Grimes 	/*
22358f0484fSRodney W. Grimes 	 * Add the specified key/data pair to the tree.  The R_IAFTER and
22458f0484fSRodney W. Grimes 	 * R_IBEFORE flags insert the key after/before the specified key.
22558f0484fSRodney W. Grimes 	 *
22658f0484fSRodney W. Grimes 	 * Pages are split as required.
22758f0484fSRodney W. Grimes 	 */
22858f0484fSRodney W. Grimes 	switch (flags) {
22958f0484fSRodney W. Grimes 	case R_IAFTER:
2304c66e4b6SXin LI 		++idx;
23158f0484fSRodney W. Grimes 		break;
23258f0484fSRodney W. Grimes 	case R_IBEFORE:
23358f0484fSRodney W. Grimes 		break;
23458f0484fSRodney W. Grimes 	default:
23558f0484fSRodney W. Grimes 		if (nrec < t->bt_nrecs &&
2364c66e4b6SXin LI 		    __rec_dleaf(t, h, idx) == RET_ERROR) {
23758f0484fSRodney W. Grimes 			mpool_put(t->bt_mp, h, 0);
23858f0484fSRodney W. Grimes 			return (RET_ERROR);
23958f0484fSRodney W. Grimes 		}
24058f0484fSRodney W. Grimes 		break;
24158f0484fSRodney W. Grimes 	}
24258f0484fSRodney W. Grimes 
24358f0484fSRodney W. Grimes 	/*
24458f0484fSRodney W. Grimes 	 * If not enough room, split the page.  The split code will insert
24558f0484fSRodney W. Grimes 	 * the key and data and unpin the current page.  If inserting into
24658f0484fSRodney W. Grimes 	 * the offset array, shift the pointers up.
24758f0484fSRodney W. Grimes 	 */
24858f0484fSRodney W. Grimes 	nbytes = NRLEAFDBT(data->size);
249f60486b3SXin LI 	if ((u_int32_t)(h->upper - h->lower) < nbytes + sizeof(indx_t)) {
2504c66e4b6SXin LI 		status = __bt_split(t, h, NULL, data, dflags, nbytes, idx);
25158f0484fSRodney W. Grimes 		if (status == RET_SUCCESS)
25258f0484fSRodney W. Grimes 			++t->bt_nrecs;
25358f0484fSRodney W. Grimes 		return (status);
25458f0484fSRodney W. Grimes 	}
25558f0484fSRodney W. Grimes 
2564c66e4b6SXin LI 	if (idx < (nxtindex = NEXTINDEX(h)))
2574c66e4b6SXin LI 		memmove(h->linp + idx + 1, h->linp + idx,
2584c66e4b6SXin LI 		    (nxtindex - idx) * sizeof(indx_t));
25958f0484fSRodney W. Grimes 	h->lower += sizeof(indx_t);
26058f0484fSRodney W. Grimes 
2614c66e4b6SXin LI 	h->linp[idx] = h->upper -= nbytes;
26258f0484fSRodney W. Grimes 	dest = (char *)h + h->upper;
26358f0484fSRodney W. Grimes 	WR_RLEAF(dest, data, dflags);
26458f0484fSRodney W. Grimes 
26558f0484fSRodney W. Grimes 	++t->bt_nrecs;
266f1e396bcSPaul Traina 	F_SET(t, B_MODIFIED);
26758f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
26858f0484fSRodney W. Grimes 
26958f0484fSRodney W. Grimes 	return (RET_SUCCESS);
27058f0484fSRodney W. Grimes }
271