From c54bc76925b01793bca4b0a81b03f1007a862b40 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Mon, 11 Jul 2022 17:36:07 +0800 Subject: [PATCH] feat:get meta ok --- examples/c/tmq.c | 71 +++++++++------ source/client/src/tmq.c | 4 +- source/dnode/mnode/impl/src/mndStb.c | 126 +++++++++++++++------------ 3 files changed, 116 insertions(+), 85 deletions(-) diff --git a/examples/c/tmq.c b/examples/c/tmq.c index c6248a62c1..c55cb2ca58 100644 --- a/examples/c/tmq.c +++ b/examples/c/tmq.c @@ -51,15 +51,14 @@ static void msg_process(TAOS_RES* msg) { int32_t ret = taos_write_raw_meta(pConn, raw); printf("write raw data: %s\n", tmq_err2str(ret)); - free(raw); taos_close(pConn); } + tmq_free_raw_meta(raw); char* result = tmq_get_json_meta(msg); if(result){ printf("meta result: %s\n", result); - free(result); } - printf("meta:%p\n", raw); + tmq_free_json_meta(result); return; } while (1) { @@ -83,7 +82,7 @@ int32_t init_env() { return -1; } - TAOS_RES* pRes = taos_query(pConn, "create database if not exists abc1 vgroups 1"); + TAOS_RES* pRes = taos_query(pConn, "create database if not exists abc1 vgroups 3"); if (taos_errno(pRes) != 0) { printf("error in create db, reason:%s\n", taos_errstr(pRes)); return -1; @@ -126,6 +125,13 @@ int32_t init_env() { } taos_free_result(pRes); + pRes = taos_query(pConn, "create table if not exists ct2 using st1 tags(NULL)"); + if (taos_errno(pRes) != 0) { + printf("failed to create child table ct2, reason:%s\n", taos_errstr(pRes)); + return -1; + } + taos_free_result(pRes); + pRes = taos_query(pConn, "insert into ct1 values(now, 3, 4, 'b')"); if (taos_errno(pRes) != 0) { printf("failed to insert into ct1, reason:%s\n", taos_errstr(pRes)); @@ -147,12 +153,12 @@ int32_t init_env() { } taos_free_result(pRes); -// pRes = taos_query(pConn, "alter table st1 add column c4 bigint"); -// if (taos_errno(pRes) != 0) { -// printf("failed to alter super table st1, reason:%s\n", taos_errstr(pRes)); -// return -1; -// } -// taos_free_result(pRes); + pRes = taos_query(pConn, "alter table st1 add column c4 bigint"); + if (taos_errno(pRes) != 0) { + printf("failed to alter super table st1, reason:%s\n", taos_errstr(pRes)); + return -1; + } + taos_free_result(pRes); pRes = taos_query(pConn, "alter table st1 modify column c3 binary(64)"); if (taos_errno(pRes) != 0) { @@ -175,19 +181,19 @@ int32_t init_env() { } taos_free_result(pRes); -// pRes = taos_query(pConn, "drop table ct3 ct1"); -// if (taos_errno(pRes) != 0) { -// printf("failed to drop child table ct3, reason:%s\n", taos_errstr(pRes)); -// return -1; -// } -// taos_free_result(pRes); -// -// pRes = taos_query(pConn, "drop table st1"); -// if (taos_errno(pRes) != 0) { -// printf("failed to drop super table st1, reason:%s\n", taos_errstr(pRes)); -// return -1; -// } -// taos_free_result(pRes); + pRes = taos_query(pConn, "drop table ct3 ct1"); + if (taos_errno(pRes) != 0) { + printf("failed to drop child table ct3, reason:%s\n", taos_errstr(pRes)); + return -1; + } + taos_free_result(pRes); + + pRes = taos_query(pConn, "drop table st1"); + if (taos_errno(pRes) != 0) { + printf("failed to drop super table st1, reason:%s\n", taos_errstr(pRes)); + return -1; + } + taos_free_result(pRes); pRes = taos_query(pConn, "create table if not exists n1(ts timestamp, c1 int, c2 nchar(4))"); if (taos_errno(pRes) != 0) { @@ -224,12 +230,12 @@ int32_t init_env() { } taos_free_result(pRes); -// pRes = taos_query(pConn, "drop table n1"); -// if (taos_errno(pRes) != 0) { -// printf("failed to drop normal table n1, reason:%s\n", taos_errstr(pRes)); -// return -1; -// } -// taos_free_result(pRes); + pRes = taos_query(pConn, "drop table n1"); + if (taos_errno(pRes) != 0) { + printf("failed to drop normal table n1, reason:%s\n", taos_errstr(pRes)); + return -1; + } + taos_free_result(pRes); pRes = taos_query(pConn, "create table jt(ts timestamp, i int) tags(t json)"); if (taos_errno(pRes) != 0) { @@ -245,6 +251,13 @@ int32_t init_env() { } taos_free_result(pRes); + pRes = taos_query(pConn, "create table jt2 using jt tags('')"); + if (taos_errno(pRes) != 0) { + printf("failed to create super table jt2, reason:%s\n", taos_errstr(pRes)); + return -1; + } + taos_free_result(pRes); + return 0; } diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index 6d24d8d9ff..4dc0dbfa64 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -2430,7 +2430,7 @@ static int32_t taosCreateStb(TAOS *taos, void *meta, int32_t metaLen){ pReq.numOfTags = req.schemaTag.nCols; pReq.commentLen = -1; pReq.suid = req.suid; - pReq.source = 1; + pReq.source = TD_REQ_FROM_TAOX; pReq.igExists = true; SName tableName; @@ -2497,7 +2497,7 @@ static int32_t taosDropStb(TAOS *taos, void *meta, int32_t metaLen){ // build drop stable pReq.igNotExists = true; - pReq.source = 1; + pReq.source = TD_REQ_FROM_TAOX; pReq.suid = req.suid; SName tableName; tNameExtractFullName(toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName), pReq.name); diff --git a/source/dnode/mnode/impl/src/mndStb.c b/source/dnode/mnode/impl/src/mndStb.c index 3a7b137f9f..5040b556e7 100644 --- a/source/dnode/mnode/impl/src/mndStb.c +++ b/source/dnode/mnode/impl/src/mndStb.c @@ -851,6 +851,75 @@ static int32_t mndProcessTtlTimer(SRpcMsg *pReq) { return 0; } +static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) { + for (int32_t tag = 0; tag < pStb->numOfTags; tag++) { + if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) { + return tag; + } + } + + return -1; +} + +static int32_t mndFindSuperTableColumnIndex(const SStbObj *pStb, const char *colName) { + for (int32_t col = 0; col < pStb->numOfColumns; col++) { + if (strcasecmp(pStb->pColumns[col].name, colName) == 0) { + return col; + } + } + + return -1; +} + +static int32_t mndBuildStbFromAlter(SStbObj *pStb, SStbObj *pDst, SMCreateStbReq *createReq) { + taosRLockLatch(&pStb->lock); + memcpy(pDst, pStb, sizeof(SStbObj)); + taosRUnLockLatch(&pStb->lock); + + pDst->updateTime = taosGetTimestampMs(); + pDst->numOfColumns = createReq->numOfColumns; + pDst->numOfTags = createReq->numOfTags; + pDst->pColumns = taosMemoryCalloc(1, pDst->numOfColumns * sizeof(SSchema)); + pDst->pTags = taosMemoryCalloc(1, pDst->numOfTags * sizeof(SSchema)); + if (pDst->pColumns == NULL || pDst->pTags == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + for (int32_t i = 0; i < pDst->numOfColumns; ++i) { + SField *pField = taosArrayGet(createReq->pColumns, i); + SSchema *pSchema = &pDst->pColumns[i]; + pSchema->type = pField->type; + pSchema->bytes = pField->bytes; + pSchema->flags = pField->flags; + memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN); + int32_t cIndex = mndFindSuperTableColumnIndex(pStb, pField->name); + if (cIndex >= 0){ + pSchema->colId = pStb->pColumns[cIndex].colId; + }else{ + pSchema->colId = pDst->nextColId++; + } + } + + for (int32_t i = 0; i < pDst->numOfTags; ++i) { + SField *pField = taosArrayGet(createReq->pTags, i); + SSchema *pSchema = &pDst->pTags[i]; + pSchema->type = pField->type; + pSchema->bytes = pField->bytes; + memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN); + int32_t cIndex = mndFindSuperTableTagIndex(pStb, pField->name); + if (cIndex >= 0){ + pSchema->colId = pStb->pTags[cIndex].colId; + }else{ + pSchema->colId = pDst->nextColId++; + } + + } + pDst->tagVer = createReq->tagVer; + pDst->colVer = createReq->colVer; + return TSDB_CODE_SUCCESS; +} + static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) { SMnode *pMnode = pReq->info.node; int32_t code = -1; @@ -937,43 +1006,12 @@ static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) { if (isAlter) { bool needRsp = false; SStbObj pDst = {0}; - taosRLockLatch(&pStb->lock); - memcpy(&pDst, pStb, sizeof(SStbObj)); - taosRUnLockLatch(&pStb->lock); - - pDst.updateTime = taosGetTimestampMs(); - pDst.nextColId = 1; - pDst.numOfColumns = createReq.numOfColumns; - pDst.numOfTags = createReq.numOfTags; - pDst.pColumns = taosMemoryCalloc(1, pDst.numOfColumns * sizeof(SSchema)); - pDst.pTags = taosMemoryCalloc(1, pDst.numOfTags * sizeof(SSchema)); - if (pDst.pColumns == NULL || pDst.pTags == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; + if (mndBuildStbFromAlter(pStb, &pDst, &createReq) != 0) { + taosMemoryFreeClear(pDst.pTags); + taosMemoryFreeClear(pDst.pColumns); goto _OVER; } - for (int32_t i = 0; i < pDst.numOfColumns; ++i) { - SField *pField = taosArrayGet(createReq.pColumns, i); - SSchema *pSchema = &pDst.pColumns[i]; - pSchema->type = pField->type; - pSchema->bytes = pField->bytes; - pSchema->flags = pField->flags; - memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN); - pSchema->colId = pDst.nextColId; - pDst.nextColId++; - } - - for (int32_t i = 0; i < pDst.numOfTags; ++i) { - SField *pField = taosArrayGet(createReq.pTags, i); - SSchema *pSchema = &pDst.pTags[i]; - pSchema->type = pField->type; - pSchema->bytes = pField->bytes; - memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN); - pSchema->colId = pDst.nextColId; - pDst.nextColId++; - } - pDst.tagVer = createReq.tagVer; - pDst.colVer = createReq.colVer; code = mndAlterStbImp(pMnode, pReq, pDb, &pDst, needRsp, NULL, 0); taosMemoryFreeClear(pDst.pTags); taosMemoryFreeClear(pDst.pColumns); @@ -1014,26 +1052,6 @@ static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) { return 0; } -static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) { - for (int32_t tag = 0; tag < pStb->numOfTags; tag++) { - if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) { - return tag; - } - } - - return -1; -} - -static int32_t mndFindSuperTableColumnIndex(const SStbObj *pStb, const char *colName) { - for (int32_t col = 0; col < pStb->numOfColumns; col++) { - if (strcasecmp(pStb->pColumns[col].name, colName) == 0) { - return col; - } - } - - return -1; -} - static int32_t mndAllocStbSchemas(const SStbObj *pOld, SStbObj *pNew) { pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema)); pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema)); -- GitLab