bt_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 * This code is derived from software contributed to Berkeley by
6 1.6.2.2 cgd * Mike Olson.
7 1.6.2.2 cgd *
8 1.6.2.2 cgd * Redistribution and use in source and binary forms, with or without
9 1.6.2.2 cgd * modification, are permitted provided that the following conditions
10 1.6.2.2 cgd * are met:
11 1.6.2.2 cgd * 1. Redistributions of source code must retain the above copyright
12 1.6.2.2 cgd * notice, this list of conditions and the following disclaimer.
13 1.6.2.2 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.6.2.2 cgd * notice, this list of conditions and the following disclaimer in the
15 1.6.2.2 cgd * documentation and/or other materials provided with the distribution.
16 1.6.2.2 cgd * 3. All advertising materials mentioning features or use of this software
17 1.6.2.2 cgd * must display the following acknowledgement:
18 1.6.2.2 cgd * This product includes software developed by the University of
19 1.6.2.2 cgd * California, Berkeley and its contributors.
20 1.6.2.2 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.6.2.2 cgd * may be used to endorse or promote products derived from this software
22 1.6.2.2 cgd * without specific prior written permission.
23 1.6.2.2 cgd *
24 1.6.2.2 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.6.2.2 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.6.2.2 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.6.2.2 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.6.2.2 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.6.2.2 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.6.2.2 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.6.2.2 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.6.2.2 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.6.2.2 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.6.2.2 cgd * SUCH DAMAGE.
35 1.6.2.2 cgd */
36 1.6.2.2 cgd
37 1.6.2.2 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.6.2.2 cgd static char sccsid[] = "@(#)bt_put.c 8.4 (Berkeley) 5/31/94";
39 1.6.2.2 cgd #endif /* LIBC_SCCS and not lint */
40 1.6.2.2 cgd
41 1.6.2.2 cgd #include <sys/types.h>
42 1.6.2.2 cgd
43 1.6.2.2 cgd #include <errno.h>
44 1.6.2.2 cgd #include <stdio.h>
45 1.6.2.2 cgd #include <stdlib.h>
46 1.6.2.2 cgd #include <string.h>
47 1.6.2.2 cgd
48 1.6.2.2 cgd #include <db.h>
49 1.6.2.2 cgd #include "btree.h"
50 1.6.2.2 cgd
51 1.6.2.2 cgd static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *));
52 1.6.2.2 cgd
53 1.6.2.2 cgd /*
54 1.6.2.2 cgd * __BT_PUT -- Add a btree item to the tree.
55 1.6.2.2 cgd *
56 1.6.2.2 cgd * Parameters:
57 1.6.2.2 cgd * dbp: pointer to access method
58 1.6.2.2 cgd * key: key
59 1.6.2.2 cgd * data: data
60 1.6.2.2 cgd * flag: R_NOOVERWRITE
61 1.6.2.2 cgd *
62 1.6.2.2 cgd * Returns:
63 1.6.2.2 cgd * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
64 1.6.2.2 cgd * tree and R_NOOVERWRITE specified.
65 1.6.2.2 cgd */
66 1.6.2.2 cgd int
67 1.6.2.2 cgd __bt_put(dbp, key, data, flags)
68 1.6.2.2 cgd const DB *dbp;
69 1.6.2.2 cgd DBT *key;
70 1.6.2.2 cgd const DBT *data;
71 1.6.2.2 cgd u_int flags;
72 1.6.2.2 cgd {
73 1.6.2.2 cgd BTREE *t;
74 1.6.2.2 cgd DBT tkey, tdata;
75 1.6.2.2 cgd EPG *e;
76 1.6.2.2 cgd PAGE *h;
77 1.6.2.2 cgd indx_t index, nxtindex;
78 1.6.2.2 cgd pgno_t pg;
79 1.6.2.2 cgd u_int32_t nbytes;
80 1.6.2.2 cgd int dflags, exact, status;
81 1.6.2.2 cgd char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
82 1.6.2.2 cgd
83 1.6.2.2 cgd t = dbp->internal;
84 1.6.2.2 cgd
85 1.6.2.2 cgd /* Toss any page pinned across calls. */
86 1.6.2.2 cgd if (t->bt_pinned != NULL) {
87 1.6.2.2 cgd mpool_put(t->bt_mp, t->bt_pinned, 0);
88 1.6.2.2 cgd t->bt_pinned = NULL;
89 1.6.2.2 cgd }
90 1.6.2.2 cgd
91 1.6.2.2 cgd switch (flags) {
92 1.6.2.2 cgd case R_CURSOR:
93 1.6.2.2 cgd if (!ISSET(t, B_SEQINIT))
94 1.6.2.2 cgd goto einval;
95 1.6.2.2 cgd if (ISSET(t, B_DELCRSR))
96 1.6.2.2 cgd goto einval;
97 1.6.2.2 cgd break;
98 1.6.2.2 cgd case 0:
99 1.6.2.2 cgd case R_NOOVERWRITE:
100 1.6.2.2 cgd break;
101 1.6.2.2 cgd default:
102 1.6.2.2 cgd einval: errno = EINVAL;
103 1.6.2.2 cgd return (RET_ERROR);
104 1.6.2.2 cgd }
105 1.6.2.2 cgd
106 1.6.2.2 cgd if (ISSET(t, B_RDONLY)) {
107 1.6.2.2 cgd errno = EPERM;
108 1.6.2.2 cgd return (RET_ERROR);
109 1.6.2.2 cgd }
110 1.6.2.2 cgd
111 1.6.2.2 cgd /*
112 1.6.2.2 cgd * If the key/data won't fit on a page, store it on indirect pages.
113 1.6.2.2 cgd * Only store the key on the overflow page if it's too big after the
114 1.6.2.2 cgd * data is on an overflow page.
115 1.6.2.2 cgd *
116 1.6.2.2 cgd * XXX
117 1.6.2.2 cgd * If the insert fails later on, these pages aren't recovered.
118 1.6.2.2 cgd */
119 1.6.2.2 cgd dflags = 0;
120 1.6.2.2 cgd if (key->size + data->size > t->bt_ovflsize) {
121 1.6.2.2 cgd if (key->size > t->bt_ovflsize) {
122 1.6.2.2 cgd storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR)
123 1.6.2.2 cgd return (RET_ERROR);
124 1.6.2.2 cgd tkey.data = kb;
125 1.6.2.2 cgd tkey.size = NOVFLSIZE;
126 1.6.2.2 cgd memmove(kb, &pg, sizeof(pgno_t));
127 1.6.2.2 cgd memmove(kb + sizeof(pgno_t),
128 1.6.2.2 cgd &key->size, sizeof(u_int32_t));
129 1.6.2.2 cgd dflags |= P_BIGKEY;
130 1.6.2.2 cgd key = &tkey;
131 1.6.2.2 cgd }
132 1.6.2.2 cgd if (key->size + data->size > t->bt_ovflsize) {
133 1.6.2.2 cgd if (__ovfl_put(t, data, &pg) == RET_ERROR)
134 1.6.2.2 cgd return (RET_ERROR);
135 1.6.2.2 cgd tdata.data = db;
136 1.6.2.2 cgd tdata.size = NOVFLSIZE;
137 1.6.2.2 cgd memmove(db, &pg, sizeof(pgno_t));
138 1.6.2.2 cgd memmove(db + sizeof(pgno_t),
139 1.6.2.2 cgd &data->size, sizeof(u_int32_t));
140 1.6.2.2 cgd dflags |= P_BIGDATA;
141 1.6.2.2 cgd data = &tdata;
142 1.6.2.2 cgd }
143 1.6.2.2 cgd if (key->size + data->size > t->bt_ovflsize)
144 1.6.2.2 cgd goto storekey;
145 1.6.2.2 cgd }
146 1.6.2.2 cgd
147 1.6.2.2 cgd /* Replace the cursor. */
148 1.6.2.2 cgd if (flags == R_CURSOR) {
149 1.6.2.2 cgd if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
150 1.6.2.2 cgd return (RET_ERROR);
151 1.6.2.2 cgd index = t->bt_bcursor.index;
152 1.6.2.2 cgd goto delete;
153 1.6.2.2 cgd }
154 1.6.2.2 cgd
155 1.6.2.2 cgd /*
156 1.6.2.2 cgd * Find the key to delete, or, the location at which to insert. Bt_fast
157 1.6.2.2 cgd * and __bt_search pin the returned page.
158 1.6.2.2 cgd */
159 1.6.2.2 cgd if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
160 1.6.2.2 cgd if ((e = __bt_search(t, key, &exact)) == NULL)
161 1.6.2.2 cgd return (RET_ERROR);
162 1.6.2.2 cgd h = e->page;
163 1.6.2.2 cgd index = e->index;
164 1.6.2.2 cgd
165 1.6.2.2 cgd /*
166 1.6.2.2 cgd * Add the specified key/data pair to the tree. If an identical key
167 1.6.2.2 cgd * is already in the tree, and R_NOOVERWRITE is set, an error is
168 1.6.2.2 cgd * returned. If R_NOOVERWRITE is not set, the key is either added (if
169 1.6.2.2 cgd * duplicates are permitted) or an error is returned.
170 1.6.2.2 cgd *
171 1.6.2.2 cgd * Pages are split as required.
172 1.6.2.2 cgd */
173 1.6.2.2 cgd switch (flags) {
174 1.6.2.2 cgd case R_NOOVERWRITE:
175 1.6.2.2 cgd if (!exact)
176 1.6.2.2 cgd break;
177 1.6.2.2 cgd /*
178 1.6.2.2 cgd * One special case is if the cursor references the record and
179 1.6.2.2 cgd * it's been flagged for deletion. Then, we delete the record,
180 1.6.2.2 cgd * leaving the cursor there -- this means that the inserted
181 1.6.2.2 cgd * record will not be seen in a cursor scan.
182 1.6.2.2 cgd */
183 1.6.2.2 cgd if (ISSET(t, B_DELCRSR) && t->bt_bcursor.pgno == h->pgno &&
184 1.6.2.2 cgd t->bt_bcursor.index == index) {
185 1.6.2.2 cgd CLR(t, B_DELCRSR);
186 1.6.2.2 cgd goto delete;
187 1.6.2.2 cgd }
188 1.6.2.2 cgd mpool_put(t->bt_mp, h, 0);
189 1.6.2.2 cgd return (RET_SPECIAL);
190 1.6.2.2 cgd default:
191 1.6.2.2 cgd if (!exact || !ISSET(t, B_NODUPS))
192 1.6.2.2 cgd break;
193 1.6.2.2 cgd delete: if (__bt_dleaf(t, h, index) == RET_ERROR) {
194 1.6.2.2 cgd mpool_put(t->bt_mp, h, 0);
195 1.6.2.2 cgd return (RET_ERROR);
196 1.6.2.2 cgd }
197 1.6.2.2 cgd break;
198 1.6.2.2 cgd }
199 1.6.2.2 cgd
200 1.6.2.2 cgd /*
201 1.6.2.2 cgd * If not enough room, or the user has put a ceiling on the number of
202 1.6.2.2 cgd * keys permitted in the page, split the page. The split code will
203 1.6.2.2 cgd * insert the key and data and unpin the current page. If inserting
204 1.6.2.2 cgd * into the offset array, shift the pointers up.
205 1.6.2.2 cgd */
206 1.6.2.2 cgd nbytes = NBLEAFDBT(key->size, data->size);
207 1.6.2.2 cgd if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
208 1.6.2.2 cgd if ((status = __bt_split(t, h, key,
209 1.6.2.2 cgd data, dflags, nbytes, index)) != RET_SUCCESS)
210 1.6.2.2 cgd return (status);
211 1.6.2.2 cgd goto success;
212 1.6.2.2 cgd }
213 1.6.2.2 cgd
214 1.6.2.2 cgd if (index < (nxtindex = NEXTINDEX(h)))
215 1.6.2.2 cgd memmove(h->linp + index + 1, h->linp + index,
216 1.6.2.2 cgd (nxtindex - index) * sizeof(indx_t));
217 1.6.2.2 cgd h->lower += sizeof(indx_t);
218 1.6.2.2 cgd
219 1.6.2.2 cgd h->linp[index] = h->upper -= nbytes;
220 1.6.2.2 cgd dest = (char *)h + h->upper;
221 1.6.2.2 cgd WR_BLEAF(dest, key, data, dflags);
222 1.6.2.2 cgd
223 1.6.2.2 cgd if (t->bt_order == NOT)
224 1.6.2.2 cgd if (h->nextpg == P_INVALID) {
225 1.6.2.2 cgd if (index == NEXTINDEX(h) - 1) {
226 1.6.2.2 cgd t->bt_order = FORWARD;
227 1.6.2.2 cgd t->bt_last.index = index;
228 1.6.2.2 cgd t->bt_last.pgno = h->pgno;
229 1.6.2.2 cgd }
230 1.6.2.2 cgd } else if (h->prevpg == P_INVALID) {
231 1.6.2.2 cgd if (index == 0) {
232 1.6.2.2 cgd t->bt_order = BACK;
233 1.6.2.2 cgd t->bt_last.index = 0;
234 1.6.2.2 cgd t->bt_last.pgno = h->pgno;
235 1.6.2.2 cgd }
236 1.6.2.2 cgd }
237 1.6.2.2 cgd
238 1.6.2.2 cgd mpool_put(t->bt_mp, h, MPOOL_DIRTY);
239 1.6.2.2 cgd
240 1.6.2.2 cgd success:
241 1.6.2.2 cgd if (flags == R_SETCURSOR) {
242 1.6.2.2 cgd t->bt_bcursor.pgno = e->page->pgno;
243 1.6.2.2 cgd t->bt_bcursor.index = e->index;
244 1.6.2.2 cgd }
245 1.6.2.2 cgd SET(t, B_MODIFIED);
246 1.6.2.2 cgd return (RET_SUCCESS);
247 1.6.2.2 cgd }
248 1.6.2.2 cgd
249 1.6.2.2 cgd #ifdef STATISTICS
250 1.6.2.2 cgd u_long bt_cache_hit, bt_cache_miss;
251 1.6.2.2 cgd #endif
252 1.6.2.2 cgd
253 1.6.2.2 cgd /*
254 1.6.2.2 cgd * BT_FAST -- Do a quick check for sorted data.
255 1.6.2.2 cgd *
256 1.6.2.2 cgd * Parameters:
257 1.6.2.2 cgd * t: tree
258 1.6.2.2 cgd * key: key to insert
259 1.6.2.2 cgd *
260 1.6.2.2 cgd * Returns:
261 1.6.2.2 cgd * EPG for new record or NULL if not found.
262 1.6.2.2 cgd */
263 1.6.2.2 cgd static EPG *
264 1.6.2.2 cgd bt_fast(t, key, data, exactp)
265 1.6.2.2 cgd BTREE *t;
266 1.6.2.2 cgd const DBT *key, *data;
267 1.6.2.2 cgd int *exactp;
268 1.6.2.2 cgd {
269 1.6.2.2 cgd PAGE *h;
270 1.6.2.2 cgd u_int32_t nbytes;
271 1.6.2.2 cgd int cmp;
272 1.6.2.2 cgd
273 1.6.2.2 cgd if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
274 1.6.2.2 cgd t->bt_order = NOT;
275 1.6.2.2 cgd return (NULL);
276 1.6.2.2 cgd }
277 1.6.2.2 cgd t->bt_cur.page = h;
278 1.6.2.2 cgd t->bt_cur.index = t->bt_last.index;
279 1.6.2.2 cgd
280 1.6.2.2 cgd /*
281 1.6.2.2 cgd * If won't fit in this page or have too many keys in this page, have
282 1.6.2.2 cgd * to search to get split stack.
283 1.6.2.2 cgd */
284 1.6.2.2 cgd nbytes = NBLEAFDBT(key->size, data->size);
285 1.6.2.2 cgd if (h->upper - h->lower < nbytes + sizeof(indx_t))
286 1.6.2.2 cgd goto miss;
287 1.6.2.2 cgd
288 1.6.2.2 cgd if (t->bt_order == FORWARD) {
289 1.6.2.2 cgd if (t->bt_cur.page->nextpg != P_INVALID)
290 1.6.2.2 cgd goto miss;
291 1.6.2.2 cgd if (t->bt_cur.index != NEXTINDEX(h) - 1)
292 1.6.2.2 cgd goto miss;
293 1.6.2.2 cgd if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
294 1.6.2.2 cgd goto miss;
295 1.6.2.2 cgd t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
296 1.6.2.2 cgd } else {
297 1.6.2.2 cgd if (t->bt_cur.page->prevpg != P_INVALID)
298 1.6.2.2 cgd goto miss;
299 1.6.2.2 cgd if (t->bt_cur.index != 0)
300 1.6.2.2 cgd goto miss;
301 1.6.2.2 cgd if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
302 1.6.2.2 cgd goto miss;
303 1.6.2.2 cgd t->bt_last.index = 0;
304 1.6.2.2 cgd }
305 1.6.2.2 cgd *exactp = cmp == 0;
306 1.6.2.2 cgd #ifdef STATISTICS
307 1.6.2.2 cgd ++bt_cache_hit;
308 1.6.2.2 cgd #endif
309 1.6.2.2 cgd return (&t->bt_cur);
310 1.6.2.2 cgd
311 1.6.2.2 cgd miss:
312 1.6.2.2 cgd #ifdef STATISTICS
313 1.6.2.2 cgd ++bt_cache_miss;
314 1.6.2.2 cgd #endif
315 1.6.2.2 cgd t->bt_order = NOT;
316 1.6.2.2 cgd mpool_put(t->bt_mp, h, 0);
317 1.6.2.2 cgd return (NULL);
318 1.6.2.2 cgd }
319