提交 5f6acdbf 编写于 作者: H Hongze Cheng

refactor

上级 c33240b0
......@@ -63,6 +63,8 @@ int tfsopen(TFILE *pf, int flags);
int tfsclose(int fd);
int tfsremove(TFILE *pf);
int tfscopy(TFILE *sf, TFILE *df);
void tfsbasename(TFILE *pf, char *dest);
void tfsdirname(TFILE *pf, char *dest);
// DIR APIs ====================================
int tfsMkdir(const char *rname);
......
......@@ -265,6 +265,20 @@ int tfscopy(TFILE *sf, TFILE *df) {
return 0;
}
void tfsbasename(TFILE *pf, char *dest) {
char tname[TSDB_FILENAME_LEN] = "\0";
strncpy(tname, pf->aname, TSDB_FILENAME_LEN);
strncpy(dest, basename(tname), TSDB_FILENAME_LEN);
}
void tfsdirname(TFILE *pf, char *dest) {
char tname[TSDB_FILENAME_LEN] = "\0";
strncpy(tname, pf->aname, TSDB_FILENAME_LEN);
strncpy(dest, dirname(tname), TSDB_FILENAME_LEN);
}
// DIR APIs ====================================
int tfsMkdir(const char *rname) {
char aname[TSDB_FILENAME_LEN] = "\0";
......
......@@ -26,6 +26,8 @@ const char *tsdbFileSuffix[] = {".head", ".data", ".last", ".stat", ".h", ".d",
static int compFGroup(const void *arg1, const void *arg2);
static int keyFGroupCompFunc(const void *key, const void *fgroup);
static void tsdbScanAllFiles(STsdbRepo *pRepo, TFILE **pfArray, int *nfiles);
static int tsdbCompareFile(void *arg1, void *arg2);
// STsdbFileH ===========================================
STsdbFileH *tsdbNewFileH(STsdbCfg *pCfg) {
......@@ -68,7 +70,23 @@ void tsdbFreeFileH(STsdbFileH *pFileH) {
int tsdbOpenFileH(STsdbRepo *pRepo) {
ASSERT(pRepo != NULL && pRepo->tsdbFileH != NULL);
TFILE *pfArray = NULL;
int nfiles = 0;
// Scan the whole directory and get data
tsdbScanAllFiles(pRepo, &pfArray, &nfiles);
if (nfiles == 0) return 0;
// Sort the files
qsort((void *)pfArray, nfiles, sizeof(TFILE), tsdbCompareFile);
// Loop to recover the files
int iter = 0;
while (true) {
if (iter >= nfiles) break;
// TODO
}
return 0;
}
......@@ -462,3 +480,64 @@ int tsdbApplyRetention(STsdbRepo *pRepo, SFidGroup *pFidGroup) {
return 0;
}
static void tsdbScanAllFiles(STsdbRepo *pRepo, TFILE **pfArray, int *nfiles) {
TDIR * tdir = NULL;
char dirName[TSDB_FILENAME_LEN] = "\0";
char bname[TSDB_FILENAME_LEN] = "\0";
int arraySize = 0;
regex_t regex1 = {0};
regex_t regex2 = {0};
const TFILE *pf = NULL;
regcomp(&regex1, "^v[0-9]+f[0-9]+\\.(head|data|last|stat)$", REG_EXTENDED);
regcomp(&regex2, "^v[0-9]+f[0-9]+\\.(h|d|l|s)$", REG_EXTENDED);
snprintf(dirName, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/data", REPO_ID(pRepo));
tdir = tfsOpendir(dirName);
while ((pf = tfsReaddir(tdir)) != NULL) {
fsbasename(pf, bname);
int code = regexec(&regex1, bname, 0, NULL, 0);
if (code != 0) {
tsdbWarn("vgId:%d file %s exists, ignore it", REPO_ID(pRepo), pf->aname);
rename(pf->aname);
continue;
}
if (nfiles + 1 >= arraySize) {
if (arraySize = 0) {
arraySize = 1024;
} else {
arraySize = arraySize * 2;
}
*pfArray = realloc(*pfArray, sizeof(TFILE) * arraySize);
}
(*pfArray)[nfiles++] = *pf;
}
tfsClosedir(tdir);
}
static int tsdbCompareFile(void *arg1, void *arg2) {
char bname1[TSDB_FILENAME_LEN] = "\0";
char bname2[TSDB_FILENAME_LEN] = "\0";
TFILE *pf1 = (TFILE *)arg1;
TFILE *pf2 = (TFILE *)arg2;
tfsbasename(pf1, bname1);
tfsbasename(pf2, bname2);
// TODO
}
static int tsdbGetTFileFid(TFILE *pf) {
char bname[TSDB_FILENAME_LEN] = "\0";
int fid = 0;
tfsbasename(pf, bname);
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册