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