From f75f60c84c8c9571fa4caf8537363403b7ad20fc Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 30 Mar 2022 10:18:04 +0000 Subject: [PATCH] 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;