From 90789973de5a6788fca0919be295bd096d715710 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 4 Mar 2022 02:10:06 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 66 +++++++++++++++++-------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 5b3bcec507..18c72c9657 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -40,12 +40,19 @@ typedef struct SFreeCell { u16 next; } SFreeCell; +typedef struct { + u16 flags; + SBTree *pBt; +} SBtreeZeroPageArg; + static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen); static int tdbEncodeLength(u8 *pBuf, uint32_t len); static int tdbBtCursorMoveToRoot(SBtCursor *pCur); static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell); static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2); static int tdbBtreeOpenImpl(SBTree *pBt); +static int tdbBtreeZeroPage(SPage *pPage, void *arg); +static int tdbBtreeInitPage(SPage *pPage, void *arg); int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, SBTree **ppBt) { SBTree *pBt; @@ -110,8 +117,8 @@ int tdbBtreeCursor(SBtCursor *pCur, SBTree *pBt) { } int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen) { - int ret; - SPager * pPager; + int ret; + SPager *pPager; ret = tdbBtCursorMoveTo(pCur, pKey, kLen); if (ret < 0) { @@ -123,8 +130,8 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p } static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen) { - int ret; - void * pCell; + int ret; + void *pCell; // ret = tdbBtCursorMoveToRoot(pCur); // if (ret < 0) { @@ -296,45 +303,46 @@ static int tdbBtreeOpenImpl(SBTree *pBt) { } static int tdbBtreeZeroPage(SPage *pPage, void *arg) { + u16 flags; + SBTree *pBt; + + flags = ((SBtreeZeroPageArg *)arg)->flags; + pBt = ((SBtreeZeroPageArg *)arg)->pBt; + pPage->pPageHdr = (SPageHdr *)pPage->pData; pPage->aCellIdx = (u16 *)(&(pPage->pPageHdr[1])); // Init the page header - { -#if 0 - pPage->pPageHdr->flags = 0; - pPage->pPageHdr->nCells = 0; - pPage->pPageHdr->cellCont = 0; - pPage->pPageHdr->freeCell = 0; - pPage->pPageHdr->nFree = 0; -#endif - } + pPage->pPageHdr->flags = flags; + pPage->pPageHdr->nCells = 0; + pPage->pPageHdr->cellCont = 0; + pPage->pPageHdr->freeCell = 0; + pPage->pPageHdr->nFree = 0; - // Init other fields - { -#if 0 - pPage->kLen = pBt->keyLen; - pPage->vLen = pBt->valLen; - pPage->maxLocal = pBt->maxLocal; - pPage->minLocal = pBt->minLocal; -#endif - } + tdbBtreeInitPage(pPage, (void *)pBt); return 0; } static int tdbBtreeInitPage(SPage *pPage, void *arg) { + SBTree *pBt; + + pBt = (SBTree *)arg; + pPage->pPageHdr = (SPageHdr *)pPage->pData; pPage->aCellIdx = (u16 *)(&(pPage->pPageHdr[1])); // Init other fields - { -#if 0 - pPage->kLen = pBt->keyLen; - pPage->vLen = pBt->valLen; - pPage->maxLocal = pBt->maxLocal; - pPage->minLocal = pBt->minLocal; -#endif + if (TDB_BTREE_PAGE_IS_LEAF(pPage->pPageHdr->flags)) { + pPage->kLen = pBt->keyLen; + pPage->vLen = pBt->valLen; + pPage->maxLocal = pBt->maxLeaf; + pPage->minLocal = pBt->minLeaf; + } else { + pPage->kLen = pBt->keyLen; + pPage->vLen = sizeof(SPgno); + pPage->maxLocal = pBt->maxLocal; + pPage->minLocal = pBt->minLocal; } return 0; -- GitLab