From 3878af10c990148f4bf3ee1e9acc2647afeb1471 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 28 Nov 2022 16:13:18 +0800 Subject: [PATCH] refactor: do some internal refactor. --- include/util/talgo.h | 4 ++-- include/util/tarray.h | 8 +------ source/client/src/clientImpl.c | 8 +++---- source/dnode/mgmt/node_util/src/dmEps.c | 2 +- source/dnode/mnode/impl/src/mndDb.c | 2 +- source/dnode/mnode/impl/src/mndDef.c | 10 ++++----- source/dnode/mnode/impl/src/mndStb.c | 2 +- source/libs/catalog/src/ctgUtil.c | 10 ++++----- source/libs/executor/src/executorimpl.c | 4 +++- source/libs/monitor/src/monMain.c | 2 +- source/libs/parser/src/parCalcConst.c | 2 +- source/libs/parser/src/parInsertUtil.c | 2 +- source/libs/parser/src/parTranslater.c | 2 +- source/libs/parser/src/parUtil.c | 8 +++---- source/libs/scheduler/src/schJob.c | 2 +- source/util/src/tarray.c | 28 ++++++++++++------------- 16 files changed, 45 insertions(+), 51 deletions(-) diff --git a/include/util/talgo.h b/include/util/talgo.h index 2ffca029bf..f9d51c4b5b 100644 --- a/include/util/talgo.h +++ b/include/util/talgo.h @@ -27,7 +27,8 @@ extern "C" { typedef int32_t (*__compar_fn_t)(const void *, const void *); #endif -typedef void *(*FCopy)(void *); +typedef void *(*__array_item_dup_fn_t)(void *); + typedef void (*FDelete)(void *); typedef int32_t (*FEncode)(void **buf, const void *dst); typedef void *(*FDecode)(const void *buf, void *dst); @@ -41,7 +42,6 @@ typedef void *(*FDecode)(const void *buf, void *dst); #define elePtrAt(base, size, idx) (void *)((char *)(base) + (size) * (idx)) typedef int32_t (*__ext_compar_fn_t)(const void *p1, const void *p2, const void *param); -typedef void (*__ext_swap_fn_t)(void *p1, void *p2, const void *param); /** * quick sort, with the compare function requiring additional parameters support diff --git a/include/util/tarray.h b/include/util/tarray.h index 0632db3103..beebd70dea 100644 --- a/include/util/tarray.h +++ b/include/util/tarray.h @@ -205,13 +205,7 @@ SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize); * clone a new array * @param pSrc */ -SArray* taosArrayDup(const SArray* pSrc); - -/** - * deep copy a new array - * @param pSrc - */ -SArray* taosArrayDeepCopy(const SArray* pSrc, FCopy deepCopy); +SArray* taosArrayDup(const SArray* pSrc, __array_item_dup_fn_t fn); /** * clear the array (remove all element) diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index c3140371c4..7054fc1731 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -373,7 +373,7 @@ int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList) { } if (pNodeList) { - pInfo->pQnodeList = taosArrayDup(pNodeList); + pInfo->pQnodeList = taosArrayDup(pNodeList, NULL); taosArraySort(pInfo->pQnodeList, compareQueryNodeLoad); tscDebug("QnodeList updated in cluster 0x%" PRIx64 ", num:%ld", pInfo->clusterId, taosArrayGetSize(pInfo->pQnodeList)); @@ -404,7 +404,7 @@ int32_t getQnodeList(SRequestObj* pRequest, SArray** pNodeList) { taosThreadMutexLock(&pInfo->qnodeMutex); if (pInfo->pQnodeList) { - *pNodeList = taosArrayDup(pInfo->pQnodeList); + *pNodeList = taosArrayDup(pInfo->pQnodeList, NULL); } taosThreadMutexUnlock(&pInfo->qnodeMutex); @@ -593,13 +593,13 @@ int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray if (pRes->code) { pQnodeList = NULL; } else { - pQnodeList = taosArrayDup((SArray*)pRes->pRes); + pQnodeList = taosArrayDup((SArray*)pRes->pRes, NULL); } } else { SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo; taosThreadMutexLock(&pInst->qnodeMutex); if (pInst->pQnodeList) { - pQnodeList = taosArrayDup(pInst->pQnodeList); + pQnodeList = taosArrayDup(pInst->pQnodeList, NULL); } taosThreadMutexUnlock(&pInst->qnodeMutex); } diff --git a/source/dnode/mgmt/node_util/src/dmEps.c b/source/dnode/mgmt/node_util/src/dmEps.c index 4bc2e64fad..2ced9a350d 100644 --- a/source/dnode/mgmt/node_util/src/dmEps.c +++ b/source/dnode/mgmt/node_util/src/dmEps.c @@ -248,7 +248,7 @@ void dmUpdateEps(SDnodeData *pData, SArray *eps) { static void dmResetEps(SDnodeData *pData, SArray *dnodeEps) { if (pData->dnodeEps != dnodeEps) { SArray *tmp = pData->dnodeEps; - pData->dnodeEps = taosArrayDup(dnodeEps); + pData->dnodeEps = taosArrayDup(dnodeEps, NULL); taosArrayDestroy(tmp); } diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 78d84c9c25..23f06a88f4 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -810,7 +810,7 @@ static int32_t mndProcessAlterDbReq(SRpcMsg *pReq) { memcpy(&dbObj, pDb, sizeof(SDbObj)); if (dbObj.cfg.pRetensions != NULL) { - dbObj.cfg.pRetensions = taosArrayDup(pDb->cfg.pRetensions); + dbObj.cfg.pRetensions = taosArrayDup(pDb->cfg.pRetensions, NULL); if (dbObj.cfg.pRetensions == NULL) goto _OVER; } diff --git a/source/dnode/mnode/impl/src/mndDef.c b/source/dnode/mnode/impl/src/mndDef.c index 143131bac8..2e984212a1 100644 --- a/source/dnode/mnode/impl/src/mndDef.c +++ b/source/dnode/mnode/impl/src/mndDef.c @@ -361,7 +361,7 @@ SMqConsumerEp *tCloneSMqConsumerEp(const SMqConsumerEp *pConsumerEpOld) { SMqConsumerEp *pConsumerEpNew = taosMemoryMalloc(sizeof(SMqConsumerEp)); if (pConsumerEpNew == NULL) return NULL; pConsumerEpNew->consumerId = pConsumerEpOld->consumerId; - pConsumerEpNew->vgs = taosArrayDeepCopy(pConsumerEpOld->vgs, (FCopy)tCloneSMqVgEp); + pConsumerEpNew->vgs = taosArrayDup(pConsumerEpOld->vgs, (__array_item_dup_fn_t)tCloneSMqVgEp); return pConsumerEpNew; } @@ -440,11 +440,11 @@ SMqSubscribeObj *tCloneSubscribeObj(const SMqSubscribeObj *pSub) { pConsumerEp = (SMqConsumerEp *)pIter; SMqConsumerEp newEp = { .consumerId = pConsumerEp->consumerId, - .vgs = taosArrayDeepCopy(pConsumerEp->vgs, (FCopy)tCloneSMqVgEp), + .vgs = taosArrayDup(pConsumerEp->vgs, (__array_item_dup_fn_t)tCloneSMqVgEp), }; taosHashPut(pSubNew->consumerHash, &newEp.consumerId, sizeof(int64_t), &newEp, sizeof(SMqConsumerEp)); } - pSubNew->unassignedVgs = taosArrayDeepCopy(pSub->unassignedVgs, (FCopy)tCloneSMqVgEp); + pSubNew->unassignedVgs = taosArrayDup(pSub->unassignedVgs, (__array_item_dup_fn_t)tCloneSMqVgEp); memcpy(pSubNew->dbName, pSub->dbName, TSDB_DB_FNAME_LEN); return pSubNew; } @@ -516,7 +516,7 @@ SMqSubActionLogEntry *tCloneSMqSubActionLogEntry(SMqSubActionLogEntry *pEntry) { SMqSubActionLogEntry *pEntryNew = taosMemoryMalloc(sizeof(SMqSubActionLogEntry)); if (pEntryNew == NULL) return NULL; pEntryNew->epoch = pEntry->epoch; - pEntryNew->consumers = taosArrayDeepCopy(pEntry->consumers, (FCopy)tCloneSMqConsumerEp); + pEntryNew->consumers = taosArrayDup(pEntry->consumers, (__array_item_dup_fn_t)tCloneSMqConsumerEp); return pEntryNew; } @@ -541,7 +541,7 @@ SMqSubActionLogObj *tCloneSMqSubActionLogObj(SMqSubActionLogObj *pLog) { SMqSubActionLogObj *pLogNew = taosMemoryMalloc(sizeof(SMqSubActionLogObj)); if (pLogNew == NULL) return pLogNew; memcpy(pLogNew->key, pLog->key, TSDB_SUBSCRIBE_KEY_LEN); - pLogNew->logs = taosArrayDeepCopy(pLog->logs, (FCopy)tCloneSMqConsumerEp); + pLogNew->logs = taosArrayDup(pLog->logs, (__array_item_dup_fn_t)tCloneSMqConsumerEp); return pLogNew; } diff --git a/source/dnode/mnode/impl/src/mndStb.c b/source/dnode/mnode/impl/src/mndStb.c index 5e16397b14..6dad7d74c8 100644 --- a/source/dnode/mnode/impl/src/mndStb.c +++ b/source/dnode/mnode/impl/src/mndStb.c @@ -1684,7 +1684,7 @@ static int32_t mndBuildStbCfgImp(SDbObj *pDb, SStbObj *pStb, const char *tbName, } if (pStb->numOfFuncs > 0) { - pRsp->pFuncs = taosArrayDup(pStb->pFuncs); + pRsp->pFuncs = taosArrayDup(pStb->pFuncs, NULL); } taosRUnLockLatch(&pStb->lock); diff --git a/source/libs/catalog/src/ctgUtil.c b/source/libs/catalog/src/ctgUtil.c index 3d21bbbcd9..f795be3ac2 100644 --- a/source/libs/catalog/src/ctgUtil.c +++ b/source/libs/catalog/src/ctgUtil.c @@ -1188,7 +1188,7 @@ SName* ctgGetFetchName(SArray* pNames, SCtgFetch* pFetch) { return (SName*)taosArrayGet(pReq->pTables, pFetch->tbIdx); } -static void* ctgCloneDbVgroup(void* pSrc) { return taosArrayDup((const SArray*)pSrc); } +static void* ctgCloneDbVgroup(void* pSrc) { return taosArrayDup((const SArray*)pSrc, NULL); } static void ctgFreeDbVgroup(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); } @@ -1238,7 +1238,7 @@ static void* ctgCloneVgroupInfo(void* pSrc) { static void ctgFreeVgroupInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); } -static void* ctgCloneTableIndices(void* pSrc) { return taosArrayDup((const SArray*)pSrc); } +static void* ctgCloneTableIndices(void* pSrc) { return taosArrayDup((const SArray*)pSrc, NULL); } static void ctgFreeTableIndices(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); } @@ -1275,7 +1275,7 @@ static void* ctgCloneUserAuth(void* pSrc) { static void ctgFreeUserAuth(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); } -static void* ctgCloneQnodeList(void* pSrc) { return taosArrayDup((const SArray*)pSrc); } +static void* ctgCloneQnodeList(void* pSrc) { return taosArrayDup((const SArray*)pSrc, NULL); } static void ctgFreeQnodeList(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); } @@ -1290,11 +1290,11 @@ static void* ctgCloneTableCfg(void* pSrc) { static void ctgFreeTableCfg(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); } -static void* ctgCloneDnodeList(void* pSrc) { return taosArrayDup((const SArray*)pSrc); } +static void* ctgCloneDnodeList(void* pSrc) { return taosArrayDup((const SArray*)pSrc, NULL); } static void ctgFreeDnodeList(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); } -static int32_t ctgCloneMetaDataArray(SArray* pSrc, FCopy copyFunc, SArray** pDst) { +static int32_t ctgCloneMetaDataArray(SArray* pSrc, __array_item_dup_fn_t copyFunc, SArray** pDst) { if (NULL == pSrc) { return TSDB_CODE_SUCCESS; } diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 514236159d..300ba52934 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -2355,10 +2355,12 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo return NULL; } - for (int32_t i = 0; i < tableListGetSize(pTableListInfo); ++i) { + size_t num = taosArrayGetSize(pList); + for (int32_t i = 0; i < num; ++i) { STableKeyInfo* p = taosArrayGet(pList, i); tableListAddTableInfo(pTableListInfo, p->uid, 0); } + taosArrayDestroy(pList); } else { // Create group with only one table tableListAddTableInfo(pTableListInfo, pBlockNode->uid, 0); diff --git a/source/libs/monitor/src/monMain.c b/source/libs/monitor/src/monMain.c index 70792ee611..b3ca0fa452 100644 --- a/source/libs/monitor/src/monMain.c +++ b/source/libs/monitor/src/monMain.c @@ -36,7 +36,7 @@ void monRecordLog(int64_t ts, ELogLevel level, const char *content) { int32_t monGetLogs(SMonLogs *logs) { taosThreadMutexLock(&tsMonitor.lock); - logs->logs = taosArrayDup(tsMonitor.logs); + logs->logs = taosArrayDup(tsMonitor.logs, NULL); logs->numOfInfoLogs = tsNumOfInfoLogs; logs->numOfErrorLogs = tsNumOfErrorLogs; logs->numOfDebugLogs = tsNumOfDebugLogs; diff --git a/source/libs/parser/src/parCalcConst.c b/source/libs/parser/src/parCalcConst.c index efc9d77e67..6cee06ea0e 100644 --- a/source/libs/parser/src/parCalcConst.c +++ b/source/libs/parser/src/parCalcConst.c @@ -169,7 +169,7 @@ static int32_t calcConstStmtCondition(SCalcConstContext* pCxt, SNode** pCond, bo static int32_t calcConstProject(SNode* pProject, bool dual, SNode** pNew) { SArray* pAssociation = NULL; if (NULL != ((SExprNode*)pProject)->pAssociation) { - pAssociation = taosArrayDup(((SExprNode*)pProject)->pAssociation); + pAssociation = taosArrayDup(((SExprNode*)pProject)->pAssociation, NULL); if (NULL == pAssociation) { return TSDB_CODE_OUT_OF_MEMORY; } diff --git a/source/libs/parser/src/parInsertUtil.c b/source/libs/parser/src/parInsertUtil.c index 5f8120171f..730dc2eab9 100644 --- a/source/libs/parser/src/parInsertUtil.c +++ b/source/libs/parser/src/parInsertUtil.c @@ -857,7 +857,7 @@ void insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, i pTbReq->ctb.tagNum = tagNum; if (sname) pTbReq->ctb.stbName = strdup(sname); pTbReq->ctb.pTag = (uint8_t*)pTag; - pTbReq->ctb.tagName = taosArrayDup(tagName); + pTbReq->ctb.tagName = taosArrayDup(tagName, NULL); pTbReq->ttl = TSDB_DEFAULT_TABLE_TTL; pTbReq->commentLen = -1; diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 0743b40662..9632c097b7 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -6625,7 +6625,7 @@ static void addCreateTbReqIntoVgroup(int32_t acctId, SHashObj* pVgroupHashmap, S req.ctb.tagNum = tagNum; req.ctb.stbName = strdup(sTableNmae); req.ctb.pTag = (uint8_t*)pTag; - req.ctb.tagName = taosArrayDup(tagName); + req.ctb.tagName = taosArrayDup(tagName, NULL); if (pStmt->ignoreExists) { req.flags |= TD_CREATE_IF_NOT_EXISTS; } diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index e8c3f2fa8d..7a56b0e0fa 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -844,7 +844,7 @@ int32_t getDbVgInfoFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, int32_t code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbVgroup, (void**)&pVgList); // pVgList is null, which is a legal value, indicating that the user DB has not been created if (TSDB_CODE_SUCCESS == code && NULL != pVgList) { - *pVgInfo = taosArrayDup(pVgList); + *pVgInfo = taosArrayDup(pVgList, NULL); if (NULL == *pVgInfo) { code = TSDB_CODE_OUT_OF_MEMORY; } @@ -961,7 +961,7 @@ int32_t getUdfInfoFromCache(SParseMetaCache* pMetaCache, const char* pFunc, SFun static void destroySmaIndex(void* p) { taosMemoryFree(((STableIndexInfo*)p)->expr); } static SArray* smaIndexesDup(SArray* pSrc) { - SArray* pDst = taosArrayDup(pSrc); + SArray* pDst = taosArrayDup(pSrc, NULL); if (NULL == pDst) { return NULL; } @@ -1011,7 +1011,7 @@ STableCfg* tableCfgDup(STableCfg* pCfg) { memcpy(pNew->pComment, pCfg->pComment, pNew->commentLen); } if (NULL != pNew->pFuncs) { - pNew->pFuncs = taosArrayDup(pNew->pFuncs); + pNew->pFuncs = taosArrayDup(pNew->pFuncs, NULL); } if (NULL != pNew->pTags) { pNew->pTags = taosMemoryCalloc(pNew->tagsLen + 1, 1); @@ -1053,7 +1053,7 @@ int32_t getDnodeListFromCache(SParseMetaCache* pMetaCache, SArray** pDnodes) { return pRes->code; } - *pDnodes = taosArrayDup((SArray*)pRes->pRes); + *pDnodes = taosArrayDup((SArray*)pRes->pRes, NULL); if (NULL == *pDnodes) { return TSDB_CODE_OUT_OF_MEMORY; } diff --git a/source/libs/scheduler/src/schJob.c b/source/libs/scheduler/src/schJob.c index 8fcfdce2b2..0eb29a3667 100644 --- a/source/libs/scheduler/src/schJob.c +++ b/source/libs/scheduler/src/schJob.c @@ -721,7 +721,7 @@ int32_t schInitJob(int64_t *pJobId, SSchedulerReq *pReq) { if (pReq->pNodeList == NULL || taosArrayGetSize(pReq->pNodeList) <= 0) { qDebug("QID:0x%" PRIx64 " input exec nodeList is empty", pReq->pDag->queryId); } else { - pJob->nodeList = taosArrayDup(pReq->pNodeList); + pJob->nodeList = taosArrayDup(pReq->pNodeList, NULL); } pJob->taskList = taosHashInit(pReq->pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, diff --git a/source/util/src/tarray.c b/source/util/src/tarray.c index bcf9a77950..0c5b311ab3 100644 --- a/source/util/src/tarray.c +++ b/source/util/src/tarray.c @@ -302,7 +302,7 @@ SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize) { return pDst; } -SArray* taosArrayDup(const SArray* pSrc) { +SArray* taosArrayDup(const SArray* pSrc, __array_item_dup_fn_t fn) { assert(pSrc != NULL); if (pSrc->size == 0) { // empty array list @@ -311,8 +311,19 @@ SArray* taosArrayDup(const SArray* pSrc) { SArray* dst = taosArrayInit(pSrc->size, pSrc->elemSize); - memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size); + if (fn == NULL) { + memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size); + } else { + ASSERT(pSrc->elemSize == sizeof(void*)); + + for(int32_t i = 0; i < pSrc->size; ++i) { + void* p = fn(taosArrayGetP(pSrc, i)); + memcpy(dst->pData + i * dst->elemSize, &p, dst->elemSize); + } + } + dst->size = pSrc->size; + return dst; } @@ -464,19 +475,6 @@ static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void return; } -SArray* taosArrayDeepCopy(const SArray* pSrc, FCopy deepCopy) { - if (NULL == pSrc) { - return NULL; - } - ASSERT(pSrc->elemSize == sizeof(void*)); - SArray* pArray = taosArrayInit(pSrc->size, sizeof(void*)); - for (int32_t i = 0; i < pSrc->size; i++) { - void* clone = deepCopy(taosArrayGetP(pSrc, i)); - taosArrayPush(pArray, &clone); - } - return pArray; -} - int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode) { int32_t tlen = 0; int32_t sz = pArray->size; -- GitLab