From c908f1781796b21e24fe92dcc5a4582df2771aef Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 4 Mar 2022 02:33:52 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 21 +++++++++++++++++++-- source/libs/tdb/src/inc/tdbInt.h | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 18c72c9657..69294a1c1f 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -20,6 +20,9 @@ #define TDB_BTREE_PAGE_IS_ROOT(flags) TDB_FLAG_HAS(flags, TDB_BTREE_ROOT) #define TDB_BTREE_PAGE_IS_LEAF(flags) TDB_FLAG_HAS(flags, TDB_BTREE_LEAF) +#define TDB_BTREE_ASSERT_FLAG(flags) \ + ASSERT(TDB_FLAG_IS(flags, TDB_BTREE_ROOT) || TDB_FLAG_IS(flags, TDB_BTREE_LEAF) || \ + TDB_FLAG_IS(flags, TDB_BTREE_ROOT | TDB_BTREE_LEAF) || TDB_FLAG_IS(flags, 0)) struct SBTree { SPgno root; @@ -315,11 +318,23 @@ static int tdbBtreeZeroPage(SPage *pPage, void *arg) { // Init the page header pPage->pPageHdr->flags = flags; pPage->pPageHdr->nCells = 0; - pPage->pPageHdr->cellCont = 0; + pPage->pPageHdr->cellCont = pBt->pageSize; pPage->pPageHdr->freeCell = 0; pPage->pPageHdr->nFree = 0; - tdbBtreeInitPage(pPage, (void *)pBt); + TDB_BTREE_ASSERT_FLAG(flags); + + 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; } @@ -332,6 +347,8 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg) { pPage->pPageHdr = (SPageHdr *)pPage->pData; pPage->aCellIdx = (u16 *)(&(pPage->pPageHdr[1])); + TDB_BTREE_ASSERT_FLAG(pPage->pPageHdr->flags); + // Init other fields if (TDB_BTREE_PAGE_IS_LEAF(pPage->pPageHdr->flags)) { pPage->kLen = pBt->keyLen; diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 364d1925fa..28d7a4e54c 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -128,6 +128,7 @@ typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, i #define BTREE_MAX_DEPTH 20 +#define TDB_FLAG_IS(flags, flag) ((flags) == (flag)) #define TDB_FLAG_HAS(flags, flag) (((flags) & (flag)) != 0) #define TDB_FLAG_NO(flags, flag) ((flags) & (flag) == 0) #define TDB_FLAG_ADD(flags, flag) ((flags) |= (flag)) -- GitLab