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