1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)bt_put.c	8.8 (Berkeley) 7/26/94";
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include <sys/types.h>
42 
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include "db-int.h"
49 #include "btree.h"
50 
51 static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *));
52 
53 /*
54  * __BT_PUT -- Add a btree item to the tree.
55  *
56  * Parameters:
57  *	dbp:	pointer to access method
58  *	key:	key
59  *	data:	data
60  *	flag:	R_NOOVERWRITE
61  *
62  * Returns:
63  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
64  *	tree and R_NOOVERWRITE specified.
65  */
66 int
__bt_put(dbp,key,data,flags)67 __bt_put(dbp, key, data, flags)
68 	const DB *dbp;
69 	DBT *key;
70 	const DBT *data;
71 	u_int flags;
72 {
73 	BTREE *t;
74 	DBT tkey, tdata;
75 	EPG *e = 0;
76 	PAGE *h;
77 	indx_t idx, nxtindex;
78 	db_pgno_t pg;
79 	u_int32_t nbytes;
80 	int dflags, exact, status;
81 	char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
82 
83 	t = dbp->internal;
84 
85 	/* Toss any page pinned across calls. */
86 	if (t->bt_pinned != NULL) {
87 		mpool_put(t->bt_mp, t->bt_pinned, 0);
88 		t->bt_pinned = NULL;
89 	}
90 
91 	/* Check for change to a read-only tree. */
92 	if (F_ISSET(t, B_RDONLY)) {
93 		errno = EPERM;
94 		return (RET_ERROR);
95 	}
96 
97 	switch (flags) {
98 	case 0:
99 	case R_NOOVERWRITE:
100 		break;
101 	case R_CURSOR:
102 		/*
103 		 * If flags is R_CURSOR, put the cursor.  Must already
104 		 * have started a scan and not have already deleted it.
105 		 */
106 		if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
107 		    !F_ISSET(&t->bt_cursor,
108 		        CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
109 			break;
110 		/* FALLTHROUGH */
111 	default:
112 		errno = EINVAL;
113 		return (RET_ERROR);
114 	}
115 
116 	/*
117 	 * If the key/data pair won't fit on a page, store it on overflow
118 	 * pages.  Only put the key on the overflow page if the pair are
119 	 * still too big after moving the data to an overflow page.
120 	 *
121 	 * XXX
122 	 * If the insert fails later on, the overflow pages aren't recovered.
123 	 */
124 	dflags = 0;
125 	if (key->size + data->size > t->bt_ovflsize) {
126 		if (key->size > t->bt_ovflsize) {
127 			u_int32_t yuck_this_is_gross_code;
128 storekey:		if (__ovfl_put(t, key, &pg) == RET_ERROR)
129 				return (RET_ERROR);
130 			tkey.data = kb;
131 			tkey.size = NOVFLSIZE;
132 			memmove(kb, &pg, sizeof(db_pgno_t));
133 			yuck_this_is_gross_code = key->size;
134 			if (yuck_this_is_gross_code != key->size)
135 				abort ();
136 			memmove(kb + sizeof(db_pgno_t),
137 				&yuck_this_is_gross_code, sizeof(u_int32_t));
138 			dflags |= P_BIGKEY;
139 			key = &tkey;
140 		}
141 		if (key->size + data->size > t->bt_ovflsize) {
142 			u_int32_t yuck_this_is_gross_code = data->size;
143 			if (__ovfl_put(t, data, &pg) == RET_ERROR)
144 				return (RET_ERROR);
145 			tdata.data = db;
146 			tdata.size = NOVFLSIZE;
147 			memmove(db, &pg, sizeof(db_pgno_t));
148 			if (yuck_this_is_gross_code != data->size)
149 				abort ();
150 			memmove(db + sizeof(db_pgno_t),
151 				&yuck_this_is_gross_code, 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 		idx = 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 	idx = 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, idx) == 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, idx)) != RET_SUCCESS)
214 			return (status);
215 		goto success;
216 	}
217 
218 	if (idx < (nxtindex = NEXTINDEX(h)))
219 		memmove(h->linp + idx + 1, h->linp + idx,
220 		    (nxtindex - idx) * sizeof(indx_t));
221 	h->lower += sizeof(indx_t);
222 
223 	h->linp[idx] = 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 >= idx)
231 		++t->bt_cursor.pg.index;
232 
233 	if (t->bt_order == NOT) {
234 		if (h->nextpg == P_INVALID) {
235 			if (idx == NEXTINDEX(h) - 1) {
236 				t->bt_order = FORWARD;
237 				t->bt_last.index = idx;
238 				t->bt_last.pgno = h->pgno;
239 			}
240 		} else if (h->prevpg == P_INVALID) {
241 			if (idx == 0) {
242 				t->bt_order = BACK;
243 				t->bt_last.index = 0;
244 				t->bt_last.pgno = h->pgno;
245 			}
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 *
bt_fast(t,key,data,exactp)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