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 * 3. 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
29 * @(#)rec_put.c 8.7 (Berkeley) 8/18/94
30 * $FreeBSD: src/lib/libc/db/recno/rec_put.c,v 1.4.6.1 2001/01/02 05:13:25 peter Exp $
31 * $DragonFly: src/lib/libc/db/recno/rec_put.c,v 1.5 2005/11/19 20:46:32 swildner Exp $
34 #include <sys/types.h>
45 * __REC_PUT -- Add a recno item to the tree.
48 * dbp: pointer to access method
51 * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
54 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
55 * already in the tree and R_NOOVERWRITE specified.
58 __rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
67 /* Toss any page pinned across calls. */
68 if (t->bt_pinned != NULL) {
69 mpool_put(t->bt_mp, t->bt_pinned, 0);
74 * If using fixed-length records, and the record is long, return
75 * EINVAL. If it's short, pad it out. Use the record data return
76 * memory, it's only short-term.
78 if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
79 if (data->size > t->bt_reclen)
82 if (t->bt_rdata.size < t->bt_reclen) {
84 reallocf(t->bt_rdata.data, t->bt_reclen);
85 if (t->bt_rdata.data == NULL)
87 t->bt_rdata.size = t->bt_reclen;
89 memmove(t->bt_rdata.data, data->data, data->size);
90 memset((char *)t->bt_rdata.data + data->size,
91 t->bt_bval, t->bt_reclen - data->size);
92 fdata.data = t->bt_rdata.data;
93 fdata.size = t->bt_reclen;
95 fdata.data = data->data;
96 fdata.size = data->size;
101 if (!F_ISSET(&t->bt_cursor, CURS_INIT))
103 nrec = t->bt_cursor.rcursor;
106 if ((nrec = *(recno_t *)key->data) == 0)
110 if ((nrec = *(recno_t *)key->data) == 0) {
117 if ((nrec = *(recno_t *)key->data) == 0)
121 if ((nrec = *(recno_t *)key->data) == 0)
123 if (nrec <= t->bt_nrecs)
124 return (RET_SPECIAL);
127 einval: errno = EINVAL;
132 * Make sure that records up to and including the put record are
133 * already in the database. If skipping records, create empty ones.
135 if (nrec > t->bt_nrecs) {
136 if (!F_ISSET(t, R_EOF | R_INMEM) &&
137 t->bt_irec(t, nrec) == RET_ERROR)
139 if (nrec > t->bt_nrecs + 1) {
140 if (F_ISSET(t, R_FIXLEN)) {
142 (void *)malloc(t->bt_reclen)) == NULL)
144 tdata.size = t->bt_reclen;
145 memset(tdata.data, t->bt_bval, tdata.size);
150 while (nrec > t->bt_nrecs + 1)
152 t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
154 if (F_ISSET(t, R_FIXLEN))
159 if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
167 t->bt_cursor.rcursor = nrec;
171 F_SET(t, R_MODIFIED);
172 return (__rec_ret(t, NULL, nrec, key, NULL));
176 * __REC_IPUT -- Add a recno item to the tree.
180 * nrec: record number
184 * RET_ERROR, RET_SUCCESS
187 __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
192 indx_t curindex, nxtindex;
196 char *dest, db[NOVFLSIZE];
199 * If the data won't fit on a page, store it on indirect pages.
202 * If the insert fails later on, these pages aren't recovered.
204 if (data->size > t->bt_ovflsize) {
205 if (__ovfl_put(t, data, &pg) == RET_ERROR)
208 tdata.size = NOVFLSIZE;
210 *(u_int32_t *)(db + sizeof(pgno_t)) = data->size;
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)
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.
229 * Pages are split as required.
238 if (nrec < t->bt_nrecs &&
239 __rec_dleaf(t, h, curindex) == RET_ERROR) {
240 mpool_put(t->bt_mp, h, 0);
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.
251 nbytes = NRLEAFDBT(data->size);
252 if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
253 status = __bt_split(t, h, NULL, data, dflags, nbytes, curindex);
254 if (status == RET_SUCCESS)
259 if (curindex < (nxtindex = NEXTINDEX(h)))
260 memmove(h->linp + curindex + 1, h->linp + curindex,
261 (nxtindex - curindex) * sizeof(indx_t));
262 h->lower += sizeof(indx_t);
264 h->linp[curindex] = h->upper -= nbytes;
265 dest = (char *)h + h->upper;
266 WR_RLEAF(dest, data, dflags);
269 F_SET(t, B_MODIFIED);
270 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
272 return (RET_SUCCESS);