From b5e9f2aca0096ba00dfd6adcfc37d75e17a3aa79 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 09:58:33 +0000 Subject: [PATCH] refact more --- source/libs/tdb/src/db/tdbBtree.c | 82 +++++++++++++++++------------- source/libs/tdb/src/db/tdbDb.c | 17 +------ source/libs/tdb/src/inc/tdbBtree.h | 2 +- 3 files changed, 50 insertions(+), 51 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index d3534ae5a2..b1728b53e7 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -29,7 +29,7 @@ struct SBTree { int minLocal; int maxLeaf; int minLeaf; - u8 *pTmp; + void *pBuf; }; #define TDB_BTREE_PAGE_COMMON_HDR u8 flags; @@ -127,66 +127,80 @@ int tdbBtreeClose(SBTree *pBt) { return 0; } -int tdbBtCursorInsert(SBTC *pBtc, const void *pKey, int kLen, const void *pVal, int vLen) { - int ret; - int idx; - SPager *pPager; - SCell *pCell; - int szCell; - int cret; - SBTree *pBt; +int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen) { + SBTC btc; + SCell *pCell; + void *pBuf; + int szCell; + int szBuf; + int ret; + int idx; + int c; - ret = tdbBtcMoveTo(pBtc, pKey, kLen, &cret); + tdbBtcOpen(&btc, pBt); + + // move to the position to insert + ret = tdbBtcMoveTo(&btc, pKey, kLen, &c); if (ret < 0) { - // TODO: handle error + tdbBtcClose(&btc); + ASSERT(0); return -1; } - if (pBtc->idx == -1) { - ASSERT(TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0); + if (btc.idx == -1) { idx = 0; } else { - if (cret > 0) { - idx = pBtc->idx + 1; - } else if (cret < 0) { - idx = pBtc->idx; + if (c > 0) { + idx = btc.idx + 1; + } else if (c < 0) { + idx = btc.idx; } else { - /* TODO */ + // TDB does NOT allow same key + tdbBtcClose(&btc); ASSERT(0); - } - } - - // TODO: refact code here - pBt = pBtc->pBt; - if (!pBt->pTmp) { - pBt->pTmp = (u8 *)tdbOsMalloc(pBt->pageSize); - if (pBt->pTmp == NULL) { return -1; } } - pCell = pBt->pTmp; + // make sure enough space to hold the space + szBuf = kLen + vLen + 14; + pBuf = TDB_REALLOC(pBt->pBuf, pBt->pageSize > szBuf ? szBuf : pBt->pageSize); + if (pBuf == NULL) { + tdbBtcClose(&btc); + ASSERT(0); + return -1; + } + pBt->pBuf = pBuf; + pCell = (SCell *)pBt->pBuf; - // Encode the cell - ret = tdbBtreeEncodeCell(pBtc->pPage, pKey, kLen, pVal, vLen, pCell, &szCell); + // encode cell + ret = tdbBtreeEncodeCell(btc.pPage, pKey, kLen, pVal, vLen, pCell, &szCell); if (ret < 0) { + tdbBtcClose(&btc); + ASSERT(0); return -1; } - // Insert the cell to the index - ret = tdbPageInsertCell(pBtc->pPage, idx, pCell, szCell, 0); + // insert the cell + ret = tdbPageInsertCell(btc.pPage, idx, pCell, szCell, 0); if (ret < 0) { + tdbBtcClose(&btc); + ASSERT(0); return -1; } - // If page is overflow, balance the tree - if (pBtc->pPage->nOverflow > 0) { - ret = tdbBtreeBalance(pBtc); + // check if need balance + if (btc.pPage->nOverflow > 0) { + ret = tdbBtreeBalance(&btc); if (ret < 0) { + tdbBtcClose(&btc); + ASSERT(0); return -1; } } + tdbBtcClose(&btc); + return 0; } diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index e41e41f72a..0de2a6433f 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -74,22 +74,7 @@ int tdbDbDrop(TDB *pDb) { } int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) { - SBTC btc; - SBTC *pCur; - int ret; - - pCur = &btc; - ret = tdbBtcOpen(pCur, pDb->pBt); - if (ret < 0) { - return -1; - } - - ret = tdbBtCursorInsert(pCur, pKey, keyLen, pVal, valLen); - if (ret < 0) { - return -1; - } - - return 0; + return tdbBtreeInsert(pDb->pBt, pKey, keyLen, pVal, valLen); } int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) { diff --git a/source/libs/tdb/src/inc/tdbBtree.h b/source/libs/tdb/src/inc/tdbBtree.h index 2797b5ea5e..2eba5f4f1a 100644 --- a/source/libs/tdb/src/inc/tdbBtree.h +++ b/source/libs/tdb/src/inc/tdbBtree.h @@ -40,7 +40,7 @@ struct SBTC { // SBTree int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt); int tdbBtreeClose(SBTree *pBt); -int tdbBtCursorInsert(SBTC *pCur, const void *pKey, int kLen, const void *pVal, int vLen); +int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen); int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen); int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen);