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