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