mndInfoSchema.c 4.3 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
H
Haojun Liao 已提交
17
#include "systable.h"
D
dapan1121 已提交
18
#include "mndInt.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;
D
dapan1121 已提交
32 33 34
  }

  *pDst = schema;
35
  return 0;
D
dapan1121 已提交
36 37
}

38
static int32_t mndInsInitMeta(SHashObj *hash) {
D
dapan1121 已提交
39 40
  STableMetaRsp meta = {0};

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

46 47 48 49 50 51 52
  size_t size = 0;
  const SSysTableMeta* pInfosTableMeta = NULL;
  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;
L
Liu Jicong 已提交
53

54
    if (mndInitInfosTableSchema(pInfosTableMeta[i].schema, pInfosTableMeta[i].colNum, &meta.pSchemas)) {
D
dapan1121 已提交
55 56
      return -1;
    }
L
Liu Jicong 已提交
57

58
    if (taosHashPut(hash, meta.tbName, strlen(meta.tbName), &meta, sizeof(meta))) {
D
dapan1121 已提交
59 60 61 62 63
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

64
  return 0;
D
dapan1121 已提交
65 66 67 68 69 70 71 72
}

int32_t mndBuildInsTableSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp) {
  if (NULL == pMnode->infosMeta) {
    terrno = TSDB_CODE_MND_NOT_READY;
    return -1;
  }

73
  STableMetaRsp *pMeta = taosHashGet(pMnode->infosMeta, tbName, strlen(tbName));
74
  if (NULL == pMeta) {
D
dapan1121 已提交
75
    mError("invalid information schema table name:%s", tbName);
76
    terrno = TSDB_CODE_MND_INVALID_SYS_TABLENAME;
D
dapan1121 已提交
77 78 79
    return -1;
  }

80
  *pRsp = *pMeta;
L
Liu Jicong 已提交
81

82
  pRsp->pSchemas = taosMemoryCalloc(pMeta->numOfColumns, sizeof(SSchema));
D
dapan1121 已提交
83 84 85 86 87 88
  if (pRsp->pSchemas == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    pRsp->pSchemas = NULL;
    return -1;
  }

89
  memcpy(pRsp->pSchemas, pMeta->pSchemas, pMeta->numOfColumns * sizeof(SSchema));
D
dapan1121 已提交
90 91 92
  return 0;
}

D
dapan1121 已提交
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
int32_t mndBuildInsTableCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
  if (NULL == pMnode->infosMeta) {
    terrno = TSDB_CODE_MND_NOT_READY;
    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 已提交
125
int32_t mndInitInfos(SMnode *pMnode) {
126
  pMnode->infosMeta = taosHashInit(20, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
D
dapan1121 已提交
127 128 129 130 131 132 133 134 135 136 137 138
  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 已提交
139

140 141 142 143
  STableMetaRsp *pMeta = taosHashIterate(pMnode->infosMeta, NULL);
  while (pMeta) {
    taosMemoryFreeClear(pMeta->pSchemas);
    pMeta = taosHashIterate(pMnode->infosMeta, pMeta);
D
dapan1121 已提交
144 145 146 147 148
  }

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