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