diff --git a/include/util/tarray.h b/include/util/tarray.h index e95568197b63b8b58e62ecf56783ed3f4dc231ee..0632db310326d826b8393fe873610ec1cf0d8159 100644 --- a/include/util/tarray.h +++ b/include/util/tarray.h @@ -104,6 +104,15 @@ static FORCE_INLINE void* taosArrayPush(SArray* pArray, const void* pData) { return taosArrayAddBatch(pArray, pData, 1); } +/** + * @brief reserve the capacity of the array + * + * @param pArray + * @param num + * @return void* the start position of the reserved memory + */ +void* taosArrayReserve(SArray* pArray, int32_t num); + /** * * @param pArray diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index d335e84c4c4c653e533b3f19b0a174f7eefa0f34..7fb962e3a7b300bde16b89af2dbb14c9e05dc6ad 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -185,6 +185,8 @@ int32_t tMapDataSearch(SMapData *pMapData, void *pSearchItem, int32_t (*tGetItem int32_t (*tItemCmprFn)(const void *, const void *), void *pItem); int32_t tPutMapData(uint8_t *p, SMapData *pMapData); int32_t tGetMapData(uint8_t *p, SMapData *pMapData); +int32_t tMapDataToArray(SMapData *pMapData, int32_t itemSize, int32_t (*tGetItemFn)(uint8_t *, void *), + SArray **ppArray); // other int32_t tsdbKeyFid(TSKEY key, int32_t minutes, int8_t precision); void tsdbFidKeyRange(int32_t fid, int32_t minutes, int8_t precision, TSKEY *minKey, TSKEY *maxKey); diff --git a/source/dnode/vnode/src/tsdb/tsdbUtil.c b/source/dnode/vnode/src/tsdb/tsdbUtil.c index 599428564788afeb1c962965f95c4fc35cde8a4a..db86a9429d255c5a441a81482f5c4d62aac6e338 100644 --- a/source/dnode/vnode/src/tsdb/tsdbUtil.c +++ b/source/dnode/vnode/src/tsdb/tsdbUtil.c @@ -101,6 +101,30 @@ void tMapDataGetItemByIdx(SMapData *pMapData, int32_t idx, void *pItem, int32_t tGetItemFn(pMapData->pData + pMapData->aOffset[idx], pItem); } +int32_t tMapDataToArray(SMapData *pMapData, int32_t itemSize, int32_t (*tGetItemFn)(uint8_t *, void *), + SArray **ppArray) { + int32_t code = 0; + + SArray *pArray = taosArrayInit(pMapData->nItem, itemSize); + if (pArray == NULL) { + code = TSDB_CODE_TDB_OUT_OF_MEMORY; + goto _exit; + } + + for (int32_t i = 0; i < pMapData->nItem; i++) { + tMapDataGetItemByIdx(pMapData, i, taosArrayReserve(pArray, 1), tGetItemFn); + } + +_exit: + if (code) { + *ppArray = NULL; + if (pArray) taosArrayDestroy(pArray); + } else { + *ppArray = pArray; + } + return code; +} + int32_t tPutMapData(uint8_t *p, SMapData *pMapData) { int32_t n = 0; diff --git a/source/util/src/tarray.c b/source/util/src/tarray.c index 95065972a3a890c253004033b1b33c5b76dd0467..5703d8f8f465486590de5f0337a97fa724741665 100644 --- a/source/util/src/tarray.c +++ b/source/util/src/tarray.c @@ -181,6 +181,17 @@ void* taosArrayAddAll(SArray* pArray, const SArray* pInput) { } } +void* taosArrayReserve(SArray* pArray, int32_t num) { + if (taosArrayEnsureCap(pArray, pArray->size + num) != 0) { + return NULL; + } + + void* dst = TARRAY_GET_ELEM(pArray, pArray->size); + pArray->size += num; + + return dst; +} + void* taosArrayPop(SArray* pArray) { assert(pArray != NULL);