提交 e2478705 编写于 作者: H Hongze Cheng

more code

上级 66ea1075
......@@ -77,45 +77,55 @@ static FORCE_INLINE int32_t tarray2_make_room(void *arg, // array
#define TARRAY2_CLEAR(a, cb) \
do { \
if (cb) { \
void (*cb_)(void *) = (cb); \
for (int32_t i = 0; i < (a)->size; ++i) { \
cb((a)->data + i); \
cb_((a)->data + i); \
} \
} \
(a)->size = 0; \
} while (0)
#define TARRAY2_CLEAR_FREE(a, cb) \
do { \
TARRAY2_CLEAR(a, cb); \
TARRAY2_FREE(a); \
} while (0)
#define TARRAY2_SEARCH(a, ep, cmp, flag) \
(((a)->size == 0) ? NULL : taosbsearch(ep, (a)->data, (a)->size, sizeof((a)->data[0]), cmp, flag))
#define TARRAY2_INSERT(a, idx, e) \
({ \
int32_t __ret = 0; \
if ((a)->size >= (a)->capacity) { \
__ret = tarray2_make_room(&(a), (a)->size + 1, sizeof(*(a)->data)); \
} \
if (!__ret) { \
if ((a)->size > (idx)) { \
memmove((a)->data + (idx) + 1, (a)->data + (idx), sizeof(*(a)->data) * ((a)->size - (idx))); \
} \
(a)->data[(idx)] = e; \
(a)->size++; \
} \
__ret; \
(((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; \
if ((a)->size >= (a)->capacity) { \
__ret = tarray2_make_room(&(a), (a)->size + 1, sizeof(typeof((a)->data[0]))); \
} \
if (!__ret) { \
if ((a)->size > (idx)) { \
memmove((a)->data + (idx) + 1, (a)->data + (idx), sizeof(typeof((a)->data[0])) * ((a)->size - (idx))); \
} \
(a)->data[(idx)] = e; \
(a)->size++; \
} \
__ret; \
})
#define TARRAY2_INSERT_P(a, idx, ep) TARRAY2_INSERT(a, idx, *(ep))
#define TARRAY2_APPEND(a, e) TARRAY2_INSERT(a, (a)->size, e)
#define TARRAY2_APPEND_P(a, ep) TARRAY2_APPEND(a, *(ep))
#define TARRAY2_REMOVE(a, idx, cb) \
do { \
if ((idx) < (a)->size) { \
if (cb) cb((a)->data + (idx)); \
if ((idx) < (a)->size - 1) { \
memmove((a)->data + (idx), (a)->data + (idx) + 1, sizeof(*(a)->data) * ((a)->size - (idx)-1)); \
} \
(a)->size--; \
} \
#define TARRAY2_REMOVE(a, idx, cb) \
do { \
if ((idx) < (a)->size) { \
if (cb) { \
void (*cb_)(void *) = cb; \
cb_((a)->data + (idx)); \
} \
if ((idx) < (a)->size - 1) { \
memmove((a)->data + (idx), (a)->data + (idx) + 1, sizeof(typeof(*(a)->data)) * ((a)->size - (idx)-1)); \
} \
(a)->size--; \
} \
} while (0)
#define TARRAY2_FOREACH(a, e) for (int32_t __i = 0; __i < (a)->size && ((e) = (a)->data[__i], 1); __i++)
......
......@@ -44,14 +44,14 @@ int32_t tsdbFSGetFSet(STFileSystem *fs, int32_t fid, const STFileSet **ppFSet);
/* Exposed Structs */
struct STFileSystem {
STsdb *pTsdb;
tsem_t canEdit;
int32_t state;
int64_t neid;
EFEditT etype;
int64_t eid;
TARRAY2(STFileSet *) cstate;
TARRAY2(STFileSet *) nstate;
STsdb *pTsdb;
tsem_t canEdit;
int32_t state;
int64_t neid;
EFEditT etype;
int64_t eid;
TFileSetArray cstate;
TFileSetArray nstate;
};
#ifdef __cplusplus
......
......@@ -25,6 +25,8 @@ extern "C" {
typedef struct STFileSet STFileSet;
typedef struct STFileOp STFileOp;
typedef struct SSttLvl SSttLvl;
typedef TARRAY2(STFileSet *) TFileSetArray;
typedef TARRAY2(SSttLvl *) TSttLvlArray;
typedef enum {
TSDB_FOP_NONE = 0,
......@@ -34,11 +36,12 @@ typedef enum {
TSDB_FOP_TRUNCATE,
} tsdb_fop_t;
int32_t tsdbFileSetInit(int32_t fid, STFileSet **fset);
int32_t tsdbFileSetInitEx(const STFileSet *fset1, STFileSet **fset2);
int32_t tsdbFileSetClear(STFileSet **fset);
int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json);
int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset);
int32_t tsdbFileSetInit(STFileSet *pSet, int32_t fid);
int32_t tsdbFileSetInitEx(const STFileSet *fset1, STFileSet *fset2);
int32_t tsdbFileSetClear(STFileSet *pSet);
int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet **fset);
int32_t tsdbFileSetEdit(STFileSet *fset, const STFileOp *op);
int32_t tsdbFSetCmprFn(const STFileSet *pSet1, const STFileSet *pSet2);
......@@ -59,9 +62,9 @@ struct SSttLvl {
};
struct STFileSet {
int32_t fid;
STFileObj *farr[TSDB_FTYPE_MAX]; // file array
SRBTree lvlTree; // SRBTree<SSttLvl>, level tree of .stt
int32_t fid;
STFileObj *farr[TSDB_FTYPE_MAX]; // file array
TSttLvlArray lvlArr; // level array
};
#ifdef __cplusplus
......
......@@ -37,13 +37,13 @@ typedef enum {
#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);
int32_t tsdbTFileInit(STsdb *pTsdb, STFile *pFile);
int32_t tsdbTFileClear(STFile *pFile);
int32_t tsdbTFileToJson(const STFile *f, cJSON *json);
int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f);
// STFileObj
int32_t tsdbTFileObjCreate(STFileObj **fobj);
int32_t tsdbTFileObjCreate(const STFile *f, STFileObj **fobj);
int32_t tsdbTFileObjDestroy(STFileObj *fobj);
struct STFile {
......
......@@ -85,7 +85,7 @@ static int32_t current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
static int32_t save_json(const cJSON *json, const char *fname) {
int32_t code = 0;
char *data = cJSON_Print(json);
char *data = cJSON_PrintUnformatted(json);
if (data == NULL) return TSDB_CODE_OUT_OF_MEMORY;
TdFilePtr fp = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
......@@ -149,7 +149,7 @@ _exit:
return code;
}
static int32_t save_fs(SArray *aTFileSet, const char *fname) {
static int32_t save_fs(const TFileSetArray *arr, const char *fname) {
int32_t code = 0;
int32_t lino = 0;
......@@ -165,15 +165,13 @@ static int32_t save_fs(SArray *aTFileSet, const char *fname) {
// fset
cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
if (!ajson) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
for (int32_t i = 0; i < taosArrayGetSize(aTFileSet); i++) {
STFileSet *pFileSet = (STFileSet *)taosArrayGet(aTFileSet, i);
cJSON *item;
item = cJSON_CreateObject();
const STFileSet *fset;
TARRAY2_FOREACH(arr, fset) {
cJSON *item = cJSON_CreateObject();
if (!item) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
cJSON_AddItemToArray(ajson, item);
code = tsdbFileSetToJson(pFileSet, item);
code = tsdbFileSetToJson(fset, item);
TSDB_CHECK_CODE(code, lino, _exit);
}
......@@ -188,11 +186,11 @@ _exit:
return code;
}
static int32_t load_fs(const char *fname, SArray *aTFileSet) {
static int32_t load_fs(const char *fname, TFileSetArray *arr) {
int32_t code = 0;
int32_t lino = 0;
taosArrayClear(aTFileSet);
TARRAY2_CLEAR(arr, NULL);
// load json
cJSON *json = NULL;
......@@ -215,10 +213,11 @@ static int32_t load_fs(const char *fname, SArray *aTFileSet) {
if (cJSON_IsArray(item)) {
const cJSON *titem;
cJSON_ArrayForEach(titem, item) {
STFileSet *fset = taosArrayReserve(aTFileSet, 1);
if (!fset) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
STFileSet *fset;
code = tsdbJsonToFileSet(titem, &fset);
TSDB_CHECK_CODE(code, lino, _exit);
code = tsdbJsonToFileSet(titem, fset);
code = TARRAY2_APPEND(arr, fset);
TSDB_CHECK_CODE(code, lino, _exit);
}
} else {
......@@ -420,8 +419,8 @@ static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
if (taosCheckExistFile(fCurrent)) { // current.json exists
// code = load_fs(fCurrent, fs->cstate);
// TSDB_CHECK_CODE(code, lino, _exit);
code = load_fs(fCurrent, &fs->cstate);
TSDB_CHECK_CODE(code, lino, _exit);
if (taosCheckExistFile(cCurrent)) {
// current.c.json exists
......@@ -447,8 +446,8 @@ static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
code = scan_and_fix_fs(fs);
TSDB_CHECK_CODE(code, lino, _exit);
} else {
// code = save_fs(fs->cstate, fCurrent);
// TSDB_CHECK_CODE(code, lino, _exit);
code = save_fs(&fs->cstate, fCurrent);
TSDB_CHECK_CODE(code, lino, _exit);
}
_exit:
......
......@@ -79,11 +79,11 @@ static int32_t json_to_stt_lvl(const cJSON *json, SSttLvl *lvl) {
cJSON_ArrayForEach(item2, item1) {
STFileObj *fobj;
int32_t code = tsdbTFileObjCreate(&fobj);
if (code) return code;
// int32_t code = tsdbTFileObjCreate(&fobj);
// if (code) return code;
code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &fobj->f);
if (code) return code;
// code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &fobj->f);
// if (code) return code;
add_file_to_stt_lvl(lvl, fobj);
}
......@@ -95,29 +95,29 @@ static int32_t json_to_stt_lvl(const cJSON *json, SSttLvl *lvl) {
}
static int32_t add_stt_lvl(STFileSet *fset, SSttLvl *lvl) {
tRBTreePut(&fset->lvlTree, &lvl->rbtn);
// tRBTreePut(&fset->lvlTree, &lvl->rbtn);
return 0;
}
static int32_t add_file_to_fset(STFileSet *fset, STFileObj *fobj) {
if (fobj->f.type == TSDB_FTYPE_STT) {
SSttLvl *lvl;
SSttLvl tlvl = {.level = fobj->f.stt.level};
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.level);
add_stt_lvl(fset, lvl);
}
add_file_to_stt_lvl(lvl, fobj);
} else {
fset->farr[fobj->f.type] = fobj;
}
// if (fobj->f.type == TSDB_FTYPE_STT) {
// SSttLvl *lvl;
// SSttLvl tlvl = {.level = fobj->f.stt.level};
// 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.level);
// add_stt_lvl(fset, lvl);
// }
// add_file_to_stt_lvl(lvl, fobj);
// } else {
// fset->farr[fobj->f.type] = fobj;
// }
return 0;
}
......@@ -134,14 +134,14 @@ static int32_t stt_lvl_cmpr(const SRBTreeNode *n1, const SRBTreeNode *n2) {
return 0;
}
static int32_t fset_init(STFileSet *fset, int32_t fid) {
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);
return 0;
}
// static int32_t fset_init(STFileSet *fset, int32_t fid) {
// 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);
// return 0;
// }
static int32_t fset_clear(STFileSet *fset) {
// TODO
......@@ -167,64 +167,64 @@ int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json) {
// each level
item1 = cJSON_AddArrayToObject(json, "stt levels");
if (item1 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
SRBTreeIter iter = tRBTreeIterCreate(&fset->lvlTree, 1);
for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
const SSttLvl *lvl;
TARRAY2_FOREACH(&fset->lvlArr, lvl) {
item2 = cJSON_CreateObject();
if (!item2) return TSDB_CODE_OUT_OF_MEMORY;
cJSON_AddItemToArray(item1, item2);
code = stt_lvl_to_json(TCONTAINER_OF(node, SSttLvl, rbtn), item2);
code = stt_lvl_to_json(lvl, item2);
if (code) return code;
}
return 0;
}
int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet *fset) {
const cJSON *item1, *item2;
int32_t code;
STFile tf;
/* fid */
item1 = cJSON_GetObjectItem(json, "fid");
if (cJSON_IsNumber(item1)) {
fset->fid = item1->valueint;
} else {
return TSDB_CODE_FILE_CORRUPTED;
}
fset_init(fset, fset->fid);
for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
code = tsdbJsonToTFile(json, ftype, &tf);
if (code == TSDB_CODE_NOT_FOUND) {
continue;
} else if (code) {
return code;
} else {
code = tsdbTFileObjCreate(&fset->farr[ftype]);
if (code) return code;
fset->farr[ftype]->f = tf;
}
}
// each level
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;
}
int32_t tsdbJsonToFileSet(const cJSON *json, STFileSet **fset) {
// const cJSON *item1, *item2;
// int32_t code;
// STFile tf;
// /* fid */
// item1 = cJSON_GetObjectItem(json, "fid");
// if (cJSON_IsNumber(item1)) {
// fset->fid = item1->valueint;
// } else {
// return TSDB_CODE_FILE_CORRUPTED;
// }
// fset_init(fset, fset->fid);
// for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
// code = tsdbJsonToTFile(json, ftype, &tf);
// if (code == TSDB_CODE_NOT_FOUND) {
// continue;
// } else if (code) {
// return code;
// } else {
// code = tsdbTFileObjCreate(&fset->farr[ftype]);
// if (code) return code;
// fset->farr[ftype]->f = tf;
// }
// }
// // each level
// 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;
// }
return 0;
}
......@@ -242,7 +242,7 @@ int32_t tsdbFileSetEdit(STFileSet *fset, const STFileOp *op) {
|| 0 /* TODO*/
) {
STFileObj *fobj;
code = tsdbTFileObjCreate(&fobj);
// code = tsdbTFileObjCreate(&fobj);
if (code) return code;
fobj->f = op->nState;
add_file_to_fset(fset, fobj);
......@@ -256,48 +256,85 @@ int32_t tsdbFileSetEdit(STFileSet *fset, const STFileOp *op) {
return 0;
}
int32_t tsdbFileSetInit(STFileSet *pSet, int32_t fid) { return fset_init(pSet, fid); }
int32_t tsdbFileSetInit(int32_t fid, STFileSet **fset) {
fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
if (fset[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
fset[0]->fid = fid;
TARRAY2_INIT(&fset[0]->lvlArr);
return 0;
}
int32_t tsdbFileSetInitEx(const STFileSet *fset1, STFileSet *fset2) {
int32_t code;
int32_t tsdbFileSetInitEx(const STFileSet *fset1, STFileSet **fset) {
int32_t code = tsdbFileSetInit(fset1->fid, fset);
if (code) return code;
fset_init(fset2, fset1->fid);
for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
if (fset1->farr[ftype] == NULL) continue;
code = tsdbTFileObjCreate(&fset2->farr[ftype]);
if (code) return code;
fset2->farr[ftype]->f = fset1->farr[ftype]->f;
code = tsdbTFileObjCreate(&fset1->farr[ftype]->f, &fset[0]->farr[ftype]);
if (code) {
tsdbFileSetClear(fset);
return code;
}
}
SRBTreeIter iter = tRBTreeIterCreate(&fset1->lvlTree, 1);
for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
SSttLvl *lvl1 = TCONTAINER_OF(node, SSttLvl, rbtn);
SSttLvl *lvl2 = taosMemoryCalloc(1, sizeof(*lvl2));
if (lvl2 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
stt_lvl_init(lvl2, lvl1->level);
add_stt_lvl(fset2, lvl2);
SRBTreeIter iter2 = tRBTreeIterCreate(&lvl1->sttTree, 1);
for (SRBTreeNode *node2 = tRBTreeIterNext(&iter2); node2; node2 = tRBTreeIterNext(&iter2)) {
STFileObj *fobj1 = TCONTAINER_OF(node2, STFileObj, rbtn);
STFileObj *fobj2;
code = tsdbTFileObjCreate(&fobj2);
if (code) return code;
fobj2->f = fobj1->f;
add_file_to_stt_lvl(lvl2, fobj2);
const SSttLvl *lvl1;
TARRAY2_FOREACH(&fset1->lvlArr, lvl1) {
SSttLvl *lvl;
// code = stt_lvl_init_ex(lvl1, &lvl);
if (code) {
tsdbFileSetClear(fset);
return code;
}
}
// SRBTreeIter iter = tRBTreeIterCreate(&fset1->lvlTree, 1);
// for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
// SSttLvl *lvl1 = TCONTAINER_OF(node, SSttLvl, rbtn);
// SSttLvl *lvl2 = taosMemoryCalloc(1, sizeof(*lvl2));
// if (lvl2 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
// stt_lvl_init(lvl2, lvl1->level);
// add_stt_lvl(fset2, lvl2);
// SRBTreeIter iter2 = tRBTreeIterCreate(&lvl1->sttTree, 1);
// for (SRBTreeNode *node2 = tRBTreeIterNext(&iter2); node2; node2 = tRBTreeIterNext(&iter2)) {
// STFileObj *fobj1 = TCONTAINER_OF(node2, STFileObj, rbtn);
// STFileObj *fobj2;
// code = tsdbTFileObjCreate(&fobj2);
// if (code) return code;
// fobj2->f = fobj1->f;
// add_file_to_stt_lvl(lvl2, fobj2);
// }
// }
return 0;
}
int32_t tsdbFileSetClear(STFileSet *pSet) {
// TODO
int32_t tsdbFileSetClear(STFileSet **fset) {
if (fset[0]) {
for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
// if (fset[0]->farr[ftype]) {
// tsdbTFileObjDestroy(&fset[0]->farr[ftype]);
// fset[0]->farr[ftype] = NULL;
// }
}
// TODO
// SSttLvl *lvl;
// TARRAY2_FOREACH(&fset[0]->lvlArr, lvl) {
// // stt_lvl_clear(&lvl);
// }
taosMemoryFree(fset[0]);
fset[0] = NULL;
}
return 0;
}
const SSttLvl *tsdbFileSetGetLvl(const STFileSet *fset, int32_t level) {
SSttLvl tlvl = {.level = level};
SRBTreeNode *node = tRBTreeGet(&fset->lvlTree, &tlvl.rbtn);
return node ? TCONTAINER_OF(node, SSttLvl, rbtn) : NULL;
// SSttLvl tlvl = {.level = level};
// SRBTreeNode *node = tRBTreeGet(&fset->lvlTree, &tlvl.rbtn);
// return node ? TCONTAINER_OF(node, SSttLvl, rbtn) : NULL;
// TODO
return NULL;
}
\ No newline at end of file
......@@ -232,7 +232,7 @@ int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f) {
return 0;
}
int32_t tsdbTFileObjCreate(STFileObj **fobj) {
int32_t tsdbTFileObjCreate(const STFile *f, STFileObj **fobj) {
fobj[0] = taosMemoryMalloc(sizeof(STFileObj));
if (!fobj[0]) return TSDB_CODE_OUT_OF_MEMORY;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册