diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index 77e07b54c477dae27f570f1ee657ab180240d2e7..ce34cbc08c1c23c5dbe312cc2789223fe9b46c07 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 e44f4838b0044d2cb727f7a2449c060371815e8e..c459f766d9307c9d2afc651337329138982e3bc3 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;