提交 1c10694f 编写于 作者: H Hongze Cheng

more code

上级 36b2495c
......@@ -29,6 +29,12 @@ typedef TARRAY2(STFileObj *) TFileObjArray;
typedef TARRAY2(SSttLvl *) TSttLvlArray;
typedef TARRAY2(STFileOp) TFileOpArray;
typedef enum {
TSDB_FOP_CREATE = 1,
TSDB_FOP_REMOVE,
TSDB_FOP_MODIFY,
} tsdb_fop_t;
// init/clear
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset);
int32_t tsdbTFileSetInitEx(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset);
......@@ -50,10 +56,10 @@ SSttLvl *tsdbTFileSetGetLvl(STFileSet *fset, int32_t level);
bool tsdbTFileSetIsEmpty(const STFileSet *fset);
struct STFileOp {
int32_t fid;
STFile *of; // old file
STFile *nf; // new file
STFile fArr[2];
int32_t fid;
tsdb_fop_t optype;
STFile of; // old file state
STFile nf; // new file state
};
struct SSttLvl {
......
......@@ -256,38 +256,68 @@ int32_t tsdbJsonToTFileSet(STsdb *pTsdb, const cJSON *json, STFileSet **fset) {
return 0;
}
// NOTE: the api does not remove file, only do memory operation
int32_t tsdbTFileSetEdit(STsdb *pTsdb, STFileSet *fset, const STFileOp *op) {
int32_t code = 0;
// if (op->of.size == 0 //
// || 0 /* TODO*/
// ) {
// STFileObj *fobj;
// code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
// if (code) return code;
// if (fobj->f.type == TSDB_FTYPE_STT) {
// SSttLvl *lvl = tsdbTFileSetGetLvl(fset, fobj->f.stt.level);
// if (!lvl) {
// code = tsdbSttLvlInit(fobj->f.stt.level, &lvl);
// if (code) return code;
// code = TARRAY2_SORT_INSERT(&fset->lvlArr, lvl, tsdbSttLvlCmprFn);
// if (code) return code;
// }
// code = TARRAY2_SORT_INSERT(&lvl->farr, fobj, tsdbTFileObjCmpr);
// if (code) return code;
// } else {
// fset->farr[fobj->f.type] = fobj;
// }
// } else if (op->nf.size == 0) {
// // delete
// ASSERT(0);
// } else {
// // modify
// ASSERT(0);
// }
if (op->optype == TSDB_FOP_CREATE) {
// create a new file
STFileObj *fobj;
code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
if (code) return code;
if (fobj->f.type == TSDB_FTYPE_STT) {
SSttLvl *lvl = tsdbTFileSetGetLvl(fset, fobj->f.stt.level);
if (!lvl) {
code = tsdbSttLvlInit(fobj->f.stt.level, &lvl);
if (code) return code;
code = TARRAY2_SORT_INSERT(&fset->lvlArr, lvl, tsdbSttLvlCmprFn);
if (code) return code;
}
code = TARRAY2_SORT_INSERT(&lvl->farr, fobj, tsdbTFileObjCmpr);
if (code) return code;
} else {
ASSERT(fset->farr[fobj->f.type] == NULL);
fset->farr[fobj->f.type] = fobj;
}
} else if (op->optype == TSDB_FOP_REMOVE) {
// delete a file
if (op->of.type == TSDB_FTYPE_STT) {
SSttLvl *lvl = tsdbTFileSetGetLvl(fset, op->of.stt.level);
ASSERT(lvl);
STFileObj tfobj = {.f = {.cid = op->of.cid}};
STFileObj *tfobjp = &tfobj;
int32_t idx = TARRAY2_SEARCH_IDX(&lvl->farr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
ASSERT(idx >= 0);
TARRAY2_REMOVE(&lvl->farr, idx, tsdbSttLvlRemoveFObj);
if (TARRAY2_SIZE(&lvl->farr) == 0) {
// TODO: remove the stt level if no file exists anymore
// TARRAY2_REMOVE(&fset->lvlArr, lvl - fset->lvlArr.data, tsdbSttLvlClear);
}
} else {
ASSERT(tsdbIsSameTFile(&op->of, &fset->farr[op->of.type]->f));
tsdbTFileObjUnref(fset->farr[op->of.type]);
fset->farr[op->of.type] = NULL;
}
} else {
if (op->nf.type == TSDB_FTYPE_STT) {
SSttLvl *lvl = tsdbTFileSetGetLvl(fset, op->of.stt.level);
ASSERT(lvl);
STFileObj tfobj = {.f = {.cid = op->of.cid}}, *tfobjp = &tfobj;
tfobjp = TARRAY2_SEARCH_EX(&lvl->farr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
ASSERT(tfobjp);
tfobjp->f = op->nf;
} else {
fset->farr[op->nf.type]->f = op->nf;
}
}
return 0;
}
......
......@@ -573,13 +573,12 @@ int32_t tsdbSttFWriterClose(SSttFileWriter **ppWriter, int8_t abort, struct STFi
STFile *f = &ppWriter[0]->config.file;
op->fid = f->fid;
if (f->size == 0) {
op->of = NULL;
op->optype = TSDB_FOP_CREATE;
} else {
op->of = &op->fArr[0];
op->of[0] = f[0];
op->optype = TSDB_FOP_MODIFY;
}
op->nf = &op->fArr[1];
op->nf[0] = ppWriter[0]->tFile;
op->of = f[0];
op->nf = ppWriter[0]->tFile;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册