more code

This commit is contained in:
Hongze Cheng 2023-05-16 14:35:57 +08:00
parent 7cb10a2b70
commit 0b17e3fe1c
4 changed files with 71 additions and 74 deletions

View File

@ -35,11 +35,9 @@ typedef enum {
int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json); int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json);
int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset); int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset);
int32_t tsdbFileSetInit(STFileSet *pSet, int32_t fid);
int32_t tsdbFileSetInit(STFileSet *pSet);
int32_t tsdbFileSetClear(STFileSet *pSet); int32_t tsdbFileSetClear(STFileSet *pSet);
int32_t tsdbFileSetEdit(STFileSet *fset, const STFileOp *op);
int32_t tsdbFSetEdit(STFileSet *pSet, const STFileOp *pOp);
int32_t tsdbFSetCmprFn(const STFileSet *pSet1, const STFileSet *pSet2); int32_t tsdbFSetCmprFn(const STFileSet *pSet1, const STFileSet *pSet2);
@ -51,7 +49,7 @@ struct STFileOp {
}; };
typedef struct SSttLvl { typedef struct SSttLvl {
int32_t lvl; // level int32_t level; // level
int32_t nstt; // number of .stt files on this level int32_t nstt; // number of .stt files on this level
SRBTree sttTree; // .stt file tree, sorted by cid SRBTree sttTree; // .stt file tree, sorted by cid
SRBTreeNode rbtn; SRBTreeNode rbtn;

View File

@ -409,37 +409,33 @@ static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSe
} }
static int32_t edit_fs(STFileSystem *pFS, const SArray *aFileOp) { static int32_t edit_fs(STFileSystem *pFS, const SArray *aFileOp) {
int32_t code = 0; int32_t code = 0;
int32_t lino = 0; int32_t lino = 0;
STFileSet *pSet = NULL; STFileSet *pSet = NULL;
for (int32_t iop = 0; iop < taosArrayGetSize(aFileOp); iop++) { for (int32_t iop = 0; iop < taosArrayGetSize(aFileOp); iop++) {
struct STFileOp *op = taosArrayGet(aFileOp, iop); struct STFileOp *op = taosArrayGet(aFileOp, iop);
if (pSet == NULL || pSet->fid != op->fid) { if (pSet == NULL || pSet->fid != op->fid) {
STFileSet fset = {.fid = op->fid}; STFileSet fset = {.fid = op->fid};
pSet = taosArraySearch(pFS->nstate, &fset, (__compar_fn_t)tsdbFSetCmprFn, TD_EQ); int32_t idx = taosArraySearchIdx(pFS->nstate, &fset, (__compar_fn_t)tsdbFSetCmprFn, TD_GE);
}
// create fset if need pSet = NULL;
if (pSet == NULL) { if (idx < 0) {
ASSERT(op->oState.size == 0 && op->nState.size > 0); idx = taosArrayGetSize(pFS->nstate);
} else {
STFileSet fset = {.fid = op->fid}; pSet = taosArrayGet(pFS->nstate, idx);
int32_t idx = taosArraySearchIdx(pFS->nstate, &fset, (__compar_fn_t)tsdbFSetCmprFn, TD_GT); if (pSet->fid != op->fid) pSet = NULL;
if (idx < 0) idx = taosArrayGetSize(pFS->nstate);
pSet = taosArrayInsert(pFS->nstate, idx, &fset);
if (pSet == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
TSDB_CHECK_CODE(code, lino, _exit);
} }
tsdbFileSetInit(pSet); if (!pSet) {
pSet = taosArrayInsert(pFS->nstate, idx, &fset);
if (!pSet) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
tsdbFileSetInit(pSet, op->fid);
}
} }
code = tsdbFSetEdit(pSet, op); code = tsdbFileSetEdit(pSet, op);
TSDB_CHECK_CODE(code, lino, _exit); TSDB_CHECK_CODE(code, lino, _exit);
} }

View File

@ -16,7 +16,7 @@
#include "inc/tsdbFSet.h" #include "inc/tsdbFSet.h"
static int32_t stt_lvl_to_json(const SSttLvl *lvl, cJSON *json) { static int32_t stt_lvl_to_json(const SSttLvl *lvl, cJSON *json) {
if (cJSON_AddNumberToObject(json, "lvl", lvl->lvl) == NULL) { if (cJSON_AddNumberToObject(json, "lvl", lvl->level) == NULL) {
return TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_OUT_OF_MEMORY;
} }
@ -49,8 +49,8 @@ static int32_t stt_file_cmpr(const SRBTreeNode *n1, const SRBTreeNode *n2) {
return 0; return 0;
} }
static int32_t stt_lvl_init(SSttLvl *lvl) { static int32_t stt_lvl_init(SSttLvl *lvl, int32_t level) {
lvl->lvl = 0; lvl->level = level;
lvl->nstt = 0; lvl->nstt = 0;
tRBTreeCreate(&lvl->sttTree, stt_file_cmpr); tRBTreeCreate(&lvl->sttTree, stt_file_cmpr);
return 0; return 0;
@ -63,17 +63,17 @@ static int32_t add_file_to_stt_lvl(SSttLvl *lvl, STFileObj *fobj) {
} }
static int32_t json_to_stt_lvl(const cJSON *json, SSttLvl *lvl) { static int32_t json_to_stt_lvl(const cJSON *json, SSttLvl *lvl) {
stt_lvl_init(lvl);
const cJSON *item1, *item2; const cJSON *item1, *item2;
item1 = cJSON_GetObjectItem(json, "lvl"); item1 = cJSON_GetObjectItem(json, "lvl");
if (cJSON_IsNumber(item1)) { if (cJSON_IsNumber(item1)) {
lvl->lvl = item1->valuedouble; lvl->level = item1->valuedouble;
} else { } else {
return TSDB_CODE_FILE_CORRUPTED; return TSDB_CODE_FILE_CORRUPTED;
} }
stt_lvl_init(lvl, lvl->level);
item1 = cJSON_GetObjectItem(json, "files"); item1 = cJSON_GetObjectItem(json, "files");
if (cJSON_IsArray(item1)) { if (cJSON_IsArray(item1)) {
cJSON_ArrayForEach(item2, item1) { cJSON_ArrayForEach(item2, item1) {
@ -94,21 +94,31 @@ static int32_t json_to_stt_lvl(const cJSON *json, SSttLvl *lvl) {
return 0; return 0;
} }
static int32_t add_file(STFileSet *fset, STFile *f) { static int32_t add_stt_lvl(STFileSet *fset, SSttLvl *lvl) {
if (f->type == TSDB_FTYPE_STT) { tRBTreePut(&fset->lvlTree, &lvl->rbtn);
SSttLvl *lvl = NULL; // TODO
// lvl->nstt++;
// lvl->fstt = f;
} else {
// fset->farr[f->type] = f;
}
return 0; return 0;
} }
static int32_t add_stt_lvl(STFileSet *fset, SSttLvl *lvl) { static int32_t add_file_to_fset(STFileSet *fset, STFileObj *fobj) {
tRBTreePut(&fset->lvlTree, &lvl->rbtn); if (fobj->f.type == TSDB_FTYPE_STT) {
SSttLvl *lvl;
SSttLvl tlvl = {.level = fobj->f.stt.lvl};
SRBTreeNode *node = tRBTreeGet(&fset->lvlTree, &tlvl.rbtn);
if (node) {
lvl = TCONTAINER_OF(node, SSttLvl, rbtn);
} else {
lvl = taosMemoryMalloc(sizeof(*lvl));
if (!lvl) return TSDB_CODE_OUT_OF_MEMORY;
stt_lvl_init(lvl, fobj->f.stt.lvl);
add_stt_lvl(fset, lvl);
}
add_file_to_stt_lvl(lvl, fobj);
} else {
fset->farr[fobj->f.type] = fobj;
}
return 0; return 0;
} }
@ -116,16 +126,19 @@ static int32_t stt_lvl_cmpr(const SRBTreeNode *n1, const SRBTreeNode *n2) {
SSttLvl *lvl1 = TCONTAINER_OF(n1, SSttLvl, rbtn); SSttLvl *lvl1 = TCONTAINER_OF(n1, SSttLvl, rbtn);
SSttLvl *lvl2 = TCONTAINER_OF(n2, SSttLvl, rbtn); SSttLvl *lvl2 = TCONTAINER_OF(n2, SSttLvl, rbtn);
if (lvl1->lvl < lvl2->lvl) { if (lvl1->level < lvl2->level) {
return -1; return -1;
} else if (lvl1->lvl > lvl2->lvl) { } else if (lvl1->level > lvl2->level) {
return 1; return 1;
} }
return 0; return 0;
} }
static int32_t fset_init(STFileSet *fset) { static int32_t fset_init(STFileSet *fset, int32_t fid) {
memset(fset, 0, sizeof(*fset)); fset->fid = fid;
for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
fset->farr[ftype] = NULL;
}
tRBTreeCreate(&fset->lvlTree, stt_lvl_cmpr); tRBTreeCreate(&fset->lvlTree, stt_lvl_cmpr);
return 0; return 0;
} }
@ -166,8 +179,8 @@ int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json) {
int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset) { int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset) {
const cJSON *item1, *item2; const cJSON *item1, *item2;
int32_t code;
fset_init(fset); STFile tf;
/* fid */ /* fid */
item1 = cJSON_GetObjectItem(json, "fid"); item1 = cJSON_GetObjectItem(json, "fid");
@ -177,8 +190,7 @@ int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset) {
return TSDB_CODE_FILE_CORRUPTED; return TSDB_CODE_FILE_CORRUPTED;
} }
int32_t code; fset_init(fset, fset->fid);
STFile tf;
for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) { for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
code = tsdbJsonToTFile(json, ftype, &tf); code = tsdbJsonToTFile(json, ftype, &tf);
if (code == TSDB_CODE_NOT_FOUND) { if (code == TSDB_CODE_NOT_FOUND) {
@ -220,37 +232,28 @@ int32_t tsdbFSetCmprFn(const STFileSet *pSet1, const STFileSet *pSet2) {
return 0; return 0;
} }
int32_t tsdbFSetEdit(STFileSet *fset, const STFileOp *op) { int32_t tsdbFileSetEdit(STFileSet *fset, const STFileOp *op) {
int32_t code; int32_t code = 0;
ASSERT(fset->fid == op->fid); if (op->oState.size == 0 //
|| 0 /* TODO*/
if (op->oState.size == 0) { ) {
// create STFileObj *fobj;
// STFile *f; code = tsdbTFileObjCreate(&fobj);
// code = tsdbTFileCreate(&op->nState, &f); if (code) return code;
// if (code) return code; fobj->f = op->nState;
add_file_to_fset(fset, fobj);
// add_file(fset, f);
} else if (op->nState.size == 0) { } else if (op->nState.size == 0) {
// delete // delete
ASSERT(0);
} else { } else {
// modify // modify
ASSERT(0);
} }
return 0; return 0;
} }
int32_t tsdbFileSetInit(STFileSet *pSet) { int32_t tsdbFileSetInit(STFileSet *pSet, int32_t fid) { return fset_init(pSet, fid); }
for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ftype++) {
pSet->farr[ftype] = NULL;
}
// LISTD_INIT(&pSet->lvl0, listNode);
// pSet->lvl0.lvl = 0;
// pSet->lvl0.nstt = 0;
// pSet->lvl0.fstt = NULL;
return 0;
}
int32_t tsdbFileSetClear(STFileSet *pSet) { int32_t tsdbFileSetClear(STFileSet *pSet) {
// TODO // TODO

View File

@ -234,7 +234,7 @@ int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f) {
int32_t tsdbTFileObjCreate(STFileObj **fobj) { int32_t tsdbTFileObjCreate(STFileObj **fobj) {
fobj[0] = taosMemoryMalloc(sizeof(STFileObj)); fobj[0] = taosMemoryMalloc(sizeof(STFileObj));
if (fobj[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY; if (!fobj[0]) return TSDB_CODE_OUT_OF_MEMORY;
fobj[0]->ref = 1; fobj[0]->ref = 1;
// TODO // TODO