From e783609ebd018d42691a262e39d575d3025c73f5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 16 May 2023 09:36:32 +0800 Subject: [PATCH] more code --- include/util/trbtree.h | 6 +-- include/util/tutil.h | 2 + .../dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h | 2 +- source/dnode/vnode/src/tsdb/dev/tsdbFSet.c | 46 +++++++++---------- source/util/src/trbtree.c | 4 +- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/include/util/trbtree.h b/include/util/trbtree.h index e226419440..dedf9c811f 100644 --- a/include/util/trbtree.h +++ b/include/util/trbtree.h @@ -67,9 +67,9 @@ struct SRBTree { }; struct SRBTreeIter { - int8_t asc; - SRBTree *pTree; - SRBTreeNode *pNode; + int8_t asc; + const SRBTree *pTree; + SRBTreeNode *pNode; }; #ifdef __cplusplus diff --git a/include/util/tutil.h b/include/util/tutil.h index 9c21672c8f..8ca1f3db25 100644 --- a/include/util/tutil.h +++ b/include/util/tutil.h @@ -118,6 +118,8 @@ static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen, #define VND_CHECK_CODE(CODE, LINO, LABEL) TSDB_CHECK_CODE(CODE, LINO, LABEL) +#define TCONTAINER_OF(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member))) + #ifdef __cplusplus } #endif diff --git a/source/dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h b/source/dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h index f32691ac6d..5f434702ed 100644 --- a/source/dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h +++ b/source/dnode/vnode/src/tsdb/dev/inc/tsdbFSet.h @@ -60,7 +60,7 @@ typedef struct SSttLvl { struct STFileSet { int32_t fid; STFileObj *farr[TSDB_FTYPE_MAX]; // file array - SRBTree lvlTree; // level tree of .stt + SRBTree lvlTree; // SRBTree, level tree of .stt }; #ifdef __cplusplus diff --git a/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c b/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c index ac3d8fafc3..3be40ba1ae 100644 --- a/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c +++ b/source/dnode/vnode/src/tsdb/dev/tsdbFSet.c @@ -20,20 +20,24 @@ static int32_t stt_lvl_to_json(const SSttLvl *lvl, cJSON *json) { return TSDB_CODE_OUT_OF_MEMORY; } - cJSON *arr = cJSON_AddArrayToObject(json, "stt"); - if (arr == NULL) return TSDB_CODE_OUT_OF_MEMORY; + cJSON *ajson = cJSON_AddArrayToObject(json, "stt"); + if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY; - // TODO: .stt files - // STFile *f; - // LISTD_FOREACH(&lvl->fstt, f, listNode) { - // cJSON *item = cJSON_CreateObject(); - // if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY; + SRBTreeIter iter = tRBTreeIterCreate(&lvl->sttTree, 1); + for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) { + STFileObj *fobj = TCONTAINER_OF(node, STFileObj, rbtn); - // int32_t code = tsdbTFileToJson(f, item); - // if (code) return code; + cJSON *item = cJSON_CreateObject(); + if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY; - // cJSON_AddItemToArray(arr, item); - // } + int32_t code = tsdbTFileToJson(&fobj->f, item); + if (code) { + cJSON_Delete(item); + return code; + } + + cJSON_AddItemToArray(ajson, item); + } return 0; } @@ -69,24 +73,20 @@ int32_t tsdbFileSetToJson(const STFileSet *fset, cJSON *json) { continue; } - // code = tsdbTFileToJson(fset->farr[ftype], json); - // if (code) return code; + code = tsdbTFileToJson(&fset->farr[ftype]->f, json); + if (code) return code; } // each level cJSON *ajson = cJSON_AddArrayToObject(json, "stt"); if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY; - SSttLvl *sttLvl; - // LISTD_FOREACH(&fset->lvl0, sttLvl, listNode) { - // cJSON *ljson = cJSON_CreateObject(); - // if (ljson == NULL) return TSDB_CODE_OUT_OF_MEMORY; - - // code = stt_lvl_to_json(sttLvl, ljson); - // if (code) return code; - - // cJSON_AddItemToArray(ajson, ljson); - // } + SRBTreeIter iter = tRBTreeIterCreate(&fset->lvlTree, 1); + for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) { + SSttLvl *lvl = TCONTAINER_OF(node, SSttLvl, rbtn); + code = stt_lvl_to_json(lvl, ajson); + if (code) return code; + } return 0; } diff --git a/source/util/src/trbtree.c b/source/util/src/trbtree.c index e7386d5912..576cbf5633 100644 --- a/source/util/src/trbtree.c +++ b/source/util/src/trbtree.c @@ -105,7 +105,7 @@ static void tRBTreeTransplant(SRBTree *pTree, SRBTreeNode *u, SRBTreeNode *v) { v->parent = u->parent; } -static SRBTreeNode *tRBTreeSuccessor(SRBTree *pTree, SRBTreeNode *pNode) { +static SRBTreeNode *tRBTreeSuccessor(const SRBTree *pTree, SRBTreeNode *pNode) { if (pNode->right != pTree->NIL) { pNode = pNode->right; while (pNode->left != pTree->NIL) { @@ -125,7 +125,7 @@ static SRBTreeNode *tRBTreeSuccessor(SRBTree *pTree, SRBTreeNode *pNode) { return pNode; } -static SRBTreeNode *tRBTreePredecessor(SRBTree *pTree, SRBTreeNode *pNode) { +static SRBTreeNode *tRBTreePredecessor(const SRBTree *pTree, SRBTreeNode *pNode) { if (pNode->left != pTree->NIL) { pNode = pNode->left; while (pNode->right != pTree->NIL) {