bt_put.c revision 1.10 1 /* $NetBSD: bt_put.c,v 1.10 1997/07/21 14:06:35 jtc 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.10 1997/07/21 14:06:35 jtc Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include "namespace.h"
49 #include <sys/types.h>
50
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include <db.h>
57 #include "btree.h"
58
59 static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *));
60
61 /*
62 * __BT_PUT -- Add a btree item to the tree.
63 *
64 * Parameters:
65 * dbp: pointer to access method
66 * key: key
67 * data: data
68 * flag: R_NOOVERWRITE
69 *
70 * Returns:
71 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
72 * tree and R_NOOVERWRITE specified.
73 */
74 int
75 __bt_put(dbp, key, data, flags)
76 const DB *dbp;
77 DBT *key;
78 const DBT *data;
79 u_int flags;
80 {
81 BTREE *t;
82 DBT tkey, tdata;
83 EPG *e = NULL; /* pacify gcc */
84 PAGE *h;
85 indx_t index, nxtindex;
86 pgno_t pg;
87 u_int32_t nbytes;
88 int dflags, exact, status;
89 char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
90
91 t = dbp->internal;
92
93 /* Toss any page pinned across calls. */
94 if (t->bt_pinned != NULL) {
95 mpool_put(t->bt_mp, t->bt_pinned, 0);
96 t->bt_pinned = NULL;
97 }
98
99 /* Check for change to a read-only tree. */
100 if (F_ISSET(t, B_RDONLY)) {
101 errno = EPERM;
102 return (RET_ERROR);
103 }
104
105 switch (flags) {
106 case 0:
107 case R_NOOVERWRITE:
108 break;
109 case R_CURSOR:
110 /*
111 * If flags is R_CURSOR, put the cursor. Must already
112 * have started a scan and not have already deleted it.
113 */
114 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
115 !F_ISSET(&t->bt_cursor,
116 CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
117 break;
118 /* FALLTHROUGH */
119 default:
120 errno = EINVAL;
121 return (RET_ERROR);
122 }
123
124 /*
125 * If the key/data pair won't fit on a page, store it on overflow
126 * pages. Only put the key on the overflow page if the pair are
127 * still too big after moving the data to an overflow page.
128 *
129 * XXX
130 * If the insert fails later on, the overflow pages aren't recovered.
131 */
132 dflags = 0;
133 if (key->size + data->size > t->bt_ovflsize) {
134 if (key->size > t->bt_ovflsize) {
135 storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR)
136 return (RET_ERROR);
137 tkey.data = kb;
138 tkey.size = NOVFLSIZE;
139 memmove(kb, &pg, sizeof(pgno_t));
140 memmove(kb + sizeof(pgno_t),
141 &key->size, sizeof(u_int32_t));
142 dflags |= P_BIGKEY;
143 key = &tkey;
144 }
145 if (key->size + data->size > t->bt_ovflsize) {
146 if (__ovfl_put(t, data, &pg) == RET_ERROR)
147 return (RET_ERROR);
148 tdata.data = db;
149 tdata.size = NOVFLSIZE;
150 memmove(db, &pg, sizeof(pgno_t));
151 memmove(db + sizeof(pgno_t),
152 &data->size, sizeof(u_int32_t));
153 dflags |= P_BIGDATA;
154 data = &tdata;
155 }
156 if (key->size + data->size > t->bt_ovflsize)
157 goto storekey;
158 }
159
160 /* Replace the cursor. */
161 if (flags == R_CURSOR) {
162 if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL)
163 return (RET_ERROR);
164 index = t->bt_cursor.pg.index;
165 goto delete;
166 }
167
168 /*
169 * Find the key to delete, or, the location at which to insert.
170 * Bt_fast and __bt_search both pin the returned page.
171 */
172 if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
173 if ((e = __bt_search(t, key, &exact)) == NULL)
174 return (RET_ERROR);
175 h = e->page;
176 index = e->index;
177
178 /*
179 * Add the key/data pair to the tree. If an identical key is already
180 * in the tree, and R_NOOVERWRITE is set, an error is returned. If
181 * R_NOOVERWRITE is not set, the key is either added (if duplicates are
182 * permitted) or an error is returned.
183 */
184 switch (flags) {
185 case R_NOOVERWRITE:
186 if (!exact)
187 break;
188 mpool_put(t->bt_mp, h, 0);
189 return (RET_SPECIAL);
190 default:
191 if (!exact || !F_ISSET(t, B_NODUPS))
192 break;
193 /*
194 * !!!
195 * Note, the delete may empty the page, so we need to put a
196 * new entry into the page immediately.
197 */
198 delete: if (__bt_dleaf(t, key, h, index) == RET_ERROR) {
199 mpool_put(t->bt_mp, h, 0);
200 return (RET_ERROR);
201 }
202 break;
203 }
204
205 /*
206 * If not enough room, or the user has put a ceiling on the number of
207 * keys permitted in the page, split the page. The split code will
208 * insert the key and data and unpin the current page. If inserting
209 * into the offset array, shift the pointers up.
210 */
211 nbytes = NBLEAFDBT(key->size, data->size);
212 if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
213 if ((status = __bt_split(t, h, key,
214 data, dflags, nbytes, index)) != RET_SUCCESS)
215 return (status);
216 goto success;
217 }
218
219 if (index < (nxtindex = NEXTINDEX(h)))
220 memmove(h->linp + index + 1, h->linp + index,
221 (nxtindex - index) * sizeof(indx_t));
222 h->lower += sizeof(indx_t);
223
224 h->linp[index] = h->upper -= nbytes;
225 dest = (char *)h + h->upper;
226 WR_BLEAF(dest, key, data, dflags);
227
228 /* If the cursor is on this page, adjust it as necessary. */
229 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
230 !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
231 t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= index)
232 ++t->bt_cursor.pg.index;
233
234 if (t->bt_order == NOT)
235 if (h->nextpg == P_INVALID) {
236 if (index == NEXTINDEX(h) - 1) {
237 t->bt_order = FORWARD;
238 t->bt_last.index = index;
239 t->bt_last.pgno = h->pgno;
240 }
241 } else if (h->prevpg == P_INVALID) {
242 if (index == 0) {
243 t->bt_order = BACK;
244 t->bt_last.index = 0;
245 t->bt_last.pgno = h->pgno;
246 }
247 }
248
249 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
250
251 success:
252 if (flags == R_SETCURSOR)
253 __bt_setcur(t, e->page->pgno, e->index);
254
255 F_SET(t, B_MODIFIED);
256 return (RET_SUCCESS);
257 }
258
259 #ifdef STATISTICS
260 u_long bt_cache_hit, bt_cache_miss;
261 #endif
262
263 /*
264 * BT_FAST -- Do a quick check for sorted data.
265 *
266 * Parameters:
267 * t: tree
268 * key: key to insert
269 *
270 * Returns:
271 * EPG for new record or NULL if not found.
272 */
273 static EPG *
274 bt_fast(t, key, data, exactp)
275 BTREE *t;
276 const DBT *key, *data;
277 int *exactp;
278 {
279 PAGE *h;
280 u_int32_t nbytes;
281 int cmp;
282
283 if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
284 t->bt_order = NOT;
285 return (NULL);
286 }
287 t->bt_cur.page = h;
288 t->bt_cur.index = t->bt_last.index;
289
290 /*
291 * If won't fit in this page or have too many keys in this page,
292 * have to search to get split stack.
293 */
294 nbytes = NBLEAFDBT(key->size, data->size);
295 if (h->upper - h->lower < nbytes + sizeof(indx_t))
296 goto miss;
297
298 if (t->bt_order == FORWARD) {
299 if (t->bt_cur.page->nextpg != P_INVALID)
300 goto miss;
301 if (t->bt_cur.index != NEXTINDEX(h) - 1)
302 goto miss;
303 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
304 goto miss;
305 t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
306 } else {
307 if (t->bt_cur.page->prevpg != P_INVALID)
308 goto miss;
309 if (t->bt_cur.index != 0)
310 goto miss;
311 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
312 goto miss;
313 t->bt_last.index = 0;
314 }
315 *exactp = cmp == 0;
316 #ifdef STATISTICS
317 ++bt_cache_hit;
318 #endif
319 return (&t->bt_cur);
320
321 miss:
322 #ifdef STATISTICS
323 ++bt_cache_miss;
324 #endif
325 t->bt_order = NOT;
326 mpool_put(t->bt_mp, h, 0);
327 return (NULL);
328 }
329