From cebef7b2b36567b9b980b227277dd6e83fee838e Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 3 Mar 2022 09:03:24 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 42 +++++++++++++++++++++++++++--- source/libs/tdb/src/db/tdbDb.c | 2 +- source/libs/tdb/src/db/tdbPager.c | 18 ------------- source/libs/tdb/src/inc/tdbBtree.h | 2 +- source/libs/tdb/src/inc/tdbPager.h | 2 ++ 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index a59a1b90f5..b31253149e 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -40,9 +40,11 @@ static int tdbBtCursorMoveToRoot(SBtCursor *pCur); static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage); 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); -int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, SBTree **ppBt) { +int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, SBTree **ppBt) { SBTree *pBt; + int ret; *ppBt = NULL; @@ -51,8 +53,6 @@ int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPager *pPager, FKeyCompa return -1; } - // pBt->root - pBt->root = rtPgno; // pBt->keyLen pBt->keyLen = keyLen; // pBt->valLen @@ -79,6 +79,13 @@ int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPager *pPager, FKeyCompa // pBt->minLeaf pBt->minLeaf = pBt->minLocal; + // TODO: pBt->root + ret = tdbBtreeOpenImpl(pBt); + if (ret < 0) { + free(pBt); + return -1; + } + *ppBt = pBt; return 0; } @@ -268,4 +275,33 @@ static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2 } } return cret; +} + +static int tdbBtreeOpenImpl(SBTree *pBt) { + // Try to get the root page of the an existing btree + + SPgno pgno; + SPage *pPage; + int ret; + + { + // TODO: Search the main DB to check if the DB exists + pgno = 0; + } + + if (pgno != 0) { + pBt->root = pgno; + return 0; + } + + // Try to create a new database + ret = tdbPagerNewPage(pBt->pPager, &pgno, &pPage); + if (ret < 0) { + return -1; + } + + ASSERT(pgno != 0); + pBt->root = pgno; + + return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index 51b654cc86..e87c57e547 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -50,7 +50,7 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF ASSERT(pPager != NULL); // pDb->pBt - ret = tdbBtreeOpen(pgno, keyLen, valLen, pPager, keyCmprFn, &(pDb->pBt)); + ret = tdbBtreeOpen(keyLen, valLen, pPager, keyCmprFn, &(pDb->pBt)); if (ret < 0) { return -1; } diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index ab750665ff..0a5a8abe82 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -187,24 +187,6 @@ int tdbPagerWrite(SPager *pPager, SPage *pPage) { return 0; } -// int tdbPagerAllocPage(SPager *pPager, SPage **ppPage, SPgno *ppgno) { -// SPage *pPage; -// SPgno pgno; - -// if (1 /*TODO: no free page*/) { -// pgno = ++pPager->dbFileSize; -// pPage = tdbPagerGet(pPager, pgno, false); -// ASSERT(pPage != NULL); -// } else { -// /* TODO: allocate from the free list */ -// ASSERT(0); -// } - -// *ppPage = pPage; -// *ppgno = pgno; -// return 0; -// } - int tdbPagerBegin(SPager *pPager) { if (pPager->inTran) { return 0; diff --git a/source/libs/tdb/src/inc/tdbBtree.h b/source/libs/tdb/src/inc/tdbBtree.h index 8b0fadd507..64b3dbeaa9 100644 --- a/source/libs/tdb/src/inc/tdbBtree.h +++ b/source/libs/tdb/src/inc/tdbBtree.h @@ -34,7 +34,7 @@ struct SBtCursor { void * pBuf; }; -int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt); +int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt); int tdbBtreeClose(SBTree *pBt); int tdbBtreeCursor(SBtCursor *pCur, SBTree *pBt); int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen); diff --git a/source/libs/tdb/src/inc/tdbPager.h b/source/libs/tdb/src/inc/tdbPager.h index 8eb1bfe105..a32439f30f 100644 --- a/source/libs/tdb/src/inc/tdbPager.h +++ b/source/libs/tdb/src/inc/tdbPager.h @@ -28,6 +28,8 @@ int tdbPagerWrite(SPager *pPager, SPage *pPage); int tdbPagerBegin(SPager *pPager); int tdbPagerCommit(SPager *pPager); int tdbPagerGetPageSize(SPager *pPager); +int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage); +int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage); #ifdef __cplusplus }