more code

This commit is contained in:
Hongze Cheng 2023-05-16 11:09:15 +08:00
parent f40986408c
commit 7cc7d35be4
5 changed files with 65 additions and 41 deletions

View File

@ -34,7 +34,7 @@ typedef enum {
} tsdb_fop_t; } tsdb_fop_t;
int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json); int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json);
int32_t tsdbFileSetFromJson(const cJSON *json, STFileSet *fset); int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset);
int32_t tsdbFileSetInit(STFileSet *pSet); int32_t tsdbFileSetInit(STFileSet *pSet);
int32_t tsdbFileSetClear(STFileSet *pSet); int32_t tsdbFileSetClear(STFileSet *pSet);

View File

@ -37,7 +37,7 @@ typedef enum {
#define TSDB_FTYPE_MAX (TSDB_FTYPE_TOMB + 1) #define TSDB_FTYPE_MAX (TSDB_FTYPE_TOMB + 1)
int32_t tsdbTFileToJson(const STFile *f, cJSON *json); int32_t tsdbTFileToJson(const STFile *f, cJSON *json);
int32_t tsdbTFileFromJson(const cJSON *json, tsdb_ftype_t ftype, STFile **f); int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f);
// create/destroy // create/destroy
int32_t tsdbTFileCreate(const STFile *pFile, STFile **f); int32_t tsdbTFileCreate(const STFile *pFile, STFile **f);

View File

@ -111,7 +111,7 @@ static int32_t save_json(const cJSON *json, const char *fname) {
goto _exit; goto _exit;
} }
if (taosWriteFile(fp, data, strlen(data) + 1) < 0) { if (taosWriteFile(fp, data, strlen(data)) < 0) {
code = TAOS_SYSTEM_ERROR(code); code = TAOS_SYSTEM_ERROR(code);
goto _exit; goto _exit;
} }
@ -130,7 +130,7 @@ _exit:
static int32_t load_json(const char *fname, cJSON **json) { static int32_t load_json(const char *fname, cJSON **json) {
int32_t code = 0; int32_t code = 0;
void *data = NULL; char *data = NULL;
TdFilePtr fp = taosOpenFile(fname, TD_FILE_READ); TdFilePtr fp = taosOpenFile(fname, TD_FILE_READ);
if (fp == NULL) return TAOS_SYSTEM_ERROR(code); if (fp == NULL) return TAOS_SYSTEM_ERROR(code);
@ -141,7 +141,7 @@ static int32_t load_json(const char *fname, cJSON **json) {
goto _exit; goto _exit;
} }
data = taosMemoryMalloc(size); data = taosMemoryMalloc(size + 1);
if (data == NULL) { if (data == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit; goto _exit;
@ -151,6 +151,7 @@ static int32_t load_json(const char *fname, cJSON **json) {
code = TAOS_SYSTEM_ERROR(code); code = TAOS_SYSTEM_ERROR(code);
goto _exit; goto _exit;
} }
data[size] = '\0';
json[0] = cJSON_Parse(data); json[0] = cJSON_Parse(data);
if (json[0] == NULL) { if (json[0] == NULL) {
@ -170,7 +171,7 @@ static int32_t save_fs(SArray *aTFileSet, const char *fname) {
int32_t lino = 0; int32_t lino = 0;
cJSON *json = cJSON_CreateObject(); cJSON *json = cJSON_CreateObject();
if (json == NULL) return TSDB_CODE_OUT_OF_MEMORY; if (!json) return TSDB_CODE_OUT_OF_MEMORY;
// fmtv // fmtv
if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) { if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
@ -180,9 +181,7 @@ static int32_t save_fs(SArray *aTFileSet, const char *fname) {
// fset // fset
cJSON *ajson = cJSON_AddArrayToObject(json, "fset"); cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
if (ajson == NULL) { if (!ajson) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
}
for (int32_t i = 0; i < taosArrayGetSize(aTFileSet); i++) { for (int32_t i = 0; i < taosArrayGetSize(aTFileSet); i++) {
STFileSet *pFileSet = (STFileSet *)taosArrayGet(aTFileSet, i); STFileSet *pFileSet = (STFileSet *)taosArrayGet(aTFileSet, i);
cJSON *item; cJSON *item;
@ -206,7 +205,7 @@ _exit:
return code; return code;
} }
static int32_t load_fs(const char *fname, SArray *aTFileSet, int64_t *eid) { static int32_t load_fs(const char *fname, SArray *aTFileSet) {
int32_t code = 0; int32_t code = 0;
int32_t lino = 0; int32_t lino = 0;
@ -228,25 +227,15 @@ static int32_t load_fs(const char *fname, SArray *aTFileSet, int64_t *eid) {
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit); TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
} }
/* eid */
item = cJSON_GetObjectItem(json, "eid");
if (cJSON_IsNumber(item)) {
eid[0] = item->valuedouble;
} else {
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
}
/* fset */ /* fset */
item = cJSON_GetObjectItem(json, "fset"); item = cJSON_GetObjectItem(json, "fset");
if (cJSON_IsArray(item)) { if (cJSON_IsArray(item)) {
const cJSON *titem; const cJSON *titem;
cJSON_ArrayForEach(titem, item) { cJSON_ArrayForEach(titem, item) {
STFileSet *pFileSet; STFileSet *fset = taosArrayReserve(aTFileSet, 1);
if ((pFileSet = taosArrayReserve(aTFileSet, 1)) == NULL) { if (!fset) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
}
code = tsdbFileSetFromJson(titem, pFileSet); code = tsdbJsonToFileSet(titem, fset);
TSDB_CHECK_CODE(code, lino, _exit); TSDB_CHECK_CODE(code, lino, _exit);
} }
} else { } else {
@ -359,7 +348,7 @@ static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M); current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
if (taosCheckExistFile(fCurrent)) { // current.json exists if (taosCheckExistFile(fCurrent)) { // current.json exists
code = load_fs(fCurrent, fs->cstate, &fs->neid); code = load_fs(fCurrent, fs->cstate);
TSDB_CHECK_CODE(code, lino, _exit); TSDB_CHECK_CODE(code, lino, _exit);
if (taosCheckExistFile(cCurrent)) { if (taosCheckExistFile(cCurrent)) {
@ -370,7 +359,7 @@ static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
code = abort_edit(fs); code = abort_edit(fs);
TSDB_CHECK_CODE(code, lino, _exit); TSDB_CHECK_CODE(code, lino, _exit);
} else { } else {
code = load_fs(cCurrent, fs->nstate, &fs->eid); code = load_fs(cCurrent, fs->nstate);
TSDB_CHECK_CODE(code, lino, _exit); TSDB_CHECK_CODE(code, lino, _exit);
code = commit_edit(fs); code = commit_edit(fs);

View File

@ -56,6 +56,29 @@ static int32_t add_file(STFileSet *fset, STFile *f) {
return 0; return 0;
} }
static int32_t stt_lvl_cmpr(const SRBTreeNode *n1, const SRBTreeNode *n2) {
SSttLvl *lvl1 = TCONTAINER_OF(n1, SSttLvl, rbtn);
SSttLvl *lvl2 = TCONTAINER_OF(n2, SSttLvl, rbtn);
if (lvl1->lvl < lvl2->lvl) {
return -1;
} else if (lvl1->lvl > lvl2->lvl) {
return 1;
}
return 0;
}
static int32_t fset_init(STFileSet *fset) {
memset(fset, 0, sizeof(*fset));
tRBTreeCreate(&fset->lvlTree, stt_lvl_cmpr);
return 0;
}
static int32_t fset_clear(STFileSet *fset) {
// TODO
return 0;
}
int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json) { int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json) {
int32_t code = 0; int32_t code = 0;
@ -85,9 +108,11 @@ int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json) {
return 0; return 0;
} }
int32_t tsdbFileSetFromJson(const cJSON *json, STFileSet *fset) { int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset) {
const cJSON *item; const cJSON *item;
fset_init(fset);
/* fid */ /* fid */
item = cJSON_GetObjectItem(json, "fid"); item = cJSON_GetObjectItem(json, "fid");
if (cJSON_IsNumber(item)) { if (cJSON_IsNumber(item)) {
@ -96,9 +121,19 @@ int32_t tsdbFileSetFromJson(const cJSON *json, STFileSet *fset) {
return TSDB_CODE_FILE_CORRUPTED; return TSDB_CODE_FILE_CORRUPTED;
} }
int32_t code;
for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) { for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
// int32_t code = tsdbTFileFromJson(json, ftype, &fset->farr[ftype]->f); STFile tf;
// if (code) return code; code = tsdbJsonToTFile(json, ftype, &tf);
if (code == TSDB_CODE_NOT_FOUND) {
continue;
} else if (code) {
return code;
} else {
// TODO
// code = tsdbFileObjCreate(&tf, &fset->farr[ftype]);
// if (code) return code;
}
} }
// each level // each level

View File

@ -212,23 +212,23 @@ int32_t tsdbTFileToJson(const STFile *file, cJSON *json) {
} }
} }
int32_t tsdbTFileFromJson(const cJSON *json, tsdb_ftype_t ftype, STFile **f) { int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f) {
const cJSON *item = cJSON_GetObjectItem(json, g_tfile_info[ftype].suffix); f[0] = (STFile){.type = ftype};
if (cJSON_IsObject(item)) {
f[0] = (STFile *)taosMemoryMalloc(sizeof(*f[0]));
if (f[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
int32_t code = g_tfile_info[ftype].from_json(item, f[0]); if (ftype == TSDB_FTYPE_STT) {
if (code) { int32_t code = g_tfile_info[ftype].from_json(json, f);
taosMemoryFree(f[0]); if (code) return code;
f[0] = NULL;
return code;
}
tsdbTFileInit(NULL /* TODO */, f[0]);
} else { } else {
f[0] = NULL; const cJSON *item = cJSON_GetObjectItem(json, g_tfile_info[ftype].suffix);
if (cJSON_IsObject(item)) {
int32_t code = g_tfile_info[ftype].from_json(item, f);
if (code) return code;
} else {
return TSDB_CODE_NOT_FOUND;
}
} }
// TODO: tsdbTFileInit(NULL, f);
return 0; return 0;
} }