From 7cd83134d243f7428f8ed751d5a84e3cc27eb7e0 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 25 Feb 2022 08:31:16 +0000 Subject: [PATCH] more --- source/libs/tdb/src/db/tdbBtree.c | 67 ++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 3c692df228..c8df1c2725 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -28,18 +28,20 @@ struct SBTree { }; typedef struct SPgHdr { - u8 flags; - u8 nFree; - u16 firstFree; - u16 nCells; - u16 pCell; - i32 kLen; - i32 vLen; + u8 flags; + u8 nFree; + u16 firstFree; + u16 nCells; + u16 pCell; + i32 kLen; + i32 vLen; + SPgno rightChild; } SPgHdr; typedef struct SBtPage { SPgHdr *pHdr; u16 * aCellIdx; + u8 * aData; } SBtPage; struct SBtCursor { @@ -48,10 +50,16 @@ struct SBtCursor { SBtPage *pPage; }; +typedef struct SFreeCell { + u16 size; + u16 next; +} SFreeCell; + static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen); static int tdbEncodeLength(u8 *pBuf, uint len); static int tdbBtCursorMoveToRoot(SBtCursor *pCur); static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage); +static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell); int tdbBtreeOpen(SPgno root, SBTree **ppBt) { *ppBt = NULL; @@ -92,10 +100,38 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p } static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen) { - int ret; + int ret; + SBtPage *pBtPage; + void * pCell; ret = tdbBtCursorMoveToRoot(pCur); - // TODO + if (ret < 0) { + return -1; + } + + for (;;) { + int lidx, ridx, midx, c; + + pBtPage = pCur->pPage; + lidx = 0; + ridx = pBtPage->pHdr->nCells - 1; + while (lidx <= ridx) { + midx = (lidx + ridx) >> 1; + pCell = (void *)(pBtPage->aData + pBtPage->aCellIdx[midx]); + + c = tdbCompareKeyAndCell(pKey, kLen, pCell); + if (c == 0) { + break; + } else if (c < 0) { + lidx = lidx + 1; + } else { + ridx = ridx - 1; + } + } + + /* code */ + } + return 0; } @@ -181,6 +217,17 @@ static int tdbBtCursorMoveToRoot(SBtCursor *pCur) { } static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage) { - // TODO + SBtPage *pBtPage; + + pBtPage = (SBtPage *)pPage->pExtra; + pBtPage->pHdr = (SPgHdr *)pPage->pData; + pBtPage->aCellIdx = (u16 *)(&((pBtPage->pHdr)[1])); + pBtPage->aData = (u8 *)pPage->pData; + + *ppBtPage = pBtPage; + return 0; +} +static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell) { + /* TODO */ return 0; } \ No newline at end of file -- GitLab