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