提交 9a9e87ec 编写于 作者: S Shengliang Guan

config from hash to array

上级 f486e7ed
...@@ -75,7 +75,10 @@ typedef struct { ...@@ -75,7 +75,10 @@ typedef struct {
const char *value; const char *value;
} SConfigPair; } SConfigPair;
typedef struct SConfig SConfig; typedef struct SConfig {
ECfgSrcType stype;
SArray *array;
} 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);
...@@ -83,8 +86,6 @@ int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs); // SConfigPair ...@@ -83,8 +86,6 @@ int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs); // SConfigPair
void cfgCleanup(SConfig *pCfg); void cfgCleanup(SConfig *pCfg);
int32_t cfgGetSize(SConfig *pCfg); int32_t cfgGetSize(SConfig *pCfg);
SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter);
void cfgCancelIterate(SConfig *pCfg, SConfigItem *pIter);
SConfigItem *cfgGetItem(SConfig *pCfg, const char *name); SConfigItem *cfgGetItem(SConfig *pCfg, const char *name);
int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype); int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype);
......
...@@ -207,6 +207,8 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *e ...@@ -207,6 +207,8 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *e
} }
static int32_t taosAddClientLogCfg(SConfig *pCfg) { static int32_t taosAddClientLogCfg(SConfig *pCfg) {
if (cfgAddDir(pCfg, "configDir", configDir, 1) != 0) return -1;
if (cfgAddDir(pCfg, "scriptDir", configDir, 1) != 0) return -1;
if (cfgAddDir(pCfg, "logDir", tsLogDir, 1) != 0) return -1; if (cfgAddDir(pCfg, "logDir", tsLogDir, 1) != 0) return -1;
if (cfgAddFloat(pCfg, "minimalLogDirGB", 1.0f, 0.001f, 10000000, 1) != 0) return -1; if (cfgAddFloat(pCfg, "minimalLogDirGB", 1.0f, 0.001f, 10000000, 1) != 0) return -1;
if (cfgAddInt32(pCfg, "numOfLogLines", tsNumOfLogLines, 1000, 2000000000, 1) != 0) return -1; if (cfgAddInt32(pCfg, "numOfLogLines", tsNumOfLogLines, 1000, 2000000000, 1) != 0) return -1;
...@@ -218,8 +220,6 @@ static int32_t taosAddClientLogCfg(SConfig *pCfg) { ...@@ -218,8 +220,6 @@ static int32_t taosAddClientLogCfg(SConfig *pCfg) {
if (cfgAddInt32(pCfg, "tmrDebugFlag", tmrDebugFlag, 0, 255, 1) != 0) return -1; if (cfgAddInt32(pCfg, "tmrDebugFlag", tmrDebugFlag, 0, 255, 1) != 0) return -1;
if (cfgAddInt32(pCfg, "jniDebugFlag", jniDebugFlag, 0, 255, 1) != 0) return -1; if (cfgAddInt32(pCfg, "jniDebugFlag", jniDebugFlag, 0, 255, 1) != 0) return -1;
if (cfgAddInt32(pCfg, "simDebugFlag", 143, 0, 255, 1) != 0) return -1; if (cfgAddInt32(pCfg, "simDebugFlag", 143, 0, 255, 1) != 0) return -1;
if (cfgAddDir(pCfg, "configDir", configDir, 1) != 0) return -1;
if (cfgAddDir(pCfg, "scriptDir", configDir, 1) != 0) return -1;
if (cfgAddInt32(pCfg, "debugFlag", 0, 0, 255, 1) != 0) return -1; if (cfgAddInt32(pCfg, "debugFlag", 0, 0, 255, 1) != 0) return -1;
return 0; return 0;
} }
...@@ -488,10 +488,10 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU ...@@ -488,10 +488,10 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU
if (taosAddClientLogCfg(tsCfg) != 0) return -1; if (taosAddClientLogCfg(tsCfg) != 0) return -1;
if (taosAddClientCfg(tsCfg) != 0) return -1; if (taosAddClientCfg(tsCfg) != 0) return -1;
} else { } else {
if (taosAddClientLogCfg(tsCfg) != 0) return -1;
if (taosAddServerLogCfg(tsCfg) != 0) return -1;
if (taosAddClientCfg(tsCfg) != 0) return -1; if (taosAddClientCfg(tsCfg) != 0) return -1;
if (taosAddServerCfg(tsCfg) != 0) return -1; if (taosAddServerCfg(tsCfg) != 0) return -1;
if (taosAddClientLogCfg(tsCfg) != 0) return -1;
if (taosAddServerLogCfg(tsCfg) != 0) return -1;
} }
taosAddSystemCfg(tsCfg); taosAddSystemCfg(tsCfg);
......
...@@ -16,18 +16,12 @@ ...@@ -16,18 +16,12 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "tconfig.h" #include "tconfig.h"
#include "taoserror.h" #include "taoserror.h"
#include "thash.h"
#include "tlog.h" #include "tlog.h"
#include "tutil.h" #include "tutil.h"
#define CFG_NAME_PRINT_LEN 24 #define CFG_NAME_PRINT_LEN 24
#define CFG_SRC_PRINT_LEN 12 #define CFG_SRC_PRINT_LEN 12
typedef struct SConfig {
ECfgSrcType stype;
SHashObj *hash;
} SConfig;
int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath); int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath);
int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath); int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath);
int32_t cfgLoadFromEnvVar(SConfig *pConfig); int32_t cfgLoadFromEnvVar(SConfig *pConfig);
...@@ -41,8 +35,8 @@ SConfig *cfgInit() { ...@@ -41,8 +35,8 @@ SConfig *cfgInit() {
return NULL; return NULL;
} }
pCfg->hash = taosHashInit(16, MurmurHash3_32, false, HASH_NO_LOCK); pCfg->array = taosArrayInit(32, sizeof(SConfigItem));
if (pCfg->hash == NULL) { if (pCfg->array == NULL) {
free(pCfg); free(pCfg);
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL; return NULL;
...@@ -91,25 +85,18 @@ static void cfgFreeItem(SConfigItem *pItem) { ...@@ -91,25 +85,18 @@ static void cfgFreeItem(SConfigItem *pItem) {
void cfgCleanup(SConfig *pCfg) { void cfgCleanup(SConfig *pCfg) {
if (pCfg != NULL) { if (pCfg != NULL) {
if (pCfg->hash != NULL) { int32_t size = taosArrayGetSize(pCfg->array);
SConfigItem *pItem = taosHashIterate(pCfg->hash, NULL); for (int32_t i = 0; i < size; ++i) {
while (pItem != NULL) { SConfigItem *pItem = taosArrayGet(pCfg->array, i);
cfgFreeItem(pItem); cfgFreeItem(pItem);
tfree(pItem->name); tfree(pItem->name);
pItem = taosHashIterate(pCfg->hash, pItem);
}
taosHashCleanup(pCfg->hash);
pCfg->hash == NULL;
} }
taosArrayDestroy(pCfg->array);
free(pCfg); free(pCfg);
} }
} }
int32_t cfgGetSize(SConfig *pCfg) { return taosHashGetSize(pCfg->hash); } int32_t cfgGetSize(SConfig *pCfg) { return taosArrayGetSize(pCfg->array); }
SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashIterate(pCfg->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) {
cfgFreeItem(pItem); cfgFreeItem(pItem);
...@@ -358,16 +345,16 @@ int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcTy ...@@ -358,16 +345,16 @@ int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcTy
} }
SConfigItem *cfgGetItem(SConfig *pCfg, const char *name) { SConfigItem *cfgGetItem(SConfig *pCfg, const char *name) {
int32_t len = strlen(name); int32_t size = taosArrayGetSize(pCfg->array);
char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; for (int32_t i = 0; i < size; ++i) {
strntolower(lowcaseName, name, TMIN(CFG_NAME_MAX_LEN, len)); SConfigItem *pItem = taosArrayGet(pCfg->array, i);
if (strcasecmp(pItem->name, name) == 0) {
SConfigItem *pItem = taosHashGet(pCfg->hash, lowcaseName, len + 1); return pItem;
if (pItem == NULL) { }
terrno = TSDB_CODE_CFG_NOT_FOUND;
} }
return pItem; terrno = TSDB_CODE_CFG_NOT_FOUND;
return NULL;
} }
static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) { static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) {
...@@ -382,7 +369,7 @@ static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) { ...@@ -382,7 +369,7 @@ static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) {
char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
strntolower(lowcaseName, name, TMIN(CFG_NAME_MAX_LEN, len)); strntolower(lowcaseName, name, TMIN(CFG_NAME_MAX_LEN, len));
if (taosHashPut(pCfg->hash, lowcaseName, len + 1, pItem, sizeof(SConfigItem)) != 0) { if (taosArrayPush(pCfg->array, pItem) == NULL) {
if (pItem->dtype == CFG_DTYPE_STRING) { if (pItem->dtype == CFG_DTYPE_STRING) {
free(pItem->str); free(pItem->str);
} }
...@@ -535,8 +522,9 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) { ...@@ -535,8 +522,9 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) {
char src[CFG_SRC_PRINT_LEN + 1] = {0}; char src[CFG_SRC_PRINT_LEN + 1] = {0};
char name[CFG_NAME_PRINT_LEN + 1] = {0}; char name[CFG_NAME_PRINT_LEN + 1] = {0};
SConfigItem *pItem = cfgIterate(pCfg, NULL); int32_t size = taosArrayGetSize(pCfg->array);
while (pItem != NULL) { for (int32_t i = 0; i < size; ++i) {
SConfigItem *pItem = taosArrayGet(pCfg->array, i);
if (tsc && !pItem->tsc) continue; if (tsc && !pItem->tsc) continue;
tstrncpy(src, cfgStypeStr(pItem->stype), CFG_SRC_PRINT_LEN); tstrncpy(src, cfgStypeStr(pItem->stype), CFG_SRC_PRINT_LEN);
for (int32_t i = 0; i < CFG_SRC_PRINT_LEN; ++i) { for (int32_t i = 0; i < CFG_SRC_PRINT_LEN; ++i) {
...@@ -595,7 +583,6 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) { ...@@ -595,7 +583,6 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) {
} }
break; break;
} }
pItem = cfgIterate(pCfg, pItem);
} }
if (dump) { if (dump) {
......
...@@ -62,9 +62,9 @@ TEST_F(CfgTest, 02_Basic) { ...@@ -62,9 +62,9 @@ TEST_F(CfgTest, 02_Basic) {
EXPECT_EQ(cfgGetSize(pConfig), 6); EXPECT_EQ(cfgGetSize(pConfig), 6);
int32_t size = 0; int32_t size = taosArrayGetSize(pConfig->array);
SConfigItem *pItem = cfgIterate(pConfig, NULL); for (int32_t i = 0; i < size; ++i) {
while (pItem != NULL) { SConfigItem *pItem = (SConfigItem *)taosArrayGet(pConfig->array, i);
switch (pItem->dtype) { switch (pItem->dtype) {
case CFG_DTYPE_BOOL: case CFG_DTYPE_BOOL:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->bval); printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->bval);
...@@ -89,13 +89,10 @@ TEST_F(CfgTest, 02_Basic) { ...@@ -89,13 +89,10 @@ TEST_F(CfgTest, 02_Basic) {
break; break;
} }
size++; size++;
pItem = cfgIterate(pConfig, pItem);
} }
cfgCancelIterate(pConfig, pItem);
EXPECT_EQ(cfgGetSize(pConfig), 6); EXPECT_EQ(cfgGetSize(pConfig), 6);
pItem = cfgGetItem(pConfig, "test_bool"); SConfigItem *pItem = cfgGetItem(pConfig, "test_bool");
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT); EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_BOOL); EXPECT_EQ(pItem->dtype, CFG_DTYPE_BOOL);
EXPECT_STREQ(pItem->name, "test_bool"); EXPECT_STREQ(pItem->name, "test_bool");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册