config.c 8.6 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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));
S
cfgtest  
Shengliang Guan 已提交
21 22 23 24 25 26 27 28 29 30 31 32
  if (pConfig == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  pConfig->hash = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
  if (pConfig->hash == NULL) {
    free(pConfig);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
33 34 35
  return pConfig;
}

S
Shengliang Guan 已提交
36
int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr) {
S
Shengliang Guan 已提交
37
  switch (cfgType) {
S
Shengliang Guan 已提交
38
    case CFG_TYPE_CFG_FILE:
S
Shengliang Guan 已提交
39 40 41 42 43 44 45 46 47 48 49 50
      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;
  }
}

S
Shengliang Guan 已提交
51
void cfgCleanup(SConfig *pConfig) {
S
Shengliang Guan 已提交
52 53 54 55 56 57
  if (pConfig != NULL) {
    if (pConfig->hash != NULL) {
      taosHashCleanup(pConfig->hash);
      pConfig->hash == NULL;
    }
    free(pConfig);
S
Shengliang Guan 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  }
}

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);
S
Shengliang Guan 已提交
73
  if (pItem->name == NULL) {
S
Shengliang Guan 已提交
74 75 76 77 78
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  if (taosHashPut(pConfig->hash, name, strlen(name) + 1, pItem, sizeof(SConfigItem)) != 0) {
S
cfgtest  
Shengliang Guan 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
    if (pItem->dtype == CFG_DTYPE_STRING) {
      free(pItem->strVal);
    } else if (pItem->dtype == CFG_DTYPE_FQDN) {
      free(pItem->fqdnVal);
    } else if (pItem->dtype == CFG_DTYPE_IPSTR) {
      free(pItem->ipstrVal);
    } else if (pItem->dtype == CFG_DTYPE_DIR) {
      free(pItem->dirVal);
    } else if (pItem->dtype == CFG_DTYPE_FILE) {
      free(pItem->fileVal);
    } else {
    }
    free(pItem->name);
S
Shengliang Guan 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    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);
}
S
Shengliang Guan 已提交
148

S
Shengliang Guan 已提交
149 150 151 152
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);
}
S
Shengliang Guan 已提交
153

S
Shengliang Guan 已提交
154 155 156
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_STRING};
  item.strVal = strdup(defaultVal);
S
Shengliang Guan 已提交
157
  if (item.strVal == NULL) {
S
Shengliang Guan 已提交
158 159 160 161 162
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return cfgAddItem(pConfig, &item, name, utype);
}
S
Shengliang Guan 已提交
163

S
Shengliang Guan 已提交
164 165 166
int32_t cfgAddFqdn(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_FQDN};
  item.fqdnVal = strdup(defaultVal);
S
Shengliang Guan 已提交
167
  if (item.fqdnVal == NULL) {
S
Shengliang Guan 已提交
168 169 170 171 172 173 174 175 176
    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);
S
Shengliang Guan 已提交
177
  if (item.ipstrVal == NULL) {
S
Shengliang Guan 已提交
178 179 180 181 182 183 184 185 186
    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);
S
Shengliang Guan 已提交
187
  if (item.dirVal == NULL) {
S
Shengliang Guan 已提交
188 189 190 191 192 193 194 195 196
    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);
S
Shengliang Guan 已提交
197
  if (item.fileVal == NULL) {
S
Shengliang Guan 已提交
198 199 200 201 202
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return cfgAddItem(pConfig, &item, name, utype);
}
S
Shengliang Guan 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

const char *cfgStypeStr(ECfgSrcType type) {
  switch (type) {
    case CFG_TYPE_DEFAULT:
      return "default";
    case CFG_TYPE_CFG_FILE:
      return "cfg";
    case CFG_TYPE_DOT_ENV:
      return ".env";
    case CFG_TYPE_ENV_VAR:
      return "env";
    case CFG_TYPE_APOLLO_URL:
      return "apollo";
    default:
      return "invalid";
  }
}

const char *cfgDtypeStr(ECfgDataType type) {
  switch (type) {
    case CFG_DTYPE_NONE:
      return "none";
    case CFG_DTYPE_BOOL:
      return "bool";
    case CFG_DTYPE_INT8:
      return "int8";
    case CFG_DTYPE_UINT8:
      return "uint8";
    case CFG_DTYPE_INT16:
      return "int16";
    case CFG_DTYPE_UINT16:
      return "uint16";
    case CFG_DTYPE_INT32:
      return "int32";
    case CFG_DTYPE_UINT32:
      return "uint32";
    case CFG_DTYPE_INT64:
      return "int64";
    case CFG_DTYPE_UINT64:
      return "uint64";
    case CFG_DTYPE_FLOAT:
      return "float";
    case CFG_DTYPE_DOUBLE:
      return "double";
    case CFG_DTYPE_STRING:
      return "string";
    case CFG_DTYPE_FQDN:
      return "fqdn";
    case CFG_DTYPE_IPSTR:
      return "ipstr";
    case CFG_DTYPE_DIR:
      return "dir";
    case CFG_DTYPE_FILE:
      return "file";
    default:
      return "invalid";
  }
}

const char *cfgUtypeStr(ECfgUnitType type) {
  switch (type) {
    case CFG_UTYPE_NONE:
      return "";
    case CFG_UTYPE_PERCENT:
      return "(%)";
    case CFG_UTYPE_GB:
      return "(GB)";
    case CFG_UTYPE_MB:
      return "(Mb)";
    case CFG_UTYPE_BYTE:
      return "(byte)";
    case CFG_UTYPE_SECOND:
      return "(s)";
    case CFG_UTYPE_MS:
      return "(ms)";
    default:
      return "invalid";
  }
}