diff --git a/src/common/inc/tdataformat.h b/src/common/inc/tdataformat.h index 706fdeff987ac5fa58ad71b85a2d85c8f8f7586e..1d3319e61dfd2fafda7474cd5f8c30e60bc8b34f 100644 --- a/src/common/inc/tdataformat.h +++ b/src/common/inc/tdataformat.h @@ -87,6 +87,22 @@ int tdGetSchemaEncodeSize(STSchema *pSchema); void * tdEncodeSchema(void *dst, STSchema *pSchema); STSchema *tdDecodeSchema(void **psrc); +static FORCE_INLINE int comparColId(const void *key1, const void *key2) { + if (*(int16_t *)key1 > ((STColumn *)key2)->colId) { + return 1; + } else if (*(int16_t *)key1 < ((STColumn *)key2)->colId) { + return -1; + } else { + return 0; + } +} + +static FORCE_INLINE STColumn *tdGetColOfID(STSchema *pSchema, int16_t colId) { + void *ptr = bsearch(&colId, (void *)pSchema->columns, schemaNCols(pSchema), sizeof(STColumn), comparColId); + if (ptr == NULL) return NULL; + return (STColumn *)ptr; +} + // ----------------- Data row structure /* A data row, the format is like below: diff --git a/src/tsdb/src/tsdbMeta.c b/src/tsdb/src/tsdbMeta.c index 32ad30598b979af7b86606fe3693825db33076fb..f057dcb96e27209ca6a471487fbb845178a2cd35 100644 --- a/src/tsdb/src/tsdbMeta.c +++ b/src/tsdb/src/tsdbMeta.c @@ -250,29 +250,24 @@ STSchema * tsdbGetTableTagSchema(STsdbMeta *pMeta, STable *pTable) { int32_t tsdbGetTableTagVal(TsdbRepoT* repo, STableId* id, int32_t colId, int16_t* type, int16_t* bytes, char** val) { STsdbMeta* pMeta = tsdbGetMeta(repo); STable* pTable = tsdbGetTableByUid(pMeta, id->uid); + + STSchema *pSchema = tsdbGetTableTagSchema(pMeta, pTable); + STColumn *pCol = tdGetColOfID(pSchema, colId); + if (pCol == NULL) { + return -1; // No matched tag volumn + } - *val = tdQueryTagByID(pTable->tagVal, colId, type); + *val = tdGetKVRowValOfCol(pTable->tagVal, colId); + *type = pCol->type; if (*val != NULL) { - switch(*type) { - case TSDB_DATA_TYPE_BINARY: - case TSDB_DATA_TYPE_NCHAR: *bytes = varDataLen(*val); break; - case TSDB_DATA_TYPE_NULL: *bytes = 0; break; - default: - *bytes = tDataTypeDesc[*type].nSize;break; + if (IS_VAR_DATA_TYPE(*type)) { + *bytes = varDataLen(*val); + } else { + *bytes = TYPE_BYTES[*type]; } } - if (pCol == NULL) { - return -1; // No matched tags. Maybe the modification of tags has not been done yet. - } - - char* d = tdGetKVRowValOfCol(pTable->tagVal, pCol->colId); - //ASSERT((int8_t)tagtype == pCol->type) - *val = d; - *type = pCol->type; - *bytes = pCol->bytes; - return TSDB_CODE_SUCCESS; } diff --git a/src/tsdb/src/tsdbRWHelper.c b/src/tsdb/src/tsdbRWHelper.c index 8c7f8309ec9c60933d2de419668f7d81b50271f6..5bdf37c81e39453acaef6eb340803f0468d2ca59 100644 --- a/src/tsdb/src/tsdbRWHelper.c +++ b/src/tsdb/src/tsdbRWHelper.c @@ -453,7 +453,7 @@ int tsdbWriteCompIdx(SRWHelper *pHelper) { pHelper->pBuffer = trealloc(pHelper->pBuffer, tsizeof(pHelper->pBuffer)*2); } buf = POINTER_SHIFT(pHelper->pBuffer, drift); - buf = taosEncodeVariant32(buf, i); + buf = taosEncodeVariantU32(buf, i); buf = tsdbEncodeSCompIdx(buf, pCompIdx); } }