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