diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 285434b402..f760598714 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -297,7 +297,8 @@ static int tdbBtreeOpenImpl(SBTree *pBt) { { // 1. TODO: Search the main DB to check if the DB exists - pgno = 0; + ret = tdbPagerOpenDB(pBt->pPager, &pgno, true); + ASSERT(ret == 0); } if (pgno != 0) { diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index f78568b51c..7672ccf34e 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -76,6 +76,8 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) { // pPager->jfd = -1; pPager->pageSize = tdbPCacheGetPageSize(pCache); + // pPager->dbOrigSize + ret = tdbGetFileSize(pPager->fd, pPager->pageSize, &(pPager->dbOrigSize)); *ppPager = pPager; return 0; @@ -91,26 +93,32 @@ int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate) { SPage *pPage; int ret; - { - // TODO: try to search the main DB to get the page number + if (pPager->dbOrigSize > 0) { + pgno = 1; + } else { pgno = 0; } - // if (pgno == 0 && toCreate) { - // ret = tdbPagerAllocPage(pPager, &pPage, &pgno); - // if (ret < 0) { - // return -1; - // } + { + // TODO: try to search the main DB to get the page number + // pgno = 0; + } - // // TODO: Need to zero the page + // if (pgno == 0 && toCreate) { + // ret = tdbPagerAllocPage(pPager, &pPage, &pgno); + // if (ret < 0) { + // return -1; + // } - // ret = tdbPagerWrite(pPager, pPage); - // if (ret < 0) { - // return -1; - // } - // } + // // TODO: Need to zero the page - *ppgno = pgno; + // ret = tdbPagerWrite(pPager, pPage); + // if (ret < 0) { + // return -1; + // } + // } + + *ppgno = pgno; return 0; } diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index fc299b3fc1..09e126772b 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -30,5 +30,18 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) { ((uint64_t *)fileid)[2] = taosRand(); } + return 0; +} + +int tdbGetFileSize(tdb_fd_t fd, int szPage, SPgno *size) { + int ret; + int64_t szBytes; + + ret = tdbOsFileSize(fd, &szBytes); + if (ret < 0) { + return -1; + } + + *size = szBytes / szPage; return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbOs.h b/source/libs/tdb/src/inc/tdbOs.h index ae389708f4..1d87285091 100644 --- a/source/libs/tdb/src/inc/tdbOs.h +++ b/source/libs/tdb/src/inc/tdbOs.h @@ -46,13 +46,14 @@ typedef TdFilePtr tdb_fd_t; #define tdbOsOpen(PATH, OPTION, MODE) taosOpenFile((PATH), (OPTION)) -#define tdbOsClose(FD) taosCloseFile(&(FD)) -#define tdbOsRead taosReadFile -#define tdbOsPRead taosPReadFile -#define tdbOsWrite taosWriteFile -#define tdbOsFSync taosFsyncFile -#define tdbOsLSeek taosLSeekFile -#define tdbOsRemove remove +#define tdbOsClose(FD) taosCloseFile(&(FD)) +#define tdbOsRead taosReadFile +#define tdbOsPRead taosPReadFile +#define tdbOsWrite taosWriteFile +#define tdbOsFSync taosFsyncFile +#define tdbOsLSeek taosLSeekFile +#define tdbOsRemove remove +#define tdbOsFileSize(FD, PSIZE) taosFStatFile(FD, PSIZE, NULL) /* directory */ #define tdbOsMkdir taosMkDir @@ -110,10 +111,11 @@ i64 tdbOsWrite(tdb_fd_t fd, const void *pData, i64 nBytes); #define tdbOsFSync fsync #define tdbOsLSeek lseek #define tdbOsRemove remove +#define tdbOsFileSize(FD, PSIZE) /* directory */ -#define tdbOsMkdir mkdir -#define tdbOsRmdir rmdir +#define tdbOsMkdir mkdir +#define tdbOsRmdir rmdir // For threads and lock ----------------- /* spin lock */ diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 6abddb5b22..042026d0a4 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -29,6 +29,7 @@ extern "C" { #define TDB_ROUND8(x) (((x) + 7) & ~7) int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); +int tdbGetFileSize(tdb_fd_t fd, int szPage, SPgno *size); #define TDB_REALLOC(PTR, SIZE) \ ({ \