]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
734aad71 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
e9ce8d39 | 7 | * |
734aad71 A |
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 | |
13 | * file. | |
14 | * | |
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 | |
e9ce8d39 A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
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. | |
e9ce8d39 A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1990, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * This code is derived from software contributed to Berkeley by | |
30 | * Mike Olson. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
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. | |
47 | * | |
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 | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | ||
62 | #include <sys/types.h> | |
63 | ||
64 | #include <errno.h> | |
65 | #include <stdio.h> | |
66 | #include <stdlib.h> | |
67 | #include <string.h> | |
68 | ||
69 | #include <db.h> | |
70 | #include "btree.h" | |
71 | ||
72 | static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *)); | |
73 | ||
74 | /* | |
75 | * __BT_PUT -- Add a btree item to the tree. | |
76 | * | |
77 | * Parameters: | |
78 | * dbp: pointer to access method | |
79 | * key: key | |
80 | * data: data | |
81 | * flag: R_NOOVERWRITE | |
82 | * | |
83 | * Returns: | |
84 | * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the | |
85 | * tree and R_NOOVERWRITE specified. | |
86 | */ | |
87 | int | |
88 | __bt_put(dbp, key, data, flags) | |
89 | const DB *dbp; | |
90 | DBT *key; | |
91 | const DBT *data; | |
92 | u_int flags; | |
93 | { | |
94 | BTREE *t; | |
95 | DBT tkey, tdata; | |
96 | EPG *e; | |
97 | PAGE *h; | |
98 | indx_t index, nxtindex; | |
99 | pgno_t pg; | |
100 | size_t nbytes; | |
101 | int dflags, exact, status; | |
102 | char *dest, db[NOVFLSIZE], kb[NOVFLSIZE]; | |
103 | ||
104 | t = dbp->internal; | |
105 | ||
106 | /* Toss any page pinned across calls. */ | |
107 | if (t->bt_pinned != NULL) { | |
108 | mpool_put(t->bt_mp, t->bt_pinned, 0); | |
109 | t->bt_pinned = NULL; | |
110 | } | |
111 | ||
112 | switch (flags) { | |
113 | case R_CURSOR: | |
114 | if (!ISSET(t, B_SEQINIT)) | |
115 | goto einval; | |
116 | if (ISSET(t, B_DELCRSR)) | |
117 | goto einval; | |
118 | break; | |
119 | case 0: | |
120 | case R_NOOVERWRITE: | |
121 | break; | |
122 | default: | |
123 | einval: errno = EINVAL; | |
124 | return (RET_ERROR); | |
125 | } | |
126 | ||
127 | if (ISSET(t, B_RDONLY)) { | |
128 | errno = EPERM; | |
129 | return (RET_ERROR); | |
130 | } | |
131 | ||
132 | /* | |
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. | |
136 | * | |
137 | * XXX | |
138 | * If the insert fails later on, these pages aren't recovered. | |
139 | */ | |
140 | dflags = 0; | |
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) | |
144 | return (RET_ERROR); | |
145 | tkey.data = kb; | |
146 | tkey.size = NOVFLSIZE; | |
147 | memmove(kb, &pg, sizeof(pgno_t)); | |
148 | memmove(kb + sizeof(pgno_t), | |
149 | &key->size, sizeof(size_t)); | |
150 | dflags |= P_BIGKEY; | |
151 | key = &tkey; | |
152 | } | |
153 | if (key->size + data->size > t->bt_ovflsize) { | |
154 | if (__ovfl_put(t, data, &pg) == RET_ERROR) | |
155 | return (RET_ERROR); | |
156 | tdata.data = db; | |
157 | tdata.size = NOVFLSIZE; | |
158 | memmove(db, &pg, sizeof(pgno_t)); | |
159 | memmove(db + sizeof(pgno_t), | |
160 | &data->size, sizeof(size_t)); | |
161 | dflags |= P_BIGDATA; | |
162 | data = &tdata; | |
163 | } | |
164 | if (key->size + data->size > t->bt_ovflsize) | |
165 | goto storekey; | |
166 | } | |
167 | ||
168 | /* Replace the cursor. */ | |
169 | if (flags == R_CURSOR) { | |
170 | if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL) | |
171 | return (RET_ERROR); | |
172 | index = t->bt_bcursor.index; | |
173 | goto delete; | |
174 | } | |
175 | ||
176 | /* | |
177 | * Find the key to delete, or, the location at which to insert. Bt_fast | |
178 | * and __bt_search pin the returned page. | |
179 | */ | |
180 | if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL) | |
181 | if ((e = __bt_search(t, key, &exact)) == NULL) | |
182 | return (RET_ERROR); | |
183 | h = e->page; | |
184 | index = e->index; | |
185 | ||
186 | /* | |
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. | |
191 | * | |
192 | * Pages are split as required. | |
193 | */ | |
194 | switch (flags) { | |
195 | case R_NOOVERWRITE: | |
196 | if (!exact) | |
197 | break; | |
198 | /* | |
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. | |
203 | */ | |
204 | if (ISSET(t, B_DELCRSR) && t->bt_bcursor.pgno == h->pgno && | |
205 | t->bt_bcursor.index == index) { | |
206 | CLR(t, B_DELCRSR); | |
207 | goto delete; | |
208 | } | |
209 | mpool_put(t->bt_mp, h, 0); | |
210 | return (RET_SPECIAL); | |
211 | default: | |
212 | if (!exact || !ISSET(t, B_NODUPS)) | |
213 | break; | |
214 | delete: if (__bt_dleaf(t, h, index) == RET_ERROR) { | |
215 | mpool_put(t->bt_mp, h, 0); | |
216 | return (RET_ERROR); | |
217 | } | |
218 | break; | |
219 | } | |
220 | ||
221 | /* | |
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. | |
226 | */ | |
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) | |
231 | return (status); | |
232 | goto success; | |
233 | } | |
234 | ||
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); | |
239 | ||
240 | h->linp[index] = h->upper -= nbytes; | |
241 | dest = (char *)h + h->upper; | |
242 | WR_BLEAF(dest, key, data, dflags); | |
243 | ||
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; | |
250 | } | |
251 | } else if (h->prevpg == P_INVALID) { | |
252 | if (index == 0) { | |
253 | t->bt_order = BACK; | |
254 | t->bt_last.index = 0; | |
255 | t->bt_last.pgno = h->pgno; | |
256 | } | |
257 | } | |
258 | ||
259 | mpool_put(t->bt_mp, h, MPOOL_DIRTY); | |
260 | ||
261 | success: | |
262 | if (flags == R_SETCURSOR) { | |
263 | t->bt_bcursor.pgno = e->page->pgno; | |
264 | t->bt_bcursor.index = e->index; | |
265 | } | |
266 | SET(t, B_MODIFIED); | |
267 | return (RET_SUCCESS); | |
268 | } | |
269 | ||
270 | #ifdef STATISTICS | |
271 | u_long bt_cache_hit, bt_cache_miss; | |
272 | #endif | |
273 | ||
274 | /* | |
275 | * BT_FAST -- Do a quick check for sorted data. | |
276 | * | |
277 | * Parameters: | |
278 | * t: tree | |
279 | * key: key to insert | |
280 | * | |
281 | * Returns: | |
282 | * EPG for new record or NULL if not found. | |
283 | */ | |
284 | static EPG * | |
285 | bt_fast(t, key, data, exactp) | |
286 | BTREE *t; | |
287 | const DBT *key, *data; | |
288 | int *exactp; | |
289 | { | |
290 | PAGE *h; | |
291 | size_t nbytes; | |
292 | int cmp; | |
293 | ||
294 | if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) { | |
295 | t->bt_order = NOT; | |
296 | return (NULL); | |
297 | } | |
298 | t->bt_cur.page = h; | |
299 | t->bt_cur.index = t->bt_last.index; | |
300 | ||
301 | /* | |
302 | * If won't fit in this page or have too many keys in this page, have | |
303 | * to search to get split stack. | |
304 | */ | |
305 | nbytes = NBLEAFDBT(key->size, data->size); | |
306 | if (h->upper - h->lower < nbytes + sizeof(indx_t)) | |
307 | goto miss; | |
308 | ||
309 | if (t->bt_order == FORWARD) { | |
310 | if (t->bt_cur.page->nextpg != P_INVALID) | |
311 | goto miss; | |
312 | if (t->bt_cur.index != NEXTINDEX(h) - 1) | |
313 | goto miss; | |
314 | if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0) | |
315 | goto miss; | |
316 | t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index; | |
317 | } else { | |
318 | if (t->bt_cur.page->prevpg != P_INVALID) | |
319 | goto miss; | |
320 | if (t->bt_cur.index != 0) | |
321 | goto miss; | |
322 | if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0) | |
323 | goto miss; | |
324 | t->bt_last.index = 0; | |
325 | } | |
326 | *exactp = cmp == 0; | |
327 | #ifdef STATISTICS | |
328 | ++bt_cache_hit; | |
329 | #endif | |
330 | return (&t->bt_cur); | |
331 | ||
332 | miss: | |
333 | #ifdef STATISTICS | |
334 | ++bt_cache_miss; | |
335 | #endif | |
336 | t->bt_order = NOT; | |
337 | mpool_put(t->bt_mp, h, 0); | |
338 | return (NULL); | |
339 | } |