TDengine/source/dnode/vnode/src/meta/metaTable.c

1199 lines
36 KiB
C
Raw Normal View History

2021-11-02 05:57:16 +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/>.
2021-11-02 07:24:55 +00:00
*/
2022-04-26 11:04:26 +00:00
#include "meta.h"
2021-11-02 07:24:55 +00:00
2022-06-04 14:33:36 +00:00
static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
static int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
2022-04-23 13:45:26 +00:00
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME);
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME);
static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME);
2022-06-26 10:44:49 +00:00
static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME);
2022-05-13 09:32:15 +00:00
static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry);
2022-05-25 08:03:05 +00:00
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type);
2022-04-21 14:01:58 +00:00
2022-06-04 14:33:36 +00:00
static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
2022-06-01 12:28:29 +00:00
pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
if (NULL == pMetaRsp->pSchemas) {
terrno = TSDB_CODE_VND_OUT_OF_MEMORY;
return -1;
}
strcpy(pMetaRsp->tbName, tbName);
pMetaRsp->numOfColumns = pSchema->nCols;
pMetaRsp->tableType = TSDB_NORMAL_TABLE;
pMetaRsp->sversion = pSchema->version;
pMetaRsp->tuid = uid;
memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
return 0;
}
2022-06-04 14:33:36 +00:00
static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
#ifdef USE_INVERTED_INDEX
if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
return -1;
}
2022-07-06 12:21:59 +00:00
void *data = pCtbEntry->ctbEntry.pTags;
2022-06-04 14:33:36 +00:00
const char *tagName = pSchema->name;
tb_uid_t suid = pCtbEntry->ctbEntry.suid;
tb_uid_t tuid = pCtbEntry->uid;
const void *pTagData = pCtbEntry->ctbEntry.pTags;
int32_t nTagData = 0;
SArray *pTagVals = NULL;
if (tTagToValArray((const STag *)data, &pTagVals) != 0) {
return -1;
}
2022-06-05 10:56:43 +00:00
2022-06-04 14:33:36 +00:00
SIndexMultiTerm *terms = indexMultiTermCreate();
int16_t nCols = taosArrayGetSize(pTagVals);
for (int i = 0; i < nCols; i++) {
STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
char type = pTagVal->type;
2022-06-07 08:02:41 +00:00
2022-07-06 12:21:59 +00:00
char *key = pTagVal->pKey;
2022-06-05 10:56:43 +00:00
int32_t nKey = strlen(key);
2022-06-04 14:33:36 +00:00
SIndexTerm *term = NULL;
if (type == TSDB_DATA_TYPE_NULL) {
2022-07-06 07:07:42 +00:00
term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2022-06-04 14:33:36 +00:00
} else if (type == TSDB_DATA_TYPE_NCHAR) {
if (pTagVal->nData > 0) {
2022-07-06 12:21:59 +00:00
char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
2022-06-05 10:56:43 +00:00
int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE);
memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
2022-06-04 14:33:36 +00:00
type = TSDB_DATA_TYPE_VARCHAR;
2022-06-05 10:56:43 +00:00
term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
2022-06-04 14:33:36 +00:00
} else if (pTagVal->nData == 0) {
2022-07-06 07:07:42 +00:00
term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
2022-06-04 14:33:36 +00:00
}
} else if (type == TSDB_DATA_TYPE_DOUBLE) {
double val = *(double *)(&pTagVal->i64);
2022-07-06 12:05:45 +00:00
int len = sizeof(val);
2022-06-04 14:33:36 +00:00
term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
} else if (type == TSDB_DATA_TYPE_BOOL) {
int val = *(int *)(&pTagVal->i64);
2022-07-06 12:05:45 +00:00
int len = sizeof(val);
2022-07-07 02:05:48 +00:00
term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
2022-06-04 14:33:36 +00:00
}
2022-06-05 10:56:43 +00:00
if (term != NULL) {
2022-06-04 14:33:36 +00:00
indexMultiTermAdd(terms, term);
}
}
2022-06-09 05:50:18 +00:00
indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
2022-06-04 14:33:36 +00:00
indexMultiTermDestroy(terms);
#endif
2022-06-06 03:51:00 +00:00
return 0;
2022-06-04 14:33:36 +00:00
}
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
#ifdef USE_INVERTED_INDEX
if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
return -1;
}
void *data = pCtbEntry->ctbEntry.pTags;
const char *tagName = pSchema->name;
tb_uid_t suid = pCtbEntry->ctbEntry.suid;
tb_uid_t tuid = pCtbEntry->uid;
const void *pTagData = pCtbEntry->ctbEntry.pTags;
int32_t nTagData = 0;
SArray *pTagVals = NULL;
if (tTagToValArray((const STag *)data, &pTagVals) != 0) {
return -1;
}
SIndexMultiTerm *terms = indexMultiTermCreate();
int16_t nCols = taosArrayGetSize(pTagVals);
for (int i = 0; i < nCols; i++) {
STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
char type = pTagVal->type;
char *key = pTagVal->pKey;
int32_t nKey = strlen(key);
SIndexTerm *term = NULL;
if (type == TSDB_DATA_TYPE_NULL) {
term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
} else if (type == TSDB_DATA_TYPE_NCHAR) {
if (pTagVal->nData > 0) {
char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE);
memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
type = TSDB_DATA_TYPE_VARCHAR;
term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
} else if (pTagVal->nData == 0) {
term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
}
} else if (type == TSDB_DATA_TYPE_DOUBLE) {
double val = *(double *)(&pTagVal->i64);
int len = sizeof(val);
term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
} else if (type == TSDB_DATA_TYPE_BOOL) {
int val = *(int *)(&pTagVal->i64);
int len = sizeof(val);
term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
2022-06-04 14:33:36 +00:00
}
2022-06-05 10:56:43 +00:00
if (term != NULL) {
2022-06-04 14:33:36 +00:00
indexMultiTermAdd(terms, term);
}
}
2022-06-09 05:50:18 +00:00
indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
2022-06-04 14:33:36 +00:00
indexMultiTermDestroy(terms);
#endif
2022-06-06 03:51:00 +00:00
return 0;
2022-06-04 14:33:36 +00:00
}
2022-04-21 14:01:58 +00:00
int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
2022-04-21 03:47:58 +00:00
SMetaEntry me = {0};
2022-04-21 14:01:58 +00:00
int kLen = 0;
int vLen = 0;
const void *pKey = NULL;
const void *pVal = NULL;
2022-07-06 12:21:59 +00:00
void *pBuf = NULL;
2022-04-21 14:01:58 +00:00
int32_t szBuf = 0;
2022-07-06 12:21:59 +00:00
void *p = NULL;
2022-04-26 02:25:07 +00:00
SMetaReader mr = {0};
// validate req
2022-04-26 11:04:26 +00:00
metaReaderInit(&mr, pMeta, 0);
2022-04-26 02:25:07 +00:00
if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
2022-04-26 02:43:00 +00:00
// TODO: just for pass case
#if 0
2022-04-26 02:25:07 +00:00
terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
metaReaderClear(&mr);
return -1;
2022-04-26 02:43:00 +00:00
#else
metaReaderClear(&mr);
return 0;
#endif
2022-04-21 14:01:58 +00:00
}
2022-04-26 02:25:07 +00:00
metaReaderClear(&mr);
2022-04-21 03:47:58 +00:00
// set structs
2022-04-23 13:45:26 +00:00
me.version = version;
2022-04-21 03:47:58 +00:00
me.type = TSDB_SUPER_TABLE;
me.uid = pReq->suid;
me.name = pReq->name;
2022-05-26 08:29:52 +00:00
me.stbEntry.schemaRow = pReq->schemaRow;
2022-04-22 12:34:37 +00:00
me.stbEntry.schemaTag = pReq->schemaTag;
2022-06-27 06:47:14 +00:00
if (pReq->rollup) {
TABLE_SET_ROLLUP(me.flags);
me.stbEntry.rsmaParam = pReq->rsmaParam;
}
2022-04-21 03:47:58 +00:00
2022-04-23 13:45:26 +00:00
if (metaHandleEntry(pMeta, &me) < 0) goto _err;
2022-04-21 03:47:58 +00:00
2022-06-02 05:57:39 +00:00
metaDebug("vgId:%d, super table is created, name:%s uid: %" PRId64, TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
2022-04-21 14:01:58 +00:00
return 0;
_err:
2022-06-02 05:57:39 +00:00
metaError("vgId:%d, failed to create super table: %s uid: %" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
2022-04-21 14:01:58 +00:00
pReq->suid, tstrerror(terrno));
return -1;
}
int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
2022-05-25 07:14:36 +00:00
void *pKey = NULL;
int nKey = 0;
void *pData = NULL;
int nData = 0;
int c = 0;
int rc = 0;
// check if super table exists
rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
if (rc < 0 || *(tb_uid_t *)pData != pReq->suid) {
terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
return -1;
2022-04-28 06:20:00 +00:00
}
2022-05-25 07:14:36 +00:00
// drop all child tables
2022-07-06 12:21:59 +00:00
TBC *pCtbIdxc = NULL;
2022-05-25 07:14:36 +00:00
SArray *pArray = taosArrayInit(8, sizeof(tb_uid_t));
2022-05-04 07:07:34 +00:00
2022-05-18 07:57:29 +00:00
tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, &pMeta->txn);
2022-05-25 07:14:36 +00:00
rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = pReq->suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
if (rc < 0) {
2022-05-18 07:57:29 +00:00
tdbTbcClose(pCtbIdxc);
2022-05-25 07:14:36 +00:00
metaWLock(pMeta);
goto _drop_super_table;
2022-05-04 07:07:34 +00:00
}
for (;;) {
2022-05-25 07:14:36 +00:00
rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, NULL, NULL);
if (rc < 0) break;
2022-05-04 07:07:34 +00:00
2022-05-25 07:14:36 +00:00
if (((SCtbIdxKey *)pKey)->suid < pReq->suid) {
continue;
} else if (((SCtbIdxKey *)pKey)->suid > pReq->suid) {
break;
}
2022-05-04 07:07:34 +00:00
2022-05-25 07:14:36 +00:00
taosArrayPush(pArray, &(((SCtbIdxKey *)pKey)->uid));
}
tdbTbcClose(pCtbIdxc);
metaWLock(pMeta);
2022-05-04 07:07:34 +00:00
2022-05-25 07:14:36 +00:00
for (int32_t iChild = 0; iChild < taosArrayGetSize(pArray); iChild++) {
tb_uid_t uid = *(tb_uid_t *)taosArrayGet(pArray, iChild);
2022-05-25 08:03:05 +00:00
metaDropTableByUid(pMeta, uid, NULL);
2022-05-04 07:07:34 +00:00
}
2022-05-25 07:14:36 +00:00
taosArrayDestroy(pArray);
// drop super table
_drop_super_table:
tdbTbGet(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pData, &nData);
tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = *(int64_t *)pData, .uid = pReq->suid}, sizeof(STbDbKey),
&pMeta->txn);
tdbTbDelete(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pMeta->txn);
tdbTbDelete(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pMeta->txn);
2022-06-26 10:44:49 +00:00
tdbTbDelete(pMeta->pSuidIdx, &pReq->suid, sizeof(tb_uid_t), &pMeta->txn);
2022-05-25 07:14:36 +00:00
metaULock(pMeta);
2022-05-04 07:07:34 +00:00
_exit:
2022-05-25 07:14:36 +00:00
tdbFree(pKey);
tdbFree(pData);
2022-06-02 05:57:39 +00:00
metaDebug("vgId:%d, super table %s uid:%" PRId64 " is dropped", TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
2022-04-20 10:03:50 +00:00
return 0;
}
2022-05-14 14:29:04 +00:00
int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
SMetaEntry oStbEntry = {0};
SMetaEntry nStbEntry = {0};
2022-07-06 12:21:59 +00:00
TBC *pUidIdxc = NULL;
TBC *pTbDbc = NULL;
2022-05-14 14:29:04 +00:00
const void *pData;
int nData;
int64_t oversion;
SDecoder dc = {0};
int32_t ret;
int32_t c;
2022-05-18 07:57:29 +00:00
tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
ret = tdbTbcMoveTo(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &c);
2022-05-14 14:29:04 +00:00
if (ret < 0 || c) {
ASSERT(0);
return -1;
}
2022-05-18 07:57:29 +00:00
ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
2022-05-14 14:29:04 +00:00
if (ret < 0) {
ASSERT(0);
return -1;
}
oversion = *(int64_t *)pData;
2022-05-18 07:57:29 +00:00
tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = pReq->suid, .version = oversion}), sizeof(STbDbKey), &c);
2022-05-14 14:29:04 +00:00
ASSERT(ret == 0 && c == 0);
2022-05-18 07:57:29 +00:00
ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
2022-05-14 14:29:04 +00:00
ASSERT(ret == 0);
2022-05-23 07:47:49 +00:00
oStbEntry.pBuf = taosMemoryMalloc(nData);
memcpy(oStbEntry.pBuf, pData, nData);
tDecoderInit(&dc, oStbEntry.pBuf, nData);
2022-05-14 14:29:04 +00:00
metaDecodeEntry(&dc, &oStbEntry);
nStbEntry.version = version;
nStbEntry.type = TSDB_SUPER_TABLE;
nStbEntry.uid = pReq->suid;
nStbEntry.name = pReq->name;
2022-05-26 08:29:52 +00:00
nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
2022-05-14 14:29:04 +00:00
nStbEntry.stbEntry.schemaTag = pReq->schemaTag;
metaWLock(pMeta);
// compare two entry
2022-05-26 08:29:52 +00:00
if (oStbEntry.stbEntry.schemaRow.version != pReq->schemaRow.version) {
metaSaveToSkmDb(pMeta, &nStbEntry);
2022-05-14 14:29:04 +00:00
}
// if (oStbEntry.stbEntry.schemaTag.sver != pReq->schemaTag.sver) {
// // change tag schema
// }
// update table.db
metaSaveToTbDb(pMeta, &nStbEntry);
// update uid index
2022-05-18 07:57:29 +00:00
tdbTbcUpsert(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &version, sizeof(version), 0);
2022-05-14 14:29:04 +00:00
2022-05-23 07:47:49 +00:00
if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
2022-05-14 14:29:04 +00:00
metaULock(pMeta);
tDecoderClear(&dc);
2022-05-18 07:57:29 +00:00
tdbTbcClose(pTbDbc);
tdbTbcClose(pUidIdxc);
2022-05-14 14:29:04 +00:00
return 0;
}
2022-04-22 09:42:31 +00:00
int metaCreateTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
2022-04-26 02:25:07 +00:00
SMetaEntry me = {0};
SMetaReader mr = {0};
2022-04-22 09:42:31 +00:00
// validate message
if (pReq->type != TSDB_CHILD_TABLE && pReq->type != TSDB_NORMAL_TABLE) {
terrno = TSDB_CODE_INVALID_MSG;
goto _err;
2021-11-03 06:49:20 +00:00
}
2022-04-26 02:25:07 +00:00
// validate req
2022-04-26 11:04:26 +00:00
metaReaderInit(&mr, pMeta, 0);
2022-04-26 02:25:07 +00:00
if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
2022-05-08 03:19:57 +00:00
pReq->uid = mr.me.uid;
if (pReq->type == TSDB_CHILD_TABLE) {
pReq->ctb.suid = mr.me.ctbEntry.suid;
}
2022-04-26 02:25:07 +00:00
terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
metaReaderClear(&mr);
return -1;
2022-04-22 09:42:31 +00:00
}
2022-04-26 02:25:07 +00:00
metaReaderClear(&mr);
2022-04-22 09:42:31 +00:00
// build SMetaEntry
2022-04-24 02:50:08 +00:00
me.version = version;
2022-04-22 09:42:31 +00:00
me.type = pReq->type;
me.uid = pReq->uid;
me.name = pReq->name;
if (me.type == TSDB_CHILD_TABLE) {
me.ctbEntry.ctime = pReq->ctime;
me.ctbEntry.ttlDays = pReq->ttl;
2022-06-16 12:45:00 +00:00
me.ctbEntry.commentLen = pReq->commentLen;
2022-06-16 12:44:25 +00:00
me.ctbEntry.comment = pReq->comment;
2022-04-22 09:42:31 +00:00
me.ctbEntry.suid = pReq->ctb.suid;
me.ctbEntry.pTags = pReq->ctb.pTag;
} else {
me.ntbEntry.ctime = pReq->ctime;
me.ntbEntry.ttlDays = pReq->ttl;
2022-06-16 12:45:00 +00:00
me.ntbEntry.commentLen = pReq->commentLen;
2022-06-16 12:44:25 +00:00
me.ntbEntry.comment = pReq->comment;
2022-05-26 08:29:52 +00:00
me.ntbEntry.schemaRow = pReq->ntb.schemaRow;
me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1;
2021-11-03 06:49:20 +00:00
}
2022-04-23 13:45:26 +00:00
if (metaHandleEntry(pMeta, &me) < 0) goto _err;
2022-04-22 09:42:31 +00:00
2022-06-02 05:57:39 +00:00
metaDebug("vgId:%d, table %s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid,
2022-04-26 09:16:29 +00:00
pReq->type);
2021-11-02 07:24:55 +00:00
return 0;
2022-04-22 09:42:31 +00:00
_err:
2022-06-02 05:57:39 +00:00
metaError("vgId:%d, failed to create table:%s type:%s since %s", TD_VID(pMeta->pVnode), pReq->name,
2022-04-22 09:42:31 +00:00
pReq->type == TSDB_CHILD_TABLE ? "child table" : "normal table", tstrerror(terrno));
return -1;
2021-11-02 07:24:55 +00:00
}
int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids) {
2022-07-06 12:21:59 +00:00
void *pData = NULL;
2022-05-25 08:03:05 +00:00
int nData = 0;
int rc = 0;
tb_uid_t uid;
int type;
2021-11-03 06:49:20 +00:00
2022-05-25 08:03:05 +00:00
rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
if (rc < 0) {
terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
2021-11-03 06:49:20 +00:00
return -1;
}
2022-05-04 03:57:16 +00:00
uid = *(tb_uid_t *)pData;
2022-05-25 08:03:05 +00:00
metaWLock(pMeta);
metaDropTableByUid(pMeta, uid, &type);
metaULock(pMeta);
2022-05-04 06:16:29 +00:00
if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
2022-05-25 08:03:05 +00:00
taosArrayPush(tbUids, &uid);
2022-05-04 06:16:29 +00:00
}
2022-05-04 03:57:16 +00:00
2022-05-25 08:03:05 +00:00
tdbFree(pData);
return 0;
}
2022-05-04 06:16:29 +00:00
int metaTtlDropTable(SMeta *pMeta, int64_t ttl, SArray *tbUids) {
metaWLock(pMeta);
int ret = metaTtlSmaller(pMeta, ttl, tbUids);
2022-07-06 12:21:59 +00:00
if (ret != 0) {
2022-06-23 04:09:24 +00:00
metaULock(pMeta);
return ret;
}
for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
tb_uid_t *uid = (tb_uid_t *)taosArrayGet(tbUids, i);
metaDropTableByUid(pMeta, *uid, NULL);
2022-07-06 12:21:59 +00:00
metaDebug("ttl drop table:%" PRId64, *uid);
}
metaULock(pMeta);
return 0;
}
2022-07-06 12:21:59 +00:00
static void metaBuildTtlIdxKey(STtlIdxKey *ttlKey, const SMetaEntry *pME) {
int64_t ttlDays;
int64_t ctime;
if (pME->type == TSDB_CHILD_TABLE) {
ctime = pME->ctbEntry.ctime;
ttlDays = pME->ctbEntry.ttlDays;
} else if (pME->type == TSDB_NORMAL_TABLE) {
ctime = pME->ntbEntry.ctime;
ttlDays = pME->ntbEntry.ttlDays;
} else {
ASSERT(0);
}
if (ttlDays <= 0) return;
2022-06-21 10:37:47 +00:00
ttlKey->dtime = ctime / 1000 + ttlDays * tsTtlUnit;
ttlKey->uid = pME->uid;
}
static int metaDeleteTtlIdx(SMeta *pMeta, const SMetaEntry *pME) {
STtlIdxKey ttlKey = {0};
metaBuildTtlIdxKey(&ttlKey, pME);
2022-07-06 12:21:59 +00:00
if (ttlKey.dtime == 0) return 0;
return tdbTbDelete(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), &pMeta->txn);
}
2022-05-25 08:03:05 +00:00
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type) {
2022-07-06 12:21:59 +00:00
void *pData = NULL;
2022-05-25 08:03:05 +00:00
int nData = 0;
int rc = 0;
SMetaEntry e = {0};
SDecoder dc = {0};
rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
2022-06-16 12:44:25 +00:00
int64_t version = *(int64_t *)pData;
2022-05-25 08:03:05 +00:00
tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
tDecoderInit(&dc, pData, nData);
metaDecodeEntry(&dc, &e);
if (type) *type = e.type;
if (e.type == TSDB_CHILD_TABLE) {
void *tData = NULL;
int tLen = 0;
if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
version = *(int64_t *)tData;
STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = version};
if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
SDecoder tdc = {0};
SMetaEntry stbEntry = {0};
tDecoderInit(&tdc, tData, tLen);
metaDecodeEntry(&tdc, &stbEntry);
const SSchema *pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
if (pTagColumn->type == TSDB_DATA_TYPE_JSON) {
metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
}
tDecoderClear(&tdc);
}
tdbFree(tData);
}
}
2022-05-25 08:03:05 +00:00
tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pMeta->txn);
tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, &pMeta->txn);
tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), &pMeta->txn);
2022-07-06 12:21:59 +00:00
if (e.type != TSDB_SUPER_TABLE) metaDeleteTtlIdx(pMeta, &e);
2022-06-26 10:44:49 +00:00
2022-05-25 08:03:05 +00:00
if (e.type == TSDB_CHILD_TABLE) {
tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), &pMeta->txn);
} else if (e.type == TSDB_NORMAL_TABLE) {
// drop schema.db (todo)
} else if (e.type == TSDB_SUPER_TABLE) {
2022-06-26 10:44:49 +00:00
tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), &pMeta->txn);
2022-05-25 08:03:05 +00:00
// drop schema.db (todo)
2022-05-04 06:16:29 +00:00
}
2022-05-25 08:03:05 +00:00
tDecoderClear(&dc);
tdbFree(pData);
2022-05-04 06:16:29 +00:00
2021-11-02 07:24:55 +00:00
return 0;
}
2022-04-21 14:01:58 +00:00
2022-06-01 12:28:29 +00:00
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
2022-07-06 12:21:59 +00:00
void *pVal = NULL;
2022-05-16 07:18:44 +00:00
int nVal = 0;
2022-07-06 12:21:59 +00:00
const void *pData = NULL;
2022-05-16 07:18:44 +00:00
int nData = 0;
int ret = 0;
tb_uid_t uid;
int64_t oversion;
2022-07-06 12:21:59 +00:00
SSchema *pColumn = NULL;
2022-05-16 07:18:44 +00:00
SMetaEntry entry = {0};
SSchemaWrapper *pSchema;
int c;
// search name index
2022-05-18 07:57:29 +00:00
ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
2022-05-16 07:18:44 +00:00
if (ret < 0) {
terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
return -1;
}
uid = *(tb_uid_t *)pVal;
tdbFree(pVal);
pVal = NULL;
// search uid index
2022-05-18 07:57:29 +00:00
TBC *pUidIdxc = NULL;
2022-05-16 07:18:44 +00:00
2022-05-18 07:57:29 +00:00
tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
2022-05-16 07:18:44 +00:00
ASSERT(c == 0);
2022-05-18 07:57:29 +00:00
tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
2022-05-16 07:18:44 +00:00
oversion = *(int64_t *)pData;
// search table.db
2022-05-18 07:57:29 +00:00
TBC *pTbDbc = NULL;
2022-05-16 07:18:44 +00:00
2022-05-18 07:57:29 +00:00
tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
2022-05-16 07:18:44 +00:00
ASSERT(c == 0);
2022-05-18 07:57:29 +00:00
tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
2022-05-16 07:18:44 +00:00
// get table entry
SDecoder dc = {0};
2022-05-23 07:47:49 +00:00
entry.pBuf = taosMemoryMalloc(nData);
memcpy(entry.pBuf, pData, nData);
tDecoderInit(&dc, entry.pBuf, nData);
2022-05-17 02:52:46 +00:00
ret = metaDecodeEntry(&dc, &entry);
ASSERT(ret == 0);
2022-05-16 07:18:44 +00:00
if (entry.type != TSDB_NORMAL_TABLE) {
terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
goto _err;
}
// search the column to add/drop/update
2022-05-26 08:29:52 +00:00
pSchema = &entry.ntbEntry.schemaRow;
2022-05-16 07:18:44 +00:00
int32_t iCol = 0;
for (;;) {
pColumn = NULL;
if (iCol >= pSchema->nCols) break;
pColumn = &pSchema->pSchema[iCol];
if (strcmp(pColumn->name, pAlterTbReq->colName) == 0) break;
iCol++;
}
entry.version = version;
2022-05-18 11:54:52 +00:00
int tlen;
SSchema *pNewSchema = NULL;
2022-05-16 07:18:44 +00:00
switch (pAlterTbReq->action) {
case TSDB_ALTER_TABLE_ADD_COLUMN:
if (pColumn) {
terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
goto _err;
}
2022-05-26 08:29:52 +00:00
pSchema->version++;
2022-05-16 07:18:44 +00:00
pSchema->nCols++;
2022-05-18 11:54:52 +00:00
pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
pSchema->pSchema = pNewSchema;
2022-05-26 08:29:52 +00:00
pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].bytes = pAlterTbReq->bytes;
pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type = pAlterTbReq->type;
pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].flags = pAlterTbReq->flags;
pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId = entry.ntbEntry.ncid++;
strcpy(pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].name, pAlterTbReq->colName);
2022-05-16 07:18:44 +00:00
break;
case TSDB_ALTER_TABLE_DROP_COLUMN:
if (pColumn == NULL) {
terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
goto _err;
}
if (pColumn->colId == 0) {
terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
goto _err;
}
2022-05-26 08:29:52 +00:00
pSchema->version++;
2022-05-16 07:18:44 +00:00
tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
if (tlen) {
memmove(pColumn, pColumn + 1, tlen);
}
2022-05-17 02:52:46 +00:00
pSchema->nCols--;
2022-05-16 07:18:44 +00:00
break;
case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
if (pColumn == NULL) {
terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
goto _err;
}
2022-05-21 03:28:29 +00:00
if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes > pAlterTbReq->colModBytes) {
2022-05-16 07:18:44 +00:00
terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
goto _err;
}
2022-05-26 08:29:52 +00:00
pSchema->version++;
2022-05-21 03:28:29 +00:00
pColumn->bytes = pAlterTbReq->colModBytes;
2022-05-16 07:18:44 +00:00
break;
case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
if (pColumn == NULL) {
terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
goto _err;
}
2022-05-26 08:29:52 +00:00
pSchema->version++;
2022-05-16 07:18:44 +00:00
strcpy(pColumn->name, pAlterTbReq->colNewName);
break;
}
entry.version = version;
2022-05-16 11:29:34 +00:00
// do actual write
metaWLock(pMeta);
// save to table db
metaSaveToTbDb(pMeta, &entry);
2022-05-18 07:57:29 +00:00
tdbTbcUpsert(pUidIdxc, &entry.uid, sizeof(tb_uid_t), &version, sizeof(version), 0);
2022-05-16 11:29:34 +00:00
metaSaveToSkmDb(pMeta, &entry);
metaULock(pMeta);
2022-06-01 12:28:29 +00:00
metaUpdateMetaRsp(uid, pAlterTbReq->tbName, pSchema, pMetaRsp);
2022-06-16 12:44:25 +00:00
if (entry.pBuf) taosMemoryFree(entry.pBuf);
2022-05-18 11:54:52 +00:00
if (pNewSchema) taosMemoryFree(pNewSchema);
2022-05-18 07:57:29 +00:00
tdbTbcClose(pTbDbc);
tdbTbcClose(pUidIdxc);
tDecoderClear(&dc);
2022-05-16 06:17:56 +00:00
return 0;
2022-05-16 07:18:44 +00:00
_err:
2022-06-16 12:44:25 +00:00
if (entry.pBuf) taosMemoryFree(entry.pBuf);
2022-05-18 07:57:29 +00:00
tdbTbcClose(pTbDbc);
tdbTbcClose(pUidIdxc);
tDecoderClear(&dc);
2022-05-16 07:18:44 +00:00
return -1;
2022-05-16 06:17:56 +00:00
}
static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
2022-05-16 12:13:59 +00:00
SMetaEntry ctbEntry = {0};
SMetaEntry stbEntry = {0};
2022-07-06 12:21:59 +00:00
void *pVal = NULL;
2022-05-16 11:29:34 +00:00
int nVal = 0;
int ret;
int c;
tb_uid_t uid;
int64_t oversion;
const void *pData = NULL;
int nData = 0;
// search name index
2022-05-18 07:57:29 +00:00
ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
2022-05-16 11:29:34 +00:00
if (ret < 0) {
terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
return -1;
}
uid = *(tb_uid_t *)pVal;
tdbFree(pVal);
pVal = NULL;
// search uid index
2022-05-18 07:57:29 +00:00
TBC *pUidIdxc = NULL;
2022-05-16 11:29:34 +00:00
2022-05-18 07:57:29 +00:00
tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
2022-05-16 11:29:34 +00:00
ASSERT(c == 0);
2022-05-18 07:57:29 +00:00
tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
2022-05-16 11:29:34 +00:00
oversion = *(int64_t *)pData;
// search table.db
2022-07-06 12:21:59 +00:00
TBC *pTbDbc = NULL;
2022-05-21 12:37:04 +00:00
SDecoder dc1 = {0};
SDecoder dc2 = {0};
2022-05-16 11:29:34 +00:00
2022-05-16 12:13:59 +00:00
/* get ctbEntry */
2022-05-18 07:57:29 +00:00
tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
2022-05-16 11:29:34 +00:00
ASSERT(c == 0);
2022-05-18 07:57:29 +00:00
tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
2022-05-16 11:29:34 +00:00
2022-05-16 12:13:59 +00:00
ctbEntry.pBuf = taosMemoryMalloc(nData);
memcpy(ctbEntry.pBuf, pData, nData);
2022-05-21 12:37:04 +00:00
tDecoderInit(&dc1, ctbEntry.pBuf, nData);
metaDecodeEntry(&dc1, &ctbEntry);
2022-05-16 11:29:34 +00:00
2022-05-16 12:13:59 +00:00
/* get stbEntry*/
2022-05-18 07:57:29 +00:00
tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal);
tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = *(int64_t *)pVal}), sizeof(STbDbKey),
(void **)&stbEntry.pBuf, &nVal);
2022-05-16 13:03:39 +00:00
tdbFree(pVal);
2022-05-21 12:37:04 +00:00
tDecoderInit(&dc2, stbEntry.pBuf, nVal);
metaDecodeEntry(&dc2, &stbEntry);
2022-05-16 13:03:39 +00:00
SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
2022-07-06 12:21:59 +00:00
SSchema *pColumn = NULL;
2022-05-16 13:03:39 +00:00
int32_t iCol = 0;
for (;;) {
pColumn = NULL;
if (iCol >= pTagSchema->nCols) break;
pColumn = &pTagSchema->pSchema[iCol];
if (strcmp(pColumn->name, pAlterTbReq->tagName) == 0) break;
iCol++;
}
if (pColumn == NULL) {
terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
goto _err;
}
2022-05-16 11:29:34 +00:00
2022-05-17 05:30:08 +00:00
if (iCol == 0) {
// TODO : need to update tag index
2022-05-16 11:29:34 +00:00
}
2022-05-17 05:30:08 +00:00
ctbEntry.version = version;
2022-05-25 07:14:36 +00:00
if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2022-05-23 11:32:10 +00:00
ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
2022-05-25 07:14:36 +00:00
if (ctbEntry.ctbEntry.pTags == NULL) {
2022-05-23 11:32:10 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-05-25 07:14:36 +00:00
memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
} else {
2022-05-28 09:16:04 +00:00
const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
2022-07-06 12:21:59 +00:00
STag *pNewTag = NULL;
SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
2022-05-28 10:05:50 +00:00
if (!pTagArray) {
2022-05-28 09:16:04 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-05-23 11:32:10 +00:00
for (int32_t i = 0; i < pTagSchema->nCols; i++) {
SSchema *pCol = &pTagSchema->pSchema[i];
if (iCol == i) {
2022-05-31 09:49:33 +00:00
STagVal val = {0};
val.type = pCol->type;
val.cid = pCol->colId;
if (IS_VAR_DATA_TYPE(pCol->type)) {
val.pData = pAlterTbReq->pTagVal;
val.nData = pAlterTbReq->nTagVal;
} else {
2022-05-31 09:49:33 +00:00
memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
}
taosArrayPush(pTagArray, &val);
2022-05-23 11:32:10 +00:00
} else {
2022-06-02 06:16:46 +00:00
STagVal val = {.cid = pCol->colId};
2022-05-31 09:49:33 +00:00
if (tTagGet(pOldTag, &val)) {
taosArrayPush(pTagArray, &val);
2022-05-17 05:30:08 +00:00
}
}
}
2022-05-28 10:05:50 +00:00
if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
taosArrayDestroy(pTagArray);
2022-05-28 09:16:04 +00:00
goto _err;
}
ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
2022-05-28 10:05:50 +00:00
taosArrayDestroy(pTagArray);
2022-05-23 11:32:10 +00:00
}
2022-05-17 05:30:08 +00:00
// save to table.db
metaSaveToTbDb(pMeta, &ctbEntry);
// save to uid.idx
2022-05-18 07:57:29 +00:00
tdbTbUpsert(pMeta->pUidIdx, &ctbEntry.uid, sizeof(tb_uid_t), &version, sizeof(version), &pMeta->txn);
2022-05-17 05:30:08 +00:00
2022-05-21 12:37:04 +00:00
tDecoderClear(&dc1);
tDecoderClear(&dc2);
2022-05-25 07:14:36 +00:00
if (ctbEntry.ctbEntry.pTags) taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
2022-05-16 13:03:39 +00:00
if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
2022-05-18 07:57:29 +00:00
tdbTbcClose(pTbDbc);
tdbTbcClose(pUidIdxc);
2022-05-16 06:17:56 +00:00
return 0;
2022-05-16 11:29:34 +00:00
_err:
2022-05-21 12:37:04 +00:00
tDecoderClear(&dc1);
tDecoderClear(&dc2);
2022-05-16 13:03:39 +00:00
if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
2022-05-18 07:57:29 +00:00
tdbTbcClose(pTbDbc);
tdbTbcClose(pUidIdxc);
2022-05-16 11:29:34 +00:00
return -1;
2022-05-16 06:17:56 +00:00
}
static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
2022-07-06 12:21:59 +00:00
void *pVal = NULL;
int nVal = 0;
const void *pData = NULL;
int nData = 0;
int ret = 0;
tb_uid_t uid;
int64_t oversion;
SMetaEntry entry = {0};
int c = 0;
2022-06-16 12:44:25 +00:00
// search name index
ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
if (ret < 0) {
terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
return -1;
}
2022-06-16 12:44:25 +00:00
uid = *(tb_uid_t *)pVal;
tdbFree(pVal);
pVal = NULL;
// search uid index
TBC *pUidIdxc = NULL;
tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
ASSERT(c == 0);
tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
oversion = *(int64_t *)pData;
// search table.db
TBC *pTbDbc = NULL;
tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
ASSERT(c == 0);
tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
// get table entry
SDecoder dc = {0};
entry.pBuf = taosMemoryMalloc(nData);
memcpy(entry.pBuf, pData, nData);
tDecoderInit(&dc, entry.pBuf, nData);
ret = metaDecodeEntry(&dc, &entry);
ASSERT(ret == 0);
entry.version = version;
metaWLock(pMeta);
// build SMetaEntry
if (entry.type == TSDB_CHILD_TABLE) {
2022-07-06 12:21:59 +00:00
if (pAlterTbReq->updateTTL) {
2022-06-16 12:44:25 +00:00
metaDeleteTtlIdx(pMeta, &entry);
entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
metaUpdateTtlIdx(pMeta, &entry);
}
2022-07-06 12:21:59 +00:00
if (pAlterTbReq->newCommentLen >= 0) {
2022-06-16 12:45:00 +00:00
entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
entry.ctbEntry.comment = pAlterTbReq->newComment;
}
2022-06-16 12:44:25 +00:00
} else {
2022-07-06 12:21:59 +00:00
if (pAlterTbReq->updateTTL) {
2022-06-16 12:44:25 +00:00
metaDeleteTtlIdx(pMeta, &entry);
entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
metaUpdateTtlIdx(pMeta, &entry);
}
2022-07-06 12:21:59 +00:00
if (pAlterTbReq->newCommentLen >= 0) {
2022-06-16 12:45:00 +00:00
entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
entry.ntbEntry.comment = pAlterTbReq->newComment;
}
}
2022-06-16 12:44:25 +00:00
// save to table db
metaSaveToTbDb(pMeta, &entry);
tdbTbcUpsert(pUidIdxc, &entry.uid, sizeof(tb_uid_t), &version, sizeof(version), 0);
metaULock(pMeta);
tdbTbcClose(pTbDbc);
tdbTbcClose(pUidIdxc);
tDecoderClear(&dc);
2022-06-16 12:44:25 +00:00
if (entry.pBuf) taosMemoryFree(entry.pBuf);
2022-05-16 06:17:56 +00:00
return 0;
}
2022-06-01 12:28:29 +00:00
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
2022-05-16 06:17:56 +00:00
switch (pReq->action) {
case TSDB_ALTER_TABLE_ADD_COLUMN:
case TSDB_ALTER_TABLE_DROP_COLUMN:
case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
2022-06-01 12:28:29 +00:00
return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp);
2022-05-16 06:17:56 +00:00
case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
return metaUpdateTableTagVal(pMeta, version, pReq);
case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
return metaUpdateTableOptions(pMeta, version, pReq);
default:
terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
return -1;
break;
}
}
2022-04-23 13:45:26 +00:00
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
2022-04-22 11:55:21 +00:00
STbDbKey tbDbKey;
2022-07-06 12:21:59 +00:00
void *pKey = NULL;
void *pVal = NULL;
2022-04-22 11:55:21 +00:00
int kLen = 0;
int vLen = 0;
2022-05-07 10:03:06 +00:00
SEncoder coder = {0};
2022-04-21 14:01:58 +00:00
// set key and value
2022-04-23 13:45:26 +00:00
tbDbKey.version = pME->version;
2022-04-22 11:55:21 +00:00
tbDbKey.uid = pME->uid;
pKey = &tbDbKey;
kLen = sizeof(tbDbKey);
2022-04-21 14:01:58 +00:00
2022-04-27 09:39:54 +00:00
int32_t ret = 0;
tEncodeSize(metaEncodeEntry, pME, vLen, ret);
if (ret < 0) {
2022-04-21 14:01:58 +00:00
goto _err;
}
pVal = taosMemoryMalloc(vLen);
if (pVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-05-07 10:03:06 +00:00
tEncoderInit(&coder, pVal, vLen);
2022-04-21 14:01:58 +00:00
if (metaEncodeEntry(&coder, pME) < 0) {
goto _err;
}
2022-05-07 10:03:06 +00:00
tEncoderClear(&coder);
2022-04-21 14:01:58 +00:00
// write to table.db
2022-05-18 07:57:29 +00:00
if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, &pMeta->txn) < 0) {
2022-04-21 14:01:58 +00:00
goto _err;
}
taosMemoryFree(pVal);
return 0;
_err:
taosMemoryFree(pVal);
return -1;
}
2022-04-23 13:45:26 +00:00
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
2022-05-18 07:57:29 +00:00
return tdbTbInsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &pME->version, sizeof(int64_t), &pMeta->txn);
2022-04-21 14:01:58 +00:00
}
2022-06-26 10:44:49 +00:00
static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME) {
return tdbTbInsert(pMeta->pSuidIdx, &pME->uid, sizeof(tb_uid_t), NULL, 0, &pMeta->txn);
}
2022-04-23 13:45:26 +00:00
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME) {
2022-05-18 07:57:29 +00:00
return tdbTbInsert(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), &pMeta->txn);
2022-04-22 09:42:31 +00:00
}
2022-04-23 13:45:26 +00:00
static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME) {
STtlIdxKey ttlKey = {0};
metaBuildTtlIdxKey(&ttlKey, pME);
2022-07-06 12:21:59 +00:00
if (ttlKey.dtime == 0) return 0;
2022-05-18 07:57:29 +00:00
return tdbTbInsert(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), NULL, 0, &pMeta->txn);
2022-04-22 09:42:31 +00:00
}
2022-04-23 13:45:26 +00:00
static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME) {
SCtbIdxKey ctbIdxKey = {.suid = pME->ctbEntry.suid, .uid = pME->uid};
2022-05-18 07:57:29 +00:00
return tdbTbInsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), NULL, 0, &pMeta->txn);
2022-04-23 13:45:26 +00:00
}
2022-06-01 01:58:58 +00:00
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
2022-06-02 08:36:48 +00:00
if (IS_VAR_DATA_TYPE(type)) {
*nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
} else {
*nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
}
2022-05-13 09:32:15 +00:00
*ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
if (*ppTagIdxKey == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
(*ppTagIdxKey)->suid = suid;
(*ppTagIdxKey)->cid = cid;
(*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
(*ppTagIdxKey)->type = type;
2022-06-02 08:36:48 +00:00
// refactor
if (IS_VAR_DATA_TYPE(type)) {
memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
*(tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData) = uid;
} else {
memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
*(tb_uid_t *)((*ppTagIdxKey)->data + nTagData) = uid;
}
2022-05-13 09:32:15 +00:00
return 0;
}
static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
}
static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
2022-07-06 12:21:59 +00:00
void *pData = NULL;
2022-05-13 09:32:15 +00:00
int nData = 0;
STbDbKey tbDbKey = {0};
SMetaEntry stbEntry = {0};
2022-07-06 12:21:59 +00:00
STagIdxKey *pTagIdxKey = NULL;
2022-05-13 09:32:15 +00:00
int32_t nTagIdxKey;
const SSchema *pTagColumn; // = &stbEntry.stbEntry.schema.pSchema[0];
2022-07-06 12:21:59 +00:00
const void *pTagData = NULL; //
2022-05-29 12:28:54 +00:00
int32_t nTagData = 0;
2022-05-13 09:32:15 +00:00
SDecoder dc = {0};
// get super table
2022-07-06 12:21:59 +00:00
if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
2022-06-21 02:52:09 +00:00
return -1;
}
2022-05-13 09:32:15 +00:00
tbDbKey.uid = pCtbEntry->ctbEntry.suid;
tbDbKey.version = *(int64_t *)pData;
2022-05-18 07:57:29 +00:00
tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
2022-05-13 09:32:15 +00:00
tDecoderInit(&dc, pData, nData);
metaDecodeEntry(&dc, &stbEntry);
pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
2022-05-28 09:16:04 +00:00
STagVal tagVal = {.cid = pTagColumn->colId};
if (pTagColumn->type != TSDB_DATA_TYPE_JSON) {
2022-05-31 15:38:21 +00:00
tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal);
if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
2022-06-01 02:29:10 +00:00
pTagData = tagVal.pData;
nTagData = (int32_t)tagVal.nData;
} else {
2022-06-01 02:29:10 +00:00
pTagData = &(tagVal.i64);
nTagData = tDataTypes[pTagColumn->type].bytes;
}
} else {
// pTagData = pCtbEntry->ctbEntry.pTags;
// nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
2022-06-04 14:33:36 +00:00
pTagData = pCtbEntry->ctbEntry.pTags;
nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
return metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
2022-05-31 15:38:21 +00:00
}
if (metaCreateTagIdxKey(pCtbEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
pCtbEntry->uid, &pTagIdxKey, &nTagIdxKey) < 0) {
2022-05-13 09:32:15 +00:00
return -1;
}
2022-05-18 07:57:29 +00:00
tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, &pMeta->txn);
2022-05-13 09:32:15 +00:00
metaDestroyTagIdxKey(pTagIdxKey);
tDecoderClear(&dc);
tdbFree(pData);
2022-04-22 09:42:31 +00:00
return 0;
}
2022-04-23 13:45:26 +00:00
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
2022-05-07 10:03:06 +00:00
SEncoder coder = {0};
2022-07-06 12:21:59 +00:00
void *pVal = NULL;
2022-04-23 13:45:26 +00:00
int vLen = 0;
int rcode = 0;
SSkmDbKey skmDbKey = {0};
const SSchemaWrapper *pSW;
if (pME->type == TSDB_SUPER_TABLE) {
2022-05-26 08:29:52 +00:00
pSW = &pME->stbEntry.schemaRow;
2022-04-23 13:45:26 +00:00
} else if (pME->type == TSDB_NORMAL_TABLE) {
2022-05-26 08:29:52 +00:00
pSW = &pME->ntbEntry.schemaRow;
2022-04-23 13:45:26 +00:00
} else {
ASSERT(0);
2022-04-22 09:42:31 +00:00
}
2022-04-23 13:45:26 +00:00
skmDbKey.uid = pME->uid;
2022-05-26 08:29:52 +00:00
skmDbKey.sver = pSW->version;
2022-04-22 12:51:50 +00:00
// if receive tmq meta message is: create stable1 then delete stable1 then create stable1 with multi vgroups
if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), NULL, NULL) == 0) {
return rcode;
}
2022-04-22 12:51:50 +00:00
// encode schema
2022-04-27 09:39:54 +00:00
int32_t ret = 0;
tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
if (ret < 0) return -1;
2022-04-23 13:45:26 +00:00
pVal = taosMemoryMalloc(vLen);
2022-04-22 12:51:50 +00:00
if (pVal == NULL) {
rcode = -1;
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
2022-05-07 10:03:06 +00:00
tEncoderInit(&coder, pVal, vLen);
2022-04-23 13:45:26 +00:00
tEncodeSSchemaWrapper(&coder, pSW);
2022-05-18 07:57:29 +00:00
if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, &pMeta->txn) < 0) {
2022-04-22 12:51:50 +00:00
rcode = -1;
goto _exit;
}
_exit:
2022-04-23 13:45:26 +00:00
taosMemoryFree(pVal);
2022-05-07 10:03:06 +00:00
tEncoderClear(&coder);
2022-04-23 13:45:26 +00:00
return rcode;
}
2022-07-06 12:21:59 +00:00
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
2022-05-11 14:20:14 +00:00
metaWLock(pMeta);
2022-04-23 13:45:26 +00:00
// save to table.db
2022-05-11 14:20:14 +00:00
if (metaSaveToTbDb(pMeta, pME) < 0) goto _err;
2022-04-23 13:45:26 +00:00
// update uid.idx
2022-05-11 14:20:14 +00:00
if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err;
2022-04-23 13:45:26 +00:00
// update name.idx
2022-05-11 14:20:14 +00:00
if (metaUpdateNameIdx(pMeta, pME) < 0) goto _err;
2022-04-23 13:45:26 +00:00
if (pME->type == TSDB_CHILD_TABLE) {
// update ctb.idx
2022-05-11 14:20:14 +00:00
if (metaUpdateCtbIdx(pMeta, pME) < 0) goto _err;
2022-04-23 13:45:26 +00:00
// update tag.idx
2022-05-11 14:20:14 +00:00
if (metaUpdateTagIdx(pMeta, pME) < 0) goto _err;
2022-04-23 13:45:26 +00:00
} else {
// update schema.db
2022-05-11 14:20:14 +00:00
if (metaSaveToSkmDb(pMeta, pME) < 0) goto _err;
2022-06-26 10:44:49 +00:00
if (pME->type == TSDB_SUPER_TABLE) {
if (metaUpdateSuidIdx(pMeta, pME) < 0) goto _err;
2022-07-06 12:21:59 +00:00
}
2022-04-23 13:45:26 +00:00
}
if (pME->type != TSDB_SUPER_TABLE) {
2022-05-11 14:20:14 +00:00
if (metaUpdateTtlIdx(pMeta, pME) < 0) goto _err;
2022-04-23 13:45:26 +00:00
}
2022-05-11 14:20:14 +00:00
metaULock(pMeta);
2022-04-22 12:51:50 +00:00
return 0;
2022-05-11 14:20:14 +00:00
_err:
metaULock(pMeta);
return -1;
}
2022-05-28 08:15:24 +00:00
// refactor later
2022-06-05 13:46:07 +00:00
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }