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