From 1075c230ed0615f80449000afe09feafd9a63723 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 21 Mar 2022 10:18:04 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 37 ++++++++++++++++++++++--------- source/libs/tdb/src/inc/tdbPage.h | 3 ++- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 52ea45578b..93fc75269c 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -644,19 +644,11 @@ static int tdbBtreeBalanceStep6(SBtreeBalanceHelper *pBlh) { static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { int ret; -#if 0 - SBtreeBalanceHelper blh; - - blh.pBt = pBt; - blh.pParent = pParent; - blh.idx = idx; -#endif int nOlds; SPage *pOlds[3]; - { - // Find 3 child pages at most to do balance + { // Find 3 child pages at most to do balance int nCells = TDB_PAGE_TOTAL_CELLS(pParent); int sIdx; if (nCells <= 2) { @@ -695,7 +687,32 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { } } - // >>>>>>>>>>>>>>>>>>> + int nNews = 0; + int cntNews[5] = {0}; // TODO: maybe 5 is not enough + int szNews[5] = {0}; + int maxPageCapacity; // TODO + + { // Get how many new pages are needed and the new distribution + + // loop to find number of pages needed + for (int i = 0; i < nOlds; i++) { + SPage *pPage = pOlds[i]; + SCell *pCell; + int cellBytes; + for (int cIdx = 0; cIdx < TDB_PAGE_TOTAL_CELLS(pPage); cIdx++) { + pCell = tdbPageGetCell(pPage, cIdx); + cellBytes = TDB_BYTES_CELL_TAKEN(pPage, pCell); + + if (szNews[nNews] + cellBytes > maxPageCapacity) { + nNews++; + } + cntNews[nNews]++; + szNews[nNews] = szNews[nNews] + cellBytes; + } + } + + // Loop to make the distribution even + } #if 0 // Step 1: find two sibling pages and get engough info about the old pages diff --git a/source/libs/tdb/src/inc/tdbPage.h b/source/libs/tdb/src/inc/tdbPage.h index e513a63fe4..479c7603e1 100644 --- a/source/libs/tdb/src/inc/tdbPage.h +++ b/source/libs/tdb/src/inc/tdbPage.h @@ -98,7 +98,8 @@ struct SPage { }) // APIs -#define TDB_PAGE_TOTAL_CELLS(pPage) ((pPage)->nOverflow + (pPage)->pPageMethods->getCellNum(pPage)) +#define TDB_PAGE_TOTAL_CELLS(pPage) ((pPage)->nOverflow + (pPage)->pPageMethods->getCellNum(pPage)) +#define TDB_BYTES_CELL_TAKEN(pPage, pCell) ((*(pPage)->xCellSize)(pPage, pCell) + (pPage)->pPageMethods->szOffset) int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg); int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg);