TDengine/source/dnode/mnode/impl/src/mndInfoSchema.c

168 lines
4.9 KiB
C
Raw Normal View History

2022-02-24 11:45:05 +00:00
/*
* 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
2022-04-13 02:48:26 +00:00
#include "mndInt.h"
2022-08-24 09:36:10 +00:00
#include "systable.h"
2022-02-24 11:45:05 +00:00
static int32_t mndInitInfosTableSchema(const SSysDbTableSchema *pSrc, int32_t colNum, SSchema **pDst) {
2024-07-20 02:51:05 +00:00
int32_t code = 0;
2022-03-25 16:29:53 +00:00
SSchema *schema = taosMemoryCalloc(colNum, sizeof(SSchema));
2022-02-24 11:45:05 +00:00
if (NULL == schema) {
code = terrno;
2024-07-20 02:51:05 +00:00
TAOS_RETURN(code);
2022-02-24 11:45:05 +00:00
}
for (int32_t i = 0; i < colNum; ++i) {
tstrncpy(schema[i].name, pSrc[i].name, sizeof(schema[i].name));
2022-02-25 06:30:16 +00:00
schema[i].type = pSrc[i].type;
schema[i].colId = i + 1;
schema[i].bytes = pSrc[i].bytes;
2022-08-24 09:36:10 +00:00
if (pSrc[i].sysInfo) {
schema[i].flags |= COL_IS_SYSINFO;
}
2022-02-24 11:45:05 +00:00
}
*pDst = schema;
2024-07-20 02:51:05 +00:00
TAOS_RETURN(code);
2022-02-24 11:45:05 +00:00
}
static int32_t mndInsInitMeta(SHashObj *hash) {
2024-07-20 02:51:05 +00:00
int32_t code = 0;
2022-02-24 11:45:05 +00:00
STableMetaRsp meta = {0};
tstrncpy(meta.dbFName, TSDB_INFORMATION_SCHEMA_DB, sizeof(meta.dbFName));
2022-03-11 06:41:57 +00:00
meta.tableType = TSDB_SYSTEM_TABLE;
2022-02-24 11:45:05 +00:00
meta.sversion = 1;
meta.tversion = 1;
2022-08-24 09:36:10 +00:00
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;
2022-08-24 09:36:10 +00:00
meta.sysInfo = pInfosTableMeta[i].sysInfo;
2022-03-29 07:48:02 +00:00
2024-07-20 02:51:05 +00:00
TAOS_CHECK_RETURN(mndInitInfosTableSchema(pInfosTableMeta[i].schema, pInfosTableMeta[i].colNum, &meta.pSchemas));
2022-03-29 07:48:02 +00:00
if (taosHashPut(hash, meta.tbName, strlen(meta.tbName), &meta, sizeof(meta))) {
2024-09-12 07:46:30 +00:00
code = terrno;
2024-07-20 02:51:05 +00:00
TAOS_RETURN(code);
2022-02-24 11:45:05 +00:00
}
}
2024-07-20 02:51:05 +00:00
TAOS_RETURN(code);
2022-02-24 11:45:05 +00:00
}
2022-08-24 09:36:10 +00:00
int32_t mndBuildInsTableSchema(SMnode *pMnode, const char *dbFName, const char *tbName, bool sysinfo,
STableMetaRsp *pRsp) {
2024-07-20 02:51:05 +00:00
int32_t code = 0;
2022-02-24 11:45:05 +00:00
if (NULL == pMnode->infosMeta) {
2024-07-20 02:51:05 +00:00
code = TSDB_CODE_APP_ERROR;
TAOS_RETURN(code);
2022-02-24 11:45:05 +00:00
}
2024-06-17 06:31:39 +00:00
STableMetaRsp *pMeta = NULL;
2024-06-19 09:54:14 +00:00
if (strcmp(tbName, TSDB_INS_TABLE_USERS_FULL) == 0) {
2024-06-17 06:31:39 +00:00
pMeta = taosHashGet(pMnode->infosMeta, TSDB_INS_TABLE_USERS_FULL, strlen(tbName));
2024-06-19 09:54:14 +00:00
} else {
2024-06-17 06:31:39 +00:00
pMeta = taosHashGet(pMnode->infosMeta, tbName, strlen(tbName));
}
2024-06-19 09:54:14 +00:00
if (NULL == pMeta) {
2022-02-24 11:45:05 +00:00
mError("invalid information schema table name:%s", tbName);
2024-07-20 02:51:05 +00:00
code = TSDB_CODE_PAR_TABLE_NOT_EXIST;
TAOS_RETURN(code);
2022-02-24 11:45:05 +00:00
}
if (!sysinfo && pMeta->sysInfo) {
mError("no permission to get schema of table name:%s", tbName);
2024-07-20 02:51:05 +00:00
code = TSDB_CODE_PAR_PERMISSION_DENIED;
TAOS_RETURN(code);
}
*pRsp = *pMeta;
2022-03-29 07:48:02 +00:00
pRsp->pSchemas = taosMemoryCalloc(pMeta->numOfColumns, sizeof(SSchema));
2022-02-24 11:45:05 +00:00
if (pRsp->pSchemas == NULL) {
code = terrno;
2022-02-24 11:45:05 +00:00
pRsp->pSchemas = NULL;
2024-07-20 02:51:05 +00:00
TAOS_RETURN(code);
2022-02-24 11:45:05 +00:00
}
memcpy(pRsp->pSchemas, pMeta->pSchemas, pMeta->numOfColumns * sizeof(SSchema));
2024-07-20 02:51:05 +00:00
TAOS_RETURN(code);
2022-02-24 11:45:05 +00:00
}
2022-06-20 12:58:36 +00:00
int32_t mndBuildInsTableCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
2024-07-20 02:51:05 +00:00
int32_t code = 0;
2022-06-20 12:58:36 +00:00
if (NULL == pMnode->infosMeta) {
2024-07-20 02:51:05 +00:00
code = TSDB_CODE_APP_ERROR;
TAOS_RETURN(code);
2022-06-20 12:58:36 +00:00
}
STableMetaRsp *pMeta = taosHashGet(pMnode->infosMeta, tbName, strlen(tbName));
if (NULL == pMeta) {
mError("invalid information schema table name:%s", tbName);
2024-07-20 02:51:05 +00:00
code = TSDB_CODE_PAR_TABLE_NOT_EXIST;
TAOS_RETURN(code);
2022-06-20 12:58:36 +00:00
}
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) {
code = terrno;
2022-06-20 12:58:36 +00:00
pRsp->pSchemas = NULL;
2024-07-20 02:51:05 +00:00
TAOS_RETURN(code);
2022-06-20 12:58:36 +00:00
}
memcpy(pRsp->pSchemas, pMeta->pSchemas, pMeta->numOfColumns * sizeof(SSchema));
2024-03-13 09:54:14 +00:00
pRsp->pSchemaExt = taosMemoryCalloc(pMeta->numOfColumns, sizeof(SSchemaExt));
2024-07-20 02:51:05 +00:00
TAOS_RETURN(code);
2022-06-20 12:58:36 +00:00
}
2022-02-24 11:45:05 +00:00
int32_t mndInitInfos(SMnode *pMnode) {
2022-04-14 03:36:06 +00:00
pMnode->infosMeta = taosHashInit(20, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
2022-02-24 11:45:05 +00:00
if (pMnode->infosMeta == NULL) {
2024-07-20 02:51:05 +00:00
TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
2022-02-24 11:45:05 +00:00
}
return mndInsInitMeta(pMnode->infosMeta);
}
void mndCleanupInfos(SMnode *pMnode) {
if (NULL == pMnode->infosMeta) {
return;
}
2022-03-29 07:48:02 +00:00
STableMetaRsp *pMeta = taosHashIterate(pMnode->infosMeta, NULL);
while (pMeta) {
taosMemoryFreeClear(pMeta->pSchemas);
pMeta = taosHashIterate(pMnode->infosMeta, pMeta);
2022-02-24 11:45:05 +00:00
}
taosHashCleanup(pMnode->infosMeta);
pMnode->infosMeta = NULL;
}