提交 d7cf7e79 编写于 作者: H Hongze Cheng

refact more code

上级 39040187
...@@ -1083,8 +1083,28 @@ int tdbBtcMoveToLast(SBTC *pBtc) { ...@@ -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) { ...@@ -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) { ...@@ -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,37 +1187,33 @@ static int tdbBtcMoveToNext(SBTC *pBtc) { ...@@ -1161,37 +1187,33 @@ static int tdbBtcMoveToNext(SBTC *pBtc) {
return 0; return 0;
} }
if (pBtc->iPage == 0) { // move upward
pBtc->idx = -1;
return 0;
}
// Move upward
for (;;) { for (;;) {
if (pBtc->iPage == 0) {
pBtc->idx = -1;
return 0;
}
tdbBtcMoveUpward(pBtc); tdbBtcMoveUpward(pBtc);
pBtc->idx++; pBtc->idx++;
nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); ASSERT(!TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
if (pBtc->idx <= nCells) { if (pBtc->idx <= TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) {
break; break;
} }
if (pBtc->iPage == 0) {
pBtc->idx = -1;
return 0;
}
} }
// Move downward // move downward
for (;;) { for (;;) {
nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break;
tdbBtcMoveDownward(pBtc);
pBtc->idx = 0;
if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { ret = tdbBtcMoveDownward(pBtc);
break; if (ret < 0) {
ASSERT(0);
return -1;
} }
pBtc->idx = 0;
} }
return 0; return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册