提交 65e388a5 编写于 作者: H Hongze Cheng

refact

上级 e96516ff
......@@ -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
......
......@@ -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
......@@ -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;
......
......@@ -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) {
......
......@@ -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(&regex, "^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(&regex, 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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册