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*56402Sbostic static char sccsid[] = "@(#)bt_split.c 5.6 (Berkeley) 10/03/92"; 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; 8751106Sbostic if (ISSET(t, BTF_RECNO)) 8850989Sbostic WR_RLEAF(dest, data, flags) 8951106Sbostic 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 * 12951106Sbostic * Space hack when inserting into BINTERNAL pages. Only need to 13050989Sbostic * retain the number of bytes that will distinguish between the 13151106Sbostic * new entry and the LAST entry on the page to its left. If the 13251106Sbostic * keys compare equal, retain the entire key. Note, we don't 13351106Sbostic * touch overflow keys and the entire key must be retained for 13451106Sbostic * the next-to-leftmost key on the leftmost page of each level, 13551106Sbostic * 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); 15556401Sbostic nbytes = NBINTERNAL(bl->ksize); 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, 21351106Sbostic 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 25451106Sbostic /* Clear any pages left on the stack. */ 25551106Sbostic 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 /* 37351106Sbostic * 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 48556401Sbostic * a leaf page) to the new root page. 48650989Sbostic * 48750989Sbostic * The btree comparison code guarantees that the left-most key on any 48850989Sbostic * level of the tree is never used, so it doesn't need to be filled 48950989Sbostic * in. (This is not just convenience -- if the insert index is 0, we 49050989Sbostic * don't *have* a key to fill in.) The right key is available because 49150989Sbostic * the split code guarantees not to split on the skipped index. 49246134Smao */ 49356401Sbostic nbytes = NBINTERNAL(0); 49450989Sbostic h->linp[0] = h->upper = t->bt_psize - nbytes; 49550989Sbostic dest = (char *)h + h->upper; 49650989Sbostic WR_BINTERNAL(dest, 0, l->pgno, 0); 49746134Smao 49850989Sbostic switch(h->flags & P_TYPE) { 49950989Sbostic case P_BLEAF: 50050989Sbostic bl = GETBLEAF(r, 0); 50150989Sbostic nbytes = NBINTERNAL(bl->ksize); 50250989Sbostic h->linp[1] = h->upper -= nbytes; 50350989Sbostic dest = (char *)h + h->upper; 50450989Sbostic WR_BINTERNAL(dest, bl->ksize, r->pgno, 0); 50550989Sbostic bcopy(bl->bytes, dest, bl->ksize); 50650989Sbostic 50756401Sbostic /* 50856401Sbostic * If the key is on an overflow page, mark the overflow chain 50956401Sbostic * so it isn't deleted when the leaf copy of the key is deleted. 51056401Sbostic */ 51150989Sbostic if (bl->flags & P_BIGKEY && 51250989Sbostic bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR) 51350989Sbostic return (RET_ERROR); 51450989Sbostic break; 51550989Sbostic case P_BINTERNAL: 51650989Sbostic bi = GETBINTERNAL(r, 0); 51750989Sbostic nbytes = NBINTERNAL(bi->ksize); 51850989Sbostic h->linp[1] = h->upper -= nbytes; 51950989Sbostic dest = (char *)h + h->upper; 52050989Sbostic bcopy(bi, dest, nbytes); 52150989Sbostic ((BINTERNAL *)dest)->pgno = r->pgno; 52250989Sbostic break; 52346134Smao } 52450989Sbostic h->lower = BTDATAOFF + 2 * sizeof(index_t); 52546134Smao 52650989Sbostic /* Unpin the root page, set to btree internal page. */ 52750989Sbostic h->flags &= ~P_TYPE; 52850989Sbostic h->flags |= P_BINTERNAL; 52950989Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 53046134Smao 53146134Smao return (RET_SUCCESS); 53246134Smao } 53346134Smao 53446134Smao /* 53550989Sbostic * BT_PSPLIT -- Do the real work of splitting the page. 53646134Smao * 53750989Sbostic * Parameters: 53850989Sbostic * t: tree 53950989Sbostic * h: page to be split 54050989Sbostic * l: page to put lower half of data 54150989Sbostic * r: page to put upper half of data 54250989Sbostic * skip: pointer to index to leave open 54346134Smao * 54450989Sbostic * Returns: 54550989Sbostic * Pointer to page in which to insert. 54646134Smao */ 54750989Sbostic static PAGE * 54851106Sbostic bt_psplit(t, h, l, r, pskip) 54950989Sbostic BTREE *t; 55050989Sbostic PAGE *h, *l, *r; 55151106Sbostic int *pskip; 55246134Smao { 55350989Sbostic BINTERNAL *bi; 55450989Sbostic BLEAF *bl; 55550989Sbostic RLEAF *rl; 55650989Sbostic EPGNO *c; 55750989Sbostic PAGE *rval; 55851106Sbostic index_t half, skip; 55950989Sbostic size_t nbytes; 56050989Sbostic void *src; 56150989Sbostic int bigkeycnt, isbigkey, nxt, off, top; 56246134Smao 56346134Smao /* 56450989Sbostic * Split the data to the left and right pages. Leave the skip index 56550989Sbostic * open and guarantee that the split doesn't happen on that index (the 56650989Sbostic * right key must be available for the parent page). Additionally, 56750989Sbostic * make some effort not to split on an overflow key. This makes it 56850989Sbostic * faster to process internal pages and can save space since overflow 56950989Sbostic * keys used by internal pages are never deleted. 57046134Smao */ 57150989Sbostic bigkeycnt = 0; 57251106Sbostic skip = *pskip; 57350989Sbostic half = (t->bt_psize - BTDATAOFF) / 2; 57450989Sbostic for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) { 57551106Sbostic if (skip == off) 57650989Sbostic continue; 57750989Sbostic switch (h->flags & P_TYPE) { 57850989Sbostic case P_BINTERNAL: 57950989Sbostic src = bi = GETBINTERNAL(h, nxt); 58050989Sbostic nbytes = NBINTERNAL(bi->ksize); 58150989Sbostic isbigkey = bi->flags & P_BIGKEY; 58250989Sbostic break; 58350989Sbostic case P_BLEAF: 58450989Sbostic src = bl = GETBLEAF(h, nxt); 58550989Sbostic nbytes = NBLEAF(bl); 58650989Sbostic isbigkey = bl->flags & P_BIGKEY; 58750989Sbostic break; 58850989Sbostic case P_RINTERNAL: 58950989Sbostic src = GETRINTERNAL(h, nxt); 59050989Sbostic nbytes = NRINTERNAL; 59150989Sbostic isbigkey = 0; 59250989Sbostic break; 59350989Sbostic case P_RLEAF: 59450989Sbostic src = rl = GETRLEAF(h, nxt); 59550989Sbostic nbytes = NRLEAF(rl); 59650989Sbostic isbigkey = 0; 59750989Sbostic break; 59850989Sbostic } 59950989Sbostic ++nxt; 60050989Sbostic l->linp[off] = l->upper -= nbytes; 60150989Sbostic bcopy(src, (char *)l + l->upper, nbytes); 60246134Smao 60350989Sbostic /* There's no empirical justification for the '3'. */ 604*56402Sbostic if (half < nbytes) { 605*56402Sbostic if (skip != off + 1) 606*56402Sbostic if (!isbigkey || bigkeycnt == 3) 607*56402Sbostic break; 608*56402Sbostic else 609*56402Sbostic ++bigkeycnt; 610*56402Sbostic } else 61150989Sbostic half -= nbytes; 61250989Sbostic } 61350989Sbostic l->lower += (off + 1) * sizeof(index_t); 61446134Smao 61550989Sbostic /* 61651106Sbostic * If splitting the page that the cursor was on, the cursor has to be 61751106Sbostic * adjusted to point to the same record as before the split. If the 61851106Sbostic * skipped slot and the cursor are both on the left page and the cursor 61951106Sbostic * is on or past the skipped slot, the cursor is incremented by one. 62051106Sbostic * If the skipped slot and the cursor are both on the right page and 62151106Sbostic * the cursor is on or past the skipped slot, the cursor is incremented 62251106Sbostic * by one. If the skipped slot and the cursor aren't on the same page, 62351106Sbostic * the cursor isn't changed. Regardless of the relationship of the 62451106Sbostic * skipped slot and the cursor, if the cursor is on the right page it 62551106Sbostic * is decremented by the number of records split to the left page. 62651106Sbostic * 62751106Sbostic * Don't bother checking for the BTF_SEQINIT flag, the page number will 62851106Sbostic * be P_INVALID. 62950989Sbostic */ 63050989Sbostic c = &t->bt_bcursor; 63150989Sbostic if (c->pgno == h->pgno) 63251106Sbostic if (c->index < off) { /* left page */ 63350989Sbostic c->pgno = l->pgno; 63451106Sbostic if (c->index >= skip) 63551106Sbostic ++c->index; 63651106Sbostic } else { /* right page */ 63750989Sbostic c->pgno = r->pgno; 63851106Sbostic if (c->index >= skip && skip > off) 63951106Sbostic ++c->index; 64050989Sbostic c->index -= off; 64146134Smao } 64246134Smao 64350989Sbostic /* 64450989Sbostic * Decide which page to return, and adjust the skip index if the 64550989Sbostic * to-be-inserted-upon page has changed. 64650989Sbostic */ 64751106Sbostic if (skip > off) { 64850989Sbostic rval = r; 64951106Sbostic *pskip -= off + 1; 65050989Sbostic } else 65150989Sbostic rval = l; 65246134Smao 65350989Sbostic for (off = 0; nxt < top; ++off) { 65451106Sbostic if (skip == nxt) { 65551106Sbostic skip = 0; 65650989Sbostic continue; 65746134Smao } 65850989Sbostic switch (h->flags & P_TYPE) { 65950989Sbostic case P_BINTERNAL: 66050989Sbostic src = bi = GETBINTERNAL(h, nxt); 66150989Sbostic nbytes = NBINTERNAL(bi->ksize); 66250989Sbostic break; 66350989Sbostic case P_BLEAF: 66450989Sbostic src = bl = GETBLEAF(h, nxt); 66550989Sbostic nbytes = NBLEAF(bl); 66650989Sbostic break; 66750989Sbostic case P_RINTERNAL: 66850989Sbostic src = GETRINTERNAL(h, nxt); 66950989Sbostic nbytes = NRINTERNAL; 67050989Sbostic break; 67150989Sbostic case P_RLEAF: 67250989Sbostic src = rl = GETRLEAF(h, nxt); 67350989Sbostic nbytes = NRLEAF(rl); 67450989Sbostic break; 67550989Sbostic } 67650989Sbostic ++nxt; 67750989Sbostic r->linp[off] = r->upper -= nbytes; 67850989Sbostic bcopy(src, (char *)r + r->upper, nbytes); 67950989Sbostic } 68050989Sbostic r->lower += off * sizeof(index_t); 68146134Smao 68250989Sbostic /* If the key is being appended to the page, adjust the index. */ 68351106Sbostic if (skip == top) 68450989Sbostic r->lower += sizeof(index_t); 68546134Smao 68650989Sbostic return (rval); 68750989Sbostic } 68846134Smao 68950989Sbostic /* 69050989Sbostic * BT_PRESERVE -- Mark a chain of pages as used by an internal node. 69150989Sbostic * 69250989Sbostic * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the 69350989Sbostic * record that references them gets deleted. Chains pointed to by internal 69450989Sbostic * pages never get deleted. This routine marks a chain as pointed to by an 69550989Sbostic * internal page. 69650989Sbostic * 69750989Sbostic * Parameters: 69850989Sbostic * t: tree 69950989Sbostic * pg: page number of first page in the chain. 70050989Sbostic * 70150989Sbostic * Returns: 70250989Sbostic * RET_SUCCESS, RET_ERROR. 70350989Sbostic */ 70450989Sbostic static int 70550989Sbostic bt_preserve(t, pg) 70650989Sbostic BTREE *t; 70750989Sbostic pgno_t pg; 70850989Sbostic { 70950989Sbostic PAGE *h; 71046134Smao 71150989Sbostic if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 71250989Sbostic return (RET_ERROR); 71350989Sbostic h->flags |= P_PRESERVE; 71450989Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 71546134Smao return (RET_SUCCESS); 71646134Smao } 71750989Sbostic 71850989Sbostic /* 71950989Sbostic * REC_TOTAL -- Return the number of recno entries below a page. 72050989Sbostic * 72150989Sbostic * Parameters: 72250989Sbostic * h: page 72350989Sbostic * 72450989Sbostic * Returns: 72550989Sbostic * The number of recno entries below a page. 72650989Sbostic * 72750989Sbostic * XXX 72850989Sbostic * These values could be set by the bt_psplit routine. The problem is that the 72950989Sbostic * entry has to be popped off of the stack etc. or the values have to be passed 73050989Sbostic * all the way back to bt_split/bt_rroot and it's not very clean. 73150989Sbostic */ 73250989Sbostic static recno_t 73350989Sbostic rec_total(h) 73450989Sbostic PAGE *h; 73550989Sbostic { 73650989Sbostic recno_t recs; 73750989Sbostic index_t nxt, top; 73850989Sbostic 73950989Sbostic for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt) 74050989Sbostic recs += GETRINTERNAL(h, nxt)->nrecs; 74150989Sbostic return (recs); 74250989Sbostic } 743