提交 f8619dfb 编写于 作者: H Hongze Cheng

refact code for further refact

上级 1d76bd6d
......@@ -154,17 +154,7 @@ struct STSRowBuilder {
struct SValue {
union {
int8_t i8; // TSDB_DATA_TYPE_BOOL||TSDB_DATA_TYPE_TINYINT
uint8_t u8; // TSDB_DATA_TYPE_UTINYINT
int16_t i16; // TSDB_DATA_TYPE_SMALLINT
uint16_t u16; // TSDB_DATA_TYPE_USMALLINT
int32_t i32; // TSDB_DATA_TYPE_INT
uint32_t u32; // TSDB_DATA_TYPE_UINT
int64_t i64; // TSDB_DATA_TYPE_BIGINT
uint64_t u64; // TSDB_DATA_TYPE_UBIGINT
TSKEY ts; // TSDB_DATA_TYPE_TIMESTAMP
float f; // TSDB_DATA_TYPE_FLOAT
double d; // TSDB_DATA_TYPE_DOUBLE
int64_t val;
struct {
uint32_t nData;
uint8_t *pData;
......
......@@ -33,105 +33,21 @@ typedef struct {
// SValue
int32_t tPutValue(uint8_t *p, SValue *pValue, int8_t type) {
int32_t n = 0;
if (IS_VAR_DATA_TYPE(type)) {
n += tPutBinary(p ? p + n : p, pValue->pData, pValue->nData);
return tPutBinary(p, pValue->pData, pValue->nData);
} else {
switch (type) {
case TSDB_DATA_TYPE_BOOL:
n += tPutI8(p ? p + n : p, pValue->i8 ? 1 : 0);
break;
case TSDB_DATA_TYPE_TINYINT:
n += tPutI8(p ? p + n : p, pValue->i8);
break;
case TSDB_DATA_TYPE_SMALLINT:
n += tPutI16(p ? p + n : p, pValue->i16);
break;
case TSDB_DATA_TYPE_INT:
n += tPutI32(p ? p + n : p, pValue->i32);
break;
case TSDB_DATA_TYPE_BIGINT:
n += tPutI64(p ? p + n : p, pValue->i64);
break;
case TSDB_DATA_TYPE_FLOAT:
n += tPutFloat(p ? p + n : p, pValue->f);
break;
case TSDB_DATA_TYPE_DOUBLE:
n += tPutDouble(p ? p + n : p, pValue->d);
break;
case TSDB_DATA_TYPE_TIMESTAMP:
n += tPutI64(p ? p + n : p, pValue->ts);
break;
case TSDB_DATA_TYPE_UTINYINT:
n += tPutU8(p ? p + n : p, pValue->u8);
break;
case TSDB_DATA_TYPE_USMALLINT:
n += tPutU16(p ? p + n : p, pValue->u16);
break;
case TSDB_DATA_TYPE_UINT:
n += tPutU32(p ? p + n : p, pValue->u32);
break;
case TSDB_DATA_TYPE_UBIGINT:
n += tPutU64(p ? p + n : p, pValue->u64);
break;
default:
ASSERT(0);
}
if (p) memcpy(p, &pValue->val, tDataTypes[type].bytes);
return tDataTypes[type].bytes;
}
return n;
}
int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type) {
int32_t n = 0;
if (IS_VAR_DATA_TYPE(type)) {
n += tGetBinary(p, &pValue->pData, pValue ? &pValue->nData : NULL);
return tGetBinary(p, &pValue->pData, pValue ? &pValue->nData : NULL);
} else {
switch (type) {
case TSDB_DATA_TYPE_BOOL:
n += tGetI8(p, &pValue->i8);
break;
case TSDB_DATA_TYPE_TINYINT:
n += tGetI8(p, &pValue->i8);
break;
case TSDB_DATA_TYPE_SMALLINT:
n += tGetI16(p, &pValue->i16);
break;
case TSDB_DATA_TYPE_INT:
n += tGetI32(p, &pValue->i32);
break;
case TSDB_DATA_TYPE_BIGINT:
n += tGetI64(p, &pValue->i64);
break;
case TSDB_DATA_TYPE_FLOAT:
n += tGetFloat(p, &pValue->f);
break;
case TSDB_DATA_TYPE_DOUBLE:
n += tGetDouble(p, &pValue->d);
break;
case TSDB_DATA_TYPE_TIMESTAMP:
n += tGetI64(p, &pValue->ts);
break;
case TSDB_DATA_TYPE_UTINYINT:
n += tGetU8(p, &pValue->u8);
break;
case TSDB_DATA_TYPE_USMALLINT:
n += tGetU16(p, &pValue->u16);
break;
case TSDB_DATA_TYPE_UINT:
n += tGetU32(p, &pValue->u32);
break;
case TSDB_DATA_TYPE_UBIGINT:
n += tGetU64(p, &pValue->u64);
break;
default:
ASSERT(0);
}
memcpy(&pValue->val, p, tDataTypes[type].bytes);
return tDataTypes[type].bytes;
}
return n;
}
int tValueCmprFn(const SValue *pValue1, const SValue *pValue2, int8_t type) {
......
......@@ -689,7 +689,7 @@ int32_t tdSTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow **ppRow) {
memcpy(varDataVal(varBuf), pColVal->value.pData, pColVal->value.nData);
val = varBuf;
} else {
val = (const void *)&pColVal->value.i64;
val = (const void *)&pColVal->value.val;
}
} else {
pColVal = NULL;
......
......@@ -260,7 +260,7 @@ int32_t tsdbCacheInsertLast(SLRUCache *pCache, tb_uid_t uid, STSRow *row, STsdb
SLastCol *tTsVal = (SLastCol *)taosArrayGet(pLast, iCol);
if (keyTs > tTsVal->ts) {
STColumn *pTColumn = &pTSchema->columns[0];
SColVal tColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = keyTs});
SColVal tColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = keyTs});
taosArraySet(pLast, iCol, &(SLastCol){.ts = keyTs, .colVal = tColVal});
}
......@@ -1048,7 +1048,7 @@ static int32_t mergeLastRow(tb_uid_t uid, STsdb *pTsdb, bool *dup, STSRow **ppRo
lastRowTs = TSDBROW_TS(pRow);
STColumn *pTColumn = &pTSchema->columns[0];
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = lastRowTs});
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = lastRowTs});
if (taosArrayPush(pColArray, pColVal) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
......@@ -1147,7 +1147,7 @@ static int32_t mergeLast(tb_uid_t uid, STsdb *pTsdb, SArray **ppLastArray) {
lastRowTs = rowTs;
STColumn *pTColumn = &pTSchema->columns[0];
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = lastRowTs});
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = lastRowTs});
if (taosArrayPush(pColArray, &(SLastCol){.ts = lastRowTs, .colVal = *pColVal}) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
......
......@@ -649,7 +649,7 @@ int32_t tRowMergerInit2(SRowMerger *pMerger, STSchema *pResTSchema, TSDBROW *pRo
ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = key.ts});
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = key.ts});
if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
......@@ -690,7 +690,7 @@ int32_t tRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
STColumn *pTColumn;
int32_t iCol, jCol = 1;
ASSERT(((SColVal *)pMerger->pArray->pData)->value.ts == key.ts);
ASSERT(((SColVal *)pMerger->pArray->pData)->value.val == key.ts);
for (iCol = 1; iCol < pMerger->pTSchema->numOfCols && jCol < pTSchema->numOfCols; ++iCol) {
pTColumn = &pMerger->pTSchema->columns[iCol];
......@@ -744,7 +744,7 @@ int32_t tRowMergerInit(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = key.ts});
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = key.ts});
if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
......@@ -770,7 +770,7 @@ int32_t tRowMerge(SRowMerger *pMerger, TSDBROW *pRow) {
TSDBKEY key = TSDBROW_KEY(pRow);
SColVal *pColVal = &(SColVal){0};
ASSERT(((SColVal *)pMerger->pArray->pData)->value.ts == key.ts);
ASSERT(((SColVal *)pMerger->pArray->pData)->value.val == key.ts);
for (int32_t iCol = 1; iCol < pMerger->pTSchema->numOfCols; iCol++) {
tsdbRowGetColVal(pRow, pMerger->pTSchema, iCol, pColVal);
......@@ -1513,73 +1513,79 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
case TSDB_DATA_TYPE_BOOL:
break;
case TSDB_DATA_TYPE_TINYINT: {
pColAgg->sum += colVal.value.i8;
if (!minAssigned || pColAgg->min > colVal.value.i8) {
pColAgg->min = colVal.value.i8;
int8_t i8 = *(int8_t *)&colVal.value.val;
pColAgg->sum += i8;
if (!minAssigned || pColAgg->min > i8) {
pColAgg->min = i8;
minAssigned = true;
}
if (!maxAssigned || pColAgg->max < colVal.value.i8) {
pColAgg->max = colVal.value.i8;
if (!maxAssigned || pColAgg->max < i8) {
pColAgg->max = i8;
maxAssigned = true;
}
break;
}
case TSDB_DATA_TYPE_SMALLINT: {
pColAgg->sum += colVal.value.i16;
if (!minAssigned || pColAgg->min > colVal.value.i16) {
pColAgg->min = colVal.value.i16;
int16_t i16 = *(int16_t *)&colVal.value.val;
pColAgg->sum += i16;
if (!minAssigned || pColAgg->min > i16) {
pColAgg->min = i16;
minAssigned = true;
}
if (!maxAssigned || pColAgg->max < colVal.value.i16) {
pColAgg->max = colVal.value.i16;
if (!maxAssigned || pColAgg->max < i16) {
pColAgg->max = i16;
maxAssigned = true;
}
break;
}
case TSDB_DATA_TYPE_INT: {
pColAgg->sum += colVal.value.i32;
if (!minAssigned || pColAgg->min > colVal.value.i32) {
pColAgg->min = colVal.value.i32;
int32_t i32 = *(int32_t *)&colVal.value.val;
pColAgg->sum += i32;
if (!minAssigned || pColAgg->min > i32) {
pColAgg->min = i32;
minAssigned = true;
}
if (!maxAssigned || pColAgg->max < colVal.value.i32) {
pColAgg->max = colVal.value.i32;
if (!maxAssigned || pColAgg->max < i32) {
pColAgg->max = i32;
maxAssigned = true;
}
break;
}
case TSDB_DATA_TYPE_BIGINT: {
pColAgg->sum += colVal.value.i64;
if (!minAssigned || pColAgg->min > colVal.value.i64) {
pColAgg->min = colVal.value.i64;
int64_t i64 = *(int64_t *)&colVal.value.val;
pColAgg->sum += i64;
if (!minAssigned || pColAgg->min > i64) {
pColAgg->min = i64;
minAssigned = true;
}
if (!maxAssigned || pColAgg->max < colVal.value.i64) {
pColAgg->max = colVal.value.i64;
if (!maxAssigned || pColAgg->max < i64) {
pColAgg->max = i64;
maxAssigned = true;
}
break;
}
case TSDB_DATA_TYPE_FLOAT: {
*(double *)(&pColAgg->sum) += colVal.value.f;
if (!minAssigned || *(double *)(&pColAgg->min) > colVal.value.f) {
*(double *)(&pColAgg->min) = colVal.value.f;
float f = *(float *)&colVal.value.val;
*(double *)(&pColAgg->sum) += f;
if (!minAssigned || *(double *)(&pColAgg->min) > f) {
*(double *)(&pColAgg->min) = f;
minAssigned = true;
}
if (!maxAssigned || *(double *)(&pColAgg->max) < colVal.value.f) {
*(double *)(&pColAgg->max) = colVal.value.f;
if (!maxAssigned || *(double *)(&pColAgg->max) < f) {
*(double *)(&pColAgg->max) = f;
maxAssigned = true;
}
break;
}
case TSDB_DATA_TYPE_DOUBLE: {
*(double *)(&pColAgg->sum) += colVal.value.d;
if (!minAssigned || *(double *)(&pColAgg->min) > colVal.value.d) {
*(double *)(&pColAgg->min) = colVal.value.d;
double d = *(double *)&colVal.value.val;
*(double *)(&pColAgg->sum) += d;
if (!minAssigned || *(double *)(&pColAgg->min) > d) {
*(double *)(&pColAgg->min) = d;
minAssigned = true;
}
if (!maxAssigned || *(double *)(&pColAgg->max) < colVal.value.d) {
*(double *)(&pColAgg->max) = colVal.value.d;
if (!maxAssigned || *(double *)(&pColAgg->max) < d) {
*(double *)(&pColAgg->max) = d;
maxAssigned = true;
}
break;
......@@ -1587,12 +1593,13 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
case TSDB_DATA_TYPE_VARCHAR:
break;
case TSDB_DATA_TYPE_TIMESTAMP: {
if (!minAssigned || pColAgg->min > colVal.value.i64) {
pColAgg->min = colVal.value.i64;
int64_t ts = *(int64_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > ts) {
pColAgg->min = ts;
minAssigned = true;
}
if (!maxAssigned || pColAgg->max < colVal.value.i64) {
pColAgg->max = colVal.value.i64;
if (!maxAssigned || pColAgg->max < ts) {
pColAgg->max = ts;
maxAssigned = true;
}
break;
......@@ -1600,49 +1607,53 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
case TSDB_DATA_TYPE_NCHAR:
break;
case TSDB_DATA_TYPE_UTINYINT: {
pColAgg->sum += colVal.value.u8;
if (!minAssigned || pColAgg->min > colVal.value.u8) {
pColAgg->min = colVal.value.u8;
uint8_t u8 = *(uint8_t *)&colVal.value.val;
pColAgg->sum += u8;
if (!minAssigned || pColAgg->min > u8) {
pColAgg->min = u8;
minAssigned = true;
}
if (!maxAssigned || pColAgg->max < colVal.value.u8) {
pColAgg->max = colVal.value.u8;
if (!maxAssigned || pColAgg->max < u8) {
pColAgg->max = u8;
maxAssigned = true;
}
break;
}
case TSDB_DATA_TYPE_USMALLINT: {
pColAgg->sum += colVal.value.u16;
if (!minAssigned || pColAgg->min > colVal.value.u16) {
pColAgg->min = colVal.value.u16;
uint16_t u16 = *(uint16_t *)&colVal.value.val;
pColAgg->sum += u16;
if (!minAssigned || pColAgg->min > u16) {
pColAgg->min = u16;
minAssigned = true;
}
if (!maxAssigned || pColAgg->max < colVal.value.u16) {
pColAgg->max = colVal.value.u16;
if (!maxAssigned || pColAgg->max < u16) {
pColAgg->max = u16;
maxAssigned = true;
}
break;
}
case TSDB_DATA_TYPE_UINT: {
pColAgg->sum += colVal.value.u32;
if (!minAssigned || pColAgg->min > colVal.value.u32) {
pColAgg->min = colVal.value.u32;
uint32_t u32 = *(uint32_t *)&colVal.value.val;
pColAgg->sum += u32;
if (!minAssigned || pColAgg->min > u32) {
pColAgg->min = u32;
minAssigned = true;
}
if (!minAssigned || pColAgg->max < colVal.value.u32) {
pColAgg->max = colVal.value.u32;
if (!minAssigned || pColAgg->max < u32) {
pColAgg->max = u32;
maxAssigned = true;
}
break;
}
case TSDB_DATA_TYPE_UBIGINT: {
pColAgg->sum += colVal.value.u64;
if (!minAssigned || pColAgg->min > colVal.value.u64) {
pColAgg->min = colVal.value.u64;
uint64_t u64 = *(uint64_t *)&colVal.value.val;
pColAgg->sum += u64;
if (!minAssigned || pColAgg->min > u64) {
pColAgg->min = u64;
minAssigned = true;
}
if (!maxAssigned || pColAgg->max < colVal.value.u64) {
pColAgg->max = colVal.value.u64;
if (!maxAssigned || pColAgg->max < u64) {
pColAgg->max = u64;
maxAssigned = true;
}
break;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册