config interface
This commit is contained in:
parent
90dd7746d6
commit
aa6a0cb47f
|
@ -0,0 +1,106 @@
|
|||
|
||||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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_*/
|
|
@ -3,6 +3,7 @@ add_executable(taosd ${DAEMON_SRC})
|
|||
target_link_libraries(
|
||||
taosd
|
||||
PUBLIC dnode
|
||||
PUBLIC config
|
||||
PUBLIC util
|
||||
PUBLIC os
|
||||
)
|
||||
|
|
|
@ -13,4 +13,5 @@ add_subdirectory(function)
|
|||
add_subdirectory(qcom)
|
||||
add_subdirectory(qworker)
|
||||
add_subdirectory(tfs)
|
||||
add_subdirectory(nodes)
|
||||
add_subdirectory(nodes)
|
||||
add_subdirectory(config)
|
|
@ -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})
|
|
@ -0,0 +1,44 @@
|
|||
|
||||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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_*/
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define _DEFAULT_SOURCE
|
||||
#include "cfgInt.h"
|
||||
|
||||
int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
|
||||
uInfo("load from apoll url %s", url);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define _DEFAULT_SOURCE
|
||||
#include "cfgInt.h"
|
||||
|
||||
int32_t cfgLoadFromDotEnvFile(SConfig *pConfig, const char *filepath) {
|
||||
uInfo("load from .env file %s", filepath);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define _DEFAULT_SOURCE
|
||||
#include "cfgInt.h"
|
||||
|
||||
int32_t cfgLoadFromGlobalEnvVariable(SConfig *pConfig) {
|
||||
uInfo("load from global env variables");
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define _DEFAULT_SOURCE
|
||||
#include "cfgInt.h"
|
||||
|
||||
int32_t cfgLoadFromTaosFile(SConfig *pConfig, const char *filepath) {
|
||||
uInfo("load from .cfg file %s", filepath);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
|
@ -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
|
||||
)
|
|
@ -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 <gtest/gtest.h>
|
||||
#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) {}
|
Loading…
Reference in New Issue