xref: /freebsd/lib/libc/db/btree/bt_put.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 /*-
2  * Copyright (c) 1990, 1993
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.3 (Berkeley) 9/16/93";
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.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
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;
76 	PAGE *h;
77 	indx_t index, nxtindex;
78 	pgno_t pg;
79 	size_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 	switch (flags) {
92 	case R_CURSOR:
93 		if (!ISSET(t, B_SEQINIT))
94 			goto einval;
95 		if (ISSET(t, B_DELCRSR))
96 			goto einval;
97 		break;
98 	case 0:
99 	case R_NOOVERWRITE:
100 		break;
101 	default:
102 einval:		errno = EINVAL;
103 		return (RET_ERROR);
104 	}
105 
106 	if (ISSET(t, B_RDONLY)) {
107 		errno = EPERM;
108 		return (RET_ERROR);
109 	}
110 
111 	/*
112 	 * If the key/data won't fit on a page, store it on indirect pages.
113 	 * Only store the key on the overflow page if it's too big after the
114 	 * data is on an overflow page.
115 	 *
116 	 * XXX
117 	 * If the insert fails later on, these pages aren't recovered.
118 	 */
119 	dflags = 0;
120 	if (key->size + data->size > t->bt_ovflsize) {
121 		if (key->size > t->bt_ovflsize) {
122 storekey:		if (__ovfl_put(t, key, &pg) == RET_ERROR)
123 				return (RET_ERROR);
124 			tkey.data = kb;
125 			tkey.size = NOVFLSIZE;
126 			memmove(kb, &pg, sizeof(pgno_t));
127 			memmove(kb + sizeof(pgno_t),
128 			    &key->size, sizeof(size_t));
129 			dflags |= P_BIGKEY;
130 			key = &tkey;
131 		}
132 		if (key->size + data->size > t->bt_ovflsize) {
133 			if (__ovfl_put(t, data, &pg) == RET_ERROR)
134 				return (RET_ERROR);
135 			tdata.data = db;
136 			tdata.size = NOVFLSIZE;
137 			memmove(db, &pg, sizeof(pgno_t));
138 			memmove(db + sizeof(pgno_t),
139 			    &data->size, sizeof(size_t));
140 			dflags |= P_BIGDATA;
141 			data = &tdata;
142 		}
143 		if (key->size + data->size > t->bt_ovflsize)
144 			goto storekey;
145 	}
146 
147 	/* Replace the cursor. */
148 	if (flags == R_CURSOR) {
149 		if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
150 			return (RET_ERROR);
151 		index = t->bt_bcursor.index;
152 		goto delete;
153 	}
154 
155 	/*
156 	 * Find the key to delete, or, the location at which to insert.  Bt_fast
157 	 * and __bt_search pin the returned page.
158 	 */
159 	if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
160 		if ((e = __bt_search(t, key, &exact)) == NULL)
161 			return (RET_ERROR);
162 	h = e->page;
163 	index = e->index;
164 
165 	/*
166 	 * Add the specified key/data pair to the tree.  If an identical key
167 	 * is already in the tree, and R_NOOVERWRITE is set, an error is
168 	 * returned.  If R_NOOVERWRITE is not set, the key is either added (if
169 	 * duplicates are permitted) or an error is returned.
170 	 *
171 	 * Pages are split as required.
172 	 */
173 	switch (flags) {
174 	case R_NOOVERWRITE:
175 		if (!exact)
176 			break;
177 		/*
178 		 * One special case is if the cursor references the record and
179 		 * it's been flagged for deletion.  Then, we delete the record,
180 		 * leaving the cursor there -- this means that the inserted
181 		 * record will not be seen in a cursor scan.
182 		 */
183 		if (ISSET(t, B_DELCRSR) && t->bt_bcursor.pgno == h->pgno &&
184 		    t->bt_bcursor.index == index) {
185 			CLR(t, B_DELCRSR);
186 			goto delete;
187 		}
188 		mpool_put(t->bt_mp, h, 0);
189 		return (RET_SPECIAL);
190 	default:
191 		if (!exact || !ISSET(t, B_NODUPS))
192 			break;
193 delete:		if (__bt_dleaf(t, h, index) == RET_ERROR) {
194 			mpool_put(t->bt_mp, h, 0);
195 			return (RET_ERROR);
196 		}
197 		break;
198 	}
199 
200 	/*
201 	 * If not enough room, or the user has put a ceiling on the number of
202 	 * keys permitted in the page, split the page.  The split code will
203 	 * insert the key and data and unpin the current page.  If inserting
204 	 * into the offset array, shift the pointers up.
205 	 */
206 	nbytes = NBLEAFDBT(key->size, data->size);
207 	if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
208 		if ((status = __bt_split(t, h, key,
209 		    data, dflags, nbytes, index)) != RET_SUCCESS)
210 			return (status);
211 		goto success;
212 	}
213 
214 	if (index < (nxtindex = NEXTINDEX(h)))
215 		memmove(h->linp + index + 1, h->linp + index,
216 		    (nxtindex - index) * sizeof(indx_t));
217 	h->lower += sizeof(indx_t);
218 
219 	h->linp[index] = h->upper -= nbytes;
220 	dest = (char *)h + h->upper;
221 	WR_BLEAF(dest, key, data, dflags);
222 
223 	if (t->bt_order == NOT)
224 		if (h->nextpg == P_INVALID) {
225 			if (index == NEXTINDEX(h) - 1) {
226 				t->bt_order = FORWARD;
227 				t->bt_last.index = index;
228 				t->bt_last.pgno = h->pgno;
229 			}
230 		} else if (h->prevpg == P_INVALID) {
231 			if (index == 0) {
232 				t->bt_order = BACK;
233 				t->bt_last.index = 0;
234 				t->bt_last.pgno = h->pgno;
235 			}
236 		}
237 
238 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
239 
240 success:
241 	if (flags == R_SETCURSOR) {
242 		t->bt_bcursor.pgno = e->page->pgno;
243 		t->bt_bcursor.index = e->index;
244 	}
245 	SET(t, B_MODIFIED);
246 	return (RET_SUCCESS);
247 }
248 
249 #ifdef STATISTICS
250 u_long bt_cache_hit, bt_cache_miss;
251 #endif
252 
253 /*
254  * BT_FAST -- Do a quick check for sorted data.
255  *
256  * Parameters:
257  *	t:	tree
258  *	key:	key to insert
259  *
260  * Returns:
261  * 	EPG for new record or NULL if not found.
262  */
263 static EPG *
264 bt_fast(t, key, data, exactp)
265 	BTREE *t;
266 	const DBT *key, *data;
267 	int *exactp;
268 {
269 	PAGE *h;
270 	size_t nbytes;
271 	int cmp;
272 
273 	if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
274 		t->bt_order = NOT;
275 		return (NULL);
276 	}
277 	t->bt_cur.page = h;
278 	t->bt_cur.index = t->bt_last.index;
279 
280 	/*
281 	 * If won't fit in this page or have too many keys in this page, have
282 	 * to search to get split stack.
283 	 */
284 	nbytes = NBLEAFDBT(key->size, data->size);
285 	if (h->upper - h->lower < nbytes + sizeof(indx_t))
286 		goto miss;
287 
288 	if (t->bt_order == FORWARD) {
289 		if (t->bt_cur.page->nextpg != P_INVALID)
290 			goto miss;
291 		if (t->bt_cur.index != NEXTINDEX(h) - 1)
292 			goto miss;
293 		if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
294 			goto miss;
295 		t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
296 	} else {
297 		if (t->bt_cur.page->prevpg != P_INVALID)
298 			goto miss;
299 		if (t->bt_cur.index != 0)
300 			goto miss;
301 		if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
302 			goto miss;
303 		t->bt_last.index = 0;
304 	}
305 	*exactp = cmp == 0;
306 #ifdef STATISTICS
307 	++bt_cache_hit;
308 #endif
309 	return (&t->bt_cur);
310 
311 miss:
312 #ifdef STATISTICS
313 	++bt_cache_miss;
314 #endif
315 	t->bt_order = NOT;
316 	mpool_put(t->bt_mp, h, 0);
317 	return (NULL);
318 }
319