From 4e8f8445de7f719dbef9a75321253df3d4b1fff5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 21 Mar 2022 05:47:06 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 41 +++++++++++++++++------------ source/libs/tdb/src/inc/tdbPage.h | 4 +++ source/libs/tdb/src/page/tdbPage.c | 10 ++++--- source/libs/tdb/src/page/tdbPageL.c | 10 ++++--- 4 files changed, 42 insertions(+), 23 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 52f0c7d466..4b2394f861 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -39,9 +39,16 @@ struct SBTree { u8 *pTmp; }; +#define TDB_BTREE_PAGE_COMMON_HDR u8 flags; + +typedef struct __attribute__((__packed__)) { + TDB_BTREE_PAGE_COMMON_HDR +} SLeafHdr; + typedef struct __attribute__((__packed__)) { - SPgno rChild; -} SBtPageHdr; + TDB_BTREE_PAGE_COMMON_HDR; + SPgno pgno; // right-most child +} SIntHdr; typedef struct { u16 flags; @@ -220,7 +227,7 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p if (TDB_PAGE_TOTAL_CELLS(pCur->pPage) == 0) { // Current page is empty - ASSERT(TDB_FLAG_IS(TDB_PAGE_FLAGS(pCur->pPage), TDB_BTREE_ROOT | TDB_BTREE_LEAF)); + // ASSERT(TDB_FLAG_IS(TDB_PAGE_FLAGS(pCur->pPage), TDB_BTREE_ROOT | TDB_BTREE_LEAF)); return 0; } @@ -265,7 +272,7 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p } // Move downward or break - u16 flags = TDB_PAGE_FLAGS(pPage); + u16 flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); u8 leaf = TDB_BTREE_PAGE_IS_LEAF(flags); if (leaf) { pCur->idx = midx; @@ -279,7 +286,7 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p if (midx == nCells - 1) { /* Move to right-most child */ pCur->idx = midx + 1; - tdbBtCursorMoveToChild(pCur, ((SBtPageHdr *)(pPage->pAmHdr))->rChild); + // tdbBtCursorMoveToChild(pCur, ((SBtPageHdr *)(pPage->pAmHdr))->rChild); } else { // TODO: reset cd as uninitialized pCur->idx = midx + 1; @@ -388,12 +395,12 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg) { ASSERT(0); // TODO: here has problem - flags = TDB_PAGE_FLAGS(pPage); + flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); isLeaf = TDB_BTREE_PAGE_IS_LEAF(flags); if (isLeaf) { - szAmHdr = 0; + szAmHdr = sizeof(SLeafHdr); } else { - szAmHdr = sizeof(SBtPageHdr); + szAmHdr = sizeof(SIntHdr); } pPage->xCellSize = NULL; // TODO @@ -427,14 +434,14 @@ static int tdbBtreeZeroPage(SPage *pPage, void *arg) { isLeaf = TDB_BTREE_PAGE_IS_LEAF(flags); if (isLeaf) { - szAmHdr = 0; + szAmHdr = sizeof(SLeafHdr); } else { - szAmHdr = sizeof(SBtPageHdr); + szAmHdr = sizeof(SIntHdr); } pPage->xCellSize = NULL; // TODO tdbPageZero(pPage, szAmHdr); - TDB_PAGE_FLAGS_SET(pPage, flags); + // TDB_PAGE_FLAGS_SET(pPage, flags); if (isLeaf) { pPage->kLen = pBt->keyLen; @@ -522,7 +529,7 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) { return -1; } - ((SBtPageHdr *)pRoot->pAmHdr)[0].rChild = pgnoChild; + // TODO: ((SBtPageHdr *)pRoot->pAmHdr)[0].rChild = pgnoChild; *ppChild = pChild; return 0; @@ -566,7 +573,7 @@ static int tdbBtreeBalanceStep1(SBtreeBalanceHelper *pBlh) { i = pBlh->nOld - 1; if (idxStart + i == nCells) { - pgno = ((SBtPageHdr *)(pParent->pAmHdr))[0].rChild; + // pgno = ((SBtPageHdr *)(pParent->pAmHdr))[0].rChild; } else { pCell = tdbPageGetCell(pParent, idxStart + i); // TODO: no need to decode the payload part, and even the kLen, vLen part @@ -676,7 +683,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { int ret; SBtreeBalanceHelper blh; - ASSERT(!TDB_BTREE_PAGE_IS_LEAF(TDB_PAGE_FLAGS(pParent))); + // ASSERT(!TDB_BTREE_PGE_IS_LEAF(TDB_PAGE_FLAGS(pParent))); blh.pBt = pBt; blh.pParent = pParent; @@ -748,7 +755,7 @@ static int tdbBtreeBalance(SBtCursor *pCur) { for (;;) { iPage = pCur->iPage; pPage = pCur->pPage; - flags = TDB_PAGE_FLAGS(pPage); + flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); leaf = TDB_BTREE_PAGE_IS_LEAF(flags); root = TDB_BTREE_PAGE_IS_ROOT(flags); @@ -842,7 +849,7 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo nPayload = 0; nHeader = 0; - flags = TDB_PAGE_FLAGS(pPage); + flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); leaf = TDB_BTREE_PAGE_IS_LEAF(flags); // 1. Encode Header part @@ -911,7 +918,7 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD int ret; nHeader = 0; - flags = TDB_PAGE_FLAGS(pPage); + flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); leaf = TDB_BTREE_PAGE_IS_LEAF(flags); // Clear the state of decoder diff --git a/source/libs/tdb/src/inc/tdbPage.h b/source/libs/tdb/src/inc/tdbPage.h index 58ae1f3963..155e7b2e37 100644 --- a/source/libs/tdb/src/inc/tdbPage.h +++ b/source/libs/tdb/src/inc/tdbPage.h @@ -27,9 +27,11 @@ typedef struct { int szOffset; int szPageHdr; int szFreeCell; +#if 0 // flags u16 (*getFlags)(SPage *); void (*setFlags)(SPage *, u16); +#endif // cell number int (*getCellNum)(SPage *); void (*setCellNum)(SPage *, int); @@ -79,9 +81,11 @@ struct SPage { TDB_PCACHE_PAGE }; +#if 0 /* For page */ #define TDB_PAGE_FLAGS(pPage) (*(pPage)->pPageMethods->getFlags)(pPage) #define TDB_PAGE_FLAGS_SET(pPage, FLAGS) (*(pPage)->pPageMethods->setFlags)(pPage, FLAGS) +#endif // For page lock #define P_LOCK_SUCC 0 diff --git a/source/libs/tdb/src/page/tdbPage.c b/source/libs/tdb/src/page/tdbPage.c index 11c9a9247e..61793b6025 100644 --- a/source/libs/tdb/src/page/tdbPage.c +++ b/source/libs/tdb/src/page/tdbPage.c @@ -399,9 +399,11 @@ typedef struct __attribute__((__packed__)) { u16 nxOffset; } SFreeCell; +#if 0 // flags static inline u16 getPageFlags(SPage *pPage) { return ((SPageHdr *)(pPage->pPageHdr))[0].flags; } static inline void setPageFlags(SPage *pPage, u16 flags) { ((SPageHdr *)(pPage->pPageHdr))[0].flags = flags; } +#endif // cellNum static inline int getPageCellNum(SPage *pPage) { return ((SPageHdr *)(pPage->pPageHdr))[0].cellNum; } @@ -456,11 +458,13 @@ static inline void setPageFreeCellInfo(SCell *pCell, int szCell, int nxOffset) { } SPageMethods pageMethods = { - 2, // szOffset - sizeof(SPageHdr), // szPageHdr - sizeof(SFreeCell), // szFreeCell + 2, // szOffset + sizeof(SPageHdr), // szPageHdr + sizeof(SFreeCell), // szFreeCell +#if 0 getPageFlags, // getPageFlags setPageFlags, // setFlagsp +#endif getPageCellNum, // getCellNum setPageCellNum, // setCellNum getPageCellBody, // getCellBody diff --git a/source/libs/tdb/src/page/tdbPageL.c b/source/libs/tdb/src/page/tdbPageL.c index e3c1bb8d37..f4bcc62494 100644 --- a/source/libs/tdb/src/page/tdbPageL.c +++ b/source/libs/tdb/src/page/tdbPageL.c @@ -28,9 +28,11 @@ typedef struct __attribute__((__packed__)) { u8 nxOffset[3]; } SFreeCellL; +#if 0 // flags static inline u16 getPageFlags(SPage *pPage) { return ((SPageHdrL *)(pPage->pPageHdr))[0].flags; } static inline void setPageFlags(SPage *pPage, u16 flags) { ((SPageHdrL *)(pPage->pPageHdr))[0].flags = flags; } +#endif // cellNum static inline int getPageCellNum(SPage *pPage) { return TDB_GET_U24(((SPageHdrL *)(pPage->pPageHdr))[0].cellNum); } @@ -80,11 +82,13 @@ static inline void setPageFreeCellInfo(SCell *pCell, int szCell, int nxOffset) { } SPageMethods pageLargeMethods = { - 3, // szOffset - sizeof(SPageHdrL), // szPageHdr - sizeof(SFreeCellL), // szFreeCell + 3, // szOffset + sizeof(SPageHdrL), // szPageHdr + sizeof(SFreeCellL), // szFreeCell +#if 0 getPageFlags, // getPageFlags setPageFlags, // setFlagsp +#endif getPageCellNum, // getCellNum setPageCellNum, // setCellNum getPageCellBody, // getCellBody -- GitLab