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