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