xref: /csrg-svn/lib/libc/db/btree/bt_put.c (revision 57445)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)bt_put.c	5.10 (Berkeley) 01/10/93";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/types.h>
16 
17 #include <db.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include "btree.h"
24 
25 static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *));
26 
27 /*
28  * __BT_PUT -- Add a btree item to the tree.
29  *
30  * Parameters:
31  *	dbp:	pointer to access method
32  *	key:	key
33  *	data:	data
34  *	flag:	R_NOOVERWRITE
35  *
36  * Returns:
37  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
38  *	tree and R_NOOVERWRITE specified.
39  */
40 int
41 __bt_put(dbp, key, data, flags)
42 	const DB *dbp;
43 	DBT *key;
44 	const DBT *data;
45 	u_int flags;
46 {
47 	BTREE *t;
48 	DBT tkey, tdata;
49 	EPG *e;
50 	PAGE *h;
51 	index_t index, nxtindex;
52 	pgno_t pg;
53 	size_t nbytes;
54 	int dflags, exact, status;
55 	char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
56 
57 	t = dbp->internal;
58 
59 	/* Clear any stack. */
60 	BT_CLR(t);
61 
62 	switch (flags) {
63 	case R_CURSOR:
64 		if (!ISSET(t, BTF_SEQINIT))
65 			goto einval;
66 		if (ISSET(t, BTF_DELCRSR))
67 			goto einval;
68 		break;
69 	case 0:
70 	case R_NOOVERWRITE:
71 		break;
72 	default:
73 einval:		errno = EINVAL;
74 		return (RET_ERROR);
75 	}
76 
77 	if (ISSET(t, BTF_RDONLY)) {
78 		errno = EPERM;
79 		return (RET_ERROR);
80 	}
81 
82 	/*
83 	 * If the key/data won't fit on a page, store it on indirect pages.
84 	 * Only store the key on the overflow page if it's too big after the
85 	 * data is on an overflow page.
86 	 *
87 	 * XXX
88 	 * If the insert fails later on, these pages aren't recovered.
89 	 */
90 	dflags = 0;
91 	if (key->size + data->size > t->bt_ovflsize) {
92 		if (key->size > t->bt_ovflsize) {
93 storekey:		if (__ovfl_put(t, key, &pg) == RET_ERROR)
94 				return (RET_ERROR);
95 			tkey.data = kb;
96 			tkey.size = NOVFLSIZE;
97 			bcopy(&pg, kb, sizeof(pgno_t));
98 			bcopy(&key->size, kb + sizeof(pgno_t), sizeof(size_t));
99 			dflags |= P_BIGKEY;
100 			key = &tkey;
101 		}
102 		if (key->size + data->size > t->bt_ovflsize) {
103 			if (__ovfl_put(t, data, &pg) == RET_ERROR)
104 				return (RET_ERROR);
105 			tdata.data = db;
106 			tdata.size = NOVFLSIZE;
107 			bcopy(&pg, db, sizeof(pgno_t));
108 			bcopy(&data->size, db + sizeof(pgno_t), sizeof(size_t));
109 			dflags |= P_BIGDATA;
110 			data = &tdata;
111 		}
112 		if (key->size + data->size > t->bt_ovflsize)
113 			goto storekey;
114 	}
115 
116 	/* Replace the cursor. */
117 	if (flags == R_CURSOR) {
118 		if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
119 			return (RET_ERROR);
120 		index = t->bt_bcursor.index;
121 		goto delete;
122 	}
123 
124 	/*
125 	 * Find the key to delete, or, the location at which to insert.  Bt_fast
126 	 * and __bt_search pin the returned page.
127 	 */
128 	if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
129 		if ((e = __bt_search(t, key, &exact)) == NULL)
130 			return (RET_ERROR);
131 	h = e->page;
132 	index = e->index;
133 
134 	/*
135 	 * Add the specified key/data pair to the tree.  If an identical key
136 	 * is already in the tree, and R_NOOVERWRITE is set, an error is
137 	 * returned.  If R_NOOVERWRITE is not set, the key is either added (if
138 	 * duplicates are permitted) or an error is returned.
139 	 *
140 	 * Pages are split as required.
141 	 */
142 	switch (flags) {
143 	case R_NOOVERWRITE:
144 		if (!exact)
145 			break;
146 		/*
147 		 * One special case is if the cursor references the record and
148 		 * it's been flagged for deletion.  Then, we delete the record,
149 		 * leaving the cursor there -- this means that the inserted
150 		 * record will not be seen in a cursor scan.
151 		 */
152 		if (ISSET(t, BTF_DELCRSR) && t->bt_bcursor.pgno == h->pgno &&
153 		    t->bt_bcursor.index == index) {
154 			CLR(t, BTF_DELCRSR);
155 			goto delete;
156 		}
157 		mpool_put(t->bt_mp, h, 0);
158 		return (RET_SPECIAL);
159 	default:
160 		if (!exact || !ISSET(t, BTF_NODUPS))
161 			break;
162 delete:		if (__bt_dleaf(t, h, index) == RET_ERROR) {
163 			mpool_put(t->bt_mp, h, 0);
164 			return (RET_ERROR);
165 		}
166 		break;
167 	}
168 
169 	/*
170 	 * If not enough room, or the user has put a ceiling on the number of
171 	 * keys permitted in the page, split the page.  The split code will
172 	 * insert the key and data and unpin the current page.  If inserting
173 	 * into the offset array, shift the pointers up.
174 	 */
175 	nbytes = NBLEAFDBT(key->size, data->size);
176 	if (h->upper - h->lower < nbytes + sizeof(index_t)) {
177 		if ((status = __bt_split(t, h, key,
178 		    data, dflags, nbytes, index)) != RET_SUCCESS)
179 			return (status);
180 		goto success;
181 	}
182 
183 	if (index < (nxtindex = NEXTINDEX(h)))
184 		bcopy(h->linp + index, h->linp + index + 1,
185 		    (nxtindex - index) * sizeof(index_t));
186 	h->lower += sizeof(index_t);
187 
188 	h->linp[index] = h->upper -= nbytes;
189 	dest = (char *)h + h->upper;
190 	WR_BLEAF(dest, key, data, dflags);
191 
192 	if (t->bt_order == NOT)
193 		if (h->nextpg == P_INVALID) {
194 			if (index == NEXTINDEX(h) - 1) {
195 				t->bt_order = FORWARD;
196 				t->bt_last.index = index;
197 				t->bt_last.pgno = h->pgno;
198 			}
199 		} else if (h->prevpg == P_INVALID) {
200 			if (index == 0) {
201 				t->bt_order = BACK;
202 				t->bt_last.index = 0;
203 				t->bt_last.pgno = h->pgno;
204 			}
205 		}
206 
207 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
208 
209 success:
210 	if (flags == R_SETCURSOR) {
211 		t->bt_bcursor.pgno = e->page->pgno;
212 		t->bt_bcursor.index = e->index;
213 	}
214 	SET(t, BTF_MODIFIED);
215 	return (RET_SUCCESS);
216 }
217 
218 #ifdef STATISTICS
219 u_long bt_cache_hit, bt_cache_miss;
220 #endif
221 
222 /*
223  * BT_FAST -- Do a quick check for sorted data.
224  *
225  * Parameters:
226  *	t:	tree
227  *	key:	key to insert
228  *
229  * Returns:
230  * 	EPG for new record or NULL if not found.
231  */
232 static EPG *
233 bt_fast(t, key, data, exactp)
234 	BTREE *t;
235 	const DBT *key, *data;
236 	int *exactp;
237 {
238 	EPG e;
239 	PAGE *h;
240 	size_t nbytes;
241 	int cmp;
242 
243 	if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
244 		t->bt_order = NOT;
245 		return (NULL);
246 	}
247 	e.page = h;
248 	e.index = t->bt_last.index;
249 
250 	/*
251 	 * If won't fit in this page or have too many keys in this page, have
252 	 * to search to get split stack.
253 	 */
254 	nbytes = NBLEAFDBT(key->size, data->size);
255 	if (h->upper - h->lower < nbytes + sizeof(index_t))
256 		goto miss;
257 
258 	if (t->bt_order == FORWARD) {
259 		if (e.page->nextpg != P_INVALID)
260 			goto miss;
261 		if (e.index != NEXTINDEX(h) - 1)
262 			goto miss;
263 		if ((cmp = __bt_cmp(t, key, &e)) < 0)
264 			goto miss;
265 		t->bt_last.index = cmp ? ++e.index : e.index;
266 	} else {
267 		if (e.page->prevpg != P_INVALID)
268 			goto miss;
269 		if (e.index != 0)
270 			goto miss;
271 		if ((cmp = __bt_cmp(t, key, &e)) > 0)
272 			goto miss;
273 		t->bt_last.index = 0;
274 	}
275 	*exactp = cmp == 0;
276 #ifdef STATISTICS
277 	++bt_cache_hit;
278 #endif
279 	return (&e);
280 
281 miss:
282 #ifdef STATISTICS
283 	++bt_cache_miss;
284 #endif
285 	t->bt_order = NOT;
286 	mpool_put(t->bt_mp, h, 0);
287 	return (NULL);
288 }
289