refact more

This commit is contained in:
Hongze Cheng 2022-03-30 08:49:40 +00:00
parent 184200470c
commit 3904018757
1 changed files with 22 additions and 12 deletions

View File

@ -1056,6 +1056,7 @@ int tdbBtcMoveToFirst(SBTC *pBtc) {
int tdbBtcMoveToLast(SBTC *pBtc) { int tdbBtcMoveToLast(SBTC *pBtc) {
int ret; int ret;
int nCells;
SBTree *pBt; SBTree *pBt;
SPager *pPager; SPager *pPager;
SPgno pgno; SPgno pgno;
@ -1071,7 +1072,16 @@ int tdbBtcMoveToLast(SBTC *pBtc) {
return -1; return -1;
} }
nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
pBtc->iPage = 0; pBtc->iPage = 0;
if (nCells > 0) {
pBtc->idx = TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage) ? nCells - 1 : nCells;
} else {
// no data at all, point to an invalid position
ASSERT(TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
pBtc->idx = -1;
return 0;
}
} else { } else {
// move from a position // move from a position
ASSERT(0); ASSERT(0);
@ -1079,19 +1089,19 @@ int tdbBtcMoveToLast(SBTC *pBtc) {
// move downward // move downward
for (;;) { for (;;) {
if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break;
// TODO: handle empty case
ASSERT(TDB_PAGE_TOTAL_CELLS(pBtc->pPage) > 0);
pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage) - 1;
break;
} else {
pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
ret = tdbBtcMoveDownward(pBtc); ret = tdbBtcMoveDownward(pBtc);
if (ret < 0) { if (ret < 0) {
ASSERT(0); ASSERT(0);
return -1; return -1;
} }
nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) {
pBtc->idx = nCells - 1;
} else {
pBtc->idx = nCells;
} }
} }