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