From 1b6c7a5923b683a29752d42dcfb073728f5caed3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 11 Oct 2021 18:36:40 +0800 Subject: [PATCH] make compile --- include/common/tcompare.h | 36 +++ include/common/ttypes.h | 6 +- include/util/{tcompare.h => compare.h} | 19 +- include/util/tdef.h | 27 -- include/util/types.h | 88 ++++++ source/common/src/tcompare.c | 384 +---------------------- source/server/CMakeLists.txt | 1 + source/server/vnode/meta/CMakeLists.txt | 6 +- source/util/CMakeLists.txt | 2 +- source/util/src/compare.c | 398 ++++++++++++++++++++++++ source/{common => util}/src/terror.c | 0 source/util/src/thashutil.c | 3 +- source/util/src/tskiplist.c | 2 +- source/util/src/tutil.c | 21 -- 14 files changed, 544 insertions(+), 449 deletions(-) create mode 100644 include/common/tcompare.h rename include/util/{tcompare.h => compare.h} (79%) create mode 100644 include/util/types.h create mode 100644 source/util/src/compare.c rename source/{common => util}/src/terror.c (100%) diff --git a/include/common/tcompare.h b/include/common/tcompare.h new file mode 100644 index 0000000000..8476a79e92 --- /dev/null +++ b/include/common/tcompare.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef _TD_TCOMPARE_H_ +#define _TD_TCOMPARE_H_ + +#include "compare.h" +#include "ttypes.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t compareStrPatternComp(const void* pLeft, const void* pRight); +int32_t compareWStrPatternComp(const void* pLeft, const void* pRight); +__compar_fn_t getComparFunc(int32_t type, int32_t optr); +__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order); +int32_t doCompare(const char* a, const char* b, int32_t type, size_t size); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_TCOMPARE_H_*/ \ No newline at end of file diff --git a/include/common/ttypes.h b/include/common/ttypes.h index 6fe6e11d05..991bb5c6be 100644 --- a/include/common/ttypes.h +++ b/include/common/ttypes.h @@ -6,10 +6,10 @@ extern "C" { #endif #include "taosdef.h" +#include "types.h" // ----------------- For variable data types such as TSDB_DATA_TYPE_BINARY and TSDB_DATA_TYPE_NCHAR typedef int32_t VarDataOffsetT; -typedef int16_t VarDataLenT; // maxVarDataLen: 32767 typedef uint16_t TDRowLenT; // not including overhead: 0 ~ 65535 typedef uint32_t TDRowTLenT; // total length, including overhead @@ -30,11 +30,7 @@ typedef struct { } SNCharNullT; #pragma pack(pop) -#define VARSTR_HEADER_SIZE sizeof(VarDataLenT) - -#define varDataLen(v) ((VarDataLenT *)(v))[0] #define varDataTLen(v) (sizeof(VarDataLenT) + varDataLen(v)) -#define varDataVal(v) ((void *)((char *)v + VARSTR_HEADER_SIZE)) #define varDataCopy(dst, v) memcpy((dst), (void*) (v), varDataTLen(v)) #define varDataLenByData(v) (*(VarDataLenT *)(((char*)(v)) - VARSTR_HEADER_SIZE)) #define varDataSetLen(v, _len) (((VarDataLenT *)(v))[0] = (VarDataLenT) (_len)) diff --git a/include/util/tcompare.h b/include/util/compare.h similarity index 79% rename from include/util/tcompare.h rename to include/util/compare.h index 4b2c583b4b..461552ca7b 100644 --- a/include/util/tcompare.h +++ b/include/util/compare.h @@ -46,11 +46,6 @@ int patternMatch(const char *pattern, const char *str, size_t size, const SPatte int WCSPatternMatch(const wchar_t *pattern, const wchar_t *str, size_t size, const SPatternCompareInfo *pInfo); -int32_t doCompare(const char* a, const char* b, int32_t type, size_t size); - -__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order); - -__compar_fn_t getComparFunc(int32_t type, int32_t optr); int32_t taosArrayCompareString(const void* a, const void* b); @@ -82,12 +77,22 @@ int32_t compareDoubleVal(const void *pLeft, const void *pRight); int32_t compareLenPrefixedStr(const void *pLeft, const void *pRight); int32_t compareLenPrefixedWStr(const void *pLeft, const void *pRight); -int32_t compareStrPatternComp(const void* pLeft, const void* pRight); int32_t compareStrRegexComp(const void* pLeft, const void* pRight); int32_t compareStrRegexCompMatch(const void* pLeft, const void* pRight); int32_t compareStrRegexCompNMatch(const void* pLeft, const void* pRight); int32_t compareFindItemInSet(const void *pLeft, const void* pRight); -int32_t compareWStrPatternComp(const void* pLeft, const void* pRight); +int32_t compareInt8ValDesc(const void *pLeft, const void *pRight); +int32_t compareInt16ValDesc(const void* pLeft, const void* pRight); +int32_t compareInt32ValDesc(const void* pLeft, const void* pRight); +int32_t compareInt64ValDesc(const void* pLeft, const void* pRight); +int32_t compareFloatValDesc(const void* pLeft, const void* pRight); +int32_t compareDoubleValDesc(const void* pLeft, const void* pRight); +int32_t compareUint8ValDesc(const void* pLeft, const void* pRight); +int32_t compareUint16ValDesc(const void* pLeft, const void* pRight); +int32_t compareUint32ValDesc(const void* pLeft, const void* pRight); +int32_t compareUint64ValDesc(const void* pLeft, const void* pRight); +int32_t compareLenPrefixedStrDesc(const void* pLeft, const void* pRight); +int32_t compareLenPrefixedWStrDesc(const void* pLeft, const void* pRight); #ifdef __cplusplus } diff --git a/include/util/tdef.h b/include/util/tdef.h index a24d2bc911..008c9215ba 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -110,33 +110,6 @@ do { \ (src) = (void *)((char *)src + sizeof(type));\ } while(0) -#define GET_INT8_VAL(x) (*(int8_t *)(x)) -#define GET_INT16_VAL(x) (*(int16_t *)(x)) -#define GET_INT32_VAL(x) (*(int32_t *)(x)) -#define GET_INT64_VAL(x) (*(int64_t *)(x)) -#define GET_UINT8_VAL(x) (*(uint8_t*) (x)) -#define GET_UINT16_VAL(x) (*(uint16_t *)(x)) -#define GET_UINT32_VAL(x) (*(uint32_t *)(x)) -#define GET_UINT64_VAL(x) (*(uint64_t *)(x)) - -#ifdef _TD_ARM_32 - float taos_align_get_float(const char* pBuf); - double taos_align_get_double(const char* pBuf); - - #define GET_FLOAT_VAL(x) taos_align_get_float(x) - #define GET_DOUBLE_VAL(x) taos_align_get_double(x) - #define SET_FLOAT_VAL(x, y) { float z = (float)(y); (*(int32_t*) x = *(int32_t*)(&z)); } - #define SET_DOUBLE_VAL(x, y) { double z = (double)(y); (*(int64_t*) x = *(int64_t*)(&z)); } - #define SET_FLOAT_PTR(x, y) { (*(int32_t*) x = *(int32_t*)y); } - #define SET_DOUBLE_PTR(x, y) { (*(int64_t*) x = *(int64_t*)y); } -#else - #define GET_FLOAT_VAL(x) (*(float *)(x)) - #define GET_DOUBLE_VAL(x) (*(double *)(x)) - #define SET_FLOAT_VAL(x, y) { (*(float *)(x)) = (float)(y); } - #define SET_DOUBLE_VAL(x, y) { (*(double *)(x)) = (double)(y); } - #define SET_FLOAT_PTR(x, y) { (*(float *)(x)) = (*(float *)(y)); } - #define SET_DOUBLE_PTR(x, y) { (*(double *)(x)) = (*(double *)(y)); } -#endif // TODO: check if below is necessary #define TSDB_RELATION_INVALID 0 diff --git a/include/util/types.h b/include/util/types.h new file mode 100644 index 0000000000..35cb614f6f --- /dev/null +++ b/include/util/types.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef _TD_TYPES_H_ +#define _TD_TYPES_H_ + +#include "os.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define GET_INT8_VAL(x) (*(int8_t *)(x)) +#define GET_INT16_VAL(x) (*(int16_t *)(x)) +#define GET_INT32_VAL(x) (*(int32_t *)(x)) +#define GET_INT64_VAL(x) (*(int64_t *)(x)) +#define GET_UINT8_VAL(x) (*(uint8_t*) (x)) +#define GET_UINT16_VAL(x) (*(uint16_t *)(x)) +#define GET_UINT32_VAL(x) (*(uint32_t *)(x)) +#define GET_UINT64_VAL(x) (*(uint64_t *)(x)) + +static FORCE_INLINE float taos_align_get_float(const char *pBuf) { +#if __STDC_VERSION__ >= 201112L + static_assert(sizeof(float) == sizeof(uint32_t), "sizeof(float) must equal to sizeof(uint32_t)"); +#else + assert(sizeof(float) == sizeof(uint32_t)); +#endif + float fv = 0; + memcpy(&fv, pBuf, sizeof(fv)); // in ARM, return *((const float*)(pBuf)) may cause problem + return fv; +} + +static FORCE_INLINE double taos_align_get_double(const char *pBuf) { +#if __STDC_VERSION__ >= 201112L + static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) must equal to sizeof(uint64_t)"); +#else + assert(sizeof(double) == sizeof(uint64_t)); +#endif + double dv = 0; + memcpy(&dv, pBuf, sizeof(dv)); // in ARM, return *((const double*)(pBuf)) may cause problem + return dv; +} + +// #ifdef _TD_ARM_32 +// float taos_align_get_float(const char* pBuf); +// double taos_align_get_double(const char* pBuf); + +// #define GET_FLOAT_VAL(x) taos_align_get_float(x) +// #define GET_DOUBLE_VAL(x) taos_align_get_double(x) +// #define SET_FLOAT_VAL(x, y) { float z = (float)(y); (*(int32_t*) x = *(int32_t*)(&z)); } +// #define SET_DOUBLE_VAL(x, y) { double z = (double)(y); (*(int64_t*) x = *(int64_t*)(&z)); } +// #define SET_FLOAT_PTR(x, y) { (*(int32_t*) x = *(int32_t*)y); } +// #define SET_DOUBLE_PTR(x, y) { (*(int64_t*) x = *(int64_t*)y); } +// #else + #define GET_FLOAT_VAL(x) (*(float *)(x)) + #define GET_DOUBLE_VAL(x) (*(double *)(x)) + #define SET_FLOAT_VAL(x, y) { (*(float *)(x)) = (float)(y); } + #define SET_DOUBLE_VAL(x, y) { (*(double *)(x)) = (double)(y); } + #define SET_FLOAT_PTR(x, y) { (*(float *)(x)) = (*(float *)(y)); } + #define SET_DOUBLE_PTR(x, y) { (*(double *)(x)) = (*(double *)(y)); } +// #endif + +typedef int16_t VarDataLenT; // maxVarDataLen: 32767 +#define VARSTR_HEADER_SIZE sizeof(VarDataLenT) + +#define varDataLen(v) ((VarDataLenT *)(v))[0] +#define varDataVal(v) ((void *)((char *)v + VARSTR_HEADER_SIZE)) + +typedef int32_t VarDataOffsetT; +typedef int16_t VarDataLenT; // maxVarDataLen: 32767 + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_TYPES_H_*/ \ No newline at end of file diff --git a/source/common/src/tcompare.c b/source/common/src/tcompare.c index e8e9ae3d68..3c0e6df735 100644 --- a/source/common/src/tcompare.c +++ b/source/common/src/tcompare.c @@ -12,334 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#define _BSD_SOURCE -#define _GNU_SOURCE -#define _XOPEN_SOURCE -#define _DEFAULT_SOURCE -#include "os.h" #include "tcompare.h" -#include "ulog.h" -#include "thash.h" -#include "regex.h" -#include "ttypes.h" - -int32_t setCompareBytes1(const void *pLeft, const void *pRight) { - return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0; -} - -int32_t setCompareBytes2(const void *pLeft, const void *pRight) { - return NULL != taosHashGet((SHashObj *)pRight, pLeft, 2) ? 1 : 0; -} - -int32_t setCompareBytes4(const void *pLeft, const void *pRight) { - return NULL != taosHashGet((SHashObj *)pRight, pLeft, 4) ? 1 : 0; -} - -int32_t setCompareBytes8(const void *pLeft, const void *pRight) { - return NULL != taosHashGet((SHashObj *)pRight, pLeft, 8) ? 1 : 0; -} - -int32_t compareInt8Val(const void *pLeft, const void *pRight) { - int8_t left = GET_INT8_VAL(pLeft), right = GET_INT8_VAL(pRight); - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -int32_t compareInt8ValDesc(const void *pLeft, const void *pRight) { - return compareInt8Val(pRight, pLeft); -} - -int32_t compareInt16Val(const void *pLeft, const void *pRight) { - int16_t left = GET_INT16_VAL(pLeft), right = GET_INT16_VAL(pRight); - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -int32_t compareInt16ValDesc(const void* pLeft, const void* pRight) { - return compareInt16Val(pRight, pLeft); -} - -int32_t compareInt32Val(const void *pLeft, const void *pRight) { - int32_t left = GET_INT32_VAL(pLeft), right = GET_INT32_VAL(pRight); - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -int32_t compareInt32ValDesc(const void* pLeft, const void* pRight) { - return compareInt32Val(pRight, pLeft); -} - -int32_t compareInt64Val(const void *pLeft, const void *pRight) { - int64_t left = GET_INT64_VAL(pLeft), right = GET_INT64_VAL(pRight); - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -int32_t compareInt64ValDesc(const void* pLeft, const void* pRight) { - return compareInt64Val(pRight, pLeft); -} - -int32_t compareUint32Val(const void *pLeft, const void *pRight) { - uint32_t left = GET_UINT32_VAL(pLeft), right = GET_UINT32_VAL(pRight); - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -int32_t compareUint32ValDesc(const void* pLeft, const void* pRight) { - return compareUint32Val(pRight, pLeft); -} - -int32_t compareUint64Val(const void *pLeft, const void *pRight) { - uint64_t left = GET_UINT64_VAL(pLeft), right = GET_UINT64_VAL(pRight); - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -int32_t compareUint64ValDesc(const void* pLeft, const void* pRight) { - return compareUint64Val(pRight, pLeft); -} - -int32_t compareUint16Val(const void *pLeft, const void *pRight) { - uint16_t left = GET_UINT16_VAL(pLeft), right = GET_UINT16_VAL(pRight); - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -int32_t compareUint16ValDesc(const void* pLeft, const void* pRight) { - return compareUint16Val(pRight, pLeft); -} - -int32_t compareUint8Val(const void* pLeft, const void* pRight) { - uint8_t left = GET_UINT8_VAL(pLeft), right = GET_UINT8_VAL(pRight); - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -int32_t compareUint8ValDesc(const void* pLeft, const void* pRight) { - return compareUint8Val(pRight, pLeft); -} - -int32_t compareFloatVal(const void *pLeft, const void *pRight) { - float p1 = GET_FLOAT_VAL(pLeft); - float p2 = GET_FLOAT_VAL(pRight); - - if (isnan(p1) && isnan(p2)) { - return 0; - } - - if (isnan(p1)) { - return -1; - } - - if (isnan(p2)) { - return 1; - } - if (FLT_EQUAL(p1, p2)) { - return 0; - } - return FLT_GREATER(p1, p2) ? 1: -1; -} - -int32_t compareFloatValDesc(const void* pLeft, const void* pRight) { - return compareFloatVal(pRight, pLeft); -} - -int32_t compareDoubleVal(const void *pLeft, const void *pRight) { - double p1 = GET_DOUBLE_VAL(pLeft); - double p2 = GET_DOUBLE_VAL(pRight); - - if (isnan(p1) && isnan(p2)) { - return 0; - } - - if (isnan(p1)) { - return -1; - } - - if (isnan(p2)) { - return 1; - } - if (FLT_EQUAL(p1, p2)) { - return 0; - } - return FLT_GREATER(p1, p2) ? 1: -1; -} - -int32_t compareDoubleValDesc(const void* pLeft, const void* pRight) { - return compareDoubleVal(pRight, pLeft); -} - -int32_t compareLenPrefixedStr(const void *pLeft, const void *pRight) { - int32_t len1 = varDataLen(pLeft); - int32_t len2 = varDataLen(pRight); - - if (len1 != len2) { - return len1 > len2? 1:-1; - } else { - int32_t ret = strncmp(varDataVal(pLeft), varDataVal(pRight), len1); - if (ret == 0) { - return 0; - } else { - return ret > 0 ? 1:-1; - } - } -} - -int32_t compareLenPrefixedStrDesc(const void* pLeft, const void* pRight) { - return compareLenPrefixedStr(pRight, pLeft); -} - -int32_t compareLenPrefixedWStr(const void *pLeft, const void *pRight) { - int32_t len1 = varDataLen(pLeft); - int32_t len2 = varDataLen(pRight); - - if (len1 != len2) { - return len1 > len2? 1:-1; - } else { - int32_t ret = memcmp((wchar_t*) pLeft, (wchar_t*) pRight, len1); - if (ret == 0) { - return 0; - } else { - return ret > 0 ? 1 : -1; - } - } -} - -int32_t compareLenPrefixedWStrDesc(const void* pLeft, const void* pRight) { - return compareLenPrefixedWStr(pRight, pLeft); -} - -/* - * Compare two strings - * TSDB_MATCH: Match - * TSDB_NOMATCH: No match - * TSDB_NOWILDCARDMATCH: No match in spite of having * or % wildcards. - * Like matching rules: - * '%': Matches zero or more characters - * '_': Matches one character - * - */ -int patternMatch(const char *patterStr, const char *str, size_t size, const SPatternCompareInfo *pInfo) { - char c, c1; - - int32_t i = 0; - int32_t j = 0; - int32_t o = 0; - int32_t m = 0; - - while ((c = patterStr[i++]) != 0) { - if (c == pInfo->matchAll) { /* Match "*" */ - - while ((c = patterStr[i++]) == pInfo->matchAll || c == pInfo->matchOne) { - if (c == pInfo->matchOne) { - if (j > size || str[j++] == 0) { - // empty string, return not match - return TSDB_PATTERN_NOWILDCARDMATCH; - } else { - ++o; - } - } - } - - if (c == 0) { - return TSDB_PATTERN_MATCH; /* "*" at the end of the pattern matches */ - } - - char next[3] = {toupper(c), tolower(c), 0}; - m = o; - while (1) { - size_t n = strcspn(str + m, next); - str += m + n; - - if (str[0] == 0 || (n >= size)) { - break; - } - - int32_t ret = patternMatch(&patterStr[i], ++str, size - n - 1, pInfo); - if (ret != TSDB_PATTERN_NOMATCH) { - return ret; - } - m = 0; - } - return TSDB_PATTERN_NOWILDCARDMATCH; - } - - c1 = str[j++]; - ++o; - - if (j <= size) { - if (c == '\\' && patterStr[i] == '_' && c1 == '_') { i++; continue; } - if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) { - continue; - } - } - - return TSDB_PATTERN_NOMATCH; - } - - return (str[j] == 0 || j >= size) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH; -} - -int WCSPatternMatch(const wchar_t *patterStr, const wchar_t *str, size_t size, const SPatternCompareInfo *pInfo) { - wchar_t c, c1; - wchar_t matchOne = L'_'; // "_" - wchar_t matchAll = L'%'; // "%" - - int32_t i = 0; - int32_t j = 0; - - while ((c = patterStr[i++]) != 0) { - if (c == matchAll) { /* Match "%" */ - - while ((c = patterStr[i++]) == matchAll || c == matchOne) { - if (c == matchOne && (j >= size || str[j++] == 0)) { - return TSDB_PATTERN_NOWILDCARDMATCH; - } - } - if (c == 0) { - return TSDB_PATTERN_MATCH; - } - - wchar_t accept[3] = {towupper(c), towlower(c), 0}; - while (1) { - size_t n = wcscspn(str, accept); - - str += n; - if (str[0] == 0 || (n >= size)) { - break; - } - - int32_t ret = WCSPatternMatch(&patterStr[i], ++str, size - n - 1, pInfo); - if (ret != TSDB_PATTERN_NOMATCH) { - return ret; - } - } - - return TSDB_PATTERN_NOWILDCARDMATCH; - } - - c1 = str[j++]; - - if (j <= size) { - if (c == c1 || towlower(c) == towlower(c1) || (c == matchOne && c1 != 0)) { - continue; - } - } - - return TSDB_PATTERN_NOMATCH; - } - - return (str[j] == 0 || j >= size) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH; -} int32_t compareStrPatternComp(const void* pLeft, const void* pRight) { SPatternCompareInfo pInfo = {'%', '_'}; @@ -359,62 +33,6 @@ int32_t compareStrPatternComp(const void* pLeft, const void* pRight) { return (ret == TSDB_PATTERN_MATCH) ? 0 : 1; } -int32_t compareStrRegexCompMatch(const void* pLeft, const void* pRight) { - return compareStrRegexComp(pLeft, pRight); -} - -int32_t compareStrRegexCompNMatch(const void* pLeft, const void* pRight) { - return compareStrRegexComp(pLeft, pRight) ? 0 : 1; -} - -int32_t compareStrRegexComp(const void* pLeft, const void* pRight) { - size_t sz = varDataLen(pRight); - char *pattern = malloc(sz + 1); - memcpy(pattern, varDataVal(pRight), varDataLen(pRight)); - pattern[sz] = 0; - - sz = varDataLen(pLeft); - char *str = malloc(sz + 1); - memcpy(str, varDataVal(pLeft), sz); - str[sz] = 0; - - int errCode = 0; - regex_t regex; - char msgbuf[256] = {0}; - - int cflags = REG_EXTENDED; - if ((errCode = regcomp(®ex, pattern, cflags)) != 0) { - regerror(errCode, ®ex, msgbuf, sizeof(msgbuf)); - uError("Failed to compile regex pattern %s. reason %s", pattern, msgbuf); - regfree(®ex); - free(str); - free(pattern); - return 1; - } - - errCode = regexec(®ex, str, 0, NULL, 0); - if (errCode != 0 && errCode != REG_NOMATCH) { - regerror(errCode, ®ex, msgbuf, sizeof(msgbuf)); - uDebug("Failed to match %s with pattern %s, reason %s", str, pattern, msgbuf) - } - int32_t result = (errCode == 0) ? 0 : 1; - regfree(®ex); - free(str); - free(pattern); - return result; -} - -int32_t taosArrayCompareString(const void* a, const void* b) { - const char* x = *(const char**)a; - const char* y = *(const char**)b; - - return compareLenPrefixedStr(x, y); -} - -int32_t compareFindItemInSet(const void *pLeft, const void* pRight) { - return NULL != taosHashGet((SHashObj *)pRight, varDataVal(pLeft), varDataLen(pLeft)) ? 1 : 0; -} - int32_t compareWStrPatternComp(const void* pLeft, const void* pRight) { SPatternCompareInfo pInfo = {'%', '_'}; @@ -600,4 +218,4 @@ int32_t doCompare(const char* f1, const char* f2, int32_t type, size_t size) { } } } -} +} \ No newline at end of file diff --git a/source/server/CMakeLists.txt b/source/server/CMakeLists.txt index 1f6da54ebe..8afa650d36 100644 --- a/source/server/CMakeLists.txt +++ b/source/server/CMakeLists.txt @@ -8,4 +8,5 @@ add_executable(taosd ${TAOSD_SRC}) target_link_libraries( taosd PUBLIC dnode + PUBLIC util ) \ No newline at end of file diff --git a/source/server/vnode/meta/CMakeLists.txt b/source/server/vnode/meta/CMakeLists.txt index 113bcd5d6f..94bf9581d3 100644 --- a/source/server/vnode/meta/CMakeLists.txt +++ b/source/server/vnode/meta/CMakeLists.txt @@ -11,6 +11,6 @@ target_link_libraries( ) target_link_libraries(meta PUBLIC rocksdb) -# if(${BUILD_TEST}) -# add_subdirectory(test) -# endif(${BUILD_TEST}) +if(${BUILD_TEST}) + add_subdirectory(test) +endif(${BUILD_TEST}) diff --git a/source/util/CMakeLists.txt b/source/util/CMakeLists.txt index 09b877ea8e..7c42afcc51 100644 --- a/source/util/CMakeLists.txt +++ b/source/util/CMakeLists.txt @@ -1,5 +1,5 @@ aux_source_directory(src UTIL_SRC) -add_library(util ${UTIL_SRC}) +add_library(util STATIC ${UTIL_SRC}) target_include_directories( util PUBLIC "${CMAKE_SOURCE_DIR}/include/util" diff --git a/source/util/src/compare.c b/source/util/src/compare.c new file mode 100644 index 0000000000..f2d320fde0 --- /dev/null +++ b/source/util/src/compare.c @@ -0,0 +1,398 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#define _BSD_SOURCE +#define _GNU_SOURCE +#define _XOPEN_SOURCE +#define _DEFAULT_SOURCE + +#include "os.h" +#include "types.h" +#include "compare.h" +#include "ulog.h" +#include "thash.h" +#include "regex.h" + +int32_t setCompareBytes1(const void *pLeft, const void *pRight) { + return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0; +} + +int32_t setCompareBytes2(const void *pLeft, const void *pRight) { + return NULL != taosHashGet((SHashObj *)pRight, pLeft, 2) ? 1 : 0; +} + +int32_t setCompareBytes4(const void *pLeft, const void *pRight) { + return NULL != taosHashGet((SHashObj *)pRight, pLeft, 4) ? 1 : 0; +} + +int32_t setCompareBytes8(const void *pLeft, const void *pRight) { + return NULL != taosHashGet((SHashObj *)pRight, pLeft, 8) ? 1 : 0; +} + +int32_t compareInt8Val(const void *pLeft, const void *pRight) { + int8_t left = GET_INT8_VAL(pLeft), right = GET_INT8_VAL(pRight); + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +int32_t compareInt8ValDesc(const void *pLeft, const void *pRight) { + return compareInt8Val(pRight, pLeft); +} + +int32_t compareInt16Val(const void *pLeft, const void *pRight) { + int16_t left = GET_INT16_VAL(pLeft), right = GET_INT16_VAL(pRight); + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +int32_t compareInt16ValDesc(const void* pLeft, const void* pRight) { + return compareInt16Val(pRight, pLeft); +} + +int32_t compareInt32Val(const void *pLeft, const void *pRight) { + int32_t left = GET_INT32_VAL(pLeft), right = GET_INT32_VAL(pRight); + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +int32_t compareInt32ValDesc(const void* pLeft, const void* pRight) { + return compareInt32Val(pRight, pLeft); +} + +int32_t compareInt64Val(const void *pLeft, const void *pRight) { + int64_t left = GET_INT64_VAL(pLeft), right = GET_INT64_VAL(pRight); + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +int32_t compareInt64ValDesc(const void* pLeft, const void* pRight) { + return compareInt64Val(pRight, pLeft); +} + +int32_t compareUint32Val(const void *pLeft, const void *pRight) { + uint32_t left = GET_UINT32_VAL(pLeft), right = GET_UINT32_VAL(pRight); + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +int32_t compareUint32ValDesc(const void* pLeft, const void* pRight) { + return compareUint32Val(pRight, pLeft); +} + +int32_t compareUint64Val(const void *pLeft, const void *pRight) { + uint64_t left = GET_UINT64_VAL(pLeft), right = GET_UINT64_VAL(pRight); + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +int32_t compareUint64ValDesc(const void* pLeft, const void* pRight) { + return compareUint64Val(pRight, pLeft); +} + +int32_t compareUint16Val(const void *pLeft, const void *pRight) { + uint16_t left = GET_UINT16_VAL(pLeft), right = GET_UINT16_VAL(pRight); + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +int32_t compareUint16ValDesc(const void* pLeft, const void* pRight) { + return compareUint16Val(pRight, pLeft); +} + +int32_t compareUint8Val(const void* pLeft, const void* pRight) { + uint8_t left = GET_UINT8_VAL(pLeft), right = GET_UINT8_VAL(pRight); + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +int32_t compareUint8ValDesc(const void* pLeft, const void* pRight) { + return compareUint8Val(pRight, pLeft); +} + +int32_t compareFloatVal(const void *pLeft, const void *pRight) { + float p1 = GET_FLOAT_VAL(pLeft); + float p2 = GET_FLOAT_VAL(pRight); + + if (isnan(p1) && isnan(p2)) { + return 0; + } + + if (isnan(p1)) { + return -1; + } + + if (isnan(p2)) { + return 1; + } + if (FLT_EQUAL(p1, p2)) { + return 0; + } + return FLT_GREATER(p1, p2) ? 1: -1; +} + +int32_t compareFloatValDesc(const void* pLeft, const void* pRight) { + return compareFloatVal(pRight, pLeft); +} + +int32_t compareDoubleVal(const void *pLeft, const void *pRight) { + double p1 = GET_DOUBLE_VAL(pLeft); + double p2 = GET_DOUBLE_VAL(pRight); + + if (isnan(p1) && isnan(p2)) { + return 0; + } + + if (isnan(p1)) { + return -1; + } + + if (isnan(p2)) { + return 1; + } + if (FLT_EQUAL(p1, p2)) { + return 0; + } + return FLT_GREATER(p1, p2) ? 1: -1; +} + +int32_t compareDoubleValDesc(const void* pLeft, const void* pRight) { + return compareDoubleVal(pRight, pLeft); +} + +int32_t compareLenPrefixedStr(const void *pLeft, const void *pRight) { + int32_t len1 = varDataLen(pLeft); + int32_t len2 = varDataLen(pRight); + + if (len1 != len2) { + return len1 > len2? 1:-1; + } else { + int32_t ret = strncmp(varDataVal(pLeft), varDataVal(pRight), len1); + if (ret == 0) { + return 0; + } else { + return ret > 0 ? 1:-1; + } + } +} + +int32_t compareLenPrefixedStrDesc(const void* pLeft, const void* pRight) { + return compareLenPrefixedStr(pRight, pLeft); +} + +int32_t compareLenPrefixedWStr(const void *pLeft, const void *pRight) { + int32_t len1 = varDataLen(pLeft); + int32_t len2 = varDataLen(pRight); + + if (len1 != len2) { + return len1 > len2? 1:-1; + } else { + int32_t ret = memcmp((wchar_t*) pLeft, (wchar_t*) pRight, len1); + if (ret == 0) { + return 0; + } else { + return ret > 0 ? 1 : -1; + } + } +} + +int32_t compareLenPrefixedWStrDesc(const void* pLeft, const void* pRight) { + return compareLenPrefixedWStr(pRight, pLeft); +} + +/* + * Compare two strings + * TSDB_MATCH: Match + * TSDB_NOMATCH: No match + * TSDB_NOWILDCARDMATCH: No match in spite of having * or % wildcards. + * Like matching rules: + * '%': Matches zero or more characters + * '_': Matches one character + * + */ +int patternMatch(const char *patterStr, const char *str, size_t size, const SPatternCompareInfo *pInfo) { + char c, c1; + + int32_t i = 0; + int32_t j = 0; + int32_t o = 0; + int32_t m = 0; + + while ((c = patterStr[i++]) != 0) { + if (c == pInfo->matchAll) { /* Match "*" */ + + while ((c = patterStr[i++]) == pInfo->matchAll || c == pInfo->matchOne) { + if (c == pInfo->matchOne) { + if (j > size || str[j++] == 0) { + // empty string, return not match + return TSDB_PATTERN_NOWILDCARDMATCH; + } else { + ++o; + } + } + } + + if (c == 0) { + return TSDB_PATTERN_MATCH; /* "*" at the end of the pattern matches */ + } + + char next[3] = {toupper(c), tolower(c), 0}; + m = o; + while (1) { + size_t n = strcspn(str + m, next); + str += m + n; + + if (str[0] == 0 || (n >= size)) { + break; + } + + int32_t ret = patternMatch(&patterStr[i], ++str, size - n - 1, pInfo); + if (ret != TSDB_PATTERN_NOMATCH) { + return ret; + } + m = 0; + } + return TSDB_PATTERN_NOWILDCARDMATCH; + } + + c1 = str[j++]; + ++o; + + if (j <= size) { + if (c == '\\' && patterStr[i] == '_' && c1 == '_') { i++; continue; } + if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) { + continue; + } + } + + return TSDB_PATTERN_NOMATCH; + } + + return (str[j] == 0 || j >= size) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH; +} + +int WCSPatternMatch(const wchar_t *patterStr, const wchar_t *str, size_t size, const SPatternCompareInfo *pInfo) { + wchar_t c, c1; + wchar_t matchOne = L'_'; // "_" + wchar_t matchAll = L'%'; // "%" + + int32_t i = 0; + int32_t j = 0; + + while ((c = patterStr[i++]) != 0) { + if (c == matchAll) { /* Match "%" */ + + while ((c = patterStr[i++]) == matchAll || c == matchOne) { + if (c == matchOne && (j >= size || str[j++] == 0)) { + return TSDB_PATTERN_NOWILDCARDMATCH; + } + } + if (c == 0) { + return TSDB_PATTERN_MATCH; + } + + wchar_t accept[3] = {towupper(c), towlower(c), 0}; + while (1) { + size_t n = wcscspn(str, accept); + + str += n; + if (str[0] == 0 || (n >= size)) { + break; + } + + int32_t ret = WCSPatternMatch(&patterStr[i], ++str, size - n - 1, pInfo); + if (ret != TSDB_PATTERN_NOMATCH) { + return ret; + } + } + + return TSDB_PATTERN_NOWILDCARDMATCH; + } + + c1 = str[j++]; + + if (j <= size) { + if (c == c1 || towlower(c) == towlower(c1) || (c == matchOne && c1 != 0)) { + continue; + } + } + + return TSDB_PATTERN_NOMATCH; + } + + return (str[j] == 0 || j >= size) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH; +} + +int32_t compareStrRegexCompMatch(const void* pLeft, const void* pRight) { + return compareStrRegexComp(pLeft, pRight); +} + +int32_t compareStrRegexCompNMatch(const void* pLeft, const void* pRight) { + return compareStrRegexComp(pLeft, pRight) ? 0 : 1; +} + +int32_t compareStrRegexComp(const void* pLeft, const void* pRight) { + size_t sz = varDataLen(pRight); + char *pattern = malloc(sz + 1); + memcpy(pattern, varDataVal(pRight), varDataLen(pRight)); + pattern[sz] = 0; + + sz = varDataLen(pLeft); + char *str = malloc(sz + 1); + memcpy(str, varDataVal(pLeft), sz); + str[sz] = 0; + + int errCode = 0; + regex_t regex; + char msgbuf[256] = {0}; + + int cflags = REG_EXTENDED; + if ((errCode = regcomp(®ex, pattern, cflags)) != 0) { + regerror(errCode, ®ex, msgbuf, sizeof(msgbuf)); + uError("Failed to compile regex pattern %s. reason %s", pattern, msgbuf); + regfree(®ex); + free(str); + free(pattern); + return 1; + } + + errCode = regexec(®ex, str, 0, NULL, 0); + if (errCode != 0 && errCode != REG_NOMATCH) { + regerror(errCode, ®ex, msgbuf, sizeof(msgbuf)); + uDebug("Failed to match %s with pattern %s, reason %s", str, pattern, msgbuf) + } + int32_t result = (errCode == 0) ? 0 : 1; + regfree(®ex); + free(str); + free(pattern); + return result; +} + +int32_t taosArrayCompareString(const void* a, const void* b) { + const char* x = *(const char**)a; + const char* y = *(const char**)b; + + return compareLenPrefixedStr(x, y); +} + +int32_t compareFindItemInSet(const void *pLeft, const void* pRight) { + return NULL != taosHashGet((SHashObj *)pRight, varDataVal(pLeft), varDataLen(pLeft)) ? 1 : 0; +} diff --git a/source/common/src/terror.c b/source/util/src/terror.c similarity index 100% rename from source/common/src/terror.c rename to source/util/src/terror.c diff --git a/source/util/src/thashutil.c b/source/util/src/thashutil.c index 83f85bfdbc..7deb4d5a78 100644 --- a/source/util/src/thashutil.c +++ b/source/util/src/thashutil.c @@ -15,8 +15,9 @@ #include "os.h" #include "thash.h" -#include "tcompare.h" +#include "compare.h" #include "tdef.h" +#include "types.h" #define ROTL32(x, r) ((x) << (r) | (x) >> (32u - (r))) diff --git a/source/util/src/tskiplist.c b/source/util/src/tskiplist.c index d02b148863..00263d7bce 100644 --- a/source/util/src/tskiplist.c +++ b/source/util/src/tskiplist.c @@ -15,7 +15,7 @@ */ #include "tskiplist.h" #include "os.h" -#include "tcompare.h" +#include "compare.h" #include "ulog.h" #include "tutil.h" diff --git a/source/util/src/tutil.c b/source/util/src/tutil.c index c5027af7c7..22e378d067 100644 --- a/source/util/src/tutil.c +++ b/source/util/src/tutil.c @@ -450,24 +450,3 @@ char *taosIpStr(uint32_t ipInt) { return ipStr; } -FORCE_INLINE float taos_align_get_float(const char* pBuf) { -#if __STDC_VERSION__ >= 201112L - static_assert(sizeof(float) == sizeof(uint32_t), "sizeof(float) must equal to sizeof(uint32_t)"); -#else - assert(sizeof(float) == sizeof(uint32_t)); -#endif - float fv = 0; - memcpy(&fv, pBuf, sizeof(fv)); // in ARM, return *((const float*)(pBuf)) may cause problem - return fv; -} - -FORCE_INLINE double taos_align_get_double(const char* pBuf) { -#if __STDC_VERSION__ >= 201112L - static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) must equal to sizeof(uint64_t)"); -#else - assert(sizeof(double) == sizeof(uint64_t)); -#endif - double dv = 0; - memcpy(&dv, pBuf, sizeof(dv)); // in ARM, return *((const double*)(pBuf)) may cause problem - return dv; -} -- GitLab