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