This commit is contained in:
Hongze Cheng 2022-03-16 06:37:07 +00:00
parent 5cdfc49ba0
commit 8303034928
2 changed files with 51 additions and 13 deletions

View File

@ -183,6 +183,11 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
return 0; return 0;
} }
static int tdbBtCursorMoveToChild(SBtCursor *pCur, SPgno pgno) {
// TODO
return 0;
}
static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *pCRst) { static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *pCRst) {
int ret; int ret;
SBTree *pBt; SBTree *pBt;
@ -211,16 +216,23 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
} }
for (;;) { for (;;) {
int lidx, ridx, midx, c; int lidx, ridx, midx, c, nCells;
SCell *pCell; SCell *pCell;
SPage *pPage; SPage *pPage;
SCellDecoder cd = {0}; SCellDecoder cd = {0};
pPage = pCur->pPage; pPage = pCur->pPage;
nCells = TDB_PAGE_NCELLS(pPage);
lidx = 0; lidx = 0;
ridx = TDB_PAGE_NCELLS(pPage) - 1; ridx = nCells - 1;
midx = (lidx + ridx) >> 1;
ASSERT(nCells > 0);
for (;;) { for (;;) {
if (lidx > ridx) break;
midx = (lidx + ridx) >> 1;
pCell = TDB_PAGE_CELL_AT(pPage, midx); pCell = TDB_PAGE_CELL_AT(pPage, midx);
ret = tdbBtreeDecodeCell(pPage, pCell, &cd); ret = tdbBtreeDecodeCell(pPage, pCell, &cd);
if (ret < 0) { if (ret < 0) {
@ -232,16 +244,42 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
// Compare the key values // Compare the key values
c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen);
if (c < 0) { if (c < 0) {
/* TODO */ /* input-key < cell-key */
ASSERT(0); ridx = midx - 1;
} else if (c > 0) { } else if (c > 0) {
/* TODO */ /* input-key > cell-key */
ASSERT(0); lidx = midx + 1;
} else { } else {
/* TODO */ /* input-key == cell-key */
ASSERT(0); break;
} }
} }
#if 1
u16 flags = TDB_PAGE_FLAGS(pPage);
u8 leaf = TDB_BTREE_PAGE_IS_LEAF(flags);
if (leaf) {
pCur->idx = midx;
break;
} else {
if (c <= 0) {
pCur->idx = midx;
tdbBtCursorMoveToChild(pCur, cd.pgno);
} else {
if (midx == nCells - 1) {
/* Move to right-most child */
pCur->idx = midx + 1;
tdbBtCursorMoveToChild(pCur, ((SBtPageHdr *)(pPage->pAmHdr))->rChild);
} else {
// TODO: reset cd as uninitialized
pCur->idx = midx + 1;
pCell = TDB_PAGE_CELL_AT(pPage, midx + 1);
tdbBtreeDecodeCell(pPage, pCell, &cd);
tdbBtCursorMoveToChild(pCur, cd.pgno);
}
}
}
#endif
} }
} else { } else {

View File

@ -26,11 +26,11 @@ typedef struct SBtCursor SBtCursor;
struct SBtCursor { struct SBtCursor {
SBTree *pBt; SBTree *pBt;
i8 iPage; i8 iPage;
SPage * pPage; SPage *pPage;
int idx; int idx;
u16 idxStack[BTREE_MAX_DEPTH + 1]; int idxStack[BTREE_MAX_DEPTH + 1];
SPage * pgStack[BTREE_MAX_DEPTH + 1]; SPage *pgStack[BTREE_MAX_DEPTH + 1];
void * pBuf; void *pBuf;
}; };
int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt); int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt);