From 65e388a584e305b9cd3f37019c045f63fc445f26 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 22 Nov 2020 19:56:39 +0800 Subject: [PATCH] refact --- src/inc/tfs.h | 8 ++++++ src/tfs/src/tfcntl.c | 52 +++++++++++++++++++++++++++++++++--- src/tsdb/inc/tsdbMain.h | 6 ++--- src/tsdb/src/tsdbCommit.c | 6 ----- src/tsdb/src/tsdbFile.c | 55 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 14 deletions(-) diff --git a/src/inc/tfs.h b/src/inc/tfs.h index fa1c5eb930..efade8cf6e 100644 --- a/src/inc/tfs.h +++ b/src/inc/tfs.h @@ -39,6 +39,14 @@ TFSDIR * tfsOpenDir(char *dir); void tfsCloseDir(TFSDIR *tdir); const TFSFILE *tfsReadDir(TFSDIR *tdir); +const char *tfsAbsName(TFSFILE *pfile, char dest[]); +const char *tfsRelName(TFSFILE *pfile, char dest[]); +void tfsDirName(TFSFILE *pfile, char dest[]); +void tfsBaseName(TFSFILE *pfile, char dest[]); + +int tfsopen(TFSFILE *pfile); +int tfsclose(int, fd); + const char *tfsGetDiskName(int level, int id); #ifdef __cplusplus diff --git a/src/tfs/src/tfcntl.c b/src/tfs/src/tfcntl.c index f373a8f9f6..1f7cb4cc6c 100644 --- a/src/tfs/src/tfcntl.c +++ b/src/tfs/src/tfcntl.c @@ -21,7 +21,8 @@ struct TFSFILE { int level; int id; - char name[TSDB_FILENAME_LEN]; + char rname[TSDB_FILENAME_LEN]; // REL name + char aname[TSDB_FILENAME_LEN]; // ABS name }; struct TFSDIR { @@ -60,12 +61,13 @@ void tfsCloseDir(TFSDIR *tdir) { const TFSFILE *tfsReadDir(TFSDIR *tdir) { if (tdir->dir == NULL) return NULL; + char rname[TSDB_FILENAME_LEN] = "\0"; + while (true) { struct dirent *dp = readdir(tdir->dir); if (dp != NULL) { - tdir->tfsfile.level = tdir->level; - tdir->tfsfile.id = tdir->id; - snprintf(tdir->tfsfile.name, TSDB_FILENAME_LEN, "%s/%s", tdir->name, dp->d_name); + snprintf(rname, TSDB_FILENAME_LEN, "%s/%s", tdir->name, dp->d_name); + tsfInitFile(&(tdir->tfsfile), tdir->level, tdir->id, rname); return &(tdir->tfsfile); } @@ -88,6 +90,41 @@ const TFSFILE *tfsReadDir(TFSDIR *tdir) { } } +const char *tfsAbsName(TFSFILE *pfile, char dest[]) { return pfile->aname; } +const char *tfsRelName(TFSFILE *pfile, char dest[]) { return pfile->rname; } + +void tfsDirName(TFSFILE *pfile, char dest[]) { + char fname[TSDB_FILENAME_LEN] = "\0"; + + tfsAbsFname(pfile, fname); + strncpy(dest, dirname(fname), TSDB_FILENAME_LEN); +} + +void tfsBaseName(TFSFILE *pfile, char dest[]) { + char fname[TSDB_FILENAME_LEN] = "\0"; + memcpy((void *)fname, (void *)pfile->rname, TSDB_FILENAME_LEN); + strncpy(dest, basename(fname), TSDB_FILENAME_LEN); +} + +int tfsopen(TFSFILE *pfile, int flags) { + int fd = open(pfile->aname, flags); + if (fd < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + + return fd; +} + +int tfsclose(int fd) { + if (close(fd) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + + return 0 +} + static int tfsOpenDirImpl(TFSDIR *tdir) { char dirName[TSDB_FILENAME_LEN] = "\0"; @@ -115,4 +152,11 @@ static int tfsOpenDirImpl(TFSDIR *tdir) { ASSERT(tdir->dir == NULL); return 0; +} + +static void tsfInitFile(TFSFILE *pfile, int level, int id, char *rname) { + pfile->level = level; + pfile->id = id; + strncpy(pfile->rname, rname, TSDB_FILENAME_LEN); + snprintf(pfile->aname, TSDB_FILENAME_LEN, "%s/%s", tfsGetDiskName(level, id), rname); } \ No newline at end of file diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index fce5fb1eaf..7697e95a8d 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -189,10 +189,8 @@ typedef struct { } STsdbFileInfo; typedef struct { - // char fname[TSDB_FILENAME_LEN]; - // int fd; - - STfsFile tfile; + int fd; + TFSFILE tfile; STsdbFileInfo info; } SFile; diff --git a/src/tsdb/src/tsdbCommit.c b/src/tsdb/src/tsdbCommit.c index 04ea90e299..61c4989b58 100644 --- a/src/tsdb/src/tsdbCommit.c +++ b/src/tsdb/src/tsdbCommit.c @@ -215,12 +215,6 @@ static int tsdbCommitToFile(STsdbRepo *pRepo, int fid, SCommitIter *iters, SRWHe goto _err; } - // Open files for write/read - if (tsdbSetAndOpenHelperFile(pHelper, pGroup) < 0) { - tsdbError("vgId:%d failed to set helper file since %s", REPO_ID(pRepo), tstrerror(terrno)); - goto _err; - } - newLast = TSDB_NLAST_FILE_OPENED(pHelper); if (tsdbLoadCompIdx(pHelper, NULL) < 0) { diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index a33d9d73b2..2c80090b6a 100644 --- a/src/tsdb/src/tsdbFile.c +++ b/src/tsdb/src/tsdbFile.c @@ -68,6 +68,61 @@ int tsdbOpenFileH(STsdbRepo *pRepo) { ASSERT(pRepo != NULL && pRepo->tsdbFileH != NULL); char dataDir[TSDB_FILENAME_LEN] = "\0"; + // 1. scan and get all files corresponds + TFSDIR *tdir = NULL; + char fname[TSDB_FILENAME_LEN] = "\0"; + regex_t regex = {0}; + int code = 0; + int vid = 0; + int fid = 0; + + const TFSFILE *pfile = NULL; + + code = regcomp(®ex, "^v[0-9]+f[0-9]+\\.(head|data|last|h|d|l)$", REG_EXTENDED); + if (code != 0) { + // TODO: deal the error + } + + snprintf(dataDir, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/data", REPO_ID(pRepo)); + tdir = tfsOpenDir(dataDir); + if (tdir == NULL) { + // TODO: deal the error + } + + while ((pfile = tfsReadDir(tdir)) != NULL) { + tfsBaseName(pfile, fname); + + if (strcmp(fname, ".") == 0 || strcmp(fname, "..") == 0) continue; + + code = regexec(®ex, fname, 0, NULL, 0); + if (code == 0) { + sscanf(fname, "v%df%d", &vid, &fid); + + if (vid != REPO_ID(pRepo)) { + tfsAbsName(pfile, fname); + tsdbError("vgId:%d invalid file %s exists, ignore", REPO_ID(pRepo), fname); + continue; + } + + // TODO + {} + } else if (code == REG_NOMATCH) { + tfsAbsName(pfile, fname); + tsdbWarn("vgId:%d unrecognizable file %s exists, ignore", REPO_ID(pRepo), fname); + continue; + } else { + tsdbError("vgId:%d regexec failed since %s", REPO_ID(pRepo), strerror(code)); + // TODO: deal with error + } + } + + // 2. Sort all files according to fid + + // 3. Recover all files of each fid + while (true) { + // TODO + } + return 0; } -- GitLab