From 492ff11f5a1bf61b4092c1c9236c259830af7515 Mon Sep 17 00:00:00 2001 From: hzcheng Date: Wed, 25 Mar 2020 22:49:54 +0800 Subject: [PATCH] TD-34 --- src/common/inc/dataformat.h | 20 +++++++++++------ src/common/src/dataformat.c | 45 ++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/common/inc/dataformat.h b/src/common/inc/dataformat.h index 207ff4dbfd..37a3ccea5a 100644 --- a/src/common/inc/dataformat.h +++ b/src/common/inc/dataformat.h @@ -105,24 +105,30 @@ SDataRow tdDataRowDup(SDataRow row); // ----------------- Data column structure typedef struct SDataCol { - int32_t len; - void * pData; + int8_t type; + int bytes; + int len; + void * pData; } SDataCol; typedef struct { - TSKEY firstKey; - TSKEY lastKey; + int maxRowSize; + int maxCols; // max number of columns + int maxPoints; // max number of points int numOfPoints; - int numOfCols; + int numOfCols; // Total number of cols void * buf; SDataCol cols[]; } SDataCols; #define keyCol(cols) (&((cols)->cols[0])) // Key column +#define dataColsKeyFirst(cols) ((int64_t *)(keyCol(cols)->pData))[0] +#define dataColsKeyLast(cols) ((int64_t *)(keyCol(cols)->pData))[(cols)->numOfPoints - 1] -SDataCols *tdNewDataCols(STSchema *pSchema, int nRows); -void tdFreeDataCols(SDataCols *pCols); +SDataCols *tdNewDataCols(int maxRowSize, int maxCols, int maxRows); void tdResetDataCols(SDataCols *pCols); +void tdInitDataCols(SDataCols *pCols, STSchema *pSchema); +void tdFreeDataCols(SDataCols *pCols); void tdAppendDataRowToDataCol(SDataRow row, SDataCols *pCols, STSchema *pSchema); #ifdef __cplusplus diff --git a/src/common/src/dataformat.c b/src/common/src/dataformat.c index 3c692f9eba..84ac1efd71 100644 --- a/src/common/src/dataformat.c +++ b/src/common/src/dataformat.c @@ -294,27 +294,34 @@ SDataRow tdDataRowDup(SDataRow row) { return trow; } -SDataCols *tdNewDataCols(STSchema *pSchema, int nRows) { - int nCols = schemaNCols(pSchema); - - SDataCols *pInfo = (SDataCols *)calloc(1, sizeof(SDataCols) + sizeof(SDataCol) * nCols); - if (pInfo == NULL) return NULL; - - pInfo->numOfCols = nCols; - pInfo->firstKey = INT64_MIN; - pInfo->lastKey = INT64_MAX; - pInfo->buf = malloc(tdMaxRowBytesFromSchema(pSchema) * nRows); - if (pInfo->buf == NULL) { - free(pInfo); +SDataCols *tdNewDataCols(int maxRowSize, int maxCols, int maxRows) { + SDataCols *pCols = (SDataCols *)calloc(1, sizeof(SDataCols) + sizeof(SDataCol) * maxCols); + if (pCols == NULL) return NULL; + + pCols->maxRowSize = maxRowSize; + pCols->maxCols = maxCols; + pCols->maxPoints = maxRows; + + pCols->buf = malloc(maxRowSize * maxRows); + if (pCols->buf == NULL) { + free(pCols); return NULL; } - pInfo->cols[0].pData = pInfo->buf; - for (int i = 1; i < nCols; i++) { - pInfo->cols[i].pData = (char *)(pInfo->cols[i - 1].pData) + schemaColAt(pSchema, i - 1)->bytes * nRows; + return pCols; +} + +void tdInitDataCols(SDataCols *pCols, STSchema *pSchema) { + // assert(schemaNCols(pSchema) <= pCols->numOfCols); + tdResetDataCols(pCols); + pCols->numOfCols = schemaNCols(pSchema); + + pCols->cols[0].pData = pCols->buf; + for (int i = 1; i < schemaNCols(pSchema); i++) { + pCols->cols[i].pData = (char *)(pCols->cols[i - 1].pData) + schemaColAt(pSchema, i - 1)->bytes * pCols->maxPoints; } - return pInfo; + return pCols; } void tdFreeDataCols(SDataCols *pCols) { @@ -325,18 +332,14 @@ void tdFreeDataCols(SDataCols *pCols) { } void tdResetDataCols(SDataCols *pCols) { - pCols->firstKey = INT64_MAX; - pCols->lastKey = INT64_MIN; pCols->numOfPoints = 0; - for (int i = 0; i < pCols->numOfCols; i++) { + for (int i = 0; i < pCols->maxCols; i++) { pCols->cols[i].len = 0; } } void tdAppendDataRowToDataCol(SDataRow row, SDataCols *pCols, STSchema *pSchema) { TSKEY key = dataRowKey(row); - if (pCols->numOfPoints == 0) pCols->firstKey = key; - pCols->lastKey = key; for (int i = 0; i < pCols->numOfCols; i++) { SDataCol *pCol = pCols->cols + i; memcpy((void *)((char *)(pCol->pData) + pCol->len), dataRowAt(row, colOffset(schemaColAt(pSchema, i))), -- GitLab