diff --git a/source/dnode/vnode/src/tsdb/dev/inc/tsdbFile.h b/source/dnode/vnode/src/tsdb/dev/inc/tsdbFile.h index dc4b0505d8..abc24b8018 100644 --- a/source/dnode/vnode/src/tsdb/dev/inc/tsdbFile.h +++ b/source/dnode/vnode/src/tsdb/dev/inc/tsdbFile.h @@ -36,17 +36,16 @@ typedef enum { #define TSDB_FTYPE_MIN TSDB_FTYPE_HEAD #define TSDB_FTYPE_MAX (TSDB_FTYPE_TOMB + 1) +// STFile int32_t tsdbTFileToJson(const STFile *f, cJSON *json); int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f); - -// create/destroy -int32_t tsdbTFileCreate(const STFile *pFile, STFile **f); -int32_t tsdbTFileDestroy(STFile *pFile); - -// init/clear int32_t tsdbTFileInit(STsdb *pTsdb, STFile *pFile); int32_t tsdbTFileClear(STFile *pFile); +// STFileObj +int32_t tsdbTFileObjCreate(STFileObj **fobj); +int32_t tsdbTFileObjDestroy(STFileObj *fobj); + struct STFile { char fname[TSDB_FILENAME_LEN]; tsdb_ftype_t type; @@ -63,9 +62,9 @@ struct STFile { }; struct STFileObj { - SRBTreeNode rbtn; - int32_t ref; - STFile f; + SRBTreeNode rbtn; + volatile int32_t ref; + STFile f; }; #ifdef __cplusplus diff --git a/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c b/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c index 01b9012b78..ed0ad08ce4 100644 --- a/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c +++ b/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c @@ -38,8 +38,59 @@ static int32_t stt_lvl_to_json(const SSttLvl *lvl, cJSON *json) { return 0; } -static int32_t stt_lvl_from_json(const cJSON *json, SSttLvl *lvl) { - // TODO +static int32_t stt_file_cmpr(const SRBTreeNode *n1, const SRBTreeNode *n2) { + STFileObj *f1 = TCONTAINER_OF(n1, STFileObj, rbtn); + STFileObj *f2 = TCONTAINER_OF(n2, STFileObj, rbtn); + if (f1->f.cid < f2->f.cid) { + return -1; + } else if (f1->f.cid > f2->f.cid) { + return 1; + } + return 0; +} + +static int32_t stt_lvl_init(SSttLvl *lvl) { + lvl->lvl = 0; + lvl->nstt = 0; + tRBTreeCreate(&lvl->sttTree, stt_file_cmpr); + return 0; +} + +static int32_t add_file_to_stt_lvl(SSttLvl *lvl, STFileObj *fobj) { + lvl->nstt++; + tRBTreePut(&lvl->sttTree, &fobj->rbtn); + return 0; +} + +static int32_t json_to_stt_lvl(const cJSON *json, SSttLvl *lvl) { + stt_lvl_init(lvl); + + const cJSON *item1, *item2; + + item1 = cJSON_GetObjectItem(json, "lvl"); + if (cJSON_IsNumber(item1)) { + lvl->lvl = item1->valuedouble; + } else { + return TSDB_CODE_FILE_CORRUPTED; + } + + item1 = cJSON_GetObjectItem(json, "files"); + if (cJSON_IsArray(item1)) { + cJSON_ArrayForEach(item2, item1) { + STFileObj *fobj; + + int32_t code = tsdbTFileObjCreate(&fobj); + if (code) return code; + + code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &fobj->f); + if (code) return code; + + add_file_to_stt_lvl(lvl, fobj); + } + } else { + return TSDB_CODE_FILE_CORRUPTED; + } + return 0; } @@ -56,6 +107,11 @@ static int32_t add_file(STFileSet *fset, STFile *f) { return 0; } +static int32_t add_stt_lvl(STFileSet *fset, SSttLvl *lvl) { + tRBTreePut(&fset->lvlTree, &lvl->rbtn); + 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); @@ -109,37 +165,48 @@ int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json) { } int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset) { - const cJSON *item; + const cJSON *item1, *item2; fset_init(fset); /* fid */ - item = cJSON_GetObjectItem(json, "fid"); - if (cJSON_IsNumber(item)) { - fset->fid = item->valueint; + item1 = cJSON_GetObjectItem(json, "fid"); + if (cJSON_IsNumber(item1)) { + fset->fid = item1->valueint; } else { return TSDB_CODE_FILE_CORRUPTED; } int32_t code; + STFile tf; for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) { - STFile tf; 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; + code = tsdbTFileObjCreate(&fset->farr[ftype]); + if (code) return code; + fset->farr[ftype]->f = tf; } } // each level - item = cJSON_GetObjectItem(json, "stt"); - if (cJSON_IsArray(item)) { - // TODO + item1 = cJSON_GetObjectItem(json, "stt"); + if (cJSON_IsArray(item1)) { + cJSON_ArrayForEach(item2, item1) { + SSttLvl *lvl = taosMemoryCalloc(1, sizeof(*lvl)); + if (lvl == NULL) return TSDB_CODE_OUT_OF_MEMORY; + + code = json_to_stt_lvl(item2, lvl); + if (code) { + taosMemoryFree(lvl); + return code; + } + + add_stt_lvl(fset, lvl); + } } else { return TSDB_CODE_FILE_CORRUPTED; } diff --git a/source/dnode/vnode/src/tsdb/dev/tsdbFile.c b/source/dnode/vnode/src/tsdb/dev/tsdbFile.c index 470e087c89..9a824fbe6c 100644 --- a/source/dnode/vnode/src/tsdb/dev/tsdbFile.c +++ b/source/dnode/vnode/src/tsdb/dev/tsdbFile.c @@ -232,17 +232,17 @@ int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f) { return 0; } -int32_t tsdbTFileCreate(const STFile *pFile, STFile **f) { - f[0] = taosMemoryMalloc(sizeof(*f[0])); - if (f[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY; +int32_t tsdbTFileObjCreate(STFileObj **fobj) { + fobj[0] = taosMemoryMalloc(sizeof(STFileObj)); + if (fobj[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY; - *f[0] = *pFile; - - // f[0]->ref = 1; + fobj[0]->ref = 1; + // TODO return 0; } -int32_t tsdbTFileDestroy(STFile *pFile) { - taosMemoryFree(pFile); +int32_t tsdbTFileObjDestroy(STFileObj *fobj) { + // TODO + taosMemoryFree(fobj); return 0; -} \ No newline at end of file +}