2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
49 * __REC_PUT -- Add a recno item to the tree.
52 * dbp: pointer to access method
55 * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
58 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
59 * already in the tree and R_NOOVERWRITE specified.
62 __rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
71 /* Toss any page pinned across calls. */
72 if (t->bt_pinned != NULL) {
73 mpool_put(t->bt_mp, t->bt_pinned, 0);
78 * If using fixed-length records, and the record is long, return
79 * EINVAL. If it's short, pad it out. Use the record data return
80 * memory, it's only short-term.
82 if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
83 if (data->size > t->bt_reclen)
86 if (t->bt_rdata.size < t->bt_reclen) {
88 reallocf(t->bt_rdata.data, t->bt_reclen);
89 if (t->bt_rdata.data == NULL)
91 t->bt_rdata.size = t->bt_reclen;
93 memmove(t->bt_rdata.data, data->data, data->size);
94 memset((char *)t->bt_rdata.data + data->size,
95 t->bt_bval, t->bt_reclen - data->size);
96 fdata.data = t->bt_rdata.data;
97 fdata.size = t->bt_reclen;
99 fdata.data = data->data;
100 fdata.size = data->size;
105 if (!F_ISSET(&t->bt_cursor, CURS_INIT))
107 nrec = t->bt_cursor.rcursor;
110 if ((nrec = *(recno_t *)key->data) == 0)
114 if ((nrec = *(recno_t *)key->data) == 0) {
121 if ((nrec = *(recno_t *)key->data) == 0)
125 if ((nrec = *(recno_t *)key->data) == 0)
127 if (nrec <= t->bt_nrecs)
128 return (RET_SPECIAL);
131 einval: errno = EINVAL;
136 * Make sure that records up to and including the put record are
137 * already in the database. If skipping records, create empty ones.
139 if (nrec > t->bt_nrecs) {
140 if (!F_ISSET(t, R_EOF | R_INMEM) &&
141 t->bt_irec(t, nrec) == RET_ERROR)
143 if (nrec > t->bt_nrecs + 1) {
144 if (F_ISSET(t, R_FIXLEN)) {
145 if ((tdata.data = malloc(t->bt_reclen)) == NULL)
147 tdata.size = t->bt_reclen;
148 memset(tdata.data, t->bt_bval, tdata.size);
153 while (nrec > t->bt_nrecs + 1)
155 t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
157 if (F_ISSET(t, R_FIXLEN))
162 if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
170 t->bt_cursor.rcursor = nrec;
174 F_SET(t, R_MODIFIED);
175 return (__rec_ret(t, NULL, nrec, key, NULL));
179 * __REC_IPUT -- Add a recno item to the tree.
183 * nrec: record number
187 * RET_ERROR, RET_SUCCESS
190 __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
195 indx_t idx, nxtindex;
199 char *dest, db[NOVFLSIZE];
202 * If the data won't fit on a page, store it on indirect pages.
205 * If the insert fails later on, these pages aren't recovered.
207 if (data->size > t->bt_ovflsize) {
208 if (__ovfl_put(t, data, &pg) == RET_ERROR)
211 tdata.size = NOVFLSIZE;
212 memcpy(db, &pg, sizeof(pg));
213 *(u_int32_t *)(db + sizeof(pgno_t)) = data->size;
219 /* __rec_search pins the returned page. */
220 if ((e = __rec_search(t, nrec,
221 nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
222 SINSERT : SEARCH)) == NULL)
229 * Add the specified key/data pair to the tree. The R_IAFTER and
230 * R_IBEFORE flags insert the key after/before the specified key.
232 * Pages are split as required.
241 if (nrec < t->bt_nrecs &&
242 __rec_dleaf(t, h, idx) == RET_ERROR) {
243 mpool_put(t->bt_mp, h, 0);
250 * If not enough room, split the page. The split code will insert
251 * the key and data and unpin the current page. If inserting into
252 * the offset array, shift the pointers up.
254 nbytes = NRLEAFDBT(data->size);
255 if ((u_int32_t)(h->upper - h->lower) < nbytes + sizeof(indx_t)) {
256 status = __bt_split(t, h, NULL, data, dflags, nbytes, idx);
257 if (status == RET_SUCCESS)
262 if (idx < (nxtindex = NEXTINDEX(h)))
263 memmove(h->linp + idx + 1, h->linp + idx,
264 (nxtindex - idx) * sizeof(indx_t));
265 h->lower += sizeof(indx_t);
267 h->linp[idx] = h->upper -= nbytes;
268 dest = (char *)h + h->upper;
269 WR_RLEAF(dest, data, dflags);
272 F_SET(t, B_MODIFIED);
273 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
275 return (RET_SUCCESS);