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