diff --git a/include/common/tdataformat.h b/include/common/tdataformat.h index 88f29b8f5c4d102d50bb259adb400a09b79c837b..864f034f16c3745e9b9f3377f975f7a47180d402 100644 --- a/include/common/tdataformat.h +++ b/include/common/tdataformat.h @@ -71,7 +71,7 @@ int32_t tBufferPut(SBuffer *pBuffer, const void *pData, int64_t nData); int32_t tBufferReserve(SBuffer *pBuffer, int64_t nData, void **ppData); // STSchema ================================ -void tTSchemaDestroy(STSchema *pTSchema); +void tDestroyTSchema(STSchema *pTSchema); // SColVal ================================ #define CV_FLAG_VALUE ((int8_t)0x0) @@ -243,6 +243,8 @@ void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) int32_t tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes); STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder); +STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version); + #endif #ifdef __cplusplus diff --git a/source/common/src/tdataformat.c b/source/common/src/tdataformat.c index 823b9182c25ecff98fd8e4682e38ebd15d6c82eb..7bf15b6c74212494021936b7adac2c5100aab859 100644 --- a/source/common/src/tdataformat.c +++ b/source/common/src/tdataformat.c @@ -709,9 +709,6 @@ _exit: } // STSchema ======================================== -void tTSchemaDestroy(STSchema *pTSchema) { - if (pTSchema) taosMemoryFree(pTSchema); -} // STag ======================================== static int tTagValCmprFn(const void *p1, const void *p2) { @@ -1179,6 +1176,43 @@ STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder) { #endif +STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) { + STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols); + if (pTSchema == NULL) return NULL; + + pTSchema->numOfCols = numOfCols; + pTSchema->version = version; + + // timestamp column + ASSERT(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP); + ASSERT(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID); + pTSchema->columns[0].colId = aSchema[0].colId; + pTSchema->columns[0].type = aSchema[0].type; + pTSchema->columns[0].flags = aSchema[0].flags; + pTSchema->columns[0].bytes = aSchema[0].bytes; + pTSchema->columns[0].offset = -1; + + // other columns + for (int32_t iCol = 1; iCol < numOfCols; iCol++) { + SSchema *pSchema = &aSchema[iCol]; + STColumn *pTColumn = &pTSchema->columns[iCol]; + + pTColumn->colId = pSchema->colId; + pTColumn->type = pSchema->type; + pTColumn->flags = pSchema->flags; + pTColumn->bytes = pSchema->bytes; + pTColumn->offset = pTSchema->flen; + + pTSchema->flen += TYPE_BYTES[pTColumn->type]; + } + + return pTSchema; +} + +void tDestroyTSchema(STSchema *pTSchema) { + if (pTSchema) taosMemoryFree(pTSchema); +} + // SColData ======================================== void tColDataDestroy(void *ph) { SColData *pColData = (SColData *)ph; diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 65a46331aa8d4bd62f29f4e0c6c068ef4f95a6cd..cf9ae30ff07e3e17bacc9ae71ea5f222b5c16827 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -341,7 +341,7 @@ int32_t tsdbUpdateTableSchema(SMeta *pMeta, int64_t suid, int64_t uid, SSkmInfo pSkmInfo->suid = suid; pSkmInfo->uid = uid; - tTSchemaDestroy(pSkmInfo->pTSchema); + tDestroyTSchema(pSkmInfo->pTSchema); code = metaGetTbTSchemaEx(pMeta, suid, uid, -1, &pSkmInfo->pTSchema); TSDB_CHECK_CODE(code, lino, _exit); @@ -365,7 +365,7 @@ static int32_t tsdbCommitterUpdateRowSchema(SCommitter *pCommitter, int64_t suid pCommitter->skmRow.suid = suid; pCommitter->skmRow.uid = uid; - tTSchemaDestroy(pCommitter->skmRow.pTSchema); + tDestroyTSchema(pCommitter->skmRow.pTSchema); code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, sver, &pCommitter->skmRow.pTSchema); TSDB_CHECK_CODE(code, lino, _exit); @@ -498,7 +498,7 @@ static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) { #if 0 ASSERT(pCommitter->minKey <= pCommitter->nextKey && pCommitter->maxKey >= pCommitter->nextKey); #endif - + pCommitter->nextKey = TSKEY_MAX; // Reader @@ -623,7 +623,8 @@ int32_t tsdbWriteDataBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SMapDa _exit: if (code) { - tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code)); + tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, + tstrerror(code)); } return code; } @@ -666,7 +667,8 @@ int32_t tsdbWriteSttBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SArray _exit: if (code) { - tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code)); + tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, + tstrerror(code)); } return code; } @@ -706,7 +708,8 @@ static int32_t tsdbCommitSttBlk(SDataFWriter *pWriter, SDiskDataBuilder *pBuilde _exit: if (code) { - tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code)); + tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, + tstrerror(code)); } return code; } @@ -919,8 +922,8 @@ static void tsdbCommitDataEnd(SCommitter *pCommitter) { #else tBlockDataDestroy(&pCommitter->dWriter.bDatal, 1); #endif - tTSchemaDestroy(pCommitter->skmTable.pTSchema); - tTSchemaDestroy(pCommitter->skmRow.pTSchema); + tDestroyTSchema(pCommitter->skmTable.pTSchema); + tDestroyTSchema(pCommitter->skmRow.pTSchema); } static int32_t tsdbCommitData(SCommitter *pCommitter) { diff --git a/source/dnode/vnode/src/tsdb/tsdbSnapshot.c b/source/dnode/vnode/src/tsdb/tsdbSnapshot.c index ec89bed17a8c9ee002d751ee0c43bb5c0f5170e2..f4bdeeb387842d1a959f7269608a6f29f6decf28 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSnapshot.c +++ b/source/dnode/vnode/src/tsdb/tsdbSnapshot.c @@ -555,7 +555,7 @@ int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) { } tBlockDataDestroy(&pReader->bData, 1); - tTSchemaDestroy(pReader->skmTable.pTSchema); + tDestroyTSchema(pReader->skmTable.pTSchema); // del if (pReader->pDelFReader) tsdbDelFReaderClose(&pReader->pDelFReader); @@ -1416,7 +1416,7 @@ int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) { taosArrayDestroy(pWriter->dReader.aBlockIdx); tBlockDataDestroy(&pWriter->bData, 1); - tTSchemaDestroy(pWriter->skmTable.pTSchema); + tDestroyTSchema(pWriter->skmTable.pTSchema); for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) { tFree(pWriter->aBuf[iBuf]);