From fee7499cfb920877e9988f2ea1d62bebab5bf79e Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 29 Apr 2022 10:02:36 +0000 Subject: [PATCH] fix child table query problem --- source/dnode/vnode/src/meta/metaQuery.c | 15 +++ source/dnode/vnode/src/meta/metaTDBImpl.c | 16 +-- source/dnode/vnode/src/meta/metaTable.c | 12 +- source/dnode/vnode/src/tsdb/tsdbTDBImpl.c | 2 +- source/libs/tdb/inc/tdb.h | 3 +- source/libs/tdb/src/db/tdbBtree.c | 140 ++++++++++++++-------- source/libs/tdb/src/db/tdbDb.c | 21 ++-- source/libs/tdb/src/inc/tdbInt.h | 31 +++-- source/libs/tdb/test/tdbTest.cpp | 4 +- 9 files changed, 157 insertions(+), 87 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaQuery.c b/source/dnode/vnode/src/meta/metaQuery.c index 17bda6a059..e10f75f69e 100644 --- a/source/dnode/vnode/src/meta/metaQuery.c +++ b/source/dnode/vnode/src/meta/metaQuery.c @@ -98,6 +98,8 @@ SMTbCursor *metaOpenTbCursor(SMeta *pMeta) { tdbDbcOpen(pMeta->pUidIdx, &pTbCur->pDbc, NULL); + tdbDbcMoveToFirst(pTbCur->pDbc); + return pTbCur; } @@ -185,7 +187,9 @@ struct SMCtbCursor { SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) { SMCtbCursor *pCtbCur = NULL; + SCtbIdxKey ctbIdxKey; int ret; + int c; pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur)); if (pCtbCur == NULL) { @@ -199,6 +203,14 @@ SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) { return NULL; } + // move to the suid + ctbIdxKey.suid = uid; + ctbIdxKey.uid = INT64_MIN; + tdbDbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c); + if (c > 0) { + tdbDbcMoveToNext(pCtbCur->pCur); + } + return pCtbCur; } @@ -225,6 +237,9 @@ tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) { } pCtbIdxKey = pCtbCur->pKey; + if (pCtbIdxKey->suid > pCtbCur->suid) { + return 0; + } return pCtbIdxKey->uid; } diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index cb556e8630..bba707076c 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -289,7 +289,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg, STbDdlH *pHandle) { pVal = pBuf = buf; metaEncodeTbInfo(&pBuf, pTbCfg); vLen = POINTER_DISTANCE(pBuf, buf); - ret = tdbDbInsert(pMetaDb->pTbDB, pKey, kLen, pVal, vLen, &pMetaDb->txn); + ret = tdbDbPut(pMetaDb->pTbDB, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -311,7 +311,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg, STbDdlH *pHandle) { pVal = pBuf = buf; metaEncodeSchemaEx(&pBuf, &schemaWrapper); vLen = POINTER_DISTANCE(pBuf, buf); - ret = tdbDbInsert(pMetaDb->pSchemaDB, pKey, kLen, pVal, vLen, &pMeta->pDB->txn); + ret = tdbDbPut(pMetaDb->pSchemaDB, pKey, kLen, pVal, vLen, &pMeta->pDB->txn); if (ret < 0) { return -1; } @@ -325,7 +325,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg, STbDdlH *pHandle) { kLen = nameLen + 1 + sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pNameIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); + ret = tdbDbPut(pMetaDb->pNameIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -336,7 +336,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg, STbDdlH *pHandle) { kLen = sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pStbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); + ret = tdbDbPut(pMetaDb->pStbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -347,7 +347,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg, STbDdlH *pHandle) { kLen = sizeof(ctbIdxKey); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pCtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); + ret = tdbDbPut(pMetaDb->pCtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -362,7 +362,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg, STbDdlH *pHandle) { kLen = sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pNtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); + ret = tdbDbPut(pMetaDb->pNtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -530,7 +530,7 @@ int metaSaveSmaToDB(SMeta *pMeta, STSma *pSmaCfg) { int32_t kLen = sizeof(pSmaCfg->indexUid); int32_t vLen = POINTER_DISTANCE(qBuf, pBuf); - ret = tdbDbInsert(pMeta->pDB->pSmaDB, key, kLen, val, vLen, &pMetaDb->txn); + ret = tdbDbPut(pMeta->pDB->pSmaDB, key, kLen, val, vLen, &pMetaDb->txn); if (ret < 0) { taosMemoryFreeClear(pBuf); return -1; @@ -545,7 +545,7 @@ int metaSaveSmaToDB(SMeta *pMeta, STSma *pSmaCfg) { val = NULL; vLen = 0; - ret = tdbDbInsert(pMeta->pDB->pSmaIdx, key, kLen, val, vLen, &pMetaDb->txn); + ret = tdbDbPut(pMeta->pDB->pSmaIdx, key, kLen, val, vLen, &pMetaDb->txn); if (ret < 0) { taosMemoryFreeClear(pBuf); return -1; diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index 41ef231dcf..cb0b3dc81c 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -218,7 +218,7 @@ static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) { tCoderClear(&coder); // write to table.db - if (tdbDbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, &pMeta->txn) < 0) { + if (tdbDbPut(pMeta->pTbDb, pKey, kLen, pVal, vLen, &pMeta->txn) < 0) { goto _err; } @@ -231,11 +231,11 @@ _err: } static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) { - return tdbDbInsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &pME->version, sizeof(int64_t), &pMeta->txn); + return tdbDbPut(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &pME->version, sizeof(int64_t), &pMeta->txn); } static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME) { - return tdbDbInsert(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), &pMeta->txn); + return tdbDbPut(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), &pMeta->txn); } static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME) { @@ -258,12 +258,12 @@ static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME) { ttlKey.dtime = ctime + ttlDays * 24 * 60 * 60; ttlKey.uid = pME->uid; - return tdbDbInsert(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), NULL, 0, &pMeta->txn); + return tdbDbPut(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), NULL, 0, &pMeta->txn); } static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME) { SCtbIdxKey ctbIdxKey = {.suid = pME->ctbEntry.suid, .uid = pME->uid}; - return tdbDbInsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), NULL, 0, &pMeta->txn); + return tdbDbPut(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), NULL, 0, &pMeta->txn); } static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pME) { @@ -304,7 +304,7 @@ static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) { tCoderInit(&coder, TD_LITTLE_ENDIAN, pVal, vLen, TD_ENCODER); tEncodeSSchemaWrapper(&coder, pSW); - if (tdbDbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, &pMeta->txn) < 0) { + if (tdbDbPut(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, &pMeta->txn) < 0) { rcode = -1; goto _exit; } diff --git a/source/dnode/vnode/src/tsdb/tsdbTDBImpl.c b/source/dnode/vnode/src/tsdb/tsdbTDBImpl.c index 8a553e94fb..74878e817f 100644 --- a/source/dnode/vnode/src/tsdb/tsdbTDBImpl.c +++ b/source/dnode/vnode/src/tsdb/tsdbTDBImpl.c @@ -97,7 +97,7 @@ int32_t tsdbCloseDBF(SDBFile *pDBF) { int32_t tsdbSaveSmaToDB(SDBFile *pDBF, void *pKey, int32_t keyLen, void *pVal, int32_t valLen, TXN *txn) { int32_t ret; - ret = tdbDbInsert(pDBF->pDB, pKey, keyLen, pVal, valLen, txn); + ret = tdbDbPut(pDBF->pDB, pKey, keyLen, pVal, valLen, txn); if (ret < 0) { tsdbError("Failed to create insert sma data into db, ret = %d", ret); return -1; diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 174a86657b..49da6fd8ed 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -40,7 +40,7 @@ int tdbCommit(TENV *pEnv, TXN *pTxn); int tdbDbOpen(const char *fname, int keyLen, int valLen, tdb_cmpr_fn_t keyCmprFn, TENV *pEnv, TDB **ppDb); int tdbDbClose(TDB *pDb); int tdbDbDrop(TDB *pDb); -int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn); +int tdbDbPut(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn); int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen); int tdbDbPGet(TDB *pDb, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen); @@ -52,6 +52,7 @@ int tdbDbcMoveToFirst(TDBC *pDbc); int tdbDbcMoveToLast(TDBC *pDbc); int tdbDbcMoveToNext(TDBC *pDbc); int tdbDbcMoveToPrev(TDBC *pDbc); +int tdbDbcGet(TDBC *pDbc, const void **ppKey, int *pkLen, const void **ppVal, int *pvLen); int tdbDbcPut(TDBC *pDbc, const void *pKey, int keyLen, const void *pVal, int valLen); int tdbDbcUpdate(TDBC *pDbc, const void *pKey, int kLen, const void *pVal, int vLen); diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index a87e14893c..2665dd1cf6 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -58,15 +58,6 @@ typedef struct { SBTree *pBt; } SBtreeInitPageArg; -typedef struct { - int kLen; - const u8 *pKey; - int vLen; - const u8 *pVal; - SPgno pgno; - u8 *pBuf; -} SCellDecoder; - static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2); static int tdbBtreeOpenImpl(SBTree *pBt); static int tdbBtreeInitPage(SPage *pPage, void *arg, int init); @@ -75,7 +66,6 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pDecoder); static int tdbBtreeBalance(SBTC *pBtc); static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell); -static int tdbBtcMoveToNext(SBTC *pBtc); static int tdbBtcMoveDownward(SBTC *pBtc); static int tdbBtcMoveUpward(SBTC *pBtc); @@ -1017,6 +1007,7 @@ int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn) { pBtc->iPage = -1; pBtc->pPage = NULL; pBtc->idx = -1; + memset(&pBtc->coder, 0, sizeof(SCellDecoder)); if (pTxn == NULL) { pBtc->pTxn = &pBtc->txn; @@ -1059,6 +1050,8 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { return 0; } } else { + ASSERT(0); +#if 0 // move from a position int iPage = 0; @@ -1076,6 +1069,7 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { tdbBtcMoveUpward(pBtc); } +#endif } // move downward @@ -1124,6 +1118,8 @@ int tdbBtcMoveToLast(SBTC *pBtc) { return 0; } } else { + ASSERT(0); +#if 0 int iPage = 0; // downward search @@ -1146,6 +1142,7 @@ int tdbBtcMoveToLast(SBTC *pBtc) { tdbBtcMoveUpward(pBtc); } +#endif } // move downward @@ -1215,7 +1212,7 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { return 0; } -static int tdbBtcMoveToNext(SBTC *pBtc) { +int tdbBtcMoveToNext(SBTC *pBtc) { int nCells; int ret; SCell *pCell; @@ -1261,6 +1258,43 @@ static int tdbBtcMoveToNext(SBTC *pBtc) { return 0; } +int tdbBtcMoveToPrev(SBTC *pBtc) { + if (pBtc->idx < 0) return -1; + + pBtc->idx--; + if (pBtc->idx >= 0) { + return 0; + } + + // move upward + for (;;) { + if (pBtc->iPage == 0) { + pBtc->idx = -1; + return 0; + } + + tdbBtcMoveUpward(pBtc); + pBtc->idx--; + if (pBtc->idx >= 0) { + break; + } + } + + // move downward + for (;;) { + if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break; + + tdbBtcMoveDownward(pBtc); + 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); + } + } + + return 0; +} + static int tdbBtcMoveDownward(SBTC *pBtc) { int ret; SPgno pgno; @@ -1306,14 +1340,38 @@ static int tdbBtcMoveUpward(SBTC *pBtc) { return 0; } +int tdbBtcGet(SBTC *pBtc, const void **ppKey, int *kLen, const void **ppVal, int *vLen) { + SCell *pCell; + + if (pBtc->idx < 0 || pBtc->idx >= TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) { + return -1; + } + + pCell = tdbPageGetCell(pBtc->pPage, pBtc->idx); + tdbBtreeDecodeCell(pBtc->pPage, pCell, &pBtc->coder); + + if (ppKey) { + *ppKey = (void *)pBtc->coder.pKey; + *kLen = pBtc->coder.kLen; + } + + if (ppVal) { + *ppVal = (void *)pBtc->coder.pVal; + *kLen = pBtc->coder.vLen; + } + + return 0; +} + int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { - int ret; - int nCells; - int c; - SCell *pCell; - SCellDecoder cd = {0}; - SBTree *pBt = pBtc->pBt; - SPager *pPager = pBt->pPager; + int ret; + int nCells; + int c; + SCell *pCell; + SBTree *pBt = pBtc->pBt; + SPager *pPager = pBt->pPager; + const void *pTKey; + int tkLen; if (pBtc->iPage < 0) { // move from a clear cursor @@ -1330,6 +1388,8 @@ 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 { + ASSERT(0); +#if 0 SPage *pPage; int idx; int iPage = 0; @@ -1364,11 +1424,12 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { if (pBtc->iPage == iPage) break; tdbBtcMoveUpward(pBtc); } +#endif } // search downward to the leaf for (;;) { - int lidx, ridx, midx; + int lidx, ridx; SPage *pPage; pPage = pBtc->pPage; @@ -1377,13 +1438,11 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { ridx = nCells - 1; 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); + pBtc->idx = lidx; + tdbBtcGet(pBtc, &pTKey, &tkLen, NULL, NULL); + c = pBt->kcmpr(pKey, kLen, pTKey, tkLen); if (c <= 0) { ridx = lidx - 1; } else { @@ -1392,10 +1451,9 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { // 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); + pBtc->idx = ridx; + tdbBtcGet(pBtc, &pTKey, &tkLen, NULL, NULL); + c = pBt->kcmpr(pKey, kLen, pTKey, tkLen); if (c >= 0) { lidx = ridx + 1; } else { @@ -1407,24 +1465,15 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { 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); + pBtc->idx = (lidx + ridx) >> 1; + tdbBtcGet(pBtc, &pTKey, &tkLen, NULL, NULL); + c = pBt->kcmpr(pKey, kLen, pTKey, tkLen); if (c < 0) { // pKey < cd.pKey - ridx = midx - 1; + ridx = pBtc->idx - 1; } else if (c > 0) { // pKey > cd.pKey - lidx = midx + 1; + lidx = pBtc->idx + 1; } else { // pKey == cd.pKey break; @@ -1433,14 +1482,11 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { // 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; + if (c > 0) { + pBtc->idx += 1; } tdbBtcMoveDownward(pBtc); } diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index 22d1f18885..aadb9238b0 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -75,7 +75,7 @@ int tdbDbDrop(TDB *pDb) { return 0; } -int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn) { +int tdbDbPut(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn) { return tdbBtreeInsert(pDb->pBt, pKey, keyLen, pVal, valLen, pTxn); } @@ -99,14 +99,6 @@ int tdbDbcOpen(TDB *pDb, TDBC **ppDbc, TXN *pTxn) { tdbBtcOpen(&pDbc->btc, pDb->pBt, pTxn); - // TODO: move to first now, we can move to any key-value - // and in any direction, design new APIs. - ret = tdbBtcMoveToFirst(&pDbc->btc); - if (ret < 0) { - ASSERT(0); - return -1; - } - *ppDbc = pDbc; return 0; } @@ -117,10 +109,12 @@ int tdbDbcMoveToFirst(TDBC *pDbc) { return tdbBtcMoveToFirst(&pDbc->btc); } int tdbDbcMoveToLast(TDBC *pDbc) { return tdbBtcMoveToLast(&pDbc->btc); } -int tdbDbcMoveToNext(TDBC *pDbc) { return 0; } -int tdbDbcMoveToPrev(TDBC *pDbc) { - // TODO - return 0; +int tdbDbcMoveToNext(TDBC *pDbc) { return tdbBtcMoveToNext(&pDbc->btc); } + +int tdbDbcMoveToPrev(TDBC *pDbc) { return tdbBtcMoveToPrev(&pDbc->btc); } + +int tdbDbcGet(TDBC *pDbc, const void **ppKey, int *pkLen, const void **ppVal, int *pvLen) { + return tdbBtcGet(&pDbc->btc, ppKey, pkLen, ppVal, pvLen); } int tdbDbcPut(TDBC *pDbc, const void *pKey, int keyLen, const void *pVal, int valLen) { @@ -147,6 +141,7 @@ int tdbDbcNext(TDBC *pDbc, void **ppKey, int *kLen, void **ppVal, int *vLen) { int tdbDbcClose(TDBC *pDbc) { if (pDbc) { + tdbBtcClose(&pDbc->btc); tdbOsFree(pDbc); } diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index c890716366..4c19dee03e 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -103,15 +103,25 @@ typedef struct SBtInfo { int nData; } SBtInfo; +typedef struct { + int kLen; + const u8 *pKey; + int vLen; + const u8 *pVal; + SPgno pgno; + u8 *pBuf; +} SCellDecoder; + struct SBTC { - SBTree *pBt; - i8 iPage; - SPage *pPage; - int idx; - int idxStack[BTREE_MAX_DEPTH + 1]; - SPage *pgStack[BTREE_MAX_DEPTH + 1]; - TXN *pTxn; - TXN txn; + SBTree *pBt; + i8 iPage; + SPage *pPage; + int idx; + int idxStack[BTREE_MAX_DEPTH + 1]; + SPage *pgStack[BTREE_MAX_DEPTH + 1]; + SCellDecoder coder; + TXN *pTxn; + TXN txn; }; // SBTree @@ -123,11 +133,14 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL // SBTC int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn); +int tdbBtcClose(SBTC *pBtc); int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst); int tdbBtcMoveToFirst(SBTC *pBtc); int tdbBtcMoveToLast(SBTC *pBtc); +int tdbBtcMoveToNext(SBTC *pBtc); +int tdbBtcMoveToPrev(SBTC *pBtc); int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen); -int tdbBtcClose(SBTC *pBtc); +int tdbBtcGet(SBTC *pBtc, const void **ppKey, int *kLen, const void **ppVal, int *vLen); // tdbPager.c ==================================== diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 9b4bc46ea9..1f6f28261f 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -152,7 +152,7 @@ TEST(tdb_test, simple_test) { for (int iData = 1; iData <= nData; iData++) { sprintf(key, "key%d", iData); sprintf(val, "value%d", iData); - ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val), &txn); + ret = tdbDbPut(pDb, key, strlen(key), val, strlen(val), &txn); GTEST_ASSERT_EQ(ret, 0); // if pool is full, commit the transaction and start a new one @@ -269,7 +269,7 @@ TEST(tdb_test, simple_test2) { for (int iData = 1; iData <= nData; iData++) { sprintf(key, "key%d", iData); sprintf(val, "value%d", iData); - ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val), &txn); + ret = tdbDbPut(pDb, key, strlen(key), val, strlen(val), &txn); GTEST_ASSERT_EQ(ret, 0); }