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