2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)rec_put.c 8.7 (Berkeley) 8/18/94";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
47 * __REC_PUT -- Add a recno item to the tree.
50 * dbp: pointer to access method
53 * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
56 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
57 * already in the tree and R_NOOVERWRITE specified.
60 __rec_put(dbp, key, data, flags)
73 /* Toss any page pinned across calls. */
74 if (t->bt_pinned != NULL) {
75 mpool_put(t->bt_mp, t->bt_pinned, 0);
80 * If using fixed-length records, and the record is long, return
81 * EINVAL. If it's short, pad it out. Use the record data return
82 * memory, it's only short-term.
84 if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
85 if (data->size > t->bt_reclen)
88 if (t->bt_rdata.size < t->bt_reclen) {
90 reallocf(t->bt_rdata.data, t->bt_reclen);
91 if (t->bt_rdata.data == NULL)
93 t->bt_rdata.size = t->bt_reclen;
95 memmove(t->bt_rdata.data, data->data, data->size);
96 memset((char *)t->bt_rdata.data + data->size,
97 t->bt_bval, t->bt_reclen - data->size);
98 fdata.data = t->bt_rdata.data;
99 fdata.size = t->bt_reclen;
101 fdata.data = data->data;
102 fdata.size = data->size;
107 if (!F_ISSET(&t->bt_cursor, CURS_INIT))
109 nrec = t->bt_cursor.rcursor;
112 if ((nrec = *(recno_t *)key->data) == 0)
116 if ((nrec = *(recno_t *)key->data) == 0) {
123 if ((nrec = *(recno_t *)key->data) == 0)
127 if ((nrec = *(recno_t *)key->data) == 0)
129 if (nrec <= t->bt_nrecs)
130 return (RET_SPECIAL);
133 einval: errno = EINVAL;
138 * Make sure that records up to and including the put record are
139 * already in the database. If skipping records, create empty ones.
141 if (nrec > t->bt_nrecs) {
142 if (!F_ISSET(t, R_EOF | R_INMEM) &&
143 t->bt_irec(t, nrec) == RET_ERROR)
145 if (nrec > t->bt_nrecs + 1) {
146 if (F_ISSET(t, R_FIXLEN)) {
148 (void *)malloc(t->bt_reclen)) == NULL)
150 tdata.size = t->bt_reclen;
151 memset(tdata.data, t->bt_bval, tdata.size);
156 while (nrec > t->bt_nrecs + 1)
158 t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
160 if (F_ISSET(t, R_FIXLEN))
165 if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
173 t->bt_cursor.rcursor = nrec;
177 F_SET(t, R_MODIFIED);
178 return (__rec_ret(t, NULL, nrec, key, NULL));
182 * __REC_IPUT -- Add a recno item to the tree.
186 * nrec: record number
190 * RET_ERROR, RET_SUCCESS
193 __rec_iput(t, nrec, data, flags)
202 indx_t index, nxtindex;
206 char *dest, db[NOVFLSIZE];
209 * If the data won't fit on a page, store it on indirect pages.
212 * If the insert fails later on, these pages aren't recovered.
214 if (data->size > t->bt_ovflsize) {
215 if (__ovfl_put(t, data, &pg) == RET_ERROR)
218 tdata.size = NOVFLSIZE;
220 *(u_int32_t *)(db + sizeof(pgno_t)) = data->size;
226 /* __rec_search pins the returned page. */
227 if ((e = __rec_search(t, nrec,
228 nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
229 SINSERT : SEARCH)) == NULL)
236 * Add the specified key/data pair to the tree. The R_IAFTER and
237 * R_IBEFORE flags insert the key after/before the specified key.
239 * Pages are split as required.
248 if (nrec < t->bt_nrecs &&
249 __rec_dleaf(t, h, index) == RET_ERROR) {
250 mpool_put(t->bt_mp, h, 0);
257 * If not enough room, split the page. The split code will insert
258 * the key and data and unpin the current page. If inserting into
259 * the offset array, shift the pointers up.
261 nbytes = NRLEAFDBT(data->size);
262 if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
263 status = __bt_split(t, h, NULL, data, dflags, nbytes, index);
264 if (status == RET_SUCCESS)
269 if (index < (nxtindex = NEXTINDEX(h)))
270 memmove(h->linp + index + 1, h->linp + index,
271 (nxtindex - index) * sizeof(indx_t));
272 h->lower += sizeof(indx_t);
274 h->linp[index] = h->upper -= nbytes;
275 dest = (char *)h + h->upper;
276 WR_RLEAF(dest, data, dflags);
279 F_SET(t, B_MODIFIED);
280 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
282 return (RET_SUCCESS);