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