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