mndInfoSchema.c 4.5 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
D
dapan1121 已提交
17
#include "mndInt.h"
18
#include "systable.h"
D
dapan1121 已提交
19

20
static int32_t mndInitInfosTableSchema(const SSysDbTableSchema *pSrc, int32_t colNum, SSchema **pDst) {
wafwerar's avatar
wafwerar 已提交
21
  SSchema *schema = taosMemoryCalloc(colNum, sizeof(SSchema));
D
dapan1121 已提交
22 23 24 25 26 27
  if (NULL == schema) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  for (int32_t i = 0; i < colNum; ++i) {
28
    tstrncpy(schema[i].name, pSrc[i].name, sizeof(schema[i].name));
D
dapan1121 已提交
29 30 31
    schema[i].type = pSrc[i].type;
    schema[i].colId = i + 1;
    schema[i].bytes = pSrc[i].bytes;
32 33 34
    if (pSrc[i].sysInfo) {
      schema[i].flags |= COL_IS_SYSINFO;
    }
D
dapan1121 已提交
35 36 37
  }

  *pDst = schema;
38
  return 0;
D
dapan1121 已提交
39 40
}

41
static int32_t mndInsInitMeta(SHashObj *hash) {
D
dapan1121 已提交
42 43
  STableMetaRsp meta = {0};

44
  tstrncpy(meta.dbFName, TSDB_INFORMATION_SCHEMA_DB, sizeof(meta.dbFName));
X
Xiaoyu Wang 已提交
45
  meta.tableType = TSDB_SYSTEM_TABLE;
D
dapan1121 已提交
46 47 48
  meta.sversion = 1;
  meta.tversion = 1;

49 50
  size_t               size = 0;
  const SSysTableMeta *pInfosTableMeta = NULL;
51 52 53 54 55
  getInfosDbMeta(&pInfosTableMeta, &size);

  for (int32_t i = 0; i < size; ++i) {
    tstrncpy(meta.tbName, pInfosTableMeta[i].name, sizeof(meta.tbName));
    meta.numOfColumns = pInfosTableMeta[i].colNum;
56
    meta.sysInfo = pInfosTableMeta[i].sysInfo;
L
Liu Jicong 已提交
57

58
    if (mndInitInfosTableSchema(pInfosTableMeta[i].schema, pInfosTableMeta[i].colNum, &meta.pSchemas)) {
D
dapan1121 已提交
59 60
      return -1;
    }
L
Liu Jicong 已提交
61

62
    if (taosHashPut(hash, meta.tbName, strlen(meta.tbName), &meta, sizeof(meta))) {
D
dapan1121 已提交
63 64 65 66 67
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

68
  return 0;
D
dapan1121 已提交
69 70
}

71 72
int32_t mndBuildInsTableSchema(SMnode *pMnode, const char *dbFName, const char *tbName, bool sysinfo,
                               STableMetaRsp *pRsp) {
D
dapan1121 已提交
73
  if (NULL == pMnode->infosMeta) {
74
    terrno = TSDB_CODE_APP_NOT_READY;
D
dapan1121 已提交
75 76 77
    return -1;
  }

78
  STableMetaRsp *pMeta = taosHashGet(pMnode->infosMeta, tbName, strlen(tbName));
79
  if (NULL == pMeta || (!sysinfo && pMeta->sysInfo)) {
D
dapan1121 已提交
80
    mError("invalid information schema table name:%s", tbName);
81
    terrno = TSDB_CODE_MND_INVALID_SYS_TABLENAME;
D
dapan1121 已提交
82 83 84
    return -1;
  }

85
  *pRsp = *pMeta;
L
Liu Jicong 已提交
86

87
  pRsp->pSchemas = taosMemoryCalloc(pMeta->numOfColumns, sizeof(SSchema));
D
dapan1121 已提交
88 89 90 91 92 93
  if (pRsp->pSchemas == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    pRsp->pSchemas = NULL;
    return -1;
  }

94
  memcpy(pRsp->pSchemas, pMeta->pSchemas, pMeta->numOfColumns * sizeof(SSchema));
D
dapan1121 已提交
95 96 97
  return 0;
}

D
dapan1121 已提交
98 99
int32_t mndBuildInsTableCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
  if (NULL == pMnode->infosMeta) {
100
    terrno = TSDB_CODE_APP_NOT_READY;
D
dapan1121 已提交
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
    return -1;
  }

  STableMetaRsp *pMeta = taosHashGet(pMnode->infosMeta, tbName, strlen(tbName));
  if (NULL == pMeta) {
    mError("invalid information schema table name:%s", tbName);
    terrno = TSDB_CODE_MND_INVALID_SYS_TABLENAME;
    return -1;
  }

  strcpy(pRsp->tbName, pMeta->tbName);
  strcpy(pRsp->stbName, pMeta->stbName);
  strcpy(pRsp->dbFName, pMeta->dbFName);
  pRsp->numOfTags = pMeta->numOfTags;
  pRsp->numOfColumns = pMeta->numOfColumns;
  pRsp->tableType = pMeta->tableType;

  pRsp->pSchemas = taosMemoryCalloc(pMeta->numOfColumns, sizeof(SSchema));
  if (pRsp->pSchemas == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    pRsp->pSchemas = NULL;
    return -1;
  }

  memcpy(pRsp->pSchemas, pMeta->pSchemas, pMeta->numOfColumns * sizeof(SSchema));
  return 0;
}

D
dapan1121 已提交
129
int32_t mndInitInfos(SMnode *pMnode) {
130
  pMnode->infosMeta = taosHashInit(20, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
D
dapan1121 已提交
131 132 133 134 135 136 137 138 139 140 141 142
  if (pMnode->infosMeta == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return mndInsInitMeta(pMnode->infosMeta);
}

void mndCleanupInfos(SMnode *pMnode) {
  if (NULL == pMnode->infosMeta) {
    return;
  }
L
Liu Jicong 已提交
143

144 145 146 147
  STableMetaRsp *pMeta = taosHashIterate(pMnode->infosMeta, NULL);
  while (pMeta) {
    taosMemoryFreeClear(pMeta->pSchemas);
    pMeta = taosHashIterate(pMnode->infosMeta, pMeta);
D
dapan1121 已提交
148 149 150 151 152
  }

  taosHashCleanup(pMnode->infosMeta);
  pMnode->infosMeta = NULL;
}