diff --git a/source/libs/index/inc/indexComm.h b/source/libs/index/inc/indexComm.h index 9b23e4eb44..3066fd1c2c 100644 --- a/source/libs/index/inc/indexComm.h +++ b/source/libs/index/inc/indexComm.h @@ -33,17 +33,17 @@ typedef enum { MATCH, CONTINUE, BREAK } TExeCond; typedef TExeCond (*_cache_range_compare)(void* a, void* b, int8_t type); -TExeCond tDoCommpare(__compar_fn_t func, int8_t comType, void* a, void* b); +TExeCond tCompare(__compar_fn_t func, int8_t cmpType, void* a, void* b, int8_t dType); +TExeCond tDoCompare(__compar_fn_t func, int8_t cmpType, void* a, void* b); _cache_range_compare indexGetCompare(RangeType ty); int32_t indexConvertData(void* src, int8_t type, void** dst); +int32_t indexConvertDataToStr(void* src, int8_t type, void** dst); int32_t indexGetDataByteLen(int8_t type); -int32_t indexMayFillNumbericData(void* number, int32_t tlen); - -int32_t indexMayUnfillNumbericData(void* number, int32_t tlen); +char* indexInt2str(int64_t val, char* dst, int radix); #ifdef __cplusplus } diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index b9df2e88ce..162d64c41c 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -271,7 +271,7 @@ SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colTy tm->nColName = nColName; char* buf = NULL; - int32_t len = indexConvertData((void*)colVal, INDEX_TYPE_GET_TYPE(colType), (void**)&buf); + int32_t len = indexConvertDataToStr((void*)colVal, INDEX_TYPE_GET_TYPE(colType), (void**)&buf); assert(len != -1); tm->colVal = buf; diff --git a/source/libs/index/src/indexComm.c b/source/libs/index/src/indexComm.c index 89f19a3b81..0c2e9e6b44 100644 --- a/source/libs/index/src/indexComm.c +++ b/source/libs/index/src/indexComm.c @@ -19,10 +19,38 @@ #include "tcoding.h" #include "tcompare.h" #include "tdataformat.h" +#include "ttypes.h" char JSON_COLUMN[] = "JSON"; char JSON_VALUE_DELIM = '&'; +char* indexInt2str(int64_t val, char* dst, int radix) { + char buffer[65]; + char* p; + int64_t new_val; + uint64_t uval = (uint64_t)val; + + if (radix < 0) { + if (val < 0) { + *dst++ = '-'; + uval = (uint64_t)0 - uval; /* Avoid integer overflow in (-val) for LLONG_MIN (BUG#31799). */ + } + } + p = &buffer[sizeof(buffer) - 1]; + *p = '\0'; + new_val = (int64_t)(uval / 10); + *--p = '0' + (char)(uval - (uint64_t)new_val * 10); + val = new_val; + + while (val != 0) { + new_val = val / 10; + *--p = '0' + (char)(val - new_val * 10); + val = new_val; + } + while ((*dst++ = *p++) != 0) + ; + return dst - 1; +} static __compar_fn_t indexGetCompar(int8_t type) { if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { return (__compar_fn_t)strcmp; @@ -31,41 +59,49 @@ static __compar_fn_t indexGetCompar(int8_t type) { } static TExeCond tCompareLessThan(void* a, void* b, int8_t type) { __compar_fn_t func = indexGetCompar(type); - - int32_t tlen = indexGetDataByteLen(type); - indexMayUnfillNumbericData(a, tlen); - indexMayUnfillNumbericData(b, tlen); - return tDoCommpare(func, QUERY_LESS_THAN, a, b); + return tCompare(func, QUERY_LESS_THAN, a, b, type); } static TExeCond tCompareLessEqual(void* a, void* b, int8_t type) { __compar_fn_t func = indexGetCompar(type); - - int32_t tlen = indexGetDataByteLen(type); - indexMayUnfillNumbericData(a, tlen); - indexMayUnfillNumbericData(b, tlen); - return tDoCommpare(func, QUERY_LESS_EQUAL, a, b); + return tCompare(func, QUERY_LESS_EQUAL, a, b, type); } static TExeCond tCompareGreaterThan(void* a, void* b, int8_t type) { __compar_fn_t func = indexGetCompar(type); - - int32_t tlen = indexGetDataByteLen(type); - indexMayUnfillNumbericData(a, tlen); - indexMayUnfillNumbericData(b, tlen); - return tDoCommpare(func, QUERY_GREATER_THAN, a, b); + return tCompare(func, QUERY_GREATER_THAN, a, b, type); } static TExeCond tCompareGreaterEqual(void* a, void* b, int8_t type) { __compar_fn_t func = indexGetCompar(type); - - int32_t tlen = indexGetDataByteLen(type); - indexMayUnfillNumbericData(a, tlen); - indexMayUnfillNumbericData(b, tlen); - return tDoCommpare(func, QUERY_GREATER_EQUAL, a, b); + return tCompare(func, QUERY_GREATER_EQUAL, a, b, type); } - -TExeCond tDoCommpare(__compar_fn_t func, int8_t comType, void* a, void* b) { +TExeCond tCompare(__compar_fn_t func, int8_t cmptype, void* a, void* b, int8_t dtype) { + if (dtype == TSDB_DATA_TYPE_BINARY || dtype == TSDB_DATA_TYPE_NCHAR) { + return tDoCompare(func, cmptype, a, b); + } +#if 1 + int8_t bytes = tDataTypes[dtype].bytes; + if (bytes == 1) { + int8_t va = taosStr2int64(a); + int8_t vb = taosStr2int64(b); + return tDoCompare(func, cmptype, &va, &vb); + } else if (bytes == 2) { + int16_t va = taosStr2int64(a); + int16_t vb = taosStr2int64(b); + return tDoCompare(func, cmptype, &va, &vb); + } else if (bytes == 4) { + int32_t va = taosStr2int64(a); + int32_t vb = taosStr2int64(b); + return tDoCompare(func, cmptype, &va, &vb); + } else { + int64_t va = taosStr2int64(a); + int64_t vb = taosStr2int64(b); + return tDoCompare(func, cmptype, &va, &vb); + } +#endif +} +TExeCond tDoCompare(__compar_fn_t func, int8_t comparType, void* a, void* b) { // optime later int32_t ret = func(a, b); - switch (comType) { + switch (comparType) { case QUERY_LESS_THAN: { if (ret < 0) return MATCH; } break; @@ -231,78 +267,92 @@ int32_t indexConvertData(void* src, int8_t type, void** dst) { break; } *dst = *dst - tlen; - - indexMayFillNumbericData(*dst, tlen); - - // if (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR && type != TSDB_DATA_TYPE_VARBINARY && - // type != TSDB_DATA_TYPE_VARCHAR) { uint8_t* p = *dst; - // for (int i = 0; i < tlen; i++) { - // if (p[i] == 0) { - // p[i] = (uint8_t)'0'; - // } - // } - //} + // indexMayFillNumbericData(*dst, tlen); return tlen; } -int32_t indexGetDataByteLen(int8_t type) { - int32_t tlen = -1; +int32_t indexConvertDataToStr(void* src, int8_t type, void** dst) { + int tlen = tDataTypes[type].bytes; + switch (type) { case TSDB_DATA_TYPE_TIMESTAMP: - tlen = sizeof(int64_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int64_t*)src, *dst, -1); break; case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_UTINYINT: - tlen = sizeof(uint8_t); + // tlen = taosEncodeFixedU8(NULL, *(uint8_t*)src); + //*dst = taosMemoryCalloc(1, tlen + 1); + // tlen = taosEncodeFixedU8(dst, *(uint8_t*)src); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(uint8_t*)src, *dst, 1); break; case TSDB_DATA_TYPE_TINYINT: - tlen = sizeof(uint8_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int8_t*)src, *dst, 1); break; case TSDB_DATA_TYPE_SMALLINT: - tlen = sizeof(int16_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int16_t*)src, *dst, -1); break; case TSDB_DATA_TYPE_USMALLINT: - tlen = sizeof(uint16_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(uint16_t*)src, *dst, -1); break; case TSDB_DATA_TYPE_INT: - tlen = sizeof(int32_t); - break; - case TSDB_DATA_TYPE_UINT: - tlen = sizeof(uint32_t); - break; - case TSDB_DATA_TYPE_BIGINT: - tlen = sizeof(int64_t); - break; - case TSDB_DATA_TYPE_UBIGINT: - tlen = sizeof(uint64_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int32_t*)src, *dst, -1); break; case TSDB_DATA_TYPE_FLOAT: - tlen = sizeof(float); + tlen = taosEncodeBinary(NULL, src, sizeof(float)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, src, sizeof(float)); + break; + case TSDB_DATA_TYPE_UINT: + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(uint32_t*)src, *dst, 1); + break; + case TSDB_DATA_TYPE_BIGINT: + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int64_t*)src, *dst, 1); break; case TSDB_DATA_TYPE_DOUBLE: - tlen = sizeof(double); + tlen = taosEncodeBinary(NULL, src, sizeof(double)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, src, sizeof(double)); break; + case TSDB_DATA_TYPE_UBIGINT: + assert(0); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(uint64_t*)src, *dst, 1); + break; + case TSDB_DATA_TYPE_NCHAR: { + tlen = taosEncodeBinary(NULL, varDataVal(src), varDataLen(src)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, varDataVal(src), varDataLen(src)); + *dst = *dst - tlen; + + break; + } + case TSDB_DATA_TYPE_VARCHAR: { // TSDB_DATA_TYPE_BINARY +#if 1 + tlen = taosEncodeBinary(NULL, src, strlen(src)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, src, strlen(src)); + *dst = *dst - tlen; + break; +#endif + } + case TSDB_DATA_TYPE_VARBINARY: +#if 1 + tlen = taosEncodeBinary(NULL, src, strlen(src)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, src, strlen(src)); + *dst = *dst - tlen; + break; +#endif default: + TASSERT(0); break; } return tlen; } - -int32_t indexMayFillNumbericData(void* number, int32_t tlen) { - for (int i = 0; i < tlen; i++) { - int8_t* p = number; - if (p[i] == 0) { - p[i] = (uint8_t)'0'; - } - } - return 0; -} - -int32_t indexMayUnfillNumbericData(void* number, int32_t tlen) { - for (int i = 0; i < tlen; i++) { - int8_t* p = number; - if (p[i] == (uint8_t)'0') { - p[i] = 0; - } - } - return 0; -} diff --git a/source/libs/index/test/.utilUT.cc.swn b/source/libs/index/test/.utilUT.cc.swn deleted file mode 100644 index 3023c72b42..0000000000 Binary files a/source/libs/index/test/.utilUT.cc.swn and /dev/null differ diff --git a/source/libs/index/test/jsonUT.cc b/source/libs/index/test/jsonUT.cc index f1c9cbab61..d6ff7264d7 100644 --- a/source/libs/index/test/jsonUT.cc +++ b/source/libs/index/test/jsonUT.cc @@ -465,7 +465,7 @@ TEST_F(JsonEnv, testWriteJsonTfileAndCache) { SIndexMultiTerm* terms = indexMultiTermCreate(); indexMultiTermAdd(terms, term); for (size_t i = 0; i < 1000; i++) { - tIndexJsonPut(index, terms, i); + tIndexJsonPut(index, terms, i + 1000); } indexMultiTermDestroy(terms); } diff --git a/source/libs/index/test/utilUT.cc b/source/libs/index/test/utilUT.cc index 0781c9897f..18a2b457c4 100644 --- a/source/libs/index/test/utilUT.cc +++ b/source/libs/index/test/utilUT.cc @@ -308,18 +308,16 @@ TEST_F(UtilEnv, 01Except) { ASSERT_EQ(*(uint64_t *)taosArrayGet(total, 1), 100); } TEST_F(UtilEnv, testFill) { - for (int i = 0; i < 10000; i++) { - char buf[10] = {0}; - void *pBuf = (void *)buf; - int val = i; - int v; - taosEncodeFixedI32((void **)(&pBuf), val); - // memcpy(buf, &val, sizeof(int)); - indexMayFillNumbericData((void *)buf, sizeof(val)); - indexMayUnfillNumbericData((void *)buf, sizeof(val)); - - taosDecodeFixedI32(buf, &v); - ASSERT_EQ(val, v); + for (int i = 0; i < 10000000; i++) { + int64_t val = i; + char buf[65] = {0}; + indexInt2str(val, buf, 1); + EXPECT_EQ(val, taosStr2int64(buf)); + } + for (int i = 0; i < 10000000; i++) { + int64_t val = 0 - i; + char buf[65] = {0}; + indexInt2str(val, buf, -1); + EXPECT_EQ(val, taosStr2int64(buf)); } - assert(0); }