From 4568c672d643e5ff72fed338d91c9d9d5bac3456 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 17 May 2022 06:40:37 +0000 Subject: [PATCH] feat: tdata --- include/common/tdataformat.h | 43 ++++++-------------- include/util/tencode.h | 2 +- source/common/src/tdataformat.c | 69 ++++++++++++++++----------------- 3 files changed, 46 insertions(+), 68 deletions(-) diff --git a/include/common/tdataformat.h b/include/common/tdataformat.h index 1097f62798..bf2ddc0920 100644 --- a/include/common/tdataformat.h +++ b/include/common/tdataformat.h @@ -38,30 +38,11 @@ int32_t tTSchemaCreate(int32_t sver, SSchema *pSchema, int32_t nCols, STSchema * void tTSchemaDestroy(STSchema *pTSchema); // SColVal -#define COL_VAL_SET_NONE(CV) \ - do { \ - (CV)->type = COL_NONE; \ - (CV)->nData = 0; \ - (CV)->pData = NULL; \ - } while (0) - -#define COL_VAL_SET_NULL(CV) \ - do { \ - (CV)->type = COL_NULL; \ - (CV)->nData = 0; \ - (CV)->pData = NULL; \ - } while (0) - -#define COL_VAL_SET_VAL(CV, PDATA, NDATA) \ - do { \ - (CV)->type = COL_VAL; \ - (CV)->nData = (NDATA); \ - (CV)->pData = (PDATA); \ - } while (0) +#define ColValNONE ((SColVal){.type = COL_VAL_NONE, .nData = 0, .pData = NULL}) +#define ColValNULL ((SColVal){.type = COL_VAL_NULL, .nData = 0, .pData = NULL}) +#define ColValDATA(nData, pData) ((SColVal){.type = COL_VAL_DATA, .nData = (nData), .pData = (pData)}) // STSRow2 -int32_t tEncodeTSRow(SEncoder *pEncoder, const STSRow2 *pRow); -int32_t tDecodeTSRow(SDecoder *pDecoder, STSRow2 *pRow); int32_t tPutTSRow(uint8_t *p, STSRow2 *pRow); int32_t tGetTSRow(uint8_t *p, STSRow2 *pRow); int32_t tTSRowGet(const STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal); @@ -96,11 +77,11 @@ struct STSchema { #define TSROW_HAS_VAL ((uint8_t)0x4U) #define TSROW_KV_ROW ((uint8_t)0x10U) struct STSRow2 { - TSKEY ts; - uint8_t flags; - int32_t sver; - uint32_t nData; - const uint8_t *pData; + TSKEY ts; + uint8_t flags; + int32_t sver; + uint32_t nData; + uint8_t *pData; }; struct STSRowBuilder { @@ -117,11 +98,11 @@ struct STSRowBuilder { STSRow2 row; }; -typedef enum { COL_VAL = 0, COL_NONE = 1, COL_NULL = 2 } EColValT; +typedef enum { COL_VAL_NONE = 0, COL_VAL_NULL = 1, COL_VAL_Data = 2 } EColValT; struct SColVal { - EColValT type; - uint32_t nData; - const uint8_t *pData; + EColValT type; + uint32_t nData; + uint8_t *pData; }; #if 1 //==================================== diff --git a/include/util/tencode.h b/include/util/tencode.h index 484c9443f7..938e3018a8 100644 --- a/include/util/tencode.h +++ b/include/util/tencode.h @@ -630,7 +630,7 @@ static FORCE_INLINE int32_t tPutBinary(uint8_t* p, uint8_t* pData, uint32_t nDat return n; } -static FORCE_INLINE int32_t tGetBinary(uint8_t* p, const uint8_t** ppData, uint32_t* nData) { +static FORCE_INLINE int32_t tGetBinary(uint8_t* p, uint8_t** ppData, uint32_t* nData) { int32_t n = 0; uint32_t nt; diff --git a/source/common/src/tdataformat.c b/source/common/src/tdataformat.c index a37d8931ad..fbc2c2306d 100644 --- a/source/common/src/tdataformat.c +++ b/source/common/src/tdataformat.c @@ -38,49 +38,46 @@ typedef struct { #define GET_BIT2(p, i) (((p)[(i) / 4] >> ((i) % 4)) & ((uint8_t)3)) // STSRow2 -int32_t tEncodeTSRow(SEncoder *pEncoder, const STSRow2 *pRow) { - if (tEncodeI64(pEncoder, pRow->ts) < 0) return -1; - if (tEncodeU8(pEncoder, pRow->flags) < 0) return -1; - if (tEncodeI32v(pEncoder, pRow->sver) < 0) return -1; +int32_t tPutTSRow(uint8_t *p, STSRow2 *pRow) { + int32_t n = 0; - ASSERT(pRow->flags & 0xf != 0); + n += tPutI64(p ? p + n : p, pRow->ts); + n += tPutI8(p ? p + n : p, pRow->flags); + n += tPutI32v(p ? p + n : p, pRow->sver); + + ASSERT(pRow->flags & 0xf); switch (pRow->flags & 0xf) { case TSROW_HAS_NONE: case TSROW_HAS_NULL: - return 0; - case TSROW_HAS_VAL: - ASSERT(!TSROW_IS_KV_ROW(pRow)); + break; default: - ASSERT(pRow->nData && pRow->pData); - if (tEncodeBinary(pEncoder, pRow->pData, pRow->nData)) return -1; + n += tPutBinary(p ? p + n : p, pRow->pData, pRow->nData); break; } - return 0; + return n; } -int32_t tDecodeTSRow(SDecoder *pDecoder, STSRow2 *pRow) { - if (tDecodeI64(pDecoder, &pRow->ts) < 0) return -1; - if (tDecodeU8(pDecoder, &pRow->flags) < 0) return -1; - if (tDecodeI32v(pDecoder, &pRow->sver) < 0) return -1; +int32_t tGetTSRow(uint8_t *p, STSRow2 *pRow) { + int32_t n = 0; + uint8_t flags; - ASSERT(pRow->flags & 0xf != 0); + n += tGetI64(p + n, pRow ? &pRow->ts : NULL); + n += tGetI8(p + n, pRow ? &pRow->flags : &flags); + n += tGetI32v(p + n, pRow ? &pRow->sver : NULL); - switch (pRow->flags & 0xf) { + if (pRow) flags = pRow->flags; + switch (flags & 0xf) { case TSROW_HAS_NONE: case TSROW_HAS_NULL: - pRow->nData = 0; - pRow->pData = NULL; - return 0; - case TSROW_HAS_VAL: - ASSERT(!TSROW_IS_KV_ROW(pRow)); + break; default: - if (tDecodeBinary(pDecoder, &pRow->pData, &pRow->nData)) return -1; + n += tGetBinary(p + n, pRow ? &pRow->pData : NULL, pRow ? &pRow->nData : NULL); break; } - return 0; + return n; } static FORCE_INLINE int kvRowCmprFn(const void *p1, const void *p2) { @@ -109,10 +106,10 @@ int32_t tTSRowGet(const STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal ASSERT(pRow->flags & 0xf != 0); switch (pRow->flags & 0xf) { case TSROW_HAS_NONE: - COL_VAL_SET_NONE(pColVal); + // COL_VAL_SET_NONE(pColVal); return 0; case TSROW_HAS_NULL: - COL_VAL_SET_NULL(pColVal); + // COL_VAL_SET_NULL(pColVal); return 0; } @@ -122,13 +119,13 @@ int32_t tTSRowGet(const STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal pTSKVRow = (STSKVRow *)pRow->pData; pKVIdx = bsearch(&pTColumn->colId, pTSKVRow->idx, pTSKVRow->nCols, sizeof(SKVIdx), kvRowCmprFn); if (pKVIdx == NULL) { - COL_VAL_SET_NONE(pColVal); + // COL_VAL_SET_NONE(pColVal); } else if (pKVIdx->offset < 0) { - COL_VAL_SET_NULL(pColVal); + // COL_VAL_SET_NULL(pColVal); } else { p = pRow->pData + sizeof(STSKVRow) + sizeof(SKVIdx) * pTSKVRow->nCols + pKVIdx->offset; // tGetBinary(p, &p, &n); (todo) - COL_VAL_SET_VAL(pColVal, p, n); + // COL_VAL_SET_VAL(pColVal, p, n); } } else { // get bitmap @@ -137,9 +134,9 @@ int32_t tTSRowGet(const STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal case TSROW_HAS_NULL | TSROW_HAS_NONE: v = GET_BIT1(p, bidx); if (v == 0) { - COL_VAL_SET_NONE(pColVal); + // COL_VAL_SET_NONE(pColVal); } else { - COL_VAL_SET_NULL(pColVal); + // COL_VAL_SET_NULL(pColVal); } return 0; case TSROW_HAS_VAL | TSROW_HAS_NONE: @@ -148,7 +145,7 @@ int32_t tTSRowGet(const STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal p = p + (pTSchema->numOfCols - 2) / 8 + 1; break; } else { - COL_VAL_SET_NONE(pColVal); + // COL_VAL_SET_NONE(pColVal); return 0; } case TSROW_HAS_VAL | TSROW_HAS_NULL: @@ -157,16 +154,16 @@ int32_t tTSRowGet(const STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal p = p + (pTSchema->numOfCols - 2) / 8 + 1; break; } else { - COL_VAL_SET_NULL(pColVal); + // COL_VAL_SET_NULL(pColVal); return 0; } case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE: v = GET_BIT2(p, bidx); if (v == 0) { - COL_VAL_SET_NONE(pColVal); + // COL_VAL_SET_NONE(pColVal); return 0; } else if (v == 1) { - COL_VAL_SET_NULL(pColVal); + // COL_VAL_SET_NULL(pColVal); return 0; } else if (v == 2) { p = p + (pTSchema->numOfCols - 2) / 4 + 1; @@ -185,7 +182,7 @@ int32_t tTSRowGet(const STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal } else { n = pTColumn->bytes; } - COL_VAL_SET_VAL(pColVal, p, n); + // COL_VAL_SET_VAL(pColVal, p, n); } return 0;