From 7976d88402f9536bd6725a7345a452f5e40a9cba Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 22 Nov 2022 10:10:23 +0800 Subject: [PATCH] add interface --- include/util/tarray.h | 9 +++++++++ source/dnode/vnode/src/inc/tsdb.h | 2 ++ source/dnode/vnode/src/tsdb/tsdbUtil.c | 24 ++++++++++++++++++++++++ source/util/src/tarray.c | 11 +++++++++++ 4 files changed, 46 insertions(+) diff --git a/include/util/tarray.h b/include/util/tarray.h index e95568197b..0632db3103 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 c6e2842c32..dc0d35f390 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 b0cf09662c..47d4435f68 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 95065972a3..5703d8f8f4 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); -- GitLab