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