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