From bc00eb0e87d771e1f35fc4a7ec06cef23b8310cf Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 11 Mar 2022 06:57:03 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbPage.c | 33 +++++++++++++++++++++++++++++-- source/libs/tdb/src/inc/tdbPage.h | 3 +++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPage.c b/source/libs/tdb/src/db/tdbPage.c index e0024f7472..cd76ff3354 100644 --- a/source/libs/tdb/src/db/tdbPage.c +++ b/source/libs/tdb/src/db/tdbPage.c @@ -51,7 +51,18 @@ int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) } int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) { - // TODO + int ret; + SCell *pTarget; + + if (pPage->nOverflow || szCell + TDB_PAGE_CELL_OFFSET_SIZE(pPage) > pPage->nFree) { + // TODO + } else { + ret = tdbPageAllocate(pPage, szCell, &pTarget); + if (ret < 0) { + return -1; + } + } + return 0; } @@ -61,11 +72,29 @@ int tdbPageDropCell(SPage *pPage, int idx) { } static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell) { - // TODO + SCell *pCell; + int szOffset; + + szOffset = TDB_PAGE_CELL_OFFSET_SIZE(pPage); + ASSERT(pPage->nFree > size + szOffset); + + if (pPage->pFreeEnd - pPage->pFreeStart > size + szOffset) { + pPage->pFreeEnd -= size; + pPage->pFreeStart += szOffset; + + pCell = pPage->pFreeEnd; + } else { + } + return 0; } static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int size) { // TODO return 0; +} + +static int tdbPageDefragment(SPage *pPage) { + // TODO + return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbPage.h b/source/libs/tdb/src/inc/tdbPage.h index ee35d0df1d..25137e8dc3 100644 --- a/source/libs/tdb/src/inc/tdbPage.h +++ b/source/libs/tdb/src/inc/tdbPage.h @@ -44,8 +44,11 @@ struct SPage { SPageHdr *pPageHdr; SPageFtr *pPageFtr; u16 *aCellIdx; + u8 *pFreeStart; + u8 *pFreeEnd; int kLen; int vLen; + int nFree; int maxLocal; int minLocal; int nOverflow; -- GitLab