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