146134Smao /*- 246134Smao * Copyright (c) 1990 The Regents of the University of California. 346134Smao * All rights reserved. 446134Smao * 546134Smao * This code is derived from software contributed to Berkeley by 646134Smao * Mike Olson. 746134Smao * 846134Smao * %sccs.include.redist.c% 946134Smao */ 1046134Smao 1146134Smao #if defined(LIBC_SCCS) && !defined(lint) 12*51106Sbostic static char sccsid[] = "@(#)bt_split.c 5.4 (Berkeley) 09/12/91"; 1346134Smao #endif /* LIBC_SCCS and not lint */ 1446134Smao 1546134Smao #include <sys/types.h> 1650989Sbostic #define __DBINTERFACE_PRIVATE 1746134Smao #include <db.h> 1850989Sbostic #include <limits.h> 1950989Sbostic #include <stdio.h> 2046561Sbostic #include <stdlib.h> 2146561Sbostic #include <string.h> 2246134Smao #include "btree.h" 2346134Smao 2450989Sbostic static int bt_preserve __P((BTREE *, pgno_t)); 2550989Sbostic static PAGE *bt_psplit __P((BTREE *, PAGE *, PAGE *, PAGE *, int *)); 2650989Sbostic static PAGE *bt_page __P((BTREE *, PAGE *, PAGE **, PAGE **, int *)); 2750989Sbostic static PAGE *bt_root __P((BTREE *, PAGE *, PAGE **, PAGE **, int *)); 2850989Sbostic static int bt_rroot __P((BTREE *, PAGE *, PAGE *, PAGE *)); 2950989Sbostic static int bt_broot __P((BTREE *, PAGE *, PAGE *, PAGE *)); 3050989Sbostic static recno_t rec_total __P((PAGE *)); 3150989Sbostic 3250989Sbostic #ifdef STATISTICS 3350989Sbostic u_long bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved; 3450989Sbostic #endif 3550989Sbostic 3646134Smao /* 3750989Sbostic * __BT_SPLIT -- Split the tree. 3846134Smao * 3950989Sbostic * Parameters: 4050989Sbostic * t: tree 4150989Sbostic * h: page to split 4250989Sbostic * key: key to insert 4350989Sbostic * data: data to insert 4450989Sbostic * flags: BIGKEY/BIGDATA flags 4550989Sbostic * nbytes: length of insertion 4650989Sbostic * skip: index to leave open 4746134Smao * 4850989Sbostic * Returns: 4950989Sbostic * RET_ERROR, RET_SUCCESS 5046134Smao */ 5146134Smao int 5250989Sbostic __bt_split(t, h, key, data, flags, nbytes, skip) 5350989Sbostic BTREE *t; 5450989Sbostic PAGE *h; 5550989Sbostic const DBT *key, *data; 5650989Sbostic u_long flags; 5750989Sbostic size_t nbytes; 5850989Sbostic int skip; 5946134Smao { 6050989Sbostic BINTERNAL *bi; 6150989Sbostic BLEAF *bl; 6250989Sbostic DBT a, b; 6350989Sbostic EPGNO *parent; 6450989Sbostic PAGE *l, *r, *lchild, *rchild; 6550989Sbostic index_t nxtindex; 6650989Sbostic size_t nksize; 6750989Sbostic int nosplit; 6850989Sbostic char *dest; 6946134Smao 7046134Smao /* 7150989Sbostic * Split the page into two pages, l and r. The split routines return 7250989Sbostic * a pointer to the page into which the key should be inserted and skip 7350989Sbostic * set to the offset which should be used. Additionally, l and r are 7450989Sbostic * pinned. 7546134Smao */ 7650989Sbostic h = h->pgno == P_ROOT ? 7750989Sbostic bt_root(t, h, &l, &r, &skip) : bt_page(t, h, &l, &r, &skip); 7850989Sbostic if (h == NULL) 7946134Smao return (RET_ERROR); 8046134Smao 8150989Sbostic /* 8250989Sbostic * Grab the space and insert the [rb]leaf structure. Always a [rb]leaf 8350989Sbostic * structure since key inserts always cause a leaf page to split first. 8450989Sbostic */ 8550989Sbostic h->linp[skip] = h->upper -= nbytes; 8650989Sbostic dest = (char *)h + h->upper; 87*51106Sbostic if (ISSET(t, BTF_RECNO)) 8850989Sbostic WR_RLEAF(dest, data, flags) 89*51106Sbostic else 9050989Sbostic WR_BLEAF(dest, key, data, flags) 9146134Smao 9246134Smao /* 9350989Sbostic * Now we walk the parent page stack -- a LIFO stack of the pages that 9450989Sbostic * were traversed when we searched for the page that split. Each stack 9550989Sbostic * entry is a page number and a page index offset. The offset is for 9650989Sbostic * the page traversed on the search. We've just split a page, so we 9750989Sbostic * have to insert a new key into the parent page. 9850989Sbostic * 9950989Sbostic * If the insert into the parent page causes it to split, may have to 10050989Sbostic * continue splitting all the way up the tree. We stop if the root 10150989Sbostic * splits or the page inserted into didn't have to split to hold the 10250989Sbostic * new key. Some algorithms replace the key for the old page as well 10350989Sbostic * as the new page. We don't, as there's no reason to believe that the 10450989Sbostic * first key on the old page is any better than the key we have, and, 10550989Sbostic * in the case of a key being placed at index 0 causing the split, the 10650989Sbostic * key is unavailable. 10750989Sbostic * 10850989Sbostic * There are a maximum of 5 pages pinned at any time. We keep the left 10950989Sbostic * and right pages pinned while working on the parent. The 5 are the 11050989Sbostic * two children, left parent and right parent (when the parent splits) 11150989Sbostic * and the root page or the overflow key page when calling bt_preserve. 11250989Sbostic * This code must make sure that all pins are released other than the 11350989Sbostic * root page or overflow page which is unlocked elsewhere. 11446134Smao */ 11550989Sbostic for (nosplit = 0; (parent = BT_POP(t)) != NULL;) { 11650989Sbostic lchild = l; 11750989Sbostic rchild = r; 11846134Smao 11950989Sbostic /* Get the parent page. */ 12050989Sbostic if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 12150989Sbostic goto err2; 12246134Smao 12350989Sbostic /* The new key goes ONE AFTER the index. */ 12450989Sbostic skip = parent->index + 1; 12546134Smao 12650989Sbostic /* 12750989Sbostic * Calculate the space needed on the parent page. 12850989Sbostic * 129*51106Sbostic * Space hack when inserting into BINTERNAL pages. Only need to 13050989Sbostic * retain the number of bytes that will distinguish between the 131*51106Sbostic * new entry and the LAST entry on the page to its left. If the 132*51106Sbostic * keys compare equal, retain the entire key. Note, we don't 133*51106Sbostic * touch overflow keys and the entire key must be retained for 134*51106Sbostic * the next-to-leftmost key on the leftmost page of each level, 135*51106Sbostic * or the search will fail. 13650989Sbostic */ 13750989Sbostic switch (rchild->flags & P_TYPE) { 13850989Sbostic case P_BINTERNAL: 13950989Sbostic bi = GETBINTERNAL(rchild, 0); 14050989Sbostic nbytes = NBINTERNAL(bi->ksize); 14150989Sbostic if (t->bt_pfx && (h->prevpg != P_INVALID || skip > 1) && 14250989Sbostic !(bi->flags & P_BIGKEY)) { 14350989Sbostic BINTERNAL *tbi; 14450989Sbostic tbi = 14550989Sbostic GETBINTERNAL(lchild, NEXTINDEX(lchild) - 1); 14650989Sbostic a.size = tbi->ksize; 14750989Sbostic a.data = tbi->bytes; 14850989Sbostic b.size = bi->ksize; 14950989Sbostic b.data = bi->bytes; 15050989Sbostic goto prefix; 15146134Smao } 15250989Sbostic break; 15350989Sbostic case P_BLEAF: 15450989Sbostic bl = GETBLEAF(rchild, 0); 15550989Sbostic nbytes = NBLEAF(bl); 15650989Sbostic if (t->bt_pfx && (h->prevpg != P_INVALID || skip > 1) && 15750989Sbostic !(bl->flags & P_BIGKEY)) { 15850989Sbostic BLEAF *tbl; 15950989Sbostic size_t n; 16050989Sbostic 16150989Sbostic tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1); 16250989Sbostic a.size = tbl->ksize; 16350989Sbostic a.data = tbl->bytes; 16450989Sbostic b.size = bl->ksize; 16550989Sbostic b.data = bl->bytes; 16650989Sbostic prefix: nksize = t->bt_pfx(&a, &b); 16750989Sbostic n = NBINTERNAL(nksize); 16850989Sbostic if (n < nbytes) { 16950989Sbostic #ifdef STATISTICS 17050989Sbostic bt_pfxsaved += nbytes - n; 17150989Sbostic #endif 17250989Sbostic nbytes = n; 17350989Sbostic } else 17450989Sbostic nksize = 0; 17550989Sbostic } else 17650989Sbostic nksize = 0; 17750989Sbostic break; 17850989Sbostic case P_RINTERNAL: 17950989Sbostic case P_RLEAF: 18050989Sbostic nbytes = NRINTERNAL; 18150989Sbostic break; 18250989Sbostic } 18350989Sbostic 18450989Sbostic /* Split the parent page if necessary or shift the indices. */ 18550989Sbostic if (h->upper - h->lower < nbytes + sizeof(index_t)) { 18650989Sbostic h = h->pgno == P_ROOT ? 18750989Sbostic bt_root(t, h, &l, &r, &skip) : 18850989Sbostic bt_page(t, h, &l, &r, &skip); 18950989Sbostic if (h == NULL) 19050989Sbostic goto err1; 19146134Smao } else { 19250989Sbostic if (skip < (nxtindex = NEXTINDEX(h))) 19350989Sbostic bcopy(h->linp + skip, h->linp + skip + 1, 19450989Sbostic (nxtindex - skip) * sizeof(index_t)); 19550989Sbostic h->lower += sizeof(index_t); 19650989Sbostic nosplit = 1; 19746134Smao } 19846134Smao 19950989Sbostic /* Insert the key into the parent page. */ 20050989Sbostic switch(rchild->flags & P_TYPE) { 20150989Sbostic case P_BINTERNAL: 20250989Sbostic h->linp[skip] = h->upper -= nbytes; 20350989Sbostic dest = (char *)h + h->linp[skip]; 20450989Sbostic bcopy(bi, dest, nbytes); 20550989Sbostic if (nksize) 20650989Sbostic ((BINTERNAL *)dest)->ksize = nksize; 20750989Sbostic ((BINTERNAL *)dest)->pgno = rchild->pgno; 20850989Sbostic break; 20950989Sbostic case P_BLEAF: 21050989Sbostic h->linp[skip] = h->upper -= nbytes; 21150989Sbostic dest = (char *)h + h->linp[skip]; 21250989Sbostic WR_BINTERNAL(dest, nksize ? nksize : bl->ksize, 213*51106Sbostic rchild->pgno, bl->flags & P_BIGKEY); 21450989Sbostic bcopy(bl->bytes, dest, nksize ? nksize : bl->ksize); 21550989Sbostic if (bl->flags & P_BIGKEY && 21650989Sbostic bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR) 21750989Sbostic goto err1; 21850989Sbostic break; 21950989Sbostic case P_RINTERNAL: 22050989Sbostic /* Update both left and right page counts. */ 22150989Sbostic h->linp[skip] = h->upper -= nbytes; 22250989Sbostic dest = (char *)h + h->linp[skip]; 22350989Sbostic ((RINTERNAL *)dest)->nrecs = rec_total(rchild); 22450989Sbostic ((RINTERNAL *)dest)->pgno = rchild->pgno; 22550989Sbostic dest = (char *)h + h->linp[skip - 1]; 22650989Sbostic ((RINTERNAL *)dest)->nrecs = rec_total(lchild); 22750989Sbostic ((RINTERNAL *)dest)->pgno = lchild->pgno; 22850989Sbostic break; 22950989Sbostic case P_RLEAF: 23050989Sbostic /* Update both left and right page counts. */ 23150989Sbostic h->linp[skip] = h->upper -= nbytes; 23250989Sbostic dest = (char *)h + h->linp[skip]; 23350989Sbostic ((RINTERNAL *)dest)->nrecs = NEXTINDEX(rchild); 23450989Sbostic ((RINTERNAL *)dest)->pgno = rchild->pgno; 23550989Sbostic dest = (char *)h + h->linp[skip - 1]; 23650989Sbostic ((RINTERNAL *)dest)->nrecs = NEXTINDEX(lchild); 23750989Sbostic ((RINTERNAL *)dest)->pgno = lchild->pgno; 23850989Sbostic break; 23950989Sbostic } 24046134Smao 24150989Sbostic /* Unpin the held pages. */ 24250989Sbostic if (nosplit) { 24350989Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 24450989Sbostic break; 24550989Sbostic } 24650989Sbostic mpool_put(t->bt_mp, lchild, MPOOL_DIRTY); 24750989Sbostic mpool_put(t->bt_mp, rchild, MPOOL_DIRTY); 24850989Sbostic } 24946134Smao 25050989Sbostic /* Unpin the held pages. */ 25150989Sbostic mpool_put(t->bt_mp, l, MPOOL_DIRTY); 25250989Sbostic mpool_put(t->bt_mp, r, MPOOL_DIRTY); 25350989Sbostic 254*51106Sbostic /* Clear any pages left on the stack. */ 255*51106Sbostic BT_CLR(t); 25650989Sbostic return (RET_SUCCESS); 25746134Smao 25846134Smao /* 25950989Sbostic * If something fails in the above loop we were already walking back 26050989Sbostic * up the tree and the tree is now inconsistent. Nothing much we can 26150989Sbostic * do about it but release any memory we're holding. 26246134Smao */ 26350989Sbostic err1: mpool_put(t->bt_mp, lchild, MPOOL_DIRTY); 26450989Sbostic mpool_put(t->bt_mp, rchild, MPOOL_DIRTY); 26546134Smao 26650989Sbostic err2: mpool_put(t->bt_mp, l, 0); 26750989Sbostic mpool_put(t->bt_mp, r, 0); 26850989Sbostic __dbpanic(t->bt_dbp); 26950989Sbostic return (RET_ERROR); 27046134Smao } 27146134Smao 27246134Smao /* 27350989Sbostic * BT_PAGE -- Split a non-root page of a btree. 27446134Smao * 27550989Sbostic * Parameters: 27650989Sbostic * t: tree 27750989Sbostic * h: root page 27850989Sbostic * lp: pointer to left page pointer 27950989Sbostic * rp: pointer to right page pointer 28050989Sbostic * skip: pointer to index to leave open 28146134Smao * 28250989Sbostic * Returns: 28350989Sbostic * Pointer to page in which to insert or NULL on error. 28446134Smao */ 28550989Sbostic static PAGE * 28650989Sbostic bt_page(t, h, lp, rp, skip) 28750989Sbostic BTREE *t; 28850989Sbostic PAGE *h, **lp, **rp; 28950989Sbostic int *skip; 29046134Smao { 29150989Sbostic PAGE *l, *r, *tp; 29250989Sbostic pgno_t npg; 29346134Smao 29450989Sbostic #ifdef STATISTICS 29550989Sbostic ++bt_split; 29650989Sbostic #endif 29750989Sbostic /* Put the new right page for the split into place. */ 29850989Sbostic if ((r = mpool_new(t->bt_mp, &npg)) == NULL) 29950989Sbostic return (NULL); 30050989Sbostic r->pgno = npg; 30150989Sbostic r->lower = BTDATAOFF; 30250989Sbostic r->upper = t->bt_psize; 30350989Sbostic r->nextpg = h->nextpg; 30450989Sbostic r->prevpg = h->pgno; 30550989Sbostic r->flags = h->flags & P_TYPE; 30646134Smao 30750989Sbostic /* 30850989Sbostic * If we're splitting the last page on a level because we're appending 30950989Sbostic * a key to it (skip is NEXTINDEX()), it's likely that the data is 31050989Sbostic * sorted. Adding an empty page on the side of the level is less work 31150989Sbostic * and can push the fill factor much higher than normal. If we're 31250989Sbostic * wrong it's no big deal, we'll just do the split the right way next 31350989Sbostic * time. It may look like it's equally easy to do a similar hack for 31450989Sbostic * reverse sorted data, that is, split the tree left, but it's not. 31550989Sbostic * Don't even try. 31650989Sbostic */ 31750989Sbostic if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) { 31850989Sbostic #ifdef STATISTICS 31950989Sbostic ++bt_sortsplit; 32050989Sbostic #endif 32150989Sbostic h->nextpg = r->pgno; 32250989Sbostic r->lower = BTDATAOFF + sizeof(index_t); 32350989Sbostic *skip = 0; 32450989Sbostic *lp = h; 32550989Sbostic *rp = r; 32650989Sbostic return (r); 32750989Sbostic } 32846134Smao 32950989Sbostic /* Put the new left page for the split into place. */ 33050989Sbostic if ((l = malloc(t->bt_psize)) == NULL) { 33150989Sbostic mpool_put(t->bt_mp, r, 0); 33250989Sbostic return (NULL); 33350989Sbostic } 33450989Sbostic l->pgno = h->pgno; 33550989Sbostic l->nextpg = r->pgno; 33650989Sbostic l->prevpg = h->prevpg; 33750989Sbostic l->lower = BTDATAOFF; 33850989Sbostic l->upper = t->bt_psize; 33950989Sbostic l->flags = h->flags & P_TYPE; 34046134Smao 34150989Sbostic /* Fix up the previous pointer of the page after the split page. */ 34250989Sbostic if (h->nextpg != P_INVALID) { 34350989Sbostic if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) { 34450989Sbostic free(l); 34550989Sbostic /* XXX mpool_free(t->bt_mp, r->pgno); */ 34650989Sbostic return (NULL); 34746134Smao } 34850989Sbostic tp->prevpg = r->pgno; 34950989Sbostic mpool_put(t->bt_mp, tp, 0); 35050989Sbostic } 35146134Smao 35250989Sbostic /* 35350989Sbostic * Split right. The key/data pairs aren't sorted in the btree page so 35450989Sbostic * it's simpler to copy the data from the split page onto two new pages 35550989Sbostic * instead of copying half the data to the right page and compacting 35650989Sbostic * the left page in place. Since the left page can't change, we have 35750989Sbostic * to swap the original and the allocated left page after the split. 35850989Sbostic */ 35950989Sbostic tp = bt_psplit(t, h, l, r, skip); 36046134Smao 36150989Sbostic /* Move the new left page onto the old left page. */ 36250989Sbostic bcopy(l, h, t->bt_psize); 36350989Sbostic if (tp == l) 36450989Sbostic tp = h; 36550989Sbostic free(l); 36650989Sbostic 36750989Sbostic *lp = h; 36850989Sbostic *rp = r; 36950989Sbostic return (tp); 37046134Smao } 37146134Smao 37246134Smao /* 373*51106Sbostic * BT_ROOT -- Split the root page of a btree. 37446134Smao * 37550989Sbostic * Parameters: 37650989Sbostic * t: tree 37750989Sbostic * h: root page 37850989Sbostic * lp: pointer to left page pointer 37950989Sbostic * rp: pointer to right page pointer 38050989Sbostic * skip: pointer to index to leave open 38146134Smao * 38250989Sbostic * Returns: 38350989Sbostic * Pointer to page in which to insert or NULL on error. 38446134Smao */ 38550989Sbostic static PAGE * 38650989Sbostic bt_root(t, h, lp, rp, skip) 38750989Sbostic BTREE *t; 38850989Sbostic PAGE *h, **lp, **rp; 38950989Sbostic int *skip; 39046134Smao { 39150989Sbostic PAGE *l, *r, *tp; 39250989Sbostic pgno_t lnpg, rnpg; 39346134Smao 39450989Sbostic #ifdef STATISTICS 39550989Sbostic ++bt_split; 39650989Sbostic ++bt_rootsplit; 39750989Sbostic #endif 39850989Sbostic /* Put the new left and right pages for the split into place. */ 39950989Sbostic if ((l = mpool_new(t->bt_mp, &lnpg)) == NULL || 40050989Sbostic (r = mpool_new(t->bt_mp, &rnpg)) == NULL) 40150989Sbostic return (NULL); 40250989Sbostic l->pgno = lnpg; 40350989Sbostic r->pgno = rnpg; 40450989Sbostic l->nextpg = r->pgno; 40550989Sbostic r->prevpg = l->pgno; 40650989Sbostic l->prevpg = r->nextpg = P_INVALID; 40750989Sbostic l->lower = r->lower = BTDATAOFF; 40850989Sbostic l->upper = r->upper = t->bt_psize; 40950989Sbostic l->flags = r->flags = h->flags & P_TYPE; 41050989Sbostic 41150989Sbostic /* Split the root page. */ 41250989Sbostic tp = bt_psplit(t, h, l, r, skip); 41350989Sbostic 41450989Sbostic /* Make the root page look right. */ 41550989Sbostic if ((ISSET(t, BTF_RECNO) ? 41650989Sbostic bt_rroot(t, h, l, r) : bt_broot(t, h, l, r)) == RET_ERROR) 41750989Sbostic return (NULL); 41850989Sbostic 41950989Sbostic *lp = l; 42050989Sbostic *rp = r; 42150989Sbostic return (tp); 42246134Smao } 42346134Smao 42446134Smao /* 42550989Sbostic * BT_RROOT -- Fix up the recno root page after the split. 42646134Smao * 42750989Sbostic * Parameters: 42850989Sbostic * t: tree 42950989Sbostic * h: root page 43046134Smao * 43150989Sbostic * Returns: 43250989Sbostic * RET_ERROR, RET_SUCCESS 43346134Smao */ 43450989Sbostic static int 43550989Sbostic bt_rroot(t, h, l, r) 43650989Sbostic BTREE *t; 43750989Sbostic PAGE *h, *l, *r; 43846134Smao { 43950989Sbostic char *dest; 44046134Smao 44150989Sbostic /* Insert the left and right keys, set the header information. */ 44250989Sbostic h->linp[0] = h->upper = t->bt_psize - NRINTERNAL; 44350989Sbostic dest = (char *)h + h->upper; 44450989Sbostic WR_RINTERNAL(dest, 44550989Sbostic l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno); 44646134Smao 44750989Sbostic h->linp[1] = h->upper -= NRINTERNAL; 44850989Sbostic dest = (char *)h + h->upper; 44950989Sbostic WR_RINTERNAL(dest, 45050989Sbostic r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno); 45146134Smao 45250989Sbostic h->lower = BTDATAOFF + 2 * sizeof(index_t); 45346134Smao 45450989Sbostic /* Unpin the root page, set to recno internal page. */ 45550989Sbostic h->flags &= ~P_TYPE; 45650989Sbostic h->flags |= P_RINTERNAL; 45750989Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 45846134Smao 45950989Sbostic return (RET_SUCCESS); 46050989Sbostic } 46146134Smao 46250989Sbostic /* 46350989Sbostic * BT_BROOT -- Fix up the btree root page after the split. 46450989Sbostic * 46550989Sbostic * Parameters: 46650989Sbostic * t: tree 46750989Sbostic * h: root page 46850989Sbostic * 46950989Sbostic * Returns: 47050989Sbostic * RET_ERROR, RET_SUCCESS 47150989Sbostic */ 47250989Sbostic static int 47350989Sbostic bt_broot(t, h, l, r) 47450989Sbostic BTREE *t; 47550989Sbostic PAGE *h, *l, *r; 47650989Sbostic { 47750989Sbostic BINTERNAL *bi; 47850989Sbostic BLEAF *bl; 47950989Sbostic size_t nbytes; 48050989Sbostic char *dest; 48146134Smao 48246134Smao /* 48350989Sbostic * If the root page was a leaf page, change it into an internal page. 48450989Sbostic * We copy the key we split on (but not the key's data, in the case of 48550989Sbostic * a leaf page) to the new root page. If the key is on an overflow 48650989Sbostic * page, mark the overflow chain so it isn't deleted when the leaf copy 48750989Sbostic * of the key is deleted. 48850989Sbostic * 48950989Sbostic * The btree comparison code guarantees that the left-most key on any 49050989Sbostic * level of the tree is never used, so it doesn't need to be filled 49150989Sbostic * in. (This is not just convenience -- if the insert index is 0, we 49250989Sbostic * don't *have* a key to fill in.) The right key is available because 49350989Sbostic * the split code guarantees not to split on the skipped index. 49446134Smao */ 49550989Sbostic nbytes = LALIGN(sizeof(size_t) + sizeof(pgno_t) + sizeof(u_char)); 49650989Sbostic h->linp[0] = h->upper = t->bt_psize - nbytes; 49750989Sbostic dest = (char *)h + h->upper; 49850989Sbostic WR_BINTERNAL(dest, 0, l->pgno, 0); 49946134Smao 50050989Sbostic switch(h->flags & P_TYPE) { 50150989Sbostic case P_BLEAF: 50250989Sbostic bl = GETBLEAF(r, 0); 50350989Sbostic nbytes = NBINTERNAL(bl->ksize); 50450989Sbostic h->linp[1] = h->upper -= nbytes; 50550989Sbostic dest = (char *)h + h->upper; 50650989Sbostic WR_BINTERNAL(dest, bl->ksize, r->pgno, 0); 50750989Sbostic bcopy(bl->bytes, dest, bl->ksize); 50850989Sbostic 50950989Sbostic if (bl->flags & P_BIGKEY && 51050989Sbostic bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR) 51150989Sbostic return (RET_ERROR); 51250989Sbostic break; 51350989Sbostic case P_BINTERNAL: 51450989Sbostic bi = GETBINTERNAL(r, 0); 51550989Sbostic nbytes = NBINTERNAL(bi->ksize); 51650989Sbostic h->linp[1] = h->upper -= nbytes; 51750989Sbostic dest = (char *)h + h->upper; 51850989Sbostic bcopy(bi, dest, nbytes); 51950989Sbostic ((BINTERNAL *)dest)->pgno = r->pgno; 52050989Sbostic break; 52146134Smao } 52250989Sbostic h->lower = BTDATAOFF + 2 * sizeof(index_t); 52346134Smao 52450989Sbostic /* Unpin the root page, set to btree internal page. */ 52550989Sbostic h->flags &= ~P_TYPE; 52650989Sbostic h->flags |= P_BINTERNAL; 52750989Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 52846134Smao 52946134Smao return (RET_SUCCESS); 53046134Smao } 53146134Smao 53246134Smao /* 53350989Sbostic * BT_PSPLIT -- Do the real work of splitting the page. 53446134Smao * 53550989Sbostic * Parameters: 53650989Sbostic * t: tree 53750989Sbostic * h: page to be split 53850989Sbostic * l: page to put lower half of data 53950989Sbostic * r: page to put upper half of data 54050989Sbostic * skip: pointer to index to leave open 54146134Smao * 54250989Sbostic * Returns: 54350989Sbostic * Pointer to page in which to insert. 54446134Smao */ 54550989Sbostic static PAGE * 546*51106Sbostic bt_psplit(t, h, l, r, pskip) 54750989Sbostic BTREE *t; 54850989Sbostic PAGE *h, *l, *r; 549*51106Sbostic int *pskip; 55046134Smao { 55150989Sbostic BINTERNAL *bi; 55250989Sbostic BLEAF *bl; 55350989Sbostic RLEAF *rl; 55450989Sbostic EPGNO *c; 55550989Sbostic PAGE *rval; 556*51106Sbostic index_t half, skip; 55750989Sbostic size_t nbytes; 55850989Sbostic void *src; 55950989Sbostic int bigkeycnt, isbigkey, nxt, off, top; 56046134Smao 56146134Smao /* 56250989Sbostic * Split the data to the left and right pages. Leave the skip index 56350989Sbostic * open and guarantee that the split doesn't happen on that index (the 56450989Sbostic * right key must be available for the parent page). Additionally, 56550989Sbostic * make some effort not to split on an overflow key. This makes it 56650989Sbostic * faster to process internal pages and can save space since overflow 56750989Sbostic * keys used by internal pages are never deleted. 56846134Smao */ 56950989Sbostic bigkeycnt = 0; 570*51106Sbostic skip = *pskip; 57150989Sbostic half = (t->bt_psize - BTDATAOFF) / 2; 57250989Sbostic for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) { 573*51106Sbostic if (skip == off) 57450989Sbostic continue; 57550989Sbostic switch (h->flags & P_TYPE) { 57650989Sbostic case P_BINTERNAL: 57750989Sbostic src = bi = GETBINTERNAL(h, nxt); 57850989Sbostic nbytes = NBINTERNAL(bi->ksize); 57950989Sbostic isbigkey = bi->flags & P_BIGKEY; 58050989Sbostic break; 58150989Sbostic case P_BLEAF: 58250989Sbostic src = bl = GETBLEAF(h, nxt); 58350989Sbostic nbytes = NBLEAF(bl); 58450989Sbostic isbigkey = bl->flags & P_BIGKEY; 58550989Sbostic break; 58650989Sbostic case P_RINTERNAL: 58750989Sbostic src = GETRINTERNAL(h, nxt); 58850989Sbostic nbytes = NRINTERNAL; 58950989Sbostic isbigkey = 0; 59050989Sbostic break; 59150989Sbostic case P_RLEAF: 59250989Sbostic src = rl = GETRLEAF(h, nxt); 59350989Sbostic nbytes = NRLEAF(rl); 59450989Sbostic isbigkey = 0; 59550989Sbostic break; 59650989Sbostic } 59750989Sbostic ++nxt; 59850989Sbostic l->linp[off] = l->upper -= nbytes; 59950989Sbostic bcopy(src, (char *)l + l->upper, nbytes); 60046134Smao 60150989Sbostic /* There's no empirical justification for the '3'. */ 60250989Sbostic if (half < nbytes) 60350989Sbostic if (!isbigkey || bigkeycnt == 3) 60450989Sbostic break; 60550989Sbostic else 60650989Sbostic ++bigkeycnt; 60750989Sbostic else 60850989Sbostic half -= nbytes; 60950989Sbostic } 61050989Sbostic l->lower += (off + 1) * sizeof(index_t); 61146134Smao 61250989Sbostic /* 613*51106Sbostic * If splitting the page that the cursor was on, the cursor has to be 614*51106Sbostic * adjusted to point to the same record as before the split. If the 615*51106Sbostic * skipped slot and the cursor are both on the left page and the cursor 616*51106Sbostic * is on or past the skipped slot, the cursor is incremented by one. 617*51106Sbostic * If the skipped slot and the cursor are both on the right page and 618*51106Sbostic * the cursor is on or past the skipped slot, the cursor is incremented 619*51106Sbostic * by one. If the skipped slot and the cursor aren't on the same page, 620*51106Sbostic * the cursor isn't changed. Regardless of the relationship of the 621*51106Sbostic * skipped slot and the cursor, if the cursor is on the right page it 622*51106Sbostic * is decremented by the number of records split to the left page. 623*51106Sbostic * 624*51106Sbostic * Don't bother checking for the BTF_SEQINIT flag, the page number will 625*51106Sbostic * be P_INVALID. 62650989Sbostic */ 62750989Sbostic c = &t->bt_bcursor; 62850989Sbostic if (c->pgno == h->pgno) 629*51106Sbostic if (c->index < off) { /* left page */ 63050989Sbostic c->pgno = l->pgno; 631*51106Sbostic if (c->index >= skip) 632*51106Sbostic ++c->index; 633*51106Sbostic } else { /* right page */ 63450989Sbostic c->pgno = r->pgno; 635*51106Sbostic if (c->index >= skip && skip > off) 636*51106Sbostic ++c->index; 63750989Sbostic c->index -= off; 63846134Smao } 63946134Smao 64050989Sbostic /* 64150989Sbostic * Decide which page to return, and adjust the skip index if the 64250989Sbostic * to-be-inserted-upon page has changed. 64350989Sbostic */ 644*51106Sbostic if (skip > off) { 64550989Sbostic rval = r; 646*51106Sbostic *pskip -= off + 1; 64750989Sbostic } else 64850989Sbostic rval = l; 64946134Smao 65050989Sbostic for (off = 0; nxt < top; ++off) { 651*51106Sbostic if (skip == nxt) { 652*51106Sbostic skip = 0; 65350989Sbostic continue; 65446134Smao } 65550989Sbostic switch (h->flags & P_TYPE) { 65650989Sbostic case P_BINTERNAL: 65750989Sbostic src = bi = GETBINTERNAL(h, nxt); 65850989Sbostic nbytes = NBINTERNAL(bi->ksize); 65950989Sbostic break; 66050989Sbostic case P_BLEAF: 66150989Sbostic src = bl = GETBLEAF(h, nxt); 66250989Sbostic nbytes = NBLEAF(bl); 66350989Sbostic break; 66450989Sbostic case P_RINTERNAL: 66550989Sbostic src = GETRINTERNAL(h, nxt); 66650989Sbostic nbytes = NRINTERNAL; 66750989Sbostic break; 66850989Sbostic case P_RLEAF: 66950989Sbostic src = rl = GETRLEAF(h, nxt); 67050989Sbostic nbytes = NRLEAF(rl); 67150989Sbostic break; 67250989Sbostic } 67350989Sbostic ++nxt; 67450989Sbostic r->linp[off] = r->upper -= nbytes; 67550989Sbostic bcopy(src, (char *)r + r->upper, nbytes); 67650989Sbostic } 67750989Sbostic r->lower += off * sizeof(index_t); 67846134Smao 67950989Sbostic /* If the key is being appended to the page, adjust the index. */ 680*51106Sbostic if (skip == top) 68150989Sbostic r->lower += sizeof(index_t); 68246134Smao 68350989Sbostic return (rval); 68450989Sbostic } 68546134Smao 68650989Sbostic /* 68750989Sbostic * BT_PRESERVE -- Mark a chain of pages as used by an internal node. 68850989Sbostic * 68950989Sbostic * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the 69050989Sbostic * record that references them gets deleted. Chains pointed to by internal 69150989Sbostic * pages never get deleted. This routine marks a chain as pointed to by an 69250989Sbostic * internal page. 69350989Sbostic * 69450989Sbostic * Parameters: 69550989Sbostic * t: tree 69650989Sbostic * pg: page number of first page in the chain. 69750989Sbostic * 69850989Sbostic * Returns: 69950989Sbostic * RET_SUCCESS, RET_ERROR. 70050989Sbostic */ 70150989Sbostic static int 70250989Sbostic bt_preserve(t, pg) 70350989Sbostic BTREE *t; 70450989Sbostic pgno_t pg; 70550989Sbostic { 70650989Sbostic PAGE *h; 70746134Smao 70850989Sbostic if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 70950989Sbostic return (RET_ERROR); 71050989Sbostic h->flags |= P_PRESERVE; 71150989Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 71246134Smao return (RET_SUCCESS); 71346134Smao } 71450989Sbostic 71550989Sbostic /* 71650989Sbostic * REC_TOTAL -- Return the number of recno entries below a page. 71750989Sbostic * 71850989Sbostic * Parameters: 71950989Sbostic * h: page 72050989Sbostic * 72150989Sbostic * Returns: 72250989Sbostic * The number of recno entries below a page. 72350989Sbostic * 72450989Sbostic * XXX 72550989Sbostic * These values could be set by the bt_psplit routine. The problem is that the 72650989Sbostic * entry has to be popped off of the stack etc. or the values have to be passed 72750989Sbostic * all the way back to bt_split/bt_rroot and it's not very clean. 72850989Sbostic */ 72950989Sbostic static recno_t 73050989Sbostic rec_total(h) 73150989Sbostic PAGE *h; 73250989Sbostic { 73350989Sbostic recno_t recs; 73450989Sbostic index_t nxt, top; 73550989Sbostic 73650989Sbostic for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt) 73750989Sbostic recs += GETRINTERNAL(h, nxt)->nrecs; 73850989Sbostic return (recs); 73950989Sbostic } 740