From df12ae0d79e73725e54cf4c7ca44743dd21eda71 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 22 Mar 2022 05:28:54 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 50 ++++++++++++++++++++++++++++-- source/libs/tdb/src/db/tdbPCache.c | 14 +-------- source/libs/tdb/src/inc/tdbUtil.h | 10 ++++++ source/libs/tdb/src/page/tdbPage.c | 7 +++++ 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 71a9e70064..196c4593c5 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -598,10 +598,56 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { } } } + + // TODO: sort the page according to the page number } - { - // Do the actual redistribute + { // Do the actual cell distribution + + SPage *pTPage[2]; + int tPage, tIdx, iOld; + SCell *pCell; + int szCell; + SBtreeInitPageArg iarg = {.flags = TDB_BTREE_PAGE_GET_FLAGS(pOlds[0]), .pBt = pBt}; + + for (int i = 0; i < 2; i++) { + ret = tdbPageCreate(pOlds[0]->pageSize, &pTPage[i], NULL, NULL); + if (ret < 0) { + ASSERT(0); + } + } + + tPage = 0; + tIdx = 0; + iOld = 0; + tdbBtreeZeroPage(pTPage[tPage], &iarg); + tdbPageCopy(pOlds[iOld++], pTPage[tPage]); + + for (int iNew = 0; iNew < nNews; iNew++) { + // fill the iNew page + tdbBtreeZeroPage(pNews[iNew], &iarg); + + for (int iCell = 0; iCell < infoNews[iNew].cnt; iCell++) { + while (tIdx >= TDB_PAGE_TOTAL_CELLS(pTPage[tPage])) { + tPage = (tPage + 1) % 2; + tIdx = 0; + + tdbBtreeZeroPage(pTPage[tPage], &iarg); + tdbPageCopy(pOlds[iOld++], pTPage[tPage]); + } + + pCell = tdbPageGetCell(pTPage[tPage], tIdx); + szCell = tdbBtreeCellSize(pTPage[tPage], pCell); + + tdbPageInsertCell(pNews[iNew], iCell, pCell, szCell); + + tIdx++; + } + } + + for (int i = 0; i < 2; i++) { + tdbPageDestroy(pTPage[i], NULL, NULL); + } } return 0; diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index 29f7395cbc..1fc0847b38 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -57,8 +57,6 @@ static void tdbPCachePinPage(SPage *pPage); static void tdbPCacheRemovePageFromHash(SPage *pPage); static void tdbPCacheAddPageToHash(SPage *pPage); static void tdbPCacheUnpinPage(SPage *pPage); -static void *tdbOsMalloc(void *arg, size_t size); -static void tdbOsFree(void *arg, void *ptr); int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) { SPCache *pCache; @@ -257,7 +255,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { pCache->nFree = 0; pCache->pFree = NULL; for (int i = 0; i < pCache->cacheSize; i++) { - ret = tdbPageCreate(pCache->pageSize, &pPage, tdbOsMalloc, NULL); + ret = tdbPageCreate(pCache->pageSize, &pPage, NULL, NULL); if (ret < 0) { // TODO: handle error return -1; @@ -297,13 +295,3 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { } int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; } - -static void *tdbOsMalloc(void *arg, size_t size) { - void *ptr; - - ptr = malloc(size); - - return ptr; -} - -static void tdbOsFree(void *arg, void *ptr) { free(ptr); } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 9576ee6d15..9358aae236 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -39,6 +39,16 @@ int tdbGetFileSize(const char *fname, int pgSize, SPgno *pSize); int tdbPRead(int fd, void *pData, int count, i64 offset); +static inline void *tdbOsMalloc(void *arg, size_t size) { + void *ptr; + + ptr = malloc(size); + + return ptr; +} + +static inline void tdbOsFree(void *arg, void *ptr) { free(ptr); } + static inline int tdbPutVarInt(u8 *p, int v) { int n = 0; diff --git a/source/libs/tdb/src/page/tdbPage.c b/source/libs/tdb/src/page/tdbPage.c index 0dc7853940..16130ba13f 100644 --- a/source/libs/tdb/src/page/tdbPage.c +++ b/source/libs/tdb/src/page/tdbPage.c @@ -48,6 +48,9 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t) *ppPage = NULL; size = pageSize + sizeof(*pPage); + if (xMalloc == NULL) { + xMalloc = tdbOsMalloc; + } ptr = (u8 *)((*xMalloc)(arg, size)); if (pPage == NULL) { @@ -73,6 +76,10 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t) int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) { u8 *ptr; + if (!xFree) { + xFree = tdbOsFree; + } + ptr = pPage->pData; (*xFree)(arg, ptr); -- GitLab