refact more code

This commit is contained in:
Hongze Cheng 2022-03-30 09:25:30 +00:00
parent 3904018757
commit d7cf7e791c
1 changed files with 46 additions and 24 deletions

View File

@ -1083,8 +1083,28 @@ int tdbBtcMoveToLast(SBTC *pBtc) {
return 0; return 0;
} }
} else { } else {
// move from a position int iPage = 0;
ASSERT(0);
// downward search
for (; iPage < pBtc->iPage; iPage++) {
ASSERT(!TDB_BTREE_PAGE_IS_LEAF(pBtc->pgStack[iPage]));
nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pgStack[iPage]);
if (pBtc->idxStack[iPage] != nCells) break;
}
// move upward
for (;;) {
if (pBtc->iPage == 0) {
if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) {
pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage) - 1;
} else {
pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
}
}
if (pBtc->iPage < iPage) break;
tdbBtcMoveUpward(pBtc);
}
} }
// move downward // move downward
@ -1114,6 +1134,7 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
void *pKey, *pVal; void *pKey, *pVal;
int ret; int ret;
// current cursor points to an invalid position
if (pBtc->idx < 0) { if (pBtc->idx < 0) {
return -1; return -1;
} }
@ -1144,12 +1165,17 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
memcpy(pVal, cd.pVal, cd.vLen); memcpy(pVal, cd.pVal, cd.vLen);
ret = tdbBtcMoveToNext(pBtc); ret = tdbBtcMoveToNext(pBtc);
if (ret < 0) {
ASSERT(0);
return -1;
}
return 0; return 0;
} }
static int tdbBtcMoveToNext(SBTC *pBtc) { static int tdbBtcMoveToNext(SBTC *pBtc) {
int nCells; int nCells;
int ret;
SCell *pCell; SCell *pCell;
ASSERT(TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)); ASSERT(TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
@ -1161,39 +1187,35 @@ static int tdbBtcMoveToNext(SBTC *pBtc) {
return 0; return 0;
} }
if (pBtc->iPage == 0) { // move upward
pBtc->idx = -1;
return 0;
}
// Move upward
for (;;) { for (;;) {
tdbBtcMoveUpward(pBtc);
pBtc->idx++;
nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
if (pBtc->idx <= nCells) {
break;
}
if (pBtc->iPage == 0) { if (pBtc->iPage == 0) {
pBtc->idx = -1; pBtc->idx = -1;
return 0; return 0;
} }
}
// Move downward tdbBtcMoveUpward(pBtc);
for (;;) { pBtc->idx++;
nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
tdbBtcMoveDownward(pBtc); ASSERT(!TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
pBtc->idx = 0; if (pBtc->idx <= TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) {
if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) {
break; break;
} }
} }
// move downward
for (;;) {
if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break;
ret = tdbBtcMoveDownward(pBtc);
if (ret < 0) {
ASSERT(0);
return -1;
}
pBtc->idx = 0;
}
return 0; return 0;
} }