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

745 lines
17 KiB
C
Raw Normal View History

2022-04-19 07:09:58 +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/>.
*/
2022-04-26 11:04:26 +00:00
#include "meta.h"
2022-04-19 13:10:03 +00:00
2022-04-26 11:04:26 +00:00
void metaReaderInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
2022-04-24 05:24:55 +00:00
memset(pReader, 0, sizeof(*pReader));
pReader->flags = flags;
2022-04-26 11:04:26 +00:00
pReader->pMeta = pMeta;
2022-05-11 14:20:14 +00:00
metaRLock(pMeta);
2022-04-24 05:24:55 +00:00
}
2022-04-22 05:30:15 +00:00
2022-04-24 05:38:56 +00:00
void metaReaderClear(SMetaReader *pReader) {
2022-05-11 14:20:14 +00:00
if (pReader->pMeta) {
metaULock(pReader->pMeta);
}
2022-05-07 10:03:06 +00:00
tDecoderClear(&pReader->coder);
2022-04-28 07:02:49 +00:00
tdbFree(pReader->pBuf);
2022-04-22 05:30:15 +00:00
}
2022-04-24 05:24:55 +00:00
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
2022-06-10 10:24:04 +00:00
SMeta * pMeta = pReader->pMeta;
2022-04-22 11:55:21 +00:00
STbDbKey tbDbKey = {.version = version, .uid = uid};
2022-04-22 05:30:15 +00:00
// query table.db
2022-05-18 07:57:29 +00:00
if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf) < 0) {
2022-04-26 13:51:01 +00:00
terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
2022-04-22 05:30:15 +00:00
goto _err;
}
// decode the entry
2022-05-07 10:03:06 +00:00
tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
2022-04-22 05:30:15 +00:00
if (metaDecodeEntry(&pReader->coder, &pReader->me) < 0) {
goto _err;
}
return 0;
_err:
return -1;
}
2022-04-24 05:24:55 +00:00
int metaGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
2022-06-10 10:24:04 +00:00
SMeta * pMeta = pReader->pMeta;
2022-04-22 05:30:15 +00:00
int64_t version;
// query uid.idx
2022-05-18 07:57:29 +00:00
if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
2022-04-26 13:51:01 +00:00
terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
2022-04-22 05:30:15 +00:00
return -1;
}
version = *(int64_t *)pReader->pBuf;
2022-04-24 05:24:55 +00:00
return metaGetTableEntryByVersion(pReader, version, uid);
2022-04-22 05:30:15 +00:00
}
2022-04-24 05:24:55 +00:00
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
2022-06-10 10:24:04 +00:00
SMeta * pMeta = pReader->pMeta;
2022-04-22 05:30:15 +00:00
tb_uid_t uid;
// query name.idx
2022-05-18 07:57:29 +00:00
if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
2022-04-26 13:51:01 +00:00
terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
2022-04-22 05:30:15 +00:00
return -1;
}
uid = *(tb_uid_t *)pReader->pBuf;
2022-04-24 05:24:55 +00:00
return metaGetTableEntryByUid(pReader, uid);
2022-04-22 05:30:15 +00:00
}
2022-05-27 03:38:09 +00:00
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
2022-06-10 10:24:04 +00:00
void * pData = NULL;
2022-05-27 03:38:09 +00:00
int nData = 0;
tb_uid_t uid = 0;
2022-06-10 09:53:13 +00:00
metaRLock(pMeta);
2022-05-27 03:38:09 +00:00
if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
uid = *(tb_uid_t *)pData;
tdbFree(pData);
}
2022-06-10 09:53:13 +00:00
metaULock(pMeta);
return uid;
2022-05-27 03:38:09 +00:00
}
2022-04-24 05:38:56 +00:00
int metaReadNext(SMetaReader *pReader) {
2022-04-24 06:19:12 +00:00
SMeta *pMeta = pReader->pMeta;
2022-04-24 05:38:56 +00:00
// TODO
2022-04-24 06:19:12 +00:00
2022-04-24 05:38:56 +00:00
return 0;
}
#if 1 // ===================================================
2022-04-19 13:10:03 +00:00
SMTbCursor *metaOpenTbCursor(SMeta *pMeta) {
SMTbCursor *pTbCur = NULL;
pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
if (pTbCur == NULL) {
return NULL;
}
2022-04-26 11:04:26 +00:00
metaReaderInit(&pTbCur->mr, pMeta, 0);
2022-04-24 06:19:12 +00:00
2022-05-18 07:57:29 +00:00
tdbTbcOpen(pMeta->pUidIdx, &pTbCur->pDbc, NULL);
2022-04-19 13:10:03 +00:00
2022-05-18 07:57:29 +00:00
tdbTbcMoveToFirst(pTbCur->pDbc);
2022-04-29 10:02:36 +00:00
2022-04-19 13:10:03 +00:00
return pTbCur;
}
void metaCloseTbCursor(SMTbCursor *pTbCur) {
if (pTbCur) {
2022-04-28 07:02:49 +00:00
tdbFree(pTbCur->pKey);
tdbFree(pTbCur->pVal);
2022-04-24 06:19:12 +00:00
metaReaderClear(&pTbCur->mr);
2022-04-19 13:10:03 +00:00
if (pTbCur->pDbc) {
2022-05-18 07:57:29 +00:00
tdbTbcClose(pTbCur->pDbc);
2022-04-19 13:10:03 +00:00
}
taosMemoryFree(pTbCur);
}
}
2022-04-24 06:19:12 +00:00
int metaTbCursorNext(SMTbCursor *pTbCur) {
2022-04-19 13:10:03 +00:00
int ret;
2022-06-10 10:24:04 +00:00
void * pBuf;
2022-04-19 13:10:03 +00:00
STbCfg tbCfg;
for (;;) {
2022-05-18 07:57:29 +00:00
ret = tdbTbcNext(pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
2022-04-24 06:19:12 +00:00
if (ret < 0) {
return -1;
2022-04-19 13:10:03 +00:00
}
2022-04-24 06:19:12 +00:00
metaGetTableEntryByVersion(&pTbCur->mr, *(int64_t *)pTbCur->pVal, *(tb_uid_t *)pTbCur->pKey);
2022-04-26 11:04:26 +00:00
if (pTbCur->mr.me.type == TSDB_SUPER_TABLE) {
2022-04-24 06:19:12 +00:00
continue;
}
2022-04-24 06:30:35 +00:00
break;
2022-04-19 13:10:03 +00:00
}
2022-04-24 06:19:12 +00:00
return 0;
2022-04-19 13:10:03 +00:00
}
2022-04-25 12:23:00 +00:00
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline) {
2022-06-10 10:24:04 +00:00
void * pData = NULL;
2022-05-28 07:10:59 +00:00
int nData = 0;
int64_t version;
SSchemaWrapper schema = {0};
SSchemaWrapper *pSchema = NULL;
SDecoder dc = {0};
2022-05-12 03:34:38 +00:00
metaRLock(pMeta);
2022-06-07 07:56:33 +00:00
_query:
if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
goto _err;
}
2022-04-19 13:10:03 +00:00
2022-06-07 07:56:33 +00:00
version = *(int64_t *)pData;
2022-04-19 13:10:03 +00:00
2022-06-07 07:56:33 +00:00
tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData);
SMetaEntry me = {0};
tDecoderInit(&dc, pData, nData);
metaDecodeEntry(&dc, &me);
if (me.type == TSDB_SUPER_TABLE) {
if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
2022-05-28 07:10:59 +00:00
pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
2022-06-07 07:56:33 +00:00
tDecoderClear(&dc);
goto _exit;
2022-05-28 07:10:59 +00:00
}
2022-06-07 07:56:33 +00:00
} else if (me.type == TSDB_CHILD_TABLE) {
uid = me.ctbEntry.suid;
2022-05-28 07:10:59 +00:00
tDecoderClear(&dc);
2022-06-07 07:56:33 +00:00
goto _query;
2022-05-28 07:10:59 +00:00
} else {
2022-06-07 07:56:33 +00:00
if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
tDecoderClear(&dc);
goto _exit;
2022-05-28 07:10:59 +00:00
}
2022-06-07 07:56:33 +00:00
}
tDecoderClear(&dc);
2022-04-19 13:10:03 +00:00
2022-06-07 07:56:33 +00:00
// query from skm db
if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
goto _err;
2022-05-28 07:10:59 +00:00
}
2022-04-25 12:23:00 +00:00
2022-06-07 07:56:33 +00:00
tDecoderInit(&dc, pData, nData);
tDecodeSSchemaWrapper(&dc, &schema);
pSchema = tCloneSSchemaWrapper(&schema);
tDecoderClear(&dc);
_exit:
2022-05-28 07:10:59 +00:00
metaULock(pMeta);
tdbFree(pData);
return pSchema;
2022-04-25 12:23:00 +00:00
2022-05-28 07:10:59 +00:00
_err:
metaULock(pMeta);
tdbFree(pData);
return NULL;
2022-04-19 13:10:03 +00:00
}
int metaTtlSmaller(SMeta *pMeta, uint64_t ttl, SArray *uidList){
TBC * pCur;
int ret = tdbTbcOpen(pMeta->pTtlIdx, &pCur, NULL);
if (ret < 0) {
return ret;
}
STtlIdxKey ttlKey = {0};
ttlKey.dtime = ttl;
ttlKey.uid = INT64_MAX;
int c = 0;
tdbTbcMoveTo(pCur, &ttlKey, sizeof(ttlKey), &c);
if (c < 0) {
tdbTbcMoveToPrev(pCur);
}
void *pKey = NULL;
int kLen = 0;
while(1){
ret = tdbTbcPrev(pCur, &pKey, &kLen, NULL, NULL);
if (ret < 0) {
break;
}
ttlKey = *(STtlIdxKey*)pKey;
taosArrayPush(uidList, &ttlKey.uid);
}
tdbTbcClose(pCur);
tdbFree(pKey);
2022-06-23 04:09:24 +00:00
return 0;
}
2022-04-26 01:42:43 +00:00
struct SMCtbCursor {
2022-06-10 10:24:04 +00:00
SMeta * pMeta;
TBC * pCur;
2022-04-26 01:42:43 +00:00
tb_uid_t suid;
2022-06-10 10:24:04 +00:00
void * pKey;
void * pVal;
2022-04-26 01:42:43 +00:00
int kLen;
int vLen;
};
2022-04-19 13:10:03 +00:00
SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) {
SMCtbCursor *pCtbCur = NULL;
2022-04-29 10:02:36 +00:00
SCtbIdxKey ctbIdxKey;
2022-06-23 06:19:21 +00:00
int ret = 0;
int c = 0;
2022-04-19 13:10:03 +00:00
2022-04-26 01:42:43 +00:00
pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
if (pCtbCur == NULL) {
return NULL;
}
2022-04-19 13:10:03 +00:00
2022-05-12 03:34:38 +00:00
pCtbCur->pMeta = pMeta;
2022-04-26 01:42:43 +00:00
pCtbCur->suid = uid;
2022-05-12 03:34:38 +00:00
metaRLock(pMeta);
2022-05-18 07:57:29 +00:00
ret = tdbTbcOpen(pMeta->pCtbIdx, &pCtbCur->pCur, NULL);
2022-04-26 01:42:43 +00:00
if (ret < 0) {
2022-05-12 03:34:38 +00:00
metaULock(pMeta);
2022-04-26 01:42:43 +00:00
taosMemoryFree(pCtbCur);
return NULL;
}
2022-04-19 13:10:03 +00:00
2022-04-29 10:02:36 +00:00
// move to the suid
ctbIdxKey.suid = uid;
ctbIdxKey.uid = INT64_MIN;
2022-05-18 07:57:29 +00:00
tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
2022-04-29 10:02:36 +00:00
if (c > 0) {
2022-05-18 07:57:29 +00:00
tdbTbcMoveToNext(pCtbCur->pCur);
2022-04-29 10:02:36 +00:00
}
2022-04-19 13:10:03 +00:00
return pCtbCur;
}
2022-05-16 15:55:17 +00:00
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
2022-04-26 01:42:43 +00:00
if (pCtbCur) {
2022-05-12 03:34:38 +00:00
if (pCtbCur->pMeta) metaULock(pCtbCur->pMeta);
2022-04-26 01:42:43 +00:00
if (pCtbCur->pCur) {
2022-05-18 07:57:29 +00:00
tdbTbcClose(pCtbCur->pCur);
2022-04-19 13:10:03 +00:00
2022-04-28 07:02:49 +00:00
tdbFree(pCtbCur->pKey);
tdbFree(pCtbCur->pVal);
2022-04-26 01:42:43 +00:00
}
2022-04-19 13:10:03 +00:00
2022-04-26 01:42:43 +00:00
taosMemoryFree(pCtbCur);
}
2022-04-19 13:10:03 +00:00
}
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
2022-04-26 01:42:43 +00:00
int ret;
SCtbIdxKey *pCtbIdxKey;
2022-04-19 13:10:03 +00:00
2022-05-18 07:57:29 +00:00
ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
2022-04-26 01:42:43 +00:00
if (ret < 0) {
return 0;
}
2022-04-19 13:10:03 +00:00
2022-04-26 01:42:43 +00:00
pCtbIdxKey = pCtbCur->pKey;
2022-04-29 10:02:36 +00:00
if (pCtbIdxKey->suid > pCtbCur->suid) {
return 0;
}
2022-04-19 13:10:03 +00:00
2022-04-26 01:42:43 +00:00
return pCtbIdxKey->uid;
2022-04-19 13:10:03 +00:00
}
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver) {
2022-06-07 07:56:33 +00:00
// SMetaReader mr = {0};
2022-06-10 10:24:04 +00:00
STSchema * pTSchema = NULL;
2022-04-25 12:23:00 +00:00
SSchemaWrapper *pSW = NULL;
STSchemaBuilder sb = {0};
2022-06-10 10:24:04 +00:00
SSchema * pSchema;
2022-04-19 13:10:03 +00:00
2022-06-07 07:56:33 +00:00
pSW = metaGetTableSchema(pMeta, uid, sver, 0);
2022-05-11 13:24:58 +00:00
if (!pSW) return NULL;
2022-05-12 03:34:38 +00:00
tdInitTSchemaBuilder(&sb, pSW->version);
2022-04-19 13:10:03 +00:00
for (int i = 0; i < pSW->nCols; i++) {
pSchema = pSW->pSchema + i;
tdAddColToSchema(&sb, pSchema->type, pSchema->flags, pSchema->colId, pSchema->bytes);
}
pTSchema = tdGetSchemaFromBuilder(&sb);
2022-05-23 08:30:26 +00:00
2022-04-19 13:10:03 +00:00
tdDestroyTSchemaBuilder(&sb);
2022-04-25 12:23:00 +00:00
taosMemoryFree(pSW->pSchema);
taosMemoryFree(pSW);
2022-04-19 13:10:03 +00:00
return pTSchema;
}
2022-05-16 15:55:17 +00:00
int metaGetTbNum(SMeta *pMeta) {
// TODO
// ASSERT(0);
return 0;
}
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
typedef struct {
2022-06-10 10:24:04 +00:00
SMeta * pMeta;
TBC * pCur;
2022-05-16 15:55:17 +00:00
tb_uid_t uid;
2022-06-10 10:24:04 +00:00
void * pKey;
void * pVal;
2022-05-16 15:55:17 +00:00
int kLen;
int vLen;
} SMSmaCursor;
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
SMSmaCursor *pSmaCur = NULL;
SSmaIdxKey smaIdxKey;
int ret;
int c;
pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
if (pSmaCur == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-04-19 13:10:03 +00:00
return NULL;
}
2022-05-16 15:55:17 +00:00
pSmaCur->pMeta = pMeta;
pSmaCur->uid = uid;
metaRLock(pMeta);
2022-05-18 07:57:29 +00:00
ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
2022-05-16 15:55:17 +00:00
if (ret < 0) {
metaULock(pMeta);
taosMemoryFree(pSmaCur);
2022-04-19 13:10:03 +00:00
return NULL;
}
2022-05-16 15:55:17 +00:00
// move to the suid
smaIdxKey.uid = uid;
smaIdxKey.smaUid = INT64_MIN;
2022-05-18 07:57:29 +00:00
tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
2022-05-16 15:55:17 +00:00
if (c > 0) {
2022-05-18 07:57:29 +00:00
tdbTbcMoveToNext(pSmaCur->pCur);
2022-05-16 15:55:17 +00:00
}
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
return pSmaCur;
}
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
if (pSmaCur) {
if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
if (pSmaCur->pCur) {
2022-05-18 07:57:29 +00:00
tdbTbcClose(pSmaCur->pCur);
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
tdbFree(pSmaCur->pKey);
tdbFree(pSmaCur->pVal);
}
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
taosMemoryFree(pSmaCur);
}
}
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
int ret;
SSmaIdxKey *pSmaIdxKey;
2022-05-18 07:57:29 +00:00
ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
2022-05-16 15:55:17 +00:00
if (ret < 0) {
return 0;
}
pSmaIdxKey = pSmaCur->pKey;
if (pSmaIdxKey->uid > pSmaCur->uid) {
return 0;
}
return pSmaIdxKey->uid;
}
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
STSmaWrapper *pSW = NULL;
2022-06-10 10:24:04 +00:00
SArray * pSmaIds = NULL;
2022-05-16 15:55:17 +00:00
if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
return NULL;
}
pSW = taosMemoryCalloc(1, sizeof(*pSW));
if (!pSW) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
pSW->number = taosArrayGetSize(pSmaIds);
pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
if (!pSW->tSma) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
SMetaReader mr = {0};
metaReaderInit(&mr, pMeta, 0);
int64_t smaId;
int smaIdx = 0;
2022-06-10 10:24:04 +00:00
STSma * pTSma = NULL;
2022-05-16 15:55:17 +00:00
for (int i = 0; i < pSW->number; ++i) {
smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
if (metaGetTableEntryByUid(&mr, smaId) < 0) {
2022-06-02 05:57:39 +00:00
metaWarn("vgId:%d, no entry for tbId: %" PRIi64 ", smaId: %" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
2022-05-16 15:55:17 +00:00
continue;
}
pTSma = pSW->tSma + smaIdx;
memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
if (deepCopy) {
if (pTSma->exprLen > 0) {
if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-05-18 05:46:16 +00:00
memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
2022-04-19 13:10:03 +00:00
}
2022-05-16 15:55:17 +00:00
if (pTSma->tagsFilterLen > 0) {
if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-04-19 13:10:03 +00:00
}
2022-05-18 05:46:16 +00:00
memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
2022-05-16 15:55:17 +00:00
} else {
pTSma->exprLen = 0;
pTSma->expr = NULL;
pTSma->tagsFilterLen = 0;
pTSma->tagsFilter = NULL;
2022-04-19 13:10:03 +00:00
}
2022-05-18 05:46:16 +00:00
2022-05-16 15:55:17 +00:00
++smaIdx;
2022-04-19 13:10:03 +00:00
}
2022-05-16 15:55:17 +00:00
if (smaIdx <= 0) goto _err;
pSW->number = smaIdx;
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
metaReaderClear(&mr);
taosArrayDestroy(pSmaIds);
2022-04-19 13:10:03 +00:00
return pSW;
2022-05-16 15:55:17 +00:00
_err:
metaReaderClear(&mr);
taosArrayDestroy(pSmaIds);
2022-06-01 11:06:58 +00:00
tFreeTSmaWrapper(pSW, deepCopy);
2022-04-19 13:10:03 +00:00
return NULL;
}
2022-05-16 15:55:17 +00:00
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
2022-06-10 10:24:04 +00:00
STSma * pTSma = NULL;
2022-05-16 15:55:17 +00:00
SMetaReader mr = {0};
metaReaderInit(&mr, pMeta, 0);
if (metaGetTableEntryByUid(&mr, indexUid) < 0) {
2022-06-02 05:57:39 +00:00
metaWarn("vgId:%d, failed to get table entry for smaId: %" PRIi64, TD_VID(pMeta->pVnode), indexUid);
2022-05-16 15:55:17 +00:00
metaReaderClear(&mr);
return NULL;
}
pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
if (!pTSma) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
metaReaderClear(&mr);
return NULL;
}
memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
metaReaderClear(&mr);
return pTSma;
2022-04-19 13:10:03 +00:00
}
2022-05-16 15:55:17 +00:00
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
2022-06-10 10:24:04 +00:00
SArray * pUids = NULL;
2022-05-16 15:55:17 +00:00
SSmaIdxKey *pSmaIdxKey = NULL;
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
if (!pCur) {
2022-04-19 13:10:03 +00:00
return NULL;
}
2022-05-16 15:55:17 +00:00
while (1) {
tb_uid_t id = metaSmaCursorNext(pCur);
if (id == 0) {
break;
}
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
if (!pUids) {
pUids = taosArrayInit(16, sizeof(tb_uid_t));
2022-04-19 13:10:03 +00:00
if (!pUids) {
2022-05-16 15:55:17 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
metaCloseSmaCursor(pCur);
return NULL;
2022-04-19 13:10:03 +00:00
}
2022-05-16 15:55:17 +00:00
}
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
if (taosArrayPush(pUids, &pSmaIdxKey->smaUid) < 0) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
metaCloseSmaCursor(pCur);
taosArrayDestroy(pUids);
return NULL;
2022-04-19 13:10:03 +00:00
}
}
metaCloseSmaCursor(pCur);
return pUids;
}
2022-05-16 15:55:17 +00:00
SArray *metaGetSmaTbUids(SMeta *pMeta) {
2022-06-10 10:24:04 +00:00
SArray * pUids = NULL;
2022-05-16 15:55:17 +00:00
SSmaIdxKey *pSmaIdxKey = NULL;
tb_uid_t lastUid = 0;
SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
if (!pCur) {
2022-04-19 13:10:03 +00:00
return NULL;
}
2022-05-16 15:55:17 +00:00
while (1) {
tb_uid_t uid = metaSmaCursorNext(pCur);
if (uid == 0) {
break;
}
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
if (lastUid == uid) {
continue;
}
2022-04-19 13:10:03 +00:00
2022-05-16 15:55:17 +00:00
lastUid = uid;
if (!pUids) {
pUids = taosArrayInit(16, sizeof(tb_uid_t));
if (!pUids) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
metaCloseSmaCursor(pCur);
return NULL;
}
}
if (taosArrayPush(pUids, &uid) < 0) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
metaCloseSmaCursor(pCur);
taosArrayDestroy(pUids);
return NULL;
}
2022-04-19 13:10:03 +00:00
}
2022-05-16 15:55:17 +00:00
metaCloseSmaCursor(pCur);
return pUids;
2022-04-22 05:30:15 +00:00
}
2022-04-28 08:31:35 +00:00
#endif
2022-05-12 11:21:44 +00:00
2022-05-31 09:49:33 +00:00
const void *metaGetTableTagVal(SMetaEntry *pEntry, int16_t type, STagVal *val) {
2022-05-12 11:21:44 +00:00
ASSERT(pEntry->type == TSDB_CHILD_TABLE);
2022-05-31 09:49:33 +00:00
STag *tag = (STag *)pEntry->ctbEntry.pTags;
2022-06-02 08:36:48 +00:00
if (type == TSDB_DATA_TYPE_JSON) {
2022-05-31 09:49:33 +00:00
return tag;
}
2022-05-31 15:38:21 +00:00
bool find = tTagGet(tag, val);
2022-06-02 08:36:48 +00:00
if (!find) {
2022-05-31 15:38:21 +00:00
return NULL;
}
return val;
2022-05-31 10:34:17 +00:00
}
2022-06-01 01:58:58 +00:00
typedef struct {
2022-06-10 10:24:04 +00:00
SMeta * pMeta;
TBC * pCur;
2022-06-01 01:58:58 +00:00
tb_uid_t suid;
int16_t cid;
int16_t type;
2022-06-10 10:24:04 +00:00
void * pKey;
void * pVal;
2022-06-01 01:58:58 +00:00
int32_t kLen;
int32_t vLen;
} SIdxCursor;
int32_t metaFilteTableIds(SMeta *pMeta, SMetaFltParam *param, SArray *pUids) {
SIdxCursor *pCursor = NULL;
2022-06-10 10:24:04 +00:00
char * buf = NULL;
int32_t maxSize = 0;
2022-06-01 01:58:58 +00:00
int32_t ret = 0, valid = 0;
pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
pCursor->pMeta = pMeta;
pCursor->suid = param->suid;
pCursor->cid = param->cid;
pCursor->type = param->type;
metaRLock(pMeta);
ret = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
if (ret < 0) {
goto END;
}
STagIdxKey *pKey = NULL;
int32_t nKey = 0;
int32_t nTagData = 0;
2022-06-10 10:24:04 +00:00
void * tagData = NULL;
2022-06-02 08:36:48 +00:00
2022-06-10 10:51:48 +00:00
if (param->val == NULL) {
2022-06-17 06:02:51 +00:00
metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
2022-06-10 10:51:48 +00:00
return -1;
2022-06-02 08:36:48 +00:00
} else {
2022-06-10 10:51:48 +00:00
if (IS_VAR_DATA_TYPE(param->type)) {
tagData = varDataVal(param->val);
nTagData = varDataLen(param->val);
if (param->type == TSDB_DATA_TYPE_NCHAR) {
maxSize = 4 * nTagData + 1;
buf = taosMemoryCalloc(1, maxSize);
if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize)) {
goto END;
}
2022-06-10 10:24:04 +00:00
2022-06-10 10:51:48 +00:00
tagData = buf;
nTagData = maxSize;
}
} else {
tagData = param->val;
nTagData = tDataTypes[param->type].bytes;
2022-06-10 10:24:04 +00:00
}
2022-06-01 01:58:58 +00:00
}
2022-06-02 08:36:48 +00:00
ret = metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
2022-06-01 01:58:58 +00:00
param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey);
2022-06-10 10:24:04 +00:00
2022-06-01 01:58:58 +00:00
if (ret != 0) {
goto END;
}
int cmp = 0;
if (tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp) < 0) {
goto END;
}
2022-06-02 08:36:48 +00:00
2022-06-10 10:24:04 +00:00
void * entryKey = NULL, *entryVal = NULL;
2022-06-01 01:58:58 +00:00
int32_t nEntryKey, nEntryVal;
2022-06-14 05:44:13 +00:00
bool first = true;
2022-06-01 01:58:58 +00:00
while (1) {
valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
if (valid < 0) {
break;
}
STagIdxKey *p = entryKey;
2022-06-14 05:44:13 +00:00
if (p->type != pCursor->type) {
if (first) {
valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
if (valid < 0) break;
continue;
} else {
break;
}
}
first = false;
2022-06-01 01:58:58 +00:00
if (p != NULL) {
int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
if (cmp == 0) {
// match
2022-06-02 08:36:48 +00:00
tb_uid_t tuid = 0;
if (IS_VAR_DATA_TYPE(pKey->type)) {
tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
} else {
tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
}
2022-06-01 01:58:58 +00:00
taosArrayPush(pUids, &tuid);
} else if (cmp == 1) {
// not match but should continue to iter
} else {
// not match and no more result
break;
}
}
valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
if (valid < 0) {
break;
}
}
END:
if (pCursor->pMeta) metaULock(pCursor->pMeta);
if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
2022-06-10 10:24:04 +00:00
taosMemoryFree(buf);
2022-06-01 01:58:58 +00:00
taosMemoryFree(pCursor);
return ret;
2022-06-02 08:36:48 +00:00
}