From 3904018757989718cd27223d61e08d2c4864b470 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 08:49:40 +0000 Subject: [PATCH 01/26] refact more --- source/libs/tdb/src/db/tdbBtree.c | 34 ++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index fc27e88cf1..adfbbc37ad 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -1056,6 +1056,7 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { int tdbBtcMoveToLast(SBTC *pBtc) { int ret; + int nCells; SBTree *pBt; SPager *pPager; SPgno pgno; @@ -1071,7 +1072,16 @@ int tdbBtcMoveToLast(SBTC *pBtc) { return -1; } + nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); pBtc->iPage = 0; + if (nCells > 0) { + pBtc->idx = TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage) ? nCells - 1 : nCells; + } else { + // no data at all, point to an invalid position + ASSERT(TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)); + pBtc->idx = -1; + return 0; + } } else { // move from a position ASSERT(0); @@ -1079,19 +1089,19 @@ int tdbBtcMoveToLast(SBTC *pBtc) { // move downward for (;;) { - if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { - // TODO: handle empty case - ASSERT(TDB_PAGE_TOTAL_CELLS(pBtc->pPage) > 0); - pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage) - 1; - break; - } else { - pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); + if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break; - ret = tdbBtcMoveDownward(pBtc); - if (ret < 0) { - ASSERT(0); - return -1; - } + ret = tdbBtcMoveDownward(pBtc); + if (ret < 0) { + ASSERT(0); + return -1; + } + + nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); + if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { + pBtc->idx = nCells - 1; + } else { + pBtc->idx = nCells; } } From d7cf7e791c16ab44ba23cf3a1b8ebdcda05d8767 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 09:25:30 +0000 Subject: [PATCH 02/26] refact more code --- source/libs/tdb/src/db/tdbBtree.c | 70 ++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index adfbbc37ad..d3534ae5a2 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -1083,8 +1083,28 @@ int tdbBtcMoveToLast(SBTC *pBtc) { return 0; } } else { - // move from a position - ASSERT(0); + int iPage = 0; + + // downward search + for (; iPage < pBtc->iPage; iPage++) { + ASSERT(!TDB_BTREE_PAGE_IS_LEAF(pBtc->pgStack[iPage])); + nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pgStack[iPage]); + if (pBtc->idxStack[iPage] != nCells) break; + } + + // move upward + for (;;) { + if (pBtc->iPage == 0) { + if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { + pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage) - 1; + } else { + pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); + } + } + + if (pBtc->iPage < iPage) break; + tdbBtcMoveUpward(pBtc); + } } // move downward @@ -1114,6 +1134,7 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { void *pKey, *pVal; int ret; + // current cursor points to an invalid position if (pBtc->idx < 0) { return -1; } @@ -1144,12 +1165,17 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { memcpy(pVal, cd.pVal, cd.vLen); ret = tdbBtcMoveToNext(pBtc); + if (ret < 0) { + ASSERT(0); + return -1; + } return 0; } static int tdbBtcMoveToNext(SBTC *pBtc) { int nCells; + int ret; SCell *pCell; ASSERT(TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)); @@ -1161,39 +1187,35 @@ static int tdbBtcMoveToNext(SBTC *pBtc) { return 0; } - if (pBtc->iPage == 0) { - pBtc->idx = -1; - return 0; - } - - // Move upward + // move upward for (;;) { - tdbBtcMoveUpward(pBtc); - pBtc->idx++; - - nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); - if (pBtc->idx <= nCells) { - break; - } - if (pBtc->iPage == 0) { pBtc->idx = -1; return 0; } - } - // Move downward - for (;;) { - nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); + tdbBtcMoveUpward(pBtc); + pBtc->idx++; - tdbBtcMoveDownward(pBtc); - pBtc->idx = 0; - - if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { + ASSERT(!TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)); + if (pBtc->idx <= TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) { break; } } + // move downward + for (;;) { + if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break; + + ret = tdbBtcMoveDownward(pBtc); + if (ret < 0) { + ASSERT(0); + return -1; + } + + pBtc->idx = 0; + } + return 0; } From b5e9f2aca0096ba00dfd6adcfc37d75e17a3aa79 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 09:58:33 +0000 Subject: [PATCH 03/26] 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); From cee75c2ba338056e17f2fd47b8732a9365b92d1b Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 10:01:12 +0000 Subject: [PATCH 04/26] more --- source/libs/tdb/src/db/tdbBtree.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index b1728b53e7..848500875d 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -162,7 +162,7 @@ int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, in } } - // make sure enough space to hold the space + // make sure enough space to hold the cell szBuf = kLen + vLen + 14; pBuf = TDB_REALLOC(pBt->pBuf, pBt->pageSize > szBuf ? szBuf : pBt->pageSize); if (pBuf == NULL) { @@ -181,6 +181,14 @@ int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, in return -1; } + // mark the page dirty + ret = tdbPagerWrite(pBt->pPager, btc.pPage); + if (ret < 0) { + tdbBtcClose(&btc); + ASSERT(0); + return -1; + } + // insert the cell ret = tdbPageInsertCell(btc.pPage, idx, pCell, szCell, 0); if (ret < 0) { From 0a62868d5ee8e1b0d637209fd9b617adb3f9da8e Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 10:07:38 +0000 Subject: [PATCH 05/26] refact more --- source/libs/tdb/src/db/tdbBtree.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 848500875d..a4ca75ff36 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -216,15 +216,22 @@ int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen SBTC btc; SCell *pCell; int cret; + int ret; void *pVal; SCellDecoder cd; tdbBtcOpen(&btc, pBt); - tdbBtcMoveTo(&btc, pKey, kLen, &cret); + ret = tdbBtcMoveTo(&btc, pKey, kLen, &cret); + if (ret < 0) { + tdbBtcClose(&btc); + ASSERT(0); + return -1; + } if (cret) { - return cret; + tdbBtcClose(&btc); + return -1; } pCell = tdbPageGetCell(btc.pPage, btc.idx); @@ -233,11 +240,15 @@ int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen *vLen = cd.vLen; pVal = TDB_REALLOC(*ppVal, *vLen); if (pVal == NULL) { + tdbBtcClose(&btc); return -1; } *ppVal = pVal; memcpy(*ppVal, cd.pVal, cd.vLen); + + tdbBtcClose(&btc); + return 0; } From f75f60c84c8c9571fa4caf8537363403b7ad20fc Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 10:18:04 +0000 Subject: [PATCH 06/26] refact more --- source/libs/tdb/src/db/tdbBtree.c | 69 +++++++++++-------------------- 1 file changed, 24 insertions(+), 45 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index a4ca75ff36..ef9b03df6c 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -213,11 +213,16 @@ int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, in } int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen) { + return tdbBtreePGet(pBt, pKey, kLen, NULL, NULL, ppVal, vLen); +} + +int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen) { SBTC btc; SCell *pCell; int cret; int ret; - void *pVal; + void *pTKey = NULL; + void *pTVal = NULL; SCellDecoder cd; tdbBtcOpen(&btc, pBt); @@ -226,7 +231,6 @@ int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen if (ret < 0) { tdbBtcClose(&btc); ASSERT(0); - return -1; } if (cret) { @@ -237,14 +241,26 @@ int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen pCell = tdbPageGetCell(btc.pPage, btc.idx); tdbBtreeDecodeCell(btc.pPage, pCell, &cd); - *vLen = cd.vLen; - pVal = TDB_REALLOC(*ppVal, *vLen); - if (pVal == NULL) { - tdbBtcClose(&btc); - return -1; + if (ppKey) { + pTKey = TDB_REALLOC(*ppKey, cd.kLen); + if (pTKey == NULL) { + tdbBtcClose(&btc); + ASSERT(0); + return -1; + } + *ppKey = pTKey; + *pkLen = cd.kLen; + memcpy(*ppKey, cd.pKey, cd.kLen); } - *ppVal = pVal; + pTVal = TDB_REALLOC(*ppVal, cd.vLen); + if (pTVal == NULL) { + tdbBtcClose(&btc); + ASSERT(0); + return -1; + } + *ppVal = pTVal; + *vLen = cd.vLen; memcpy(*ppVal, cd.pVal, cd.vLen); tdbBtcClose(&btc); @@ -252,43 +268,6 @@ int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen return 0; } -int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen) { - SBTC btc; - SCell *pCell; - int cret; - void *pTKey; - void *pTVal; - SCellDecoder cd; - - tdbBtcOpen(&btc, pBt); - - tdbBtcMoveTo(&btc, pKey, kLen, &cret); - if (cret) { - return cret; - } - - pCell = tdbPageGetCell(btc.pPage, btc.idx); - tdbBtreeDecodeCell(btc.pPage, pCell, &cd); - - pTKey = TDB_REALLOC(*ppKey, cd.kLen); - pTVal = TDB_REALLOC(*ppVal, cd.vLen); - - if (pTKey == NULL || pTVal == NULL) { - TDB_FREE(pTKey); - TDB_FREE(pTVal); - } - - *ppKey = pTKey; - *ppVal = pTVal; - *pkLen = cd.kLen; - *vLen = cd.vLen; - - memcpy(*ppKey, cd.pKey, cd.kLen); - memcpy(*ppVal, cd.pVal, cd.vLen); - - return 0; -} - static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2) { int mlen; int cret; From a6f0d2b55396d01f045f3cdc39df9b867fe5bcbb Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 10:31:28 +0000 Subject: [PATCH 07/26] refact more --- source/libs/tdb/src/db/tdbPager.c | 1 - source/libs/tdb/src/inc/tdbOs.h | 6 +++++- source/libs/tdb/test/tdbTest.cpp | 18 ++---------------- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 748633da34..b00bba2f66 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -210,7 +210,6 @@ int tdbPagerCommit(SPager *pPager) { tdbOsClose(pPager->jfd); tdbOsRemove(pPager->jFileName); - // pPager->jfd = -1; return 0; } diff --git a/source/libs/tdb/src/inc/tdbOs.h b/source/libs/tdb/src/inc/tdbOs.h index 196bbdafc7..5259cfb4d3 100644 --- a/source/libs/tdb/src/inc/tdbOs.h +++ b/source/libs/tdb/src/inc/tdbOs.h @@ -95,7 +95,11 @@ typedef int tdb_fd_t; #define tdbOsOpen(PATH, OPTION, MODE) open((PATH), (OPTION), (MODE)) -#define tdbOsClose close +#define tdbOsClose(FD) \ + do { \ + close(FD); \ + (FD) = -1; \ + } while (0) i64 tdbOsRead(tdb_fd_t fd, void *pData, i64 nBytes); i64 tdbOsPRead(tdb_fd_t fd, void *pData, i64 nBytes, i64 offset); diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index f41e2bcbee..2cb318a4d0 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -134,13 +134,7 @@ TEST(tdb_test, simple_test) { char val[64]; { // Insert some data - int i = 1; - SPoolMem *pPool; - int memPoolCapacity = 16 * 1024; - - pPool = openPool(); - - tdbTxnBegin(pEnv); + int i = 1; for (;;) { if (i > nData) break; @@ -150,18 +144,10 @@ TEST(tdb_test, simple_test) { ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val)); GTEST_ASSERT_EQ(ret, 0); - if (pPool->size >= memPoolCapacity) { - tdbTxnCommit(pEnv); - - clearPool(pPool); - - tdbTxnBegin(pEnv); - } - i++; } - closePool(pPool); + // tdbPagerCommit() } { // Query the data From cb4bd33c80fc1966a0066f697e6468e72d1dabd3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 10:46:08 +0000 Subject: [PATCH 08/26] more --- source/libs/tdb/src/db/tdbTxn.c | 12 ++++++++++++ source/libs/tdb/test/tdbTest.cpp | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/src/db/tdbTxn.c b/source/libs/tdb/src/db/tdbTxn.c index fd4d5de60e..bb0bc6c845 100644 --- a/source/libs/tdb/src/db/tdbTxn.c +++ b/source/libs/tdb/src/db/tdbTxn.c @@ -21,6 +21,18 @@ int tdbTxnBegin(TENV *pEnv) { } int tdbTxnCommit(TENV *pEnv) { + SPager *pPager = NULL; + int ret; + + for (;;) { + break; + ret = tdbPagerCommit(pPager); + if (ret < 0) { + ASSERT(0); + return -1; + } + } + // TODO return 0; } diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 2cb318a4d0..a976a9e581 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -147,7 +147,7 @@ TEST(tdb_test, simple_test) { i++; } - // tdbPagerCommit() + tdbTxnCommit(pEnv); } { // Query the data From 110703f9dfaba749bed52185cd1acfd9889b4742 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 10:51:07 +0000 Subject: [PATCH 09/26] refact more --- source/libs/tdb/src/db/tdbEnv.c | 15 +++++++++++ source/libs/tdb/src/db/tdbTxn.c | 44 ++++++++++++++++---------------- source/libs/tdb/src/inc/tdbEnv.h | 5 ++++ source/libs/tdb/src/inc/tdbInt.h | 17 ------------ source/libs/tdb/src/inc/tdbTxn.h | 4 --- source/libs/tdb/test/tdbTest.cpp | 2 +- 6 files changed, 43 insertions(+), 44 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 4439147e09..5a5c281ed1 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -64,6 +64,21 @@ int tdbEnvClose(TENV *pEnv) { return 0; } +int tdbBegin(TENV *pEnv) { + // TODO + return 0; +} + +int tdbCommit(TENV *pEnv) { + // TODO + return 0; +} + +int tdbRollback(TENV *pEnv) { + // TODO + return 0; +} + SPager *tdbEnvGetPager(TENV *pEnv, const char *fname) { // TODO return NULL; diff --git a/source/libs/tdb/src/db/tdbTxn.c b/source/libs/tdb/src/db/tdbTxn.c index bb0bc6c845..03bcbb44a7 100644 --- a/source/libs/tdb/src/db/tdbTxn.c +++ b/source/libs/tdb/src/db/tdbTxn.c @@ -15,29 +15,29 @@ #include "tdbInt.h" -int tdbTxnBegin(TENV *pEnv) { - // TODO - return 0; -} +// int tdbTxnBegin(TENV *pEnv) { +// // TODO +// return 0; +// } -int tdbTxnCommit(TENV *pEnv) { - SPager *pPager = NULL; - int ret; +// int tdbTxnCommit(TENV *pEnv) { +// SPager *pPager = NULL; +// int ret; - for (;;) { - break; - ret = tdbPagerCommit(pPager); - if (ret < 0) { - ASSERT(0); - return -1; - } - } +// for (;;) { +// break; +// ret = tdbPagerCommit(pPager); +// if (ret < 0) { +// ASSERT(0); +// return -1; +// } +// } - // TODO - return 0; -} +// // TODO +// return 0; +// } -int tdbTxnRollback(TENV *pEnv) { - // TODO - return 0; -} \ No newline at end of file +// int tdbTxnRollback(TENV *pEnv) { +// // TODO +// return 0; +// } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index a651c3a12e..693be86cd8 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -25,10 +25,15 @@ typedef struct STEnv { char *jfname; int jfd; SPCache *pCache; + int nHash; + SPager **pagerHash; } TENV; int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv); int tdbEnvClose(TENV *pEnv); +int tdbBegin(TENV *pEnv); +int tdbCommit(TENV *pEnv); +int tdbRollback(TENV *pEnv); SPager *tdbEnvGetPager(TENV *pEnv, const char *fname); diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 361a460cef..26f9247952 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -91,23 +91,6 @@ static FORCE_INLINE int tdbCmprPgId(const void *p1, const void *p2) { // dbname #define TDB_MAX_DBNAME_LEN 24 -// tdb_log -#define tdbError(var) - -#define TERR_A(val, op, flag) \ - do { \ - if (((val) = (op)) != 0) { \ - goto flag; \ - } \ - } while (0) - -#define TERR_B(val, op, flag) \ - do { \ - if (((val) = (op)) == NULL) { \ - goto flag; \ - } \ - } while (0) - #define TDB_VARIANT_LEN ((int)-1) typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, int kLen2); diff --git a/source/libs/tdb/src/inc/tdbTxn.h b/source/libs/tdb/src/inc/tdbTxn.h index cc11369785..0be2dad3c2 100644 --- a/source/libs/tdb/src/inc/tdbTxn.h +++ b/source/libs/tdb/src/inc/tdbTxn.h @@ -28,10 +28,6 @@ struct STxn { void *xArg; }; -int tdbTxnBegin(TENV *pEnv); -int tdbTxnCommit(TENV *pEnv); -int tdbTxnRollback(TENV *pEnv); - #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index a976a9e581..2d19596ca6 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -147,7 +147,7 @@ TEST(tdb_test, simple_test) { i++; } - tdbTxnCommit(pEnv); + tdbCommit(pEnv); } { // Query the data From 4ecbe41d134bef9e69d93458b83fb4d04a9efe7a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 11:07:58 +0000 Subject: [PATCH 10/26] adjust some code --- source/libs/tdb/src/db/tdbBtree.c | 2 +- source/libs/tdb/src/db/tdbEnv.c | 25 ++++++++++++++++++++++--- source/libs/tdb/src/db/tdbPager.c | 16 ---------------- source/libs/tdb/src/inc/tdbEnv.h | 5 +++++ source/libs/tdb/src/inc/tdbPager.h | 15 ++++++++++++++- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index ef9b03df6c..f7e0d7181a 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -101,7 +101,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, S // pBt->kcmpr pBt->kcmpr = kcmpr ? kcmpr : tdbDefaultKeyCmprFn; // pBt->pageSize - pBt->pageSize = tdbPagerGetPageSize(pPager); + pBt->pageSize = pPager->pageSize; // pBt->maxLocal pBt->maxLocal = tdbPageCapacity(pBt->pageSize, sizeof(SIntHdr)) / 4; // pBt->minLocal: Should not be allowed smaller than 15, which is [nPayload][nKey][nData] diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 5a5c281ed1..a981e8c99e 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -19,6 +19,7 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv) { TENV *pEnv; int dsize; int zsize; + int tsize; u8 *pPtr; int ret; @@ -53,6 +54,14 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv) { return -1; } + pEnv->nHash = 8; + tsize = sizeof(SPager *) * pEnv->nHash; + pEnv->pagerHash = TDB_REALLOC(pEnv->pagerHash, tsize); + if (pEnv->pagerHash == NULL) { + return -1; + } + memset(pEnv->pagerHash, 0, tsize); + mkdir(rootDir, 0755); *ppEnv = pEnv; @@ -65,21 +74,31 @@ int tdbEnvClose(TENV *pEnv) { } int tdbBegin(TENV *pEnv) { - // TODO + ASSERT(0); return 0; } int tdbCommit(TENV *pEnv) { - // TODO + SPager *pPager; + + pPager = pEnv->pagerList; + while (pPager) { + tdbPagerCommit(pPager); + } + return 0; } int tdbRollback(TENV *pEnv) { - // TODO + ASSERT(0); return 0; } SPager *tdbEnvGetPager(TENV *pEnv, const char *fname) { // TODO return NULL; +} + +static void tdbEnvAddPager(TENV *pEnv, SPager *pPager) { + } \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index b00bba2f66..818b8a371a 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -15,20 +15,6 @@ #include "tdbInt.h" -struct SPager { - char *dbFileName; - char *jFileName; - int pageSize; - uint8_t fid[TDB_FILE_ID_LEN]; - tdb_fd_t fd; - tdb_fd_t jfd; - SPCache *pCache; - SPgno dbFileSize; - SPgno dbOrigSize; - SPage *pDirty; - u8 inTran; -}; - typedef struct __attribute__((__packed__)) { u8 hdrString[16]; u16 pageSize; @@ -229,8 +215,6 @@ static int tdbPagerReadPage(SPager *pPager, SPage *pPage) { return 0; } -int tdbPagerGetPageSize(SPager *pPager) { return pPager->pageSize; } - int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg) { SPage *pPage; SPgid pgid; diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index 693be86cd8..3eb3ebb328 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -20,11 +20,16 @@ extern "C" { #endif +#define TDB_PAGER_ENV_FIELDS \ + SPager *pNext; \ + SPager *pHashNext; + typedef struct STEnv { char *rootDir; char *jfname; int jfd; SPCache *pCache; + SPager *pagerList; int nHash; SPager **pagerHash; } TENV; diff --git a/source/libs/tdb/src/inc/tdbPager.h b/source/libs/tdb/src/inc/tdbPager.h index f4cc822f27..7ae86eb3ac 100644 --- a/source/libs/tdb/src/inc/tdbPager.h +++ b/source/libs/tdb/src/inc/tdbPager.h @@ -20,13 +20,26 @@ extern "C" { #endif +struct SPager { + char *dbFileName; + char *jFileName; + int pageSize; + uint8_t fid[TDB_FILE_ID_LEN]; + tdb_fd_t fd; + tdb_fd_t jfd; + SPCache *pCache; + SPgno dbFileSize; + SPgno dbOrigSize; + SPage *pDirty; + u8 inTran; +}; + int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager); int tdbPagerClose(SPager *pPager); int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate); int tdbPagerWrite(SPager *pPager, SPage *pPage); int tdbPagerBegin(SPager *pPager); int tdbPagerCommit(SPager *pPager); -int tdbPagerGetPageSize(SPager *pPager); int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg); int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg); void tdbPagerReturnPage(SPager *pPager, SPage *pPage); From a466372179f897abb4e3bbe3546a4c06e2795582 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 02:10:26 +0000 Subject: [PATCH 11/26] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 71 ++++++++++++++++++++++++++---- source/libs/tdb/src/inc/tdbEnv.h | 13 +++--- source/libs/tdb/src/inc/tdbPager.h | 2 + source/libs/tdb/src/inc/tdbUtil.h | 5 +++ 4 files changed, 75 insertions(+), 16 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index a981e8c99e..6e74aefc62 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -54,13 +54,13 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv) { return -1; } - pEnv->nHash = 8; - tsize = sizeof(SPager *) * pEnv->nHash; - pEnv->pagerHash = TDB_REALLOC(pEnv->pagerHash, tsize); - if (pEnv->pagerHash == NULL) { + pEnv->nPgrHash = 8; + tsize = sizeof(SPager *) * pEnv->nPgrHash; + pEnv->pgrHash = TDB_REALLOC(pEnv->pgrHash, tsize); + if (pEnv->pgrHash == NULL) { return -1; } - memset(pEnv->pagerHash, 0, tsize); + memset(pEnv->pgrHash, 0, tsize); mkdir(rootDir, 0755); @@ -81,7 +81,7 @@ int tdbBegin(TENV *pEnv) { int tdbCommit(TENV *pEnv) { SPager *pPager; - pPager = pEnv->pagerList; + pPager = pEnv->pgrList; while (pPager) { tdbPagerCommit(pPager); } @@ -95,10 +95,63 @@ int tdbRollback(TENV *pEnv) { } SPager *tdbEnvGetPager(TENV *pEnv, const char *fname) { - // TODO - return NULL; + int hash; + SPager **ppPager; + + hash = tdbCstringHash(fname); + ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash]; + for (; *ppPager && (strcmp(fname, (*ppPager)->dbFileName) != 0); ppPager = &((*ppPager)->pHashNext)) { + } + + return *ppPager; } -static void tdbEnvAddPager(TENV *pEnv, SPager *pPager) { +void tdbEnvAddPager(TENV *pEnv, SPager *pPager) { + int hash; + SPager **ppPager; + // rehash if neccessary + if (pEnv->nPager + 1 > pEnv->nPgrHash) { + // TODO + } + + // add to list + pPager->pNext = pEnv->pgrList; + pEnv->pgrList = pPager; + + // add to hash + hash = tdbCstringHash(pPager->dbFileName); + ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash]; + pPager->pHashNext = *ppPager; + *ppPager = pPager; + + // increase the counter + pEnv->nPager++; +} + +void tdbEnvRemovePager(TENV *pEnv, SPager *pPager) { + int hash; + SPager **ppPager; + + // remove from the list + for (ppPager = &pEnv->pgrList; *ppPager && (*ppPager != pPager); ppPager = &((*ppPager)->pNext)) { + } + ASSERT(*ppPager == pPager); + *ppPager = pPager->pNext; + + // remove from hash + hash = tdbCstringHash(pPager->dbFileName); + ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash]; + for (; *ppPager && *ppPager != pPager; ppPager = &((*ppPager)->pHashNext)) { + } + ASSERT(*ppPager == pPager); + *ppPager = pPager->pNext; + + // decrease the counter + pEnv->nPager--; + + // rehash if necessary + if (pEnv->nPgrHash > 8 && pEnv->nPager < pEnv->nPgrHash / 2) { + // TODO + } } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index 3eb3ebb328..e10c5d54e0 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -20,18 +20,15 @@ extern "C" { #endif -#define TDB_PAGER_ENV_FIELDS \ - SPager *pNext; \ - SPager *pHashNext; - typedef struct STEnv { char *rootDir; char *jfname; int jfd; SPCache *pCache; - SPager *pagerList; - int nHash; - SPager **pagerHash; + SPager *pgrList; + int nPager; + int nPgrHash; + SPager **pgrHash; } TENV; int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv); @@ -40,6 +37,8 @@ int tdbBegin(TENV *pEnv); int tdbCommit(TENV *pEnv); int tdbRollback(TENV *pEnv); +void tdbEnvAddPager(TENV *pEnv, SPager *pPager); +void tdbEnvRemovePager(TENV *pEnv, SPager *pPager); SPager *tdbEnvGetPager(TENV *pEnv, const char *fname); #ifdef __cplusplus diff --git a/source/libs/tdb/src/inc/tdbPager.h b/source/libs/tdb/src/inc/tdbPager.h index 7ae86eb3ac..81b6074431 100644 --- a/source/libs/tdb/src/inc/tdbPager.h +++ b/source/libs/tdb/src/inc/tdbPager.h @@ -32,6 +32,8 @@ struct SPager { SPgno dbOrigSize; SPage *pDirty; u8 inTran; + SPager *pNext; // used by TENV + SPager *pHashNext; // used by TENV }; int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager); diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index c06d9d18c9..a4cb09ea94 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -101,6 +101,11 @@ static inline int tdbGetVarInt(const u8 *p, int *v) { return n; } +static inline int tdbCstringHash(const char *s) { + // TODO + return 0; +} + #ifdef __cplusplus } #endif From 14bd838a9f285464b1025634a77f0a1d6c56e522 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 02:23:58 +0000 Subject: [PATCH 12/26] more TDB --- .clang-format | 1 + source/libs/tdb/src/db/tdbEnv.c | 6 +++--- source/libs/tdb/src/inc/tdbInt.h | 2 -- source/libs/tdb/src/inc/tdbOs.h | 2 ++ source/libs/tdb/src/inc/tdbUtil.h | 5 +---- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.clang-format b/.clang-format index 3ddd8b43f6..e58d518b3b 100644 --- a/.clang-format +++ b/.clang-format @@ -5,6 +5,7 @@ AccessModifierOffset: -1 AlignAfterOpenBracket: Align AlignConsecutiveAssignments: false AlignConsecutiveDeclarations: true +AlignConsecutiveMacros: true AlignEscapedNewlinesLeft: true AlignOperands: true AlignTrailingComments: true diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 6e74aefc62..a9e29d3f73 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -95,7 +95,7 @@ int tdbRollback(TENV *pEnv) { } SPager *tdbEnvGetPager(TENV *pEnv, const char *fname) { - int hash; + u32 hash; SPager **ppPager; hash = tdbCstringHash(fname); @@ -107,7 +107,7 @@ SPager *tdbEnvGetPager(TENV *pEnv, const char *fname) { } void tdbEnvAddPager(TENV *pEnv, SPager *pPager) { - int hash; + u32 hash; SPager **ppPager; // rehash if neccessary @@ -130,7 +130,7 @@ void tdbEnvAddPager(TENV *pEnv, SPager *pPager) { } void tdbEnvRemovePager(TENV *pEnv, SPager *pPager) { - int hash; + u32 hash; SPager **ppPager; // remove from the list diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 26f9247952..57e01f904c 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -16,8 +16,6 @@ #ifndef _TD_TDB_INTERNAL_H_ #define _TD_TDB_INTERNAL_H_ -#include "os.h" - #include "tdb.h" #ifdef __cplusplus diff --git a/source/libs/tdb/src/inc/tdbOs.h b/source/libs/tdb/src/inc/tdbOs.h index 5259cfb4d3..ae389708f4 100644 --- a/source/libs/tdb/src/inc/tdbOs.h +++ b/source/libs/tdb/src/inc/tdbOs.h @@ -24,6 +24,8 @@ extern "C" { #define TDB_FOR_TDENGINE #ifdef TDB_FOR_TDENGINE +#include "os.h" +#include "thash.h" // For memory ----------------- #define tdbOsMalloc taosMemoryMalloc diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index a4cb09ea94..6abddb5b22 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -101,10 +101,7 @@ static inline int tdbGetVarInt(const u8 *p, int *v) { return n; } -static inline int tdbCstringHash(const char *s) { - // TODO - return 0; -} +static inline u32 tdbCstringHash(const char *s) { return MurmurHash3_32(s, strlen(s)); } #ifdef __cplusplus } From 34d9d1b9df91e215a52c9cdbdd62df2a4cec8064 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 02:25:29 +0000 Subject: [PATCH 13/26] more --- source/libs/tdb/src/db/tdbEnv.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index a9e29d3f73..c78dd67146 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -81,10 +81,10 @@ int tdbBegin(TENV *pEnv) { int tdbCommit(TENV *pEnv) { SPager *pPager; - pPager = pEnv->pgrList; - while (pPager) { - tdbPagerCommit(pPager); - } + // pPager = pEnv->pgrList; + // while (pPager) { + // tdbPagerCommit(pPager); + // } return 0; } From 7f08a2b7c30201075736867dce14f221bb914207 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 02:55:07 +0000 Subject: [PATCH 14/26] more TDB --- source/libs/tdb/src/db/tdbDb.c | 2 ++ source/libs/tdb/src/db/tdbEnv.c | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index 0de2a6433f..fe7b8c6d48 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -49,6 +49,8 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF if (ret < 0) { return -1; } + + tdbEnvAddPager(pEnv, pPager); } ASSERT(pPager != NULL); diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index c78dd67146..06d37df653 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -74,17 +74,31 @@ int tdbEnvClose(TENV *pEnv) { } int tdbBegin(TENV *pEnv) { - ASSERT(0); + SPager *pPager; + int ret; + + for (pPager = pEnv->pgrList; pPager; pPager = pPager->pNext) { + ret = tdbPagerBegin(pPager); + if (ret < 0) { + ASSERT(0); + return -1; + } + } + return 0; } int tdbCommit(TENV *pEnv) { SPager *pPager; + int ret; - // pPager = pEnv->pgrList; - // while (pPager) { - // tdbPagerCommit(pPager); - // } + for (pPager = pEnv->pgrList; pPager; pPager = pPager->pNext) { + ret = tdbPagerCommit(pPager); + if (ret < 0) { + ASSERT(0); + return -1; + } + } return 0; } From 839912e4f57cd1927f8658c5608cda467263da16 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 03:55:46 +0000 Subject: [PATCH 15/26] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 132 +++++++++++++++--------------- 1 file changed, 64 insertions(+), 68 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index f7e0d7181a..dae9f1d88d 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -34,10 +34,10 @@ struct SBTree { #define TDB_BTREE_PAGE_COMMON_HDR u8 flags; -#define TDB_BTREE_PAGE_GET_FLAGS(PAGE) (PAGE)->pData[0] +#define TDB_BTREE_PAGE_GET_FLAGS(PAGE) (PAGE)->pData[0] #define TDB_BTREE_PAGE_SET_FLAGS(PAGE, flags) ((PAGE)->pData[0] = (flags)) -#define TDB_BTREE_PAGE_IS_ROOT(PAGE) (TDB_BTREE_PAGE_GET_FLAGS(PAGE) & TDB_BTREE_ROOT) -#define TDB_BTREE_PAGE_IS_LEAF(PAGE) (TDB_BTREE_PAGE_GET_FLAGS(PAGE) & TDB_BTREE_LEAF) +#define TDB_BTREE_PAGE_IS_ROOT(PAGE) (TDB_BTREE_PAGE_GET_FLAGS(PAGE) & TDB_BTREE_ROOT) +#define TDB_BTREE_PAGE_IS_LEAF(PAGE) (TDB_BTREE_PAGE_GET_FLAGS(PAGE) & 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)) @@ -1274,91 +1274,87 @@ static int tdbBtcMoveUpward(SBTC *pBtc) { } static int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { - int ret; - SBTree *pBt; - SPager *pPager; + int ret; + SBTree *pBt; + SCell *pCell; + SPager *pPager; + SCellDecoder cd = {0}; pBt = pBtc->pBt; pPager = pBt->pPager; if (pBtc->iPage < 0) { - ASSERT(pBtc->iPage == -1); - ASSERT(pBtc->idx == -1); - - // Move from the root + // move from a clear cursor ret = tdbPagerFetchPage(pPager, pBt->root, &(pBtc->pPage), tdbBtreeInitPage, pBt); if (ret < 0) { + // TODO ASSERT(0); - return -1; - } - - pBtc->iPage = 0; - - if (TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0) { - // Current page is empty - // ASSERT(TDB_FLAG_IS(TDB_PAGE_FLAGS(pBtc->pPage), TDB_BTREE_ROOT | TDB_BTREE_LEAF)); return 0; } + pBtc->iPage = 0; + pBtc->idx = -1; + // for empty tree, just return with an invalid position + if (TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0) return 0; + } else { + // move upward to a page that the search key is in the range + ASSERT(0); + } + + // search downward to the leaf + for (;;) { + int lidx, ridx, midx, c, nCells; + SPage *pPage; + + pPage = pBtc->pPage; + nCells = TDB_PAGE_TOTAL_CELLS(pPage); + lidx = 0; + ridx = nCells - 1; + + ASSERT(nCells > 0); + ASSERT(pBtc->idx == -1); + + // binary search for (;;) { - int lidx, ridx, midx, c, nCells; - SCell *pCell; - SPage *pPage; - SCellDecoder cd = {0}; + if (lidx > ridx) break; - pPage = pBtc->pPage; - nCells = TDB_PAGE_TOTAL_CELLS(pPage); - lidx = 0; - ridx = nCells - 1; + midx = (lidx + ridx) >> 1; - ASSERT(nCells > 0); - - for (;;) { - if (lidx > ridx) break; - - midx = (lidx + ridx) >> 1; - - pCell = tdbPageGetCell(pPage, midx); - ret = tdbBtreeDecodeCell(pPage, pCell, &cd); - if (ret < 0) { - // TODO: handle error - ASSERT(0); - return -1; - } - - // Compare the key values - c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); - if (c < 0) { - /* input-key < cell-key */ - ridx = midx - 1; - } else if (c > 0) { - /* input-key > cell-key */ - lidx = midx + 1; - } else { - /* input-key == cell-key */ - break; - } + pCell = tdbPageGetCell(pPage, midx); + ret = tdbBtreeDecodeCell(pPage, pCell, &cd); + if (ret < 0) { + // TODO: handle error + ASSERT(0); + return -1; } - // Move downward or break - u8 leaf = TDB_BTREE_PAGE_IS_LEAF(pPage); - if (leaf) { - pBtc->idx = midx; - *pCRst = c; - break; + // Compare the key values + c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); + if (c < 0) { + // pKey < cd.pKey + ridx = midx - 1; + } else if (c > 0) { + // pKey > cd.pKey + lidx = midx + 1; } else { - if (c <= 0) { - pBtc->idx = midx; - } else { - pBtc->idx = midx + 1; - } - tdbBtcMoveDownward(pBtc); + // pKey == cd.pKey + break; } } - } else { - // TODO: Move the cursor from a some position instead of a clear state - ASSERT(0); + // keep search downward or break + if (TDB_BTREE_PAGE_IS_LEAF(pPage)) { + pBtc->idx = midx; + *pCRst = c; + break; + } else { + if (c <= 0) { + pBtc->idx = midx; + } else { + pBtc->idx = midx + 1; + } + tdbBtcMoveDownward(pBtc); + } } return 0; From 59061a6fb562b1922559f78606337518fefe97e7 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 06:22:29 +0000 Subject: [PATCH 16/26] refact more --- source/libs/tdb/src/db/tdbBtree.c | 47 ++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index dae9f1d88d..32a04c6272 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -1040,12 +1040,11 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { // move upward for (;;) { - if (pBtc->iPage == 0) { + if (pBtc->iPage == iPage) { pBtc->idx = 0; break; } - if (pBtc->iPage < iPage) break; tdbBtcMoveUpward(pBtc); } } @@ -1106,15 +1105,15 @@ int tdbBtcMoveToLast(SBTC *pBtc) { // move upward for (;;) { - if (pBtc->iPage == 0) { + if (pBtc->iPage == iPage) { if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage) - 1; } else { pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); } + break; } - if (pBtc->iPage < iPage) break; tdbBtcMoveUpward(pBtc); } } @@ -1275,6 +1274,8 @@ static int tdbBtcMoveUpward(SBTC *pBtc) { static int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { int ret; + int nCells; + int c; SBTree *pBt; SCell *pCell; SPager *pPager; @@ -1297,13 +1298,45 @@ static int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { // for empty tree, just return with an invalid position if (TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0) return 0; } else { - // move upward to a page that the search key is in the range - ASSERT(0); + SPage *pPage; + int idx; + int iPage = 0; + + // downward search + for (; iPage < pBtc->iPage; iPage++) { + pPage = pBtc->pgStack[iPage]; + idx = pBtc->idxStack[iPage]; + nCells = TDB_PAGE_TOTAL_CELLS(pPage); + + ASSERT(!TDB_BTREE_PAGE_IS_LEAF(pPage)); + + // check if key <= current position + if (idx < nCells) { + pCell = tdbPageGetCell(pPage, idx); + tdbBtreeDecodeCell(pPage, pCell, &cd); + c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); + if (c > 0) break; + } + + // check if key > current - 1 position + if (idx > 0) { + pCell = tdbPageGetCell(pPage, idx - 1); + tdbBtreeDecodeCell(pPage, pCell, &cd); + c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); + if (c <= 0) break; + } + } + + // move upward + for (;;) { + if (pBtc->iPage == iPage) break; + tdbBtcMoveUpward(pBtc); + } } // search downward to the leaf for (;;) { - int lidx, ridx, midx, c, nCells; + int lidx, ridx, midx; SPage *pPage; pPage = pBtc->pPage; From ba4b33e7c7ed98eeba709fb84bf8b2f15cc0cfd9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 07:26:51 +0000 Subject: [PATCH 17/26] more progress --- source/libs/tdb/src/db/tdbBtree.c | 49 ++++++++++++++++++++++--------- source/libs/tdb/src/db/tdbPager.c | 42 ++++++++++++++------------ source/libs/tdb/src/inc/tdbPage.h | 1 + source/libs/tdb/test/tdbTest.cpp | 3 +- 4 files changed, 62 insertions(+), 33 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 32a04c6272..81aa591808 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -383,17 +383,7 @@ static int tdbBtreeZeroPage(SPage *pPage, void *arg) { return 0; } -#ifndef TDB_BTREE_BALANCE -typedef struct { - SBTree *pBt; - SPage *pParent; - int idx; - i8 nOld; - SPage *pOldPages[3]; - i8 nNewPages; - SPage *pNewPages[5]; -} SBtreeBalanceHelper; - +// TDB_BTREE_BALANCE ===================== static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) { SPager *pPager; SPage *pChild; @@ -420,6 +410,13 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) { ((SIntHdr *)pChild->pData)->pgno = ((SIntHdr *)(pRoot->pData))->pgno; } + ret = tdbPagerWrite(pPager, pChild); + if (ret < 0) { + // TODO + ASSERT(0); + return 0; + } + // Copy the root page content to the child page tdbPageCopy(pRoot, pChild); @@ -497,6 +494,13 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { } if (i < nOlds - 1) { + ret = tdbPagerWrite(pBt->pPager, pOlds[i]); + if (ret < 0) { + // TODO + ASSERT(0); + return -1; + } + ((SPgno *)pDivCell[i])[0] = ((SIntHdr *)pOlds[i]->pData)->pgno; ((SIntHdr *)pOlds[i]->pData)->pgno = 0; tdbPageInsertCell(pOlds[i], TDB_PAGE_TOTAL_CELLS(pOlds[i]), pDivCell[i], szDivCell[i], 1); @@ -504,6 +508,14 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { } rPgno = ((SIntHdr *)pOlds[nOlds - 1]->pData)->pgno; } + + ret = tdbPagerWrite(pBt->pPager, pParent); + if (ret < 0) { + // TODO + ASSERT(0); + return -1; + } + // drop the cells on parent page for (int i = 0; i < nOlds; i++) { nCells = TDB_PAGE_TOTAL_CELLS(pParent); @@ -631,6 +643,13 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { if (ret < 0) { ASSERT(0); } + + ret = tdbPagerWrite(pBt->pPager, pNews[iNew]); + if (ret < 0) { + // TODO + ASSERT(0); + return -1; + } } } @@ -749,9 +768,10 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { static int tdbBtreeBalance(SBTC *pBtc) { int iPage; + int ret; + int nFree; SPage *pParent; SPage *pPage; - int ret; u8 flags; u8 leaf; u8 root; @@ -762,10 +782,11 @@ static int tdbBtreeBalance(SBTC *pBtc) { pPage = pBtc->pPage; leaf = TDB_BTREE_PAGE_IS_LEAF(pPage); root = TDB_BTREE_PAGE_IS_ROOT(pPage); + nFree = TDB_PAGE_FREE_SIZE(pPage); // when the page is not overflow and not too empty, the balance work // is finished. Just break out the balance loop. - if (pPage->nOverflow == 0 /* TODO: && pPage->nFree <= */) { + if (pPage->nOverflow == 0 && nFree < TDB_PAGE_USABLE_SIZE(pPage) * 2 / 3) { break; } @@ -800,7 +821,7 @@ static int tdbBtreeBalance(SBTC *pBtc) { return 0; } -#endif +// TDB_BTREE_BALANCE // TDB_BTREE_CELL ===================== static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const void *pKey, int kLen, const void *pVal, diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 818b8a371a..a37309f032 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -117,14 +117,18 @@ int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate) { } int tdbPagerWrite(SPager *pPager, SPage *pPage) { - int ret; + int ret; + SPage **ppPage; + ASSERT(pPager->inTran); +#if 0 if (pPager->inTran == 0) { ret = tdbPagerBegin(pPager); if (ret < 0) { return -1; } } +#endif if (pPage->isDirty) return 0; @@ -132,11 +136,14 @@ int tdbPagerWrite(SPager *pPager, SPage *pPage) { pPage->isDirty = 1; // Add page to dirty list - // TODO: sort the list according to the page number - pPage->pDirtyNext = pPager->pDirty; - pPager->pDirty = pPage; + for (ppPage = &pPager->pDirty; (*ppPage) && TDB_PAGE_PGNO(*ppPage) < TDB_PAGE_PGNO(pPage); + ppPage = &((*ppPage)->pDirtyNext)) { + } + ASSERT(*ppPage == NULL || TDB_PAGE_PGNO(*ppPage) > TDB_PAGE_PGNO(pPage)); + pPage->pDirtyNext = *ppPage; + *ppPage = pPage; - // Write page to journal + // Write page to journal if neccessary if (TDB_PAGE_PGNO(pPage) <= pPager->dbOrigSize) { ret = tdbPagerWritePageToJournal(pPager, pPage); if (ret < 0) { @@ -170,30 +177,29 @@ int tdbPagerCommit(SPager *pPager) { SPage *pPage; int ret; - // Begin commit - { - // TODO: Sync the journal file (Here or when write ?) + // sync the journal file + ret = tdbOsFSync(pPager->jfd); + if (ret < 0) { + // TODO + ASSERT(0); + return 0; } - for (;;) { - pPage = pPager->pDirty; - - if (pPage == NULL) break; - + // loop to write the dirty pages to file + for (pPage = pPager->pDirty; pPage; pPage = pPage->pDirtyNext) { ret = tdbPagerWritePageToDB(pPager, pPage); if (ret < 0) { ASSERT(0); return -1; } - - pPager->pDirty = pPage->pDirtyNext; - pPage->pDirtyNext = NULL; - - // TODO: release the page } + // TODO: loop to release the dirty pages + + // sync the db file tdbOsFSync(pPager->fd); + // remote the journal file tdbOsClose(pPager->jfd); tdbOsRemove(pPager->jFileName); diff --git a/source/libs/tdb/src/inc/tdbPage.h b/source/libs/tdb/src/inc/tdbPage.h index 563fb53e98..77a1ee5f6e 100644 --- a/source/libs/tdb/src/inc/tdbPage.h +++ b/source/libs/tdb/src/inc/tdbPage.h @@ -100,6 +100,7 @@ struct SPage { // APIs #define TDB_PAGE_TOTAL_CELLS(pPage) ((pPage)->nOverflow + (pPage)->pPageMethods->getCellNum(pPage)) #define TDB_PAGE_USABLE_SIZE(pPage) ((u8 *)(pPage)->pPageFtr - (pPage)->pCellIdx) +#define TDB_PAGE_FREE_SIZE(pPage) (*(pPage)->pPageMethods->getFreeBytes)(pPage) #define TDB_PAGE_PGNO(pPage) ((pPage)->pgid.pgno) #define TDB_BYTES_CELL_TAKEN(pPage, pCell) ((*(pPage)->xCellSize)(pPage, pCell) + (pPage)->pPageMethods->szOffset) #define TDB_PAGE_OFFSET_SIZE(pPage) ((pPage)->pPageMethods->szOffset) diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 2d19596ca6..33a82094dc 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -134,8 +134,9 @@ TEST(tdb_test, simple_test) { char val[64]; { // Insert some data - int i = 1; + tdbBegin(pEnv); + int i = 1; for (;;) { if (i > nData) break; From 71c8b91059211c053324707c04c3d5d9bbb66f0c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 07:52:35 +0000 Subject: [PATCH 18/26] more --- source/libs/tdb/src/db/tdbBtree.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 81aa591808..51cd5fbffc 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -312,7 +312,8 @@ static int tdbBtreeOpenImpl(SBTree *pBt) { return -1; } - // TODO: Unref the page + // TODO: here still has problem + tdbPagerReturnPage(pBt->pPager, pPage); ASSERT(pgno != 0); pBt->root = pgno; @@ -763,6 +764,15 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { } } + // TODO: here is not corrent for drop case + for (int i = 0; i < nNews; i++) { + if (i < nOlds) { + tdbPagerReturnPage(pBt->pPager, pOlds[i]); + } else { + tdbPagerReturnPage(pBt->pPager, pNews[i]); + } + } + return 0; } @@ -814,6 +824,8 @@ static int tdbBtreeBalance(SBTC *pBtc) { return -1; } + tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage); + pBtc->iPage--; pBtc->pPage = pBtc->pgStack[pBtc->iPage]; } From 0492362f3821e960ab90b1d57617942ce1427fe9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 08:04:38 +0000 Subject: [PATCH 19/26] more TDB --- source/libs/tdb/src/db/tdbPCache.c | 21 +++------------------ source/libs/tdb/src/db/tdbPager.c | 15 ++++++++++++++- source/libs/tdb/src/inc/tdbPCache.h | 12 ++++++++++++ 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index 07c267a15c..d886cfd889 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -34,18 +34,6 @@ struct SPCache { }) #define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL) -// For page ref -#define TDB_INIT_PAGE_REF(pPage) ((pPage)->nRef = 0) -#if 0 -#define TDB_REF_PAGE(pPage) (++(pPage)->nRef) -#define TDB_UNREF_PAGE(pPage) (--(pPage)->nRef) -#define TDB_GET_PAGE_REF(pPage) ((pPage)->nRef) -#else -#define TDB_REF_PAGE(pPage) atomic_add_fetch_32(&((pPage)->nRef), 1) -#define TDB_UNREF_PAGE(pPage) atomic_sub_fetch_32(&((pPage)->nRef), 1) -#define TDB_GET_PAGE_REF(pPage) atomic_load_32(&((pPage)->nRef)) -#endif - static int tdbPCacheOpenImpl(SPCache *pCache); static void tdbPCacheInitLock(SPCache *pCache); static void tdbPCacheClearLock(SPCache *pCache); @@ -107,12 +95,7 @@ void tdbPCacheRelease(SPCache *pCache, SPage *pPage) { ASSERT(nRef >= 0); if (nRef == 0) { - if (1 /*TODO: page still clean*/) { - tdbPCacheUnpinPage(pCache, pPage); - } else { - // TODO - ASSERT(0); - } + tdbPCacheUnpinPage(pCache, pPage); } } @@ -192,6 +175,8 @@ static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) { tdbPCacheLock(pCache); + ASSERT(!pPage->isDirty); + nRef = TDB_GET_PAGE_REF(pPage); ASSERT(nRef >= 0); if (nRef == 0) { diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index a37309f032..90496e1263 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -132,6 +132,9 @@ int tdbPagerWrite(SPager *pPager, SPage *pPage) { if (pPage->isDirty) return 0; + // ref page one more time so the page will not be release + TDB_REF_PAGE(pPage); + // Set page as dirty pPage->isDirty = 1; @@ -187,6 +190,7 @@ int tdbPagerCommit(SPager *pPager) { // loop to write the dirty pages to file for (pPage = pPager->pDirty; pPage; pPage = pPage->pDirtyNext) { + // TODO: update the page footer ret = tdbPagerWritePageToDB(pPager, pPage); if (ret < 0) { ASSERT(0); @@ -194,7 +198,15 @@ int tdbPagerCommit(SPager *pPager) { } } - // TODO: loop to release the dirty pages + // release the page + for (pPage = pPager->pDirty; pPage; pPage = pPage->pDirtyNext) { + pPager->pDirty = pPage->pDirtyNext; + pPage->pDirtyNext = NULL; + + pPage->isDirty = 0; + + tdbPCacheRelease(pPager->pCache, pPage); + } // sync the db file tdbOsFSync(pPager->fd); @@ -202,6 +214,7 @@ int tdbPagerCommit(SPager *pPager) { // remote the journal file tdbOsClose(pPager->jfd); tdbOsRemove(pPager->jFileName); + pPager->dbOrigSize = pPager->dbFileSize; return 0; } diff --git a/source/libs/tdb/src/inc/tdbPCache.h b/source/libs/tdb/src/inc/tdbPCache.h index c7fa155615..f71d34ab53 100644 --- a/source/libs/tdb/src/inc/tdbPCache.h +++ b/source/libs/tdb/src/inc/tdbPCache.h @@ -33,6 +33,18 @@ extern "C" { SPager *pPager; \ SPgid pgid; +// For page ref +#define TDB_INIT_PAGE_REF(pPage) ((pPage)->nRef = 0) +#if 0 +#define TDB_REF_PAGE(pPage) (++(pPage)->nRef) +#define TDB_UNREF_PAGE(pPage) (--(pPage)->nRef) +#define TDB_GET_PAGE_REF(pPage) ((pPage)->nRef) +#else +#define TDB_REF_PAGE(pPage) atomic_add_fetch_32(&((pPage)->nRef), 1) +#define TDB_UNREF_PAGE(pPage) atomic_sub_fetch_32(&((pPage)->nRef), 1) +#define TDB_GET_PAGE_REF(pPage) atomic_load_32(&((pPage)->nRef)) +#endif + int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache); int tdbPCacheClose(SPCache *pCache); SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage); From 4e6ea0d2d269b3f8428c340316470cad90bd33d4 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 08:23:17 +0000 Subject: [PATCH 20/26] new test --- source/libs/tdb/test/tdbTest.cpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 33a82094dc..4eb1cfde63 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -121,7 +121,7 @@ TEST(tdb_test, simple_test) { int nData = 1000000; // Open Env - ret = tdbEnvOpen("tdb", 4096, 256000, &pEnv); + ret = tdbEnvOpen("tdb", 4096, 100, &pEnv); GTEST_ASSERT_EQ(ret, 0); // Create a database @@ -134,21 +134,19 @@ TEST(tdb_test, simple_test) { char val[64]; { // Insert some data - tdbBegin(pEnv); + for (int i = 1; i <= nData;) { + tdbBegin(pEnv); - int i = 1; - for (;;) { - if (i > nData) break; + for (int k = 0; k < 10; k++) { + sprintf(key, "key%d", i); + sprintf(val, "value%d", i); + ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val)); + GTEST_ASSERT_EQ(ret, 0); + i++; + } - sprintf(key, "key%d", i); - sprintf(val, "value%d", i); - ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val)); - GTEST_ASSERT_EQ(ret, 0); - - i++; + tdbCommit(pEnv); } - - tdbCommit(pEnv); } { // Query the data From 59398ee2df2ad680df215936b431d278884acdb5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 09:41:46 +0000 Subject: [PATCH 21/26] commit loop work now --- source/libs/tdb/src/db/tdbPager.c | 22 ++++++++++++++++++---- source/libs/tdb/test/tdbTest.cpp | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 90496e1263..a60b6c2afe 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -29,7 +29,7 @@ TDB_STATIC_ASSERT(sizeof(SFileHdr) == 128, "Size of file header is not correct") static int tdbPagerReadPage(SPager *pPager, SPage *pPage); static int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno); -static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *), void *arg); +static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *), void *arg, u8 loadPage); static int tdbPagerWritePageToJournal(SPager *pPager, SPage *pPage); static int tdbPagerWritePageToDB(SPager *pPager, SPage *pPage); @@ -249,7 +249,7 @@ int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage // Initialize the page if need if (!TDB_PAGE_INITIALIZED(pPage)) { - ret = tdbPagerInitPage(pPager, pPage, initPage, arg); + ret = tdbPagerInitPage(pPager, pPage, initPage, arg, 1); if (ret < 0) { return -1; } @@ -286,7 +286,7 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage ASSERT(!TDB_PAGE_INITIALIZED(pPage)); // Initialize the page if need - ret = tdbPagerInitPage(pPager, pPage, initPage, arg); + ret = tdbPagerInitPage(pPager, pPage, initPage, arg, 0); if (ret < 0) { return -1; } @@ -334,10 +334,11 @@ static int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno) { return 0; } -static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *), void *arg) { +static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *), void *arg, u8 loadPage) { int ret; int lcode; int nLoops; + i64 nRead; lcode = TDB_TRY_LOCK_PAGE(pPage); if (lcode == P_LOCK_SUCC) { @@ -346,6 +347,19 @@ static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage return 0; } + if (loadPage) { + nRead = tdbOsPRead(pPager->fd, pPage->pData, pPage->pageSize, ((i64)pPage->pageSize) * TDB_PAGE_PGNO(pPage)); + if (nRead < 0) { + // TODO + ASSERT(0); + return -1; + } else if (nRead < pPage->pageSize) { + // TODO + ASSERT(0); + return -1; + } + } + ret = (*initPage)(pPage, arg); if (ret < 0) { TDB_UNLOCK_PAGE(pPage); diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 4eb1cfde63..f98eff7e5b 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -121,7 +121,7 @@ TEST(tdb_test, simple_test) { int nData = 1000000; // Open Env - ret = tdbEnvOpen("tdb", 4096, 100, &pEnv); + ret = tdbEnvOpen("tdb", 4096, 8192, &pEnv); GTEST_ASSERT_EQ(ret, 0); // Create a database @@ -137,7 +137,7 @@ TEST(tdb_test, simple_test) { for (int i = 1; i <= nData;) { tdbBegin(pEnv); - for (int k = 0; k < 10; k++) { + for (int k = 0; k < 2000; k++) { sprintf(key, "key%d", i); sprintf(val, "value%d", i); ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val)); From 1a71bbfbd3310685559d3d69e6247eb288c9dcb7 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 09:42:23 +0000 Subject: [PATCH 22/26] refact --- source/libs/tdb/src/db/tdbPager.c | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index a60b6c2afe..3b2e17798e 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -27,7 +27,6 @@ TDB_STATIC_ASSERT(sizeof(SFileHdr) == 128, "Size of file header is not correct") #define TDB_PAGE_INITIALIZED(pPage) ((pPage)->pPager != NULL) -static int tdbPagerReadPage(SPager *pPager, SPage *pPage); static int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno); static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *), void *arg, u8 loadPage); static int tdbPagerWritePageToJournal(SPager *pPager, SPage *pPage); @@ -219,21 +218,6 @@ int tdbPagerCommit(SPager *pPager) { return 0; } -static int tdbPagerReadPage(SPager *pPager, SPage *pPage) { - i64 offset; - int ret; - - ASSERT(memcmp(pPager->fid, pPage->pgid.fileid, TDB_FILE_ID_LEN) == 0); - - offset = (pPage->pgid.pgno - 1) * (i64)(pPager->pageSize); - ret = tdbOsPRead(pPager->fd, pPage->pData, pPager->pageSize, offset); - if (ret < 0) { - // TODO: handle error - return -1; - } - return 0; -} - int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg) { SPage *pPage; SPgid pgid; From 6b0b988f46907fe1e4aabfac9981172c44d5cd50 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 1 Apr 2022 02:38:50 +0000 Subject: [PATCH 23/26] fix a bug --- source/libs/tdb/src/db/tdbPager.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 3b2e17798e..c570e47c0e 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -137,7 +137,7 @@ int tdbPagerWrite(SPager *pPager, SPage *pPage) { // Set page as dirty pPage->isDirty = 1; - // Add page to dirty list + // Add page to dirty list(TODO: NOT use O(n^2) algorithm) for (ppPage = &pPager->pDirty; (*ppPage) && TDB_PAGE_PGNO(*ppPage) < TDB_PAGE_PGNO(pPage); ppPage = &((*ppPage)->pDirtyNext)) { } @@ -198,7 +198,7 @@ int tdbPagerCommit(SPager *pPager) { } // release the page - for (pPage = pPager->pDirty; pPage; pPage = pPage->pDirtyNext) { + for (pPage = pPager->pDirty; pPage; pPage = pPager->pDirty) { pPager->pDirty = pPage->pDirtyNext; pPage->pDirtyNext = NULL; From 0e7a420e83958ff158a66a7cc8262ede2d013755 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 1 Apr 2022 03:35:43 +0000 Subject: [PATCH 24/26] fix another bug --- source/libs/tdb/src/db/tdbBtree.c | 14 +++++++------- source/libs/tdb/src/db/tdbPager.c | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 51cd5fbffc..740e665cd2 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -482,6 +482,13 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { ASSERT(0); return -1; } + + ret = tdbPagerWrite(pBt->pPager, pOlds[i]); + if (ret < 0) { + // TODO + ASSERT(0); + return -1; + } } // copy the parent key out if child pages are not leaf page childNotLeaf = !TDB_BTREE_PAGE_IS_LEAF(pOlds[0]); @@ -495,13 +502,6 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { } if (i < nOlds - 1) { - ret = tdbPagerWrite(pBt->pPager, pOlds[i]); - if (ret < 0) { - // TODO - ASSERT(0); - return -1; - } - ((SPgno *)pDivCell[i])[0] = ((SIntHdr *)pOlds[i]->pData)->pgno; ((SIntHdr *)pOlds[i]->pData)->pgno = 0; tdbPageInsertCell(pOlds[i], TDB_PAGE_TOTAL_CELLS(pOlds[i]), pDivCell[i], szDivCell[i], 1); diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index c570e47c0e..2bc40a6aad 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -214,6 +214,7 @@ int tdbPagerCommit(SPager *pPager) { tdbOsClose(pPager->jfd); tdbOsRemove(pPager->jFileName); pPager->dbOrigSize = pPager->dbFileSize; + pPager->inTran = 0; return 0; } From e25b407c5a6379cf1dd0062f0b795220231f76a5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 1 Apr 2022 08:00:46 +0000 Subject: [PATCH 25/26] optimize search process --- source/libs/tdb/src/db/tdbBtree.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 740e665cd2..f4e1621742 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -1380,6 +1380,30 @@ static int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { ASSERT(nCells > 0); ASSERT(pBtc->idx == -1); + // compare first cell + midx = lidx; + pCell = tdbPageGetCell(pPage, midx); + tdbBtreeDecodeCell(pPage, pCell, &cd); + c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); + if (c <= 0) { + ridx = lidx - 1; + } else { + lidx = lidx + 1; + } + + // compare last cell + if (lidx <= ridx) { + midx = ridx; + pCell = tdbPageGetCell(pPage, midx); + tdbBtreeDecodeCell(pPage, pCell, &cd); + c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); + if (c >= 0) { + lidx = ridx + 1; + } else { + ridx = ridx - 1; + } + } + // binary search for (;;) { if (lidx > ridx) break; From 9525e37651ac50352f8fb9869f55ce7b01a4175b Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 1 Apr 2022 08:44:59 +0000 Subject: [PATCH 26/26] 256K for 1.5G data --- source/libs/tdb/test/tdbTest.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index f98eff7e5b..9e1277a53d 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -118,10 +118,10 @@ TEST(tdb_test, simple_test) { TENV *pEnv; TDB *pDb; FKeyComparator compFunc; - int nData = 1000000; + int nData = 50000000; // Open Env - ret = tdbEnvOpen("tdb", 4096, 8192, &pEnv); + ret = tdbEnvOpen("tdb", 4096, 64, &pEnv); GTEST_ASSERT_EQ(ret, 0); // Create a database @@ -149,6 +149,8 @@ TEST(tdb_test, simple_test) { } } + tdbCommit(pEnv); + { // Query the data void *pVal = NULL; int vLen; @@ -158,6 +160,7 @@ TEST(tdb_test, simple_test) { sprintf(val, "value%d", i); ret = tdbDbGet(pDb, key, strlen(key), &pVal, &vLen); + ASSERT(ret == 0); GTEST_ASSERT_EQ(ret, 0); GTEST_ASSERT_EQ(vLen, strlen(val));