diff --git a/include/util/tarray2.h b/include/util/tarray2.h index 5aec8f6892..a0319052d5 100644 --- a/include/util/tarray2.h +++ b/include/util/tarray2.h @@ -93,9 +93,6 @@ static FORCE_INLINE int32_t tarray2_make_room(void *arg, // array TARRAY2_FREE(a); \ } while (0) -#define TARRAY2_SEARCH(a, ep, cmp, flag) \ - (((a)->size == 0) ? NULL : taosbsearch(ep, (a)->data, (a)->size, sizeof(typeof((a)->data[0])), cmp, flag)) - #define TARRAY2_INSERT(a, idx, e) \ ({ \ int32_t __ret = 0; \ @@ -116,6 +113,24 @@ static FORCE_INLINE int32_t tarray2_make_room(void *arg, // array #define TARRAY2_APPEND(a, e) TARRAY2_INSERT(a, (a)->size, e) #define TARRAY2_APPEND_P(a, ep) TARRAY2_APPEND(a, *(ep)) +#define TARRAY2_SEARCH(a, ep, cmp, flag) \ + (((a)->size == 0) ? NULL \ + : taosbsearch(ep, (a)->data, (a)->size, sizeof(typeof((a)->data[0])), (__compar_fn_t)cmp, flag)) + +#define TARRAY2_SEARCH_IDX(a, ep, cmp, flag) \ + ({ \ + typeof((a)->data) __p = TARRAY2_SEARCH(a, ep, cmp, flag); \ + __p ? __p - (a)->data : -1; \ + }) + +#define TARRAY2_SORT_INSERT(a, e, cmp) \ + ({ \ + int32_t __idx = TARRAY2_SEARCH_IDX(a, &(e), cmp, TD_GT); \ + TARRAY2_INSERT(a, __idx < 0 ? (a)->size : __idx, e); \ + }) + +#define TARRAY2_SORT_INSERT_P(a, ep, cmp) TARRAY2_SORT_INSERT(a, *(ep), cmp) + #define TARRAY2_REMOVE(a, idx, cb) \ do { \ if ((idx) < (a)->size) { \ diff --git a/source/dnode/vnode/src/tsdb/dev/inc/tsdbFS.h b/source/dnode/vnode/src/tsdb/dev/inc/tsdbFS.h index 40bdb55287..b39d5fc051 100644 --- a/source/dnode/vnode/src/tsdb/dev/inc/tsdbFS.h +++ b/source/dnode/vnode/src/tsdb/dev/inc/tsdbFS.h @@ -24,7 +24,6 @@ extern "C" { /* Exposed Handle */ typedef struct STFileSystem STFileSystem; -typedef TARRAY2(STFileOp) TFileOpArray; typedef enum { TSDB_FEDIT_COMMIT = 1, // diff --git a/source/dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h b/source/dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h index 2639d805ab..80b20e5436 100644 --- a/source/dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h +++ b/source/dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h @@ -27,6 +27,7 @@ typedef struct STFileOp STFileOp; typedef struct SSttLvl SSttLvl; typedef TARRAY2(STFileSet *) TFileSetArray; typedef TARRAY2(SSttLvl *) TSttLvlArray; +typedef TARRAY2(STFileOp) TFileOpArray; typedef enum { TSDB_FOP_NONE = 0, @@ -43,7 +44,10 @@ int32_t tsdbTFileSetClear(STFileSet **fset); int32_t tsdbTFileSetToJson(const STFileSet *fset, cJSON *json); int32_t tsdbJsonToTFileSet(const cJSON *json, STFileSet **fset); +int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2); + int32_t tsdbTFileSetEdit(STFileSet *fset, const STFileOp *op); +int32_t tsdbTFileSetEditEx(const STFileSet *fset1, STFileSet *fset); const SSttLvl *tsdbTFileSetGetLvl(const STFileSet *fset, int32_t level); diff --git a/source/dnode/vnode/src/tsdb/dev/tsdbFS.c b/source/dnode/vnode/src/tsdb/dev/tsdbFS.c index a9ec116b37..19f98fbbb7 100644 --- a/source/dnode/vnode/src/tsdb/dev/tsdbFS.c +++ b/source/dnode/vnode/src/tsdb/dev/tsdbFS.c @@ -499,39 +499,37 @@ static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSe return 0; } -static int32_t edit_fs(STFileSystem *pFS, const SArray *aFileOp) { - int32_t code = 0; - int32_t lino = 0; - STFileSet *pSet = NULL; +static int32_t edit_fs(TFileSetArray *fset_arr, const TFileOpArray *op_arr) { + int32_t code = 0; + int32_t lino = 0; - for (int32_t iop = 0; iop < taosArrayGetSize(aFileOp); iop++) { - struct STFileOp *op = taosArrayGet(aFileOp, iop); + STFileSet *fset = NULL; + const STFileOp *op; + TARRAY2_FOREACH_PTR(op_arr, op) { + if (!fset || fset->fid != op->fid) { + STFileSet tfset = {.fid = op->fid}; + fset = &tfset; + fset = TARRAY2_SEARCH(fset_arr, &fset, tsdbTFileSetCmprFn, TD_EQ); - if (pSet == NULL || pSet->fid != op->fid) { - // STFileSet fset = {.fid = op->fid}; - // int32_t idx = taosArraySearchIdx(pFS->nstate, &fset, (__compar_fn_t)tsdbFSetCmprFn, TD_GE); + if (!fset) { + code = tsdbTFileSetInit(op->fid, &fset); + TSDB_CHECK_CODE(code, lino, _exit); - // pSet = NULL; - // if (idx < 0) { - // idx = taosArrayGetSize(pFS->nstate); - // } else { - // pSet = taosArrayGet(pFS->nstate, idx); - // if (pSet->fid != op->fid) pSet = NULL; - // } - - // 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 = TARRAY2_SORT_INSERT(fset_arr, fset, tsdbTFileSetCmprFn); + TSDB_CHECK_CODE(code, lino, _exit); + } } - code = tsdbTFileSetEdit(pSet, op); + code = tsdbTFileSetEdit(fset, op); TSDB_CHECK_CODE(code, lino, _exit); + + if (0) { + // TODO check if the file set should be deleted + } } _exit: - return 0; + return code; } int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) { @@ -588,7 +586,7 @@ int32_t tsdbFSEditBegin(STFileSystem *fs, const SArray *aFileOp, EFEditT etype) fs->etype = etype; // edit - code = edit_fs(fs, aFileOp); + code = edit_fs(&fs->nstate, NULL /* TODO */); TSDB_CHECK_CODE(code, lino, _exit); // save fs diff --git a/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c b/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c index 76e0c14444..f912f43eca 100644 --- a/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c +++ b/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c @@ -208,6 +208,11 @@ int32_t tsdbTFileSetEdit(STFileSet *fset, const STFileOp *op) { return 0; } +int32_t tsdbTFileSetEditEx(const STFileSet *fset1, STFileSet *fset) { + // TODO + return 0; +} + int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) { fset[0] = taosMemoryCalloc(1, sizeof(STFileSet)); if (fset[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY; @@ -268,4 +273,10 @@ const SSttLvl *tsdbTFileSetGetLvl(const STFileSet *fset, int32_t level) { // return node ? TCONTAINER_OF(node, SSttLvl, rbtn) : NULL; // TODO return NULL; +} + +int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2) { + if (fset1[0]->fid < fset2[0]->fid) return -1; + if (fset1[0]->fid > fset2[0]->fid) return 1; + return 0; } \ No newline at end of file