From aa6a0cb47f9fc206c7daa6fe65a94217d6735979 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 18 Feb 2022 21:28:03 +0800 Subject: [PATCH] config interface --- include/libs/config/config.h | 106 ++++++++++++++++++++++++ source/dnode/mgmt/daemon/CMakeLists.txt | 1 + source/libs/CMakeLists.txt | 3 +- source/libs/config/CMakeLists.txt | 16 ++++ source/libs/config/inc/cfgInt.h | 44 ++++++++++ source/libs/config/src/cfgApolloUrl.c | 22 +++++ source/libs/config/src/cfgDotEnv.c | 22 +++++ source/libs/config/src/cfgEnvVar.c | 22 +++++ source/libs/config/src/cfgTaosFile.c | 22 +++++ source/libs/config/src/config.c | 45 ++++++++++ source/libs/config/test/CMakeLists.txt | 14 ++++ source/libs/config/test/cfgTest.cpp | 31 +++++++ 12 files changed, 347 insertions(+), 1 deletion(-) create mode 100644 include/libs/config/config.h create mode 100644 source/libs/config/CMakeLists.txt create mode 100644 source/libs/config/inc/cfgInt.h create mode 100644 source/libs/config/src/cfgApolloUrl.c create mode 100644 source/libs/config/src/cfgDotEnv.c create mode 100644 source/libs/config/src/cfgEnvVar.c create mode 100644 source/libs/config/src/cfgTaosFile.c create mode 100644 source/libs/config/src/config.c create mode 100644 source/libs/config/test/CMakeLists.txt create mode 100644 source/libs/config/test/cfgTest.cpp diff --git a/include/libs/config/config.h b/include/libs/config/config.h new file mode 100644 index 0000000000..b25016a324 --- /dev/null +++ b/include/libs/config/config.h @@ -0,0 +1,106 @@ + +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_CONFIG_H_ +#define _TD_CONFIG_H_ + +#include "os.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { CFG_TYPE_NONE, CFG_TYPE_TAOS_CFG, CFG_TYPE_DOT_ENV, CFG_TYPE_ENV_VAR, CFG_TYPE_APOLLO_URL } ECfgType; + +typedef enum { + CFG_DYPE_NONE, + CFG_DYPE_BOOL, + CFG_DTYPE_INT8, + CFG_DTYPE_UINT8, + CFG_DTYPE_INT16, + CFG_DTYPE_UINT16, + CFG_DTYPE_INT32, + CFG_DTYPE_UINT32, + CFG_DTYPE_INT64, + CFG_DTYPE_UINT64, + CFG_DTYPE_FLOAT, + CFG_DTYPE_DOUBLE, + CFG_DTYPE_STRING, + CFG_DTYPE_FQDN, + CFG_DTYPE_IPSTR, + CFG_DTYPE_DIR, + CFG_DTYPE_FILE +} ECfgDataType; + +typedef enum { + CFG_UTYPE_NONE, + CFG_UTYPE_PERCENT, + CFG_UTYPE_GB, + CFG_UTYPE_MB, + CFG_UTYPE_BYTE, + CFG_UTYPE_SECOND, + CFG_UTYPE_MS +} ECfgUnitType; + +typedef struct SConfig SConfig; + +SConfig *cfgInit(); +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); + +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); + +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); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_CONFIG_H_*/ diff --git a/source/dnode/mgmt/daemon/CMakeLists.txt b/source/dnode/mgmt/daemon/CMakeLists.txt index f1ce726d85..7f9a7c4d21 100644 --- a/source/dnode/mgmt/daemon/CMakeLists.txt +++ b/source/dnode/mgmt/daemon/CMakeLists.txt @@ -3,6 +3,7 @@ add_executable(taosd ${DAEMON_SRC}) target_link_libraries( taosd PUBLIC dnode + PUBLIC config PUBLIC util PUBLIC os ) diff --git a/source/libs/CMakeLists.txt b/source/libs/CMakeLists.txt index 049b69991f..f515dda4bb 100644 --- a/source/libs/CMakeLists.txt +++ b/source/libs/CMakeLists.txt @@ -13,4 +13,5 @@ add_subdirectory(function) add_subdirectory(qcom) add_subdirectory(qworker) add_subdirectory(tfs) -add_subdirectory(nodes) \ No newline at end of file +add_subdirectory(nodes) +add_subdirectory(config) \ No newline at end of file diff --git a/source/libs/config/CMakeLists.txt b/source/libs/config/CMakeLists.txt new file mode 100644 index 0000000000..9d899d2ff9 --- /dev/null +++ b/source/libs/config/CMakeLists.txt @@ -0,0 +1,16 @@ +aux_source_directory(src CONFIG_SRC) +add_library(config ${CONFIG_SRC}) +target_include_directories( + config + PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/config" + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +target_link_libraries( + config + PRIVATE os util +) + +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) \ No newline at end of file diff --git a/source/libs/config/inc/cfgInt.h b/source/libs/config/inc/cfgInt.h new file mode 100644 index 0000000000..46ba14ed03 --- /dev/null +++ b/source/libs/config/inc/cfgInt.h @@ -0,0 +1,44 @@ + +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_CFG_INT_H_ +#define _TD_CFG_INT_H_ + +#include "config.h" +#include "thash.h" +#include "tlockfree.h" +#include "ulog.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct SConfig { + ECfgType loadType; + SRWLatch lock; + SHashObj *hash; +} SConfig; + +int32_t cfgLoadFromTaosFile(SConfig *pConfig, const char *filepath); +int32_t cfgLoadFromDotEnvFile(SConfig *pConfig, const char *filepath); +int32_t cfgLoadFromGlobalEnvVariable(SConfig *pConfig); +int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_CFG_INT_H_*/ diff --git a/source/libs/config/src/cfgApolloUrl.c b/source/libs/config/src/cfgApolloUrl.c new file mode 100644 index 0000000000..f35eca70c3 --- /dev/null +++ b/source/libs/config/src/cfgApolloUrl.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "cfgInt.h" + +int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { + uInfo("load from apoll url %s", url); + return 0; +} \ No newline at end of file diff --git a/source/libs/config/src/cfgDotEnv.c b/source/libs/config/src/cfgDotEnv.c new file mode 100644 index 0000000000..e65dddae7e --- /dev/null +++ b/source/libs/config/src/cfgDotEnv.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "cfgInt.h" + +int32_t cfgLoadFromDotEnvFile(SConfig *pConfig, const char *filepath) { + uInfo("load from .env file %s", filepath); + return 0; +} \ No newline at end of file diff --git a/source/libs/config/src/cfgEnvVar.c b/source/libs/config/src/cfgEnvVar.c new file mode 100644 index 0000000000..caa5a88110 --- /dev/null +++ b/source/libs/config/src/cfgEnvVar.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "cfgInt.h" + +int32_t cfgLoadFromGlobalEnvVariable(SConfig *pConfig) { + uInfo("load from global env variables"); + return 0; +} \ No newline at end of file diff --git a/source/libs/config/src/cfgTaosFile.c b/source/libs/config/src/cfgTaosFile.c new file mode 100644 index 0000000000..7d5b4e4743 --- /dev/null +++ b/source/libs/config/src/cfgTaosFile.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "cfgInt.h" + +int32_t cfgLoadFromTaosFile(SConfig *pConfig, const char *filepath) { + uInfo("load from .cfg file %s", filepath); + return 0; +} \ No newline at end of file diff --git a/source/libs/config/src/config.c b/source/libs/config/src/config.c new file mode 100644 index 0000000000..23fbfc1dc0 --- /dev/null +++ b/source/libs/config/src/config.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "cfgInt.h" + +SConfig *cfgInit() { + SConfig *pConfig = calloc(1, sizeof(SConfig)); + return pConfig; +} + +int32_t cfgLoad(SConfig *pConfig, ECfgType cfgType, const char *sourceStr) { + switch (cfgType) { + case CFG_TYPE_TAOS_CFG: + return cfgLoadFromTaosFile(pConfig, sourceStr); + case CFG_TYPE_DOT_ENV: + return cfgLoadFromDotEnvFile(pConfig, sourceStr); + case CFG_TYPE_ENV_VAR: + return cfgLoadFromGlobalEnvVariable(pConfig); + case CFG_TYPE_APOLLO_URL: + return cfgLoadFromApollUrl(pConfig, sourceStr); + default: + return -1; + } +} + +void cfgCleanup(SConfig *pConfig) { free(pConfig); } + +int32_t cfgGetSize(SConfig *pConfig) { return 0; } + +void *cfgIterate(SConfig *pConfig, void *p) { return NULL; } + +void cfgCancelIterate(SConfig *pConfig, void *p); diff --git a/source/libs/config/test/CMakeLists.txt b/source/libs/config/test/CMakeLists.txt new file mode 100644 index 0000000000..1e63d5025d --- /dev/null +++ b/source/libs/config/test/CMakeLists.txt @@ -0,0 +1,14 @@ +enable_testing() + +aux_source_directory(. CFG_TEST_SRC) +add_executable(cfg_test ${CFG_TEST_SRC}) +target_link_libraries( + cfg_test + PUBLIC config + PUBLIC gtest_main +) + +add_test( + NAME cfg_test + COMMAND cfg_test +) diff --git a/source/libs/config/test/cfgTest.cpp b/source/libs/config/test/cfgTest.cpp new file mode 100644 index 0000000000..ba58a5b977 --- /dev/null +++ b/source/libs/config/test/cfgTest.cpp @@ -0,0 +1,31 @@ +/** + * @file cfgTest.cpp + * @author slguan (slguan@taosdata.com) + * @brief config module tests + * @version 1.0 + * @date 2022-02-20 + * + * @copyright Copyright (c) 2022 + * + */ + +#include +#include "os.h" + +#include "tfs.h" + +class CfgTest : public ::testing::Test { + protected: + static void SetUpTestSuite() {} + static void TearDownTestSuite() {} + + public: + void SetUp() override {} + void TearDown() override {} + + static const char *pConfig; +}; + +const char *TfsTest::pConfig; + +TEST_F(CfgTest, 01_Taos_File) {}