提交 0a0dfd5f 编写于 作者: S Shengliang Guan

rename var

上级 460f7890
...@@ -73,28 +73,30 @@ typedef struct SConfigItem { ...@@ -73,28 +73,30 @@ typedef struct SConfigItem {
typedef struct SConfig SConfig; typedef struct SConfig SConfig;
SConfig *cfgInit(); SConfig *cfgInit();
int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr); int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr);
void cfgCleanup(SConfig *pConfig); void cfgCleanup(SConfig *pCfg);
int32_t cfgGetSize(SConfig *pConfig); int32_t cfgGetSize(SConfig *pCfg);
SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter); SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter);
void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter); void cfgCancelIterate(SConfig *pCfg, SConfigItem *pIter);
SConfigItem *cfgGetItem(SConfig *pConfig, const char *name); SConfigItem *cfgGetItem(SConfig *pCfg, const char *name);
int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal); int32_t cfgAddBool(SConfig *pCfg, const char *name, bool defaultVal);
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval); int32_t cfgAddInt32(SConfig *pCfg, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval);
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval); int32_t cfgAddInt64(SConfig *pCfg, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval);
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double minval, double maxval); int32_t cfgAddFloat(SConfig *pCfg, const char *name, float defaultVal, double minval, double maxval);
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal); int32_t cfgAddString(SConfig *pCfg, const char *name, const char *defaultVal);
int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVa); int32_t cfgAddIpStr(SConfig *pCfg, const char *name, const char *defaultVa);
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal); int32_t cfgAddDir(SConfig *pCfg, const char *name, const char *defaultVal);
int32_t cfgAddLocale(SConfig *pConfig, const char *name, const char *defaultVal); int32_t cfgAddLocale(SConfig *pCfg, const char *name, const char *defaultVal);
int32_t cfgAddCharset(SConfig *pConfig, const char *name, const char *defaultVal); int32_t cfgAddCharset(SConfig *pCfg, const char *name, const char *defaultVal);
int32_t cfgAddTimezone(SConfig *pConfig, const char *name, const char *defaultVal); int32_t cfgAddTimezone(SConfig *pCfg, const char *name, const char *defaultVal);
const char *cfgStypeStr(ECfgSrcType type); const char *cfgStypeStr(ECfgSrcType type);
const char *cfgDtypeStr(ECfgDataType type); const char *cfgDtypeStr(ECfgDataType type);
void cfgDumpCfg(SConfig *pCfg);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -74,13 +74,11 @@ SConfig *dmnReadCfg(const char *cfgDir, const char *envFile, const char *apolloU ...@@ -74,13 +74,11 @@ SConfig *dmnReadCfg(const char *cfgDir, const char *envFile, const char *apolloU
if (dmnCheckCfg(pCfg) != 0) { if (dmnCheckCfg(pCfg) != 0) {
uError("failed to check cfg since %s", terrstr()); uError("failed to check cfg since %s", terrstr());
} cfgCleanup(pCfg);
if (taosCheckAndPrintCfg() != 0) {
uError("failed to check config");
return NULL; return NULL;
} }
cfgDumpCfg(pCfg);
return pCfg; return pCfg;
} }
......
...@@ -21,52 +21,52 @@ ...@@ -21,52 +21,52 @@
#include "ttimezone.h" #include "ttimezone.h"
SConfig *cfgInit() { SConfig *cfgInit() {
SConfig *pConfig = calloc(1, sizeof(SConfig)); SConfig *pCfg = calloc(1, sizeof(SConfig));
if (pConfig == NULL) { if (pCfg == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL; return NULL;
} }
pConfig->hash = taosHashInit(16, MurmurHash3_32, false, HASH_NO_LOCK); pCfg->hash = taosHashInit(16, MurmurHash3_32, false, HASH_NO_LOCK);
if (pConfig->hash == NULL) { if (pCfg->hash == NULL) {
free(pConfig); free(pCfg);
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL; return NULL;
} }
return pConfig; return pCfg;
} }
int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr) { int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr) {
switch (cfgType) { switch (cfgType) {
case CFG_STYPE_CFG_FILE: case CFG_STYPE_CFG_FILE:
return cfgLoadFromCfgFile(pConfig, sourceStr); return cfgLoadFromCfgFile(pCfg, sourceStr);
case CFG_STYPE_ENV_FILE: case CFG_STYPE_ENV_FILE:
return cfgLoadFromEnvFile(pConfig, sourceStr); return cfgLoadFromEnvFile(pCfg, sourceStr);
case CFG_STYPE_ENV_VAR: case CFG_STYPE_ENV_VAR:
return cfgLoadFromEnvVar(pConfig); return cfgLoadFromEnvVar(pCfg);
case CFG_STYPE_APOLLO_URL: case CFG_STYPE_APOLLO_URL:
return cfgLoadFromApollUrl(pConfig, sourceStr); return cfgLoadFromApollUrl(pCfg, sourceStr);
default: default:
return -1; return -1;
} }
} }
void cfgCleanup(SConfig *pConfig) { void cfgCleanup(SConfig *pCfg) {
if (pConfig != NULL) { if (pCfg != NULL) {
if (pConfig->hash != NULL) { if (pCfg->hash != NULL) {
taosHashCleanup(pConfig->hash); taosHashCleanup(pCfg->hash);
pConfig->hash == NULL; pCfg->hash == NULL;
} }
free(pConfig); free(pCfg);
} }
} }
int32_t cfgGetSize(SConfig *pConfig) { return taosHashGetSize(pConfig->hash); } int32_t cfgGetSize(SConfig *pCfg) { return taosHashGetSize(pCfg->hash); }
SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashIterate(pConfig->hash, pIter); } SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashIterate(pCfg->hash, pIter); }
void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashCancelIterate(pConfig->hash, pIter); } void cfgCancelIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashCancelIterate(pCfg->hash, pIter); }
static int32_t cfgCheckAndSetTimezone(SConfigItem *pItem, const char *timezone) { static int32_t cfgCheckAndSetTimezone(SConfigItem *pItem, const char *timezone) {
tfree(pItem->str); tfree(pItem->str);
...@@ -292,8 +292,8 @@ static int32_t cfgSetTimezone(SConfigItem *pItem, const char *value, ECfgSrcType ...@@ -292,8 +292,8 @@ static int32_t cfgSetTimezone(SConfigItem *pItem, const char *value, ECfgSrcType
return 0; return 0;
} }
int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSrcType stype) { int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype) {
SConfigItem *pItem = cfgGetItem(pConfig, name); SConfigItem *pItem = cfgGetItem(pCfg, name);
if (pItem == NULL) { if (pItem == NULL) {
return -1; return -1;
} }
...@@ -328,12 +328,12 @@ int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSr ...@@ -328,12 +328,12 @@ int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSr
return -1; return -1;
} }
SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) { SConfigItem *cfgGetItem(SConfig *pCfg, const char *name) {
char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
memcpy(lowcaseName, name, CFG_NAME_MAX_LEN); memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
strntolower(lowcaseName, name, CFG_NAME_MAX_LEN); strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);
SConfigItem *pItem = taosHashGet(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1); SConfigItem *pItem = taosHashGet(pCfg->hash, lowcaseName, strlen(lowcaseName) + 1);
if (pItem == NULL) { if (pItem == NULL) {
terrno = TSDB_CODE_CFG_NOT_FOUND; terrno = TSDB_CODE_CFG_NOT_FOUND;
} }
...@@ -341,7 +341,7 @@ SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) { ...@@ -341,7 +341,7 @@ SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) {
return pItem; return pItem;
} }
static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name) { static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) {
pItem->stype = CFG_STYPE_DEFAULT; pItem->stype = CFG_STYPE_DEFAULT;
pItem->name = strdup(name); pItem->name = strdup(name);
if (pItem->name == NULL) { if (pItem->name == NULL) {
...@@ -353,7 +353,7 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name ...@@ -353,7 +353,7 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name
memcpy(lowcaseName, name, CFG_NAME_MAX_LEN); memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
strntolower(lowcaseName, name, CFG_NAME_MAX_LEN); strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);
if (taosHashPut(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) { if (taosHashPut(pCfg->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) {
if (pItem->dtype == CFG_DTYPE_STRING) { if (pItem->dtype == CFG_DTYPE_STRING) {
free(pItem->str); free(pItem->str);
} }
...@@ -365,94 +365,94 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name ...@@ -365,94 +365,94 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name
return 0; return 0;
} }
int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal) { int32_t cfgAddBool(SConfig *pCfg, const char *name, bool defaultVal) {
SConfigItem item = {.dtype = CFG_DTYPE_BOOL, .bval = defaultVal}; SConfigItem item = {.dtype = CFG_DTYPE_BOOL, .bval = defaultVal};
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval) { int32_t cfgAddInt32(SConfig *pCfg, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval) {
if (defaultVal < minval || defaultVal > maxval) { if (defaultVal < minval || defaultVal > maxval) {
terrno = TSDB_CODE_OUT_OF_RANGE; terrno = TSDB_CODE_OUT_OF_RANGE;
return -1; return -1;
} }
SConfigItem item = {.dtype = CFG_DTYPE_INT32, .i32 = defaultVal, .imin = minval, .imax = maxval}; SConfigItem item = {.dtype = CFG_DTYPE_INT32, .i32 = defaultVal, .imin = minval, .imax = maxval};
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval) { int32_t cfgAddInt64(SConfig *pCfg, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval) {
if (defaultVal < minval || defaultVal > maxval) { if (defaultVal < minval || defaultVal > maxval) {
terrno = TSDB_CODE_OUT_OF_RANGE; terrno = TSDB_CODE_OUT_OF_RANGE;
return -1; return -1;
} }
SConfigItem item = {.dtype = CFG_DTYPE_INT64, .i64 = defaultVal, .imin = minval, .imax = maxval}; SConfigItem item = {.dtype = CFG_DTYPE_INT64, .i64 = defaultVal, .imin = minval, .imax = maxval};
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double minval, double maxval) { int32_t cfgAddFloat(SConfig *pCfg, const char *name, float defaultVal, double minval, double maxval) {
if (defaultVal < minval || defaultVal > maxval) { if (defaultVal < minval || defaultVal > maxval) {
terrno = TSDB_CODE_OUT_OF_RANGE; terrno = TSDB_CODE_OUT_OF_RANGE;
return -1; return -1;
} }
SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .fval = defaultVal, .fmin = minval, .fmax = maxval}; SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .fval = defaultVal, .fmin = minval, .fmax = maxval};
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal) { int32_t cfgAddString(SConfig *pCfg, const char *name, const char *defaultVal) {
SConfigItem item = {.dtype = CFG_DTYPE_STRING}; SConfigItem item = {.dtype = CFG_DTYPE_STRING};
item.str = strdup(defaultVal); item.str = strdup(defaultVal);
if (item.str == NULL) { if (item.str == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1; return -1;
} }
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal) { int32_t cfgAddIpStr(SConfig *pCfg, const char *name, const char *defaultVal) {
SConfigItem item = {.dtype = CFG_DTYPE_IPSTR}; SConfigItem item = {.dtype = CFG_DTYPE_IPSTR};
if (cfgCheckAndSetIpStr(&item, defaultVal) != 0) { if (cfgCheckAndSetIpStr(&item, defaultVal) != 0) {
return -1; return -1;
} }
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal) { int32_t cfgAddDir(SConfig *pCfg, const char *name, const char *defaultVal) {
SConfigItem item = {.dtype = CFG_DTYPE_DIR}; SConfigItem item = {.dtype = CFG_DTYPE_DIR};
if (cfgCheckAndSetDir(&item, defaultVal) != 0) { if (cfgCheckAndSetDir(&item, defaultVal) != 0) {
return -1; return -1;
} }
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddLocale(SConfig *pConfig, const char *name, const char *defaultVal) { int32_t cfgAddLocale(SConfig *pCfg, const char *name, const char *defaultVal) {
SConfigItem item = {.dtype = CFG_DTYPE_LOCALE}; SConfigItem item = {.dtype = CFG_DTYPE_LOCALE};
if (cfgCheckAndSetLocale(&item, defaultVal) != 0) { if (cfgCheckAndSetLocale(&item, defaultVal) != 0) {
return -1; return -1;
} }
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddCharset(SConfig *pConfig, const char *name, const char *defaultVal) { int32_t cfgAddCharset(SConfig *pCfg, const char *name, const char *defaultVal) {
SConfigItem item = {.dtype = CFG_DTYPE_CHARSET}; SConfigItem item = {.dtype = CFG_DTYPE_CHARSET};
if (cfgCheckAndSetCharset(&item, defaultVal) != 0) { if (cfgCheckAndSetCharset(&item, defaultVal) != 0) {
return -1; return -1;
} }
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
int32_t cfgAddTimezone(SConfig *pConfig, const char *name, const char *defaultVal) { int32_t cfgAddTimezone(SConfig *pCfg, const char *name, const char *defaultVal) {
SConfigItem item = {.dtype = CFG_DTYPE_TIMEZONE}; SConfigItem item = {.dtype = CFG_DTYPE_TIMEZONE};
if (cfgCheckAndSetTimezone(&item, defaultVal) != 0) { if (cfgCheckAndSetTimezone(&item, defaultVal) != 0) {
return -1; return -1;
} }
return cfgAddItem(pConfig, &item, name); return cfgAddItem(pCfg, &item, name);
} }
const char *cfgStypeStr(ECfgSrcType type) { const char *cfgStypeStr(ECfgSrcType type) {
...@@ -537,20 +537,54 @@ void cfgPrintCfg(SConfig *pCfg) { ...@@ -537,20 +537,54 @@ void cfgPrintCfg(SConfig *pCfg) {
} }
} }
int32_t cfgCheck(SConfig *pConfig) { void cfgDumpCfg(SConfig *pCfg) {
uInfo("global config");
uInfo("==================================");
SConfigItem *pItem = cfgIterate(pCfg, NULL);
while (pItem != NULL) {
switch (pItem->dtype) {
case CFG_DTYPE_BOOL:
uInfo("cfg:%s, value:%u src:%s", pItem->name, pItem->bval, cfgStypeStr(pItem->stype));
break;
case CFG_DTYPE_INT32:
uInfo("cfg:%s, value:%d src:%s", pItem->name, pItem->i32, cfgStypeStr(pItem->stype));
break;
case CFG_DTYPE_INT64:
uInfo("cfg:%s, value:%" PRId64 " src:%s", pItem->name, pItem->i64, cfgStypeStr(pItem->stype));
break;
case CFG_DTYPE_FLOAT:
uInfo("cfg:%s, value:%f src:%s", pItem->name, pItem->fval, cfgStypeStr(pItem->stype));
break;
case CFG_DTYPE_STRING:
case CFG_DTYPE_IPSTR:
case CFG_DTYPE_DIR:
case CFG_DTYPE_LOCALE:
case CFG_DTYPE_CHARSET:
case CFG_DTYPE_TIMEZONE:
uInfo("cfg:%s, value:%s src:%s", pItem->name, pItem->str, cfgStypeStr(pItem->stype));
break;
}
pItem = cfgIterate(pCfg, pItem);
}
uInfo("==================================");
}
int32_t cfgCheck(SConfig *pCfg) {
SConfigItem *pItem = NULL; SConfigItem *pItem = NULL;
pItem = cfgGetItem(pConfig, "localFqdn"); pItem = cfgGetItem(pCfg, "localFqdn");
if (pItem != NULL) { if (pItem != NULL) {
tstrncpy(tsLocalFqdn, pItem->str, TSDB_FQDN_LEN); tstrncpy(tsLocalFqdn, pItem->str, TSDB_FQDN_LEN);
} }
pItem = cfgGetItem(pConfig, "serverPort"); pItem = cfgGetItem(pCfg, "serverPort");
if (pItem != NULL) { if (pItem != NULL) {
tsServerPort = (uint16_t)pItem->i32; tsServerPort = (uint16_t)pItem->i32;
} }
pItem = cfgGetItem(pConfig, "firstEp"); pItem = cfgGetItem(pCfg, "firstEp");
if (pItem != NULL) { if (pItem != NULL) {
tstrncpy(tsFirst, pItem->str, TSDB_EP_LEN); tstrncpy(tsFirst, pItem->str, TSDB_EP_LEN);
} }
...@@ -566,7 +600,7 @@ int32_t cfgCheck(SConfig *pConfig) { ...@@ -566,7 +600,7 @@ int32_t cfgCheck(SConfig *pConfig) {
snprintf(tsFirst, TSDB_EP_LEN, "%s:%u", ep.fqdn, ep.port); snprintf(tsFirst, TSDB_EP_LEN, "%s:%u", ep.fqdn, ep.port);
} }
pItem = cfgGetItem(pConfig, "secondEp"); pItem = cfgGetItem(pCfg, "secondEp");
if (pItem != NULL) { if (pItem != NULL) {
tstrncpy(tsSecond, pItem->str, TSDB_EP_LEN); tstrncpy(tsSecond, pItem->str, TSDB_EP_LEN);
} }
...@@ -578,7 +612,7 @@ int32_t cfgCheck(SConfig *pConfig) { ...@@ -578,7 +612,7 @@ int32_t cfgCheck(SConfig *pConfig) {
snprintf(tsSecond, TSDB_EP_LEN, "%s:%u", ep.fqdn, ep.port); snprintf(tsSecond, TSDB_EP_LEN, "%s:%u", ep.fqdn, ep.port);
} }
pItem = cfgGetItem(pConfig, "dataDir"); pItem = cfgGetItem(pCfg, "dataDir");
if (pItem != NULL) { if (pItem != NULL) {
tstrncpy(tsDataDir, pItem->str, PATH_MAX); tstrncpy(tsDataDir, pItem->str, PATH_MAX);
} }
...@@ -602,7 +636,7 @@ int32_t cfgCheck(SConfig *pConfig) { ...@@ -602,7 +636,7 @@ int32_t cfgCheck(SConfig *pConfig) {
tsSetTimeZone(); tsSetTimeZone();
// } // }
pItem = cfgGetItem(pConfig, "numOfCores"); pItem = cfgGetItem(pCfg, "numOfCores");
if (pItem != NULL) { if (pItem != NULL) {
tsNumOfCores = pItem->i32; tsNumOfCores = pItem->i32;
} }
...@@ -615,7 +649,7 @@ int32_t cfgCheck(SConfig *pConfig) { ...@@ -615,7 +649,7 @@ int32_t cfgCheck(SConfig *pConfig) {
tsQueryBufferSizeBytes = tsQueryBufferSize * 1048576UL; tsQueryBufferSizeBytes = tsQueryBufferSize * 1048576UL;
} }
cfgPrintCfg(pConfig); cfgPrintCfg(pCfg);
return 0; return 0;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册