diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 3e17ec2777..da7413bcbd 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -211,7 +211,7 @@ static int tdbBtCursorMoveToRoot(SBtCursor *pCur) { pBt = pCur->pBt; pPager = pBt->pPager; - pPage = tdbPagerGet(pPager, pBt->root); + pPage = tdbPagerGet(pPager, pBt->root, true); if (pPage == NULL) { // TODO: handle error } diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index f023b08605..91427d004c 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -124,13 +124,15 @@ int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate) { return 0; } -SPage *tdbPagerGet(SPager *pPager, SPgno pgno) { +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 @@ -138,6 +140,26 @@ SPage *tdbPagerGet(SPager *pPager, SPgno pgno) { } 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; } @@ -168,7 +190,7 @@ int tdbPagerAllocPage(SPager *pPager, SPage **ppPage, SPgno *ppgno) { if (1 /*TODO: no free page*/) { pgno = ++pPager->dbFileSize; - pPage = tdbPagerGet(pPager, pgno); + pPage = tdbPagerGet(pPager, pgno, false); ASSERT(pPage != NULL); } else { /* TODO: allocate from the free list */ diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index bf43632815..9b6dec8d44 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -132,6 +132,8 @@ typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, i #include "tdbPage.h" +typedef struct SPager SPager; + #include "tdbPCache.h" #include "tdbPager.h" diff --git a/source/libs/tdb/src/inc/tdbPCache.h b/source/libs/tdb/src/inc/tdbPCache.h index 2412cb1bd0..92a1ffd02b 100644 --- a/source/libs/tdb/src/inc/tdbPCache.h +++ b/source/libs/tdb/src/inc/tdbPCache.h @@ -37,7 +37,7 @@ struct SPage { SPage * pLruNext; SPage * pLruPrev; SPage * pDirtyNext; - SPage * pPager; + SPager * pPager; }; int tdbPCacheOpen(int pageSize, int cacheSize, int extraSize, SPCache **ppCache); diff --git a/source/libs/tdb/src/inc/tdbPager.h b/source/libs/tdb/src/inc/tdbPager.h index 93e58e698e..361ae8dc49 100644 --- a/source/libs/tdb/src/inc/tdbPager.h +++ b/source/libs/tdb/src/inc/tdbPager.h @@ -20,12 +20,10 @@ extern "C" { #endif -typedef struct SPager SPager; - int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppFile); int tdbPagerClose(SPager *pFile); int tdbPagerOpenDB(SPager *pFile, SPgno *ppgno, bool toCreate); -SPage *tdbPagerGet(SPager *pFile, SPgno pgno); +SPage *tdbPagerGet(SPager *pPager, SPgno pgno, bool toLoad); int tdbPagerWrite(SPager *pFile, SPage *pPage); int tdbPagerAllocPage(SPager *pFile, SPage **ppPage, SPgno *ppgno); int tdbPagerBegin(SPager *pFile);