]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_put.c
c7f61c74e8af52054ac374ade2684b579dd56c86
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/types.h>
72 static EPG
*bt_fast
__P((BTREE
*, const DBT
*, const DBT
*, int *));
75 * __BT_PUT -- Add a btree item to the tree.
78 * dbp: pointer to access method
84 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
85 * tree and R_NOOVERWRITE specified.
88 __bt_put(dbp
, key
, data
, flags
)
98 indx_t index
, nxtindex
;
101 int dflags
, exact
, status
;
102 char *dest
, db
[NOVFLSIZE
], kb
[NOVFLSIZE
];
106 /* Toss any page pinned across calls. */
107 if (t
->bt_pinned
!= NULL
) {
108 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
114 if (!ISSET(t
, B_SEQINIT
))
116 if (ISSET(t
, B_DELCRSR
))
123 einval
: errno
= EINVAL
;
127 if (ISSET(t
, B_RDONLY
)) {
133 * If the key/data won't fit on a page, store it on indirect pages.
134 * Only store the key on the overflow page if it's too big after the
135 * data is on an overflow page.
138 * If the insert fails later on, these pages aren't recovered.
141 if (key
->size
+ data
->size
> t
->bt_ovflsize
) {
142 if (key
->size
> t
->bt_ovflsize
) {
143 storekey
: if (__ovfl_put(t
, key
, &pg
) == RET_ERROR
)
146 tkey
.size
= NOVFLSIZE
;
147 memmove(kb
, &pg
, sizeof(pgno_t
));
148 memmove(kb
+ sizeof(pgno_t
),
149 &key
->size
, sizeof(size_t));
153 if (key
->size
+ data
->size
> t
->bt_ovflsize
) {
154 if (__ovfl_put(t
, data
, &pg
) == RET_ERROR
)
157 tdata
.size
= NOVFLSIZE
;
158 memmove(db
, &pg
, sizeof(pgno_t
));
159 memmove(db
+ sizeof(pgno_t
),
160 &data
->size
, sizeof(size_t));
164 if (key
->size
+ data
->size
> t
->bt_ovflsize
)
168 /* Replace the cursor. */
169 if (flags
== R_CURSOR
) {
170 if ((h
= mpool_get(t
->bt_mp
, t
->bt_bcursor
.pgno
, 0)) == NULL
)
172 index
= t
->bt_bcursor
.index
;
177 * Find the key to delete, or, the location at which to insert. Bt_fast
178 * and __bt_search pin the returned page.
180 if (t
->bt_order
== NOT
|| (e
= bt_fast(t
, key
, data
, &exact
)) == NULL
)
181 if ((e
= __bt_search(t
, key
, &exact
)) == NULL
)
187 * Add the specified key/data pair to the tree. If an identical key
188 * is already in the tree, and R_NOOVERWRITE is set, an error is
189 * returned. If R_NOOVERWRITE is not set, the key is either added (if
190 * duplicates are permitted) or an error is returned.
192 * Pages are split as required.
199 * One special case is if the cursor references the record and
200 * it's been flagged for deletion. Then, we delete the record,
201 * leaving the cursor there -- this means that the inserted
202 * record will not be seen in a cursor scan.
204 if (ISSET(t
, B_DELCRSR
) && t
->bt_bcursor
.pgno
== h
->pgno
&&
205 t
->bt_bcursor
.index
== index
) {
209 mpool_put(t
->bt_mp
, h
, 0);
210 return (RET_SPECIAL
);
212 if (!exact
|| !ISSET(t
, B_NODUPS
))
214 delete: if (__bt_dleaf(t
, h
, index
) == RET_ERROR
) {
215 mpool_put(t
->bt_mp
, h
, 0);
222 * If not enough room, or the user has put a ceiling on the number of
223 * keys permitted in the page, split the page. The split code will
224 * insert the key and data and unpin the current page. If inserting
225 * into the offset array, shift the pointers up.
227 nbytes
= NBLEAFDBT(key
->size
, data
->size
);
228 if (h
->upper
- h
->lower
< nbytes
+ sizeof(indx_t
)) {
229 if ((status
= __bt_split(t
, h
, key
,
230 data
, dflags
, nbytes
, index
)) != RET_SUCCESS
)
235 if (index
< (nxtindex
= NEXTINDEX(h
)))
236 memmove(h
->linp
+ index
+ 1, h
->linp
+ index
,
237 (nxtindex
- index
) * sizeof(indx_t
));
238 h
->lower
+= sizeof(indx_t
);
240 h
->linp
[index
] = h
->upper
-= nbytes
;
241 dest
= (char *)h
+ h
->upper
;
242 WR_BLEAF(dest
, key
, data
, dflags
);
244 if (t
->bt_order
== NOT
)
245 if (h
->nextpg
== P_INVALID
) {
246 if (index
== NEXTINDEX(h
) - 1) {
247 t
->bt_order
= FORWARD
;
248 t
->bt_last
.index
= index
;
249 t
->bt_last
.pgno
= h
->pgno
;
251 } else if (h
->prevpg
== P_INVALID
) {
254 t
->bt_last
.index
= 0;
255 t
->bt_last
.pgno
= h
->pgno
;
259 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
262 if (flags
== R_SETCURSOR
) {
263 t
->bt_bcursor
.pgno
= e
->page
->pgno
;
264 t
->bt_bcursor
.index
= e
->index
;
267 return (RET_SUCCESS
);
271 u_long bt_cache_hit
, bt_cache_miss
;
275 * BT_FAST -- Do a quick check for sorted data.
282 * EPG for new record or NULL if not found.
285 bt_fast(t
, key
, data
, exactp
)
287 const DBT
*key
, *data
;
294 if ((h
= mpool_get(t
->bt_mp
, t
->bt_last
.pgno
, 0)) == NULL
) {
299 t
->bt_cur
.index
= t
->bt_last
.index
;
302 * If won't fit in this page or have too many keys in this page, have
303 * to search to get split stack.
305 nbytes
= NBLEAFDBT(key
->size
, data
->size
);
306 if (h
->upper
- h
->lower
< nbytes
+ sizeof(indx_t
))
309 if (t
->bt_order
== FORWARD
) {
310 if (t
->bt_cur
.page
->nextpg
!= P_INVALID
)
312 if (t
->bt_cur
.index
!= NEXTINDEX(h
) - 1)
314 if ((cmp
= __bt_cmp(t
, key
, &t
->bt_cur
)) < 0)
316 t
->bt_last
.index
= cmp
? ++t
->bt_cur
.index
: t
->bt_cur
.index
;
318 if (t
->bt_cur
.page
->prevpg
!= P_INVALID
)
320 if (t
->bt_cur
.index
!= 0)
322 if ((cmp
= __bt_cmp(t
, key
, &t
->bt_cur
)) > 0)
324 t
->bt_last
.index
= 0;
337 mpool_put(t
->bt_mp
, h
, 0);