diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 0f5e6733ed0be35742363bfd38a89de972722aa3..f5715866edbf8bfe111585c31a215bdec7cd404e 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -203,6 +203,34 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p return 0; } +int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen) { + SBtCursor btc; + SCell *pCell; + int cret; + SCellDecoder cd; + + tdbBtreeCursor(&btc, pBt); + + tdbBtCursorMoveTo(&btc, pKey, kLen, &cret); + + if (cret) { + return cret; + } + + pCell = tdbPageGetCell(btc.pPage, btc.idx); + tdbBtreeDecodeCell(btc.pPage, pCell, &cd); + + *vLen = cd.vLen; + // TODO: here may have memory leak + *ppVal = realloc(*ppVal, *vLen); + if (*ppVal == NULL) { + return -1; + } + + memcpy(*ppVal, cd.pVal, cd.vLen); + return 0; +} + static int tdbBtCursorMoveToChild(SBtCursor *pCur, SPgno pgno) { int ret; diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index 27cbaeaa01e6a370abbac4b13f504416b412279f..2ba9daaa4a966f32333c53f4a301802e30d4cd4c 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -88,7 +88,6 @@ int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int v return 0; } -int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void *pVal, int *vLen) { - // TODO - return 0; +int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) { + return tdbBtreeGet(pDb->pBt, pKey, kLen, ppVal, vLen); } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbBtree.h b/source/libs/tdb/src/inc/tdbBtree.h index c1fe77c22ea18ad0720f74659f7eac46259c74f3..a0b054a6a8b14c2b26280f31d18ca04ba8600b96 100644 --- a/source/libs/tdb/src/inc/tdbBtree.h +++ b/source/libs/tdb/src/inc/tdbBtree.h @@ -37,6 +37,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SB int tdbBtreeClose(SBTree *pBt); int tdbBtreeCursor(SBtCursor *pCur, SBTree *pBt); int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen); +int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbDb.h b/source/libs/tdb/src/inc/tdbDb.h index fdf4c4aca59aaa4ed0a9945d15d5c1ee6dc01c7b..6a9d653eac9f128dd9f141ccb1a44bd97daae9bf 100644 --- a/source/libs/tdb/src/inc/tdbDb.h +++ b/source/libs/tdb/src/inc/tdbDb.h @@ -26,7 +26,7 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF int tdbDbClose(STDb *pDb); int tdbDbDrop(STDb *pDb); int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen); -int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void *pVal, int *vLen); +int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void **ppVal, int *vLen); #ifdef __cplusplus } diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index c5a2787aaf26f7e5765ccb80c86e9f1553c2a178..3d06d568e41847145382625ec92bce728218493f 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -37,7 +37,7 @@ TEST(tdb_test, simple_test) { sprintf(key, "key%d", i); sprintf(val, "value%d", i); - ret = tdbDbGet(pDb, key, strlen(key), pVal, &vLen); + ret = tdbDbGet(pDb, key, strlen(key), &pVal, &vLen); GTEST_ASSERT_EQ(ret, 0); GTEST_ASSERT_EQ(vLen, strlen(val));