From 4383b3b53a45d597b8c2cbcd8a11df46cf923d55 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 24 Mar 2022 02:09:56 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 28 ++++++++++++++++++++++++++++ source/libs/tdb/src/db/tdbDb.c | 5 ++--- source/libs/tdb/src/inc/tdbBtree.h | 1 + source/libs/tdb/src/inc/tdbDb.h | 2 +- source/libs/tdb/test/tdbTest.cpp | 2 +- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 0f5e6733ed..f5715866ed 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 27cbaeaa01..2ba9daaa4a 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 c1fe77c22e..a0b054a6a8 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 fdf4c4aca5..6a9d653eac 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 c5a2787aaf..3d06d568e4 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)); -- GitLab