2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: bt_put.c /main/3 1996/06/11 17:12:56 cde-hal $ */
25 * Copyright (c) 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #if defined(LIBC_SCCS) && !defined(lint)
61 static char sccsid[] = "@(#)bt_put.c 8.2 (Berkeley) 9/7/93";
62 #endif /* LIBC_SCCS and not lint */
64 #include <sys/types.h>
74 static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *));
77 * __BT_PUT -- Add a btree item to the tree.
80 * dbp: pointer to access method
86 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
87 * tree and R_NOOVERWRITE specified.
90 __bt_put(dbp, key, data, flags)
100 indx_t index, nxtindex;
103 int dflags, exact, status;
104 char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
108 /* Toss any page pinned across calls. */
109 if (t->bt_pinned != NULL) {
110 mpool_put(t->bt_mp, t->bt_pinned, 0);
116 if (!ISSET(t, B_SEQINIT))
118 if (ISSET(t, B_DELCRSR))
125 einval: errno = EINVAL;
129 if (ISSET(t, B_RDONLY)) {
135 * If the key/data won't fit on a page, store it on indirect pages.
136 * Only store the key on the overflow page if it's too big after the
137 * data is on an overflow page.
140 * If the insert fails later on, these pages aren't recovered.
143 if (key->size + data->size > t->bt_ovflsize) {
144 if (key->size > t->bt_ovflsize) {
145 storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR)
148 tkey.size = NOVFLSIZE;
149 memmove(kb, &pg, sizeof(pgno_t));
150 memmove(kb + sizeof(pgno_t),
151 &key->size, sizeof(size_t));
155 if (key->size + data->size > t->bt_ovflsize) {
156 if (__ovfl_put(t, data, &pg) == RET_ERROR)
159 tdata.size = NOVFLSIZE;
160 memmove(db, &pg, sizeof(pgno_t));
161 memmove(db + sizeof(pgno_t),
162 &data->size, sizeof(size_t));
166 if (key->size + data->size > t->bt_ovflsize)
170 /* Replace the cursor. */
171 if (flags == R_CURSOR) {
172 if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
174 index = t->bt_bcursor.index;
179 * Find the key to delete, or, the location at which to insert. Bt_fast
180 * and __bt_search pin the returned page.
182 if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
183 if ((e = __bt_search(t, key, &exact)) == NULL)
189 * Add the specified key/data pair to the tree. If an identical key
190 * is already in the tree, and R_NOOVERWRITE is set, an error is
191 * returned. If R_NOOVERWRITE is not set, the key is either added (if
192 * duplicates are permitted) or an error is returned.
194 * Pages are split as required.
201 * One special case is if the cursor references the record and
202 * it's been flagged for deletion. Then, we delete the record,
203 * leaving the cursor there -- this means that the inserted
204 * record will not be seen in a cursor scan.
206 if (ISSET(t, B_DELCRSR) && t->bt_bcursor.pgno == h->pgno &&
207 t->bt_bcursor.index == index) {
211 mpool_put(t->bt_mp, h, 0);
212 return (RET_SPECIAL);
214 if (!exact || !ISSET(t, B_NODUPS))
216 delete: if (__bt_dleaf(t, h, index) == RET_ERROR) {
217 mpool_put(t->bt_mp, h, 0);
224 * If not enough room, or the user has put a ceiling on the number of
225 * keys permitted in the page, split the page. The split code will
226 * insert the key and data and unpin the current page. If inserting
227 * into the offset array, shift the pointers up.
229 nbytes = NBLEAFDBT(key->size, data->size);
230 if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
231 if ((status = __bt_split(t, h, key,
232 data, dflags, nbytes, index)) != RET_SUCCESS)
237 if (index < (nxtindex = NEXTINDEX(h)))
238 memmove(h->linp + index + 1, h->linp + index,
239 (nxtindex - index) * sizeof(indx_t));
240 h->lower += sizeof(indx_t);
242 h->linp[index] = h->upper -= nbytes;
243 dest = (char *)h + h->upper;
244 WR_BLEAF(dest, key, data, dflags);
246 if (t->bt_order == NOT)
247 if (h->nextpg == P_INVALID) {
248 if (index == NEXTINDEX(h) - 1) {
249 t->bt_order = FORWARD;
250 t->bt_last.index = index;
251 t->bt_last.pgno = h->pgno;
253 } else if (h->prevpg == P_INVALID) {
256 t->bt_last.index = 0;
257 t->bt_last.pgno = h->pgno;
261 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
264 if (flags == R_SETCURSOR) {
265 t->bt_bcursor.pgno = e->page->pgno;
266 t->bt_bcursor.index = e->index;
269 return (RET_SUCCESS);
273 u_long bt_cache_hit, bt_cache_miss;
277 * BT_FAST -- Do a quick check for sorted data.
284 * EPG for new record or NULL if not found.
287 bt_fast(t, key, data, exactp)
289 const DBT *key, *data;
297 if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
302 e.index = t->bt_last.index;
305 * If won't fit in this page or have too many keys in this page, have
306 * to search to get split stack.
308 nbytes = NBLEAFDBT(key->size, data->size);
309 if (h->upper - h->lower < nbytes + sizeof(indx_t))
312 if (t->bt_order == FORWARD) {
313 if (e.page->nextpg != P_INVALID)
315 if (e.index != NEXTINDEX(h) - 1)
317 if ((cmp = __bt_cmp(t, key, &e)) < 0)
319 t->bt_last.index = cmp ? ++e.index : e.index;
321 if (e.page->prevpg != P_INVALID)
325 if ((cmp = __bt_cmp(t, key, &e)) > 0)
327 t->bt_last.index = 0;
340 mpool_put(t->bt_mp, h, 0);