diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index 596fbf70b0..21602627e9 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -557,8 +557,7 @@ _exit: int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { SMetaEntry oStbEntry = {0}; SMetaEntry nStbEntry = {0}; - - STbDbKey tbDbKey = {0}; + STbDbKey tbDbKey = {0}; TBC *pUidIdxc = NULL; TBC *pTbDbc = NULL; @@ -569,32 +568,37 @@ int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { int32_t ret; int32_t c = -2; tb_uid_t suid = pReq->suid; + int32_t code = 0; // get super table - if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData) != 0) { - ret = -1; + if ((code = tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData)) != 0) { goto _err; } tbDbKey.uid = suid; tbDbKey.version = ((SUidIdxVal *)pData)[0].version; - (void)tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData); + if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) { + goto _err; + } tDecoderInit(&dc, pData, nData); - ret = metaDecodeEntry(&dc, &oStbEntry); - if (ret < 0) { + code = metaDecodeEntry(&dc, &oStbEntry); + if (code < 0) { goto _err; } if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) { + code = TSDB_CODE_INVALID_PARA; goto _err; } if (oStbEntry.stbEntry.schemaTag.version == pReq->schemaTag.version) { + code = TSDB_CODE_INVALID_PARA; goto _err; } if (oStbEntry.stbEntry.schemaTag.nCols != pReq->schemaTag.nCols) { + code = TSDB_CODE_INVALID_PARA; goto _err; } @@ -604,6 +608,7 @@ int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { SSchema *pOld = oStbEntry.stbEntry.schemaTag.pSchema + i; if (pNew->type != pOld->type || pNew->colId != pOld->colId || pNew->bytes != pOld->bytes || strncmp(pOld->name, pNew->name, sizeof(pNew->name))) { + code = TSDB_CODE_INVALID_PARA; goto _err; } if (IS_IDX_ON(pNew) && !IS_IDX_ON(pOld)) { @@ -614,13 +619,14 @@ int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { } if (diffIdx == -1) { + code = TSDB_CODE_INVALID_PARA; goto _err; } // Get target schema info SSchemaWrapper *pTagSchema = &pReq->schemaTag; if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { - terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS; + code = TSDB_CODE_INVALID_PARA; goto _err; } SSchema *pCol = pTagSchema->pSchema + diffIdx; @@ -629,17 +635,21 @@ int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { * iterator all pTdDbc by uid and version */ TBC *pCtbIdxc = NULL; - TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL)); - int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c); - if (rc < 0) { + code = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL); + if (code != 0) { + goto _err; + } + + code = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c); + if (code < 0) { (void)tdbTbcClose(pCtbIdxc); goto _err; } for (;;) { void *pKey = NULL, *pVal = NULL; int nKey = 0, nVal = 0; - rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal); - if (rc < 0) { + code = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal); + if (code < 0) { tdbFree(pKey); tdbFree(pVal); (void)tdbTbcClose(pCtbIdxc); @@ -672,10 +682,10 @@ int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { nTagData = tDataTypes[pCol->type].bytes; } } - rc = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey); + code = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey); tdbFree(pKey); tdbFree(pVal); - if (rc < 0) { + if (code < 0) { metaDestroyTagIdxKey(pTagIdxKey); (void)tdbTbcClose(pCtbIdxc); goto _err; @@ -684,6 +694,7 @@ int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { metaWLock(pMeta); (void)tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn); metaULock(pMeta); + metaDestroyTagIdxKey(pTagIdxKey); pTagIdxKey = NULL; } @@ -714,9 +725,10 @@ _err: tDecoderClear(&dc); tdbFree(pData); - return TSDB_CODE_VND_COL_ALREADY_EXISTS; + return code; } int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) { + int32_t code = 0; SMetaEntry oStbEntry = {0}; SMetaEntry nStbEntry = {0}; @@ -732,17 +744,19 @@ int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) tb_uid_t suid = pReq->stbUid; - if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData) != 0) { - ret = -1; + if ((code = tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData)) != 0) { goto _err; } tbDbKey.uid = suid; tbDbKey.version = ((SUidIdxVal *)pData)[0].version; - (void)tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData); + if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) { + goto _err; + } + tDecoderInit(&dc, pData, nData); - ret = metaDecodeEntry(&dc, &oStbEntry); - if (ret < 0) { + code = metaDecodeEntry(&dc, &oStbEntry); + if (code != 0) { goto _err; } @@ -759,6 +773,7 @@ int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) } if (pCol == NULL) { + code = TSDB_CODE_VND_COL_NOT_EXISTS; goto _err; } @@ -766,17 +781,22 @@ int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) * iterator all pTdDbc by uid and version */ TBC *pCtbIdxc = NULL; - TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL)); - int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c); - if (rc < 0) { + code = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL); + if (code != 0) { + goto _err; + } + + code = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c); + if (code < 0) { (void)tdbTbcClose(pCtbIdxc); goto _err; } for (;;) { void *pKey = NULL, *pVal = NULL; int nKey = 0, nVal = 0; - rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal); - if (rc < 0) { + + code = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal); + if (code < 0) { tdbFree(pKey); tdbFree(pVal); (void)tdbTbcClose(pCtbIdxc); @@ -809,10 +829,11 @@ int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) nTagData = tDataTypes[pCol->type].bytes; } } - rc = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey); + + code = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey); tdbFree(pKey); tdbFree(pVal); - if (rc < 0) { + if (code < 0) { metaDestroyTagIdxKey(pTagIdxKey); (void)tdbTbcClose(pCtbIdxc); goto _err; @@ -836,6 +857,16 @@ int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) SSchemaWrapper *row = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaRow); SSchemaWrapper *tag = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaTag); SColCmprWrapper *cmpr = tCloneSColCmprWrapper(&oStbEntry.colCmpr); + if (row == NULL || tag == NULL || cmpr == NULL) { + tDeleteSchemaWrapper(row); + tDeleteSchemaWrapper(tag); + tDeleteSColCmprWrapper(cmpr); + code = TSDB_CODE_OUT_OF_MEMORY; + + (void)tdbTbcClose(pCtbIdxc); + goto _err; + } + nStbEntry.stbEntry.schemaRow = *row; nStbEntry.stbEntry.schemaTag = *tag; @@ -866,7 +897,7 @@ _err: tDecoderClear(&dc); tdbFree(pData); - return -1; + return code; } int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) { diff --git a/source/libs/tdb/src/db/tdbTable.c b/source/libs/tdb/src/db/tdbTable.c index dacca1aea1..11275c9ec8 100644 --- a/source/libs/tdb/src/db/tdbTable.c +++ b/source/libs/tdb/src/db/tdbTable.c @@ -221,12 +221,12 @@ int tdbTbcOpen(TTB *pTb, TBC **ppTbc, TXN *pTxn) { *ppTbc = NULL; pTbc = (TBC *)tdbOsMalloc(sizeof(*pTbc)); if (pTbc == NULL) { - return -1; + return TSDB_CODE_OUT_OF_MEMORY; } - if (tdbBtcOpen(&pTbc->btc, pTb->pBt, pTxn)) { + if ((ret = tdbBtcOpen(&pTbc->btc, pTb->pBt, pTxn)) != 0) { taosMemoryFree(pTbc); - return -1; + return ret; } *ppTbc = pTbc;