config from hash to array
This commit is contained in:
parent
f486e7edd5
commit
9a9e87ec34
|
@ -75,7 +75,10 @@ typedef struct {
|
|||
const char *value;
|
||||
} SConfigPair;
|
||||
|
||||
typedef struct SConfig SConfig;
|
||||
typedef struct SConfig {
|
||||
ECfgSrcType stype;
|
||||
SArray *array;
|
||||
} SConfig;
|
||||
|
||||
SConfig *cfgInit();
|
||||
int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr);
|
||||
|
@ -83,8 +86,6 @@ int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs); // SConfigPair
|
|||
void cfgCleanup(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);
|
||||
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
|
|||
}
|
||||
|
||||
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 (cfgAddFloat(pCfg, "minimalLogDirGB", 1.0f, 0.001f, 10000000, 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) {
|
|||
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, "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;
|
||||
return 0;
|
||||
}
|
||||
|
@ -488,10 +488,10 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU
|
|||
if (taosAddClientLogCfg(tsCfg) != 0) return -1;
|
||||
if (taosAddClientCfg(tsCfg) != 0) return -1;
|
||||
} else {
|
||||
if (taosAddClientLogCfg(tsCfg) != 0) return -1;
|
||||
if (taosAddServerLogCfg(tsCfg) != 0) return -1;
|
||||
if (taosAddClientCfg(tsCfg) != 0) return -1;
|
||||
if (taosAddServerCfg(tsCfg) != 0) return -1;
|
||||
if (taosAddClientLogCfg(tsCfg) != 0) return -1;
|
||||
if (taosAddServerLogCfg(tsCfg) != 0) return -1;
|
||||
}
|
||||
taosAddSystemCfg(tsCfg);
|
||||
|
||||
|
|
|
@ -16,18 +16,12 @@
|
|||
#define _DEFAULT_SOURCE
|
||||
#include "tconfig.h"
|
||||
#include "taoserror.h"
|
||||
#include "thash.h"
|
||||
#include "tlog.h"
|
||||
#include "tutil.h"
|
||||
|
||||
#define CFG_NAME_PRINT_LEN 24
|
||||
#define CFG_SRC_PRINT_LEN 12
|
||||
|
||||
typedef struct SConfig {
|
||||
ECfgSrcType stype;
|
||||
SHashObj *hash;
|
||||
} SConfig;
|
||||
|
||||
int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath);
|
||||
int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath);
|
||||
int32_t cfgLoadFromEnvVar(SConfig *pConfig);
|
||||
|
@ -41,8 +35,8 @@ SConfig *cfgInit() {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
pCfg->hash = taosHashInit(16, MurmurHash3_32, false, HASH_NO_LOCK);
|
||||
if (pCfg->hash == NULL) {
|
||||
pCfg->array = taosArrayInit(32, sizeof(SConfigItem));
|
||||
if (pCfg->array == NULL) {
|
||||
free(pCfg);
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
return NULL;
|
||||
|
@ -91,25 +85,18 @@ static void cfgFreeItem(SConfigItem *pItem) {
|
|||
|
||||
void cfgCleanup(SConfig *pCfg) {
|
||||
if (pCfg != NULL) {
|
||||
if (pCfg->hash != NULL) {
|
||||
SConfigItem *pItem = taosHashIterate(pCfg->hash, NULL);
|
||||
while (pItem != NULL) {
|
||||
cfgFreeItem(pItem);
|
||||
tfree(pItem->name);
|
||||
pItem = taosHashIterate(pCfg->hash, pItem);
|
||||
}
|
||||
taosHashCleanup(pCfg->hash);
|
||||
pCfg->hash == NULL;
|
||||
int32_t size = taosArrayGetSize(pCfg->array);
|
||||
for (int32_t i = 0; i < size; ++i) {
|
||||
SConfigItem *pItem = taosArrayGet(pCfg->array, i);
|
||||
cfgFreeItem(pItem);
|
||||
tfree(pItem->name);
|
||||
}
|
||||
taosArrayDestroy(pCfg->array);
|
||||
free(pCfg);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t cfgGetSize(SConfig *pCfg) { return taosHashGetSize(pCfg->hash); }
|
||||
|
||||
SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashIterate(pCfg->hash, pIter); }
|
||||
|
||||
void cfgCancelIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashCancelIterate(pCfg->hash, pIter); }
|
||||
int32_t cfgGetSize(SConfig *pCfg) { return taosArrayGetSize(pCfg->array); }
|
||||
|
||||
static int32_t cfgCheckAndSetTimezone(SConfigItem *pItem, const char *timezone) {
|
||||
cfgFreeItem(pItem);
|
||||
|
@ -358,16 +345,16 @@ int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcTy
|
|||
}
|
||||
|
||||
SConfigItem *cfgGetItem(SConfig *pCfg, const char *name) {
|
||||
int32_t len = strlen(name);
|
||||
char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
|
||||
strntolower(lowcaseName, name, TMIN(CFG_NAME_MAX_LEN, len));
|
||||
|
||||
SConfigItem *pItem = taosHashGet(pCfg->hash, lowcaseName, len + 1);
|
||||
if (pItem == NULL) {
|
||||
terrno = TSDB_CODE_CFG_NOT_FOUND;
|
||||
int32_t size = taosArrayGetSize(pCfg->array);
|
||||
for (int32_t i = 0; i < size; ++i) {
|
||||
SConfigItem *pItem = taosArrayGet(pCfg->array, i);
|
||||
if (strcasecmp(pItem->name, name) == 0) {
|
||||
return pItem;
|
||||
}
|
||||
}
|
||||
|
||||
return pItem;
|
||||
terrno = TSDB_CODE_CFG_NOT_FOUND;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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};
|
||||
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) {
|
||||
free(pItem->str);
|
||||
}
|
||||
|
@ -535,8 +522,9 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) {
|
|||
char src[CFG_SRC_PRINT_LEN + 1] = {0};
|
||||
char name[CFG_NAME_PRINT_LEN + 1] = {0};
|
||||
|
||||
SConfigItem *pItem = cfgIterate(pCfg, NULL);
|
||||
while (pItem != NULL) {
|
||||
int32_t size = taosArrayGetSize(pCfg->array);
|
||||
for (int32_t i = 0; i < size; ++i) {
|
||||
SConfigItem *pItem = taosArrayGet(pCfg->array, i);
|
||||
if (tsc && !pItem->tsc) continue;
|
||||
tstrncpy(src, cfgStypeStr(pItem->stype), CFG_SRC_PRINT_LEN);
|
||||
for (int32_t i = 0; i < CFG_SRC_PRINT_LEN; ++i) {
|
||||
|
@ -595,7 +583,6 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) {
|
|||
}
|
||||
break;
|
||||
}
|
||||
pItem = cfgIterate(pCfg, pItem);
|
||||
}
|
||||
|
||||
if (dump) {
|
||||
|
|
|
@ -62,9 +62,9 @@ TEST_F(CfgTest, 02_Basic) {
|
|||
|
||||
EXPECT_EQ(cfgGetSize(pConfig), 6);
|
||||
|
||||
int32_t size = 0;
|
||||
SConfigItem *pItem = cfgIterate(pConfig, NULL);
|
||||
while (pItem != NULL) {
|
||||
int32_t size = taosArrayGetSize(pConfig->array);
|
||||
for (int32_t i = 0; i < size; ++i) {
|
||||
SConfigItem *pItem = (SConfigItem *)taosArrayGet(pConfig->array, i);
|
||||
switch (pItem->dtype) {
|
||||
case CFG_DTYPE_BOOL:
|
||||
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->bval);
|
||||
|
@ -89,13 +89,10 @@ TEST_F(CfgTest, 02_Basic) {
|
|||
break;
|
||||
}
|
||||
size++;
|
||||
pItem = cfgIterate(pConfig, pItem);
|
||||
}
|
||||
cfgCancelIterate(pConfig, pItem);
|
||||
|
||||
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->dtype, CFG_DTYPE_BOOL);
|
||||
EXPECT_STREQ(pItem->name, "test_bool");
|
||||
|
|
Loading…
Reference in New Issue