bt_split.c revision 1.17.6.2 1 1.17.6.2 joerg /* $NetBSD: bt_split.c,v 1.17.6.2 2008/09/11 12:58:01 joerg Exp $ */
2 1.17.6.2 joerg
3 1.17.6.2 joerg /*-
4 1.17.6.2 joerg * Copyright (c) 1990, 1993, 1994
5 1.17.6.2 joerg * The Regents of the University of California. All rights reserved.
6 1.17.6.2 joerg *
7 1.17.6.2 joerg * This code is derived from software contributed to Berkeley by
8 1.17.6.2 joerg * Mike Olson.
9 1.17.6.2 joerg *
10 1.17.6.2 joerg * Redistribution and use in source and binary forms, with or without
11 1.17.6.2 joerg * modification, are permitted provided that the following conditions
12 1.17.6.2 joerg * are met:
13 1.17.6.2 joerg * 1. Redistributions of source code must retain the above copyright
14 1.17.6.2 joerg * notice, this list of conditions and the following disclaimer.
15 1.17.6.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
16 1.17.6.2 joerg * notice, this list of conditions and the following disclaimer in the
17 1.17.6.2 joerg * documentation and/or other materials provided with the distribution.
18 1.17.6.2 joerg * 3. Neither the name of the University nor the names of its contributors
19 1.17.6.2 joerg * may be used to endorse or promote products derived from this software
20 1.17.6.2 joerg * without specific prior written permission.
21 1.17.6.2 joerg *
22 1.17.6.2 joerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.17.6.2 joerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.17.6.2 joerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.17.6.2 joerg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.17.6.2 joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.17.6.2 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.17.6.2 joerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.17.6.2 joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.17.6.2 joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.17.6.2 joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.17.6.2 joerg * SUCH DAMAGE.
33 1.17.6.2 joerg */
34 1.17.6.2 joerg
35 1.17.6.2 joerg #if HAVE_NBTOOL_CONFIG_H
36 1.17.6.2 joerg #include "nbtool_config.h"
37 1.17.6.2 joerg #endif
38 1.17.6.2 joerg
39 1.17.6.2 joerg #include <sys/cdefs.h>
40 1.17.6.2 joerg __RCSID("$NetBSD: bt_split.c,v 1.17.6.2 2008/09/11 12:58:01 joerg Exp $");
41 1.17.6.2 joerg
42 1.17.6.2 joerg #include "namespace.h"
43 1.17.6.2 joerg #include <sys/types.h>
44 1.17.6.2 joerg
45 1.17.6.2 joerg #include <assert.h>
46 1.17.6.2 joerg #include <limits.h>
47 1.17.6.2 joerg #include <stdio.h>
48 1.17.6.2 joerg #include <stdlib.h>
49 1.17.6.2 joerg #include <string.h>
50 1.17.6.2 joerg
51 1.17.6.2 joerg #include <db.h>
52 1.17.6.2 joerg #include "btree.h"
53 1.17.6.2 joerg
54 1.17.6.2 joerg static int bt_broot(BTREE *, PAGE *, PAGE *, PAGE *);
55 1.17.6.2 joerg static PAGE *bt_page(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
56 1.17.6.2 joerg static int bt_preserve(BTREE *, pgno_t);
57 1.17.6.2 joerg static PAGE *bt_psplit(BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t);
58 1.17.6.2 joerg static PAGE *bt_root(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
59 1.17.6.2 joerg static int bt_rroot(BTREE *, PAGE *, PAGE *, PAGE *);
60 1.17.6.2 joerg static recno_t rec_total(PAGE *);
61 1.17.6.2 joerg
62 1.17.6.2 joerg #ifdef STATISTICS
63 1.17.6.2 joerg unsigned long bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved;
64 1.17.6.2 joerg #endif
65 1.17.6.2 joerg
66 1.17.6.2 joerg /*
67 1.17.6.2 joerg * __BT_SPLIT -- Split the tree.
68 1.17.6.2 joerg *
69 1.17.6.2 joerg * Parameters:
70 1.17.6.2 joerg * t: tree
71 1.17.6.2 joerg * sp: page to split
72 1.17.6.2 joerg * key: key to insert
73 1.17.6.2 joerg * data: data to insert
74 1.17.6.2 joerg * flags: BIGKEY/BIGDATA flags
75 1.17.6.2 joerg * ilen: insert length
76 1.17.6.2 joerg * skip: index to leave open
77 1.17.6.2 joerg *
78 1.17.6.2 joerg * Returns:
79 1.17.6.2 joerg * RET_ERROR, RET_SUCCESS
80 1.17.6.2 joerg */
81 1.17.6.2 joerg int
82 1.17.6.2 joerg __bt_split(BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags,
83 1.17.6.2 joerg size_t ilen, uint32_t argskip)
84 1.17.6.2 joerg {
85 1.17.6.2 joerg BINTERNAL *bi = NULL; /* pacify gcc */
86 1.17.6.2 joerg BLEAF *bl = NULL, *tbl; /* pacify gcc */
87 1.17.6.2 joerg DBT a, b;
88 1.17.6.2 joerg EPGNO *parent;
89 1.17.6.2 joerg PAGE *h, *l, *r, *lchild, *rchild;
90 1.17.6.2 joerg indx_t nxtindex;
91 1.17.6.2 joerg uint16_t skip;
92 1.17.6.2 joerg uint32_t n, nbytes, nksize = 0; /* pacify gcc */
93 1.17.6.2 joerg int parentsplit;
94 1.17.6.2 joerg char *dest;
95 1.17.6.2 joerg
96 1.17.6.2 joerg /*
97 1.17.6.2 joerg * Split the page into two pages, l and r. The split routines return
98 1.17.6.2 joerg * a pointer to the page into which the key should be inserted and with
99 1.17.6.2 joerg * skip set to the offset which should be used. Additionally, l and r
100 1.17.6.2 joerg * are pinned.
101 1.17.6.2 joerg */
102 1.17.6.2 joerg skip = argskip;
103 1.17.6.2 joerg h = sp->pgno == P_ROOT ?
104 1.17.6.2 joerg bt_root(t, sp, &l, &r, &skip, ilen) :
105 1.17.6.2 joerg bt_page(t, sp, &l, &r, &skip, ilen);
106 1.17.6.2 joerg if (h == NULL)
107 1.17.6.2 joerg return (RET_ERROR);
108 1.17.6.2 joerg
109 1.17.6.2 joerg /*
110 1.17.6.2 joerg * Insert the new key/data pair into the leaf page. (Key inserts
111 1.17.6.2 joerg * always cause a leaf page to split first.)
112 1.17.6.2 joerg */
113 1.17.6.2 joerg _DBFIT(ilen, indx_t);
114 1.17.6.2 joerg h->upper -= (indx_t)ilen;
115 1.17.6.2 joerg h->linp[skip] = h->upper;
116 1.17.6.2 joerg dest = (char *)(void *)h + h->upper;
117 1.17.6.2 joerg if (F_ISSET(t, R_RECNO))
118 1.17.6.2 joerg WR_RLEAF(dest, data, flags);
119 1.17.6.2 joerg else
120 1.17.6.2 joerg WR_BLEAF(dest, key, data, flags);
121 1.17.6.2 joerg
122 1.17.6.2 joerg /* If the root page was split, make it look right. */
123 1.17.6.2 joerg if (sp->pgno == P_ROOT &&
124 1.17.6.2 joerg (F_ISSET(t, R_RECNO) ?
125 1.17.6.2 joerg bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
126 1.17.6.2 joerg goto err2;
127 1.17.6.2 joerg
128 1.17.6.2 joerg /*
129 1.17.6.2 joerg * Now we walk the parent page stack -- a LIFO stack of the pages that
130 1.17.6.2 joerg * were traversed when we searched for the page that split. Each stack
131 1.17.6.2 joerg * entry is a page number and a page index offset. The offset is for
132 1.17.6.2 joerg * the page traversed on the search. We've just split a page, so we
133 1.17.6.2 joerg * have to insert a new key into the parent page.
134 1.17.6.2 joerg *
135 1.17.6.2 joerg * If the insert into the parent page causes it to split, may have to
136 1.17.6.2 joerg * continue splitting all the way up the tree. We stop if the root
137 1.17.6.2 joerg * splits or the page inserted into didn't have to split to hold the
138 1.17.6.2 joerg * new key. Some algorithms replace the key for the old page as well
139 1.17.6.2 joerg * as the new page. We don't, as there's no reason to believe that the
140 1.17.6.2 joerg * first key on the old page is any better than the key we have, and,
141 1.17.6.2 joerg * in the case of a key being placed at index 0 causing the split, the
142 1.17.6.2 joerg * key is unavailable.
143 1.17.6.2 joerg *
144 1.17.6.2 joerg * There are a maximum of 5 pages pinned at any time. We keep the left
145 1.17.6.2 joerg * and right pages pinned while working on the parent. The 5 are the
146 1.17.6.2 joerg * two children, left parent and right parent (when the parent splits)
147 1.17.6.2 joerg * and the root page or the overflow key page when calling bt_preserve.
148 1.17.6.2 joerg * This code must make sure that all pins are released other than the
149 1.17.6.2 joerg * root page or overflow page which is unlocked elsewhere.
150 1.17.6.2 joerg */
151 1.17.6.2 joerg while ((parent = BT_POP(t)) != NULL) {
152 1.17.6.2 joerg lchild = l;
153 1.17.6.2 joerg rchild = r;
154 1.17.6.2 joerg
155 1.17.6.2 joerg /* Get the parent page. */
156 1.17.6.2 joerg if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
157 1.17.6.2 joerg goto err2;
158 1.17.6.2 joerg
159 1.17.6.2 joerg /*
160 1.17.6.2 joerg * The new key goes ONE AFTER the index, because the split
161 1.17.6.2 joerg * was to the right.
162 1.17.6.2 joerg */
163 1.17.6.2 joerg skip = parent->index + 1;
164 1.17.6.2 joerg
165 1.17.6.2 joerg /*
166 1.17.6.2 joerg * Calculate the space needed on the parent page.
167 1.17.6.2 joerg *
168 1.17.6.2 joerg * Prefix trees: space hack when inserting into BINTERNAL
169 1.17.6.2 joerg * pages. Retain only what's needed to distinguish between
170 1.17.6.2 joerg * the new entry and the LAST entry on the page to its left.
171 1.17.6.2 joerg * If the keys compare equal, retain the entire key. Note,
172 1.17.6.2 joerg * we don't touch overflow keys, and the entire key must be
173 1.17.6.2 joerg * retained for the next-to-left most key on the leftmost
174 1.17.6.2 joerg * page of each level, or the search will fail. Applicable
175 1.17.6.2 joerg * ONLY to internal pages that have leaf pages as children.
176 1.17.6.2 joerg * Further reduction of the key between pairs of internal
177 1.17.6.2 joerg * pages loses too much information.
178 1.17.6.2 joerg */
179 1.17.6.2 joerg switch (rchild->flags & P_TYPE) {
180 1.17.6.2 joerg case P_BINTERNAL:
181 1.17.6.2 joerg bi = GETBINTERNAL(rchild, 0);
182 1.17.6.2 joerg nbytes = NBINTERNAL(bi->ksize);
183 1.17.6.2 joerg break;
184 1.17.6.2 joerg case P_BLEAF:
185 1.17.6.2 joerg bl = GETBLEAF(rchild, 0);
186 1.17.6.2 joerg nbytes = NBINTERNAL(bl->ksize);
187 1.17.6.2 joerg if (t->bt_pfx && !(bl->flags & P_BIGKEY) &&
188 1.17.6.2 joerg (h->prevpg != P_INVALID || skip > 1)) {
189 1.17.6.2 joerg size_t temp;
190 1.17.6.2 joerg tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1);
191 1.17.6.2 joerg a.size = tbl->ksize;
192 1.17.6.2 joerg a.data = tbl->bytes;
193 1.17.6.2 joerg b.size = bl->ksize;
194 1.17.6.2 joerg b.data = bl->bytes;
195 1.17.6.2 joerg temp = t->bt_pfx(&a, &b);
196 1.17.6.2 joerg _DBFIT(temp, uint32_t);
197 1.17.6.2 joerg nksize = (uint32_t)temp;
198 1.17.6.2 joerg n = NBINTERNAL(nksize);
199 1.17.6.2 joerg if (n < nbytes) {
200 1.17.6.2 joerg #ifdef STATISTICS
201 1.17.6.2 joerg bt_pfxsaved += nbytes - n;
202 1.17.6.2 joerg #endif
203 1.17.6.2 joerg nbytes = n;
204 1.17.6.2 joerg } else
205 1.17.6.2 joerg nksize = 0;
206 1.17.6.2 joerg } else
207 1.17.6.2 joerg nksize = 0;
208 1.17.6.2 joerg break;
209 1.17.6.2 joerg case P_RINTERNAL:
210 1.17.6.2 joerg case P_RLEAF:
211 1.17.6.2 joerg nbytes = NRINTERNAL;
212 1.17.6.2 joerg break;
213 1.17.6.2 joerg default:
214 1.17.6.2 joerg abort();
215 1.17.6.2 joerg }
216 1.17.6.2 joerg
217 1.17.6.2 joerg /* Split the parent page if necessary or shift the indices. */
218 1.17.6.2 joerg if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
219 1.17.6.2 joerg sp = h;
220 1.17.6.2 joerg h = h->pgno == P_ROOT ?
221 1.17.6.2 joerg bt_root(t, h, &l, &r, &skip, nbytes) :
222 1.17.6.2 joerg bt_page(t, h, &l, &r, &skip, nbytes);
223 1.17.6.2 joerg if (h == NULL)
224 1.17.6.2 joerg goto err1;
225 1.17.6.2 joerg parentsplit = 1;
226 1.17.6.2 joerg } else {
227 1.17.6.2 joerg if (skip < (nxtindex = NEXTINDEX(h)))
228 1.17.6.2 joerg memmove(h->linp + skip + 1, h->linp + skip,
229 1.17.6.2 joerg (nxtindex - skip) * sizeof(indx_t));
230 1.17.6.2 joerg h->lower += sizeof(indx_t);
231 1.17.6.2 joerg parentsplit = 0;
232 1.17.6.2 joerg }
233 1.17.6.2 joerg
234 1.17.6.2 joerg /* Insert the key into the parent page. */
235 1.17.6.2 joerg switch (rchild->flags & P_TYPE) {
236 1.17.6.2 joerg case P_BINTERNAL:
237 1.17.6.2 joerg h->linp[skip] = h->upper -= nbytes;
238 1.17.6.2 joerg dest = (char *)(void *)h + h->linp[skip];
239 1.17.6.2 joerg memmove(dest, bi, nbytes);
240 1.17.6.2 joerg ((BINTERNAL *)(void *)dest)->pgno = rchild->pgno;
241 1.17.6.2 joerg break;
242 1.17.6.2 joerg case P_BLEAF:
243 1.17.6.2 joerg h->linp[skip] = h->upper -= nbytes;
244 1.17.6.2 joerg dest = (char *)(void *)h + h->linp[skip];
245 1.17.6.2 joerg WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
246 1.17.6.2 joerg rchild->pgno, bl->flags & P_BIGKEY);
247 1.17.6.2 joerg memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
248 1.17.6.2 joerg if (bl->flags & P_BIGKEY &&
249 1.17.6.2 joerg bt_preserve(t, *(pgno_t *)(void *)bl->bytes) ==
250 1.17.6.2 joerg RET_ERROR)
251 1.17.6.2 joerg goto err1;
252 1.17.6.2 joerg break;
253 1.17.6.2 joerg case P_RINTERNAL:
254 1.17.6.2 joerg /*
255 1.17.6.2 joerg * Update the left page count. If split
256 1.17.6.2 joerg * added at index 0, fix the correct page.
257 1.17.6.2 joerg */
258 1.17.6.2 joerg if (skip > 0)
259 1.17.6.2 joerg dest = (char *)(void *)h + h->linp[skip - 1];
260 1.17.6.2 joerg else
261 1.17.6.2 joerg dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1];
262 1.17.6.2 joerg ((RINTERNAL *)(void *)dest)->nrecs = rec_total(lchild);
263 1.17.6.2 joerg ((RINTERNAL *)(void *)dest)->pgno = lchild->pgno;
264 1.17.6.2 joerg
265 1.17.6.2 joerg /* Update the right page count. */
266 1.17.6.2 joerg h->linp[skip] = h->upper -= nbytes;
267 1.17.6.2 joerg dest = (char *)(void *)h + h->linp[skip];
268 1.17.6.2 joerg ((RINTERNAL *)(void *)dest)->nrecs = rec_total(rchild);
269 1.17.6.2 joerg ((RINTERNAL *)(void *)dest)->pgno = rchild->pgno;
270 1.17.6.2 joerg break;
271 1.17.6.2 joerg case P_RLEAF:
272 1.17.6.2 joerg /*
273 1.17.6.2 joerg * Update the left page count. If split
274 1.17.6.2 joerg * added at index 0, fix the correct page.
275 1.17.6.2 joerg */
276 1.17.6.2 joerg if (skip > 0)
277 1.17.6.2 joerg dest = (char *)(void *)h + h->linp[skip - 1];
278 1.17.6.2 joerg else
279 1.17.6.2 joerg dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1];
280 1.17.6.2 joerg ((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(lchild);
281 1.17.6.2 joerg ((RINTERNAL *)(void *)dest)->pgno = lchild->pgno;
282 1.17.6.2 joerg
283 1.17.6.2 joerg /* Update the right page count. */
284 1.17.6.2 joerg h->linp[skip] = h->upper -= nbytes;
285 1.17.6.2 joerg dest = (char *)(void *)h + h->linp[skip];
286 1.17.6.2 joerg ((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(rchild);
287 1.17.6.2 joerg ((RINTERNAL *)(void *)dest)->pgno = rchild->pgno;
288 1.17.6.2 joerg break;
289 1.17.6.2 joerg default:
290 1.17.6.2 joerg abort();
291 1.17.6.2 joerg }
292 1.17.6.2 joerg
293 1.17.6.2 joerg /* Unpin the held pages. */
294 1.17.6.2 joerg if (!parentsplit) {
295 1.17.6.2 joerg mpool_put(t->bt_mp, h, MPOOL_DIRTY);
296 1.17.6.2 joerg break;
297 1.17.6.2 joerg }
298 1.17.6.2 joerg
299 1.17.6.2 joerg /* If the root page was split, make it look right. */
300 1.17.6.2 joerg if (sp->pgno == P_ROOT &&
301 1.17.6.2 joerg (F_ISSET(t, R_RECNO) ?
302 1.17.6.2 joerg bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
303 1.17.6.2 joerg goto err1;
304 1.17.6.2 joerg
305 1.17.6.2 joerg mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
306 1.17.6.2 joerg mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
307 1.17.6.2 joerg }
308 1.17.6.2 joerg
309 1.17.6.2 joerg /* Unpin the held pages. */
310 1.17.6.2 joerg mpool_put(t->bt_mp, l, MPOOL_DIRTY);
311 1.17.6.2 joerg mpool_put(t->bt_mp, r, MPOOL_DIRTY);
312 1.17.6.2 joerg
313 1.17.6.2 joerg /* Clear any pages left on the stack. */
314 1.17.6.2 joerg return (RET_SUCCESS);
315 1.17.6.2 joerg
316 1.17.6.2 joerg /*
317 1.17.6.2 joerg * If something fails in the above loop we were already walking back
318 1.17.6.2 joerg * up the tree and the tree is now inconsistent. Nothing much we can
319 1.17.6.2 joerg * do about it but release any memory we're holding.
320 1.17.6.2 joerg */
321 1.17.6.2 joerg err1: mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
322 1.17.6.2 joerg mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
323 1.17.6.2 joerg
324 1.17.6.2 joerg err2: mpool_put(t->bt_mp, l, 0);
325 1.17.6.2 joerg mpool_put(t->bt_mp, r, 0);
326 1.17.6.2 joerg __dbpanic(t->bt_dbp);
327 1.17.6.2 joerg return (RET_ERROR);
328 1.17.6.2 joerg }
329 1.17.6.2 joerg
330 1.17.6.2 joerg /*
331 1.17.6.2 joerg * BT_PAGE -- Split a non-root page of a btree.
332 1.17.6.2 joerg *
333 1.17.6.2 joerg * Parameters:
334 1.17.6.2 joerg * t: tree
335 1.17.6.2 joerg * h: root page
336 1.17.6.2 joerg * lp: pointer to left page pointer
337 1.17.6.2 joerg * rp: pointer to right page pointer
338 1.17.6.2 joerg * skip: pointer to index to leave open
339 1.17.6.2 joerg * ilen: insert length
340 1.17.6.2 joerg *
341 1.17.6.2 joerg * Returns:
342 1.17.6.2 joerg * Pointer to page in which to insert or NULL on error.
343 1.17.6.2 joerg */
344 1.17.6.2 joerg static PAGE *
345 1.17.6.2 joerg bt_page(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
346 1.17.6.2 joerg {
347 1.17.6.2 joerg PAGE *l, *r, *tp;
348 1.17.6.2 joerg pgno_t npg;
349 1.17.6.2 joerg
350 1.17.6.2 joerg #ifdef STATISTICS
351 1.17.6.2 joerg ++bt_split;
352 1.17.6.2 joerg #endif
353 1.17.6.2 joerg /* Put the new right page for the split into place. */
354 1.17.6.2 joerg if ((r = __bt_new(t, &npg)) == NULL)
355 1.17.6.2 joerg return (NULL);
356 1.17.6.2 joerg r->pgno = npg;
357 1.17.6.2 joerg r->lower = BTDATAOFF;
358 1.17.6.2 joerg r->upper = t->bt_psize;
359 1.17.6.2 joerg r->nextpg = h->nextpg;
360 1.17.6.2 joerg r->prevpg = h->pgno;
361 1.17.6.2 joerg r->flags = h->flags & P_TYPE;
362 1.17.6.2 joerg
363 1.17.6.2 joerg /*
364 1.17.6.2 joerg * If we're splitting the last page on a level because we're appending
365 1.17.6.2 joerg * a key to it (skip is NEXTINDEX()), it's likely that the data is
366 1.17.6.2 joerg * sorted. Adding an empty page on the side of the level is less work
367 1.17.6.2 joerg * and can push the fill factor much higher than normal. If we're
368 1.17.6.2 joerg * wrong it's no big deal, we'll just do the split the right way next
369 1.17.6.2 joerg * time. It may look like it's equally easy to do a similar hack for
370 1.17.6.2 joerg * reverse sorted data, that is, split the tree left, but it's not.
371 1.17.6.2 joerg * Don't even try.
372 1.17.6.2 joerg */
373 1.17.6.2 joerg if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
374 1.17.6.2 joerg #ifdef STATISTICS
375 1.17.6.2 joerg ++bt_sortsplit;
376 1.17.6.2 joerg #endif
377 1.17.6.2 joerg h->nextpg = r->pgno;
378 1.17.6.2 joerg r->lower = BTDATAOFF + sizeof(indx_t);
379 1.17.6.2 joerg *skip = 0;
380 1.17.6.2 joerg *lp = h;
381 1.17.6.2 joerg *rp = r;
382 1.17.6.2 joerg return (r);
383 1.17.6.2 joerg }
384 1.17.6.2 joerg
385 1.17.6.2 joerg /* Put the new left page for the split into place. */
386 1.17.6.2 joerg if ((l = (PAGE *)malloc(t->bt_psize)) == NULL) {
387 1.17.6.2 joerg mpool_put(t->bt_mp, r, 0);
388 1.17.6.2 joerg return (NULL);
389 1.17.6.2 joerg }
390 1.17.6.2 joerg #ifdef PURIFY
391 1.17.6.2 joerg memset(l, 0xff, t->bt_psize);
392 1.17.6.2 joerg #endif
393 1.17.6.2 joerg l->pgno = h->pgno;
394 1.17.6.2 joerg l->nextpg = r->pgno;
395 1.17.6.2 joerg l->prevpg = h->prevpg;
396 1.17.6.2 joerg l->lower = BTDATAOFF;
397 1.17.6.2 joerg l->upper = t->bt_psize;
398 1.17.6.2 joerg l->flags = h->flags & P_TYPE;
399 1.17.6.2 joerg
400 1.17.6.2 joerg /* Fix up the previous pointer of the page after the split page. */
401 1.17.6.2 joerg if (h->nextpg != P_INVALID) {
402 1.17.6.2 joerg if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) {
403 1.17.6.2 joerg free(l);
404 1.17.6.2 joerg /* XXX mpool_free(t->bt_mp, r->pgno); */
405 1.17.6.2 joerg return (NULL);
406 1.17.6.2 joerg }
407 1.17.6.2 joerg tp->prevpg = r->pgno;
408 1.17.6.2 joerg mpool_put(t->bt_mp, tp, MPOOL_DIRTY);
409 1.17.6.2 joerg }
410 1.17.6.2 joerg
411 1.17.6.2 joerg /*
412 1.17.6.2 joerg * Split right. The key/data pairs aren't sorted in the btree page so
413 1.17.6.2 joerg * it's simpler to copy the data from the split page onto two new pages
414 1.17.6.2 joerg * instead of copying half the data to the right page and compacting
415 1.17.6.2 joerg * the left page in place. Since the left page can't change, we have
416 1.17.6.2 joerg * to swap the original and the allocated left page after the split.
417 1.17.6.2 joerg */
418 1.17.6.2 joerg tp = bt_psplit(t, h, l, r, skip, ilen);
419 1.17.6.2 joerg
420 1.17.6.2 joerg /* Move the new left page onto the old left page. */
421 1.17.6.2 joerg memmove(h, l, t->bt_psize);
422 1.17.6.2 joerg if (tp == l)
423 1.17.6.2 joerg tp = h;
424 1.17.6.2 joerg free(l);
425 1.17.6.2 joerg
426 1.17.6.2 joerg *lp = h;
427 1.17.6.2 joerg *rp = r;
428 1.17.6.2 joerg return (tp);
429 1.17.6.2 joerg }
430 1.17.6.2 joerg
431 1.17.6.2 joerg /*
432 1.17.6.2 joerg * BT_ROOT -- Split the root page of a btree.
433 1.17.6.2 joerg *
434 1.17.6.2 joerg * Parameters:
435 1.17.6.2 joerg * t: tree
436 1.17.6.2 joerg * h: root page
437 1.17.6.2 joerg * lp: pointer to left page pointer
438 1.17.6.2 joerg * rp: pointer to right page pointer
439 1.17.6.2 joerg * skip: pointer to index to leave open
440 1.17.6.2 joerg * ilen: insert length
441 1.17.6.2 joerg *
442 1.17.6.2 joerg * Returns:
443 1.17.6.2 joerg * Pointer to page in which to insert or NULL on error.
444 1.17.6.2 joerg */
445 1.17.6.2 joerg static PAGE *
446 1.17.6.2 joerg bt_root(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
447 1.17.6.2 joerg {
448 1.17.6.2 joerg PAGE *l, *r, *tp;
449 1.17.6.2 joerg pgno_t lnpg, rnpg;
450 1.17.6.2 joerg
451 1.17.6.2 joerg #ifdef STATISTICS
452 1.17.6.2 joerg ++bt_split;
453 1.17.6.2 joerg ++bt_rootsplit;
454 1.17.6.2 joerg #endif
455 1.17.6.2 joerg /* Put the new left and right pages for the split into place. */
456 1.17.6.2 joerg if ((l = __bt_new(t, &lnpg)) == NULL ||
457 1.17.6.2 joerg (r = __bt_new(t, &rnpg)) == NULL)
458 1.17.6.2 joerg return (NULL);
459 1.17.6.2 joerg l->pgno = lnpg;
460 1.17.6.2 joerg r->pgno = rnpg;
461 1.17.6.2 joerg l->nextpg = r->pgno;
462 1.17.6.2 joerg r->prevpg = l->pgno;
463 1.17.6.2 joerg l->prevpg = r->nextpg = P_INVALID;
464 1.17.6.2 joerg l->lower = r->lower = BTDATAOFF;
465 1.17.6.2 joerg l->upper = r->upper = t->bt_psize;
466 1.17.6.2 joerg l->flags = r->flags = h->flags & P_TYPE;
467 1.17.6.2 joerg
468 1.17.6.2 joerg /* Split the root page. */
469 1.17.6.2 joerg tp = bt_psplit(t, h, l, r, skip, ilen);
470 1.17.6.2 joerg
471 1.17.6.2 joerg *lp = l;
472 1.17.6.2 joerg *rp = r;
473 1.17.6.2 joerg return (tp);
474 1.17.6.2 joerg }
475 1.17.6.2 joerg
476 1.17.6.2 joerg /*
477 1.17.6.2 joerg * BT_RROOT -- Fix up the recno root page after it has been split.
478 1.17.6.2 joerg *
479 1.17.6.2 joerg * Parameters:
480 1.17.6.2 joerg * t: tree
481 1.17.6.2 joerg * h: root page
482 1.17.6.2 joerg * l: left page
483 1.17.6.2 joerg * r: right page
484 1.17.6.2 joerg *
485 1.17.6.2 joerg * Returns:
486 1.17.6.2 joerg * RET_ERROR, RET_SUCCESS
487 1.17.6.2 joerg */
488 1.17.6.2 joerg static int
489 1.17.6.2 joerg bt_rroot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
490 1.17.6.2 joerg {
491 1.17.6.2 joerg char *dest;
492 1.17.6.2 joerg uint32_t sz;
493 1.17.6.2 joerg size_t temp;
494 1.17.6.2 joerg
495 1.17.6.2 joerg temp = t->bt_psize - NRINTERNAL;
496 1.17.6.2 joerg _DBFIT(temp, uint32_t);
497 1.17.6.2 joerg sz = (uint32_t)temp;
498 1.17.6.2 joerg
499 1.17.6.2 joerg /* Insert the left and right keys, set the header information. */
500 1.17.6.2 joerg _DBFIT(sz, indx_t);
501 1.17.6.2 joerg h->linp[0] = h->upper = (indx_t)sz;
502 1.17.6.2 joerg dest = (char *)(void *)h + h->upper;
503 1.17.6.2 joerg WR_RINTERNAL(dest,
504 1.17.6.2 joerg l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno);
505 1.17.6.2 joerg
506 1.17.6.2 joerg h->linp[1] = h->upper -= NRINTERNAL;
507 1.17.6.2 joerg dest = (char *)(void *)h + h->upper;
508 1.17.6.2 joerg WR_RINTERNAL(dest,
509 1.17.6.2 joerg r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno);
510 1.17.6.2 joerg
511 1.17.6.2 joerg h->lower = BTDATAOFF + 2 * sizeof(indx_t);
512 1.17.6.2 joerg
513 1.17.6.2 joerg /* Unpin the root page, set to recno internal page. */
514 1.17.6.2 joerg h->flags &= ~P_TYPE;
515 1.17.6.2 joerg h->flags |= P_RINTERNAL;
516 1.17.6.2 joerg mpool_put(t->bt_mp, h, MPOOL_DIRTY);
517 1.17.6.2 joerg
518 1.17.6.2 joerg return (RET_SUCCESS);
519 1.17.6.2 joerg }
520 1.17.6.2 joerg
521 1.17.6.2 joerg /*
522 1.17.6.2 joerg * BT_BROOT -- Fix up the btree root page after it has been split.
523 1.17.6.2 joerg *
524 1.17.6.2 joerg * Parameters:
525 1.17.6.2 joerg * t: tree
526 1.17.6.2 joerg * h: root page
527 1.17.6.2 joerg * l: left page
528 1.17.6.2 joerg * r: right page
529 1.17.6.2 joerg *
530 1.17.6.2 joerg * Returns:
531 1.17.6.2 joerg * RET_ERROR, RET_SUCCESS
532 1.17.6.2 joerg */
533 1.17.6.2 joerg static int
534 1.17.6.2 joerg bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
535 1.17.6.2 joerg {
536 1.17.6.2 joerg BINTERNAL *bi = NULL; /* pacify gcc */
537 1.17.6.2 joerg BLEAF *bl;
538 1.17.6.2 joerg uint32_t nbytes;
539 1.17.6.2 joerg char *dest;
540 1.17.6.2 joerg
541 1.17.6.2 joerg /*
542 1.17.6.2 joerg * If the root page was a leaf page, change it into an internal page.
543 1.17.6.2 joerg * We copy the key we split on (but not the key's data, in the case of
544 1.17.6.2 joerg * a leaf page) to the new root page.
545 1.17.6.2 joerg *
546 1.17.6.2 joerg * The btree comparison code guarantees that the left-most key on any
547 1.17.6.2 joerg * level of the tree is never used, so it doesn't need to be filled in.
548 1.17.6.2 joerg */
549 1.17.6.2 joerg nbytes = NBINTERNAL(0);
550 1.17.6.2 joerg h->linp[0] = h->upper = t->bt_psize - nbytes;
551 1.17.6.2 joerg dest = (char *)(void *)h + h->upper;
552 1.17.6.2 joerg WR_BINTERNAL(dest, 0, l->pgno, 0);
553 1.17.6.2 joerg
554 1.17.6.2 joerg switch (h->flags & P_TYPE) {
555 1.17.6.2 joerg case P_BLEAF:
556 1.17.6.2 joerg bl = GETBLEAF(r, 0);
557 1.17.6.2 joerg nbytes = NBINTERNAL(bl->ksize);
558 1.17.6.2 joerg h->linp[1] = h->upper -= nbytes;
559 1.17.6.2 joerg dest = (char *)(void *)h + h->upper;
560 1.17.6.2 joerg WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
561 1.17.6.2 joerg memmove(dest, bl->bytes, bl->ksize);
562 1.17.6.2 joerg
563 1.17.6.2 joerg /*
564 1.17.6.2 joerg * If the key is on an overflow page, mark the overflow chain
565 1.17.6.2 joerg * so it isn't deleted when the leaf copy of the key is deleted.
566 1.17.6.2 joerg */
567 1.17.6.2 joerg if (bl->flags & P_BIGKEY &&
568 1.17.6.2 joerg bt_preserve(t, *(pgno_t *)(void *)bl->bytes) == RET_ERROR)
569 1.17.6.2 joerg return (RET_ERROR);
570 1.17.6.2 joerg break;
571 1.17.6.2 joerg case P_BINTERNAL:
572 1.17.6.2 joerg bi = GETBINTERNAL(r, 0);
573 1.17.6.2 joerg nbytes = NBINTERNAL(bi->ksize);
574 1.17.6.2 joerg h->linp[1] = h->upper -= nbytes;
575 1.17.6.2 joerg dest = (char *)(void *)h + h->upper;
576 1.17.6.2 joerg memmove(dest, bi, nbytes);
577 1.17.6.2 joerg ((BINTERNAL *)(void *)dest)->pgno = r->pgno;
578 1.17.6.2 joerg break;
579 1.17.6.2 joerg default:
580 1.17.6.2 joerg abort();
581 1.17.6.2 joerg }
582 1.17.6.2 joerg
583 1.17.6.2 joerg /* There are two keys on the page. */
584 1.17.6.2 joerg h->lower = BTDATAOFF + 2 * sizeof(indx_t);
585 1.17.6.2 joerg
586 1.17.6.2 joerg /* Unpin the root page, set to btree internal page. */
587 1.17.6.2 joerg h->flags &= ~P_TYPE;
588 1.17.6.2 joerg h->flags |= P_BINTERNAL;
589 1.17.6.2 joerg mpool_put(t->bt_mp, h, MPOOL_DIRTY);
590 1.17.6.2 joerg
591 1.17.6.2 joerg return (RET_SUCCESS);
592 1.17.6.2 joerg }
593 1.17.6.2 joerg
594 1.17.6.2 joerg /*
595 1.17.6.2 joerg * BT_PSPLIT -- Do the real work of splitting the page.
596 1.17.6.2 joerg *
597 1.17.6.2 joerg * Parameters:
598 1.17.6.2 joerg * t: tree
599 1.17.6.2 joerg * h: page to be split
600 1.17.6.2 joerg * l: page to put lower half of data
601 1.17.6.2 joerg * r: page to put upper half of data
602 1.17.6.2 joerg * pskip: pointer to index to leave open
603 1.17.6.2 joerg * ilen: insert length
604 1.17.6.2 joerg *
605 1.17.6.2 joerg * Returns:
606 1.17.6.2 joerg * Pointer to page in which to insert.
607 1.17.6.2 joerg */
608 1.17.6.2 joerg static PAGE *
609 1.17.6.2 joerg bt_psplit(BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen)
610 1.17.6.2 joerg {
611 1.17.6.2 joerg BINTERNAL *bi;
612 1.17.6.2 joerg BLEAF *bl;
613 1.17.6.2 joerg CURSOR *c;
614 1.17.6.2 joerg RLEAF *rl;
615 1.17.6.2 joerg PAGE *rval;
616 1.17.6.2 joerg void *src = NULL; /* pacify gcc */
617 1.17.6.2 joerg indx_t full, half, nxt, off, skip, top, used;
618 1.17.6.2 joerg uint32_t nbytes;
619 1.17.6.2 joerg size_t temp;
620 1.17.6.2 joerg int bigkeycnt, isbigkey;
621 1.17.6.2 joerg
622 1.17.6.2 joerg /*
623 1.17.6.2 joerg * Split the data to the left and right pages. Leave the skip index
624 1.17.6.2 joerg * open. Additionally, make some effort not to split on an overflow
625 1.17.6.2 joerg * key. This makes internal page processing faster and can save
626 1.17.6.2 joerg * space as overflow keys used by internal pages are never deleted.
627 1.17.6.2 joerg */
628 1.17.6.2 joerg bigkeycnt = 0;
629 1.17.6.2 joerg skip = *pskip;
630 1.17.6.2 joerg temp = t->bt_psize - BTDATAOFF;
631 1.17.6.2 joerg _DBFIT(temp, indx_t);
632 1.17.6.2 joerg full = (indx_t)temp;
633 1.17.6.2 joerg half = full / 2;
634 1.17.6.2 joerg used = 0;
635 1.17.6.2 joerg for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) {
636 1.17.6.2 joerg if (skip == off) {
637 1.17.6.2 joerg _DBFIT(ilen, uint32_t);
638 1.17.6.2 joerg nbytes = (uint32_t)ilen;
639 1.17.6.2 joerg isbigkey = 0; /* XXX: not really known. */
640 1.17.6.2 joerg } else
641 1.17.6.2 joerg switch (h->flags & P_TYPE) {
642 1.17.6.2 joerg case P_BINTERNAL:
643 1.17.6.2 joerg src = bi = GETBINTERNAL(h, nxt);
644 1.17.6.2 joerg nbytes = NBINTERNAL(bi->ksize);
645 1.17.6.2 joerg isbigkey = bi->flags & P_BIGKEY;
646 1.17.6.2 joerg break;
647 1.17.6.2 joerg case P_BLEAF:
648 1.17.6.2 joerg src = bl = GETBLEAF(h, nxt);
649 1.17.6.2 joerg nbytes = NBLEAF(bl);
650 1.17.6.2 joerg isbigkey = bl->flags & P_BIGKEY;
651 1.17.6.2 joerg break;
652 1.17.6.2 joerg case P_RINTERNAL:
653 1.17.6.2 joerg src = GETRINTERNAL(h, nxt);
654 1.17.6.2 joerg nbytes = NRINTERNAL;
655 1.17.6.2 joerg isbigkey = 0;
656 1.17.6.2 joerg break;
657 1.17.6.2 joerg case P_RLEAF:
658 1.17.6.2 joerg src = rl = GETRLEAF(h, nxt);
659 1.17.6.2 joerg nbytes = NRLEAF(rl);
660 1.17.6.2 joerg isbigkey = 0;
661 1.17.6.2 joerg break;
662 1.17.6.2 joerg default:
663 1.17.6.2 joerg abort();
664 1.17.6.2 joerg }
665 1.17.6.2 joerg
666 1.17.6.2 joerg /*
667 1.17.6.2 joerg * If the key/data pairs are substantial fractions of the max
668 1.17.6.2 joerg * possible size for the page, it's possible to get situations
669 1.17.6.2 joerg * where we decide to try and copy too much onto the left page.
670 1.17.6.2 joerg * Make sure that doesn't happen.
671 1.17.6.2 joerg */
672 1.17.6.2 joerg if ((skip <= off && used + nbytes + sizeof(indx_t) >= full) ||
673 1.17.6.2 joerg nxt == top - 1) {
674 1.17.6.2 joerg --off;
675 1.17.6.2 joerg break;
676 1.17.6.2 joerg }
677 1.17.6.2 joerg
678 1.17.6.2 joerg /* Copy the key/data pair, if not the skipped index. */
679 1.17.6.2 joerg if (skip != off) {
680 1.17.6.2 joerg ++nxt;
681 1.17.6.2 joerg
682 1.17.6.2 joerg l->linp[off] = l->upper -= nbytes;
683 1.17.6.2 joerg memmove((char *)(void *)l + l->upper, src, nbytes);
684 1.17.6.2 joerg }
685 1.17.6.2 joerg
686 1.17.6.2 joerg temp = nbytes + sizeof(indx_t);
687 1.17.6.2 joerg _DBFIT(temp, indx_t);
688 1.17.6.2 joerg used += (indx_t)temp;
689 1.17.6.2 joerg if (used >= half) {
690 1.17.6.2 joerg if (!isbigkey || bigkeycnt == 3)
691 1.17.6.2 joerg break;
692 1.17.6.2 joerg else
693 1.17.6.2 joerg ++bigkeycnt;
694 1.17.6.2 joerg }
695 1.17.6.2 joerg }
696 1.17.6.2 joerg
697 1.17.6.2 joerg /*
698 1.17.6.2 joerg * Off is the last offset that's valid for the left page.
699 1.17.6.2 joerg * Nxt is the first offset to be placed on the right page.
700 1.17.6.2 joerg */
701 1.17.6.2 joerg temp = (off + 1) * sizeof(indx_t);
702 1.17.6.2 joerg _DBFIT(temp, indx_t);
703 1.17.6.2 joerg l->lower += (indx_t)temp;
704 1.17.6.2 joerg
705 1.17.6.2 joerg /*
706 1.17.6.2 joerg * If splitting the page that the cursor was on, the cursor has to be
707 1.17.6.2 joerg * adjusted to point to the same record as before the split. If the
708 1.17.6.2 joerg * cursor is at or past the skipped slot, the cursor is incremented by
709 1.17.6.2 joerg * one. If the cursor is on the right page, it is decremented by the
710 1.17.6.2 joerg * number of records split to the left page.
711 1.17.6.2 joerg */
712 1.17.6.2 joerg c = &t->bt_cursor;
713 1.17.6.2 joerg if (F_ISSET(c, CURS_INIT) && c->pg.pgno == h->pgno) {
714 1.17.6.2 joerg if (c->pg.index >= skip)
715 1.17.6.2 joerg ++c->pg.index;
716 1.17.6.2 joerg if (c->pg.index < nxt) /* Left page. */
717 1.17.6.2 joerg c->pg.pgno = l->pgno;
718 1.17.6.2 joerg else { /* Right page. */
719 1.17.6.2 joerg c->pg.pgno = r->pgno;
720 1.17.6.2 joerg c->pg.index -= nxt;
721 1.17.6.2 joerg }
722 1.17.6.2 joerg }
723 1.17.6.2 joerg
724 1.17.6.2 joerg /*
725 1.17.6.2 joerg * If the skipped index was on the left page, just return that page.
726 1.17.6.2 joerg * Otherwise, adjust the skip index to reflect the new position on
727 1.17.6.2 joerg * the right page.
728 1.17.6.2 joerg */
729 1.17.6.2 joerg if (skip <= off) {
730 1.17.6.2 joerg skip = MAX_PAGE_OFFSET;
731 1.17.6.2 joerg rval = l;
732 1.17.6.2 joerg } else {
733 1.17.6.2 joerg rval = r;
734 1.17.6.2 joerg *pskip -= nxt;
735 1.17.6.2 joerg }
736 1.17.6.2 joerg
737 1.17.6.2 joerg for (off = 0; nxt < top; ++off) {
738 1.17.6.2 joerg if (skip == nxt) {
739 1.17.6.2 joerg ++off;
740 1.17.6.2 joerg skip = MAX_PAGE_OFFSET;
741 1.17.6.2 joerg }
742 1.17.6.2 joerg switch (h->flags & P_TYPE) {
743 1.17.6.2 joerg case P_BINTERNAL:
744 1.17.6.2 joerg src = bi = GETBINTERNAL(h, nxt);
745 1.17.6.2 joerg nbytes = NBINTERNAL(bi->ksize);
746 1.17.6.2 joerg break;
747 1.17.6.2 joerg case P_BLEAF:
748 1.17.6.2 joerg src = bl = GETBLEAF(h, nxt);
749 1.17.6.2 joerg nbytes = NBLEAF(bl);
750 1.17.6.2 joerg break;
751 1.17.6.2 joerg case P_RINTERNAL:
752 1.17.6.2 joerg src = GETRINTERNAL(h, nxt);
753 1.17.6.2 joerg nbytes = NRINTERNAL;
754 1.17.6.2 joerg break;
755 1.17.6.2 joerg case P_RLEAF:
756 1.17.6.2 joerg src = rl = GETRLEAF(h, nxt);
757 1.17.6.2 joerg nbytes = NRLEAF(rl);
758 1.17.6.2 joerg break;
759 1.17.6.2 joerg default:
760 1.17.6.2 joerg abort();
761 1.17.6.2 joerg }
762 1.17.6.2 joerg ++nxt;
763 1.17.6.2 joerg r->linp[off] = r->upper -= nbytes;
764 1.17.6.2 joerg memmove((char *)(void *)r + r->upper, src, nbytes);
765 1.17.6.2 joerg }
766 1.17.6.2 joerg temp = off * sizeof(indx_t);
767 1.17.6.2 joerg _DBFIT(temp, indx_t);
768 1.17.6.2 joerg r->lower += (indx_t)temp;
769 1.17.6.2 joerg
770 1.17.6.2 joerg /* If the key is being appended to the page, adjust the index. */
771 1.17.6.2 joerg if (skip == top)
772 1.17.6.2 joerg r->lower += sizeof(indx_t);
773 1.17.6.2 joerg
774 1.17.6.2 joerg return (rval);
775 1.17.6.2 joerg }
776 1.17.6.2 joerg
777 1.17.6.2 joerg /*
778 1.17.6.2 joerg * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
779 1.17.6.2 joerg *
780 1.17.6.2 joerg * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
781 1.17.6.2 joerg * record that references them gets deleted. Chains pointed to by internal
782 1.17.6.2 joerg * pages never get deleted. This routine marks a chain as pointed to by an
783 1.17.6.2 joerg * internal page.
784 1.17.6.2 joerg *
785 1.17.6.2 joerg * Parameters:
786 1.17.6.2 joerg * t: tree
787 1.17.6.2 joerg * pg: page number of first page in the chain.
788 1.17.6.2 joerg *
789 1.17.6.2 joerg * Returns:
790 1.17.6.2 joerg * RET_SUCCESS, RET_ERROR.
791 1.17.6.2 joerg */
792 1.17.6.2 joerg static int
793 1.17.6.2 joerg bt_preserve(BTREE *t, pgno_t pg)
794 1.17.6.2 joerg {
795 1.17.6.2 joerg PAGE *h;
796 1.17.6.2 joerg
797 1.17.6.2 joerg if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
798 1.17.6.2 joerg return (RET_ERROR);
799 1.17.6.2 joerg h->flags |= P_PRESERVE;
800 1.17.6.2 joerg mpool_put(t->bt_mp, h, MPOOL_DIRTY);
801 1.17.6.2 joerg return (RET_SUCCESS);
802 1.17.6.2 joerg }
803 1.17.6.2 joerg
804 1.17.6.2 joerg /*
805 1.17.6.2 joerg * REC_TOTAL -- Return the number of recno entries below a page.
806 1.17.6.2 joerg *
807 1.17.6.2 joerg * Parameters:
808 1.17.6.2 joerg * h: page
809 1.17.6.2 joerg *
810 1.17.6.2 joerg * Returns:
811 1.17.6.2 joerg * The number of recno entries below a page.
812 1.17.6.2 joerg *
813 1.17.6.2 joerg * XXX
814 1.17.6.2 joerg * These values could be set by the bt_psplit routine. The problem is that the
815 1.17.6.2 joerg * entry has to be popped off of the stack etc. or the values have to be passed
816 1.17.6.2 joerg * all the way back to bt_split/bt_rroot and it's not very clean.
817 1.17.6.2 joerg */
818 1.17.6.2 joerg static recno_t
819 1.17.6.2 joerg rec_total(PAGE *h)
820 1.17.6.2 joerg {
821 1.17.6.2 joerg recno_t recs;
822 1.17.6.2 joerg indx_t nxt, top;
823 1.17.6.2 joerg
824 1.17.6.2 joerg for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
825 1.17.6.2 joerg recs += GETRINTERNAL(h, nxt)->nrecs;
826 1.17.6.2 joerg return (recs);
827 1.17.6.2 joerg }
828