rec_put.c revision 1.8.2.1 1 /* $NetBSD: rec_put.c,v 1.8.2.1 1996/09/16 18:40:04 jtc 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.2.1 1996/09/16 18:40:04 jtc Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #include "namespace.h"
45 #include <sys/types.h>
46
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include <db.h>
53 #include "recno.h"
54
55 /*
56 * __REC_PUT -- Add a recno item to the tree.
57 *
58 * Parameters:
59 * dbp: pointer to access method
60 * key: key
61 * data: data
62 * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
63 *
64 * Returns:
65 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
66 * already in the tree and R_NOOVERWRITE specified.
67 */
68 int
69 __rec_put(dbp, key, data, flags)
70 const DB *dbp;
71 DBT *key;
72 const DBT *data;
73 u_int flags;
74 {
75 BTREE *t;
76 DBT fdata, tdata;
77 recno_t nrec;
78 int status;
79
80 t = dbp->internal;
81
82 /* Toss any page pinned across calls. */
83 if (t->bt_pinned != NULL) {
84 mpool_put(t->bt_mp, t->bt_pinned, 0);
85 t->bt_pinned = NULL;
86 }
87
88 /*
89 * If using fixed-length records, and the record is long, return
90 * EINVAL. If it's short, pad it out. Use the record data return
91 * memory, it's only short-term.
92 */
93 if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
94 if (data->size > t->bt_reclen)
95 goto einval;
96
97 if (t->bt_rdata.size < t->bt_reclen) {
98 t->bt_rdata.data = t->bt_rdata.data == NULL ?
99 malloc(t->bt_reclen) :
100 realloc(t->bt_rdata.data, t->bt_reclen);
101 if (t->bt_rdata.data == NULL)
102 return (RET_ERROR);
103 t->bt_rdata.size = t->bt_reclen;
104 }
105 memmove(t->bt_rdata.data, data->data, data->size);
106 memset((char *)t->bt_rdata.data + data->size,
107 t->bt_bval, t->bt_reclen - data->size);
108 fdata.data = t->bt_rdata.data;
109 fdata.size = t->bt_reclen;
110 } else {
111 fdata.data = data->data;
112 fdata.size = data->size;
113 }
114
115 switch (flags) {
116 case R_CURSOR:
117 if (!F_ISSET(&t->bt_cursor, CURS_INIT))
118 goto einval;
119 nrec = t->bt_cursor.rcursor;
120 break;
121 case R_SETCURSOR:
122 if ((nrec = *(recno_t *)key->data) == 0)
123 goto einval;
124 break;
125 case R_IAFTER:
126 if ((nrec = *(recno_t *)key->data) == 0) {
127 nrec = 1;
128 flags = R_IBEFORE;
129 }
130 break;
131 case 0:
132 case R_IBEFORE:
133 if ((nrec = *(recno_t *)key->data) == 0)
134 goto einval;
135 break;
136 case R_NOOVERWRITE:
137 if ((nrec = *(recno_t *)key->data) == 0)
138 goto einval;
139 if (nrec <= t->bt_nrecs)
140 return (RET_SPECIAL);
141 break;
142 default:
143 einval: errno = EINVAL;
144 return (RET_ERROR);
145 }
146
147 /*
148 * Make sure that records up to and including the put record are
149 * already in the database. If skipping records, create empty ones.
150 */
151 if (nrec > t->bt_nrecs) {
152 if (!F_ISSET(t, R_EOF | R_INMEM) &&
153 t->bt_irec(t, nrec) == RET_ERROR)
154 return (RET_ERROR);
155 if (nrec > t->bt_nrecs + 1) {
156 if (F_ISSET(t, R_FIXLEN)) {
157 if ((tdata.data =
158 (void *)malloc(t->bt_reclen)) == NULL)
159 return (RET_ERROR);
160 tdata.size = t->bt_reclen;
161 memset(tdata.data, t->bt_bval, tdata.size);
162 } else {
163 tdata.data = NULL;
164 tdata.size = 0;
165 }
166 while (nrec > t->bt_nrecs + 1)
167 if (__rec_iput(t,
168 t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
169 return (RET_ERROR);
170 if (F_ISSET(t, R_FIXLEN))
171 free(tdata.data);
172 }
173 }
174
175 if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
176 return (status);
177
178 if (flags == R_SETCURSOR)
179 t->bt_cursor.rcursor = nrec;
180
181 F_SET(t, R_MODIFIED);
182 return (__rec_ret(t, NULL, nrec, key, NULL));
183 }
184
185 /*
186 * __REC_IPUT -- Add a recno item to the tree.
187 *
188 * Parameters:
189 * t: tree
190 * nrec: record number
191 * data: data
192 *
193 * Returns:
194 * RET_ERROR, RET_SUCCESS
195 */
196 int
197 __rec_iput(t, nrec, data, flags)
198 BTREE *t;
199 recno_t nrec;
200 const DBT *data;
201 u_int flags;
202 {
203 DBT tdata;
204 EPG *e;
205 PAGE *h;
206 indx_t index, nxtindex;
207 pgno_t pg;
208 u_int32_t nbytes;
209 int dflags, status;
210 char *dest, db[NOVFLSIZE];
211
212 /*
213 * If the data won't fit on a page, store it on indirect pages.
214 *
215 * XXX
216 * If the insert fails later on, these pages aren't recovered.
217 */
218 if (data->size > t->bt_ovflsize) {
219 if (__ovfl_put(t, data, &pg) == RET_ERROR)
220 return (RET_ERROR);
221 tdata.data = db;
222 tdata.size = NOVFLSIZE;
223 *(pgno_t *)db = pg;
224 *(u_int32_t *)(db + sizeof(pgno_t)) = data->size;
225 dflags = P_BIGDATA;
226 data = &tdata;
227 } else
228 dflags = 0;
229
230 /* __rec_search pins the returned page. */
231 if ((e = __rec_search(t, nrec,
232 nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
233 SINSERT : SEARCH)) == NULL)
234 return (RET_ERROR);
235
236 h = e->page;
237 index = e->index;
238
239 /*
240 * Add the specified key/data pair to the tree. The R_IAFTER and
241 * R_IBEFORE flags insert the key after/before the specified key.
242 *
243 * Pages are split as required.
244 */
245 switch (flags) {
246 case R_IAFTER:
247 ++index;
248 break;
249 case R_IBEFORE:
250 break;
251 default:
252 if (nrec < t->bt_nrecs &&
253 __rec_dleaf(t, h, index) == RET_ERROR) {
254 mpool_put(t->bt_mp, h, 0);
255 return (RET_ERROR);
256 }
257 break;
258 }
259
260 /*
261 * If not enough room, split the page. The split code will insert
262 * the key and data and unpin the current page. If inserting into
263 * the offset array, shift the pointers up.
264 */
265 nbytes = NRLEAFDBT(data->size);
266 if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
267 status = __bt_split(t, h, NULL, data, dflags, nbytes, index);
268 if (status == RET_SUCCESS)
269 ++t->bt_nrecs;
270 return (status);
271 }
272
273 if (index < (nxtindex = NEXTINDEX(h)))
274 memmove(h->linp + index + 1, h->linp + index,
275 (nxtindex - index) * sizeof(indx_t));
276 h->lower += sizeof(indx_t);
277
278 h->linp[index] = h->upper -= nbytes;
279 dest = (char *)h + h->upper;
280 WR_RLEAF(dest, data, dflags);
281
282 ++t->bt_nrecs;
283 F_SET(t, B_MODIFIED);
284 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
285
286 return (RET_SUCCESS);
287 }
288