diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index f2003c5844..38c26c6126 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -50,7 +50,6 @@ typedef struct { static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen); static int tdbEncodeLength(u8 *pBuf, uint32_t len); -static int tdbBtCursorMoveToRoot(SBtCursor *pCur); static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell); static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2); static int tdbBtreeOpenImpl(SBTree *pBt); @@ -114,7 +113,7 @@ int tdbBtreeCursor(SBtCursor *pCur, SBTree *pBt) { pCur->pBt = pBt; pCur->iPage = -1; pCur->pPage = NULL; - pCur->idx = 0; + pCur->idx = -1; return 0; } @@ -129,44 +128,81 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p return -1; } + if (pCur->idx == -1) { + ASSERT(pCur->pPage->pPageHdr->nCells == 0); + // TODO: insert the K-V pair to idx 0 + } + return 0; } static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen) { - int ret; - void *pCell; + int ret; + SBTree *pBt; + SPager *pPager; - // ret = tdbBtCursorMoveToRoot(pCur); - // if (ret < 0) { - // return -1; - // } + pBt = pCur->pBt; + pPager = pBt->pPager; - // if (pCur->pPage->pHdr->nCells == 0) { - // // Tree is empty - // } else { - // for (;;) { - // int lidx, ridx, midx, c; + if (pCur->iPage < 0) { + ASSERT(pCur->iPage == -1); + ASSERT(pCur->idx == -1); - // pBtPage = pCur->pPage; - // lidx = 0; - // ridx = pBtPage->pHdr->nCells - 1; - // while (lidx <= ridx) { - // midx = (lidx + ridx) >> 1; - // pCell = (void *)(pBtPage->aData + pBtPage->aCellIdx[midx]); + // Move from the root + ret = tdbPagerFetchPage(pPager, pBt->root, &(pCur->pPage), tdbBtreeInitPage, pBt); + if (ret < 0) { + ASSERT(0); + return -1; + } - // c = tdbCompareKeyAndCell(pKey, kLen, pCell); - // if (c == 0) { - // break; - // } else if (c < 0) { - // lidx = lidx + 1; - // } else { - // ridx = ridx - 1; - // } - // } - // } + pCur->iPage = 0; - // /* code */ - // } + if (pCur->pPage->pPageHdr->nCells == 0) { + // Current page is empty + ASSERT(TDB_FLAG_IS(pCur->pPage->pPageHdr->flags, TDB_BTREE_ROOT | TDB_BTREE_LEAF)); + return 0; + } + + // Search from root page down to leaf + { + // TODO + ASSERT(0); + // ret = tdbBtCursorMoveToRoot(pCur); + // if (ret < 0) { + // return -1; + // } + + // if (pCur->pPage->pHdr->nCells == 0) { + // // Tree is empty + // } else { + // for (;;) { + // int lidx, ridx, midx, c; + + // pBtPage = pCur->pPage; + // lidx = 0; + // ridx = pBtPage->pHdr->nCells - 1; + // while (lidx <= ridx) { + // midx = (lidx + ridx) >> 1; + // pCell = (void *)(pBtPage->aData + pBtPage->aCellIdx[midx]); + + // c = tdbCompareKeyAndCell(pKey, kLen, pCell); + // if (c == 0) { + // break; + // } else if (c < 0) { + // lidx = lidx + 1; + // } else { + // ridx = ridx - 1; + // } + // } + // } + + // /* code */ + // } + } + + } else { + // TODO: Move the cursor from a some position instead of a clear state + } return 0; } diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 0450108c84..26dd57d622 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -130,45 +130,6 @@ int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate) { return 0; } -SPage *tdbPagerGet(SPager *pPager, SPgno pgno, bool toLoad) { - SPgid pgid; - SPage *pPage; - int ret; - - memcpy(pgid.fileid, pPager->fid, TDB_FILE_ID_LEN); - pgid.pgno = pgno; - - // Get page frame from the SPCache - pPage = tdbPCacheFetch(pPager->pCache, &pgid, 1); - if (pPage == NULL) { - // TODO: handle error - return NULL; - } - tdbPCacheFetchFinish(pPager->pCache, pPage); - - // Zero the page or load page content from backend - // according to the options - if (pPage->pPager == NULL || !toLoad) { - if (!toLoad || pgno >= pPager->dbFileSize) { - memset(pPage->pData, 0, pPager->pageSize); - } else { - ret = tdbPagerReadPage(pPager, pPage); - if (ret < 0) { - // TODO: Need to drop the page - return NULL; - } - } - - if (pPage->pPager) { - ASSERT(pPage->pPager == pPager); - } else { - pPage->pPager = pPager; - } - } - - return pPage; -} - int tdbPagerWrite(SPager *pPager, SPage *pPage) { int ret; diff --git a/source/libs/tdb/src/inc/tdbBtree.h b/source/libs/tdb/src/inc/tdbBtree.h index 4f6aa38a88..1a5063c586 100644 --- a/source/libs/tdb/src/inc/tdbBtree.h +++ b/source/libs/tdb/src/inc/tdbBtree.h @@ -27,7 +27,7 @@ struct SBtCursor { SBTree *pBt; i8 iPage; SPage * pPage; - u16 idx; + int idx; u16 idxStack[BTREE_MAX_DEPTH + 1]; SPage * pgStack[BTREE_MAX_DEPTH + 1]; void * pBuf; diff --git a/source/libs/tdb/src/inc/tdbPager.h b/source/libs/tdb/src/inc/tdbPager.h index 1122ed8e4e..e4ed8552fd 100644 --- a/source/libs/tdb/src/inc/tdbPager.h +++ b/source/libs/tdb/src/inc/tdbPager.h @@ -23,7 +23,6 @@ extern "C" { int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager); int tdbPagerClose(SPager *pPager); int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate); -SPage *tdbPagerGet(SPager *pPager, SPgno pgno, bool toLoad); int tdbPagerWrite(SPager *pPager, SPage *pPage); int tdbPagerBegin(SPager *pPager); int tdbPagerCommit(SPager *pPager); diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index eb4f2a6cc0..c76dd8f1c7 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -16,7 +16,7 @@ TEST(tdb_test, simple_test) { GTEST_ASSERT_EQ(ret, 0); // // Insert some data - ret = tdbDbInsert(pDb, "1", 1, "world", 5); + ret = tdbDbInsert(pDb, "key1", 4, "value1", 6); GTEST_ASSERT_EQ(ret, 0); ret = tdbDbDrop(pDb);