提交 0729144b 编写于 作者: D dapan1121

Merge remote-tracking branch 'origin/3.0' into feature/qnode

...@@ -26,6 +26,7 @@ int32_t taosMkDir(const char *dirname); ...@@ -26,6 +26,7 @@ int32_t taosMkDir(const char *dirname);
void taosRemoveOldFiles(const char *dirname, int32_t keepDays); void taosRemoveOldFiles(const char *dirname, int32_t keepDays);
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen); int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen);
int32_t taosRealPath(char *dirname, int32_t maxlen); int32_t taosRealPath(char *dirname, int32_t maxlen);
bool taosIsDir(const char *dirname);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -33,21 +33,26 @@ typedef struct { ...@@ -33,21 +33,26 @@ typedef struct {
SDiskSize size; SDiskSize size;
} SDiskSpace; } SDiskSpace;
int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize);
int32_t taosGetCpuCores();
void taosGetSystemInfo(); void taosGetSystemInfo();
bool taosGetEmail(char *email, int32_t maxLen);
bool taosGetOsReleaseName(char *releaseName, int32_t maxLen);
bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores);
int32_t taosGetCpuCores();
bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage);
bool taosGetTotalSysMemoryKB(uint64_t *kb);
bool taosGetProcMemory(float *memoryUsedMB); //
bool taosGetSysMemory(float *memoryUsedMB); //
void taosGetDisk();
int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize);
bool taosReadProcIO(int64_t *rchars, int64_t *wchars); bool taosReadProcIO(int64_t *rchars, int64_t *wchars);
bool taosGetProcIO(float *readKB, float *writeKB); bool taosGetProcIO(float *readKB, float *writeKB);
bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes);
bool taosGetBandSpeed(float *bandSpeedKb); bool taosGetBandSpeed(float *bandSpeedKb);
void taosGetDisk();
bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage); int32_t taosSystem(const char *cmd);
bool taosGetProcMemory(float *memoryUsedMB);
bool taosGetSysMemory(float *memoryUsedMB);
int taosSystem(const char *cmd);
void taosKillSystem(); void taosKillSystem();
int32_t taosGetSystemUUID(char *uid, int32_t uidlen); int32_t taosGetSystemUUID(char *uid, int32_t uidlen);
char * taosGetCmdlineByPID(int pid); char *taosGetCmdlineByPID(int32_t pid);
void taosSetCoreDump(bool enable); void taosSetCoreDump(bool enable);
typedef struct { typedef struct {
......
...@@ -82,7 +82,7 @@ typedef struct SConfig { ...@@ -82,7 +82,7 @@ typedef struct SConfig {
SConfig *cfgInit(); SConfig *cfgInit();
int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr); int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr);
int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs); // SConfigPair int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs); // SConfigPair
void cfgCleanup(SConfig *pCfg); void cfgCleanup(SConfig *pCfg);
int32_t cfgGetSize(SConfig *pCfg); int32_t cfgGetSize(SConfig *pCfg);
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_UTIL_HTTP_H_
#define _TD_UTIL_HTTP_H_
#include "os.h"
#ifdef __cplusplus
extern "C" {
#endif
int32_t taosSendHttpReport(const char* server, uint16_t port, const char* pCont, int32_t contLen);
#ifdef __cplusplus
}
#endif
#endif /*_TD_UTIL_UTIL_H_*/
...@@ -177,24 +177,24 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *e ...@@ -177,24 +177,24 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *e
snprintf(cfgFile, sizeof(cfgFile), "%s" TD_DIRSEP "taos.cfg", cfgDir); snprintf(cfgFile, sizeof(cfgFile), "%s" TD_DIRSEP "taos.cfg", cfgDir);
if (cfgLoad(pCfg, CFG_STYPE_APOLLO_URL, apolloUrl) != 0) { if (cfgLoad(pCfg, CFG_STYPE_APOLLO_URL, apolloUrl) != 0) {
uError("failed to load from apollo url:%s since %s\n", apolloUrl, terrstr()); uError("failed to load from apollo url:%s since %s", apolloUrl, terrstr());
return -1; return -1;
} }
if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgFile) != 0) { if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgDir) != 0) {
if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgDir) != 0) { if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgFile) != 0) {
uError("failed to load from config file:%s since %s\n", cfgFile, terrstr()); uError("failed to load from config file:%s since %s", cfgFile, terrstr());
return -1; return 0;
} }
} }
if (cfgLoad(pCfg, CFG_STYPE_ENV_FILE, envFile) != 0) { if (cfgLoad(pCfg, CFG_STYPE_ENV_FILE, envFile) != 0) {
uError("failed to load from env file:%s since %s\n", envFile, terrstr()); uError("failed to load from env file:%s since %s", envFile, terrstr());
return -1; return -1;
} }
if (cfgLoad(pCfg, CFG_STYPE_ENV_VAR, NULL) != 0) { if (cfgLoad(pCfg, CFG_STYPE_ENV_VAR, NULL) != 0) {
uError("failed to load from global env variables since %s\n", terrstr()); uError("failed to load from global env variables since %s", terrstr());
return -1; return -1;
} }
...@@ -438,8 +438,10 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi ...@@ -438,8 +438,10 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi
if (pCfg == NULL) return -1; if (pCfg == NULL) return -1;
if (tsc) { if (tsc) {
tscEmbeddedInUtil = 0;
if (taosAddClientLogCfg(pCfg) != 0) return -1; if (taosAddClientLogCfg(pCfg) != 0) return -1;
} else { } else {
tscEmbeddedInUtil = 1;
if (taosAddClientLogCfg(pCfg) != 0) return -1; if (taosAddClientLogCfg(pCfg) != 0) return -1;
if (taosAddServerLogCfg(pCfg) != 0) return -1; if (taosAddServerLogCfg(pCfg) != 0) return -1;
} }
...@@ -450,7 +452,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi ...@@ -450,7 +452,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi
return -1; return -1;
} }
if (cfgLoadArray(pCfg, pArgs) != 0) { if (cfgLoadFromArray(pCfg, pArgs) != 0) {
uError("failed to load cfg from array since %s", terrstr()); uError("failed to load cfg from array since %s", terrstr());
cfgCleanup(pCfg); cfgCleanup(pCfg);
return -1; return -1;
...@@ -465,8 +467,14 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi ...@@ -465,8 +467,14 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi
taosSetAllDebugFlag(cfgGetItem(pCfg, "debugFlag")->i32); taosSetAllDebugFlag(cfgGetItem(pCfg, "debugFlag")->i32);
if (taosMkDir(tsLogDir) != 0) {
uError("failed to create dir:%s since %s", tsLogDir, terrstr());
cfgCleanup(pCfg);
return -1;
}
if (taosInitLog(logname, logFileNum) != 0) { if (taosInitLog(logname, logFileNum) != 0) {
printf("failed to init log file since %s\n", terrstr()); uError("failed to init log file since %s", terrstr());
cfgCleanup(pCfg); cfgCleanup(pCfg);
return -1; return -1;
} }
...@@ -497,7 +505,7 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU ...@@ -497,7 +505,7 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU
return -1; return -1;
} }
if (cfgLoadArray(tsCfg, pArgs) != 0) { if (cfgLoadFromArray(tsCfg, pArgs) != 0) {
uError("failed to load cfg from array since %s", terrstr()); uError("failed to load cfg from array since %s", terrstr());
cfgCleanup(tsCfg); cfgCleanup(tsCfg);
return -1; return -1;
...@@ -512,6 +520,16 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU ...@@ -512,6 +520,16 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU
} }
taosSetSystemCfg(tsCfg); taosSetSystemCfg(tsCfg);
if (taosMkDir(tsTempDir) != 0) {
uError("failed to create dir:%s since %s", tsTempDir, terrstr());
return -1;
}
if (!tsc && taosMkDir(tsDataDir) != 0) {
uError("failed to create dir:%s since %s", tsDataDir, terrstr());
return -1;
}
cfgDumpCfg(tsCfg, tsc, false); cfgDumpCfg(tsCfg, tsc, false);
return 0; return 0;
} }
......
...@@ -49,16 +49,16 @@ const int32_t TYPE_BYTES[15] = { ...@@ -49,16 +49,16 @@ const int32_t TYPE_BYTES[15] = {
} \ } \
} while (0) } while (0)
static void getStatics_bool(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, static void getStatics_bool(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
int8_t *data = (int8_t *)pData; int8_t *data = (int8_t *)pData;
*min = INT64_MAX; *min = INT64_MAX;
*max = INT64_MIN; *max = INT64_MIN;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if (data[i] == TSDB_DATA_BOOL_NULL) { if (data[i] == TSDB_DATA_BOOL_NULL) {
(*numOfNull) += 1; (*numOfNull) += 1;
...@@ -76,9 +76,9 @@ static void getStatics_i8(const void *pData, int32_t numOfRow, int64_t *min, int ...@@ -76,9 +76,9 @@ static void getStatics_i8(const void *pData, int32_t numOfRow, int64_t *min, int
*max = INT64_MIN; *max = INT64_MIN;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if (((uint8_t)data[i]) == TSDB_DATA_TINYINT_NULL) { if (((uint8_t)data[i]) == TSDB_DATA_TINYINT_NULL) {
(*numOfNull) += 1; (*numOfNull) += 1;
...@@ -122,9 +122,9 @@ static void getStatics_i16(const void *pData, int32_t numOfRow, int64_t *min, in ...@@ -122,9 +122,9 @@ static void getStatics_i16(const void *pData, int32_t numOfRow, int64_t *min, in
*max = INT64_MIN; *max = INT64_MIN;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if (((uint16_t)data[i]) == TSDB_DATA_SMALLINT_NULL) { if (((uint16_t)data[i]) == TSDB_DATA_SMALLINT_NULL) {
(*numOfNull) += 1; (*numOfNull) += 1;
...@@ -133,15 +133,14 @@ static void getStatics_i16(const void *pData, int32_t numOfRow, int64_t *min, in ...@@ -133,15 +133,14 @@ static void getStatics_i16(const void *pData, int32_t numOfRow, int64_t *min, in
DO_STATICS(*sum, *min, *max, *minIndex, *maxIndex, data, i); DO_STATICS(*sum, *min, *max, *minIndex, *maxIndex, data, i);
} }
} }
static void getStatics_u16(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, static void getStatics_u16(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
uint16_t *data = (uint16_t *)pData; uint16_t *data = (uint16_t *)pData;
uint64_t _min = UINT64_MAX; uint64_t _min = UINT64_MAX;
uint64_t _max = 0; uint64_t _max = 0;
uint64_t _sum = 0; uint64_t _sum = 0;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
...@@ -169,9 +168,9 @@ static void getStatics_i32(const void *pData, int32_t numOfRow, int64_t *min, in ...@@ -169,9 +168,9 @@ static void getStatics_i32(const void *pData, int32_t numOfRow, int64_t *min, in
*max = INT64_MIN; *max = INT64_MIN;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if (((uint32_t)data[i]) == TSDB_DATA_INT_NULL) { if (((uint32_t)data[i]) == TSDB_DATA_INT_NULL) {
(*numOfNull) += 1; (*numOfNull) += 1;
...@@ -182,12 +181,12 @@ static void getStatics_i32(const void *pData, int32_t numOfRow, int64_t *min, in ...@@ -182,12 +181,12 @@ static void getStatics_i32(const void *pData, int32_t numOfRow, int64_t *min, in
} }
} }
static void getStatics_u32(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, static void getStatics_u32(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
uint32_t *data = (uint32_t *)pData; uint32_t *data = (uint32_t *)pData;
uint64_t _min = UINT64_MAX; uint64_t _min = UINT64_MAX;
uint64_t _max = 0; uint64_t _max = 0;
uint64_t _sum = 0; uint64_t _sum = 0;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
...@@ -208,16 +207,16 @@ static void getStatics_u32(const void *pData, int32_t numOfRow, int64_t *min, in ...@@ -208,16 +207,16 @@ static void getStatics_u32(const void *pData, int32_t numOfRow, int64_t *min, in
*sum = _sum; *sum = _sum;
} }
static void getStatics_i64(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, static void getStatics_i64(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
int64_t *data = (int64_t *)pData; int64_t *data = (int64_t *)pData;
*min = INT64_MAX; *min = INT64_MAX;
*max = INT64_MIN; *max = INT64_MIN;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if (((uint64_t)data[i]) == TSDB_DATA_BIGINT_NULL) { if (((uint64_t)data[i]) == TSDB_DATA_BIGINT_NULL) {
(*numOfNull) += 1; (*numOfNull) += 1;
...@@ -228,12 +227,12 @@ static void getStatics_i64(const void *pData, int32_t numOfRow, int64_t *min, in ...@@ -228,12 +227,12 @@ static void getStatics_i64(const void *pData, int32_t numOfRow, int64_t *min, in
} }
} }
static void getStatics_u64(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, static void getStatics_u64(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
uint64_t *data = (uint64_t *)pData; uint64_t *data = (uint64_t *)pData;
uint64_t _min = UINT64_MAX; uint64_t _min = UINT64_MAX;
uint64_t _max = 0; uint64_t _max = 0;
uint64_t _sum = 0; uint64_t _sum = 0;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
...@@ -254,91 +253,91 @@ static void getStatics_u64(const void *pData, int32_t numOfRow, int64_t *min, in ...@@ -254,91 +253,91 @@ static void getStatics_u64(const void *pData, int32_t numOfRow, int64_t *min, in
*sum = _sum; *sum = _sum;
} }
static void getStatics_f(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, static void getStatics_f(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
float *data = (float *)pData; float *data = (float *)pData;
float fmin = FLT_MAX; float fmin = FLT_MAX;
float fmax = -FLT_MAX; float fmax = -FLT_MAX;
double dsum = 0; double dsum = 0;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if ((*(uint32_t*)&(data[i])) == TSDB_DATA_FLOAT_NULL) { if ((*(uint32_t *)&(data[i])) == TSDB_DATA_FLOAT_NULL) {
(*numOfNull) += 1; (*numOfNull) += 1;
continue; continue;
} }
float fv = GET_FLOAT_VAL((const char*)&(data[i])); float fv = GET_FLOAT_VAL((const char *)&(data[i]));
dsum += fv; dsum += fv;
if (fmin > fv) { if (fmin > fv) {
fmin = fv; fmin = fv;
*minIndex = i; *minIndex = i;
} }
if (fmax < fv) { if (fmax < fv) {
fmax = fv; fmax = fv;
*maxIndex = i; *maxIndex = i;
} }
} }
SET_DOUBLE_VAL(sum, dsum); SET_DOUBLE_VAL(sum, dsum);
SET_DOUBLE_VAL(max, fmax); SET_DOUBLE_VAL(max, fmax);
SET_DOUBLE_VAL(min, fmin); SET_DOUBLE_VAL(min, fmin);
} }
static void getStatics_d(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, static void getStatics_d(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
double *data = (double *)pData; double *data = (double *)pData;
double dmin = DBL_MAX; double dmin = DBL_MAX;
double dmax = -DBL_MAX; double dmax = -DBL_MAX;
double dsum = 0; double dsum = 0;
*minIndex = 0; *minIndex = 0;
*maxIndex = 0; *maxIndex = 0;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if ((*(uint64_t*)&(data[i])) == TSDB_DATA_DOUBLE_NULL) { if ((*(uint64_t *)&(data[i])) == TSDB_DATA_DOUBLE_NULL) {
(*numOfNull) += 1; (*numOfNull) += 1;
continue; continue;
} }
double dv = 0; double dv = 0;
dv = GET_DOUBLE_VAL((const char*)&(data[i])); dv = GET_DOUBLE_VAL((const char *)&(data[i]));
dsum += dv; dsum += dv;
if (dmin > dv) { if (dmin > dv) {
dmin = dv; dmin = dv;
*minIndex = i; *minIndex = i;
} }
if (dmax < dv) { if (dmax < dv) {
dmax = dv; dmax = dv;
*maxIndex = i; *maxIndex = i;
} }
} }
SET_DOUBLE_PTR(sum, &dsum); SET_DOUBLE_PTR(sum, &dsum);
SET_DOUBLE_PTR(max, &dmax); SET_DOUBLE_PTR(max, &dmax);
SET_DOUBLE_PTR(min, &dmin); SET_DOUBLE_PTR(min, &dmin);
} }
static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
const char* data = pData; const char *data = pData;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if (isNull(data, TSDB_DATA_TYPE_BINARY)) { if (isNull(data, TSDB_DATA_TYPE_BINARY)) {
(*numOfNull) += 1; (*numOfNull) += 1;
} }
data += varDataTLen(data); data += varDataTLen(data);
} }
*sum = 0; *sum = 0;
*max = 0; *max = 0;
*min = 0; *min = 0;
...@@ -346,19 +345,19 @@ static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, in ...@@ -346,19 +345,19 @@ static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, in
*maxIndex = 0; *maxIndex = 0;
} }
static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum,
int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) {
const char* data = pData; const char *data = pData;
assert(numOfRow <= INT16_MAX); assert(numOfRow <= INT16_MAX);
for (int32_t i = 0; i < numOfRow; ++i) { for (int32_t i = 0; i < numOfRow; ++i) {
if (isNull(data, TSDB_DATA_TYPE_NCHAR)) { if (isNull(data, TSDB_DATA_TYPE_NCHAR)) {
(*numOfNull) += 1; (*numOfNull) += 1;
} }
data += varDataTLen(data); data += varDataTLen(data);
} }
*sum = 0; *sum = 0;
*max = 0; *max = 0;
*min = 0; *min = 0;
...@@ -367,21 +366,28 @@ static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, i ...@@ -367,21 +366,28 @@ static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, i
} }
tDataTypeDescriptor tDataTypes[15] = { tDataTypeDescriptor tDataTypes[15] = {
{TSDB_DATA_TYPE_NULL, 6, 1, "NOTYPE", 0, 0, NULL, NULL, NULL}, {TSDB_DATA_TYPE_NULL, 6, 1, "NOTYPE", 0, 0, NULL, NULL, NULL},
{TSDB_DATA_TYPE_BOOL, 4, CHAR_BYTES, "BOOL", false, true, tsCompressBool, tsDecompressBool, getStatics_bool}, {TSDB_DATA_TYPE_BOOL, 4, CHAR_BYTES, "BOOL", false, true, tsCompressBool, tsDecompressBool, getStatics_bool},
{TSDB_DATA_TYPE_TINYINT, 7, CHAR_BYTES, "TINYINT", INT8_MIN, INT8_MAX, tsCompressTinyint, tsDecompressTinyint, getStatics_i8}, {TSDB_DATA_TYPE_TINYINT, 7, CHAR_BYTES, "TINYINT", INT8_MIN, INT8_MAX, tsCompressTinyint, tsDecompressTinyint,
{TSDB_DATA_TYPE_SMALLINT, 8, SHORT_BYTES, "SMALLINT", INT16_MIN, INT16_MAX, tsCompressSmallint, tsDecompressSmallint, getStatics_i16}, getStatics_i8},
{TSDB_DATA_TYPE_INT, 3, INT_BYTES, "INT", INT32_MIN, INT32_MAX, tsCompressInt, tsDecompressInt, getStatics_i32}, {TSDB_DATA_TYPE_SMALLINT, 8, SHORT_BYTES, "SMALLINT", INT16_MIN, INT16_MAX, tsCompressSmallint,
{TSDB_DATA_TYPE_BIGINT, 6, LONG_BYTES, "BIGINT", INT64_MIN, INT64_MAX, tsCompressBigint, tsDecompressBigint, getStatics_i64}, tsDecompressSmallint, getStatics_i16},
{TSDB_DATA_TYPE_FLOAT, 5, FLOAT_BYTES, "FLOAT", 0, 0, tsCompressFloat, tsDecompressFloat, getStatics_f}, {TSDB_DATA_TYPE_INT, 3, INT_BYTES, "INT", INT32_MIN, INT32_MAX, tsCompressInt, tsDecompressInt, getStatics_i32},
{TSDB_DATA_TYPE_DOUBLE, 6, DOUBLE_BYTES, "DOUBLE", 0, 0, tsCompressDouble, tsDecompressDouble, getStatics_d}, {TSDB_DATA_TYPE_BIGINT, 6, LONG_BYTES, "BIGINT", INT64_MIN, INT64_MAX, tsCompressBigint, tsDecompressBigint,
{TSDB_DATA_TYPE_BINARY, 6, 0, "BINARY", 0, 0, tsCompressString, tsDecompressString, getStatics_bin}, getStatics_i64},
{TSDB_DATA_TYPE_TIMESTAMP, 9, LONG_BYTES, "TIMESTAMP", INT64_MIN, INT64_MAX, tsCompressTimestamp, tsDecompressTimestamp, getStatics_i64}, {TSDB_DATA_TYPE_FLOAT, 5, FLOAT_BYTES, "FLOAT", 0, 0, tsCompressFloat, tsDecompressFloat, getStatics_f},
{TSDB_DATA_TYPE_NCHAR, 5, 8, "NCHAR", 0, 0, tsCompressString, tsDecompressString, getStatics_nchr}, {TSDB_DATA_TYPE_DOUBLE, 6, DOUBLE_BYTES, "DOUBLE", 0, 0, tsCompressDouble, tsDecompressDouble, getStatics_d},
{TSDB_DATA_TYPE_UTINYINT, 16, CHAR_BYTES, "TINYINT UNSIGNED", 0, UINT8_MAX, tsCompressTinyint, tsDecompressTinyint, getStatics_u8}, {TSDB_DATA_TYPE_BINARY, 6, 0, "BINARY", 0, 0, tsCompressString, tsDecompressString, getStatics_bin},
{TSDB_DATA_TYPE_USMALLINT, 17, SHORT_BYTES, "SMALLINT UNSIGNED", 0, UINT16_MAX, tsCompressSmallint, tsDecompressSmallint, getStatics_u16}, {TSDB_DATA_TYPE_TIMESTAMP, 9, LONG_BYTES, "TIMESTAMP", INT64_MIN, INT64_MAX, tsCompressTimestamp,
{TSDB_DATA_TYPE_UINT, 12, INT_BYTES, "INT UNSIGNED", 0, UINT32_MAX, tsCompressInt, tsDecompressInt, getStatics_u32}, tsDecompressTimestamp, getStatics_i64},
{TSDB_DATA_TYPE_UBIGINT, 15, LONG_BYTES, "BIGINT UNSIGNED", 0, UINT64_MAX, tsCompressBigint, tsDecompressBigint, getStatics_u64}, {TSDB_DATA_TYPE_NCHAR, 5, 8, "NCHAR", 0, 0, tsCompressString, tsDecompressString, getStatics_nchr},
{TSDB_DATA_TYPE_UTINYINT, 16, CHAR_BYTES, "TINYINT UNSIGNED", 0, UINT8_MAX, tsCompressTinyint, tsDecompressTinyint,
getStatics_u8},
{TSDB_DATA_TYPE_USMALLINT, 17, SHORT_BYTES, "SMALLINT UNSIGNED", 0, UINT16_MAX, tsCompressSmallint,
tsDecompressSmallint, getStatics_u16},
{TSDB_DATA_TYPE_UINT, 12, INT_BYTES, "INT UNSIGNED", 0, UINT32_MAX, tsCompressInt, tsDecompressInt, getStatics_u32},
{TSDB_DATA_TYPE_UBIGINT, 15, LONG_BYTES, "BIGINT UNSIGNED", 0, UINT64_MAX, tsCompressBigint, tsDecompressBigint,
getStatics_u64},
}; };
char tTokenTypeSwitcher[13] = { char tTokenTypeSwitcher[13] = {
...@@ -400,10 +406,10 @@ char tTokenTypeSwitcher[13] = { ...@@ -400,10 +406,10 @@ char tTokenTypeSwitcher[13] = {
TSDB_DATA_TYPE_NCHAR, // TK_NCHAR TSDB_DATA_TYPE_NCHAR, // TK_NCHAR
}; };
float floatMin = -FLT_MAX, floatMax = FLT_MAX; float floatMin = -FLT_MAX, floatMax = FLT_MAX;
double doubleMin = -DBL_MAX, doubleMax = DBL_MAX; double doubleMin = -DBL_MAX, doubleMax = DBL_MAX;
FORCE_INLINE void* getDataMin(int32_t type) { FORCE_INLINE void *getDataMin(int32_t type) {
switch (type) { switch (type) {
case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_FLOAT:
return &floatMin; return &floatMin;
...@@ -414,7 +420,7 @@ FORCE_INLINE void* getDataMin(int32_t type) { ...@@ -414,7 +420,7 @@ FORCE_INLINE void* getDataMin(int32_t type) {
} }
} }
FORCE_INLINE void* getDataMax(int32_t type) { FORCE_INLINE void *getDataMax(int32_t type) {
switch (type) { switch (type) {
case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_FLOAT:
return &floatMax; return &floatMax;
...@@ -425,17 +431,15 @@ FORCE_INLINE void* getDataMax(int32_t type) { ...@@ -425,17 +431,15 @@ FORCE_INLINE void* getDataMax(int32_t type) {
} }
} }
bool isValidDataType(int32_t type) { bool isValidDataType(int32_t type) { return type >= TSDB_DATA_TYPE_NULL && type <= TSDB_DATA_TYPE_UBIGINT; }
return type >= TSDB_DATA_TYPE_NULL && type <= TSDB_DATA_TYPE_UBIGINT;
}
void setVardataNull(void* val, int32_t type) { void setVardataNull(void *val, int32_t type) {
if (type == TSDB_DATA_TYPE_BINARY) { if (type == TSDB_DATA_TYPE_BINARY) {
varDataSetLen(val, sizeof(int8_t)); varDataSetLen(val, sizeof(int8_t));
*(uint8_t*) varDataVal(val) = TSDB_DATA_BINARY_NULL; *(uint8_t *)varDataVal(val) = TSDB_DATA_BINARY_NULL;
} else if (type == TSDB_DATA_TYPE_NCHAR) { } else if (type == TSDB_DATA_TYPE_NCHAR) {
varDataSetLen(val, sizeof(int32_t)); varDataSetLen(val, sizeof(int32_t));
*(uint32_t*) varDataVal(val) = TSDB_DATA_NCHAR_NULL; *(uint32_t *)varDataVal(val) = TSDB_DATA_NCHAR_NULL;
} else { } else {
assert(0); assert(0);
} }
...@@ -533,9 +537,8 @@ static SBinaryNullT nullBinary = {1, TSDB_DATA_BINARY_NULL}; ...@@ -533,9 +537,8 @@ static SBinaryNullT nullBinary = {1, TSDB_DATA_BINARY_NULL};
static SNCharNullT nullNchar = {4, TSDB_DATA_NCHAR_NULL}; static SNCharNullT nullNchar = {4, TSDB_DATA_NCHAR_NULL};
static const void *nullValues[] = { static const void *nullValues[] = {
&nullBool, &nullTinyInt, &nullSmallInt, &nullInt, &nullBigInt, &nullBool, &nullTinyInt, &nullSmallInt, &nullInt, &nullBigInt, &nullFloat, &nullDouble,
&nullFloat, &nullDouble, &nullBinary, &nullBigInt, &nullNchar, &nullBinary, &nullBigInt, &nullNchar, &nullTinyIntu, &nullSmallIntu, &nullIntu, &nullBigIntu,
&nullTinyIntu, &nullSmallIntu, &nullIntu, &nullBigIntu,
}; };
const void *getNullValue(int32_t type) { const void *getNullValue(int32_t type) {
...@@ -590,7 +593,7 @@ void operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type) { ...@@ -590,7 +593,7 @@ void operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type) {
*((int8_t *)dst) = GET_INT8_VAL(s1) + GET_INT8_VAL(s2); *((int8_t *)dst) = GET_INT8_VAL(s1) + GET_INT8_VAL(s2);
break; break;
case TSDB_DATA_TYPE_UTINYINT: case TSDB_DATA_TYPE_UTINYINT:
*((uint8_t *)dst) = GET_UINT8_VAL(s1) + GET_UINT8_VAL(s2); *((uint8_t *)dst) = GET_UINT8_VAL(s1) + GET_UINT8_VAL(s2);
break; break;
case TSDB_DATA_TYPE_SMALLINT: case TSDB_DATA_TYPE_SMALLINT:
*((int16_t *)dst) = GET_INT16_VAL(s1) + GET_INT16_VAL(s2); *((int16_t *)dst) = GET_INT16_VAL(s1) + GET_INT16_VAL(s2);
...@@ -629,14 +632,14 @@ void operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type) { ...@@ -629,14 +632,14 @@ void operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type) {
} }
} }
void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void* buf) { void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void *buf) {
switch (type) { switch (type) {
case TSDB_DATA_TYPE_INT: case TSDB_DATA_TYPE_INT:
case TSDB_DATA_TYPE_UINT: { case TSDB_DATA_TYPE_UINT: {
TSWAP(*(int32_t *)(pLeft), *(int32_t *)(pRight), int32_t); TSWAP(*(int32_t *)(pLeft), *(int32_t *)(pRight), int32_t);
break; break;
} }
case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_BIGINT:
case TSDB_DATA_TYPE_UBIGINT: case TSDB_DATA_TYPE_UBIGINT:
case TSDB_DATA_TYPE_TIMESTAMP: { case TSDB_DATA_TYPE_TIMESTAMP: {
...@@ -652,19 +655,19 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void* buf ...@@ -652,19 +655,19 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void* buf
TSWAP(*(int16_t *)(pLeft), *(int16_t *)(pRight), int16_t); TSWAP(*(int16_t *)(pLeft), *(int16_t *)(pRight), int16_t);
break; break;
} }
case TSDB_DATA_TYPE_FLOAT: { case TSDB_DATA_TYPE_FLOAT: {
TSWAP(*(float *)(pLeft), *(float *)(pRight), float); TSWAP(*(float *)(pLeft), *(float *)(pRight), float);
break; break;
} }
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_TINYINT:
case TSDB_DATA_TYPE_UTINYINT: { case TSDB_DATA_TYPE_UTINYINT: {
TSWAP(*(int8_t *)(pLeft), *(int8_t *)(pRight), int8_t); TSWAP(*(int8_t *)(pLeft), *(int8_t *)(pRight), int8_t);
break; break;
} }
default: { default: {
memcpy(buf, pLeft, size); memcpy(buf, pLeft, size);
memcpy(pLeft, pRight, size); memcpy(pLeft, pRight, size);
......
...@@ -35,9 +35,9 @@ ...@@ -35,9 +35,9 @@
assert(0); \ assert(0); \
} while (0) } while (0)
int32_t toInteger(const char* z, int32_t n, int32_t base, int64_t* value, bool* isSigned) { int32_t toInteger(const char *z, int32_t n, int32_t base, int64_t *value, bool *isSigned) {
errno = 0; errno = 0;
char* endPtr = NULL; char *endPtr = NULL;
int32_t index = 0; int32_t index = 0;
...@@ -63,15 +63,15 @@ int32_t toInteger(const char* z, int32_t n, int32_t base, int64_t* value, bool* ...@@ -63,15 +63,15 @@ int32_t toInteger(const char* z, int32_t n, int32_t base, int64_t* value, bool*
*isSigned = specifiedSign || (val <= INT64_MAX); *isSigned = specifiedSign || (val <= INT64_MAX);
if (*isSigned) { if (*isSigned) {
*value = (z[0] == '-')? -val:val; *value = (z[0] == '-') ? -val : val;
} else { } else {
*(uint64_t*) value = val; *(uint64_t *)value = val;
} }
return 0; return 0;
} }
void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) { void taosVariantCreate(SVariant *pVar, const char *z, int32_t n, int32_t type) {
int32_t ret = 0; int32_t ret = 0;
memset(pVar, 0, sizeof(SVariant)); memset(pVar, 0, sizeof(SVariant));
...@@ -90,7 +90,7 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) { ...@@ -90,7 +90,7 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) {
case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_TINYINT:
case TSDB_DATA_TYPE_SMALLINT: case TSDB_DATA_TYPE_SMALLINT:
case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_BIGINT:
case TSDB_DATA_TYPE_INT:{ case TSDB_DATA_TYPE_INT: {
bool sign = true; bool sign = true;
int32_t base = 10; int32_t base = 10;
...@@ -104,11 +104,11 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) { ...@@ -104,11 +104,11 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) {
ret = toInteger(z, n, base, &pVar->i, &sign); ret = toInteger(z, n, base, &pVar->i, &sign);
if (ret != 0) { if (ret != 0) {
pVar->nType = -1; // -1 means error type pVar->nType = -1; // -1 means error type
return; return;
} }
pVar->nType = (sign)? TSDB_DATA_TYPE_BIGINT:TSDB_DATA_TYPE_UBIGINT; pVar->nType = (sign) ? TSDB_DATA_TYPE_BIGINT : TSDB_DATA_TYPE_UBIGINT;
break; break;
} }
case TSDB_DATA_TYPE_DOUBLE: case TSDB_DATA_TYPE_DOUBLE:
...@@ -123,15 +123,15 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) { ...@@ -123,15 +123,15 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) {
} }
case TSDB_DATA_TYPE_TIMESTAMP: { case TSDB_DATA_TYPE_TIMESTAMP: {
assert(0); assert(0);
pVar->i = taosGetTimestamp(TSDB_TIME_PRECISION_NANO); pVar->i = taosGetTimestamp(TSDB_TIME_PRECISION_NANO);
break; break;
} }
default: { // nType == 0 means the null value default: { // nType == 0 means the null value
type = TSDB_DATA_TYPE_NULL; type = TSDB_DATA_TYPE_NULL;
} }
} }
pVar->nType = type; pVar->nType = type;
} }
...@@ -196,13 +196,13 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin ...@@ -196,13 +196,13 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin
pVar->d = GET_FLOAT_VAL(pz); pVar->d = GET_FLOAT_VAL(pz);
break; break;
} }
case TSDB_DATA_TYPE_NCHAR: { // here we get the nchar length from raw binary bits length case TSDB_DATA_TYPE_NCHAR: { // here we get the nchar length from raw binary bits length
size_t lenInwchar = len / TSDB_NCHAR_SIZE; size_t lenInwchar = len / TSDB_NCHAR_SIZE;
pVar->wpz = calloc(1, (lenInwchar + 1) * TSDB_NCHAR_SIZE); pVar->wpz = calloc(1, (lenInwchar + 1) * TSDB_NCHAR_SIZE);
memcpy(pVar->wpz, pz, lenInwchar * TSDB_NCHAR_SIZE); memcpy(pVar->wpz, pz, lenInwchar * TSDB_NCHAR_SIZE);
pVar->nLen = (int32_t)len; pVar->nLen = (int32_t)len;
break; break;
} }
case TSDB_DATA_TYPE_BINARY: { // todo refactor, extract a method case TSDB_DATA_TYPE_BINARY: { // todo refactor, extract a method
...@@ -211,18 +211,18 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin ...@@ -211,18 +211,18 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin
pVar->nLen = (int32_t)len; pVar->nLen = (int32_t)len;
break; break;
} }
default: default:
pVar->i = GET_INT32_VAL(pz); pVar->i = GET_INT32_VAL(pz);
pVar->nLen = tDataTypes[TSDB_DATA_TYPE_INT].bytes; pVar->nLen = tDataTypes[TSDB_DATA_TYPE_INT].bytes;
} }
pVar->nType = type; pVar->nType = type;
} }
void taosVariantDestroy(SVariant *pVar) { void taosVariantDestroy(SVariant *pVar) {
if (pVar == NULL) return; if (pVar == NULL) return;
if (pVar->nType == TSDB_DATA_TYPE_BINARY || pVar->nType == TSDB_DATA_TYPE_NCHAR) { if (pVar->nType == TSDB_DATA_TYPE_BINARY || pVar->nType == TSDB_DATA_TYPE_NCHAR) {
tfree(pVar->pz); tfree(pVar->pz);
pVar->nLen = 0; pVar->nLen = 0;
...@@ -231,8 +231,8 @@ void taosVariantDestroy(SVariant *pVar) { ...@@ -231,8 +231,8 @@ void taosVariantDestroy(SVariant *pVar) {
// NOTE: this is only for string array // NOTE: this is only for string array
if (pVar->nType == TSDB_DATA_TYPE_POINTER_ARRAY) { if (pVar->nType == TSDB_DATA_TYPE_POINTER_ARRAY) {
size_t num = taosArrayGetSize(pVar->arr); size_t num = taosArrayGetSize(pVar->arr);
for(size_t i = 0; i < num; i++) { for (size_t i = 0; i < num; i++) {
void* p = taosArrayGetP(pVar->arr, i); void *p = taosArrayGetP(pVar->arr, i);
free(p); free(p);
} }
taosArrayDestroy(pVar->arr); taosArrayDestroy(pVar->arr);
...@@ -250,11 +250,11 @@ bool taosVariantIsValid(SVariant *pVar) { ...@@ -250,11 +250,11 @@ bool taosVariantIsValid(SVariant *pVar) {
void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) { void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) {
if (pSrc == NULL || pDst == NULL) return; if (pSrc == NULL || pDst == NULL) return;
pDst->nType = pSrc->nType; pDst->nType = pSrc->nType;
if (pSrc->nType == TSDB_DATA_TYPE_BINARY || pSrc->nType == TSDB_DATA_TYPE_NCHAR) { if (pSrc->nType == TSDB_DATA_TYPE_BINARY || pSrc->nType == TSDB_DATA_TYPE_NCHAR) {
int32_t len = pSrc->nLen + TSDB_NCHAR_SIZE; int32_t len = pSrc->nLen + TSDB_NCHAR_SIZE;
char* p = realloc(pDst->pz, len); char *p = realloc(pDst->pz, len);
assert(p); assert(p);
memset(p, 0, len); memset(p, 0, len);
...@@ -263,28 +263,27 @@ void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) { ...@@ -263,28 +263,27 @@ void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) {
memcpy(pDst->pz, pSrc->pz, pSrc->nLen); memcpy(pDst->pz, pSrc->pz, pSrc->nLen);
pDst->nLen = pSrc->nLen; pDst->nLen = pSrc->nLen;
return; return;
} }
if (IS_NUMERIC_TYPE(pSrc->nType) || (pSrc->nType == TSDB_DATA_TYPE_BOOL)) { if (IS_NUMERIC_TYPE(pSrc->nType) || (pSrc->nType == TSDB_DATA_TYPE_BOOL)) {
pDst->i = pSrc->i; pDst->i = pSrc->i;
} else if (pSrc->nType == TSDB_DATA_TYPE_POINTER_ARRAY) { // this is only for string array } else if (pSrc->nType == TSDB_DATA_TYPE_POINTER_ARRAY) { // this is only for string array
size_t num = taosArrayGetSize(pSrc->arr); size_t num = taosArrayGetSize(pSrc->arr);
pDst->arr = taosArrayInit(num, sizeof(char*)); pDst->arr = taosArrayInit(num, sizeof(char *));
for(size_t i = 0; i < num; i++) { for (size_t i = 0; i < num; i++) {
char* p = (char*)taosArrayGetP(pSrc->arr, i); char *p = (char *)taosArrayGetP(pSrc->arr, i);
char* n = strdup(p); char *n = strdup(p);
taosArrayPush(pDst->arr, &n); taosArrayPush(pDst->arr, &n);
} }
} else if (pSrc->nType == TSDB_DATA_TYPE_VALUE_ARRAY) { } else if (pSrc->nType == TSDB_DATA_TYPE_VALUE_ARRAY) {
size_t num = taosArrayGetSize(pSrc->arr); size_t num = taosArrayGetSize(pSrc->arr);
pDst->arr = taosArrayInit(num, sizeof(int64_t)); pDst->arr = taosArrayInit(num, sizeof(int64_t));
pDst->nLen = pSrc->nLen; pDst->nLen = pSrc->nLen;
assert(pSrc->nLen == num); assert(pSrc->nLen == num);
for(size_t i = 0; i < num; i++) { for (size_t i = 0; i < num; i++) {
int64_t *p = taosArrayGet(pSrc->arr, i); int64_t *p = taosArrayGet(pSrc->arr, i);
taosArrayPush(pDst->arr, p); taosArrayPush(pDst->arr, p);
} }
} }
if (pDst->nType != TSDB_DATA_TYPE_POINTER_ARRAY && pDst->nType != TSDB_DATA_TYPE_VALUE_ARRAY) { if (pDst->nType != TSDB_DATA_TYPE_POINTER_ARRAY && pDst->nType != TSDB_DATA_TYPE_VALUE_ARRAY) {
...@@ -292,7 +291,7 @@ void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) { ...@@ -292,7 +291,7 @@ void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) {
} }
} }
int32_t taosVariantCompare(const SVariant* p1, const SVariant* p2) { int32_t taosVariantCompare(const SVariant *p1, const SVariant *p2) {
if (p1->nType == TSDB_DATA_TYPE_NULL && p2->nType == TSDB_DATA_TYPE_NULL) { if (p1->nType == TSDB_DATA_TYPE_NULL && p2->nType == TSDB_DATA_TYPE_NULL) {
return 0; return 0;
} }
...@@ -309,39 +308,39 @@ int32_t taosVariantCompare(const SVariant* p1, const SVariant* p2) { ...@@ -309,39 +308,39 @@ int32_t taosVariantCompare(const SVariant* p1, const SVariant* p2) {
if (p1->nLen == p2->nLen) { if (p1->nLen == p2->nLen) {
return memcmp(p1->pz, p2->pz, p1->nLen); return memcmp(p1->pz, p2->pz, p1->nLen);
} else { } else {
return p1->nLen > p2->nLen? 1:-1; return p1->nLen > p2->nLen ? 1 : -1;
} }
} else if (p1->nType == TSDB_DATA_TYPE_FLOAT || p1->nType == TSDB_DATA_TYPE_DOUBLE) { } else if (p1->nType == TSDB_DATA_TYPE_FLOAT || p1->nType == TSDB_DATA_TYPE_DOUBLE) {
if (p1->d == p2->d) { if (p1->d == p2->d) {
return 0; return 0;
} else { } else {
return p1->d > p2->d? 1:-1; return p1->d > p2->d ? 1 : -1;
} }
} else if (IS_UNSIGNED_NUMERIC_TYPE(p1->nType)) { } else if (IS_UNSIGNED_NUMERIC_TYPE(p1->nType)) {
if (p1->u == p2->u) { if (p1->u == p2->u) {
return 0; return 0;
} else { } else {
return p1->u > p2->u? 1:-1; return p1->u > p2->u ? 1 : -1;
} }
} else { } else {
if (p1->i == p2->i) { if (p1->i == p2->i) {
return 0; return 0;
} else { } else {
return p1->i > p2->i? 1:-1; return p1->i > p2->i ? 1 : -1;
} }
} }
} }
int32_t taosVariantToString(SVariant *pVar, char *dst) { int32_t taosVariantToString(SVariant *pVar, char *dst) {
if (pVar == NULL || dst == NULL) return 0; if (pVar == NULL || dst == NULL) return 0;
switch (pVar->nType) { switch (pVar->nType) {
case TSDB_DATA_TYPE_BINARY: { case TSDB_DATA_TYPE_BINARY: {
int32_t len = sprintf(dst, "\'%s\'", pVar->pz); int32_t len = sprintf(dst, "\'%s\'", pVar->pz);
assert(len <= pVar->nLen + sizeof("\'") * 2); // two more chars assert(len <= pVar->nLen + sizeof("\'") * 2); // two more chars
return len; return len;
} }
case TSDB_DATA_TYPE_NCHAR: { case TSDB_DATA_TYPE_NCHAR: {
dst[0] = '\''; dst[0] = '\'';
taosUcs4ToMbs(pVar->wpz, (twcslen(pVar->wpz) + 1) * TSDB_NCHAR_SIZE, dst + 1); taosUcs4ToMbs(pVar->wpz, (twcslen(pVar->wpz) + 1) * TSDB_NCHAR_SIZE, dst + 1);
...@@ -350,7 +349,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) { ...@@ -350,7 +349,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) {
dst[len + 1] = 0; dst[len + 1] = 0;
return len + 1; return len + 1;
} }
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_TINYINT:
case TSDB_DATA_TYPE_SMALLINT: case TSDB_DATA_TYPE_SMALLINT:
...@@ -359,7 +358,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) { ...@@ -359,7 +358,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) {
case TSDB_DATA_TYPE_USMALLINT: case TSDB_DATA_TYPE_USMALLINT:
case TSDB_DATA_TYPE_UINT: case TSDB_DATA_TYPE_UINT:
return sprintf(dst, "%d", (int32_t)pVar->i); return sprintf(dst, "%d", (int32_t)pVar->i);
case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_BIGINT:
return sprintf(dst, "%" PRId64, pVar->i); return sprintf(dst, "%" PRId64, pVar->i);
case TSDB_DATA_TYPE_UBIGINT: case TSDB_DATA_TYPE_UBIGINT:
...@@ -367,7 +366,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) { ...@@ -367,7 +366,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) {
case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_FLOAT:
case TSDB_DATA_TYPE_DOUBLE: case TSDB_DATA_TYPE_DOUBLE:
return sprintf(dst, "%.9lf", pVar->d); return sprintf(dst, "%.9lf", pVar->d);
default: default:
return 0; return 0;
} }
...@@ -399,27 +398,27 @@ static FORCE_INLINE int32_t wcsconvertToBoolImpl(wchar_t *pstr, int32_t len) { ...@@ -399,27 +398,27 @@ static FORCE_INLINE int32_t wcsconvertToBoolImpl(wchar_t *pstr, int32_t len) {
static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) { static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
const int32_t INITIAL_ALLOC_SIZE = 40; const int32_t INITIAL_ALLOC_SIZE = 40;
char * pBuf = NULL; char *pBuf = NULL;
// it is a in-place convert type for SVariant, local buffer is needed // it is a in-place convert type for SVariant, local buffer is needed
if (*pDest == pVariant->pz) { if (*pDest == pVariant->pz) {
pBuf = calloc(1, INITIAL_ALLOC_SIZE); pBuf = calloc(1, INITIAL_ALLOC_SIZE);
} }
if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) { if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
size_t newSize = pVariant->nLen * TSDB_NCHAR_SIZE; size_t newSize = pVariant->nLen * TSDB_NCHAR_SIZE;
if (pBuf != NULL) { if (pBuf != NULL) {
if (newSize >= INITIAL_ALLOC_SIZE) { if (newSize >= INITIAL_ALLOC_SIZE) {
pBuf = realloc(pBuf, newSize + 1); pBuf = realloc(pBuf, newSize + 1);
} }
taosUcs4ToMbs(pVariant->wpz, (int32_t)newSize, pBuf); taosUcs4ToMbs(pVariant->wpz, (int32_t)newSize, pBuf);
free(pVariant->wpz); free(pVariant->wpz);
pBuf[newSize] = 0; pBuf[newSize] = 0;
} else { } else {
taosUcs4ToMbs(pVariant->wpz, (int32_t)newSize, *pDest); taosUcs4ToMbs(pVariant->wpz, (int32_t)newSize, *pDest);
} }
} else { } else {
if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) { if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) {
sprintf(pBuf == NULL ? *pDest : pBuf, "%" PRId64, pVariant->i); sprintf(pBuf == NULL ? *pDest : pBuf, "%" PRId64, pVariant->i);
...@@ -431,26 +430,26 @@ static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) { ...@@ -431,26 +430,26 @@ static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
setNull(pBuf == NULL ? *pDest : pBuf, TSDB_DATA_TYPE_BINARY, 0); setNull(pBuf == NULL ? *pDest : pBuf, TSDB_DATA_TYPE_BINARY, 0);
} }
} }
if (pBuf != NULL) { if (pBuf != NULL) {
*pDest = pBuf; *pDest = pBuf;
} }
*pDestSize = (int32_t)strlen(*pDest); *pDestSize = (int32_t)strlen(*pDest);
return 0; return 0;
} }
static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
char tmpBuf[40] = {0}; char tmpBuf[40] = {0};
char * pDst = tmpBuf; char *pDst = tmpBuf;
int32_t nLen = 0; int32_t nLen = 0;
// convert the number to string, than convert it to wchar string. // convert the number to string, than convert it to wchar string.
if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) { if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) {
nLen = sprintf(pDst, "%" PRId64, pVariant->i); nLen = sprintf(pDst, "%" PRId64, pVariant->i);
} else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
nLen = sprintf(pDst, "%"PRIu64, pVariant->u); nLen = sprintf(pDst, "%" PRIu64, pVariant->u);
} else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) { } else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) {
nLen = sprintf(pDst, "%lf", pVariant->d); nLen = sprintf(pDst, "%lf", pVariant->d);
} else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) { } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
...@@ -459,10 +458,10 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { ...@@ -459,10 +458,10 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
} else if (pVariant->nType == TSDB_DATA_TYPE_BOOL) { } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL) {
nLen = sprintf(pDst, "%s", (pVariant->i == TSDB_TRUE) ? "TRUE" : "FALSE"); nLen = sprintf(pDst, "%s", (pVariant->i == TSDB_TRUE) ? "TRUE" : "FALSE");
} }
if (*pDest == pVariant->pz) { if (*pDest == pVariant->pz) {
wchar_t *pWStr = calloc(1, (nLen + 1) * TSDB_NCHAR_SIZE); wchar_t *pWStr = calloc(1, (nLen + 1) * TSDB_NCHAR_SIZE);
bool ret = taosMbsToUcs4(pDst, nLen, (char *)pWStr, (nLen + 1) * TSDB_NCHAR_SIZE, NULL); bool ret = taosMbsToUcs4(pDst, nLen, (char *)pWStr, (nLen + 1) * TSDB_NCHAR_SIZE, NULL);
if (!ret) { if (!ret) {
tfree(pWStr); tfree(pWStr);
return -1; return -1;
...@@ -472,14 +471,14 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { ...@@ -472,14 +471,14 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
if (pVariant->nType == TSDB_DATA_TYPE_BINARY) { if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
free(pVariant->wpz); free(pVariant->wpz);
} }
pVariant->wpz = pWStr; pVariant->wpz = pWStr;
*pDestSize = twcslen(pVariant->wpz); *pDestSize = twcslen(pVariant->wpz);
// shrink the allocate memory, no need to check here. // shrink the allocate memory, no need to check here.
char* tmp = realloc(pVariant->wpz, (*pDestSize + 1)*TSDB_NCHAR_SIZE); char *tmp = realloc(pVariant->wpz, (*pDestSize + 1) * TSDB_NCHAR_SIZE);
assert(tmp != NULL); assert(tmp != NULL);
pVariant->wpz = (wchar_t *)tmp; pVariant->wpz = (wchar_t *)tmp;
} else { } else {
int32_t output = 0; int32_t output = 0;
...@@ -493,21 +492,22 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { ...@@ -493,21 +492,22 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
*pDestSize = output; *pDestSize = output;
} }
} }
return 0; return 0;
} }
static FORCE_INLINE int32_t convertToDouble(char *pStr, int32_t len, double *value) { static FORCE_INLINE int32_t convertToDouble(char *pStr, int32_t len, double *value) {
// SToken stoken = {.z = pStr, .n = len}; // SToken stoken = {.z = pStr, .n = len};
// if (TK_ILLEGAL == tGetNumericStringType(&stoken)) { // if (TK_ILLEGAL == tGetNumericStringType(&stoken)) {
// return -1; // return -1;
// } // }
// //
// *value = strtod(pStr, NULL); // *value = strtod(pStr, NULL);
return 0; return 0;
} }
static FORCE_INLINE int32_t convertToInteger(SVariant *pVariant, int64_t *result, int32_t type, bool issigned, bool releaseVariantPtr, bool *converted) { static FORCE_INLINE int32_t convertToInteger(SVariant *pVariant, int64_t *result, int32_t type, bool issigned,
bool releaseVariantPtr, bool *converted) {
if (pVariant->nType == TSDB_DATA_TYPE_NULL) { if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
setNull((char *)result, type, tDataTypes[type].bytes); setNull((char *)result, type, tDataTypes[type].bytes);
return 0; return 0;
...@@ -645,7 +645,7 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) { ...@@ -645,7 +645,7 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) {
if ((ret = convertToBoolImpl(pVariant->pz, pVariant->nLen)) < 0) { if ((ret = convertToBoolImpl(pVariant->pz, pVariant->nLen)) < 0) {
return ret; return ret;
} }
*pDest = ret; *pDest = ret;
} else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) { } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
int32_t ret = 0; int32_t ret = 0;
...@@ -656,7 +656,7 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) { ...@@ -656,7 +656,7 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) {
} else if (pVariant->nType == TSDB_DATA_TYPE_NULL) { } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
*pDest = TSDB_DATA_BOOL_NULL; *pDest = TSDB_DATA_BOOL_NULL;
} }
assert(*pDest == TSDB_TRUE || *pDest == TSDB_FALSE || *pDest == TSDB_DATA_BOOL_NULL); assert(*pDest == TSDB_TRUE || *pDest == TSDB_FALSE || *pDest == TSDB_DATA_BOOL_NULL);
return 0; return 0;
} }
...@@ -665,11 +665,12 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) { ...@@ -665,11 +665,12 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) {
* transfer data from variant serve as the implicit data conversion: from input sql string pVariant->nType * transfer data from variant serve as the implicit data conversion: from input sql string pVariant->nType
* to column type defined in schema * to column type defined in schema
*/ */
int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted, char *extInfo) { int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted,
char *extInfo) {
if (converted) { if (converted) {
*converted = false; *converted = false;
} }
if (pVariant == NULL || (pVariant->nType != 0 && !isValidDataType(pVariant->nType))) { if (pVariant == NULL || (pVariant->nType != 0 && !isValidDataType(pVariant->nType))) {
return -1; return -1;
} }
...@@ -686,25 +687,25 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc ...@@ -686,25 +687,25 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc
*(int8_t *)payload = (int8_t)result; *(int8_t *)payload = (int8_t)result;
break; break;
} }
case TSDB_DATA_TYPE_TINYINT: { case TSDB_DATA_TYPE_TINYINT: {
if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) { if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
SET_EXT_INFO(converted, result, INT8_MIN + 1, INT8_MAX, extInfo); SET_EXT_INFO(converted, result, INT8_MIN + 1, INT8_MAX, extInfo);
return -1; return -1;
} }
*((int8_t *)payload) = (int8_t) result; *((int8_t *)payload) = (int8_t)result;
break; break;
} }
case TSDB_DATA_TYPE_UTINYINT: { case TSDB_DATA_TYPE_UTINYINT: {
if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) { if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
SET_EXT_INFO(converted, result, 0, UINT8_MAX - 1, extInfo); SET_EXT_INFO(converted, result, 0, UINT8_MAX - 1, extInfo);
return -1; return -1;
} }
*((uint8_t *)payload) = (uint8_t) result; *((uint8_t *)payload) = (uint8_t)result;
break; break;
} }
case TSDB_DATA_TYPE_SMALLINT: { case TSDB_DATA_TYPE_SMALLINT: {
if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) { if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
SET_EXT_INFO(converted, result, INT16_MIN + 1, INT16_MAX, extInfo); SET_EXT_INFO(converted, result, INT16_MIN + 1, INT16_MAX, extInfo);
...@@ -722,7 +723,7 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc ...@@ -722,7 +723,7 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc
*((uint16_t *)payload) = (uint16_t)result; *((uint16_t *)payload) = (uint16_t)result;
break; break;
} }
case TSDB_DATA_TYPE_INT: { case TSDB_DATA_TYPE_INT: {
if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) { if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
SET_EXT_INFO(converted, result, INT32_MIN + 1, INT32_MAX, extInfo); SET_EXT_INFO(converted, result, INT32_MIN + 1, INT32_MAX, extInfo);
...@@ -740,7 +741,7 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc ...@@ -740,7 +741,7 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc
*((uint32_t *)payload) = (uint32_t)result; *((uint32_t *)payload) = (uint32_t)result;
break; break;
} }
case TSDB_DATA_TYPE_BIGINT: { case TSDB_DATA_TYPE_BIGINT: {
if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) { if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
SET_EXT_INFO(converted, (int64_t)result, INT64_MIN + 1, INT64_MAX, extInfo); SET_EXT_INFO(converted, (int64_t)result, INT64_MIN + 1, INT64_MAX, extInfo);
...@@ -775,19 +776,20 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc ...@@ -775,19 +776,20 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc
if (converted) { if (converted) {
*converted = true; *converted = true;
} }
if (value > FLT_MAX || value < -FLT_MAX) { if (value > FLT_MAX || value < -FLT_MAX) {
SET_EXT_INFO(converted, value, -FLT_MAX, FLT_MAX, extInfo); SET_EXT_INFO(converted, value, -FLT_MAX, FLT_MAX, extInfo);
return -1; return -1;
} }
SET_FLOAT_VAL(payload, value); SET_FLOAT_VAL(payload, value);
} }
} else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) ||
IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
if (converted) { if (converted) {
*converted = true; *converted = true;
} }
if (pVariant->i > FLT_MAX || pVariant->i < -FLT_MAX) { if (pVariant->i > FLT_MAX || pVariant->i < -FLT_MAX) {
SET_EXT_INFO(converted, pVariant->i, -FLT_MAX, FLT_MAX, extInfo); SET_EXT_INFO(converted, pVariant->i, -FLT_MAX, FLT_MAX, extInfo);
return -1; return -1;
} }
...@@ -797,12 +799,12 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc ...@@ -797,12 +799,12 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc
if (converted) { if (converted) {
*converted = true; *converted = true;
} }
if (pVariant->d > FLT_MAX || pVariant->d < -FLT_MAX) { if (pVariant->d > FLT_MAX || pVariant->d < -FLT_MAX) {
SET_EXT_INFO(converted, pVariant->d, -FLT_MAX, FLT_MAX, extInfo); SET_EXT_INFO(converted, pVariant->d, -FLT_MAX, FLT_MAX, extInfo);
return -1; return -1;
} }
SET_FLOAT_VAL(payload, pVariant->d); SET_FLOAT_VAL(payload, pVariant->d);
} else if (pVariant->nType == TSDB_DATA_TYPE_NULL) { } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
*((uint32_t *)payload) = TSDB_DATA_FLOAT_NULL; *((uint32_t *)payload) = TSDB_DATA_FLOAT_NULL;
...@@ -831,7 +833,8 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc ...@@ -831,7 +833,8 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc
SET_DOUBLE_VAL(payload, value); SET_DOUBLE_VAL(payload, value);
} }
} else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) ||
IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
SET_DOUBLE_VAL(payload, pVariant->i); SET_DOUBLE_VAL(payload, pVariant->i);
} else if (IS_FLOAT_TYPE(pVariant->nType)) { } else if (IS_FLOAT_TYPE(pVariant->nType)) {
SET_DOUBLE_VAL(payload, pVariant->d); SET_DOUBLE_VAL(payload, pVariant->d);
...@@ -847,11 +850,11 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc ...@@ -847,11 +850,11 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc
break; break;
} }
case TSDB_DATA_TYPE_BINARY: { case TSDB_DATA_TYPE_BINARY: {
if (!includeLengthPrefix) { if (!includeLengthPrefix) {
if (pVariant->nType == TSDB_DATA_TYPE_NULL) { if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
*(uint8_t*) payload = TSDB_DATA_BINARY_NULL; *(uint8_t *)payload = TSDB_DATA_BINARY_NULL;
} else { } else {
if (pVariant->nType != TSDB_DATA_TYPE_BINARY) { if (pVariant->nType != TSDB_DATA_TYPE_BINARY) {
toBinary(pVariant, &payload, &pVariant->nLen); toBinary(pVariant, &payload, &pVariant->nLen);
...@@ -918,11 +921,11 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc ...@@ -918,11 +921,11 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc
assert(p == varDataVal(payload)); assert(p == varDataVal(payload));
} }
} }
break; break;
} }
} }
return 0; return 0;
} }
...@@ -944,13 +947,13 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { ...@@ -944,13 +947,13 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) {
if (pVariant == NULL || pVariant->nType == 0) { // value is not set if (pVariant == NULL || pVariant->nType == 0) { // value is not set
return 0; return 0;
} }
switch (type) { switch (type) {
case TSDB_DATA_TYPE_BOOL: { // bool case TSDB_DATA_TYPE_BOOL: { // bool
if (convertToBool(pVariant, &pVariant->i) < 0) { if (convertToBool(pVariant, &pVariant->i) < 0) {
return -1; return -1;
} }
pVariant->nType = type; pVariant->nType = type;
break; break;
} }
...@@ -971,7 +974,7 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { ...@@ -971,7 +974,7 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) {
free(pVariant->pz); free(pVariant->pz);
return -1; return -1;
} }
free(pVariant->pz); free(pVariant->pz);
pVariant->d = v; pVariant->d = v;
} else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) { } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
...@@ -981,14 +984,14 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { ...@@ -981,14 +984,14 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) {
free(pVariant->pz); free(pVariant->pz);
return -1; return -1;
} }
free(pVariant->pz); free(pVariant->pz);
pVariant->d = v; pVariant->d = v;
} else if (pVariant->nType >= TSDB_DATA_TYPE_BOOL && pVariant->nType <= TSDB_DATA_TYPE_BIGINT) { } else if (pVariant->nType >= TSDB_DATA_TYPE_BOOL && pVariant->nType <= TSDB_DATA_TYPE_BIGINT) {
double tmp = (double) pVariant->i; double tmp = (double)pVariant->i;
pVariant->d = tmp; pVariant->d = tmp;
} }
pVariant->nType = TSDB_DATA_TYPE_DOUBLE; pVariant->nType = TSDB_DATA_TYPE_DOUBLE;
break; break;
} }
...@@ -1009,6 +1012,6 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { ...@@ -1009,6 +1012,6 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) {
break; break;
} }
} }
return 0; return 0;
} }
\ No newline at end of file
...@@ -18,227 +18,68 @@ ...@@ -18,227 +18,68 @@
#include "mndCluster.h" #include "mndCluster.h"
#include "mndSync.h" #include "mndSync.h"
#include "tbuffer.h" #include "tbuffer.h"
#include "tversion.h" #include "tjson.h"
#include "thttp.h"
#define TELEMETRY_SERVER "telemetry.taosdata.com" #define TELEMETRY_SERVER "telemetry.taosdata.com"
#define TELEMETRY_PORT 80 #define TELEMETRY_PORT 80
#define REPORT_INTERVAL 86400
static void mndBeginObject(SBufferWriter* bw) { tbufWriteChar(bw, '{'); } static void mndBuildRuntimeInfo(SMnode* pMnode, SJson* pJson) {
SMnodeLoad load = {0};
static void mndCloseObject(SBufferWriter* bw) { if (mndGetLoad(pMnode, &load) != 0) return;
size_t len = tbufTell(bw);
if (tbufGetData(bw, false)[len - 1] == ',') { tjsonAddDoubleToObject(pJson, "numOfDnode", load.numOfDnode);
tbufWriteCharAt(bw, len - 1, '}'); tjsonAddDoubleToObject(pJson, "numOfMnode", load.numOfMnode);
} else { tjsonAddDoubleToObject(pJson, "numOfVgroup", load.numOfVgroup);
tbufWriteChar(bw, '}'); tjsonAddDoubleToObject(pJson, "numOfDatabase", load.numOfDatabase);
} tjsonAddDoubleToObject(pJson, "numOfSuperTable", load.numOfSuperTable);
} tjsonAddDoubleToObject(pJson, "numOfChildTable", load.numOfChildTable);
tjsonAddDoubleToObject(pJson, "numOfColumn", load.numOfColumn);
static void mndWriteString(SBufferWriter* bw, const char* str) { tjsonAddDoubleToObject(pJson, "numOfPoint", load.totalPoints);
tbufWriteChar(bw, '"'); tjsonAddDoubleToObject(pJson, "totalStorage", load.totalStorage);
tbufWrite(bw, str, strlen(str)); tjsonAddDoubleToObject(pJson, "compStorage", load.compStorage);
tbufWriteChar(bw, '"');
}
static void mndAddIntField(SBufferWriter* bw, const char* k, int64_t v) {
mndWriteString(bw, k);
tbufWriteChar(bw, ':');
char buf[32] = {0};
sprintf(buf, "%" PRId64, v);
tbufWrite(bw, buf, strlen(buf));
tbufWriteChar(bw, ',');
}
static void mndAddStringField(SBufferWriter* bw, const char* k, const char* v) {
mndWriteString(bw, k);
tbufWriteChar(bw, ':');
mndWriteString(bw, v);
tbufWriteChar(bw, ',');
}
static void mndAddCpuInfo(SMnode* pMnode, SBufferWriter* bw) {
char* line = NULL;
size_t size = 0;
int32_t done = 0;
// FILE* fp = fopen("/proc/cpuinfo", "r");
TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) {
return;
}
while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) {
line[size - 1] = '\0';
if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) {
const char* v = strchr(line, ':') + 2;
mndAddStringField(bw, "cpuModel", v);
done |= 1;
} else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) {
const char* v = strchr(line, ':') + 2;
mndWriteString(bw, "numOfCpu");
tbufWriteChar(bw, ':');
tbufWrite(bw, v, strlen(v));
tbufWriteChar(bw, ',');
done |= 2;
}
}
if(line != NULL) free(line);
taosCloseFile(&pFile);
}
static void mndAddOsInfo(SMnode* pMnode, SBufferWriter* bw) {
char* line = NULL;
size_t size = 0;
// FILE* fp = fopen("/etc/os-release", "r");
TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) {
return;
}
while ((size = taosGetLineFile(pFile, &line)) != -1) {
line[size - 1] = '\0';
if (strncmp(line, "PRETTY_NAME", 11) == 0) {
const char* p = strchr(line, '=') + 1;
if (*p == '"') {
p++;
line[size - 2] = 0;
}
mndAddStringField(bw, "os", p);
break;
}
}
if(line != NULL) free(line);
taosCloseFile(&pFile);
}
static void mndAddMemoryInfo(SMnode* pMnode, SBufferWriter* bw) {
char* line = NULL;
size_t size = 0;
// FILE* fp = fopen("/proc/meminfo", "r");
TdFilePtr pFile = taosOpenFile("/proc/meminfo", TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) {
return;
}
while ((size = taosGetLineFile(pFile, &line)) != -1) {
line[size - 1] = '\0';
if (strncmp(line, "MemTotal", 8) == 0) {
const char* p = strchr(line, ':') + 1;
while (*p == ' ') p++;
mndAddStringField(bw, "memory", p);
break;
}
}
if(line != NULL) free(line);
taosCloseFile(&pFile);
} }
static void mndAddVersionInfo(SMnode* pMnode, SBufferWriter* bw) { static char* mndBuildTelemetryReport(SMnode* pMnode) {
char tmp[4096] = {0};
STelemMgmt* pMgmt = &pMnode->telemMgmt; STelemMgmt* pMgmt = &pMnode->telemMgmt;
mndAddStringField(bw, "version", version);
mndAddStringField(bw, "buildInfo", buildinfo);
mndAddStringField(bw, "gitInfo", gitinfo);
mndAddStringField(bw, "email", pMgmt->email);
}
static void mndAddRuntimeInfo(SMnode* pMnode, SBufferWriter* bw) {
SMnodeLoad load = {0};
if (mndGetLoad(pMnode, &load) != 0) {
return;
}
mndAddIntField(bw, "numOfDnode", load.numOfDnode); SJson* pJson = tjsonCreateObject();
mndAddIntField(bw, "numOfMnode", load.numOfMnode); if (pJson == NULL) return NULL;
mndAddIntField(bw, "numOfVgroup", load.numOfVgroup);
mndAddIntField(bw, "numOfDatabase", load.numOfDatabase);
mndAddIntField(bw, "numOfSuperTable", load.numOfSuperTable);
mndAddIntField(bw, "numOfChildTable", load.numOfChildTable);
mndAddIntField(bw, "numOfColumn", load.numOfColumn);
mndAddIntField(bw, "numOfPoint", load.totalPoints);
mndAddIntField(bw, "totalStorage", load.totalStorage);
mndAddIntField(bw, "compStorage", load.compStorage);
}
static void mndSendTelemetryReport(SMnode* pMnode) {
STelemMgmt* pMgmt = &pMnode->telemMgmt;
SBufferWriter bw = tbufInitWriter(NULL, false);
int32_t code = -1;
char buf[128] = {0};
SOCKET fd = 0;
char clusterName[64] = {0}; char clusterName[64] = {0};
if (mndGetClusterName(pMnode, clusterName, sizeof(clusterName)) != 0) { mndGetClusterName(pMnode, clusterName, sizeof(clusterName));
goto SEND_OVER; tjsonAddStringToObject(pJson, "instanceId", clusterName);
} tjsonAddDoubleToObject(pJson, "reportVersion", 1);
mndBeginObject(&bw);
mndAddStringField(&bw, "instanceId", clusterName);
mndAddIntField(&bw, "reportVersion", 1);
mndAddOsInfo(pMnode, &bw);
mndAddCpuInfo(pMnode, &bw);
mndAddMemoryInfo(pMnode, &bw);
mndAddVersionInfo(pMnode, &bw);
mndAddRuntimeInfo(pMnode, &bw);
mndCloseObject(&bw);
uint32_t ip = taosGetIpv4FromFqdn(TELEMETRY_SERVER); if (taosGetOsReleaseName(tmp, sizeof(tmp))) {
if (ip == 0xffffffff) { tjsonAddStringToObject(pJson, "os", tmp);
terrno = TAOS_SYSTEM_ERROR(errno);
mError("failed to get ip of %s since :%s", TELEMETRY_SERVER, terrstr());
goto SEND_OVER;
} }
fd = taosOpenTcpClientSocket(ip, TELEMETRY_PORT, 0); int32_t numOfCores = 0;
if (fd < 0) { if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores)) {
terrno = TAOS_SYSTEM_ERROR(errno); tjsonAddStringToObject(pJson, "cpuModel", tmp);
mError("failed to create socket to %s:%d since:%s", TELEMETRY_SERVER, TELEMETRY_PORT, terrstr()); tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores);
goto SEND_OVER; } else {
} tjsonAddDoubleToObject(pJson, "numOfCpu", taosGetCpuCores());
const char* header =
"POST /report HTTP/1.1\n"
"Host: " TELEMETRY_SERVER
"\n"
"Content-Type: application/json\n"
"Content-Length: ";
if (taosWriteSocket(fd, (void*)header, (int32_t)strlen(header)) < 0) {
goto SEND_OVER;
}
int32_t contLen = (int32_t)(tbufTell(&bw));
sprintf(buf, "%d\n\n", contLen);
if (taosWriteSocket(fd, buf, (int32_t)strlen(buf)) < 0) {
goto SEND_OVER;
}
const char* pCont = tbufGetData(&bw, false);
if (taosWriteSocket(fd, (void*)pCont, contLen) < 0) {
goto SEND_OVER;
} }
// read something to avoid nginx error 499 uint64_t memoryKB = 0;
if (taosReadSocket(fd, buf, 10) < 0) { if (taosGetTotalSysMemoryKB(&memoryKB)) {
terrno = TAOS_SYSTEM_ERROR(errno); snprintf(tmp, sizeof(tmp), "%" PRIu64 " kB", memoryKB);
mError("failed to receive response since %s", terrstr()); tjsonAddStringToObject(pJson, "memory", tmp);
goto SEND_OVER;
} }
mInfo("send telemetry to %s:%d, len:%d content: %s", TELEMETRY_SERVER, TELEMETRY_PORT, contLen, pCont); tjsonAddStringToObject(pJson, "version", version);
code = 0; tjsonAddStringToObject(pJson, "buildInfo", buildinfo);
tjsonAddStringToObject(pJson, "gitInfo", gitinfo);
tjsonAddStringToObject(pJson, "email", pMgmt->email);
SEND_OVER: mndBuildRuntimeInfo(pMnode, pJson);
tbufCloseWriter(&bw);
taosCloseSocket(fd);
if (code != 0) { char* pCont = tjsonToString(pJson);
mError("failed to send telemetry to %s:%d since %s", TELEMETRY_SERVER, TELEMETRY_PORT, terrstr()); tjsonDelete(pJson);
} return pCont;
} }
static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) { static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) {
...@@ -247,33 +88,23 @@ static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) { ...@@ -247,33 +88,23 @@ static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) {
if (!pMgmt->enable) return 0; if (!pMgmt->enable) return 0;
taosWLockLatch(&pMgmt->lock); taosWLockLatch(&pMgmt->lock);
mndSendTelemetryReport(pMnode); char* pCont = mndBuildTelemetryReport(pMnode);
if (pCont != NULL) {
taosSendHttpReport(TELEMETRY_SERVER, TELEMETRY_PORT, pCont, strlen(pCont));
free(pCont);
}
taosWUnLockLatch(&pMgmt->lock); taosWUnLockLatch(&pMgmt->lock);
return 0; return 0;
} }
static void mndGetEmail(SMnode* pMnode, char* filepath) {
STelemMgmt* pMgmt = &pMnode->telemMgmt;
TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ);
if (pFile == NULL) {
return;
}
if (taosReadFile(pFile, (void*)pMgmt->email, TSDB_FQDN_LEN) < 0) {
mError("failed to read %d bytes from file %s since %s", TSDB_FQDN_LEN, filepath, strerror(errno));
}
taosCloseFile(&pFile);
}
int32_t mndInitTelem(SMnode* pMnode) { int32_t mndInitTelem(SMnode* pMnode) {
STelemMgmt* pMgmt = &pMnode->telemMgmt; STelemMgmt* pMgmt = &pMnode->telemMgmt;
pMgmt->enable = tsEnableTelemetryReporting;
taosInitRWLatch(&pMgmt->lock);
mndGetEmail(pMnode, "/usr/local/taos/email");
taosInitRWLatch(&pMgmt->lock);
pMgmt->enable = tsEnableTelemetryReporting;
taosGetEmail(pMgmt->email, sizeof(pMgmt->email));
mndSetMsgHandle(pMnode, TDMT_MND_TELEM_TIMER, mndProcessTelemTimer); mndSetMsgHandle(pMnode, TDMT_MND_TELEM_TIMER, mndProcessTelemTimer);
mDebug("mnode telemetry is initialized"); mDebug("mnode telemetry is initialized");
return 0; return 0;
} }
......
...@@ -140,4 +140,13 @@ int32_t taosRealPath(char *dirname, int32_t maxlen) { ...@@ -140,4 +140,13 @@ int32_t taosRealPath(char *dirname, int32_t maxlen) {
return 0; return 0;
} }
bool taosIsDir(const char *dirname) {
DIR *dir = opendir(dirname);
if (dir != NULL) {
closedir(dir);
return true;
}
return false;
}
#endif #endif
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
#define ALLOW_FORBID_FUNC #define ALLOW_FORBID_FUNC
#include "os.h" #include "os.h"
#define MAX_FPRINTFLINE_BUFFER_SIZE (1000)
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
#include <io.h> #include <io.h>
...@@ -46,10 +44,15 @@ extern int openU(const char *, int, ...); /* MsvcLibX UTF-8 version of open */ ...@@ -46,10 +44,15 @@ extern int openU(const char *, int, ...); /* MsvcLibX UTF-8 version of open */
typedef int32_t FileFd; typedef int32_t FileFd;
#define FILE_WITH_LOCK 1
typedef struct TdFile { typedef struct TdFile {
int refId; #if FILE_WITH_LOCK
FileFd fd; pthread_rwlock_t rwlock;
FILE *fp; #endif
int refId;
FileFd fd;
FILE *fp;
} * TdFilePtr, TdFile; } * TdFilePtr, TdFile;
void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) { void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) {
...@@ -238,6 +241,9 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { ...@@ -238,6 +241,9 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
if (fp != NULL) fclose(fp); if (fp != NULL) fclose(fp);
return NULL; return NULL;
} }
#if FILE_WITH_LOCK
pthread_rwlock_init(&(pFile->rwlock),NULL);
#endif
pFile->fd = fd; pFile->fd = fd;
pFile->fp = fp; pFile->fp = fp;
pFile->refId = 0; pFile->refId = 0;
...@@ -249,6 +255,12 @@ int64_t taosCloseFile(TdFilePtr *ppFile) { ...@@ -249,6 +255,12 @@ int64_t taosCloseFile(TdFilePtr *ppFile) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
return 0; return 0;
#else #else
if (ppFile == NULL || *ppFile == NULL) {
return 0;
}
#if FILE_WITH_LOCK
pthread_rwlock_wrlock(&((*ppFile)->rwlock));
#endif
if (ppFile == NULL || *ppFile == NULL || (*ppFile)->fd == -1) { if (ppFile == NULL || *ppFile == NULL || (*ppFile)->fd == -1) {
return 0; return 0;
} }
...@@ -263,6 +275,10 @@ int64_t taosCloseFile(TdFilePtr *ppFile) { ...@@ -263,6 +275,10 @@ int64_t taosCloseFile(TdFilePtr *ppFile) {
(*ppFile)->fd = -1; (*ppFile)->fd = -1;
} }
(*ppFile)->refId = 0; (*ppFile)->refId = 0;
#if FILE_WITH_LOCK
pthread_rwlock_unlock(&((*ppFile)->rwlock));
pthread_rwlock_destroy(&((*ppFile)->rwlock));
#endif
free(*ppFile); free(*ppFile);
*ppFile = NULL; *ppFile = NULL;
return 0; return 0;
...@@ -273,6 +289,9 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { ...@@ -273,6 +289,9 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
if (pFile == NULL) { if (pFile == NULL) {
return 0; return 0;
} }
#if FILE_WITH_LOCK
pthread_rwlock_rdlock(&(pFile->rwlock));
#endif
assert(pFile->fd >= 0); assert(pFile->fd >= 0);
int64_t leftbytes = count; int64_t leftbytes = count;
int64_t readbytes; int64_t readbytes;
...@@ -284,9 +303,15 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { ...@@ -284,9 +303,15 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
if (errno == EINTR) { if (errno == EINTR) {
continue; continue;
} else { } else {
#if FILE_WITH_LOCK
pthread_rwlock_unlock(&(pFile->rwlock));
#endif
return -1; return -1;
} }
} else if (readbytes == 0) { } else if (readbytes == 0) {
#if FILE_WITH_LOCK
pthread_rwlock_unlock(&(pFile->rwlock));
#endif
return (int64_t)(count - leftbytes); return (int64_t)(count - leftbytes);
} }
...@@ -294,6 +319,9 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { ...@@ -294,6 +319,9 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
tbuf += readbytes; tbuf += readbytes;
} }
#if FILE_WITH_LOCK
pthread_rwlock_unlock(&(pFile->rwlock));
#endif
return count; return count;
} }
...@@ -301,14 +329,24 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) ...@@ -301,14 +329,24 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset)
if (pFile == NULL) { if (pFile == NULL) {
return 0; return 0;
} }
#if FILE_WITH_LOCK
pthread_rwlock_rdlock(&(pFile->rwlock));
#endif
assert(pFile->fd >= 0); assert(pFile->fd >= 0);
return pread(pFile->fd, buf, count, offset); int64_t ret = pread(pFile->fd, buf, count, offset);
#if FILE_WITH_LOCK
pthread_rwlock_unlock(&(pFile->rwlock));
#endif
return ret;
} }
int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
if (pFile == NULL) { if (pFile == NULL) {
return 0; return 0;
} }
#if FILE_WITH_LOCK
pthread_rwlock_wrlock(&(pFile->rwlock));
#endif
assert(pFile->fd >= 0); assert(pFile->fd >= 0);
int64_t nleft = count; int64_t nleft = count;
...@@ -321,12 +359,18 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { ...@@ -321,12 +359,18 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
if (errno == EINTR) { if (errno == EINTR) {
continue; continue;
} }
#if FILE_WITH_LOCK
pthread_rwlock_unlock(&(pFile->rwlock));
#endif
return -1; return -1;
} }
nleft -= nwritten; nleft -= nwritten;
tbuf += nwritten; tbuf += nwritten;
} }
fsync(pFile->fd);
#if FILE_WITH_LOCK
pthread_rwlock_unlock(&(pFile->rwlock));
#endif
return count; return count;
} }
...@@ -334,8 +378,15 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) { ...@@ -334,8 +378,15 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) {
if (pFile == NULL) { if (pFile == NULL) {
return 0; return 0;
} }
#if FILE_WITH_LOCK
pthread_rwlock_rdlock(&(pFile->rwlock));
#endif
assert(pFile->fd >= 0); assert(pFile->fd >= 0);
return (int64_t)lseek(pFile->fd, (long)offset, whence); int64_t ret = lseek(pFile->fd, (long)offset, whence);
#if FILE_WITH_LOCK
pthread_rwlock_unlock(&(pFile->rwlock));
#endif
return ret;
} }
int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) { int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
...@@ -637,7 +688,6 @@ void taosFprintfFile(TdFilePtr pFile, const char *format, ...) { ...@@ -637,7 +688,6 @@ void taosFprintfFile(TdFilePtr pFile, const char *format, ...) {
} }
assert(pFile->fp != NULL); assert(pFile->fp != NULL);
char buffer[MAX_FPRINTFLINE_BUFFER_SIZE] = {0};
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vfprintf(pFile->fp, format, ap); vfprintf(pFile->fp, format, ap);
......
...@@ -888,4 +888,99 @@ SysNameInfo taosGetSysNameInfo() { ...@@ -888,4 +888,99 @@ SysNameInfo taosGetSysNameInfo() {
return info; return info;
} }
#endif bool taosGetEmail(char *email, int32_t maxLen) {
\ No newline at end of file const char *filepath = "/usr/local/taos/email";
TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ);
if (pFile == NULL) return false;
if (taosReadFile(pFile, (void *)email, maxLen) < 0) {
taosCloseFile(&pFile);
return false;
}
taosCloseFile(&pFile);
return true;
}
bool taosGetOsReleaseName(char *releaseName, int32_t maxLen) {
char *line = NULL;
size_t size = 0;
bool ret = false;
TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) return false;
while ((size = taosGetLineFile(pFile, &line)) != -1) {
line[size - 1] = '\0';
if (strncmp(line, "PRETTY_NAME", 11) == 0) {
const char *p = strchr(line, '=') + 1;
if (*p == '"') {
p++;
line[size - 2] = 0;
}
tstrncpy(releaseName, p, maxLen);
ret = true;
break;
}
}
if (line != NULL) free(line);
taosCloseFile(&pFile);
return ret;
}
bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) {
char *line = NULL;
size_t size = 0;
int32_t done = 0;
bool ret = false;
TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) return false;
while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) {
line[size - 1] = '\0';
if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) {
const char *v = strchr(line, ':') + 2;
tstrncpy(cpuModel, v, maxLen);
ret = true;
done |= 1;
} else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) {
const char *v = strchr(line, ':') + 2;
*numOfCores = atoi(v);
done |= 2;
}
}
if (line != NULL) free(line);
taosCloseFile(&pFile);
return ret;
}
bool taosGetTotalSysMemoryKB(uint64_t *kb) {
char *line = NULL;
size_t size = 0;
bool ret = false;
TdFilePtr pFile = taosOpenFile("/proc/meminfo", TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) return false;
while ((size = taosGetLineFile(pFile, &line)) != -1) {
line[size - 1] = '\0';
if (strncmp(line, "MemTotal", 8) == 0) {
const char *p = strchr(line, ':') + 1;
while (*p == ' ') p++;
ret = true;
*kb = atoll(p);
break;
}
}
if (line != NULL) free(line);
taosCloseFile(&pFile);
return ret;
}
#endif
...@@ -60,7 +60,7 @@ int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr) { ...@@ -60,7 +60,7 @@ int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr) {
} }
} }
int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs) { int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs) {
int32_t size = taosArrayGetSize(pArgs); int32_t size = taosArrayGetSize(pArgs);
for (int32_t i = 0; i < size; ++i) { for (int32_t i = 0; i < size; ++i) {
SConfigPair *pPair = taosArrayGet(pArgs, i); SConfigPair *pPair = taosArrayGet(pArgs, i);
...@@ -145,10 +145,12 @@ static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) { ...@@ -145,10 +145,12 @@ static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) {
return -1; return -1;
} }
#if 0
if (taosMkDir(fullDir) != 0) { if (taosMkDir(fullDir) != 0) {
uError("failed to create dir:%s realpath:%s since %s", inputDir, fullDir, terrstr()); uError("failed to create dir:%s realpath:%s since %s", inputDir, fullDir, terrstr());
return -1; return -1;
} }
#endif
cfgFreeItem(pItem); cfgFreeItem(pItem);
pItem->str = strdup(fullDir); pItem->str = strdup(fullDir);
...@@ -608,7 +610,10 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { ...@@ -608,7 +610,10 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) {
int32_t olen, vlen, vlen2, vlen3; int32_t olen, vlen, vlen2, vlen3;
ssize_t _bytes = 0; ssize_t _bytes = 0;
// FILE *fp = fopen(filepath, "r"); if (taosIsDir(filepath)) {
return -1;
}
TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM); TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) { if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#define _DEFAULT_SOURCE
#include "thttp.h"
#include "taoserror.h"
#include "tlog.h"
int32_t taosSendHttpReport(const char* server, uint16_t port, const char* pCont, int32_t contLen) {
int32_t code = -1;
SOCKET fd = 0;
uint32_t ip = taosGetIpv4FromFqdn(server);
if (ip == 0xffffffff) {
terrno = TAOS_SYSTEM_ERROR(errno);
uError("failed to get http server:%s ip since %s", server, terrstr());
goto SEND_OVER;
}
fd = taosOpenTcpClientSocket(ip, port, 0);
if (fd < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
uError("failed to create http socket since %s", terrstr());
goto SEND_OVER;
}
char header[4096] = {0};
int32_t headLen = snprintf(header, sizeof(header),
"POST /report HTTP/1.1\n"
"Host: %s\n"
"Content-Type: application/json\n"
"Content-Length: %d\n\n",
server, contLen);
if (taosWriteSocket(fd, (void*)header, headLen) < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
uError("failed to send http header since %s", terrstr());
goto SEND_OVER;
}
if (taosWriteSocket(fd, (void*)pCont, contLen) < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
uError("failed to send http content since %s", terrstr());
goto SEND_OVER;
}
// read something to avoid nginx error 499
if (taosReadSocket(fd, header, 10) < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
uError("failed to receive response since %s", terrstr());
goto SEND_OVER;
}
uInfo("send http to %s:%d, len:%d content: %s", server, port, contLen, pCont);
code = 0;
SEND_OVER:
if (fd != 0) {
taosCloseSocket(fd);
}
return code;
}
...@@ -18,36 +18,73 @@ ...@@ -18,36 +18,73 @@
#include "cJSON.h" #include "cJSON.h"
#include "taoserror.h" #include "taoserror.h"
SJson* tjsonCreateObject() { return cJSON_CreateObject(); } SJson* tjsonCreateObject() {
SJson* pJson = cJSON_CreateObject();
if (pJson == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
}
return pJson;
}
void tjsonDelete(SJson* pJson) { cJSON_Delete((cJSON*)pJson); } void tjsonDelete(SJson* pJson) { cJSON_Delete((cJSON*)pJson); }
int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number) { int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number) {
char tmp[40] = {0}; char tmp[40] = {0};
snprintf(tmp, tListLen(tmp), "%" PRId64, number); snprintf(tmp, sizeof(tmp), "%" PRId64, number);
return tjsonAddStringToObject(pJson, pName, tmp); return tjsonAddStringToObject(pJson, pName, tmp);
} }
int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number) { int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number) {
return (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number) ? TSDB_CODE_FAILED : TSDB_CODE_SUCCESS); if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return TSDB_CODE_FAILED;
}
return TSDB_CODE_SUCCESS;
} }
int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean) { int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean) {
return (NULL == cJSON_AddBoolToObject((cJSON*)pJson, pName, boolean) ? TSDB_CODE_FAILED : TSDB_CODE_SUCCESS); if (NULL == cJSON_AddBoolToObject((cJSON*)pJson, pName, boolean)) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return TSDB_CODE_FAILED;
}
return TSDB_CODE_SUCCESS;
} }
int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal) { int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal) {
return (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal) ? TSDB_CODE_FAILED : TSDB_CODE_SUCCESS); if (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal)) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return TSDB_CODE_FAILED;
}
return TSDB_CODE_SUCCESS;
} }
SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) { return cJSON_AddArrayToObject((cJSON*)pJson, pName); } SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) {
SJson* ret = (SJson*)cJSON_AddArrayToObject((cJSON*)pJson, pName);
if (ret == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
}
return ret;
}
int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem) { int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem) {
return (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem) ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED); if (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem)) {
return TSDB_CODE_SUCCESS;
}
terrno = TSDB_CODE_OUT_OF_MEMORY;
return TSDB_CODE_FAILED;
} }
int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem) { int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem) {
return (cJSON_AddItemToArray((cJSON*)pJson, pItem) ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED); if (cJSON_AddItemToArray((cJSON*)pJson, pItem)) {
return TSDB_CODE_SUCCESS;
}
terrno = TSDB_CODE_OUT_OF_MEMORY;
return TSDB_CODE_FAILED;
} }
int32_t tjsonAddObject(SJson* pJson, const char* pName, FToJson func, const void* pObj) { int32_t tjsonAddObject(SJson* pJson, const char* pName, FToJson func, const void* pObj) {
......
Subproject commit 904e6f0e152e8fe61edfe0a0a9ae497cfde2a72c Subproject commit 08ed39f0a5fcbbfb5a630b945ab3d1998d4b4136
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册