From 863ad191d53e6da8e9f4c38e34dd1d7ea0c1c935 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 21 Feb 2022 15:09:10 +0800 Subject: [PATCH] config interface --- include/libs/config/config.h | 87 +++++++++++--------- source/libs/config/inc/cfgInt.h | 5 +- source/libs/config/src/config.c | 138 +++++++++++++++++++++++++++++++- 3 files changed, 183 insertions(+), 47 deletions(-) diff --git a/include/libs/config/config.h b/include/libs/config/config.h index fb484ff379..a04ebad981 100644 --- a/include/libs/config/config.h +++ b/include/libs/config/config.h @@ -24,7 +24,7 @@ extern "C" { #endif typedef enum { - CFG_TYPE_NONE, + CFG_TYPE_DEFAULT, CFG_TYPE_TAOS_CFG, CFG_TYPE_DOT_ENV, CFG_TYPE_ENV_VAR, @@ -33,8 +33,8 @@ typedef enum { } ECfgType; typedef enum { - CFG_DYPE_NONE, - CFG_DYPE_BOOL, + CFG_DTYPE_NONE, + CFG_DTYPE_BOOL, CFG_DTYPE_INT8, CFG_DTYPE_UINT8, CFG_DTYPE_INT16, @@ -62,6 +62,31 @@ typedef enum { CFG_UTYPE_MS } ECfgUnitType; +typedef struct SConfigItem { + ECfgType stype; + ECfgUnitType utype; + ECfgDataType dtype; + const char *name; + union { + bool boolVal; + uint8_t uint8Val; + int8_t int8Val; + uint16_t uint16Val; + int16_t int16Val; + uint32_t uint32Val; + int32_t int32Val; + uint64_t uint64Val; + int64_t int64Val; + float floatVal; + double doubleVal; + char *strVal; + char *fqdnVal; + char *ipstrVal; + char *dirVal; + char *fileVal; + }; +} SConfigItem; + typedef struct SConfig SConfig; SConfig *cfgInit(); @@ -69,44 +94,26 @@ int32_t cfgLoad(SConfig *pConfig, ECfgType cfgType, const char *sourceStr); void cfgCleanup(SConfig *pConfig); int32_t cfgGetSize(SConfig *pConfig); -void *cfgIterate(SConfig *pConfig, void *p); -void cfgCancelIterate(SConfig *pConfig, void *p); -ECfgUnitType cfgGetUtype(SConfig *pConfig, const char *name); -ECfgDataType cfgGetDtype(SConfig *pConfig, const char *name); - -void cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal, ECfgUnitType utype); -void cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, ECfgUnitType utype); -void cfgAddUInt8(SConfig *pConfig, const char *name, uint8_t defaultVal, ECfgUnitType utype); -void cfgAddInt16(SConfig *pConfig, const char *name, int16_t defaultVal, ECfgUnitType utype); -void cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, ECfgUnitType utype); -void cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, ECfgUnitType utype); -void cfgAddUInt32(SConfig *pConfig, const char *name, uint32_t defaultVal, ECfgUnitType utype); -void cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, ECfgUnitType utype); -void cfgAddUInt64(SConfig *pConfig, const char *name, uint64_t defaultVal, ECfgUnitType utype); -void cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, ECfgUnitType utype); -void cfgAddDouble(SConfig *pConfig, const char *name, double defaultVal, ECfgUnitType utype); -void cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); -void cfgAddFqdn(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); -void cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); -void cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); -void cfgAddFile(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); +SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter); +void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter); +SConfigItem *cfgGetItem(SConfig *pConfig, const char *name); -bool cfgGetBool(SConfig *pConfig, const char *name); -int8_t cfgGetInt8(SConfig *pConfig, const char *name); -uint8_t cfgGetUInt8(SConfig *pConfig, const char *name); -int16_t cfgGetInt16(SConfig *pConfig, const char *name); -uint16_t cfgGetUInt16(SConfig *pConfig, const char *name); -int32_t cfgGetInt32(SConfig *pConfig, const char *name); -uint32_t cfgGetUInt32(SConfig *pConfig, const char *name); -int64_t cfgGetInt64(SConfig *pConfig, const char *name); -uint64_t cfgGetUInt64(SConfig *pConfig, const char *name); -float cfgGetFloat(SConfig *pConfig, const char *name); -double cfgGetDouble(SConfig *pConfig, const char *name); -const char *cfgGetString(SConfig *pConfig, const char *name); -const char *cfgGetFqdn(SConfig *pConfig, const char *name); -const char *cfgGetIpStr(SConfig *pConfig, const char *name); -const char *cfgGetDir(SConfig *pConfig, const char *name); -const char *cfgGetFile(SConfig *pConfig, const char *name); +int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal, ECfgUnitType utype); +int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, ECfgUnitType utype); +int32_t cfgAddUInt8(SConfig *pConfig, const char *name, uint8_t defaultVal, ECfgUnitType utype); +int32_t cfgAddInt16(SConfig *pConfig, const char *name, int16_t defaultVal, ECfgUnitType utype); +int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, ECfgUnitType utype); +int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, ECfgUnitType utype); +int32_t cfgAddUInt32(SConfig *pConfig, const char *name, uint32_t defaultVal, ECfgUnitType utype); +int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, ECfgUnitType utype); +int32_t cfgAddUInt64(SConfig *pConfig, const char *name, uint64_t defaultVal, ECfgUnitType utype); +int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, ECfgUnitType utype); +int32_t cfgAddDouble(SConfig *pConfig, const char *name, double defaultVal, ECfgUnitType utype); +int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); +int32_t cfgAddFqdn(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); +int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); +int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); +int32_t cfgAddFile(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); #ifdef __cplusplus } diff --git a/source/libs/config/inc/cfgInt.h b/source/libs/config/inc/cfgInt.h index 46ba14ed03..2c4b05af7f 100644 --- a/source/libs/config/inc/cfgInt.h +++ b/source/libs/config/inc/cfgInt.h @@ -18,8 +18,8 @@ #define _TD_CFG_INT_H_ #include "config.h" +#include "taoserror.h" #include "thash.h" -#include "tlockfree.h" #include "ulog.h" #ifdef __cplusplus @@ -27,8 +27,7 @@ extern "C" { #endif typedef struct SConfig { - ECfgType loadType; - SRWLatch lock; + ECfgType stype; SHashObj *hash; } SConfig; diff --git a/source/libs/config/src/config.c b/source/libs/config/src/config.c index 23fbfc1dc0..acebfee32b 100644 --- a/source/libs/config/src/config.c +++ b/source/libs/config/src/config.c @@ -36,10 +36,140 @@ int32_t cfgLoad(SConfig *pConfig, ECfgType cfgType, const char *sourceStr) { } } -void cfgCleanup(SConfig *pConfig) { free(pConfig); } +void cfgCleanup(SConfig *pConfig) { + if (pConfig == NULL) return; + if (pConfig->hash != NULL) { + taosHashCleanup(pConfig->hash); + pConfig->hash == NULL; + } +} + +int32_t cfgGetSize(SConfig *pConfig) { return taosHashGetSize(pConfig->hash); } + +SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashIterate(pConfig->hash, pIter); } + +void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashCancelIterate(pConfig->hash, pIter); } + +SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) { taosHashGet(pConfig->hash, name, strlen(name) + 1); } + +static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name, ECfgUnitType utype) { + pItem->stype = CFG_TYPE_DEFAULT; + pItem->utype = utype; + pItem->name = strdup(name); + if (pItem->name != NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + if (taosHashPut(pConfig->hash, name, strlen(name) + 1, pItem, sizeof(SConfigItem)) != 0) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + return 0; +} + +int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_BOOL, .boolVal = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_INT8, .int8Val = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddUInt8(SConfig *pConfig, const char *name, uint8_t defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_UINT8, .uint8Val = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddInt16(SConfig *pConfig, const char *name, int16_t defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_INT16, .int16Val = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_UINT16, .uint16Val = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_INT32, .int32Val = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddUInt32(SConfig *pConfig, const char *name, uint32_t defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_UINT32, .uint32Val = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_INT64, .int64Val = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddUInt64(SConfig *pConfig, const char *name, uint64_t defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_UINT64, .uint64Val = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .floatVal = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} -int32_t cfgGetSize(SConfig *pConfig) { return 0; } +int32_t cfgAddDouble(SConfig *pConfig, const char *name, double defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_DOUBLE, .doubleVal = defaultVal}; + return cfgAddItem(pConfig, &item, name, utype); +} -void *cfgIterate(SConfig *pConfig, void *p) { return NULL; } +int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_STRING}; + item.strVal = strdup(defaultVal); + if (item.strVal != NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + return cfgAddItem(pConfig, &item, name, utype); +} -void cfgCancelIterate(SConfig *pConfig, void *p); +int32_t cfgAddFqdn(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_FQDN}; + item.fqdnVal = strdup(defaultVal); + if (item.fqdnVal != NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_IPSTR}; + item.ipstrVal = strdup(defaultVal); + if (item.ipstrVal != NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_DIR}; + item.dirVal = strdup(defaultVal); + if (item.dirVal != NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + return cfgAddItem(pConfig, &item, name, utype); +} + +int32_t cfgAddFile(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_FILE}; + item.fileVal = strdup(defaultVal); + if (item.fileVal != NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + return cfgAddItem(pConfig, &item, name, utype); +} -- GitLab