diff --git a/src/query/inc/qExecutor.h b/src/query/inc/qExecutor.h index 5d570821cb6608fb382398e146766eb1f8aff284..33b724e4348a9c50997ab3c61f7cb5607684c82a 100644 --- a/src/query/inc/qExecutor.h +++ b/src/query/inc/qExecutor.h @@ -185,11 +185,18 @@ enum { QUERY_RESULT_READY = 2, }; +typedef struct SMemRef { + int32_t ref; + void *mem; + void *imem; +} SMemRef; + typedef struct SQInfo { void* signature; int32_t code; // error code to returned to client int64_t owner; // if it is in execution void* tsdb; + SMemRef memRef; int32_t vgId; STableGroupInfo tableGroupInfo; // table list SArray STableGroupInfo tableqinfoGroupInfo; // this is a group array list, including SArray structure diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index b8ccbd903c52563ac465f85a95335aa4a3d6c77c..ac6c2e0c5a9d5590e7dd57863dd18d8726912de5 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -20,6 +20,7 @@ #include "exception.h" #include "../../query/inc/qAst.h" // todo move to common module +#include "../../query/inc/qExecutor.h" // todo move to common module #include "tlosertree.h" #include "tsdb.h" #include "tsdbMain.h" @@ -143,6 +144,7 @@ static int tsdbReadRowsFromCache(STableCheckInfo* pCheckInfo, TSKEY maxKey, STsdbQueryHandle* pQueryHandle); static int tsdbCheckInfoCompar(const void* key1, const void* key2); + static void tsdbInitDataBlockLoadInfo(SDataBlockLoadInfo* pBlockLoadInfo) { pBlockLoadInfo->slot = -1; pBlockLoadInfo->tid = -1; @@ -182,6 +184,27 @@ static SArray* getDefaultLoadColumns(STsdbQueryHandle* pQueryHandle, bool loadTS return pLocalIdList; } +static void tsdbMayTakeMemSnapshot(TsdbQueryHandleT pHandle) { + STsdbQueryHandle* pSecQueryHandle = (STsdbQueryHandle*) pHandle; + SQInfo *pQInfo = (SQInfo *)(pSecQueryHandle->qinfo); + + if (pQInfo->memRef.ref++ == 0) { + tsdbTakeMemSnapshot(pSecQueryHandle->pTsdb, &pSecQueryHandle->mem, &pSecQueryHandle->imem); + pQInfo->memRef.mem = pSecQueryHandle->mem; + pQInfo->memRef.imem = pSecQueryHandle->imem; + } else { + pSecQueryHandle->mem = (SMemTable *)(pQInfo->memRef.mem); + pSecQueryHandle->imem = (SMemTable *)(pQInfo->memRef.imem); + } +} +static void tsdbMayUnTakeMemSnapshot(TsdbQueryHandleT pHandle) { + STsdbQueryHandle* pSecQueryHandle = (STsdbQueryHandle*) pHandle; + SQInfo *pQInfo = (SQInfo *)(pSecQueryHandle->qinfo); + + if (--pQInfo->memRef.ref == 0) { + tsdbUnTakeMemSnapShot(pSecQueryHandle->pTsdb, pSecQueryHandle->mem, pSecQueryHandle->imem); + } +} static SArray* createCheckInfoFromTableGroup(STsdbQueryHandle* pQueryHandle, STableGroupInfo* pGroupList, STsdbMeta* pMeta) { size_t sizeOfGroup = taosArrayGetSize(pGroupList->pGroupList); assert(sizeOfGroup >= 1 && pMeta != NULL); @@ -270,7 +293,7 @@ static STsdbQueryHandle* tsdbQueryTablesImpl(TSDB_REPO_T* tsdb, STsdbQueryCond* goto out_of_memory; } - tsdbTakeMemSnapshot(pQueryHandle->pTsdb, &pQueryHandle->mem, &pQueryHandle->imem); + tsdbMayTakeMemSnapshot(pQueryHandle); assert(pCond != NULL && pCond->numOfCols > 0); if (ASCENDING_TRAVERSE(pCond->order)) { @@ -2701,7 +2724,7 @@ void tsdbCleanupQueryHandle(TsdbQueryHandleT queryHandle) { taosTFree(pQueryHandle->statis); // todo check error - tsdbUnTakeMemSnapShot(pQueryHandle->pTsdb, pQueryHandle->mem, pQueryHandle->imem); + tsdbMayUnTakeMemSnapshot(pQueryHandle); tsdbDestroyHelper(&pQueryHandle->rhelper);