From 32dcbfe52ad665a232d19f141928e76251a16994 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 16 Jun 2020 02:59:18 +0000 Subject: [PATCH] TD-353 --- src/tsdb/src/tsdbFile.c | 60 ++++++++++++++++++++++------------------- src/tsdb/src/tsdbMain.c | 2 +- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index 77e07b54c4..ce34cbc08c 100644 --- a/src/tsdb/src/tsdbFile.c +++ b/src/tsdb/src/tsdbFile.c @@ -30,20 +30,22 @@ #include "ttime.h" const char *tsdbFileSuffix[] = { - ".head", // TSDB_FILE_TYPE_HEAD - ".data", // TSDB_FILE_TYPE_DATA - ".last" // TSDB_FILE_TYPE_LAST + ".head", + ".data", + ".last", + ".h", + ".l" }; // ---------------- INTERNAL FUNCTIONS ---------------- -STsdbFileH* tsdbNewFileH(STsdbCfg* pCfg) { +STsdbFileH *tsdbNewFileH(STsdbCfg *pCfg) { STsdbFileH *pFileH = (STsdbFileH *)calloc(1, sizeof(*pFileH)); if (pFileH == NULL) { terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; goto _err; } - pFileH->maxFGroups = pCfg->keep / pCfg->daysPerFile + 3; + pFileH->maxFGroups = TSDB_MAX_FILE(pCfg->keep, pCfg->daysPerFile); pFileH->pFGroup = (SFileGroup *)calloc(pFileH->maxFGroups, sizeof(SFileGroup)); if (pFileH->pFGroup == NULL) { @@ -53,51 +55,55 @@ STsdbFileH* tsdbNewFileH(STsdbCfg* pCfg) { return pFileH; - _err: +_err: tsdbFreeFileH(pFileH); return NULL; } -void tsdbFreeFileH(STsdbFileH* pFileH) { +void tsdbFreeFileH(STsdbFileH *pFileH) { if (pFileH) { tfree(pFileH->pFGroup); free(pFileH); } } -STsdbFileH *tsdbInitFileH(char *dataDir, STsdbCfg *pCfg) { - STsdbFileH *pFileH = (STsdbFileH *)calloc(1, sizeof(STsdbFileH)); - if (pFileH == NULL) { // TODO: deal with ERROR here - return NULL; - } +int *tsdbOpenFileH(STsdbRepo *pRepo) { + ASSERT(pRepo != NULL && pRepo->tsdbFileH != NULL); - pFileH->maxFGroups = pCfg->keep / pCfg->daysPerFile + 3; + char *tDataDir = NULL; + DIR * dir = NULL; + int fid = 0; - pFileH->fGroup = (SFileGroup *)calloc(pFileH->maxFGroups, sizeof(SFileGroup)); - if (pFileH->fGroup == NULL) { - free(pFileH); - return NULL; + tsdbGetDataDirName(pRepo->rootDir); + if (tDataDir == NULL) { + terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; + goto _err; } - DIR *dir = opendir(dataDir); + DIR *dir = opendir(tDataDir); if (dir == NULL) { - free(pFileH); - return NULL; + tsdbError("vgId:%d failed to open directory %s since %s", REPO_ID(pRepo), tDataDir, strerror(errno)); + terrno = TAOS_SYSTEM_ERROR(errno); + goto _err; } struct dirent *dp = NULL; while ((dp = readdir(dir)) != NULL) { - if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 1) == 0) continue; - int fid = 0; + if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) continue; sscanf(dp->d_name, "f%d", &fid); - if (tsdbOpenFGroup(pFileH, dataDir, fid) < 0) { - break; - // TODO - } + // if (tsdbOpenFGroup(pFileH, dataDir, fid) < 0) { + // break; + // } } + + tfree(tDataDir); closedir(dir); + return 0; - return pFileH; +_err: + tfree(tDataDir); + if (dir != NULL) closedir(tDataDir); + return -1; } void tsdbCloseFileH(STsdbFileH *pFileH) { diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index e44f4838b0..c459f766d9 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -595,7 +595,7 @@ static STsdbRepo *tsdbNewRepo(char *rootDir, STsdbAppH *pAppH, STsdbCfg *pCfg) { goto _err; } - pRepo->tsdbFileH = tsdbNewFileH(pRepo); + pRepo->tsdbFileH = tsdbNewFileH(pCfg); if (pRepo->tsdbFileH == NULL) { tsdbError("vgId:%d failed to create file handle since %s", REPO_ID(pRepo), tstrerror(terrno)); goto _err; -- GitLab