This commit is contained in:
Hongze Cheng 2022-03-22 08:26:43 +00:00
parent aee8b60324
commit d7b54201bc
1 changed files with 27 additions and 23 deletions

View File

@ -520,7 +520,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
} }
// copy the parent key out if child pages are not leaf page // copy the parent key out if child pages are not leaf page
childLeaf = TDB_BTREE_PAGE_IS_LEAF(TDB_BTREE_PAGE_GET_FLAGS(pOlds[0])); childLeaf = TDB_BTREE_PAGE_IS_LEAF(TDB_BTREE_PAGE_GET_FLAGS(pOlds[0]));
if (childLeaf) { if (!childLeaf) {
for (int i = 0; i < nOlds - 1; i++) { for (int i = 0; i < nOlds - 1; i++) {
pCell = tdbPageGetCell(pParent, sIdx + i); pCell = tdbPageGetCell(pParent, sIdx + i);
@ -564,8 +564,8 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
if (infoNews[nNews].size + cellBytes > TDB_PAGE_USABLE_SIZE(pPage)) { if (infoNews[nNews].size + cellBytes > TDB_PAGE_USABLE_SIZE(pPage)) {
// page is full, use a new page // page is full, use a new page
nNews++; nNews++;
// for a child leaf case, this cell is used as the new divider cell to parent // for a internal leaf case, this cell is used as the new divider cell to parent
if (childLeaf) continue; if (!childLeaf) continue;
} }
infoNews[nNews].cnt++; infoNews[nNews].cnt++;
infoNews[nNews].size += cellBytes; infoNews[nNews].size += cellBytes;
@ -573,8 +573,8 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
infoNews[nNews].oIdx = oIdx; infoNews[nNews].oIdx = oIdx;
} }
// For child leaf pages // For internal pages
if (childLeaf && oPage < nOlds - 1) { if (!childLeaf && oPage < nOlds - 1) {
if (infoNews[nNews].size + szDivCell[oPage] + TDB_PAGE_OFFSET_SIZE(pPage) > TDB_PAGE_USABLE_SIZE(pPage)) { if (infoNews[nNews].size + szDivCell[oPage] + TDB_PAGE_OFFSET_SIZE(pPage) > TDB_PAGE_USABLE_SIZE(pPage)) {
nNews++; nNews++;
} }
@ -594,27 +594,31 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
int nCells; int nCells;
int cellBytes; int cellBytes;
pPage = pOlds[infoNews[iNew - 1].oPage]; if (childLeaf) { // child leaf
nCells = TDB_PAGE_TOTAL_CELLS(pPage); pPage = pOlds[infoNews[iNew - 1].oPage];
nCells = TDB_PAGE_TOTAL_CELLS(pPage);
for (;;) { for (;;) {
pCell = tdbPageGetCell(pPage, infoNews[iNew - 1].oIdx); pCell = tdbPageGetCell(pPage, infoNews[iNew - 1].oIdx);
cellBytes = TDB_BYTES_CELL_TAKEN(pPage, pCell); cellBytes = TDB_BYTES_CELL_TAKEN(pPage, pCell);
infoNews[iNew].cnt++; infoNews[iNew].cnt++;
infoNews[iNew].size += cellBytes; infoNews[iNew].size += cellBytes;
infoNews[iNew - 1].cnt--; infoNews[iNew - 1].cnt--;
infoNews[iNew - 1].size -= cellBytes; infoNews[iNew - 1].size -= cellBytes;
if ((infoNews[iNew - 1].oIdx--) == 0) { if ((infoNews[iNew - 1].oIdx--) == 0) {
infoNews[iNew - 1].oPage--; infoNews[iNew - 1].oPage--;
pPage = pOlds[infoNews[iNew - 1].oPage]; pPage = pOlds[infoNews[iNew - 1].oPage];
nCells = TDB_PAGE_TOTAL_CELLS(pPage); nCells = TDB_PAGE_TOTAL_CELLS(pPage);
infoNews[iNew - 1].oIdx = TDB_PAGE_TOTAL_CELLS(pPage); infoNews[iNew - 1].oIdx = TDB_PAGE_TOTAL_CELLS(pPage);
} }
if (infoNews[iNew].size > infoNews[iNew - 1].size) { if (infoNews[iNew].size > infoNews[iNew - 1].size) {
break; break;
}
} }
} else { // internal leaf
// TODO
} }
} }
} }