tmsg.c 264.1 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
common  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
H
Hongze Cheng 已提交
17
#include "tmsg.h"
D
dapan1121 已提交
18

H
Hongze Cheng 已提交
19 20 21 22 23
#undef TD_MSG_NUMBER_
#undef TD_MSG_DICT_
#define TD_MSG_INFO_
#undef TD_MSG_SEG_CODE_
#include "tmsgdef.h"
D
dapan1121 已提交
24

H
Hongze Cheng 已提交
25 26 27 28
#undef TD_MSG_NUMBER_
#undef TD_MSG_INFO_
#define TD_MSG_DICT_
#undef TD_MSG_SEG_CODE_
H
Hongze Cheng 已提交
29
#include "tmsgdef.h"
C
Cary Xu 已提交
30

S
Shengliang Guan 已提交
31 32
#include "tlog.h"

33 34 35
static int32_t tDecodeSVAlterTbReqCommon(SDecoder *pDecoder, SVAlterTbReq *pReq);
static int32_t tDecodeSBatchDeleteReqCommon(SDecoder *pDecoder, SBatchDeleteReq *pReq);

L
Liu Jicong 已提交
36
int32_t tInitSubmitMsgIter(const SSubmitReq *pMsg, SSubmitMsgIter *pIter) {
C
Cary Xu 已提交
37 38 39 40 41 42
  if (pMsg == NULL) {
    terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP;
    return -1;
  }

  pIter->totalLen = htonl(pMsg->length);
C
Cary Xu 已提交
43
  pIter->numOfBlocks = htonl(pMsg->numOfBlocks);
44
  ASSERT(pIter->totalLen > 0);
C
Cary Xu 已提交
45 46 47 48 49 50 51 52 53 54
  pIter->len = 0;
  pIter->pMsg = pMsg;
  if (pIter->totalLen <= sizeof(SSubmitReq)) {
    terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP;
    return -1;
  }

  return 0;
}

C
Cary Xu 已提交
55
int32_t tGetSubmitMsgNext(SSubmitMsgIter *pIter, SSubmitBlk **pPBlock) {
56
  ASSERT(pIter->len >= 0);
C
Cary Xu 已提交
57 58 59 60 61

  if (pIter->len == 0) {
    pIter->len += sizeof(SSubmitReq);
  } else {
    if (pIter->len >= pIter->totalLen) {
62
      ASSERT(0);
C
Cary Xu 已提交
63 64 65
    }

    pIter->len += (sizeof(SSubmitBlk) + pIter->dataLen + pIter->schemaLen);
66
    ASSERT(pIter->len > 0);
C
Cary Xu 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  }

  if (pIter->len > pIter->totalLen) {
    terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP;
    *pPBlock = NULL;
    return -1;
  }

  if (pIter->len == pIter->totalLen) {
    *pPBlock = NULL;
  } else {
    *pPBlock = (SSubmitBlk *)POINTER_SHIFT(pIter->pMsg, pIter->len);
    pIter->uid = htobe64((*pPBlock)->uid);
    pIter->suid = htobe64((*pPBlock)->suid);
    pIter->sversion = htonl((*pPBlock)->sversion);
    pIter->dataLen = htonl((*pPBlock)->dataLen);
    pIter->schemaLen = htonl((*pPBlock)->schemaLen);
84
    pIter->numOfRows = htonl((*pPBlock)->numOfRows);
C
Cary Xu 已提交
85 86 87 88
  }
  return 0;
}

C
Cary Xu 已提交
89
int32_t tInitSubmitBlkIter(SSubmitMsgIter *pMsgIter, SSubmitBlk *pBlock, SSubmitBlkIter *pIter) {
C
Cary Xu 已提交
90 91 92 93 94 95 96
  if (pMsgIter->dataLen <= 0) return -1;
  pIter->totalLen = pMsgIter->dataLen;
  pIter->len = 0;
  pIter->row = (STSRow *)(pBlock->data + pMsgIter->schemaLen);
  return 0;
}

C
Cary Xu 已提交
97
STSRow *tGetSubmitBlkNext(SSubmitBlkIter *pIter) {
C
Cary Xu 已提交
98 99 100 101 102 103 104 105 106 107 108 109
  STSRow *row = pIter->row;

  if (pIter->len >= pIter->totalLen) {
    return NULL;
  } else {
    pIter->len += TD_ROW_LEN(row);
    if (pIter->len < pIter->totalLen) {
      pIter->row = POINTER_SHIFT(row, TD_ROW_LEN(row));
    }
    return row;
  }
}
C
Cary Xu 已提交
110

H
Hongze Cheng 已提交
111
int32_t tPrintFixedSchemaSubmitReq(SSubmitReq *pReq, STSchema *pTschema) {
L
Liu Jicong 已提交
112
  SSubmitMsgIter msgIter = {0};
L
Liu Jicong 已提交
113
  if (tInitSubmitMsgIter(pReq, &msgIter) < 0) return -1;
L
Liu Jicong 已提交
114 115
  while (true) {
    SSubmitBlk *pBlock = NULL;
L
Liu Jicong 已提交
116
    if (tGetSubmitMsgNext(&msgIter, &pBlock) < 0) return -1;
L
Liu Jicong 已提交
117 118
    if (pBlock == NULL) break;
    SSubmitBlkIter blkIter = {0};
L
Liu Jicong 已提交
119
    tInitSubmitBlkIter(&msgIter, pBlock, &blkIter);
L
Liu Jicong 已提交
120 121 122
    STSRowIter rowIter = {0};
    tdSTSRowIterInit(&rowIter, pTschema);
    STSRow *row;
L
Liu Jicong 已提交
123
    while ((row = tGetSubmitBlkNext(&blkIter)) != NULL) {
L
Liu Jicong 已提交
124 125 126 127 128 129
      tdSRowPrint(row, pTschema, "stream");
    }
  }
  return 0;
}

H
Hongze Cheng 已提交
130
int32_t tEncodeSEpSet(SEncoder *pEncoder, const SEpSet *pEp) {
S
Shengliang Guan 已提交
131 132 133 134
  if (tEncodeI8(pEncoder, pEp->inUse) < 0) return -1;
  if (tEncodeI8(pEncoder, pEp->numOfEps) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; i++) {
    if (tEncodeU16(pEncoder, pEp->eps[i].port) < 0) return -1;
135
    if (tEncodeCStrWithLen(pEncoder, pEp->eps[i].fqdn, TSDB_FQDN_LEN) < 0) return -1;
S
Shengliang Guan 已提交
136 137 138 139
  }
  return 0;
}

H
Hongze Cheng 已提交
140
int32_t tDecodeSEpSet(SDecoder *pDecoder, SEpSet *pEp) {
S
Shengliang Guan 已提交
141 142 143 144 145 146 147 148 149
  if (tDecodeI8(pDecoder, &pEp->inUse) < 0) return -1;
  if (tDecodeI8(pDecoder, &pEp->numOfEps) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; i++) {
    if (tDecodeU16(pDecoder, &pEp->eps[i].port) < 0) return -1;
    if (tDecodeCStrTo(pDecoder, pEp->eps[i].fqdn) < 0) return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
150
int32_t tEncodeSQueryNodeAddr(SEncoder *pEncoder, SQueryNodeAddr *pAddr) {
D
dapan1121 已提交
151 152 153 154 155
  if (tEncodeI32(pEncoder, pAddr->nodeId) < 0) return -1;
  if (tEncodeSEpSet(pEncoder, &pAddr->epSet) < 0) return -1;
  return 0;
}

D
dapan1121 已提交
156 157 158 159 160 161
int32_t tEncodeSQueryNodeLoad(SEncoder *pEncoder, SQueryNodeLoad *pLoad) {
  if (tEncodeSQueryNodeAddr(pEncoder, &pLoad->addr) < 0) return -1;
  if (tEncodeU64(pEncoder, pLoad->load) < 0) return -1;
  return 0;
}

H
Hongze Cheng 已提交
162
int32_t tDecodeSQueryNodeAddr(SDecoder *pDecoder, SQueryNodeAddr *pAddr) {
D
dapan1121 已提交
163 164 165 166 167
  if (tDecodeI32(pDecoder, &pAddr->nodeId) < 0) return -1;
  if (tDecodeSEpSet(pDecoder, &pAddr->epSet) < 0) return -1;
  return 0;
}

D
dapan1121 已提交
168 169 170 171 172 173
int32_t tDecodeSQueryNodeLoad(SDecoder *pDecoder, SQueryNodeLoad *pLoad) {
  if (tDecodeSQueryNodeAddr(pDecoder, &pLoad->addr) < 0) return -1;
  if (tDecodeU64(pDecoder, &pLoad->load) < 0) return -1;
  return 0;
}

S
Shengliang Guan 已提交
174 175 176 177 178 179 180 181 182 183 184
int32_t taosEncodeSEpSet(void **buf, const SEpSet *pEp) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI8(buf, pEp->inUse);
  tlen += taosEncodeFixedI8(buf, pEp->numOfEps);
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; i++) {
    tlen += taosEncodeFixedU16(buf, pEp->eps[i].port);
    tlen += taosEncodeString(buf, pEp->eps[i].fqdn);
  }
  return tlen;
}

185
void *taosDecodeSEpSet(const void *buf, SEpSet *pEp) {
S
Shengliang Guan 已提交
186 187 188 189 190 191
  buf = taosDecodeFixedI8(buf, &pEp->inUse);
  buf = taosDecodeFixedI8(buf, &pEp->numOfEps);
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; i++) {
    buf = taosDecodeFixedU16(buf, &pEp->eps[i].port);
    buf = taosDecodeStringTo(buf, pEp->eps[i].fqdn);
  }
192
  return (void *)buf;
S
Shengliang Guan 已提交
193 194
}

H
Hongze Cheng 已提交
195
static int32_t tSerializeSClientHbReq(SEncoder *pEncoder, const SClientHbReq *pReq) {
S
Shengliang Guan 已提交
196
  if (tEncodeSClientHbKey(pEncoder, &pReq->connKey) < 0) return -1;
L
Liu Jicong 已提交
197

D
dapan1121 已提交
198
  if (pReq->connKey.connType == CONN_TYPE__QUERY) {
D
dapan1121 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211
    if (tEncodeI64(pEncoder, pReq->app.appId) < 0) return -1;
    if (tEncodeI32(pEncoder, pReq->app.pid) < 0) return -1;
    if (tEncodeCStr(pEncoder, pReq->app.name) < 0) return -1;
    if (tEncodeI64(pEncoder, pReq->app.startTime) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.numOfInsertsReq) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.numOfInsertRows) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.insertElapsedTime) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.insertBytes) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.fetchBytes) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.queryElapsedTime) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.numOfSlowQueries) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.totalRequests) < 0) return -1;
    if (tEncodeU64(pEncoder, pReq->app.summary.currentRequests) < 0) return -1;
X
Xiaoyu Wang 已提交
212

D
dapan1121 已提交
213 214 215 216 217
    int32_t queryNum = 0;
    if (pReq->query) {
      queryNum = 1;
      if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
      if (tEncodeU32(pEncoder, pReq->query->connId) < 0) return -1;
218

D
dapan1121 已提交
219 220
      int32_t num = taosArrayGetSize(pReq->query->queryDesc);
      if (tEncodeI32(pEncoder, num) < 0) return -1;
221

D
dapan1121 已提交
222 223 224 225 226 227 228
      for (int32_t i = 0; i < num; ++i) {
        SQueryDesc *desc = taosArrayGet(pReq->query->queryDesc, i);
        if (tEncodeCStr(pEncoder, desc->sql) < 0) return -1;
        if (tEncodeU64(pEncoder, desc->queryId) < 0) return -1;
        if (tEncodeI64(pEncoder, desc->useconds) < 0) return -1;
        if (tEncodeI64(pEncoder, desc->stime) < 0) return -1;
        if (tEncodeI64(pEncoder, desc->reqRid) < 0) return -1;
229
        if (tEncodeI8(pEncoder, desc->stableQuery) < 0) return -1;
230
        if (tEncodeI8(pEncoder, desc->isSubQuery) < 0) return -1;
D
dapan1121 已提交
231 232 233 234 235 236 237 238
        if (tEncodeCStr(pEncoder, desc->fqdn) < 0) return -1;
        if (tEncodeI32(pEncoder, desc->subPlanNum) < 0) return -1;

        int32_t snum = desc->subDesc ? taosArrayGetSize(desc->subDesc) : 0;
        if (tEncodeI32(pEncoder, snum) < 0) return -1;
        for (int32_t m = 0; m < snum; ++m) {
          SQuerySubDesc *sDesc = taosArrayGet(desc->subDesc, m);
          if (tEncodeI64(pEncoder, sDesc->tid) < 0) return -1;
239
          if (tEncodeCStr(pEncoder, sDesc->status) < 0) return -1;
D
dapan1121 已提交
240 241 242 243 244 245
        }
      }
    } else {
      if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
    }
  }
246

L
Liu Jicong 已提交
247
  int32_t kvNum = taosHashGetSize(pReq->info);
S
Shengliang Guan 已提交
248
  if (tEncodeI32(pEncoder, kvNum) < 0) return -1;
S
Shengliang Guan 已提交
249
  void *pIter = taosHashIterate(pReq->info, NULL);
L
Liu Jicong 已提交
250
  while (pIter != NULL) {
S
Shengliang Guan 已提交
251 252
    SKv *kv = pIter;
    if (tEncodeSKv(pEncoder, kv) < 0) return -1;
L
Liu Jicong 已提交
253
    pIter = taosHashIterate(pReq->info, pIter);
L
Liu Jicong 已提交
254
  }
S
Shengliang Guan 已提交
255 256

  return 0;
L
Liu Jicong 已提交
257 258
}

H
Hongze Cheng 已提交
259
static int32_t tDeserializeSClientHbReq(SDecoder *pDecoder, SClientHbReq *pReq) {
S
Shengliang Guan 已提交
260
  if (tDecodeSClientHbKey(pDecoder, &pReq->connKey) < 0) return -1;
L
Liu Jicong 已提交
261

D
dapan1121 已提交
262
  if (pReq->connKey.connType == CONN_TYPE__QUERY) {
D
dapan1121 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276
    if (tDecodeI64(pDecoder, &pReq->app.appId) < 0) return -1;
    if (tDecodeI32(pDecoder, &pReq->app.pid) < 0) return -1;
    if (tDecodeCStrTo(pDecoder, pReq->app.name) < 0) return -1;
    if (tDecodeI64(pDecoder, &pReq->app.startTime) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.numOfInsertsReq) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.numOfInsertRows) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.insertElapsedTime) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.insertBytes) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.fetchBytes) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.queryElapsedTime) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.numOfSlowQueries) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.totalRequests) < 0) return -1;
    if (tDecodeU64(pDecoder, &pReq->app.summary.currentRequests) < 0) return -1;

D
dapan1121 已提交
277 278 279 280 281 282 283 284 285 286 287 288
    int32_t queryNum = 0;
    if (tDecodeI32(pDecoder, &queryNum) < 0) return -1;
    if (queryNum) {
      pReq->query = taosMemoryCalloc(1, sizeof(*pReq->query));
      if (NULL == pReq->query) return -1;
      if (tDecodeU32(pDecoder, &pReq->query->connId) < 0) return -1;

      int32_t num = 0;
      if (tDecodeI32(pDecoder, &num) < 0) return -1;
      if (num > 0) {
        pReq->query->queryDesc = taosArrayInit(num, sizeof(SQueryDesc));
        if (NULL == pReq->query->queryDesc) return -1;
289

D
dapan1121 已提交
290 291 292 293 294 295 296
        for (int32_t i = 0; i < num; ++i) {
          SQueryDesc desc = {0};
          if (tDecodeCStrTo(pDecoder, desc.sql) < 0) return -1;
          if (tDecodeU64(pDecoder, &desc.queryId) < 0) return -1;
          if (tDecodeI64(pDecoder, &desc.useconds) < 0) return -1;
          if (tDecodeI64(pDecoder, &desc.stime) < 0) return -1;
          if (tDecodeI64(pDecoder, &desc.reqRid) < 0) return -1;
X
Xiaoyu Wang 已提交
297
          if (tDecodeI8(pDecoder, (int8_t *)&desc.stableQuery) < 0) return -1;
298
          if (tDecodeI8(pDecoder, (int8_t *)&desc.isSubQuery) < 0) return -1;
D
dapan1121 已提交
299 300 301 302 303 304 305 306
          if (tDecodeCStrTo(pDecoder, desc.fqdn) < 0) return -1;
          if (tDecodeI32(pDecoder, &desc.subPlanNum) < 0) return -1;

          int32_t snum = 0;
          if (tDecodeI32(pDecoder, &snum) < 0) return -1;
          if (snum > 0) {
            desc.subDesc = taosArrayInit(snum, sizeof(SQuerySubDesc));
            if (NULL == desc.subDesc) return -1;
307

D
dapan1121 已提交
308 309 310
            for (int32_t m = 0; m < snum; ++m) {
              SQuerySubDesc sDesc = {0};
              if (tDecodeI64(pDecoder, &sDesc.tid) < 0) return -1;
311
              if (tDecodeCStrTo(pDecoder, sDesc.status) < 0) return -1;
D
dapan1121 已提交
312 313 314
              taosArrayPush(desc.subDesc, &sDesc);
            }
          }
315

316
          ASSERT(desc.subPlanNum == taosArrayGetSize(desc.subDesc));
D
dapan1121 已提交
317 318 319 320 321 322 323

          taosArrayPush(pReq->query->queryDesc, &desc);
        }
      }
    }
  }

S
Shengliang Guan 已提交
324 325
  int32_t kvNum = 0;
  if (tDecodeI32(pDecoder, &kvNum) < 0) return -1;
L
Liu Jicong 已提交
326 327 328
  if (pReq->info == NULL) {
    pReq->info = taosHashInit(kvNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
  }
S
Shengliang Guan 已提交
329
  if (pReq->info == NULL) return -1;
S
Shengliang Guan 已提交
330
  for (int32_t i = 0; i < kvNum; i++) {
S
Shengliang Guan 已提交
331 332
    SKv kv = {0};
    if (tDecodeSKv(pDecoder, &kv) < 0) return -1;
D
dapan1121 已提交
333
    taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
L
Liu Jicong 已提交
334 335
  }

S
Shengliang Guan 已提交
336
  return 0;
L
Liu Jicong 已提交
337 338
}

H
Hongze Cheng 已提交
339
static int32_t tSerializeSClientHbRsp(SEncoder *pEncoder, const SClientHbRsp *pRsp) {
S
Shengliang Guan 已提交
340 341 342
  if (tEncodeSClientHbKey(pEncoder, &pRsp->connKey) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->status) < 0) return -1;

D
dapan1121 已提交
343 344 345
  int32_t queryNum = 0;
  if (pRsp->query) {
    queryNum = 1;
346
    if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
D
dapan1121 已提交
347 348 349 350 351 352
    if (tEncodeU32(pEncoder, pRsp->query->connId) < 0) return -1;
    if (tEncodeU64(pEncoder, pRsp->query->killRid) < 0) return -1;
    if (tEncodeI32(pEncoder, pRsp->query->totalDnodes) < 0) return -1;
    if (tEncodeI32(pEncoder, pRsp->query->onlineDnodes) < 0) return -1;
    if (tEncodeI8(pEncoder, pRsp->query->killConnection) < 0) return -1;
    if (tEncodeSEpSet(pEncoder, &pRsp->query->epSet) < 0) return -1;
D
dapan1121 已提交
353 354 355 356 357 358
    int32_t num = taosArrayGetSize(pRsp->query->pQnodeList);
    if (tEncodeI32(pEncoder, num) < 0) return -1;
    for (int32_t i = 0; i < num; ++i) {
      SQueryNodeLoad *pLoad = taosArrayGet(pRsp->query->pQnodeList, i);
      if (tEncodeSQueryNodeLoad(pEncoder, pLoad) < 0) return -1;
    }
D
dapan1121 已提交
359
  } else {
360
    if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
D
dapan1121 已提交
361
  }
362

D
dapan1121 已提交
363
  int32_t kvNum = taosArrayGetSize(pRsp->info);
S
Shengliang Guan 已提交
364
  if (tEncodeI32(pEncoder, kvNum) < 0) return -1;
S
Shengliang Guan 已提交
365
  for (int32_t i = 0; i < kvNum; i++) {
S
Shengliang Guan 已提交
366 367
    SKv *kv = taosArrayGet(pRsp->info, i);
    if (tEncodeSKv(pEncoder, kv) < 0) return -1;
D
dapan1121 已提交
368
  }
S
Shengliang Guan 已提交
369 370

  return 0;
L
Liu Jicong 已提交
371
}
S
Shengliang Guan 已提交
372

H
Hongze Cheng 已提交
373
static int32_t tDeserializeSClientHbRsp(SDecoder *pDecoder, SClientHbRsp *pRsp) {
S
Shengliang Guan 已提交
374 375 376
  if (tDecodeSClientHbKey(pDecoder, &pRsp->connKey) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->status) < 0) return -1;

D
dapan1121 已提交
377 378 379 380 381 382 383 384 385 386 387
  int32_t queryNum = 0;
  if (tDecodeI32(pDecoder, &queryNum) < 0) return -1;
  if (queryNum) {
    pRsp->query = taosMemoryCalloc(1, sizeof(*pRsp->query));
    if (NULL == pRsp->query) return -1;
    if (tDecodeU32(pDecoder, &pRsp->query->connId) < 0) return -1;
    if (tDecodeU64(pDecoder, &pRsp->query->killRid) < 0) return -1;
    if (tDecodeI32(pDecoder, &pRsp->query->totalDnodes) < 0) return -1;
    if (tDecodeI32(pDecoder, &pRsp->query->onlineDnodes) < 0) return -1;
    if (tDecodeI8(pDecoder, &pRsp->query->killConnection) < 0) return -1;
    if (tDecodeSEpSet(pDecoder, &pRsp->query->epSet) < 0) return -1;
D
dapan1121 已提交
388 389 390 391 392
    int32_t pQnodeNum = 0;
    if (tDecodeI32(pDecoder, &pQnodeNum) < 0) return -1;
    if (pQnodeNum > 0) {
      pRsp->query->pQnodeList = taosArrayInit(pQnodeNum, sizeof(SQueryNodeLoad));
      if (NULL == pRsp->query->pQnodeList) return -1;
D
dapan1121 已提交
393 394 395 396 397
      for (int32_t i = 0; i < pQnodeNum; ++i) {
        SQueryNodeLoad load = {0};
        if (tDecodeSQueryNodeLoad(pDecoder, &load) < 0) return -1;
        taosArrayPush(pRsp->query->pQnodeList, &load);
      }
D
dapan1121 已提交
398
    }
D
dapan1121 已提交
399 400
  }

D
dapan1121 已提交
401
  int32_t kvNum = 0;
S
Shengliang Guan 已提交
402
  if (tDecodeI32(pDecoder, &kvNum) < 0) return -1;
D
dapan1121 已提交
403
  pRsp->info = taosArrayInit(kvNum, sizeof(SKv));
S
Shengliang Guan 已提交
404
  if (pRsp->info == NULL) return -1;
S
Shengliang Guan 已提交
405
  for (int32_t i = 0; i < kvNum; i++) {
D
dapan1121 已提交
406
    SKv kv = {0};
S
Shengliang Guan 已提交
407
    tDecodeSKv(pDecoder, &kv);
D
dapan1121 已提交
408 409
    taosArrayPush(pRsp->info, &kv);
  }
S
Shengliang Guan 已提交
410

S
Shengliang Guan 已提交
411
  return 0;
L
Liu Jicong 已提交
412 413
}

S
Shengliang Guan 已提交
414
int32_t tSerializeSClientHbBatchReq(void *buf, int32_t bufLen, const SClientHbBatchReq *pBatchReq) {
H
Hongze Cheng 已提交
415 416
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
417 418 419 420

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pBatchReq->reqId) < 0) return -1;

L
Liu Jicong 已提交
421
  int32_t reqNum = taosArrayGetSize(pBatchReq->reqs);
S
Shengliang Guan 已提交
422
  if (tEncodeI32(&encoder, reqNum) < 0) return -1;
S
Shengliang Guan 已提交
423 424
  for (int32_t i = 0; i < reqNum; i++) {
    SClientHbReq *pReq = taosArrayGet(pBatchReq->reqs, i);
S
Shengliang Guan 已提交
425
    if (tSerializeSClientHbReq(&encoder, pReq) < 0) return -1;
L
Liu Jicong 已提交
426
  }
S
Shengliang Guan 已提交
427 428 429
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
430
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
431 432 433
  return tlen;
}

S
Shengliang Guan 已提交
434
int32_t tDeserializeSClientHbBatchReq(void *buf, int32_t bufLen, SClientHbBatchReq *pBatchReq) {
H
Hongze Cheng 已提交
435 436
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
437 438 439 440 441 442

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pBatchReq->reqId) < 0) return -1;

  int32_t reqNum = 0;
  if (tDecodeI32(&decoder, &reqNum) < 0) return -1;
D
dapan1121 已提交
443
  if (reqNum > 0) {
S
Shengliang Guan 已提交
444
    pBatchReq->reqs = taosArrayInit(reqNum, sizeof(SClientHbReq));
D
dapan1121 已提交
445
    if (NULL == pBatchReq->reqs) return -1;
L
Liu Jicong 已提交
446
  }
S
Shengliang Guan 已提交
447
  for (int32_t i = 0; i < reqNum; i++) {
L
Liu Jicong 已提交
448
    SClientHbReq req = {0};
S
Shengliang Guan 已提交
449
    tDeserializeSClientHbReq(&decoder, &req);
L
Liu Jicong 已提交
450 451
    taosArrayPush(pBatchReq->reqs, &req);
  }
S
Shengliang Guan 已提交
452 453

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
454
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
455
  return 0;
L
Liu Jicong 已提交
456 457
}

S
Shengliang Guan 已提交
458
int32_t tSerializeSClientHbBatchRsp(void *buf, int32_t bufLen, const SClientHbBatchRsp *pBatchRsp) {
H
Hongze Cheng 已提交
459 460
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
461 462 463 464

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pBatchRsp->reqId) < 0) return -1;
  if (tEncodeI64(&encoder, pBatchRsp->rspId) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
465
  if (tEncodeI32(&encoder, pBatchRsp->svrTimestamp) < 0) return -1;
S
Shengliang Guan 已提交
466 467 468 469

  int32_t rspNum = taosArrayGetSize(pBatchRsp->rsps);
  if (tEncodeI32(&encoder, rspNum) < 0) return -1;
  for (int32_t i = 0; i < rspNum; i++) {
S
Shengliang Guan 已提交
470
    SClientHbRsp *pRsp = taosArrayGet(pBatchRsp->rsps, i);
S
Shengliang Guan 已提交
471
    if (tSerializeSClientHbRsp(&encoder, pRsp) < 0) return -1;
L
Liu Jicong 已提交
472
  }
S
Shengliang Guan 已提交
473 474 475
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
476
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
477 478 479
  return tlen;
}

S
Shengliang Guan 已提交
480
int32_t tDeserializeSClientHbBatchRsp(void *buf, int32_t bufLen, SClientHbBatchRsp *pBatchRsp) {
H
Hongze Cheng 已提交
481 482
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
483 484 485 486

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pBatchRsp->reqId) < 0) return -1;
  if (tDecodeI64(&decoder, &pBatchRsp->rspId) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
487
  if (tDecodeI32(&decoder, &pBatchRsp->svrTimestamp) < 0) return -1;
S
Shengliang Guan 已提交
488 489 490 491

  int32_t rspNum = 0;
  if (tDecodeI32(&decoder, &rspNum) < 0) return -1;
  if (pBatchRsp->rsps == NULL) {
L
Liu Jicong 已提交
492
    pBatchRsp->rsps = taosArrayInit(rspNum, sizeof(SClientHbRsp));
S
Shengliang Guan 已提交
493 494
  }
  for (int32_t i = 0; i < rspNum; i++) {
L
Liu Jicong 已提交
495
    SClientHbRsp rsp = {0};
S
Shengliang Guan 已提交
496
    tDeserializeSClientHbRsp(&decoder, &rsp);
L
Liu Jicong 已提交
497 498
    taosArrayPush(pBatchRsp->rsps, &rsp);
  }
S
Shengliang Guan 已提交
499 500

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
501
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
502
  return 0;
L
Liu Jicong 已提交
503 504
}

S
Shengliang Guan 已提交
505
int32_t tSerializeSMCreateStbReq(void *buf, int32_t bufLen, SMCreateStbReq *pReq) {
H
Hongze Cheng 已提交
506 507
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
508

S
Shengliang Guan 已提交
509 510 511
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
512 513 514 515 516
  if (tEncodeI8(&encoder, pReq->source) < 0) return -1;
  for (int32_t i = 0; i < sizeof(pReq->reserved) / sizeof(int8_t); ++i) {
    if (tEncodeI8(&encoder, pReq->reserved[i]) < 0) return -1;
  }
  if (tEncodeI64(&encoder, pReq->suid) < 0) return -1;
X
Xiaoyu Wang 已提交
517 518 519 520
  if (tEncodeI64(&encoder, pReq->delay1) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->delay2) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->watermark1) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->watermark2) < 0) return -1;
S
sma  
Shengliang Guan 已提交
521
  if (tEncodeI32(&encoder, pReq->ttl) < 0) return -1;
522 523
  if (tEncodeI32(&encoder, pReq->colVer) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->tagVer) < 0) return -1;
S
Shengliang Guan 已提交
524 525
  if (tEncodeI32(&encoder, pReq->numOfColumns) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfTags) < 0) return -1;
S
Shengliang Guan 已提交
526
  if (tEncodeI32(&encoder, pReq->numOfFuncs) < 0) return -1;
S
sma  
Shengliang Guan 已提交
527
  if (tEncodeI32(&encoder, pReq->commentLen) < 0) return -1;
528 529
  if (tEncodeI32(&encoder, pReq->ast1Len) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->ast2Len) < 0) return -1;
S
Shengliang Guan 已提交
530 531 532

  for (int32_t i = 0; i < pReq->numOfColumns; ++i) {
    SField *pField = taosArrayGet(pReq->pColumns, i);
S
Shengliang Guan 已提交
533
    if (tEncodeI8(&encoder, pField->type) < 0) return -1;
S
Shengliang Guan 已提交
534
    if (tEncodeI8(&encoder, pField->flags) < 0) return -1;
S
Shengliang Guan 已提交
535 536
    if (tEncodeI32(&encoder, pField->bytes) < 0) return -1;
    if (tEncodeCStr(&encoder, pField->name) < 0) return -1;
S
Shengliang Guan 已提交
537 538 539 540
  }

  for (int32_t i = 0; i < pReq->numOfTags; ++i) {
    SField *pField = taosArrayGet(pReq->pTags, i);
S
Shengliang Guan 已提交
541
    if (tEncodeI8(&encoder, pField->type) < 0) return -1;
S
Shengliang Guan 已提交
542
    if (tEncodeI8(&encoder, pField->flags) < 0) return -1;
S
Shengliang Guan 已提交
543 544
    if (tEncodeI32(&encoder, pField->bytes) < 0) return -1;
    if (tEncodeCStr(&encoder, pField->name) < 0) return -1;
S
Shengliang Guan 已提交
545 546 547 548 549
  }

  for (int32_t i = 0; i < pReq->numOfFuncs; ++i) {
    const char *pFunc = taosArrayGet(pReq->pFuncs, i);
    if (tEncodeCStr(&encoder, pFunc) < 0) return -1;
S
sma  
Shengliang Guan 已提交
550 551 552
  }

  if (pReq->commentLen > 0) {
S
Shengliang Guan 已提交
553
    if (tEncodeCStr(&encoder, pReq->pComment) < 0) return -1;
S
sma  
Shengliang Guan 已提交
554
  }
555 556 557 558 559 560
  if (pReq->ast1Len > 0) {
    if (tEncodeBinary(&encoder, pReq->pAst1, pReq->ast1Len) < 0) return -1;
  }
  if (pReq->ast2Len > 0) {
    if (tEncodeBinary(&encoder, pReq->pAst2, pReq->ast2Len) < 0) return -1;
  }
561 562
  if (tEncodeI64(&encoder, pReq->deleteMark1) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->deleteMark2) < 0) return -1;
D
dapan1121 已提交
563

S
Shengliang Guan 已提交
564 565 566
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
567
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
568 569 570
  return tlen;
}

S
Shengliang Guan 已提交
571
int32_t tDeserializeSMCreateStbReq(void *buf, int32_t bufLen, SMCreateStbReq *pReq) {
H
Hongze Cheng 已提交
572 573
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
574 575 576 577

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
578 579 580 581 582
  if (tDecodeI8(&decoder, &pReq->source) < 0) return -1;
  for (int32_t i = 0; i < sizeof(pReq->reserved) / sizeof(int8_t); ++i) {
    if (tDecodeI8(&decoder, &pReq->reserved[i]) < 0) return -1;
  }
  if (tDecodeI64(&decoder, &pReq->suid) < 0) return -1;
X
Xiaoyu Wang 已提交
583 584 585 586
  if (tDecodeI64(&decoder, &pReq->delay1) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->delay2) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->watermark1) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->watermark2) < 0) return -1;
S
sma  
Shengliang Guan 已提交
587
  if (tDecodeI32(&decoder, &pReq->ttl) < 0) return -1;
588 589
  if (tDecodeI32(&decoder, &pReq->colVer) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->tagVer) < 0) return -1;
S
Shengliang Guan 已提交
590 591
  if (tDecodeI32(&decoder, &pReq->numOfColumns) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfTags) < 0) return -1;
S
Shengliang Guan 已提交
592
  if (tDecodeI32(&decoder, &pReq->numOfFuncs) < 0) return -1;
S
sma  
Shengliang Guan 已提交
593
  if (tDecodeI32(&decoder, &pReq->commentLen) < 0) return -1;
594 595
  if (tDecodeI32(&decoder, &pReq->ast1Len) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->ast2Len) < 0) return -1;
S
Shengliang Guan 已提交
596 597 598

  pReq->pColumns = taosArrayInit(pReq->numOfColumns, sizeof(SField));
  pReq->pTags = taosArrayInit(pReq->numOfTags, sizeof(SField));
S
Shengliang Guan 已提交
599 600
  pReq->pFuncs = taosArrayInit(pReq->numOfFuncs, TSDB_FUNC_NAME_LEN);
  if (pReq->pColumns == NULL || pReq->pTags == NULL || pReq->pFuncs == NULL) {
S
Shengliang Guan 已提交
601
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
602
    return -1;
S
Shengliang Guan 已提交
603 604 605 606
  }

  for (int32_t i = 0; i < pReq->numOfColumns; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
607
    if (tDecodeI8(&decoder, &field.type) < 0) return -1;
S
Shengliang Guan 已提交
608
    if (tDecodeI8(&decoder, &field.flags) < 0) return -1;
S
Shengliang Guan 已提交
609 610
    if (tDecodeI32(&decoder, &field.bytes) < 0) return -1;
    if (tDecodeCStrTo(&decoder, field.name) < 0) return -1;
S
Shengliang Guan 已提交
611 612
    if (taosArrayPush(pReq->pColumns, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
613
      return -1;
S
Shengliang Guan 已提交
614 615 616 617 618
    }
  }

  for (int32_t i = 0; i < pReq->numOfTags; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
619
    if (tDecodeI8(&decoder, &field.type) < 0) return -1;
S
Shengliang Guan 已提交
620
    if (tDecodeI8(&decoder, &field.flags) < 0) return -1;
S
Shengliang Guan 已提交
621 622
    if (tDecodeI32(&decoder, &field.bytes) < 0) return -1;
    if (tDecodeCStrTo(&decoder, field.name) < 0) return -1;
S
Shengliang Guan 已提交
623 624
    if (taosArrayPush(pReq->pTags, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
625
      return -1;
S
Shengliang Guan 已提交
626 627 628
    }
  }

S
Shengliang Guan 已提交
629 630 631 632 633 634 635 636 637
  for (int32_t i = 0; i < pReq->numOfFuncs; ++i) {
    char pFunc[TSDB_FUNC_NAME_LEN] = {0};
    if (tDecodeCStrTo(&decoder, pFunc) < 0) return -1;
    if (taosArrayPush(pReq->pFuncs, pFunc) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

S
sma  
Shengliang Guan 已提交
638
  if (pReq->commentLen > 0) {
S
Shengliang Guan 已提交
639 640 641
    pReq->pComment = taosMemoryMalloc(pReq->commentLen + 1);
    if (pReq->pComment == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->pComment) < 0) return -1;
S
sma  
Shengliang Guan 已提交
642 643
  }

644 645 646 647 648 649 650 651 652 653 654 655
  if (pReq->ast1Len > 0) {
    pReq->pAst1 = taosMemoryMalloc(pReq->ast1Len);
    if (pReq->pAst1 == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->pAst1) < 0) return -1;
  }

  if (pReq->ast2Len > 0) {
    pReq->pAst2 = taosMemoryMalloc(pReq->ast2Len);
    if (pReq->pAst2 == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->pAst2) < 0) return -1;
  }

656 657 658
  if (tDecodeI64(&decoder, &pReq->deleteMark1) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->deleteMark2) < 0) return -1;

S
Shengliang Guan 已提交
659
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
660
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
661
  return 0;
S
Shengliang Guan 已提交
662
}
S
Shengliang Guan 已提交
663

S
Shengliang Guan 已提交
664 665 666
void tFreeSMCreateStbReq(SMCreateStbReq *pReq) {
  taosArrayDestroy(pReq->pColumns);
  taosArrayDestroy(pReq->pTags);
S
Shengliang Guan 已提交
667 668
  taosArrayDestroy(pReq->pFuncs);
  taosMemoryFreeClear(pReq->pComment);
669 670
  taosMemoryFreeClear(pReq->pAst1);
  taosMemoryFreeClear(pReq->pAst2);
S
Shengliang Guan 已提交
671 672
}

S
Shengliang Guan 已提交
673
int32_t tSerializeSMDropStbReq(void *buf, int32_t bufLen, SMDropStbReq *pReq) {
H
Hongze Cheng 已提交
674 675
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
676

S
Shengliang Guan 已提交
677 678 679
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
680
  if (tEncodeI8(&encoder, pReq->source) < 0) return -1;
681
  for (int32_t i = 0; i < sizeof(pReq->reserved) / sizeof(int8_t); ++i) {
wmmhello's avatar
wmmhello 已提交
682 683
    if (tEncodeI8(&encoder, pReq->reserved[i]) < 0) return -1;
  }
684
  if (tEncodeI64(&encoder, pReq->suid) < 0) return -1;
S
Shengliang Guan 已提交
685
  tEndEncode(&encoder);
S
Shengliang Guan 已提交
686

S
Shengliang Guan 已提交
687
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
688
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
689 690 691
  return tlen;
}

S
Shengliang Guan 已提交
692
int32_t tDeserializeSMDropStbReq(void *buf, int32_t bufLen, SMDropStbReq *pReq) {
H
Hongze Cheng 已提交
693 694
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
695

S
Shengliang Guan 已提交
696 697 698
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
699
  if (tDecodeI8(&decoder, &pReq->source) < 0) return -1;
700
  for (int32_t i = 0; i < sizeof(pReq->reserved) / sizeof(int8_t); ++i) {
wmmhello's avatar
wmmhello 已提交
701 702
    if (tDecodeI8(&decoder, &pReq->reserved[i]) < 0) return -1;
  }
703
  if (tDecodeI64(&decoder, &pReq->suid) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
704

S
Shengliang Guan 已提交
705
  tEndDecode(&decoder);
S
Shengliang Guan 已提交
706

H
Hongze Cheng 已提交
707
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
708 709
  return 0;
}
S
Shengliang Guan 已提交
710

S
Shengliang Guan 已提交
711
int32_t tSerializeSMAlterStbReq(void *buf, int32_t bufLen, SMAlterStbReq *pReq) {
H
Hongze Cheng 已提交
712 713
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
714

S
Shengliang Guan 已提交
715 716 717 718
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->alterType) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfFields) < 0) return -1;
S
Shengliang Guan 已提交
719 720
  for (int32_t i = 0; i < pReq->numOfFields; ++i) {
    SField *pField = taosArrayGet(pReq->pFields, i);
S
Shengliang Guan 已提交
721 722 723
    if (tEncodeI8(&encoder, pField->type) < 0) return -1;
    if (tEncodeI32(&encoder, pField->bytes) < 0) return -1;
    if (tEncodeCStr(&encoder, pField->name) < 0) return -1;
S
Shengliang Guan 已提交
724
  }
X
Xiaoyu Wang 已提交
725 726 727 728 729
  if (tEncodeI32(&encoder, pReq->ttl) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->commentLen) < 0) return -1;
  if (pReq->commentLen > 0) {
    if (tEncodeCStr(&encoder, pReq->comment) < 0) return -1;
  }
S
Shengliang Guan 已提交
730
  tEndEncode(&encoder);
S
Shengliang Guan 已提交
731

S
Shengliang Guan 已提交
732
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
733
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
734 735 736
  return tlen;
}

S
Shengliang Guan 已提交
737
int32_t tDeserializeSMAlterStbReq(void *buf, int32_t bufLen, SMAlterStbReq *pReq) {
H
Hongze Cheng 已提交
738 739
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
740

S
Shengliang Guan 已提交
741 742 743 744
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->alterType) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfFields) < 0) return -1;
S
Shengliang Guan 已提交
745 746 747
  pReq->pFields = taosArrayInit(pReq->numOfFields, sizeof(SField));
  if (pReq->pFields == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
748
    return -1;
S
Shengliang Guan 已提交
749 750 751 752
  }

  for (int32_t i = 0; i < pReq->numOfFields; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
753 754 755
    if (tDecodeI8(&decoder, &field.type) < 0) return -1;
    if (tDecodeI32(&decoder, &field.bytes) < 0) return -1;
    if (tDecodeCStrTo(&decoder, field.name) < 0) return -1;
S
Shengliang Guan 已提交
756 757
    if (taosArrayPush(pReq->pFields, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
758
      return -1;
S
Shengliang Guan 已提交
759 760 761
    }
  }

X
Xiaoyu Wang 已提交
762 763 764
  if (tDecodeI32(&decoder, &pReq->ttl) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->commentLen) < 0) return -1;
  if (pReq->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
765
    pReq->comment = taosMemoryMalloc(pReq->commentLen + 1);
X
Xiaoyu Wang 已提交
766 767 768 769
    if (pReq->comment == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->comment) < 0) return -1;
  }

S
Shengliang Guan 已提交
770
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
771
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
772 773 774
  return 0;
}

S
Shengliang Guan 已提交
775
void tFreeSMAltertbReq(SMAlterStbReq *pReq) {
S
Shengliang Guan 已提交
776 777
  taosArrayDestroy(pReq->pFields);
  pReq->pFields = NULL;
778
  taosMemoryFreeClear(pReq->comment);
S
Shengliang Guan 已提交
779
}
780 781

int32_t tSerializeSEpSet(void *buf, int32_t bufLen, const SEpSet *pEpset) {
H
Hongze Cheng 已提交
782 783
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
784
  if (tStartEncode(&encoder) < 0) return -1;
785
  if (tEncodeSEpSet(&encoder, pEpset) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
786

dengyihao's avatar
dengyihao 已提交
787 788
  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
789
  tEncoderClear(&encoder);
dengyihao's avatar
dengyihao 已提交
790 791
  return tlen;
}
792 793

int32_t tDeserializeSEpSet(void *buf, int32_t bufLen, SEpSet *pEpset) {
H
Hongze Cheng 已提交
794 795
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
796
  if (tStartDecode(&decoder) < 0) return -1;
797
  if (tDecodeSEpSet(&decoder, pEpset) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
798 799

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
800
  tDecoderClear(&decoder);
dengyihao's avatar
dengyihao 已提交
801 802
  return 0;
}
S
Shengliang Guan 已提交
803

S
Shengliang Guan 已提交
804
int32_t tSerializeSMCreateSmaReq(void *buf, int32_t bufLen, SMCreateSmaReq *pReq) {
H
Hongze Cheng 已提交
805 806
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
807 808 809 810 811 812 813 814

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->stb) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->intervalUnit) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->slidingUnit) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->timezone) < 0) return -1;
S
sma  
Shengliang Guan 已提交
815
  if (tEncodeI32(&encoder, pReq->dstVgId) < 0) return -1;
S
Shengliang Guan 已提交
816 817 818
  if (tEncodeI64(&encoder, pReq->interval) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->offset) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->sliding) < 0) return -1;
819 820
  if (tEncodeI64(&encoder, pReq->watermark) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->maxDelay) < 0) return -1;
S
Shengliang Guan 已提交
821
  if (tEncodeI32(&encoder, pReq->exprLen) < 0) return -1;
S
sma  
Shengliang Guan 已提交
822 823 824
  if (tEncodeI32(&encoder, pReq->tagsFilterLen) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->sqlLen) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->astLen) < 0) return -1;
S
Shengliang Guan 已提交
825 826 827 828 829 830
  if (pReq->exprLen > 0) {
    if (tEncodeBinary(&encoder, pReq->expr, pReq->exprLen) < 0) return -1;
  }
  if (pReq->tagsFilterLen > 0) {
    if (tEncodeBinary(&encoder, pReq->tagsFilter, pReq->tagsFilterLen) < 0) return -1;
  }
S
sma  
Shengliang Guan 已提交
831 832 833 834 835 836
  if (pReq->sqlLen > 0) {
    if (tEncodeBinary(&encoder, pReq->sql, pReq->sqlLen) < 0) return -1;
  }
  if (pReq->astLen > 0) {
    if (tEncodeBinary(&encoder, pReq->ast, pReq->astLen) < 0) return -1;
  }
837
  if (tEncodeI64(&encoder, pReq->deleteMark) < 0) return -1;
S
Shengliang Guan 已提交
838 839 840
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
841
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
842 843 844 845
  return tlen;
}

int32_t tDeserializeSMCreateSmaReq(void *buf, int32_t bufLen, SMCreateSmaReq *pReq) {
H
Hongze Cheng 已提交
846 847
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
848 849 850 851 852 853 854 855

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->stb) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->intervalUnit) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->slidingUnit) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->timezone) < 0) return -1;
S
sma  
Shengliang Guan 已提交
856
  if (tDecodeI32(&decoder, &pReq->dstVgId) < 0) return -1;
S
Shengliang Guan 已提交
857 858 859
  if (tDecodeI64(&decoder, &pReq->interval) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->offset) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->sliding) < 0) return -1;
860 861
  if (tDecodeI64(&decoder, &pReq->watermark) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->maxDelay) < 0) return -1;
S
Shengliang Guan 已提交
862
  if (tDecodeI32(&decoder, &pReq->exprLen) < 0) return -1;
S
sma  
Shengliang Guan 已提交
863 864 865
  if (tDecodeI32(&decoder, &pReq->tagsFilterLen) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->sqlLen) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->astLen) < 0) return -1;
S
Shengliang Guan 已提交
866
  if (pReq->exprLen > 0) {
wafwerar's avatar
wafwerar 已提交
867
    pReq->expr = taosMemoryMalloc(pReq->exprLen);
S
Shengliang Guan 已提交
868 869 870 871
    if (pReq->expr == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->expr) < 0) return -1;
  }
  if (pReq->tagsFilterLen > 0) {
wafwerar's avatar
wafwerar 已提交
872
    pReq->tagsFilter = taosMemoryMalloc(pReq->tagsFilterLen);
S
Shengliang Guan 已提交
873 874 875
    if (pReq->tagsFilter == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->tagsFilter) < 0) return -1;
  }
S
sma  
Shengliang Guan 已提交
876
  if (pReq->sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
877
    pReq->sql = taosMemoryMalloc(pReq->sqlLen);
S
sma  
Shengliang Guan 已提交
878 879 880 881
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }
  if (pReq->astLen > 0) {
wafwerar's avatar
wafwerar 已提交
882
    pReq->ast = taosMemoryMalloc(pReq->astLen);
S
sma  
Shengliang Guan 已提交
883 884 885
    if (pReq->ast == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
  }
886
  if (tDecodeI64(&decoder, &pReq->deleteMark) < 0) return -1;
S
Shengliang Guan 已提交
887
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
888
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
889 890 891 892
  return 0;
}

void tFreeSMCreateSmaReq(SMCreateSmaReq *pReq) {
wafwerar's avatar
wafwerar 已提交
893 894 895 896
  taosMemoryFreeClear(pReq->expr);
  taosMemoryFreeClear(pReq->tagsFilter);
  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->ast);
S
Shengliang Guan 已提交
897 898 899
}

int32_t tSerializeSMDropSmaReq(void *buf, int32_t bufLen, SMDropSmaReq *pReq) {
H
Hongze Cheng 已提交
900 901
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
902 903 904

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
905

S
Shengliang Guan 已提交
906 907 908 909
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
910
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
911 912 913 914
  return tlen;
}

int32_t tDeserializeSMDropSmaReq(void *buf, int32_t bufLen, SMDropSmaReq *pReq) {
H
Hongze Cheng 已提交
915 916
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
917 918 919 920 921 922

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
923
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
924 925
  return 0;
}
dengyihao's avatar
dengyihao 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

int32_t tSerializeSCreateTagIdxReq(void *buf, int32_t bufLen, SCreateTagIndexReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->dbFName) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->stbName) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->colName) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->idxName) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->idxType) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}
int32_t tDeserializeSCreateTagIdxReq(void *buf, int32_t bufLen, SCreateTagIndexReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDecodeCStrTo(&decoder, pReq->dbFName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->stbName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->colName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->idxName) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->idxType) < 0) return -1;

  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}
dengyihao's avatar
dengyihao 已提交
959 960 961 962 963 964
int32_t tSerializeSDropTagIdxReq(void *buf, int32_t bufLen, SDropTagIndexReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;
  tEndEncode(&encoder);

dengyihao's avatar
dengyihao 已提交
965 966
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
967 968 969 970 971 972 973 974 975 976

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}
int32_t tDeserializeSDropTagIdxReq(void *buf, int32_t bufLen, SDropTagIndexReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
  if (tStartDecode(&decoder) < 0) return -1;

dengyihao's avatar
dengyihao 已提交
977 978
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
979 980 981 982 983 984

  tEndDecode(&decoder);
  tDecoderClear(&decoder);

  return 0;
}
dengyihao's avatar
dengyihao 已提交
985
int32_t tSerializeSMCreateFullTextReq(void *buf, int32_t bufLen, SMCreateFullTextReq *pReq) {
H
Hongze Cheng 已提交
986 987
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
988 989 990 991 992

  if (tStartEncode(&encoder) < 0) return -1;

  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
993
  tEncoderClear(&encoder);
dengyihao's avatar
dengyihao 已提交
994 995 996
  return tlen;
}
int32_t tDeserializeSMCreateFullTextReq(void *buf, int32_t bufLen, SMCreateFullTextReq *pReq) {
H
Hongze Cheng 已提交
997 998
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
999 1000 1001
  if (tStartDecode(&decoder) < 0) return -1;

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
1002
  tDecoderClear(&decoder);
dengyihao's avatar
dengyihao 已提交
1003 1004 1005 1006 1007 1008
  return 0;
}
void tFreeSMCreateFullTextReq(SMCreateFullTextReq *pReq) {
  // impl later
  return;
}
dengyihao's avatar
dengyihao 已提交
1009
int32_t tSerializeSMDropFullTextReq(void *buf, int32_t bufLen, SMDropFullTextReq *pReq) {
H
Hongze Cheng 已提交
1010 1011
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
1012 1013 1014

  if (tStartEncode(&encoder) < 0) return -1;

dengyihao's avatar
dengyihao 已提交
1015 1016 1017 1018
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;

  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;

dengyihao's avatar
dengyihao 已提交
1019 1020
  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1021
  tEncoderClear(&encoder);
dengyihao's avatar
dengyihao 已提交
1022 1023 1024
  return tlen;
}
int32_t tDeserializeSMDropFullTextReq(void *buf, int32_t bufLen, SMDropFullTextReq *pReq) {
H
Hongze Cheng 已提交
1025 1026
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
1027
  if (tStartDecode(&decoder) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
1028 1029
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
1030 1031

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
1032
  tDecoderClear(&decoder);
dengyihao's avatar
dengyihao 已提交
1033 1034
  return 0;
}
S
Shengliang Guan 已提交
1035

S
Shengliang Guan 已提交
1036
int32_t tSerializeSStatusReq(void *buf, int32_t bufLen, SStatusReq *pReq) {
H
Hongze Cheng 已提交
1037 1038
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1039 1040

  if (tStartEncode(&encoder) < 0) return -1;
S
Shengliang Guan 已提交
1041 1042

  // status
S
Shengliang Guan 已提交
1043
  if (tEncodeI32(&encoder, pReq->sver) < 0) return -1;
S
Shengliang Guan 已提交
1044
  if (tEncodeI64(&encoder, pReq->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1045 1046 1047 1048
  if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->clusterId) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->rebootTime) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->updateTime) < 0) return -1;
S
Shengliang Guan 已提交
1049
  if (tEncodeFloat(&encoder, pReq->numOfCores) < 0) return -1;
S
Shengliang Guan 已提交
1050
  if (tEncodeI32(&encoder, pReq->numOfSupportVnodes) < 0) return -1;
1051 1052
  if (tEncodeI64(&encoder, pReq->memTotal) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->memAvail) < 0) return -1;
S
Shengliang Guan 已提交
1053
  if (tEncodeCStr(&encoder, pReq->dnodeEp) < 0) return -1;
S
Shengliang Guan 已提交
1054 1055

  // cluster cfg
S
Shengliang Guan 已提交
1056 1057 1058 1059 1060
  if (tEncodeI32(&encoder, pReq->clusterCfg.statusInterval) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->clusterCfg.checkTime) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->clusterCfg.timezone) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->clusterCfg.locale) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->clusterCfg.charset) < 0) return -1;
S
Shengliang Guan 已提交
1061 1062 1063

  // vnode loads
  int32_t vlen = (int32_t)taosArrayGetSize(pReq->pVloads);
S
Shengliang Guan 已提交
1064
  if (tEncodeI32(&encoder, vlen) < 0) return -1;
S
Shengliang Guan 已提交
1065 1066
  for (int32_t i = 0; i < vlen; ++i) {
    SVnodeLoad *pload = taosArrayGet(pReq->pVloads, i);
1067
    int64_t     reserved = 0;
S
Shengliang Guan 已提交
1068
    if (tEncodeI32(&encoder, pload->vgId) < 0) return -1;
1069 1070
    if (tEncodeI8(&encoder, pload->syncState) < 0) return -1;
    if (tEncodeI8(&encoder, pload->syncRestore) < 0) return -1;
1071
    if (tEncodeI8(&encoder, pload->syncCanRead) < 0) return -1;
1072
    if (tEncodeI64(&encoder, pload->cacheUsage) < 0) return -1;
S
Shengliang Guan 已提交
1073 1074 1075 1076 1077
    if (tEncodeI64(&encoder, pload->numOfTables) < 0) return -1;
    if (tEncodeI64(&encoder, pload->numOfTimeSeries) < 0) return -1;
    if (tEncodeI64(&encoder, pload->totalStorage) < 0) return -1;
    if (tEncodeI64(&encoder, pload->compStorage) < 0) return -1;
    if (tEncodeI64(&encoder, pload->pointsWritten) < 0) return -1;
D
dapan1121 已提交
1078 1079
    if (tEncodeI32(&encoder, pload->numOfCachedTables) < 0) return -1;
    if (tEncodeI32(&encoder, reserved) < 0) return -1;
1080 1081
    if (tEncodeI64(&encoder, reserved) < 0) return -1;
    if (tEncodeI64(&encoder, reserved) < 0) return -1;
S
Shengliang Guan 已提交
1082 1083
  }

1084
  // mnode loads
1085 1086
  if (tEncodeI8(&encoder, pReq->mload.syncState) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->mload.syncRestore) < 0) return -1;
1087

D
dapan1121 已提交
1088 1089 1090 1091 1092 1093
  if (tEncodeI32(&encoder, pReq->qload.dnodeId) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.numOfProcessedQuery) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.numOfProcessedCQuery) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.numOfProcessedFetch) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.numOfProcessedDrop) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.numOfProcessedHb) < 0) return -1;
D
dapan1121 已提交
1094
  if (tEncodeI64(&encoder, pReq->qload.numOfProcessedDelete) < 0) return -1;
D
dapan1121 已提交
1095 1096 1097 1098 1099 1100
  if (tEncodeI64(&encoder, pReq->qload.cacheDataSize) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.numOfQueryInQueue) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.numOfFetchInQueue) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.timeInQueryQueue) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->qload.timeInFetchQueue) < 0) return -1;

1101
  if (tEncodeI32(&encoder, pReq->statusSeq) < 0) return -1;
S
Shengliang Guan 已提交
1102 1103 1104
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1105
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1106 1107 1108
  return tlen;
}

S
Shengliang Guan 已提交
1109
int32_t tDeserializeSStatusReq(void *buf, int32_t bufLen, SStatusReq *pReq) {
H
Hongze Cheng 已提交
1110 1111
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1112 1113 1114

  if (tStartDecode(&decoder) < 0) return -1;

S
Shengliang Guan 已提交
1115
  // status
S
Shengliang Guan 已提交
1116
  if (tDecodeI32(&decoder, &pReq->sver) < 0) return -1;
S
Shengliang Guan 已提交
1117
  if (tDecodeI64(&decoder, &pReq->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1118 1119 1120 1121
  if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->clusterId) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->rebootTime) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->updateTime) < 0) return -1;
S
Shengliang Guan 已提交
1122
  if (tDecodeFloat(&decoder, &pReq->numOfCores) < 0) return -1;
S
Shengliang Guan 已提交
1123
  if (tDecodeI32(&decoder, &pReq->numOfSupportVnodes) < 0) return -1;
1124 1125
  if (tDecodeI64(&decoder, &pReq->memTotal) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->memAvail) < 0) return -1;
S
Shengliang Guan 已提交
1126
  if (tDecodeCStrTo(&decoder, pReq->dnodeEp) < 0) return -1;
S
Shengliang Guan 已提交
1127 1128

  // cluster cfg
S
Shengliang Guan 已提交
1129 1130 1131 1132 1133
  if (tDecodeI32(&decoder, &pReq->clusterCfg.statusInterval) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->clusterCfg.checkTime) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->clusterCfg.timezone) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->clusterCfg.locale) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->clusterCfg.charset) < 0) return -1;
S
Shengliang Guan 已提交
1134 1135 1136

  // vnode loads
  int32_t vlen = 0;
S
Shengliang Guan 已提交
1137
  if (tDecodeI32(&decoder, &vlen) < 0) return -1;
S
Shengliang Guan 已提交
1138 1139 1140
  pReq->pVloads = taosArrayInit(vlen, sizeof(SVnodeLoad));
  if (pReq->pVloads == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1141
    return -1;
S
Shengliang Guan 已提交
1142 1143 1144 1145
  }

  for (int32_t i = 0; i < vlen; ++i) {
    SVnodeLoad vload = {0};
Y
yihaoDeng 已提交
1146 1147
    int64_t    reserved64 = 0;
    int32_t    reserved32 = 0;
S
Shengliang Guan 已提交
1148
    if (tDecodeI32(&decoder, &vload.vgId) < 0) return -1;
1149 1150
    if (tDecodeI8(&decoder, &vload.syncState) < 0) return -1;
    if (tDecodeI8(&decoder, &vload.syncRestore) < 0) return -1;
1151
    if (tDecodeI8(&decoder, &vload.syncCanRead) < 0) return -1;
1152
    if (tDecodeI64(&decoder, &vload.cacheUsage) < 0) return -1;
S
Shengliang Guan 已提交
1153 1154 1155 1156 1157
    if (tDecodeI64(&decoder, &vload.numOfTables) < 0) return -1;
    if (tDecodeI64(&decoder, &vload.numOfTimeSeries) < 0) return -1;
    if (tDecodeI64(&decoder, &vload.totalStorage) < 0) return -1;
    if (tDecodeI64(&decoder, &vload.compStorage) < 0) return -1;
    if (tDecodeI64(&decoder, &vload.pointsWritten) < 0) return -1;
D
dapan1121 已提交
1158
    if (tDecodeI32(&decoder, &vload.numOfCachedTables) < 0) return -1;
Y
yihaoDeng 已提交
1159 1160 1161
    if (tDecodeI32(&decoder, (int32_t *)&reserved32) < 0) return -1;
    if (tDecodeI64(&decoder, &reserved64) < 0) return -1;
    if (tDecodeI64(&decoder, &reserved64) < 0) return -1;
S
Shengliang Guan 已提交
1162 1163
    if (taosArrayPush(pReq->pVloads, &vload) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1164
      return -1;
S
Shengliang Guan 已提交
1165 1166 1167
    }
  }

1168 1169
  if (tDecodeI8(&decoder, &pReq->mload.syncState) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->mload.syncRestore) < 0) return -1;
1170

D
dapan1121 已提交
1171 1172 1173 1174 1175 1176
  if (tDecodeI32(&decoder, &pReq->qload.dnodeId) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.numOfProcessedQuery) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.numOfProcessedCQuery) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.numOfProcessedFetch) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.numOfProcessedDrop) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.numOfProcessedHb) < 0) return -1;
D
dapan1121 已提交
1177
  if (tDecodeI64(&decoder, &pReq->qload.numOfProcessedDelete) < 0) return -1;
D
dapan1121 已提交
1178 1179 1180 1181 1182 1183
  if (tDecodeI64(&decoder, &pReq->qload.cacheDataSize) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.numOfQueryInQueue) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.numOfFetchInQueue) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.timeInQueryQueue) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->qload.timeInFetchQueue) < 0) return -1;

1184
  if (tDecodeI32(&decoder, &pReq->statusSeq) < 0) return -1;
S
Shengliang Guan 已提交
1185
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
1186
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1187
  return 0;
S
Shengliang Guan 已提交
1188
}
S
Shengliang Guan 已提交
1189

S
Shengliang Guan 已提交
1190 1191
void tFreeSStatusReq(SStatusReq *pReq) { taosArrayDestroy(pReq->pVloads); }

S
Shengliang Guan 已提交
1192
int32_t tSerializeSStatusRsp(void *buf, int32_t bufLen, SStatusRsp *pRsp) {
H
Hongze Cheng 已提交
1193 1194
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1195

S
Shengliang Guan 已提交
1196
  if (tStartEncode(&encoder) < 0) return -1;
S
Shengliang Guan 已提交
1197

S
Shengliang Guan 已提交
1198
  // status
S
Shengliang Guan 已提交
1199
  if (tEncodeI64(&encoder, pRsp->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1200 1201

  // dnode cfg
S
Shengliang Guan 已提交
1202 1203
  if (tEncodeI32(&encoder, pRsp->dnodeCfg.dnodeId) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->dnodeCfg.clusterId) < 0) return -1;
S
Shengliang Guan 已提交
1204 1205 1206

  // dnode eps
  int32_t dlen = (int32_t)taosArrayGetSize(pRsp->pDnodeEps);
S
Shengliang Guan 已提交
1207
  if (tEncodeI32(&encoder, dlen) < 0) return -1;
S
Shengliang Guan 已提交
1208 1209
  for (int32_t i = 0; i < dlen; ++i) {
    SDnodeEp *pDnodeEp = taosArrayGet(pRsp->pDnodeEps, i);
S
Shengliang Guan 已提交
1210 1211 1212 1213
    if (tEncodeI32(&encoder, pDnodeEp->id) < 0) return -1;
    if (tEncodeI8(&encoder, pDnodeEp->isMnode) < 0) return -1;
    if (tEncodeCStr(&encoder, pDnodeEp->ep.fqdn) < 0) return -1;
    if (tEncodeU16(&encoder, pDnodeEp->ep.port) < 0) return -1;
S
Shengliang Guan 已提交
1214 1215
  }

1216
  if (tEncodeI32(&encoder, pRsp->statusSeq) < 0) return -1;
S
Shengliang Guan 已提交
1217 1218 1219
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1220
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1221 1222 1223
  return tlen;
}

S
Shengliang Guan 已提交
1224
int32_t tDeserializeSStatusRsp(void *buf, int32_t bufLen, SStatusRsp *pRsp) {
H
Hongze Cheng 已提交
1225 1226
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1227 1228 1229

  if (tStartDecode(&decoder) < 0) return -1;

S
Shengliang Guan 已提交
1230
  // status
S
Shengliang Guan 已提交
1231
  if (tDecodeI64(&decoder, &pRsp->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1232 1233

  // cluster cfg
S
Shengliang Guan 已提交
1234 1235
  if (tDecodeI32(&decoder, &pRsp->dnodeCfg.dnodeId) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->dnodeCfg.clusterId) < 0) return -1;
S
Shengliang Guan 已提交
1236 1237 1238

  // dnode eps
  int32_t dlen = 0;
S
Shengliang Guan 已提交
1239
  if (tDecodeI32(&decoder, &dlen) < 0) return -1;
S
Shengliang Guan 已提交
1240 1241 1242
  pRsp->pDnodeEps = taosArrayInit(dlen, sizeof(SDnodeEp));
  if (pRsp->pDnodeEps == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1243
    return -1;
S
Shengliang Guan 已提交
1244 1245 1246 1247
  }

  for (int32_t i = 0; i < dlen; ++i) {
    SDnodeEp dnodeEp = {0};
S
Shengliang Guan 已提交
1248 1249 1250 1251
    if (tDecodeI32(&decoder, &dnodeEp.id) < 0) return -1;
    if (tDecodeI8(&decoder, &dnodeEp.isMnode) < 0) return -1;
    if (tDecodeCStrTo(&decoder, dnodeEp.ep.fqdn) < 0) return -1;
    if (tDecodeU16(&decoder, &dnodeEp.ep.port) < 0) return -1;
S
Shengliang Guan 已提交
1252 1253
    if (taosArrayPush(pRsp->pDnodeEps, &dnodeEp) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1254
      return -1;
S
Shengliang Guan 已提交
1255 1256 1257
    }
  }

1258
  if (tDecodeI32(&decoder, &pRsp->statusSeq) < 0) return -1;
S
Shengliang Guan 已提交
1259
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
1260
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1261
  return 0;
S
Shengliang Guan 已提交
1262
}
S
Shengliang Guan 已提交
1263

S
shm  
Shengliang Guan 已提交
1264 1265
void tFreeSStatusRsp(SStatusRsp *pRsp) { taosArrayDestroy(pRsp->pDnodeEps); }

S
Shengliang Guan 已提交
1266
int32_t tSerializeSCreateAcctReq(void *buf, int32_t bufLen, SCreateAcctReq *pReq) {
H
Hongze Cheng 已提交
1267 1268
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->pass) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->maxUsers) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->maxDbs) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->maxTimeSeries) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->maxStreams) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->accessState) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->maxStorage) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1282
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1283 1284 1285
  return tlen;
}

S
Shengliang Guan 已提交
1286
int32_t tDeserializeSCreateAcctReq(void *buf, int32_t bufLen, SCreateAcctReq *pReq) {
H
Hongze Cheng 已提交
1287 1288
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->pass) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->maxUsers) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->maxDbs) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->maxTimeSeries) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->maxStreams) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->accessState) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->maxStorage) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1301
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1302
  return 0;
S
Shengliang Guan 已提交
1303 1304
}

S
Shengliang Guan 已提交
1305
int32_t tSerializeSDropUserReq(void *buf, int32_t bufLen, SDropUserReq *pReq) {
H
Hongze Cheng 已提交
1306 1307
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1308 1309 1310 1311 1312 1313

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1314
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1315 1316 1317
  return tlen;
}

S
Shengliang Guan 已提交
1318
int32_t tDeserializeSDropUserReq(void *buf, int32_t bufLen, SDropUserReq *pReq) {
H
Hongze Cheng 已提交
1319 1320
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1321 1322 1323 1324 1325

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1326
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1327
  return 0;
S
Shengliang Guan 已提交
1328 1329
}

S
Shengliang Guan 已提交
1330
int32_t tSerializeSCreateUserReq(void *buf, int32_t bufLen, SCreateUserReq *pReq) {
H
Hongze Cheng 已提交
1331 1332
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1333 1334 1335 1336

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->createType) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->superUser) < 0) return -1;
1337 1338
  if (tEncodeI8(&encoder, pReq->sysInfo) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->enable) < 0) return -1;
S
Shengliang Guan 已提交
1339 1340 1341 1342 1343
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->pass) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1344
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1345 1346 1347
  return tlen;
}

S
Shengliang Guan 已提交
1348
int32_t tDeserializeSCreateUserReq(void *buf, int32_t bufLen, SCreateUserReq *pReq) {
H
Hongze Cheng 已提交
1349 1350
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1351 1352 1353 1354

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->createType) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->superUser) < 0) return -1;
1355 1356
  if (tDecodeI8(&decoder, &pReq->sysInfo) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->enable) < 0) return -1;
S
Shengliang Guan 已提交
1357 1358 1359 1360
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->pass) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1361
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1362
  return 0;
S
Shengliang Guan 已提交
1363 1364
}

S
Shengliang Guan 已提交
1365
int32_t tSerializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq) {
H
Hongze Cheng 已提交
1366 1367
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1368 1369 1370 1371

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->alterType) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->superUser) < 0) return -1;
1372 1373
  if (tEncodeI8(&encoder, pReq->sysInfo) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->enable) < 0) return -1;
S
Shengliang Guan 已提交
1374 1375
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->pass) < 0) return -1;
1376
  if (tEncodeCStr(&encoder, pReq->objname) < 0) return -1;
X
Xiaoyu Wang 已提交
1377 1378 1379 1380 1381
  int32_t len = strlen(pReq->tabName);
  if (tEncodeI32(&encoder, len) < 0) return -1;
  if (len > 0) {
    if (tEncodeCStr(&encoder, pReq->tabName) < 0) return -1;
  }
X
Xiaoyu Wang 已提交
1382
  if (tEncodeBinary(&encoder, pReq->tagCond, pReq->tagCondLen) < 0) return -1;
S
Shengliang Guan 已提交
1383 1384 1385
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1386
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1387 1388 1389
  return tlen;
}

S
Shengliang Guan 已提交
1390
int32_t tDeserializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq) {
H
Hongze Cheng 已提交
1391 1392
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1393 1394 1395 1396

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->alterType) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->superUser) < 0) return -1;
1397 1398
  if (tDecodeI8(&decoder, &pReq->sysInfo) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->enable) < 0) return -1;
S
Shengliang Guan 已提交
1399 1400
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->pass) < 0) return -1;
1401
  if (tDecodeCStrTo(&decoder, pReq->objname) < 0) return -1;
X
Xiaoyu Wang 已提交
1402 1403 1404 1405 1406 1407
  if (!tDecodeIsEnd(&decoder)) {
    int32_t len = 0;
    if (tDecodeI32(&decoder, &len) < 0) return -1;
    if (len > 0) {
      if (tDecodeCStrTo(&decoder, pReq->tabName) < 0) return -1;
    }
X
Xiaoyu Wang 已提交
1408 1409 1410
    uint64_t tagCondLen = 0;
    if (tDecodeBinaryAlloc(&decoder, (void **)&pReq->tagCond, &tagCondLen) < 0) return -1;
    pReq->tagCondLen = tagCondLen;
X
Xiaoyu Wang 已提交
1411
  }
S
Shengliang Guan 已提交
1412 1413
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1414
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1415
  return 0;
S
Shengliang Guan 已提交
1416 1417
}

X
Xiaoyu Wang 已提交
1418 1419
void tFreeSAlterUserReq(SAlterUserReq *pReq) { taosMemoryFreeClear(pReq->tagCond); }

S
Shengliang Guan 已提交
1420
int32_t tSerializeSGetUserAuthReq(void *buf, int32_t bufLen, SGetUserAuthReq *pReq) {
H
Hongze Cheng 已提交
1421 1422
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1423 1424 1425 1426 1427 1428

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1429
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1430 1431 1432
  return tlen;
}

S
Shengliang Guan 已提交
1433
int32_t tDeserializeSGetUserAuthReq(void *buf, int32_t bufLen, SGetUserAuthReq *pReq) {
H
Hongze Cheng 已提交
1434 1435
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1436 1437 1438 1439 1440

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1441
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1442
  return 0;
S
Shengliang Guan 已提交
1443 1444
}

H
Hongze Cheng 已提交
1445
int32_t tSerializeSGetUserAuthRspImpl(SEncoder *pEncoder, SGetUserAuthRsp *pRsp) {
D
dapan 已提交
1446 1447
  if (tEncodeCStr(pEncoder, pRsp->user) < 0) return -1;
  if (tEncodeI8(pEncoder, pRsp->superAuth) < 0) return -1;
1448 1449 1450
  if (tEncodeI8(pEncoder, pRsp->sysInfo) < 0) return -1;
  if (tEncodeI8(pEncoder, pRsp->enable) < 0) return -1;
  if (tEncodeI8(pEncoder, pRsp->reserve) < 0) return -1;
D
dapan 已提交
1451
  if (tEncodeI32(pEncoder, pRsp->version) < 0) return -1;
S
Shengliang Guan 已提交
1452

D
dapan 已提交
1453
  int32_t numOfCreatedDbs = taosHashGetSize(pRsp->createdDbs);
S
Shengliang Guan 已提交
1454 1455
  int32_t numOfReadDbs = taosHashGetSize(pRsp->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pRsp->writeDbs);
C
cademfly 已提交
1456

D
dapan 已提交
1457 1458 1459
  if (tEncodeI32(pEncoder, numOfCreatedDbs) < 0) return -1;
  if (tEncodeI32(pEncoder, numOfReadDbs) < 0) return -1;
  if (tEncodeI32(pEncoder, numOfWriteDbs) < 0) return -1;
S
Shengliang Guan 已提交
1460

D
dapan 已提交
1461
  char *db = taosHashIterate(pRsp->createdDbs, NULL);
S
Shengliang Guan 已提交
1462
  while (db != NULL) {
D
dapan 已提交
1463 1464 1465 1466 1467 1468 1469
    if (tEncodeCStr(pEncoder, db) < 0) return -1;
    db = taosHashIterate(pRsp->createdDbs, db);
  }

  db = taosHashIterate(pRsp->readDbs, NULL);
  while (db != NULL) {
    if (tEncodeCStr(pEncoder, db) < 0) return -1;
S
Shengliang Guan 已提交
1470
    db = taosHashIterate(pRsp->readDbs, db);
S
Shengliang Guan 已提交
1471 1472
  }

S
Shengliang Guan 已提交
1473
  db = taosHashIterate(pRsp->writeDbs, NULL);
S
Shengliang Guan 已提交
1474
  while (db != NULL) {
D
dapan 已提交
1475
    if (tEncodeCStr(pEncoder, db) < 0) return -1;
S
Shengliang Guan 已提交
1476
    db = taosHashIterate(pRsp->writeDbs, db);
S
Shengliang Guan 已提交
1477 1478
  }

X
Xiaoyu Wang 已提交
1479 1480 1481 1482 1483 1484 1485
  int32_t numOfReadTbs = taosHashGetSize(pRsp->readTbs);
  int32_t numOfWriteTbs = taosHashGetSize(pRsp->writeTbs);
  int32_t numOfUseTbs = taosHashGetSize(pRsp->useDbs);
  if (tEncodeI32(pEncoder, numOfReadTbs) < 0) return -1;
  if (tEncodeI32(pEncoder, numOfWriteTbs) < 0) return -1;
  if (tEncodeI32(pEncoder, numOfUseTbs) < 0) return -1;

C
cademfly 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
  char *tb = taosHashIterate(pRsp->readTbs, NULL);
  while (tb != NULL) {
    size_t keyLen = 0;
    void  *key = taosHashGetKey(tb, &keyLen);
    if (tEncodeI32(pEncoder, keyLen) < 0) return -1;
    if (tEncodeCStr(pEncoder, key) < 0) return -1;

    size_t valueLen = 0;
    valueLen = strlen(tb);
    if (tEncodeI32(pEncoder, valueLen) < 0) return -1;
    if (tEncodeCStr(pEncoder, tb) < 0) return -1;

    tb = taosHashIterate(pRsp->readTbs, tb);
  }

  tb = taosHashIterate(pRsp->writeTbs, NULL);
  while (tb != NULL) {
    size_t keyLen = 0;
    void  *key = taosHashGetKey(tb, &keyLen);
    if (tEncodeI32(pEncoder, keyLen) < 0) return -1;
    if (tEncodeCStr(pEncoder, key) < 0) return -1;

    size_t valueLen = 0;
    valueLen = strlen(tb);
    if (tEncodeI32(pEncoder, valueLen) < 0) return -1;
    if (tEncodeCStr(pEncoder, tb) < 0) return -1;

    tb = taosHashIterate(pRsp->writeTbs, tb);
  }

X
Xiaoyu Wang 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
  int32_t *useDb = taosHashIterate(pRsp->useDbs, NULL);
  while (useDb != NULL) {
    size_t keyLen = 0;
    void  *key = taosHashGetKey(useDb, &keyLen);
    if (tEncodeI32(pEncoder, keyLen) < 0) return -1;
    if (tEncodeCStr(pEncoder, key) < 0) return -1;

    if (tEncodeI32(pEncoder, *useDb) < 0) return -1;
    useDb = taosHashIterate(pRsp->useDbs, useDb);
  }

K
kailixu 已提交
1527 1528 1529
  // since 3.0.7.0
  if (tEncodeI32(pEncoder, pRsp->passVer) < 0) return -1;

D
dapan 已提交
1530 1531 1532 1533
  return 0;
}

int32_t tSerializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pRsp) {
H
Hongze Cheng 已提交
1534 1535
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan 已提交
1536 1537 1538 1539 1540

  if (tStartEncode(&encoder) < 0) return -1;

  if (tSerializeSGetUserAuthRspImpl(&encoder, pRsp) < 0) return -1;

S
Shengliang Guan 已提交
1541 1542 1543
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1544
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1545 1546 1547
  return tlen;
}

H
Hongze Cheng 已提交
1548
int32_t tDeserializeSGetUserAuthRspImpl(SDecoder *pDecoder, SGetUserAuthRsp *pRsp) {
Y
yihaoDeng 已提交
1549
  char *key = NULL, *value = NULL;
D
dapan 已提交
1550 1551 1552
  pRsp->createdDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  pRsp->readDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  pRsp->writeDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
X
Xiaoyu Wang 已提交
1553 1554
  pRsp->readTbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  pRsp->writeTbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
X
Xiaoyu Wang 已提交
1555 1556 1557
  pRsp->useDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pRsp->createdDbs == NULL || pRsp->readDbs == NULL || pRsp->writeDbs == NULL || pRsp->readTbs == NULL ||
      pRsp->writeTbs == NULL || pRsp->useDbs == NULL) {
Y
yihaoDeng 已提交
1558
    goto _err;
S
Shengliang Guan 已提交
1559 1560
  }

Y
yihaoDeng 已提交
1561 1562 1563 1564 1565 1566
  if (tDecodeCStrTo(pDecoder, pRsp->user) < 0) goto _err;
  if (tDecodeI8(pDecoder, &pRsp->superAuth) < 0) goto _err;
  if (tDecodeI8(pDecoder, &pRsp->sysInfo) < 0) goto _err;
  if (tDecodeI8(pDecoder, &pRsp->enable) < 0) goto _err;
  if (tDecodeI8(pDecoder, &pRsp->reserve) < 0) goto _err;
  if (tDecodeI32(pDecoder, &pRsp->version) < 0) goto _err;
S
Shengliang Guan 已提交
1567

D
dapan 已提交
1568
  int32_t numOfCreatedDbs = 0;
S
Shengliang Guan 已提交
1569 1570
  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
Y
yihaoDeng 已提交
1571 1572 1573
  if (tDecodeI32(pDecoder, &numOfCreatedDbs) < 0) goto _err;
  if (tDecodeI32(pDecoder, &numOfReadDbs) < 0) goto _err;
  if (tDecodeI32(pDecoder, &numOfWriteDbs) < 0) goto _err;
D
dapan 已提交
1574 1575 1576

  for (int32_t i = 0; i < numOfCreatedDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
Y
yihaoDeng 已提交
1577
    if (tDecodeCStrTo(pDecoder, db) < 0) goto _err;
D
fix bug  
dapan 已提交
1578
    int32_t len = strlen(db);
D
dapan1121 已提交
1579
    taosHashPut(pRsp->createdDbs, db, len, db, len + 1);
D
dapan 已提交
1580
  }
S
Shengliang Guan 已提交
1581 1582 1583

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
Y
yihaoDeng 已提交
1584
    if (tDecodeCStrTo(pDecoder, db) < 0) goto _err;
D
fix bug  
dapan 已提交
1585
    int32_t len = strlen(db);
D
dapan1121 已提交
1586
    taosHashPut(pRsp->readDbs, db, len, db, len + 1);
S
Shengliang Guan 已提交
1587 1588 1589 1590
  }

  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
Y
yihaoDeng 已提交
1591
    if (tDecodeCStrTo(pDecoder, db) < 0) goto _err;
D
fix bug  
dapan 已提交
1592
    int32_t len = strlen(db);
D
dapan1121 已提交
1593
    taosHashPut(pRsp->writeDbs, db, len, db, len + 1);
S
Shengliang Guan 已提交
1594 1595
  }

X
Xiaoyu Wang 已提交
1596 1597 1598 1599
  if (!tDecodeIsEnd(pDecoder)) {
    int32_t numOfReadTbs = 0;
    int32_t numOfWriteTbs = 0;
    int32_t numOfUseDbs = 0;
Y
yihaoDeng 已提交
1600 1601 1602
    if (tDecodeI32(pDecoder, &numOfReadTbs) < 0) goto _err;
    if (tDecodeI32(pDecoder, &numOfWriteTbs) < 0) goto _err;
    if (tDecodeI32(pDecoder, &numOfUseDbs) < 0) goto _err;
C
cademfly 已提交
1603

X
Xiaoyu Wang 已提交
1604 1605
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
      int32_t keyLen = 0;
Y
yihaoDeng 已提交
1606
      if (tDecodeI32(pDecoder, &keyLen) < 0) goto _err;
C
cademfly 已提交
1607

Y
yihaoDeng 已提交
1608 1609
      key = taosMemoryCalloc(keyLen + 1, sizeof(char));
      if (tDecodeCStrTo(pDecoder, key) < 0) goto _err;
C
cademfly 已提交
1610

X
Xiaoyu Wang 已提交
1611
      int32_t valuelen = 0;
Y
yihaoDeng 已提交
1612 1613 1614 1615
      if (tDecodeI32(pDecoder, &valuelen) < 0) goto _err;

      value = taosMemoryCalloc(valuelen + 1, sizeof(char));
      if (tDecodeCStrTo(pDecoder, value) < 0) goto _err;
C
cademfly 已提交
1616

X
Xiaoyu Wang 已提交
1617 1618
      taosHashPut(pRsp->readTbs, key, strlen(key), value, valuelen + 1);

Y
yihaoDeng 已提交
1619 1620
      taosMemoryFreeClear(key);
      taosMemoryFreeClear(value);
X
Xiaoyu Wang 已提交
1621
    }
C
cademfly 已提交
1622

X
Xiaoyu Wang 已提交
1623 1624
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
      int32_t keyLen = 0;
Y
yihaoDeng 已提交
1625
      if (tDecodeI32(pDecoder, &keyLen) < 0) goto _err;
C
cademfly 已提交
1626

Y
yihaoDeng 已提交
1627 1628
      key = taosMemoryCalloc(keyLen + 1, sizeof(char));
      if (tDecodeCStrTo(pDecoder, key) < 0) goto _err;
C
cademfly 已提交
1629

X
Xiaoyu Wang 已提交
1630
      int32_t valuelen = 0;
Y
yihaoDeng 已提交
1631 1632 1633 1634
      if (tDecodeI32(pDecoder, &valuelen) < 0) goto _err;

      value = taosMemoryCalloc(valuelen + 1, sizeof(char));
      if (tDecodeCStrTo(pDecoder, value) < 0) goto _err;
C
cademfly 已提交
1635

X
Xiaoyu Wang 已提交
1636
      taosHashPut(pRsp->writeTbs, key, strlen(key), value, valuelen + 1);
C
cademfly 已提交
1637

Y
yihaoDeng 已提交
1638 1639
      taosMemoryFreeClear(key);
      taosMemoryFreeClear(value);
X
Xiaoyu Wang 已提交
1640 1641 1642 1643
    }

    for (int32_t i = 0; i < numOfUseDbs; ++i) {
      int32_t keyLen = 0;
Y
yihaoDeng 已提交
1644
      if (tDecodeI32(pDecoder, &keyLen) < 0) goto _err;
X
Xiaoyu Wang 已提交
1645

Y
yihaoDeng 已提交
1646 1647
      key = taosMemoryCalloc(keyLen + 1, sizeof(char));
      if (tDecodeCStrTo(pDecoder, key) < 0) goto _err;
X
Xiaoyu Wang 已提交
1648 1649

      int32_t ref = 0;
Y
yihaoDeng 已提交
1650 1651
      if (tDecodeI32(pDecoder, &ref) < 0) goto _err;

X
Xiaoyu Wang 已提交
1652
      taosHashPut(pRsp->useDbs, key, strlen(key), &ref, sizeof(ref));
Y
yihaoDeng 已提交
1653
      taosMemoryFreeClear(key);
X
Xiaoyu Wang 已提交
1654
    }
K
kailixu 已提交
1655 1656
    // since 3.0.7.0
    if (!tDecodeIsEnd(pDecoder)) {
Y
yihaoDeng 已提交
1657
      if (tDecodeI32(pDecoder, &pRsp->passVer) < 0) goto _err;
K
kailixu 已提交
1658 1659 1660
    } else {
      pRsp->passVer = 0;
    }
C
cademfly 已提交
1661
  }
D
dapan 已提交
1662
  return 0;
Y
yihaoDeng 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
_err:
  taosHashCleanup(pRsp->createdDbs);
  taosHashCleanup(pRsp->readDbs);
  taosHashCleanup(pRsp->writeDbs);
  taosHashCleanup(pRsp->writeTbs);
  taosHashCleanup(pRsp->readTbs);
  taosHashCleanup(pRsp->useDbs);

  taosMemoryFreeClear(key);
  taosMemoryFreeClear(value);
  return -1;
D
dapan 已提交
1674 1675 1676
}

int32_t tDeserializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pRsp) {
H
Hongze Cheng 已提交
1677 1678
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan 已提交
1679 1680 1681 1682 1683

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDeserializeSGetUserAuthRspImpl(&decoder, pRsp) < 0) return -1;

S
Shengliang Guan 已提交
1684 1685
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1686
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1687
  return 0;
S
Shengliang Guan 已提交
1688
}
S
Shengliang Guan 已提交
1689

S
Shengliang Guan 已提交
1690
void tFreeSGetUserAuthRsp(SGetUserAuthRsp *pRsp) {
D
dapan 已提交
1691
  taosHashCleanup(pRsp->createdDbs);
S
Shengliang Guan 已提交
1692 1693
  taosHashCleanup(pRsp->readDbs);
  taosHashCleanup(pRsp->writeDbs);
X
Xiaoyu Wang 已提交
1694 1695
  taosHashCleanup(pRsp->writeTbs);
  taosHashCleanup(pRsp->readTbs);
X
Xiaoyu Wang 已提交
1696
  taosHashCleanup(pRsp->useDbs);
S
Shengliang Guan 已提交
1697 1698
}

S
Shengliang Guan 已提交
1699
int32_t tSerializeSCreateDropMQSNodeReq(void *buf, int32_t bufLen, SMCreateQnodeReq *pReq) {
H
Hongze Cheng 已提交
1700 1701
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1702 1703 1704 1705 1706 1707

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1708
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1709 1710 1711
  return tlen;
}

S
Shengliang Guan 已提交
1712
int32_t tDeserializeSCreateDropMQSNodeReq(void *buf, int32_t bufLen, SMCreateQnodeReq *pReq) {
H
Hongze Cheng 已提交
1713 1714
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1715 1716 1717 1718 1719

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1720
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1721 1722
  return 0;
}
S
Shengliang Guan 已提交
1723 1724

int32_t tSerializeSDropDnodeReq(void *buf, int32_t bufLen, SDropDnodeReq *pReq) {
H
Hongze Cheng 已提交
1725 1726
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
1727 1728 1729 1730 1731

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->fqdn) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->port) < 0) return -1;
1732
  if (tEncodeI8(&encoder, pReq->force) < 0) return -1;
D
dapan1121 已提交
1733
  if (tEncodeI8(&encoder, pReq->unsafe) < 0) return -1;
1734 1735 1736
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1737
  tEncoderClear(&encoder);
1738
  return tlen;
S
Shengliang Guan 已提交
1739 1740 1741
}

int32_t tDeserializeSDropDnodeReq(void *buf, int32_t bufLen, SDropDnodeReq *pReq) {
H
Hongze Cheng 已提交
1742 1743
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
1744 1745 1746 1747 1748

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->fqdn) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->port) < 0) return -1;
1749
  if (tDecodeI8(&decoder, &pReq->force) < 0) return -1;
D
dmchen 已提交
1750 1751 1752 1753 1754
  if (!tDecodeIsEnd(&decoder)) {
    if (tDecodeI8(&decoder, &pReq->unsafe) < 0) return -1;
  } else {
    pReq->unsafe = false;
  }
1755

1756 1757
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1758
  tDecoderClear(&decoder);
1759
  return 0;
S
Shengliang Guan 已提交
1760 1761
}

C
cadem 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
int32_t tSerializeSRestoreDnodeReq(void *buf, int32_t bufLen, SRestoreDnodeReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->restoreType) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSRestoreDnodeReq(void *buf, int32_t bufLen, SRestoreDnodeReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->restoreType) < 0) return -1;
1783 1784
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1785
  tDecoderClear(&decoder);
1786
  return 0;
S
Shengliang Guan 已提交
1787 1788 1789
}

int32_t tSerializeSMCfgDnodeReq(void *buf, int32_t bufLen, SMCfgDnodeReq *pReq) {
H
Hongze Cheng 已提交
1790 1791
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1792 1793 1794 1795 1796 1797 1798 1799

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->config) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->value) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1800
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1801 1802 1803 1804
  return tlen;
}

int32_t tDeserializeSMCfgDnodeReq(void *buf, int32_t bufLen, SMCfgDnodeReq *pReq) {
H
Hongze Cheng 已提交
1805 1806
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1807 1808 1809 1810 1811 1812 1813

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->config) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->value) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1814
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1815 1816 1817
  return 0;
}

S
Shengliang Guan 已提交
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
int32_t tSerializeSDCfgDnodeReq(void *buf, int32_t bufLen, SDCfgDnodeReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->config) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->value) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSDCfgDnodeReq(void *buf, int32_t bufLen, SDCfgDnodeReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->config) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->value) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
1845
int32_t tSerializeSCreateDnodeReq(void *buf, int32_t bufLen, SCreateDnodeReq *pReq) {
H
Hongze Cheng 已提交
1846 1847
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1848 1849 1850 1851 1852 1853 1854

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->fqdn) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->port) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1855
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1856 1857 1858 1859
  return tlen;
}

int32_t tDeserializeSCreateDnodeReq(void *buf, int32_t bufLen, SCreateDnodeReq *pReq) {
H
Hongze Cheng 已提交
1860 1861
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1862 1863 1864 1865 1866 1867

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->fqdn) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->port) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1868
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1869 1870
  return 0;
}
S
Shengliang Guan 已提交
1871 1872

int32_t tSerializeSCreateFuncReq(void *buf, int32_t bufLen, SCreateFuncReq *pReq) {
H
Hongze Cheng 已提交
1873 1874
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1875 1876 1877 1878 1879 1880 1881 1882 1883

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->funcType) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->scriptType) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->outputType) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->outputLen) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->bufSize) < 0) return -1;
1884
  if (tEncodeI32(&encoder, pReq->codeLen) < 0) return -1;
S
Shengliang Guan 已提交
1885
  if (tEncodeI64(&encoder, pReq->signature) < 0) return -1;
1886 1887

  if (pReq->pCode != NULL) {
S
Shengliang Guan 已提交
1888
    if (tEncodeBinary(&encoder, pReq->pCode, pReq->codeLen) < 0) return -1;
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
  }

  int32_t commentSize = 0;
  if (pReq->pComment != NULL) {
    commentSize = strlen(pReq->pComment) + 1;
  }
  if (tEncodeI32(&encoder, commentSize) < 0) return -1;
  if (pReq->pComment != NULL) {
    if (tEncodeCStr(&encoder, pReq->pComment) < 0) return -1;
  }

1900 1901
  if (tEncodeI8(&encoder, pReq->orReplace) < 0) return -1;

S
Shengliang Guan 已提交
1902 1903 1904
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1905
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1906 1907 1908 1909
  return tlen;
}

int32_t tDeserializeSCreateFuncReq(void *buf, int32_t bufLen, SCreateFuncReq *pReq) {
H
Hongze Cheng 已提交
1910 1911
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1912 1913 1914 1915 1916 1917 1918 1919 1920

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->funcType) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->scriptType) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->outputType) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->outputLen) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->bufSize) < 0) return -1;
1921
  if (tDecodeI32(&decoder, &pReq->codeLen) < 0) return -1;
S
Shengliang Guan 已提交
1922
  if (tDecodeI64(&decoder, &pReq->signature) < 0) return -1;
1923

S
Shengliang Guan 已提交
1924 1925
  if (pReq->codeLen > 0) {
    pReq->pCode = taosMemoryCalloc(1, pReq->codeLen);
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
    if (pReq->pCode == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    if (tDecodeCStrTo(&decoder, pReq->pCode) < 0) return -1;
  }

  int32_t commentSize = 0;
  if (tDecodeI32(&decoder, &commentSize) < 0) return -1;
  if (commentSize > 0) {
    pReq->pComment = taosMemoryCalloc(1, commentSize);
    if (pReq->pComment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    if (tDecodeCStrTo(&decoder, pReq->pComment) < 0) return -1;
  }

1944 1945 1946 1947 1948 1949
  if (!tDecodeIsEnd(&decoder)) {
    if (tDecodeI8(&decoder, &pReq->orReplace) < 0) return -1;
  } else {
    pReq->orReplace = false;
  }

S
Shengliang Guan 已提交
1950 1951
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1952
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1953 1954 1955
  return 0;
}

1956 1957 1958 1959 1960
void tFreeSCreateFuncReq(SCreateFuncReq *pReq) {
  taosMemoryFree(pReq->pCode);
  taosMemoryFree(pReq->pComment);
}

S
Shengliang Guan 已提交
1961
int32_t tSerializeSDropFuncReq(void *buf, int32_t bufLen, SDropFuncReq *pReq) {
H
Hongze Cheng 已提交
1962 1963
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1964 1965 1966 1967 1968 1969 1970

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1971
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1972 1973 1974 1975
  return tlen;
}

int32_t tDeserializeSDropFuncReq(void *buf, int32_t bufLen, SDropFuncReq *pReq) {
H
Hongze Cheng 已提交
1976 1977
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1978 1979 1980 1981 1982 1983

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1984
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1985 1986 1987 1988
  return 0;
}

int32_t tSerializeSRetrieveFuncReq(void *buf, int32_t bufLen, SRetrieveFuncReq *pReq) {
H
Hongze Cheng 已提交
1989 1990
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1991 1992 1993

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfFuncs) < 0) return -1;
D
dapan1121 已提交
1994
  if (tEncodeI8(&encoder, pReq->ignoreCodeComment) < 0) return -1;
S
Shengliang Guan 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004

  if (pReq->numOfFuncs != (int32_t)taosArrayGetSize(pReq->pFuncNames)) return -1;
  for (int32_t i = 0; i < pReq->numOfFuncs; ++i) {
    char *fname = taosArrayGet(pReq->pFuncNames, i);
    if (tEncodeCStr(&encoder, fname) < 0) return -1;
  }

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2005
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2006 2007 2008 2009
  return tlen;
}

int32_t tDeserializeSRetrieveFuncReq(void *buf, int32_t bufLen, SRetrieveFuncReq *pReq) {
H
Hongze Cheng 已提交
2010 2011
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2012 2013 2014

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfFuncs) < 0) return -1;
D
dapan1121 已提交
2015
  if (tDecodeI8(&decoder, (int8_t *)&pReq->ignoreCodeComment) < 0) return -1;
S
Shengliang Guan 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026

  pReq->pFuncNames = taosArrayInit(pReq->numOfFuncs, TSDB_FUNC_NAME_LEN);
  if (pReq->pFuncNames == NULL) return -1;

  for (int32_t i = 0; i < pReq->numOfFuncs; ++i) {
    char fname[TSDB_FUNC_NAME_LEN] = {0};
    if (tDecodeCStrTo(&decoder, fname) < 0) return -1;
    taosArrayPush(pReq->pFuncNames, fname);
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2027
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2028 2029 2030
  return 0;
}

2031 2032
void tFreeSRetrieveFuncReq(SRetrieveFuncReq *pReq) { taosArrayDestroy(pReq->pFuncNames); }

S
Shengliang Guan 已提交
2033
int32_t tSerializeSRetrieveFuncRsp(void *buf, int32_t bufLen, SRetrieveFuncRsp *pRsp) {
H
Hongze Cheng 已提交
2034 2035
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->numOfFuncs) < 0) return -1;

  if (pRsp->numOfFuncs != (int32_t)taosArrayGetSize(pRsp->pFuncInfos)) return -1;
  for (int32_t i = 0; i < pRsp->numOfFuncs; ++i) {
    SFuncInfo *pInfo = taosArrayGet(pRsp->pFuncInfos, i);

    if (tEncodeCStr(&encoder, pInfo->name) < 0) return -1;
    if (tEncodeI8(&encoder, pInfo->funcType) < 0) return -1;
    if (tEncodeI8(&encoder, pInfo->scriptType) < 0) return -1;
    if (tEncodeI8(&encoder, pInfo->outputType) < 0) return -1;
    if (tEncodeI32(&encoder, pInfo->outputLen) < 0) return -1;
    if (tEncodeI32(&encoder, pInfo->bufSize) < 0) return -1;
    if (tEncodeI64(&encoder, pInfo->signature) < 0) return -1;
    if (tEncodeI32(&encoder, pInfo->codeSize) < 0) return -1;
2052
    if (tEncodeI32(&encoder, pInfo->commentSize) < 0) return -1;
D
dapan1121 已提交
2053
    if (pInfo->codeSize) {
S
Shengliang Guan 已提交
2054
      if (tEncodeBinary(&encoder, pInfo->pCode, pInfo->codeSize) < 0) return -1;
D
dapan1121 已提交
2055 2056 2057 2058
    }
    if (pInfo->commentSize) {
      if (tEncodeCStr(&encoder, pInfo->pComment) < 0) return -1;
    }
S
Shengliang Guan 已提交
2059 2060
  }

2061
  if (pRsp->numOfFuncs != (int32_t)taosArrayGetSize(pRsp->pFuncExtraInfos)) return -1;
2062
  for (int32_t i = 0; i < pRsp->numOfFuncs; ++i) {
2063 2064 2065
    SFuncExtraInfo *extraInfo = taosArrayGet(pRsp->pFuncExtraInfos, i);
    if (tEncodeI32(&encoder, extraInfo->funcVersion) < 0) return -1;
    if (tEncodeI64(&encoder, extraInfo->funcCreatedTime) < 0) return -1;
2066 2067
  }

S
Shengliang Guan 已提交
2068 2069 2070
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2071
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2072 2073 2074 2075
  return tlen;
}

int32_t tDeserializeSRetrieveFuncRsp(void *buf, int32_t bufLen, SRetrieveFuncRsp *pRsp) {
H
Hongze Cheng 已提交
2076 2077
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->numOfFuncs) < 0) return -1;

  pRsp->pFuncInfos = taosArrayInit(pRsp->numOfFuncs, sizeof(SFuncInfo));
  if (pRsp->pFuncInfos == NULL) return -1;

  for (int32_t i = 0; i < pRsp->numOfFuncs; ++i) {
    SFuncInfo fInfo = {0};
    if (tDecodeCStrTo(&decoder, fInfo.name) < 0) return -1;
    if (tDecodeI8(&decoder, &fInfo.funcType) < 0) return -1;
    if (tDecodeI8(&decoder, &fInfo.scriptType) < 0) return -1;
    if (tDecodeI8(&decoder, &fInfo.outputType) < 0) return -1;
    if (tDecodeI32(&decoder, &fInfo.outputLen) < 0) return -1;
    if (tDecodeI32(&decoder, &fInfo.bufSize) < 0) return -1;
    if (tDecodeI64(&decoder, &fInfo.signature) < 0) return -1;
    if (tDecodeI32(&decoder, &fInfo.codeSize) < 0) return -1;
2095
    if (tDecodeI32(&decoder, &fInfo.commentSize) < 0) return -1;
D
dapan1121 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
    if (fInfo.codeSize) {
      fInfo.pCode = taosMemoryCalloc(1, fInfo.codeSize);
      if (fInfo.pCode == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
      if (tDecodeCStrTo(&decoder, fInfo.pCode) < 0) return -1;
    }
    if (fInfo.commentSize) {
      fInfo.pComment = taosMemoryCalloc(1, fInfo.commentSize);
      if (fInfo.pComment == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
      if (tDecodeCStrTo(&decoder, fInfo.pComment) < 0) return -1;
2111 2112
    }

S
Shengliang Guan 已提交
2113 2114
    taosArrayPush(pRsp->pFuncInfos, &fInfo);
  }
2115

2116 2117
  pRsp->pFuncExtraInfos = taosArrayInit(pRsp->numOfFuncs, sizeof(SFuncExtraInfo));
  if (pRsp->pFuncExtraInfos == NULL) return -1;
2118 2119
  if (tDecodeIsEnd(&decoder)) {
    for (int32_t i = 0; i < pRsp->numOfFuncs; ++i) {
X
Xiaoyu Wang 已提交
2120
      SFuncExtraInfo extraInfo = {0};
2121
      taosArrayPush(pRsp->pFuncExtraInfos, &extraInfo);
2122 2123 2124
    }
  } else {
    for (int32_t i = 0; i < pRsp->numOfFuncs; ++i) {
X
Xiaoyu Wang 已提交
2125
      SFuncExtraInfo extraInfo = {0};
2126 2127 2128
      if (tDecodeI32(&decoder, &extraInfo.funcVersion) < 0) return -1;
      if (tDecodeI64(&decoder, &extraInfo.funcCreatedTime) < 0) return -1;
      taosArrayPush(pRsp->pFuncExtraInfos, &extraInfo);
2129
    }
2130
  }
S
Shengliang Guan 已提交
2131 2132
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2133
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2134 2135
  return 0;
}
S
Shengliang Guan 已提交
2136

D
dapan1121 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145
void tFreeSFuncInfo(SFuncInfo *pInfo) {
  if (NULL == pInfo) {
    return;
  }

  taosMemoryFree(pInfo->pCode);
  taosMemoryFree(pInfo->pComment);
}

2146 2147 2148 2149
void tFreeSRetrieveFuncRsp(SRetrieveFuncRsp *pRsp) {
  int32_t size = taosArrayGetSize(pRsp->pFuncInfos);
  for (int32_t i = 0; i < size; ++i) {
    SFuncInfo *pInfo = taosArrayGet(pRsp->pFuncInfos, i);
D
dapan1121 已提交
2150
    tFreeSFuncInfo(pInfo);
2151 2152
  }
  taosArrayDestroy(pRsp->pFuncInfos);
2153
  taosArrayDestroy(pRsp->pFuncExtraInfos);
2154 2155
}

D
dapan1121 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
int32_t tSerializeSTableCfgReq(void *buf, int32_t bufLen, STableCfgReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->dbFName) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->tbName) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->header.vgId);
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
}

int32_t tDeserializeSTableCfgReq(void *buf, int32_t bufLen, STableCfgReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);

  SMsgHead *pHead = buf;
  pHead->vgId = pReq->header.vgId;
  pHead->contLen = pReq->header.contLen;

  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->dbFName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->tbName) < 0) return -1;

  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

int32_t tSerializeSTableCfgRsp(void *buf, int32_t bufLen, STableCfgRsp *pRsp) {
D
dapan1121 已提交
2203 2204
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
2205

D
dapan1121 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->tbName) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->stbName) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->dbFName) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->numOfTags) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->numOfColumns) < 0) return -1;
  if (tEncodeI8(&encoder, pRsp->tableType) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->delay1) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->delay2) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->watermark1) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->watermark2) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->ttl) < 0) return -1;
2218

D
dapan1121 已提交
2219 2220 2221 2222 2223 2224 2225
  int32_t numOfFuncs = taosArrayGetSize(pRsp->pFuncs);
  if (tEncodeI32(&encoder, numOfFuncs) < 0) return -1;
  for (int32_t i = 0; i < numOfFuncs; ++i) {
    const char *pFunc = taosArrayGet(pRsp->pFuncs, i);
    if (tEncodeCStr(&encoder, pFunc) < 0) return -1;
  }

2226
  if (tEncodeI32(&encoder, pRsp->commentLen) < 0) return -1;
D
dapan1121 已提交
2227 2228 2229
  if (pRsp->commentLen > 0) {
    if (tEncodeCStr(&encoder, pRsp->pComment) < 0) return -1;
  }
2230

D
dapan1121 已提交
2231 2232 2233 2234 2235
  for (int32_t i = 0; i < pRsp->numOfColumns + pRsp->numOfTags; ++i) {
    SSchema *pSchema = &pRsp->pSchemas[i];
    if (tEncodeSSchema(&encoder, pSchema) < 0) return -1;
  }

2236 2237 2238
  if (tEncodeI32(&encoder, pRsp->tagsLen) < 0) return -1;
  if (tEncodeBinary(&encoder, pRsp->pTags, pRsp->tagsLen) < 0) return -1;

D
dapan1121 已提交
2239 2240 2241 2242 2243
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
D
dapan1121 已提交
2244 2245 2246
}

int32_t tDeserializeSTableCfgRsp(void *buf, int32_t bufLen, STableCfgRsp *pRsp) {
D
dapan1121 已提交
2247 2248
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
2249

D
dapan1121 已提交
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->tbName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->stbName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->dbFName) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->numOfTags) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->numOfColumns) < 0) return -1;
  if (tDecodeI8(&decoder, &pRsp->tableType) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->delay1) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->delay2) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->watermark1) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->watermark2) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->ttl) < 0) return -1;

  int32_t numOfFuncs = 0;
  if (tDecodeI32(&decoder, &numOfFuncs) < 0) return -1;
  if (numOfFuncs > 0) {
    pRsp->pFuncs = taosArrayInit(numOfFuncs, TSDB_FUNC_NAME_LEN);
    if (NULL == pRsp->pFuncs) return -1;
  }
  for (int32_t i = 0; i < numOfFuncs; ++i) {
    char pFunc[TSDB_FUNC_NAME_LEN];
    if (tDecodeCStrTo(&decoder, pFunc) < 0) return -1;
    if (taosArrayPush(pRsp->pFuncs, pFunc) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

  if (tDecodeI32(&decoder, &pRsp->commentLen) < 0) return -1;
  if (pRsp->commentLen > 0) {
    if (tDecodeCStrAlloc(&decoder, &pRsp->pComment) < 0) return -1;
  } else {
    pRsp->pComment = NULL;
  }

  int32_t totalCols = pRsp->numOfTags + pRsp->numOfColumns;
  pRsp->pSchemas = taosMemoryMalloc(sizeof(SSchema) * totalCols);
  if (pRsp->pSchemas == NULL) return -1;

  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pRsp->pSchemas[i];
    if (tDecodeSSchema(&decoder, pSchema) < 0) return -1;
  }

  if (tDecodeI32(&decoder, &pRsp->tagsLen) < 0) return -1;
2295
  if (tDecodeBinaryAlloc(&decoder, (void **)&pRsp->pTags, NULL) < 0) return -1;
D
dapan1121 已提交
2296 2297 2298 2299 2300

  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
D
dapan1121 已提交
2301 2302
}

D
dapan1121 已提交
2303 2304 2305 2306 2307 2308 2309 2310
void tFreeSTableCfgRsp(STableCfgRsp *pRsp) {
  if (NULL == pRsp) {
    return;
  }

  taosMemoryFreeClear(pRsp->pComment);
  taosMemoryFreeClear(pRsp->pSchemas);
  taosMemoryFreeClear(pRsp->pTags);
2311

D
dapan1121 已提交
2312 2313
  taosArrayDestroy(pRsp->pFuncs);
}
D
dapan1121 已提交
2314

S
Shengliang Guan 已提交
2315
int32_t tSerializeSCreateDbReq(void *buf, int32_t bufLen, SCreateDbReq *pReq) {
H
Hongze Cheng 已提交
2316 2317
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2318 2319 2320 2321

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfVgroups) < 0) return -1;
S
Shengliang Guan 已提交
2322 2323 2324 2325
  if (tEncodeI32(&encoder, pReq->numOfStables) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->buffer) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pageSize) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pages) < 0) return -1;
2326
  if (tEncodeI32(&encoder, pReq->cacheLastSize) < 0) return -1;
S
Shengliang Guan 已提交
2327 2328 2329 2330 2331 2332
  if (tEncodeI32(&encoder, pReq->daysPerFile) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep0) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep1) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep2) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->minRows) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->maxRows) < 0) return -1;
2333
  if (tEncodeI32(&encoder, pReq->walFsyncPeriod) < 0) return -1;
S
Shengliang Guan 已提交
2334 2335 2336 2337
  if (tEncodeI8(&encoder, pReq->walLevel) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->precision) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->compression) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->replications) < 0) return -1;
S
Shengliang Guan 已提交
2338
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
2339
  if (tEncodeI8(&encoder, pReq->cacheLast) < 0) return -1;
X
Xiaoyu Wang 已提交
2340
  if (tEncodeI8(&encoder, pReq->schemaless) < 0) return -1;
X
Xiaoyu Wang 已提交
2341
  if (tEncodeI32(&encoder, pReq->walRetentionPeriod) < 0) return -1;
2342
  if (tEncodeI64(&encoder, pReq->walRetentionSize) < 0) return -1;
X
Xiaoyu Wang 已提交
2343
  if (tEncodeI32(&encoder, pReq->walRollPeriod) < 0) return -1;
2344
  if (tEncodeI64(&encoder, pReq->walSegmentSize) < 0) return -1;
2345
  if (tEncodeI32(&encoder, pReq->sstTrigger) < 0) return -1;
2346 2347
  if (tEncodeI16(&encoder, pReq->hashPrefix) < 0) return -1;
  if (tEncodeI16(&encoder, pReq->hashSuffix) < 0) return -1;
S
Shengliang Guan 已提交
2348
  if (tEncodeI8(&encoder, pReq->ignoreExist) < 0) return -1;
S
Shengliang Guan 已提交
2349 2350 2351
  if (tEncodeI32(&encoder, pReq->numOfRetensions) < 0) return -1;
  for (int32_t i = 0; i < pReq->numOfRetensions; ++i) {
    SRetention *pRetension = taosArrayGet(pReq->pRetensions, i);
C
Cary Xu 已提交
2352 2353
    if (tEncodeI64(&encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI64(&encoder, pRetension->keep) < 0) return -1;
S
sma  
Shengliang Guan 已提交
2354 2355
    if (tEncodeI8(&encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->keepUnit) < 0) return -1;
S
Shengliang Guan 已提交
2356
  }
2357
  if (tEncodeI32(&encoder, pReq->tsdbPageSize) < 0) return -1;
S
Shengliang Guan 已提交
2358 2359 2360
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2361
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2362 2363 2364 2365
  return tlen;
}

int32_t tDeserializeSCreateDbReq(void *buf, int32_t bufLen, SCreateDbReq *pReq) {
H
Hongze Cheng 已提交
2366 2367
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2368 2369 2370 2371

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfVgroups) < 0) return -1;
S
Shengliang Guan 已提交
2372 2373 2374 2375
  if (tDecodeI32(&decoder, &pReq->numOfStables) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->buffer) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pageSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pages) < 0) return -1;
2376
  if (tDecodeI32(&decoder, &pReq->cacheLastSize) < 0) return -1;
S
Shengliang Guan 已提交
2377 2378 2379 2380 2381 2382
  if (tDecodeI32(&decoder, &pReq->daysPerFile) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep0) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep1) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep2) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->minRows) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->maxRows) < 0) return -1;
2383
  if (tDecodeI32(&decoder, &pReq->walFsyncPeriod) < 0) return -1;
S
Shengliang Guan 已提交
2384 2385 2386 2387
  if (tDecodeI8(&decoder, &pReq->walLevel) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->precision) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->compression) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->replications) < 0) return -1;
S
Shengliang Guan 已提交
2388
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
2389
  if (tDecodeI8(&decoder, &pReq->cacheLast) < 0) return -1;
X
Xiaoyu Wang 已提交
2390
  if (tDecodeI8(&decoder, &pReq->schemaless) < 0) return -1;
X
Xiaoyu Wang 已提交
2391
  if (tDecodeI32(&decoder, &pReq->walRetentionPeriod) < 0) return -1;
2392
  if (tDecodeI64(&decoder, &pReq->walRetentionSize) < 0) return -1;
X
Xiaoyu Wang 已提交
2393
  if (tDecodeI32(&decoder, &pReq->walRollPeriod) < 0) return -1;
2394
  if (tDecodeI64(&decoder, &pReq->walSegmentSize) < 0) return -1;
2395
  if (tDecodeI32(&decoder, &pReq->sstTrigger) < 0) return -1;
2396 2397
  if (tDecodeI16(&decoder, &pReq->hashPrefix) < 0) return -1;
  if (tDecodeI16(&decoder, &pReq->hashSuffix) < 0) return -1;
S
Shengliang Guan 已提交
2398
  if (tDecodeI8(&decoder, &pReq->ignoreExist) < 0) return -1;
S
Shengliang Guan 已提交
2399 2400 2401 2402 2403 2404 2405 2406 2407
  if (tDecodeI32(&decoder, &pReq->numOfRetensions) < 0) return -1;
  pReq->pRetensions = taosArrayInit(pReq->numOfRetensions, sizeof(SRetention));
  if (pReq->pRetensions == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  for (int32_t i = 0; i < pReq->numOfRetensions; ++i) {
    SRetention rentension = {0};
C
Cary Xu 已提交
2408 2409
    if (tDecodeI64(&decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI64(&decoder, &rentension.keep) < 0) return -1;
S
sma  
Shengliang Guan 已提交
2410 2411
    if (tDecodeI8(&decoder, &rentension.freqUnit) < 0) return -1;
    if (tDecodeI8(&decoder, &rentension.keepUnit) < 0) return -1;
S
Shengliang Guan 已提交
2412 2413 2414 2415 2416 2417
    if (taosArrayPush(pReq->pRetensions, &rentension) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

2418 2419
  if (tDecodeI32(&decoder, &pReq->tsdbPageSize) < 0) return -1;

S
Shengliang Guan 已提交
2420 2421
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2422
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2423 2424 2425
  return 0;
}

S
Shengliang Guan 已提交
2426 2427 2428 2429 2430
void tFreeSCreateDbReq(SCreateDbReq *pReq) {
  taosArrayDestroy(pReq->pRetensions);
  pReq->pRetensions = NULL;
}

S
Shengliang Guan 已提交
2431
int32_t tSerializeSAlterDbReq(void *buf, int32_t bufLen, SAlterDbReq *pReq) {
H
Hongze Cheng 已提交
2432 2433
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2434 2435 2436

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
S
Shengliang Guan 已提交
2437 2438 2439
  if (tEncodeI32(&encoder, pReq->buffer) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pageSize) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pages) < 0) return -1;
2440
  if (tEncodeI32(&encoder, pReq->cacheLastSize) < 0) return -1;
S
Shengliang Guan 已提交
2441
  if (tEncodeI32(&encoder, pReq->daysPerFile) < 0) return -1;
S
Shengliang Guan 已提交
2442 2443 2444
  if (tEncodeI32(&encoder, pReq->daysToKeep0) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep1) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep2) < 0) return -1;
2445
  if (tEncodeI32(&encoder, pReq->walFsyncPeriod) < 0) return -1;
S
Shengliang Guan 已提交
2446
  if (tEncodeI8(&encoder, pReq->walLevel) < 0) return -1;
S
Shengliang Guan 已提交
2447
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
2448
  if (tEncodeI8(&encoder, pReq->cacheLast) < 0) return -1;
X
Xiaoyu Wang 已提交
2449
  if (tEncodeI8(&encoder, pReq->replications) < 0) return -1;
2450
  if (tEncodeI32(&encoder, pReq->sstTrigger) < 0) return -1;
H
Hongze Cheng 已提交
2451 2452

  // 1st modification
2453
  if (tEncodeI32(&encoder, pReq->minRows) < 0) return -1;
2454 2455 2456
  // 2nd modification
  if (tEncodeI32(&encoder, pReq->walRetentionPeriod) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->walRetentionSize) < 0) return -1;
S
Shengliang Guan 已提交
2457 2458 2459
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2460
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2461 2462 2463 2464
  return tlen;
}

int32_t tDeserializeSAlterDbReq(void *buf, int32_t bufLen, SAlterDbReq *pReq) {
H
Hongze Cheng 已提交
2465 2466
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2467 2468 2469

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
S
Shengliang Guan 已提交
2470 2471 2472
  if (tDecodeI32(&decoder, &pReq->buffer) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pageSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pages) < 0) return -1;
2473
  if (tDecodeI32(&decoder, &pReq->cacheLastSize) < 0) return -1;
S
Shengliang Guan 已提交
2474
  if (tDecodeI32(&decoder, &pReq->daysPerFile) < 0) return -1;
S
Shengliang Guan 已提交
2475 2476 2477
  if (tDecodeI32(&decoder, &pReq->daysToKeep0) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep1) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep2) < 0) return -1;
2478
  if (tDecodeI32(&decoder, &pReq->walFsyncPeriod) < 0) return -1;
S
Shengliang Guan 已提交
2479
  if (tDecodeI8(&decoder, &pReq->walLevel) < 0) return -1;
S
Shengliang Guan 已提交
2480
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
2481
  if (tDecodeI8(&decoder, &pReq->cacheLast) < 0) return -1;
X
Xiaoyu Wang 已提交
2482
  if (tDecodeI8(&decoder, &pReq->replications) < 0) return -1;
2483
  if (tDecodeI32(&decoder, &pReq->sstTrigger) < 0) return -1;
H
Hongze Cheng 已提交
2484 2485

  // 1st modification
2486 2487
  if (!tDecodeIsEnd(&decoder)) {
    if (tDecodeI32(&decoder, &pReq->minRows) < 0) return -1;
H
Hongze Cheng 已提交
2488 2489
  } else {
    pReq->minRows = -1;
2490
  }
2491 2492 2493 2494 2495 2496 2497 2498 2499

  // 2nd modification
  if (!tDecodeIsEnd(&decoder)) {
    if (tDecodeI32(&decoder, &pReq->walRetentionPeriod) < 0) return -1;
    if (tDecodeI32(&decoder, &pReq->walRetentionSize) < 0) return -1;
  } else {
    pReq->walRetentionPeriod = -1;
    pReq->walRetentionSize = -1;
  }
S
Shengliang Guan 已提交
2500 2501
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2502
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2503 2504 2505 2506
  return 0;
}

int32_t tSerializeSDropDbReq(void *buf, int32_t bufLen, SDropDbReq *pReq) {
H
Hongze Cheng 已提交
2507 2508
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2509 2510 2511 2512 2513 2514 2515

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->ignoreNotExists) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2516
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2517 2518 2519 2520
  return tlen;
}

int32_t tDeserializeSDropDbReq(void *buf, int32_t bufLen, SDropDbReq *pReq) {
H
Hongze Cheng 已提交
2521 2522
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2523 2524 2525 2526 2527 2528

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->ignoreNotExists) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2529
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2530 2531 2532 2533
  return 0;
}

int32_t tSerializeSDropDbRsp(void *buf, int32_t bufLen, SDropDbRsp *pRsp) {
H
Hongze Cheng 已提交
2534 2535
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2536 2537 2538

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
2539
  if (tEncodeI64(&encoder, pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
2540 2541 2542
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2543
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2544 2545 2546 2547
  return tlen;
}

int32_t tDeserializeSDropDbRsp(void *buf, int32_t bufLen, SDropDbRsp *pRsp) {
H
Hongze Cheng 已提交
2548 2549
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2550 2551 2552

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
2553
  if (tDecodeI64(&decoder, &pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
2554 2555
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2556
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2557 2558 2559 2560
  return 0;
}

int32_t tSerializeSUseDbReq(void *buf, int32_t bufLen, SUseDbReq *pReq) {
H
Hongze Cheng 已提交
2561 2562
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2563 2564 2565

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
D
dapan1121 已提交
2566
  if (tEncodeI64(&encoder, pReq->dbId) < 0) return -1;
S
Shengliang Guan 已提交
2567
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
D
dapan1121 已提交
2568
  if (tEncodeI32(&encoder, pReq->numOfTable) < 0) return -1;
2569
  if (tEncodeI64(&encoder, pReq->stateTs) < 0) return -1;
S
Shengliang Guan 已提交
2570 2571 2572
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2573
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2574 2575 2576 2577
  return tlen;
}

int32_t tDeserializeSUseDbReq(void *buf, int32_t bufLen, SUseDbReq *pReq) {
H
Hongze Cheng 已提交
2578 2579
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2580 2581 2582

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
D
dapan1121 已提交
2583
  if (tDecodeI64(&decoder, &pReq->dbId) < 0) return -1;
S
Shengliang Guan 已提交
2584
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
D
dapan1121 已提交
2585
  if (tDecodeI32(&decoder, &pReq->numOfTable) < 0) return -1;
2586
  if (tDecodeI64(&decoder, &pReq->stateTs) < 0) return -1;
S
Shengliang Guan 已提交
2587 2588
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2589
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2590 2591 2592
  return 0;
}

L
Liu Jicong 已提交
2593
int32_t tSerializeSQnodeListReq(void *buf, int32_t bufLen, SQnodeListReq *pReq) {
H
Hongze Cheng 已提交
2594 2595
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
2596 2597 2598 2599 2600 2601

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->rowNum) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2602
  tEncoderClear(&encoder);
D
dapan1121 已提交
2603 2604 2605 2606
  return tlen;
}

int32_t tDeserializeSQnodeListReq(void *buf, int32_t bufLen, SQnodeListReq *pReq) {
H
Hongze Cheng 已提交
2607 2608
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
2609 2610 2611 2612 2613

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->rowNum) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2614
  tDecoderClear(&decoder);
D
dapan1121 已提交
2615 2616 2617
  return 0;
}

D
dapan1121 已提交
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
int32_t tSerializeSDnodeListReq(void *buf, int32_t bufLen, SDnodeListReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->rowNum) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSDnodeListReq(void *buf, int32_t bufLen, SDnodeListReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->rowNum) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
int32_t tSerializeSServerVerReq(void *buf, int32_t bufLen, SServerVerReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->useless) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSServerVerReq(void *buf, int32_t bufLen, SServerVerReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->useless) < 0) return -1;

  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

int32_t tSerializeSServerVerRsp(void *buf, int32_t bufLen, SServerVerRsp *pRsp) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->ver) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSServerVerRsp(void *buf, int32_t bufLen, SServerVerRsp *pRsp) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->ver) < 0) return -1;

  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
2693
int32_t tSerializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp) {
H
Hongze Cheng 已提交
2694 2695
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
2696 2697

  if (tStartEncode(&encoder) < 0) return -1;
D
dapan1121 已提交
2698
  int32_t num = taosArrayGetSize(pRsp->qnodeList);
L
Liu Jicong 已提交
2699
  if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
2700
  for (int32_t i = 0; i < num; ++i) {
D
dapan1121 已提交
2701 2702
    SQueryNodeLoad *pLoad = taosArrayGet(pRsp->qnodeList, i);
    if (tEncodeSQueryNodeLoad(&encoder, pLoad) < 0) return -1;
D
dapan1121 已提交
2703 2704 2705 2706
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2707
  tEncoderClear(&encoder);
D
dapan1121 已提交
2708 2709 2710 2711
  return tlen;
}

int32_t tDeserializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp) {
H
Hongze Cheng 已提交
2712 2713
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
2714 2715 2716 2717

  if (tStartDecode(&decoder) < 0) return -1;
  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
D
dapan1121 已提交
2718 2719 2720
  if (NULL == pRsp->qnodeList) {
    pRsp->qnodeList = taosArrayInit(num, sizeof(SQueryNodeLoad));
    if (NULL == pRsp->qnodeList) return -1;
D
dapan 已提交
2721
  }
2722

D
dapan1121 已提交
2723
  for (int32_t i = 0; i < num; ++i) {
D
dapan1121 已提交
2724 2725 2726
    SQueryNodeLoad load = {0};
    if (tDecodeSQueryNodeLoad(&decoder, &load) < 0) return -1;
    taosArrayPush(pRsp->qnodeList, &load);
D
dapan1121 已提交
2727 2728 2729
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2730
  tDecoderClear(&decoder);
D
dapan1121 已提交
2731 2732 2733
  return 0;
}

D
dapan1121 已提交
2734
void tFreeSQnodeListRsp(SQnodeListRsp *pRsp) { taosArrayDestroy(pRsp->qnodeList); }
D
dapan1121 已提交
2735

D
dapan1121 已提交
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
int32_t tSerializeSDnodeListRsp(void *buf, int32_t bufLen, SDnodeListRsp *pRsp) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  int32_t num = taosArrayGetSize(pRsp->dnodeList);
  if (tEncodeI32(&encoder, num) < 0) return -1;
  for (int32_t i = 0; i < num; ++i) {
    SEpSet *pEpSet = taosArrayGet(pRsp->dnodeList, i);
    if (tEncodeSEpSet(&encoder, pEpSet) < 0) return -1;
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSDnodeListRsp(void *buf, int32_t bufLen, SDnodeListRsp *pRsp) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
  if (NULL == pRsp->dnodeList) {
    pRsp->dnodeList = taosArrayInit(num, sizeof(SEpSet));
    if (NULL == pRsp->dnodeList) return -1;
  }

  for (int32_t i = 0; i < num; ++i) {
    SEpSet epSet = {0};
    if (tDecodeSEpSet(&decoder, &epSet) < 0) return -1;
    taosArrayPush(pRsp->dnodeList, &epSet);
  }
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

void tFreeSDnodeListRsp(SDnodeListRsp *pRsp) { taosArrayDestroy(pRsp->dnodeList); }

S
Shengliang Guan 已提交
2779
int32_t tSerializeSCompactDbReq(void *buf, int32_t bufLen, SCompactDbReq *pReq) {
H
Hongze Cheng 已提交
2780 2781
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2782 2783 2784

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
2785 2786
  if (tEncodeI64(&encoder, pReq->timeRange.skey) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->timeRange.ekey) < 0) return -1;
S
Shengliang Guan 已提交
2787 2788 2789
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2790
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2791 2792 2793
  return tlen;
}

S
Shengliang Guan 已提交
2794
int32_t tDeserializeSCompactDbReq(void *buf, int32_t bufLen, SCompactDbReq *pReq) {
H
Hongze Cheng 已提交
2795 2796
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2797 2798 2799

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
2800 2801
  if (tDecodeI64(&decoder, &pReq->timeRange.skey) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->timeRange.ekey) < 0) return -1;
S
Shengliang Guan 已提交
2802 2803
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2804
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2805
  return 0;
S
Shengliang Guan 已提交
2806 2807
}

H
Hongze Cheng 已提交
2808
int32_t tSerializeSUseDbRspImp(SEncoder *pEncoder, const SUseDbRsp *pRsp) {
S
Shengliang Guan 已提交
2809
  if (tEncodeCStr(pEncoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
2810
  if (tEncodeI64(pEncoder, pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
2811 2812
  if (tEncodeI32(pEncoder, pRsp->vgVersion) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->vgNum) < 0) return -1;
2813 2814
  if (tEncodeI16(pEncoder, pRsp->hashPrefix) < 0) return -1;
  if (tEncodeI16(pEncoder, pRsp->hashSuffix) < 0) return -1;
S
Shengliang Guan 已提交
2815 2816 2817 2818 2819 2820 2821
  if (tEncodeI8(pEncoder, pRsp->hashMethod) < 0) return -1;

  for (int32_t i = 0; i < pRsp->vgNum; ++i) {
    SVgroupInfo *pVgInfo = taosArrayGet(pRsp->pVgroupInfos, i);
    if (tEncodeI32(pEncoder, pVgInfo->vgId) < 0) return -1;
    if (tEncodeU32(pEncoder, pVgInfo->hashBegin) < 0) return -1;
    if (tEncodeU32(pEncoder, pVgInfo->hashEnd) < 0) return -1;
L
Liu Jicong 已提交
2822
    if (tEncodeSEpSet(pEncoder, &pVgInfo->epSet) < 0) return -1;
D
dapan1121 已提交
2823
    if (tEncodeI32(pEncoder, pVgInfo->numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
2824 2825
  }

2826
  if (tEncodeI32(pEncoder, pRsp->errCode) < 0) return -1;
2827
  if (tEncodeI64(pEncoder, pRsp->stateTs) < 0) return -1;
S
Shengliang Guan 已提交
2828 2829 2830
  return 0;
}

L
Liu Jicong 已提交
2831
int32_t tSerializeSUseDbRsp(void *buf, int32_t bufLen, const SUseDbRsp *pRsp) {
H
Hongze Cheng 已提交
2832 2833
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2834 2835 2836 2837 2838 2839

  if (tStartEncode(&encoder) < 0) return -1;
  if (tSerializeSUseDbRspImp(&encoder, pRsp) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2840
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2841 2842 2843
  return tlen;
}

D
dapan1121 已提交
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
int32_t tSerializeSDbHbRspImp(SEncoder *pEncoder, const SDbHbRsp *pRsp) {
  if (pRsp->useDbRsp) {
    if (tEncodeI8(pEncoder, 1) < 0) return -1;
    if (tSerializeSUseDbRspImp(pEncoder, pRsp->useDbRsp) < 0) return -1;
  } else {
    if (tEncodeI8(pEncoder, 0) < 0) return -1;
  }

  if (pRsp->cfgRsp) {
    if (tEncodeI8(pEncoder, 1) < 0) return -1;
    if (tSerializeSDbCfgRspImpl(pEncoder, pRsp->cfgRsp) < 0) return -1;
  } else {
    if (tEncodeI8(pEncoder, 0) < 0) return -1;
  }

  return 0;
}

int32_t tSerializeSDbHbBatchRsp(void *buf, int32_t bufLen, SDbHbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
2863 2864
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2865 2866 2867 2868 2869 2870

  if (tStartEncode(&encoder) < 0) return -1;

  int32_t numOfBatch = taosArrayGetSize(pRsp->pArray);
  if (tEncodeI32(&encoder, numOfBatch) < 0) return -1;
  for (int32_t i = 0; i < numOfBatch; ++i) {
D
dapan1121 已提交
2871 2872
    SDbHbRsp *pDbRsp = taosArrayGet(pRsp->pArray, i);
    if (tSerializeSDbHbRspImp(&encoder, pDbRsp) < 0) return -1;
S
Shengliang Guan 已提交
2873 2874 2875 2876
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2877
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2878 2879 2880
  return tlen;
}

H
Hongze Cheng 已提交
2881
int32_t tDeserializeSUseDbRspImp(SDecoder *pDecoder, SUseDbRsp *pRsp) {
S
Shengliang Guan 已提交
2882
  if (tDecodeCStrTo(pDecoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
2883
  if (tDecodeI64(pDecoder, &pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
2884 2885
  if (tDecodeI32(pDecoder, &pRsp->vgVersion) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->vgNum) < 0) return -1;
2886 2887
  if (tDecodeI16(pDecoder, &pRsp->hashPrefix) < 0) return -1;
  if (tDecodeI16(pDecoder, &pRsp->hashSuffix) < 0) return -1;
S
Shengliang Guan 已提交
2888 2889
  if (tDecodeI8(pDecoder, &pRsp->hashMethod) < 0) return -1;

D
dapan1121 已提交
2890 2891 2892 2893 2894 2895
  if (pRsp->vgNum > 0) {
    pRsp->pVgroupInfos = taosArrayInit(pRsp->vgNum, sizeof(SVgroupInfo));
    if (pRsp->pVgroupInfos == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
2896

D
dapan1121 已提交
2897 2898 2899 2900 2901 2902 2903 2904 2905
    for (int32_t i = 0; i < pRsp->vgNum; ++i) {
      SVgroupInfo vgInfo = {0};
      if (tDecodeI32(pDecoder, &vgInfo.vgId) < 0) return -1;
      if (tDecodeU32(pDecoder, &vgInfo.hashBegin) < 0) return -1;
      if (tDecodeU32(pDecoder, &vgInfo.hashEnd) < 0) return -1;
      if (tDecodeSEpSet(pDecoder, &vgInfo.epSet) < 0) return -1;
      if (tDecodeI32(pDecoder, &vgInfo.numOfTable) < 0) return -1;
      taosArrayPush(pRsp->pVgroupInfos, &vgInfo);
    }
S
Shengliang Guan 已提交
2906 2907
  }

2908
  if (tDecodeI32(pDecoder, &pRsp->errCode) < 0) return -1;
2909
  if (tDecodeI64(pDecoder, &pRsp->stateTs) < 0) return -1;
S
Shengliang Guan 已提交
2910 2911 2912 2913
  return 0;
}

int32_t tDeserializeSUseDbRsp(void *buf, int32_t bufLen, SUseDbRsp *pRsp) {
H
Hongze Cheng 已提交
2914 2915
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2916 2917 2918 2919 2920

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDeserializeSUseDbRspImp(&decoder, pRsp) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2921
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2922 2923 2924
  return 0;
}

Y
yihaoDeng 已提交
2925
int32_t tDeserializeSDbHbRspImp(SDecoder *decoder, SDbHbRsp *pRsp) {
D
dapan1121 已提交
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
  int8_t flag = 0;
  if (tDecodeI8(decoder, &flag) < 0) return -1;
  if (flag) {
    pRsp->useDbRsp = taosMemoryCalloc(1, sizeof(SUseDbRsp));
    if (NULL == pRsp->useDbRsp) return -1;
    if (tDeserializeSUseDbRspImp(decoder, pRsp->useDbRsp) < 0) return -1;
  }
  if (tDecodeI8(decoder, &flag) < 0) return -1;
  if (flag) {
    pRsp->cfgRsp = taosMemoryCalloc(1, sizeof(SDbCfgRsp));
    if (NULL == pRsp->cfgRsp) return -1;
    if (tDeserializeSDbCfgRspImpl(decoder, pRsp->cfgRsp) < 0) return -1;
  }

  return 0;
}

int32_t tDeserializeSDbHbBatchRsp(void *buf, int32_t bufLen, SDbHbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
2944 2945
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2946 2947 2948 2949 2950 2951

  if (tStartDecode(&decoder) < 0) return -1;

  int32_t numOfBatch = taosArrayGetSize(pRsp->pArray);
  if (tDecodeI32(&decoder, &numOfBatch) < 0) return -1;

D
dapan1121 已提交
2952
  pRsp->pArray = taosArrayInit(numOfBatch, sizeof(SDbHbRsp));
S
Shengliang Guan 已提交
2953 2954 2955 2956 2957 2958
  if (pRsp->pArray == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  for (int32_t i = 0; i < numOfBatch; ++i) {
D
dapan1121 已提交
2959 2960
    SDbHbRsp rsp = {0};
    if (tDeserializeSDbHbRspImp(&decoder, &rsp) < 0) {
2961 2962 2963
      tDecoderClear(&decoder);
      return -1;
    }
D
dapan1121 已提交
2964
    taosArrayPush(pRsp->pArray, &rsp);
S
Shengliang Guan 已提交
2965 2966 2967
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2968
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2969 2970 2971 2972 2973
  return 0;
}

void tFreeSUsedbRsp(SUseDbRsp *pRsp) { taosArrayDestroy(pRsp->pVgroupInfos); }

D
dapan1121 已提交
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
void tFreeSDbHbRsp(SDbHbRsp *pDbRsp) {
  if (NULL == pDbRsp) {
    return;
  }

  if (pDbRsp->useDbRsp) {
    tFreeSUsedbRsp(pDbRsp->useDbRsp);
    taosMemoryFree(pDbRsp->useDbRsp);
  }

  if (pDbRsp->cfgRsp) {
    tFreeSDbCfgRsp(pDbRsp->cfgRsp);
    taosMemoryFree(pDbRsp->cfgRsp);
  }
}

void tFreeSDbHbBatchRsp(SDbHbBatchRsp *pRsp) {
S
Shengliang Guan 已提交
2991 2992
  int32_t numOfBatch = taosArrayGetSize(pRsp->pArray);
  for (int32_t i = 0; i < numOfBatch; ++i) {
D
dapan1121 已提交
2993 2994
    SDbHbRsp *pDbRsp = taosArrayGet(pRsp->pArray, i);
    tFreeSDbHbRsp(pDbRsp);
S
Shengliang Guan 已提交
2995 2996 2997
  }

  taosArrayDestroy(pRsp->pArray);
S
Shengliang Guan 已提交
2998 2999
}

H
Hongze Cheng 已提交
3000
int32_t tSerializeSUserAuthBatchRsp(void *buf, int32_t bufLen, SUserAuthBatchRsp *pRsp) {
H
Hongze Cheng 已提交
3001 3002
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan 已提交
3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014

  if (tStartEncode(&encoder) < 0) return -1;

  int32_t numOfBatch = taosArrayGetSize(pRsp->pArray);
  if (tEncodeI32(&encoder, numOfBatch) < 0) return -1;
  for (int32_t i = 0; i < numOfBatch; ++i) {
    SGetUserAuthRsp *pUserAuthRsp = taosArrayGet(pRsp->pArray, i);
    if (tSerializeSGetUserAuthRspImpl(&encoder, pUserAuthRsp) < 0) return -1;
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3015
  tEncoderClear(&encoder);
D
dapan 已提交
3016 3017 3018
  return tlen;
}

H
Hongze Cheng 已提交
3019
int32_t tDeserializeSUserAuthBatchRsp(void *buf, int32_t bufLen, SUserAuthBatchRsp *pRsp) {
H
Hongze Cheng 已提交
3020 3021
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan 已提交
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040

  if (tStartDecode(&decoder) < 0) return -1;

  int32_t numOfBatch = taosArrayGetSize(pRsp->pArray);
  if (tDecodeI32(&decoder, &numOfBatch) < 0) return -1;

  pRsp->pArray = taosArrayInit(numOfBatch, sizeof(SGetUserAuthRsp));
  if (pRsp->pArray == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  for (int32_t i = 0; i < numOfBatch; ++i) {
    SGetUserAuthRsp rsp = {0};
    if (tDeserializeSGetUserAuthRspImpl(&decoder, &rsp) < 0) return -1;
    taosArrayPush(pRsp->pArray, &rsp);
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3041
  tDecoderClear(&decoder);
D
dapan 已提交
3042 3043 3044
  return 0;
}

H
Hongze Cheng 已提交
3045
void tFreeSUserAuthBatchRsp(SUserAuthBatchRsp *pRsp) {
D
dapan 已提交
3046 3047 3048 3049 3050 3051 3052 3053 3054
  int32_t numOfBatch = taosArrayGetSize(pRsp->pArray);
  for (int32_t i = 0; i < numOfBatch; ++i) {
    SGetUserAuthRsp *pUserAuthRsp = taosArrayGet(pRsp->pArray, i);
    tFreeSGetUserAuthRsp(pUserAuthRsp);
  }

  taosArrayDestroy(pRsp->pArray);
}

S
Shengliang Guan 已提交
3055
int32_t tSerializeSDbCfgReq(void *buf, int32_t bufLen, SDbCfgReq *pReq) {
H
Hongze Cheng 已提交
3056 3057
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
3058 3059 3060 3061 3062 3063

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3064
  tEncoderClear(&encoder);
D
dapan1121 已提交
3065 3066 3067
  return tlen;
}

S
Shengliang Guan 已提交
3068
int32_t tDeserializeSDbCfgReq(void *buf, int32_t bufLen, SDbCfgReq *pReq) {
H
Hongze Cheng 已提交
3069 3070
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
3071 3072 3073 3074 3075

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3076
  tDecoderClear(&decoder);
D
dapan1121 已提交
3077 3078 3079
  return 0;
}

X
Xiaoyu Wang 已提交
3080 3081 3082 3083 3084 3085
int32_t tSerializeSTrimDbReq(void *buf, int32_t bufLen, STrimDbReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
3086
  if (tEncodeI32(&encoder, pReq->maxSpeed) < 0) return -1;
X
Xiaoyu Wang 已提交
3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSTrimDbReq(void *buf, int32_t bufLen, STrimDbReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
3100
  if (tDecodeI32(&decoder, &pReq->maxSpeed) < 0) return -1;
X
Xiaoyu Wang 已提交
3101 3102 3103 3104 3105 3106
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
3107 3108 3109 3110 3111
int32_t tSerializeSVTrimDbReq(void *buf, int32_t bufLen, SVTrimDbReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
3112
  if (tEncodeI32(&encoder, pReq->timestamp) < 0) return -1;
S
Shengliang Guan 已提交
3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSVTrimDbReq(void *buf, int32_t bufLen, SVTrimDbReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
3125
  if (tDecodeI32(&decoder, &pReq->timestamp) < 0) return -1;
S
Shengliang Guan 已提交
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

int32_t tSerializeSVDropTtlTableReq(void *buf, int32_t bufLen, SVDropTtlTableReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
3137
  if (tEncodeI32(&encoder, pReq->timestampSec) < 0) return -1;
S
Shengliang Guan 已提交
3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSVDropTtlTableReq(void *buf, int32_t bufLen, SVDropTtlTableReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
3150
  if (tDecodeI32(&decoder, &pReq->timestampSec) < 0) return -1;
S
Shengliang Guan 已提交
3151 3152 3153 3154 3155 3156
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200
int32_t tSerializeSDbCfgRspImpl(SEncoder *encoder, const SDbCfgRsp *pRsp) {
  if (tEncodeCStr(encoder, pRsp->db) < 0) return -1;
  if (tEncodeI64(encoder, pRsp->dbId) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->cfgVersion) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->numOfVgroups) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->numOfStables) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->buffer) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->cacheSize) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->pageSize) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->pages) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->daysPerFile) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->daysToKeep0) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->daysToKeep1) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->daysToKeep2) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->minRows) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->maxRows) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->walFsyncPeriod) < 0) return -1;
  if (tEncodeI16(encoder, pRsp->hashPrefix) < 0) return -1;
  if (tEncodeI16(encoder, pRsp->hashSuffix) < 0) return -1;
  if (tEncodeI8(encoder, pRsp->walLevel) < 0) return -1;
  if (tEncodeI8(encoder, pRsp->precision) < 0) return -1;
  if (tEncodeI8(encoder, pRsp->compression) < 0) return -1;
  if (tEncodeI8(encoder, pRsp->replications) < 0) return -1;
  if (tEncodeI8(encoder, pRsp->strict) < 0) return -1;
  if (tEncodeI8(encoder, pRsp->cacheLast) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->tsdbPageSize) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->walRetentionPeriod) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->walRollPeriod) < 0) return -1;
  if (tEncodeI64(encoder, pRsp->walRetentionSize) < 0) return -1;
  if (tEncodeI64(encoder, pRsp->walSegmentSize) < 0) return -1;
  if (tEncodeI32(encoder, pRsp->numOfRetensions) < 0) return -1;
  for (int32_t i = 0; i < pRsp->numOfRetensions; ++i) {
    SRetention *pRetension = taosArrayGet(pRsp->pRetensions, i);
    if (tEncodeI64(encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI64(encoder, pRetension->keep) < 0) return -1;
    if (tEncodeI8(encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(encoder, pRetension->keepUnit) < 0) return -1;
  }
  if (tEncodeI8(encoder, pRsp->schemaless) < 0) return -1;
  if (tEncodeI16(encoder, pRsp->sstTrigger) < 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
3201
int32_t tSerializeSDbCfgRsp(void *buf, int32_t bufLen, const SDbCfgRsp *pRsp) {
H
Hongze Cheng 已提交
3202 3203
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
3204 3205

  if (tStartEncode(&encoder) < 0) return -1;
D
dapan1121 已提交
3206
  tSerializeSDbCfgRspImpl(&encoder, pRsp);
D
dapan1121 已提交
3207 3208
  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3209
  tEncoderClear(&encoder);
D
dapan1121 已提交
3210 3211 3212
  return tlen;
}

Y
yihaoDeng 已提交
3213
int32_t tDeserializeSDbCfgRspImpl(SDecoder *decoder, SDbCfgRsp *pRsp) {
D
dapan1121 已提交
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
  if (tDecodeCStrTo(decoder, pRsp->db) < 0) return -1;
  if (tDecodeI64(decoder, &pRsp->dbId) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->cfgVersion) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->numOfVgroups) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->numOfStables) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->buffer) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->cacheSize) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->pageSize) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->pages) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->daysPerFile) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->daysToKeep0) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->daysToKeep1) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->daysToKeep2) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->minRows) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->maxRows) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->walFsyncPeriod) < 0) return -1;
  if (tDecodeI16(decoder, &pRsp->hashPrefix) < 0) return -1;
  if (tDecodeI16(decoder, &pRsp->hashSuffix) < 0) return -1;
  if (tDecodeI8(decoder, &pRsp->walLevel) < 0) return -1;
  if (tDecodeI8(decoder, &pRsp->precision) < 0) return -1;
  if (tDecodeI8(decoder, &pRsp->compression) < 0) return -1;
  if (tDecodeI8(decoder, &pRsp->replications) < 0) return -1;
  if (tDecodeI8(decoder, &pRsp->strict) < 0) return -1;
  if (tDecodeI8(decoder, &pRsp->cacheLast) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->tsdbPageSize) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->walRetentionPeriod) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->walRollPeriod) < 0) return -1;
  if (tDecodeI64(decoder, &pRsp->walRetentionSize) < 0) return -1;
  if (tDecodeI64(decoder, &pRsp->walSegmentSize) < 0) return -1;
  if (tDecodeI32(decoder, &pRsp->numOfRetensions) < 0) return -1;
3244 3245 3246 3247 3248 3249
  if (pRsp->numOfRetensions > 0) {
    pRsp->pRetensions = taosArrayInit(pRsp->numOfRetensions, sizeof(SRetention));
    if (pRsp->pRetensions == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
3250
  }
D
dapan1121 已提交
3251

3252 3253
  for (int32_t i = 0; i < pRsp->numOfRetensions; ++i) {
    SRetention rentension = {0};
D
dapan1121 已提交
3254 3255 3256 3257
    if (tDecodeI64(decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI64(decoder, &rentension.keep) < 0) return -1;
    if (tDecodeI8(decoder, &rentension.freqUnit) < 0) return -1;
    if (tDecodeI8(decoder, &rentension.keepUnit) < 0) return -1;
3258 3259 3260 3261 3262
    if (taosArrayPush(pRsp->pRetensions, &rentension) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }
D
dapan1121 已提交
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274
  if (tDecodeI8(decoder, &pRsp->schemaless) < 0) return -1;
  if (tDecodeI16(decoder, &pRsp->sstTrigger) < 0) return -1;

  return 0;
}

int32_t tDeserializeSDbCfgRsp(void *buf, int32_t bufLen, SDbCfgRsp *pRsp) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDeserializeSDbCfgRspImpl(&decoder, pRsp) < 0) return -1;
D
dapan1121 已提交
3275 3276
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3277
  tDecoderClear(&decoder);
D
dapan1121 已提交
3278 3279 3280
  return 0;
}

D
dapan1121 已提交
3281 3282 3283 3284 3285 3286 3287 3288
void tFreeSDbCfgRsp(SDbCfgRsp *pRsp) {
  if (NULL == pRsp) {
    return;
  }

  taosArrayDestroy(pRsp->pRetensions);
}

S
Shengliang Guan 已提交
3289
int32_t tSerializeSUserIndexReq(void *buf, int32_t bufLen, SUserIndexReq *pReq) {
H
Hongze Cheng 已提交
3290 3291
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
3292 3293 3294 3295 3296 3297

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->indexFName) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3298
  tEncoderClear(&encoder);
D
dapan1121 已提交
3299 3300 3301
  return tlen;
}

S
Shengliang Guan 已提交
3302
int32_t tDeserializeSUserIndexReq(void *buf, int32_t bufLen, SUserIndexReq *pReq) {
H
Hongze Cheng 已提交
3303 3304
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
3305 3306 3307 3308 3309

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->indexFName) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3310
  tDecoderClear(&decoder);
D
dapan1121 已提交
3311 3312 3313
  return 0;
}

S
Shengliang Guan 已提交
3314
int32_t tSerializeSUserIndexRsp(void *buf, int32_t bufLen, const SUserIndexRsp *pRsp) {
H
Hongze Cheng 已提交
3315 3316
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
3317 3318 3319 3320 3321 3322 3323 3324 3325 3326

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->dbFName) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->tblFName) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->colName) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->indexType) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->indexExts) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3327
  tEncoderClear(&encoder);
D
dapan1121 已提交
3328 3329 3330
  return tlen;
}

S
Shengliang Guan 已提交
3331
int32_t tDeserializeSUserIndexRsp(void *buf, int32_t bufLen, SUserIndexRsp *pRsp) {
H
Hongze Cheng 已提交
3332 3333
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
3334 3335 3336 3337 3338 3339 3340 3341 3342

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->dbFName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->tblFName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->colName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->indexType) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->indexExts) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3343
  tDecoderClear(&decoder);
D
dapan1121 已提交
3344 3345 3346
  return 0;
}

D
dapan1121 已提交
3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
int32_t tSerializeSTableIndexReq(void *buf, int32_t bufLen, STableIndexReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->tbFName) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSTableIndexReq(void *buf, int32_t bufLen, STableIndexReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->tbFName) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

C
Cary Xu 已提交
3372
int32_t tSerializeSTableIndexInfo(SEncoder *pEncoder, STableIndexInfo *pInfo) {
D
dapan1121 已提交
3373 3374 3375 3376 3377 3378 3379
  if (tEncodeI8(pEncoder, pInfo->intervalUnit) < 0) return -1;
  if (tEncodeI8(pEncoder, pInfo->slidingUnit) < 0) return -1;
  if (tEncodeI64(pEncoder, pInfo->interval) < 0) return -1;
  if (tEncodeI64(pEncoder, pInfo->offset) < 0) return -1;
  if (tEncodeI64(pEncoder, pInfo->sliding) < 0) return -1;
  if (tEncodeI64(pEncoder, pInfo->dstTbUid) < 0) return -1;
  if (tEncodeI32(pEncoder, pInfo->dstVgId) < 0) return -1;
D
dapan1121 已提交
3380
  if (tEncodeSEpSet(pEncoder, &pInfo->epSet) < 0) return -1;
D
dapan1121 已提交
3381 3382 3383 3384 3385 3386 3387 3388 3389
  if (tEncodeCStr(pEncoder, pInfo->expr) < 0) return -1;
  return 0;
}

int32_t tSerializeSTableIndexRsp(void *buf, int32_t bufLen, const STableIndexRsp *pRsp) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
D
dapan1121 已提交
3390 3391
  if (tEncodeCStr(&encoder, pRsp->tbName) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->dbFName) < 0) return -1;
D
dapan1121 已提交
3392
  if (tEncodeU64(&encoder, pRsp->suid) < 0) return -1;
D
dapan1121 已提交
3393
  if (tEncodeI32(&encoder, pRsp->version) < 0) return -1;
D
dapan1121 已提交
3394
  if (tEncodeI32(&encoder, pRsp->indexSize) < 0) return -1;
D
dapan1121 已提交
3395 3396 3397 3398
  int32_t num = taosArrayGetSize(pRsp->pIndex);
  if (tEncodeI32(&encoder, num) < 0) return -1;
  if (num > 0) {
    for (int32_t i = 0; i < num; ++i) {
C
Cary Xu 已提交
3399
      STableIndexInfo *pInfo = (STableIndexInfo *)taosArrayGet(pRsp->pIndex, i);
D
dapan1121 已提交
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409
      if (tSerializeSTableIndexInfo(&encoder, pInfo) < 0) return -1;
    }
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

3410 3411
void tFreeSerializeSTableIndexRsp(STableIndexRsp *pRsp) {
  if (pRsp->pIndex != NULL) {
K
kailixu 已提交
3412
    tFreeSTableIndexRsp(pRsp);
3413 3414 3415 3416
    pRsp->pIndex = NULL;
  }
}

D
dapan1121 已提交
3417 3418 3419 3420 3421 3422 3423 3424
int32_t tDeserializeSTableIndexInfo(SDecoder *pDecoder, STableIndexInfo *pInfo) {
  if (tDecodeI8(pDecoder, &pInfo->intervalUnit) < 0) return -1;
  if (tDecodeI8(pDecoder, &pInfo->slidingUnit) < 0) return -1;
  if (tDecodeI64(pDecoder, &pInfo->interval) < 0) return -1;
  if (tDecodeI64(pDecoder, &pInfo->offset) < 0) return -1;
  if (tDecodeI64(pDecoder, &pInfo->sliding) < 0) return -1;
  if (tDecodeI64(pDecoder, &pInfo->dstTbUid) < 0) return -1;
  if (tDecodeI32(pDecoder, &pInfo->dstVgId) < 0) return -1;
D
dapan1121 已提交
3425
  if (tDecodeSEpSet(pDecoder, &pInfo->epSet) < 0) return -1;
D
dapan1121 已提交
3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
  if (tDecodeCStrAlloc(pDecoder, &pInfo->expr) < 0) return -1;

  return 0;
}

int32_t tDeserializeSTableIndexRsp(void *buf, int32_t bufLen, STableIndexRsp *pRsp) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
D
dapan1121 已提交
3436 3437
  if (tDecodeCStrTo(&decoder, pRsp->tbName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->dbFName) < 0) return -1;
D
dapan1121 已提交
3438
  if (tDecodeU64(&decoder, &pRsp->suid) < 0) return -1;
D
dapan1121 已提交
3439
  if (tDecodeI32(&decoder, &pRsp->version) < 0) return -1;
D
dapan1121 已提交
3440
  if (tDecodeI32(&decoder, &pRsp->indexSize) < 0) return -1;
D
dapan1121 已提交
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460
  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
  if (num > 0) {
    pRsp->pIndex = taosArrayInit(num, sizeof(STableIndexInfo));
    if (NULL == pRsp->pIndex) return -1;
    STableIndexInfo info;
    for (int32_t i = 0; i < num; ++i) {
      if (tDeserializeSTableIndexInfo(&decoder, &info) < 0) return -1;
      if (NULL == taosArrayPush(pRsp->pIndex, &info)) {
        taosMemoryFree(info.expr);
        return -1;
      }
    }
  }
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

3461
void tFreeSTableIndexInfo(void *info) {
D
dapan1121 已提交
3462 3463 3464 3465
  if (NULL == info) {
    return;
  }

3466
  STableIndexInfo *pInfo = (STableIndexInfo *)info;
D
dapan1121 已提交
3467 3468 3469

  taosMemoryFree(pInfo->expr);
}
D
dapan1121 已提交
3470

X
Xiaoyu Wang 已提交
3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495
int32_t tSerializeSShowVariablesReq(void *buf, int32_t bufLen, SShowVariablesReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->useless) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSShowVariablesReq(void *buf, int32_t bufLen, SShowVariablesReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->useless) < 0) return -1;

  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

L
Liu Jicong 已提交
3496
int32_t tEncodeSVariablesInfo(SEncoder *pEncoder, SVariablesInfo *pInfo) {
D
dapan1121 已提交
3497 3498 3499 3500 3501
  if (tEncodeCStr(pEncoder, pInfo->name) < 0) return -1;
  if (tEncodeCStr(pEncoder, pInfo->value) < 0) return -1;
  return 0;
}

L
Liu Jicong 已提交
3502
int32_t tDecodeSVariablesInfo(SDecoder *pDecoder, SVariablesInfo *pInfo) {
D
dapan1121 已提交
3503 3504 3505 3506 3507
  if (tDecodeCStrTo(pDecoder, pInfo->name) < 0) return -1;
  if (tDecodeCStrTo(pDecoder, pInfo->value) < 0) return -1;
  return 0;
}

L
Liu Jicong 已提交
3508
int32_t tSerializeSShowVariablesRsp(void *buf, int32_t bufLen, SShowVariablesRsp *pRsp) {
D
dapan1121 已提交
3509 3510 3511 3512 3513 3514 3515
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  int32_t varNum = taosArrayGetSize(pRsp->variables);
  if (tEncodeI32(&encoder, varNum) < 0) return -1;
  for (int32_t i = 0; i < varNum; ++i) {
L
Liu Jicong 已提交
3516
    SVariablesInfo *pInfo = taosArrayGet(pRsp->variables, i);
D
dapan1121 已提交
3517 3518 3519 3520 3521 3522 3523 3524 3525
    if (tEncodeSVariablesInfo(&encoder, pInfo) < 0) return -1;
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

L
Liu Jicong 已提交
3526
int32_t tDeserializeSShowVariablesRsp(void *buf, int32_t bufLen, SShowVariablesRsp *pRsp) {
D
dapan1121 已提交
3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  int32_t varNum = 0;
  if (tDecodeI32(&decoder, &varNum) < 0) return -1;
  if (varNum > 0) {
    pRsp->variables = taosArrayInit(varNum, sizeof(SVariablesInfo));
    if (NULL == pRsp->variables) return -1;
    for (int32_t i = 0; i < varNum; ++i) {
      SVariablesInfo info = {0};
      if (tDecodeSVariablesInfo(&decoder, &info) < 0) return -1;
      if (NULL == taosArrayPush(pRsp->variables, &info)) return -1;
    }
  }

  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

L
Liu Jicong 已提交
3548
void tFreeSShowVariablesRsp(SShowVariablesRsp *pRsp) {
D
dapan1121 已提交
3549 3550 3551
  if (NULL == pRsp) {
    return;
  }
L
Liu Jicong 已提交
3552

D
dapan1121 已提交
3553 3554 3555
  taosArrayDestroy(pRsp->variables);
}

S
Shengliang Guan 已提交
3556
int32_t tSerializeSShowReq(void *buf, int32_t bufLen, SShowReq *pReq) {
H
Hongze Cheng 已提交
3557 3558
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3559 3560 3561 3562 3563 3564

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->type) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->payloadLen) < 0) return -1;
  if (pReq->payloadLen > 0) {
S
Shengliang Guan 已提交
3565
    if (tEncodeBinary(&encoder, pReq->payload, pReq->payloadLen) < 0) return -1;
S
Shengliang Guan 已提交
3566 3567 3568 3569
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3570
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3571 3572 3573 3574
  return tlen;
}

int32_t tDeserializeSShowReq(void *buf, int32_t bufLen, SShowReq *pReq) {
H
Hongze Cheng 已提交
3575 3576
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3577 3578 3579 3580 3581 3582

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->type) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->payloadLen) < 0) return -1;
  if (pReq->payloadLen > 0) {
wafwerar's avatar
wafwerar 已提交
3583
    pReq->payload = taosMemoryMalloc(pReq->payloadLen);
S
Shengliang Guan 已提交
3584 3585 3586 3587 3588
    if (pReq->payload == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->payload) < 0) return -1;
  }

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
3589
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3590 3591 3592
  return 0;
}

wafwerar's avatar
wafwerar 已提交
3593
void tFreeSShowReq(SShowReq *pReq) { taosMemoryFreeClear(pReq->payload); }
3594 3595

int32_t tSerializeSRetrieveTableReq(void *buf, int32_t bufLen, SRetrieveTableReq *pReq) {
H
Hongze Cheng 已提交
3596 3597
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
3598 3599 3600

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->showId) < 0) return -1;
H
Haojun Liao 已提交
3601 3602
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->tb) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
3603
  if (tEncodeCStr(&encoder, pReq->filterTb) < 0) return -1;
3604
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
3605 3606 3607
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3608
  tEncoderClear(&encoder);
3609 3610 3611 3612
  return tlen;
}

int32_t tDeserializeSRetrieveTableReq(void *buf, int32_t bufLen, SRetrieveTableReq *pReq) {
H
Hongze Cheng 已提交
3613 3614
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
3615 3616 3617

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->showId) < 0) return -1;
H
Haojun Liao 已提交
3618 3619
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->tb) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
3620
  if (tDecodeCStrTo(&decoder, pReq->filterTb) < 0) return -1;
3621 3622
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;

3623
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
3624
  tDecoderClear(&decoder);
3625 3626
  return 0;
}
S
Shengliang Guan 已提交
3627

H
Hongze Cheng 已提交
3628
static int32_t tEncodeSTableMetaRsp(SEncoder *pEncoder, STableMetaRsp *pRsp) {
S
Shengliang Guan 已提交
3629 3630 3631
  if (tEncodeCStr(pEncoder, pRsp->tbName) < 0) return -1;
  if (tEncodeCStr(pEncoder, pRsp->stbName) < 0) return -1;
  if (tEncodeCStr(pEncoder, pRsp->dbFName) < 0) return -1;
L
Liu Jicong 已提交
3632
  if (tEncodeI64(pEncoder, pRsp->dbId) < 0) return -1;
S
Shengliang Guan 已提交
3633 3634 3635 3636 3637 3638
  if (tEncodeI32(pEncoder, pRsp->numOfTags) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->numOfColumns) < 0) return -1;
  if (tEncodeI8(pEncoder, pRsp->precision) < 0) return -1;
  if (tEncodeI8(pEncoder, pRsp->tableType) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->sversion) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->tversion) < 0) return -1;
L
Liu Jicong 已提交
3639 3640
  if (tEncodeU64(pEncoder, pRsp->suid) < 0) return -1;
  if (tEncodeU64(pEncoder, pRsp->tuid) < 0) return -1;
S
Shengliang Guan 已提交
3641 3642 3643 3644 3645 3646 3647 3648 3649
  if (tEncodeI32(pEncoder, pRsp->vgId) < 0) return -1;
  for (int32_t i = 0; i < pRsp->numOfColumns + pRsp->numOfTags; ++i) {
    SSchema *pSchema = &pRsp->pSchemas[i];
    if (tEncodeSSchema(pEncoder, pSchema) < 0) return -1;
  }

  return 0;
}

H
Hongze Cheng 已提交
3650
static int32_t tDecodeSTableMetaRsp(SDecoder *pDecoder, STableMetaRsp *pRsp) {
S
Shengliang Guan 已提交
3651 3652 3653
  if (tDecodeCStrTo(pDecoder, pRsp->tbName) < 0) return -1;
  if (tDecodeCStrTo(pDecoder, pRsp->stbName) < 0) return -1;
  if (tDecodeCStrTo(pDecoder, pRsp->dbFName) < 0) return -1;
L
Liu Jicong 已提交
3654
  if (tDecodeI64(pDecoder, &pRsp->dbId) < 0) return -1;
S
Shengliang Guan 已提交
3655 3656 3657 3658 3659 3660
  if (tDecodeI32(pDecoder, &pRsp->numOfTags) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->numOfColumns) < 0) return -1;
  if (tDecodeI8(pDecoder, &pRsp->precision) < 0) return -1;
  if (tDecodeI8(pDecoder, &pRsp->tableType) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->sversion) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->tversion) < 0) return -1;
L
Liu Jicong 已提交
3661 3662
  if (tDecodeU64(pDecoder, &pRsp->suid) < 0) return -1;
  if (tDecodeU64(pDecoder, &pRsp->tuid) < 0) return -1;
S
Shengliang Guan 已提交
3663 3664 3665
  if (tDecodeI32(pDecoder, &pRsp->vgId) < 0) return -1;

  int32_t totalCols = pRsp->numOfTags + pRsp->numOfColumns;
3666 3667 3668
  if (totalCols > 0) {
    pRsp->pSchemas = taosMemoryMalloc(sizeof(SSchema) * totalCols);
    if (pRsp->pSchemas == NULL) return -1;
S
Shengliang Guan 已提交
3669

3670 3671 3672 3673 3674 3675
    for (int32_t i = 0; i < totalCols; ++i) {
      SSchema *pSchema = &pRsp->pSchemas[i];
      if (tDecodeSSchema(pDecoder, pSchema) < 0) return -1;
    }
  } else {
    pRsp->pSchemas = NULL;
S
Shengliang Guan 已提交
3676 3677 3678 3679 3680 3681
  }

  return 0;
}

int32_t tSerializeSTableMetaRsp(void *buf, int32_t bufLen, STableMetaRsp *pRsp) {
H
Hongze Cheng 已提交
3682 3683
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3684 3685 3686 3687 3688 3689

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeSTableMetaRsp(&encoder, pRsp) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3690
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3691 3692 3693
  return tlen;
}

D
dapan1121 已提交
3694
int32_t tSerializeSSTbHbRsp(void *buf, int32_t bufLen, SSTbHbRsp *pRsp) {
H
Hongze Cheng 已提交
3695 3696
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3697 3698 3699

  if (tStartEncode(&encoder) < 0) return -1;

D
dapan1121 已提交
3700 3701 3702 3703
  int32_t numOfMeta = taosArrayGetSize(pRsp->pMetaRsp);
  if (tEncodeI32(&encoder, numOfMeta) < 0) return -1;
  for (int32_t i = 0; i < numOfMeta; ++i) {
    STableMetaRsp *pMetaRsp = taosArrayGet(pRsp->pMetaRsp, i);
S
Shengliang Guan 已提交
3704 3705
    if (tEncodeSTableMetaRsp(&encoder, pMetaRsp) < 0) return -1;
  }
D
dapan1121 已提交
3706 3707

  int32_t numOfIndex = taosArrayGetSize(pRsp->pIndexRsp);
C
Cary Xu 已提交
3708
  if (tEncodeI32(&encoder, numOfIndex) < 0) return -1;
D
dapan1121 已提交
3709
  for (int32_t i = 0; i < numOfIndex; ++i) {
D
dapan1121 已提交
3710 3711 3712 3713 3714
    STableIndexRsp *pIndexRsp = taosArrayGet(pRsp->pIndexRsp, i);
    if (tEncodeCStr(&encoder, pIndexRsp->tbName) < 0) return -1;
    if (tEncodeCStr(&encoder, pIndexRsp->dbFName) < 0) return -1;
    if (tEncodeU64(&encoder, pIndexRsp->suid) < 0) return -1;
    if (tEncodeI32(&encoder, pIndexRsp->version) < 0) return -1;
D
dapan1121 已提交
3715
    if (tEncodeI32(&encoder, pIndexRsp->indexSize) < 0) return -1;
D
dapan1121 已提交
3716 3717
    int32_t num = taosArrayGetSize(pIndexRsp->pIndex);
    if (tEncodeI32(&encoder, num) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
3718 3719
    for (int32_t j = 0; j < num; ++j) {
      STableIndexInfo *pInfo = (STableIndexInfo *)taosArrayGet(pIndexRsp->pIndex, j);
D
dapan1121 已提交
3720 3721
      if (tSerializeSTableIndexInfo(&encoder, pInfo) < 0) return -1;
    }
D
dapan1121 已提交
3722 3723
  }

S
Shengliang Guan 已提交
3724 3725 3726
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3727
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3728 3729 3730 3731
  return tlen;
}

int32_t tDeserializeSTableMetaRsp(void *buf, int32_t bufLen, STableMetaRsp *pRsp) {
H
Hongze Cheng 已提交
3732 3733
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3734 3735 3736 3737 3738

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeSTableMetaRsp(&decoder, pRsp) < 0) return -1;

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
3739
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3740 3741 3742
  return 0;
}

D
dapan1121 已提交
3743
int32_t tDeserializeSSTbHbRsp(void *buf, int32_t bufLen, SSTbHbRsp *pRsp) {
H
Hongze Cheng 已提交
3744 3745
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3746 3747 3748

  if (tStartDecode(&decoder) < 0) return -1;

D
dapan1121 已提交
3749
  int32_t numOfMeta = 0;
D
dapan1121 已提交
3750 3751 3752
  if (tDecodeI32(&decoder, &numOfMeta) < 0) return -1;
  pRsp->pMetaRsp = taosArrayInit(numOfMeta, sizeof(STableMetaRsp));
  if (pRsp->pMetaRsp == NULL) {
S
Shengliang Guan 已提交
3753 3754 3755 3756
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

D
dapan1121 已提交
3757
  for (int32_t i = 0; i < numOfMeta; ++i) {
S
Shengliang Guan 已提交
3758 3759
    STableMetaRsp tableMetaRsp = {0};
    if (tDecodeSTableMetaRsp(&decoder, &tableMetaRsp) < 0) return -1;
D
dapan1121 已提交
3760 3761 3762
    taosArrayPush(pRsp->pMetaRsp, &tableMetaRsp);
  }

D
dapan1121 已提交
3763
  int32_t numOfIndex = 0;
D
dapan1121 已提交
3764 3765 3766 3767 3768 3769
  if (tDecodeI32(&decoder, &numOfIndex) < 0) return -1;

  pRsp->pIndexRsp = taosArrayInit(numOfIndex, sizeof(STableIndexRsp));
  if (pRsp->pIndexRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
Shengliang Guan 已提交
3770
  }
D
dapan1121 已提交
3771 3772 3773 3774 3775 3776 3777

  for (int32_t i = 0; i < numOfIndex; ++i) {
    STableIndexRsp tableIndexRsp = {0};
    if (tDecodeCStrTo(&decoder, tableIndexRsp.tbName) < 0) return -1;
    if (tDecodeCStrTo(&decoder, tableIndexRsp.dbFName) < 0) return -1;
    if (tDecodeU64(&decoder, &tableIndexRsp.suid) < 0) return -1;
    if (tDecodeI32(&decoder, &tableIndexRsp.version) < 0) return -1;
D
dapan1121 已提交
3778
    if (tDecodeI32(&decoder, &tableIndexRsp.indexSize) < 0) return -1;
D
dapan1121 已提交
3779 3780 3781 3782 3783 3784
    int32_t num = 0;
    if (tDecodeI32(&decoder, &num) < 0) return -1;
    if (num > 0) {
      tableIndexRsp.pIndex = taosArrayInit(num, sizeof(STableIndexInfo));
      if (NULL == tableIndexRsp.pIndex) return -1;
      STableIndexInfo info;
wmmhello's avatar
wmmhello 已提交
3785
      for (int32_t j = 0; j < num; ++j) {
D
dapan1121 已提交
3786 3787 3788 3789 3790 3791 3792 3793
        if (tDeserializeSTableIndexInfo(&decoder, &info) < 0) return -1;
        if (NULL == taosArrayPush(tableIndexRsp.pIndex, &info)) {
          taosMemoryFree(info.expr);
          return -1;
        }
      }
    }
    taosArrayPush(pRsp->pIndexRsp, &tableIndexRsp);
S
Shengliang Guan 已提交
3794
  }
C
Cary Xu 已提交
3795

S
Shengliang Guan 已提交
3796 3797
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3798
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3799 3800 3801
  return 0;
}

3802
void tFreeSTableMetaRsp(void *pRsp) {
D
dapan1121 已提交
3803 3804 3805
  if (NULL == pRsp) {
    return;
  }
3806 3807

  taosMemoryFreeClear(((STableMetaRsp *)pRsp)->pSchemas);
D
dapan1121 已提交
3808
}
S
Shengliang Guan 已提交
3809

D
dapan1121 已提交
3810 3811 3812 3813 3814 3815 3816 3817 3818 3819
void tFreeSTableIndexRsp(void *info) {
  if (NULL == info) {
    return;
  }

  STableIndexRsp *pInfo = (STableIndexRsp *)info;

  taosArrayDestroyEx(pInfo->pIndex, tFreeSTableIndexInfo);
}

D
dapan1121 已提交
3820
void tFreeSSTbHbRsp(SSTbHbRsp *pRsp) {
D
dapan1121 已提交
3821 3822 3823
  int32_t numOfMeta = taosArrayGetSize(pRsp->pMetaRsp);
  for (int32_t i = 0; i < numOfMeta; ++i) {
    STableMetaRsp *pMetaRsp = taosArrayGet(pRsp->pMetaRsp, i);
S
Shengliang Guan 已提交
3824 3825 3826
    tFreeSTableMetaRsp(pMetaRsp);
  }

D
dapan1121 已提交
3827 3828 3829 3830 3831 3832 3833 3834 3835
  taosArrayDestroy(pRsp->pMetaRsp);

  int32_t numOfIndex = taosArrayGetSize(pRsp->pIndexRsp);
  for (int32_t i = 0; i < numOfIndex; ++i) {
    STableIndexRsp *pIndexRsp = taosArrayGet(pRsp->pIndexRsp, i);
    tFreeSTableIndexRsp(pIndexRsp);
  }

  taosArrayDestroy(pRsp->pIndexRsp);
S
Shengliang Guan 已提交
3836 3837 3838
}

int32_t tSerializeSShowRsp(void *buf, int32_t bufLen, SShowRsp *pRsp) {
H
Hongze Cheng 已提交
3839 3840
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3841 3842 3843 3844 3845 3846 3847

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->showId) < 0) return -1;
  if (tEncodeSTableMetaRsp(&encoder, &pRsp->tableMeta) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3848
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3849 3850 3851 3852
  return tlen;
}

int32_t tDeserializeSShowRsp(void *buf, int32_t bufLen, SShowRsp *pRsp) {
H
Hongze Cheng 已提交
3853 3854
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3855 3856 3857 3858 3859 3860

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->showId) < 0) return -1;
  if (tDecodeSTableMetaRsp(&decoder, &pRsp->tableMeta) < 0) return -1;

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
3861
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3862 3863 3864 3865 3866 3867 3868
  return 0;
}

void tFreeSShowRsp(SShowRsp *pRsp) { tFreeSTableMetaRsp(&pRsp->tableMeta); }

int32_t tSerializeSTableInfoReq(void *buf, int32_t bufLen, STableInfoReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
S
Shengliang Guan 已提交
3869 3870 3871 3872
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }
S
Shengliang Guan 已提交
3873

H
Hongze Cheng 已提交
3874 3875
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3876 3877 3878 3879 3880 3881 3882

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->dbFName) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->tbName) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3883
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3884 3885 3886 3887 3888 3889 3890 3891

  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->header.vgId);
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
S
Shengliang Guan 已提交
3892 3893 3894
}

int32_t tDeserializeSTableInfoReq(void *buf, int32_t bufLen, STableInfoReq *pReq) {
S
Shengliang Guan 已提交
3895 3896 3897 3898 3899
  int32_t headLen = sizeof(SMsgHead);

  SMsgHead *pHead = buf;
  pHead->vgId = pReq->header.vgId;
  pHead->contLen = pReq->header.contLen;
S
Shengliang Guan 已提交
3900

H
Hongze Cheng 已提交
3901 3902
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);
S
Shengliang Guan 已提交
3903 3904 3905 3906 3907 3908

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->dbFName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->tbName) < 0) return -1;

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
3909
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3910 3911
  return 0;
}
S
Shengliang Guan 已提交
3912

S
Shengliang Guan 已提交
3913
int32_t tSerializeSMDropTopicReq(void *buf, int32_t bufLen, SMDropTopicReq *pReq) {
H
Hongze Cheng 已提交
3914 3915
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3916 3917 3918 3919 3920 3921 3922

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3923
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3924 3925 3926 3927
  return tlen;
}

int32_t tDeserializeSMDropTopicReq(void *buf, int32_t bufLen, SMDropTopicReq *pReq) {
H
Hongze Cheng 已提交
3928 3929
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3930 3931 3932 3933 3934 3935

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3936
  tDecoderClear(&decoder);
3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965
  return 0;
}

int32_t tSerializeSMDropCgroupReq(void *buf, int32_t bufLen, SMDropCgroupReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->topic) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->cgroup) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMDropCgroupReq(void *buf, int32_t bufLen, SMDropCgroupReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->topic) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->cgroup) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3966 3967
  return 0;
}
S
Shengliang Guan 已提交
3968

L
Liu Jicong 已提交
3969
int32_t tSerializeSCMCreateTopicReq(void *buf, int32_t bufLen, const SCMCreateTopicReq *pReq) {
H
Hongze Cheng 已提交
3970 3971
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3972 3973 3974 3975

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
L
Liu Jicong 已提交
3976
  if (tEncodeI8(&encoder, pReq->subType) < 0) return -1;
L
Liu Jicong 已提交
3977
  if (tEncodeI8(&encoder, pReq->withMeta) < 0) return -1;
L
Liu Jicong 已提交
3978
  if (tEncodeCStr(&encoder, pReq->subDbName) < 0) return -1;
3979 3980
  if (TOPIC_SUB_TYPE__DB == pReq->subType) {
  } else {
3981 3982 3983
    if (TOPIC_SUB_TYPE__TABLE == pReq->subType) {
      if (tEncodeCStr(&encoder, pReq->subStbName) < 0) return -1;
    }
3984 3985 3986 3987 3988 3989
    if (pReq->ast && strlen(pReq->ast) > 0) {
      if (tEncodeI32(&encoder, strlen(pReq->ast)) < 0) return -1;
      if (tEncodeCStr(&encoder, pReq->ast) < 0) return -1;
    } else {
      if (tEncodeI32(&encoder, 0) < 0) return -1;
    }
3990 3991 3992
  }
  if (tEncodeI32(&encoder, strlen(pReq->sql)) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->sql) < 0) return -1;
S
Shengliang Guan 已提交
3993 3994 3995 3996

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3997
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3998 3999 4000
  return tlen;
}

L
Liu Jicong 已提交
4001
int32_t tDeserializeSCMCreateTopicReq(void *buf, int32_t bufLen, SCMCreateTopicReq *pReq) {
S
Shengliang Guan 已提交
4002
  int32_t sqlLen = 0;
4003
  int32_t astLen = 0;
S
Shengliang Guan 已提交
4004

H
Hongze Cheng 已提交
4005 4006
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4007 4008 4009 4010

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
L
Liu Jicong 已提交
4011
  if (tDecodeI8(&decoder, &pReq->subType) < 0) return -1;
L
Liu Jicong 已提交
4012
  if (tDecodeI8(&decoder, &pReq->withMeta) < 0) return -1;
L
Liu Jicong 已提交
4013
  if (tDecodeCStrTo(&decoder, pReq->subDbName) < 0) return -1;
4014 4015
  if (TOPIC_SUB_TYPE__DB == pReq->subType) {
  } else {
4016 4017 4018
    if (TOPIC_SUB_TYPE__TABLE == pReq->subType) {
      if (tDecodeCStrTo(&decoder, pReq->subStbName) < 0) return -1;
    }
4019 4020 4021 4022 4023 4024 4025
    if (tDecodeI32(&decoder, &astLen) < 0) return -1;
    if (astLen > 0) {
      pReq->ast = taosMemoryCalloc(1, astLen + 1);
      if (pReq->ast == NULL) return -1;
      if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
    }
  }
S
Shengliang Guan 已提交
4026
  if (tDecodeI32(&decoder, &sqlLen) < 0) return -1;
4027
  if (sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
4028
    pReq->sql = taosMemoryCalloc(1, sqlLen + 1);
4029 4030 4031 4032
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }

S
Shengliang Guan 已提交
4033 4034
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4035
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4036 4037 4038
  return 0;
}

L
Liu Jicong 已提交
4039
void tFreeSCMCreateTopicReq(SCMCreateTopicReq *pReq) {
wafwerar's avatar
wafwerar 已提交
4040
  taosMemoryFreeClear(pReq->sql);
4041
  if (TOPIC_SUB_TYPE__DB != pReq->subType) {
4042 4043
    taosMemoryFreeClear(pReq->ast);
  }
S
Shengliang Guan 已提交
4044 4045
}

L
Liu Jicong 已提交
4046
int32_t tSerializeSCMCreateTopicRsp(void *buf, int32_t bufLen, const SCMCreateTopicRsp *pRsp) {
H
Hongze Cheng 已提交
4047 4048
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4049 4050 4051 4052 4053 4054

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->topicId) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4055
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4056 4057 4058
  return tlen;
}

L
Liu Jicong 已提交
4059
int32_t tDeserializeSCMCreateTopicRsp(void *buf, int32_t bufLen, SCMCreateTopicRsp *pRsp) {
H
Hongze Cheng 已提交
4060 4061
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4062 4063 4064 4065 4066

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->topicId) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4067
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4068 4069
  return 0;
}
S
Shengliang Guan 已提交
4070 4071

int32_t tSerializeSConnectReq(void *buf, int32_t bufLen, SConnectReq *pReq) {
H
Hongze Cheng 已提交
4072 4073
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4074 4075

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
4076
  if (tEncodeI8(&encoder, pReq->connType) < 0) return -1;
S
Shengliang Guan 已提交
4077 4078 4079
  if (tEncodeI32(&encoder, pReq->pid) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->app) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
4080
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
4081
  if (tEncodeCStrWithLen(&encoder, pReq->passwd, TSDB_PASSWORD_LEN) < 0) return -1;
S
Shengliang Guan 已提交
4082
  if (tEncodeI64(&encoder, pReq->startTime) < 0) return -1;
4083
  if (tEncodeCStr(&encoder, pReq->sVer) < 0) return -1;
S
Shengliang Guan 已提交
4084 4085 4086
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4087
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4088 4089 4090 4091
  return tlen;
}

int32_t tDeserializeSConnectReq(void *buf, int32_t bufLen, SConnectReq *pReq) {
H
Hongze Cheng 已提交
4092 4093
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4094 4095

  if (tStartDecode(&decoder) < 0) return -1;
L
Liu Jicong 已提交
4096
  if (tDecodeI8(&decoder, &pReq->connType) < 0) return -1;
S
Shengliang Guan 已提交
4097 4098 4099
  if (tDecodeI32(&decoder, &pReq->pid) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->app) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
4100 4101
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->passwd) < 0) return -1;
S
Shengliang Guan 已提交
4102
  if (tDecodeI64(&decoder, &pReq->startTime) < 0) return -1;
4103 4104 4105 4106 4107 4108
  // Check the client version from version 3.0.3.0
  if (tDecodeIsEnd(&decoder)) {
    tDecoderClear(&decoder);
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
  }
  if (tDecodeCStrTo(&decoder, pReq->sVer) < 0) return -1;
S
Shengliang Guan 已提交
4109 4110
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4111
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4112 4113 4114 4115
  return 0;
}

int32_t tSerializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) {
H
Hongze Cheng 已提交
4116 4117
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4118 4119 4120 4121

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->acctId) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->clusterId) < 0) return -1;
D
dapan1121 已提交
4122
  if (tEncodeU32(&encoder, pRsp->connId) < 0) return -1;
D
dapan 已提交
4123
  if (tEncodeI32(&encoder, pRsp->dnodeNum) < 0) return -1;
S
Shengliang Guan 已提交
4124
  if (tEncodeI8(&encoder, pRsp->superUser) < 0) return -1;
4125
  if (tEncodeI8(&encoder, pRsp->sysInfo) < 0) return -1;
L
Liu Jicong 已提交
4126
  if (tEncodeI8(&encoder, pRsp->connType) < 0) return -1;
S
Shengliang Guan 已提交
4127
  if (tEncodeSEpSet(&encoder, &pRsp->epSet) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
4128
  if (tEncodeI32(&encoder, pRsp->svrTimestamp) < 0) return -1;
D
dapan1121 已提交
4129 4130
  if (tEncodeCStr(&encoder, pRsp->sVer) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->sDetailVer) < 0) return -1;
4131
  if (tEncodeI32(&encoder, pRsp->passVer) < 0) return -1;
K
kailixu 已提交
4132
  if (tEncodeI32(&encoder, pRsp->authVer) < 0) return -1;
S
Shengliang Guan 已提交
4133 4134 4135
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4136
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4137 4138 4139 4140
  return tlen;
}

int32_t tDeserializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) {
H
Hongze Cheng 已提交
4141 4142
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4143 4144 4145 4146

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->acctId) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->clusterId) < 0) return -1;
D
dapan1121 已提交
4147
  if (tDecodeU32(&decoder, &pRsp->connId) < 0) return -1;
D
dapan 已提交
4148
  if (tDecodeI32(&decoder, &pRsp->dnodeNum) < 0) return -1;
S
Shengliang Guan 已提交
4149
  if (tDecodeI8(&decoder, &pRsp->superUser) < 0) return -1;
4150
  if (tDecodeI8(&decoder, &pRsp->sysInfo) < 0) return -1;
L
Liu Jicong 已提交
4151
  if (tDecodeI8(&decoder, &pRsp->connType) < 0) return -1;
S
Shengliang Guan 已提交
4152
  if (tDecodeSEpSet(&decoder, &pRsp->epSet) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
4153
  if (tDecodeI32(&decoder, &pRsp->svrTimestamp) < 0) return -1;
D
dapan1121 已提交
4154 4155
  if (tDecodeCStrTo(&decoder, pRsp->sVer) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->sDetailVer) < 0) return -1;
4156 4157 4158 4159 4160 4161

  if (!tDecodeIsEnd(&decoder)) {
    if (tDecodeI32(&decoder, &pRsp->passVer) < 0) return -1;
  } else {
    pRsp->passVer = 0;
  }
K
kailixu 已提交
4162 4163 4164 4165
  // since 3.0.7.0
  if (!tDecodeIsEnd(&decoder)) {
    if (tDecodeI32(&decoder, &pRsp->authVer) < 0) return -1;
  } else {
K
kailixu 已提交
4166
    pRsp->authVer = 0;
4167 4168
  }

S
Shengliang Guan 已提交
4169 4170
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4171
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4172 4173
  return 0;
}
S
Shengliang Guan 已提交
4174 4175

int32_t tSerializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) {
H
Hongze Cheng 已提交
4176 4177
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4178 4179 4180 4181 4182 4183

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->reserved) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4184
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4185 4186 4187 4188
  return tlen;
}

int32_t tDeserializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) {
H
Hongze Cheng 已提交
4189 4190
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4191 4192 4193 4194 4195

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->reserved) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4196
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4197 4198
  return 0;
}
S
Shengliang Guan 已提交
4199

4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224
int32_t tSerializeSMStreamTickMsg(void *buf, int32_t bufLen, SMStreamTickReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->tick) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMStreamTickMsg(void *buf, int32_t bufLen, SMStreamTickReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->tick) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

H
Hongze Cheng 已提交
4225
int32_t tEncodeSReplica(SEncoder *pEncoder, SReplica *pReplica) {
S
Shengliang Guan 已提交
4226 4227 4228 4229 4230 4231
  if (tEncodeI32(pEncoder, pReplica->id) < 0) return -1;
  if (tEncodeU16(pEncoder, pReplica->port) < 0) return -1;
  if (tEncodeCStr(pEncoder, pReplica->fqdn) < 0) return -1;
  return 0;
}

H
Hongze Cheng 已提交
4232
int32_t tDecodeSReplica(SDecoder *pDecoder, SReplica *pReplica) {
S
Shengliang Guan 已提交
4233 4234 4235 4236 4237 4238
  if (tDecodeI32(pDecoder, &pReplica->id) < 0) return -1;
  if (tDecodeU16(pDecoder, &pReplica->port) < 0) return -1;
  if (tDecodeCStrTo(pDecoder, pReplica->fqdn) < 0) return -1;
  return 0;
}

S
Shengliang Guan 已提交
4239
int32_t tSerializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) {
H
Hongze Cheng 已提交
4240 4241
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4242 4243 4244 4245

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
L
Liu Jicong 已提交
4246
  if (tEncodeI64(&encoder, pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
4247
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
S
Shengliang Guan 已提交
4248 4249 4250 4251
  if (tEncodeI32(&encoder, pReq->numOfStables) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->buffer) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pageSize) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pages) < 0) return -1;
4252
  if (tEncodeI32(&encoder, pReq->cacheLastSize) < 0) return -1;
S
Shengliang Guan 已提交
4253 4254 4255 4256 4257 4258
  if (tEncodeI32(&encoder, pReq->daysPerFile) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep0) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep1) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep2) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->minRows) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->maxRows) < 0) return -1;
4259
  if (tEncodeI32(&encoder, pReq->walFsyncPeriod) < 0) return -1;
D
dapan1121 已提交
4260 4261 4262
  if (tEncodeU32(&encoder, pReq->hashBegin) < 0) return -1;
  if (tEncodeU32(&encoder, pReq->hashEnd) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->hashMethod) < 0) return -1;
S
Shengliang Guan 已提交
4263 4264 4265
  if (tEncodeI8(&encoder, pReq->walLevel) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->precision) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->compression) < 0) return -1;
S
Shengliang Guan 已提交
4266
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
4267
  if (tEncodeI8(&encoder, pReq->cacheLast) < 0) return -1;
S
Shengliang Guan 已提交
4268 4269 4270 4271
  if (tEncodeI8(&encoder, pReq->replica) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->selfIndex) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) {
    SReplica *pReplica = &pReq->replicas[i];
S
Shengliang Guan 已提交
4272
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
S
Shengliang Guan 已提交
4273
  }
S
sma  
Shengliang Guan 已提交
4274 4275 4276
  if (tEncodeI32(&encoder, pReq->numOfRetensions) < 0) return -1;
  for (int32_t i = 0; i < pReq->numOfRetensions; ++i) {
    SRetention *pRetension = taosArrayGet(pReq->pRetensions, i);
C
Cary Xu 已提交
4277 4278
    if (tEncodeI64(&encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI64(&encoder, pRetension->keep) < 0) return -1;
S
sma  
Shengliang Guan 已提交
4279 4280 4281
    if (tEncodeI8(&encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->keepUnit) < 0) return -1;
  }
S
Shengliang Guan 已提交
4282 4283

  if (tEncodeI8(&encoder, pReq->isTsma) < 0) return -1;
C
Cary Xu 已提交
4284 4285 4286 4287
  if (pReq->isTsma) {
    uint32_t tsmaLen = (uint32_t)(htonl(((SMsgHead *)pReq->pTsma)->contLen));
    if (tEncodeBinary(&encoder, (const uint8_t *)pReq->pTsma, tsmaLen) < 0) return -1;
  }
S
Shengliang Guan 已提交
4288
  if (tEncodeI32(&encoder, pReq->walRetentionPeriod) < 0) return -1;
L
Liu Jicong 已提交
4289
  if (tEncodeI64(&encoder, pReq->walRetentionSize) < 0) return -1;
S
Shengliang Guan 已提交
4290
  if (tEncodeI32(&encoder, pReq->walRollPeriod) < 0) return -1;
L
Liu Jicong 已提交
4291
  if (tEncodeI64(&encoder, pReq->walSegmentSize) < 0) return -1;
4292
  if (tEncodeI16(&encoder, pReq->sstTrigger) < 0) return -1;
4293 4294
  if (tEncodeI16(&encoder, pReq->hashPrefix) < 0) return -1;
  if (tEncodeI16(&encoder, pReq->hashSuffix) < 0) return -1;
4295
  if (tEncodeI32(&encoder, pReq->tsdbPageSize) < 0) return -1;
S
Shengliang Guan 已提交
4296 4297 4298
  for (int32_t i = 0; i < 8; ++i) {
    if (tEncodeI64(&encoder, pReq->reserved[i]) < 0) return -1;
  }
C
cadem 已提交
4299 4300 4301 4302 4303 4304
  if (tEncodeI8(&encoder, pReq->learnerReplica) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->learnerSelfIndex) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_LEARNER_REPLICA; ++i) {
    SReplica *pReplica = &pReq->learnerReplicas[i];
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
  }
C
Cary Xu 已提交
4305

S
Shengliang Guan 已提交
4306 4307 4308
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4309
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4310 4311 4312 4313
  return tlen;
}

int32_t tDeserializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) {
H
Hongze Cheng 已提交
4314 4315
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4316 4317 4318 4319

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
L
Liu Jicong 已提交
4320
  if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
4321
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
S
Shengliang Guan 已提交
4322 4323 4324 4325
  if (tDecodeI32(&decoder, &pReq->numOfStables) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->buffer) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pageSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pages) < 0) return -1;
4326
  if (tDecodeI32(&decoder, &pReq->cacheLastSize) < 0) return -1;
S
Shengliang Guan 已提交
4327 4328 4329 4330 4331 4332
  if (tDecodeI32(&decoder, &pReq->daysPerFile) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep0) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep1) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep2) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->minRows) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->maxRows) < 0) return -1;
4333
  if (tDecodeI32(&decoder, &pReq->walFsyncPeriod) < 0) return -1;
D
dapan1121 已提交
4334 4335 4336
  if (tDecodeU32(&decoder, &pReq->hashBegin) < 0) return -1;
  if (tDecodeU32(&decoder, &pReq->hashEnd) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->hashMethod) < 0) return -1;
S
Shengliang Guan 已提交
4337 4338 4339
  if (tDecodeI8(&decoder, &pReq->walLevel) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->precision) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->compression) < 0) return -1;
S
Shengliang Guan 已提交
4340
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
4341
  if (tDecodeI8(&decoder, &pReq->cacheLast) < 0) return -1;
S
Shengliang Guan 已提交
4342 4343 4344 4345
  if (tDecodeI8(&decoder, &pReq->replica) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->selfIndex) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) {
    SReplica *pReplica = &pReq->replicas[i];
S
Shengliang Guan 已提交
4346
    if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
S
Shengliang Guan 已提交
4347
  }
4348
  if (tDecodeI32(&decoder, &pReq->numOfRetensions) < 0) return -1;
S
sma  
Shengliang Guan 已提交
4349 4350 4351 4352 4353 4354 4355 4356
  pReq->pRetensions = taosArrayInit(pReq->numOfRetensions, sizeof(SRetention));
  if (pReq->pRetensions == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  for (int32_t i = 0; i < pReq->numOfRetensions; ++i) {
    SRetention rentension = {0};
C
Cary Xu 已提交
4357 4358
    if (tDecodeI64(&decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI64(&decoder, &rentension.keep) < 0) return -1;
S
sma  
Shengliang Guan 已提交
4359 4360 4361 4362 4363 4364 4365 4366
    if (tDecodeI8(&decoder, &rentension.freqUnit) < 0) return -1;
    if (tDecodeI8(&decoder, &rentension.keepUnit) < 0) return -1;
    if (taosArrayPush(pReq->pRetensions, &rentension) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

S
Shengliang Guan 已提交
4367
  if (tDecodeI8(&decoder, &pReq->isTsma) < 0) return -1;
C
Cary Xu 已提交
4368
  if (pReq->isTsma) {
C
Cary Xu 已提交
4369
    if (tDecodeBinary(&decoder, (uint8_t **)&pReq->pTsma, NULL) < 0) return -1;
C
Cary Xu 已提交
4370
  }
S
Shengliang Guan 已提交
4371

S
Shengliang Guan 已提交
4372
  if (tDecodeI32(&decoder, &pReq->walRetentionPeriod) < 0) return -1;
L
Liu Jicong 已提交
4373
  if (tDecodeI64(&decoder, &pReq->walRetentionSize) < 0) return -1;
S
Shengliang Guan 已提交
4374
  if (tDecodeI32(&decoder, &pReq->walRollPeriod) < 0) return -1;
L
Liu Jicong 已提交
4375
  if (tDecodeI64(&decoder, &pReq->walSegmentSize) < 0) return -1;
4376
  if (tDecodeI16(&decoder, &pReq->sstTrigger) < 0) return -1;
4377 4378
  if (tDecodeI16(&decoder, &pReq->hashPrefix) < 0) return -1;
  if (tDecodeI16(&decoder, &pReq->hashSuffix) < 0) return -1;
4379
  if (tDecodeI32(&decoder, &pReq->tsdbPageSize) < 0) return -1;
S
Shengliang Guan 已提交
4380 4381 4382
  for (int32_t i = 0; i < 8; ++i) {
    if (tDecodeI64(&decoder, &pReq->reserved[i]) < 0) return -1;
  }
C
cadem 已提交
4383
  if (!tDecodeIsEnd(&decoder)) {
C
cadem 已提交
4384 4385 4386 4387 4388 4389
    if (tDecodeI8(&decoder, &pReq->learnerReplica) < 0) return -1;
    if (tDecodeI8(&decoder, &pReq->learnerSelfIndex) < 0) return -1;
    for (int32_t i = 0; i < TSDB_MAX_LEARNER_REPLICA; ++i) {
      SReplica *pReplica = &pReq->learnerReplicas[i];
      if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
    }
C
cadem 已提交
4390
  }
S
Shengliang Guan 已提交
4391

S
Shengliang Guan 已提交
4392
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
4393
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4394 4395 4396
  return 0;
}

S
sma  
Shengliang Guan 已提交
4397 4398 4399
int32_t tFreeSCreateVnodeReq(SCreateVnodeReq *pReq) {
  taosArrayDestroy(pReq->pRetensions);
  pReq->pRetensions = NULL;
4400
  return 0;
S
sma  
Shengliang Guan 已提交
4401 4402
}

S
Shengliang Guan 已提交
4403
int32_t tSerializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) {
H
Hongze Cheng 已提交
4404 4405
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4406 4407 4408 4409

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1;
L
Liu Jicong 已提交
4410
  if (tEncodeI64(&encoder, pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
4411
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
S
Shengliang Guan 已提交
4412 4413 4414
  for (int32_t i = 0; i < 8; ++i) {
    if (tEncodeI64(&encoder, pReq->reserved[i]) < 0) return -1;
  }
S
Shengliang Guan 已提交
4415 4416 4417
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4418
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4419 4420 4421 4422
  return tlen;
}

int32_t tDeserializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) {
H
Hongze Cheng 已提交
4423 4424
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4425 4426 4427 4428

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1;
L
Liu Jicong 已提交
4429
  if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
4430
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
S
Shengliang Guan 已提交
4431 4432 4433
  for (int32_t i = 0; i < 8; ++i) {
    if (tDecodeI64(&decoder, &pReq->reserved[i]) < 0) return -1;
  }
S
Shengliang Guan 已提交
4434 4435
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4436
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4437 4438
  return 0;
}
dengyihao's avatar
dengyihao 已提交
4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476
int32_t tSerializeSDropIdxReq(void *buf, int32_t bufLen, SDropIndexReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->colName) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->stb) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->stbUid) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->dbUid) < 0) return -1;
  for (int32_t i = 0; i < 8; ++i) {
    if (tEncodeI64(&encoder, pReq->reserved[i]) < 0) return -1;
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
  // TODO
  return 0;
}
int32_t tDeserializeSDropIdxReq(void *buf, int32_t bufLen, SDropIndexReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->colName) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->stb) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->stbUid) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1;
  for (int32_t i = 0; i < 8; ++i) {
    if (tDecodeI64(&decoder, &pReq->reserved[i]) < 0) return -1;
  }
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  // TODO
  return 0;
}
S
Shengliang Guan 已提交
4477

S
Shengliang Guan 已提交
4478
int32_t tSerializeSCompactVnodeReq(void *buf, int32_t bufLen, SCompactVnodeReq *pReq) {
H
Hongze Cheng 已提交
4479 4480
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4481 4482 4483 4484

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->dbUid) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
4485
  if (tEncodeI64(&encoder, pReq->compactStartTime) < 0) return -1;
H
Hongze Cheng 已提交
4486 4487 4488 4489 4490

  // 1.1 add tw.skey and tw.ekey
  if (tEncodeI64(&encoder, pReq->tw.skey) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->tw.ekey) < 0) return -1;

S
Shengliang Guan 已提交
4491 4492 4493
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4494
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4495 4496 4497 4498
  return tlen;
}

int32_t tDeserializeSCompactVnodeReq(void *buf, int32_t bufLen, SCompactVnodeReq *pReq) {
H
Hongze Cheng 已提交
4499 4500
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4501 4502

  if (tStartDecode(&decoder) < 0) return -1;
H
Hongze Cheng 已提交
4503

S
Shengliang Guan 已提交
4504 4505
  if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
4506
  if (tDecodeI64(&decoder, &pReq->compactStartTime) < 0) return -1;
S
Shengliang Guan 已提交
4507

H
Hongze Cheng 已提交
4508 4509 4510 4511 4512 4513 4514 4515 4516 4517
  // 1.1
  if (tDecodeIsEnd(&decoder)) {
    pReq->tw.skey = TSKEY_MIN;
    pReq->tw.ekey = TSKEY_MAX;
  } else {
    if (tDecodeI64(&decoder, &pReq->tw.skey) < 0) return -1;
    if (tDecodeI64(&decoder, &pReq->tw.ekey) < 0) return -1;
  }

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
4518
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4519 4520 4521
  return 0;
}

S
Shengliang Guan 已提交
4522
int32_t tSerializeSAlterVnodeConfigReq(void *buf, int32_t bufLen, SAlterVnodeConfigReq *pReq) {
H
Hongze Cheng 已提交
4523 4524
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4525 4526 4527

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
S
Shengliang Guan 已提交
4528 4529 4530
  if (tEncodeI32(&encoder, pReq->buffer) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pageSize) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pages) < 0) return -1;
4531
  if (tEncodeI32(&encoder, pReq->cacheLastSize) < 0) return -1;
4532
  if (tEncodeI32(&encoder, pReq->daysPerFile) < 0) return -1;
S
Shengliang Guan 已提交
4533 4534 4535
  if (tEncodeI32(&encoder, pReq->daysToKeep0) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep1) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep2) < 0) return -1;
4536
  if (tEncodeI32(&encoder, pReq->walFsyncPeriod) < 0) return -1;
S
Shengliang Guan 已提交
4537 4538
  if (tEncodeI8(&encoder, pReq->walLevel) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
4539
  if (tEncodeI8(&encoder, pReq->cacheLast) < 0) return -1;
S
Shengliang Guan 已提交
4540 4541
  for (int32_t i = 0; i < 8; ++i) {
    if (tEncodeI64(&encoder, pReq->reserved[i]) < 0) return -1;
S
Shengliang Guan 已提交
4542
  }
4543

H
refact  
Hongze Cheng 已提交
4544
  // 1st modification
4545 4546
  if (tEncodeI16(&encoder, pReq->sttTrigger) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->minRows) < 0) return -1;
4547 4548 4549
  // 2nd modification
  if (tEncodeI32(&encoder, pReq->walRetentionPeriod) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->walRetentionSize) < 0) return -1;
S
Shengliang Guan 已提交
4550 4551 4552
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4553
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4554 4555 4556
  return tlen;
}

S
Shengliang Guan 已提交
4557
int32_t tDeserializeSAlterVnodeConfigReq(void *buf, int32_t bufLen, SAlterVnodeConfigReq *pReq) {
H
Hongze Cheng 已提交
4558 4559
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4560 4561 4562

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
S
Shengliang Guan 已提交
4563 4564 4565
  if (tDecodeI32(&decoder, &pReq->buffer) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pageSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pages) < 0) return -1;
4566
  if (tDecodeI32(&decoder, &pReq->cacheLastSize) < 0) return -1;
S
Shengliang Guan 已提交
4567
  if (tDecodeI32(&decoder, &pReq->daysPerFile) < 0) return -1;
S
Shengliang Guan 已提交
4568 4569 4570
  if (tDecodeI32(&decoder, &pReq->daysToKeep0) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep1) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep2) < 0) return -1;
4571
  if (tDecodeI32(&decoder, &pReq->walFsyncPeriod) < 0) return -1;
S
Shengliang Guan 已提交
4572 4573
  if (tDecodeI8(&decoder, &pReq->walLevel) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
4574
  if (tDecodeI8(&decoder, &pReq->cacheLast) < 0) return -1;
S
Shengliang Guan 已提交
4575 4576 4577
  for (int32_t i = 0; i < 8; ++i) {
    if (tDecodeI64(&decoder, &pReq->reserved[i]) < 0) return -1;
  }
4578

H
refact  
Hongze Cheng 已提交
4579
  // 1st modification
4580 4581 4582 4583 4584 4585 4586
  if (tDecodeIsEnd(&decoder)) {
    pReq->sttTrigger = -1;
    pReq->minRows = -1;
  } else {
    if (tDecodeI16(&decoder, &pReq->sttTrigger) < 0) return -1;
    if (tDecodeI32(&decoder, &pReq->minRows) < 0) return -1;
  }
S
Shengliang Guan 已提交
4587

4588 4589 4590 4591 4592 4593 4594 4595
  // 2n modification
  if (tDecodeIsEnd(&decoder)) {
    pReq->walRetentionPeriod = -1;
    pReq->walRetentionSize = -1;
  } else {
    if (tDecodeI32(&decoder, &pReq->walRetentionPeriod) < 0) return -1;
    if (tDecodeI32(&decoder, &pReq->walRetentionSize) < 0) return -1;
  }
S
Shengliang Guan 已提交
4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616
  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

int32_t tSerializeSAlterVnodeReplicaReq(void *buf, int32_t bufLen, SAlterVnodeReplicaReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->selfIndex) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->replica) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) {
    SReplica *pReplica = &pReq->replicas[i];
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
  }
  for (int32_t i = 0; i < 8; ++i) {
    if (tEncodeI64(&encoder, pReq->reserved[i]) < 0) return -1;
  }
C
cadem 已提交
4617 4618 4619 4620 4621 4622
  if (tEncodeI8(&encoder, pReq->learnerSelfIndex) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->learnerReplica) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_LEARNER_REPLICA; ++i) {
    SReplica *pReplica = &pReq->learnerReplicas[i];
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
  }
S
Shengliang Guan 已提交
4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSAlterVnodeReplicaReq(void *buf, int32_t bufLen, SAlterVnodeReplicaReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
4637
  if (tDecodeI8(&decoder, &pReq->selfIndex) < 0) return -1;
4638
  if (tDecodeI8(&decoder, &pReq->replica) < 0) return -1;
S
Shengliang Guan 已提交
4639 4640 4641 4642
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) {
    SReplica *pReplica = &pReq->replicas[i];
    if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
  }
S
Shengliang Guan 已提交
4643 4644 4645
  for (int32_t i = 0; i < 8; ++i) {
    if (tDecodeI64(&decoder, &pReq->reserved[i]) < 0) return -1;
  }
C
cadem 已提交
4646
  if (!tDecodeIsEnd(&decoder)) {
C
cadem 已提交
4647 4648 4649 4650 4651 4652
    if (tDecodeI8(&decoder, &pReq->learnerSelfIndex) < 0) return -1;
    if (tDecodeI8(&decoder, &pReq->learnerReplica) < 0) return -1;
    for (int32_t i = 0; i < TSDB_MAX_LEARNER_REPLICA; ++i) {
      SReplica *pReplica = &pReq->learnerReplicas[i];
      if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
    }
C
cadem 已提交
4653
  }
4654

S
Shengliang Guan 已提交
4655
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
4656
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4657 4658 4659
  return 0;
}

4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687
int32_t tSerializeSDisableVnodeWriteReq(void *buf, int32_t bufLen, SDisableVnodeWriteReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->disable) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSDisableVnodeWriteReq(void *buf, int32_t bufLen, SDisableVnodeWriteReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->disable) < 0) return -1;

  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721
int32_t tSerializeSAlterVnodeHashRangeReq(void *buf, int32_t bufLen, SAlterVnodeHashRangeReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->srcVgId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dstVgId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->hashBegin) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->hashEnd) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->reserved) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSAlterVnodeHashRangeReq(void *buf, int32_t bufLen, SAlterVnodeHashRangeReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->srcVgId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dstVgId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->hashBegin) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->hashEnd) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->reserved) < 0) return -1;

  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
4722
int32_t tSerializeSKillQueryReq(void *buf, int32_t bufLen, SKillQueryReq *pReq) {
H
Hongze Cheng 已提交
4723 4724
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4725 4726

  if (tStartEncode(&encoder) < 0) return -1;
X
Xiaoyu Wang 已提交
4727
  if (tEncodeCStr(&encoder, pReq->queryStrId) < 0) return -1;
S
Shengliang Guan 已提交
4728 4729 4730
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4731
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4732 4733 4734 4735
  return tlen;
}

int32_t tDeserializeSKillQueryReq(void *buf, int32_t bufLen, SKillQueryReq *pReq) {
H
Hongze Cheng 已提交
4736 4737
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4738 4739

  if (tStartDecode(&decoder) < 0) return -1;
X
Xiaoyu Wang 已提交
4740
  if (tDecodeCStrTo(&decoder, pReq->queryStrId) < 0) return -1;
S
Shengliang Guan 已提交
4741 4742
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4743
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4744 4745 4746 4747
  return 0;
}

int32_t tSerializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) {
H
Hongze Cheng 已提交
4748 4749
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4750 4751

  if (tStartEncode(&encoder) < 0) return -1;
D
dapan1121 已提交
4752
  if (tEncodeU32(&encoder, pReq->connId) < 0) return -1;
S
Shengliang Guan 已提交
4753 4754 4755
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4756
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4757 4758 4759 4760
  return tlen;
}

int32_t tDeserializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) {
H
Hongze Cheng 已提交
4761 4762
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4763 4764

  if (tStartDecode(&decoder) < 0) return -1;
D
dapan1121 已提交
4765
  if (tDecodeU32(&decoder, &pReq->connId) < 0) return -1;
S
Shengliang Guan 已提交
4766 4767
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4768
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4769
  return 0;
S
Shengliang Guan 已提交
4770 4771
}

S
Shengliang Guan 已提交
4772
int32_t tSerializeSKillTransReq(void *buf, int32_t bufLen, SKillTransReq *pReq) {
H
Hongze Cheng 已提交
4773 4774
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4775 4776 4777 4778 4779 4780

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->transId) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4781
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4782 4783 4784 4785
  return tlen;
}

int32_t tDeserializeSKillTransReq(void *buf, int32_t bufLen, SKillTransReq *pReq) {
H
Hongze Cheng 已提交
4786 4787
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4788 4789 4790 4791 4792

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->transId) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4793
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4794 4795 4796
  return 0;
}

X
Xiaoyu Wang 已提交
4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821
int32_t tSerializeSBalanceVgroupReq(void *buf, int32_t bufLen, SBalanceVgroupReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->useless) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSBalanceVgroupReq(void *buf, int32_t bufLen, SBalanceVgroupReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->useless) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

C
cadem 已提交
4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
int32_t tSerializeSBalanceVgroupLeaderReq(void *buf, int32_t bufLen, SBalanceVgroupLeaderReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->useless) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSBalanceVgroupLeaderReq(void *buf, int32_t bufLen, SBalanceVgroupLeaderReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->useless) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

X
Xiaoyu Wang 已提交
4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904
int32_t tSerializeSMergeVgroupReq(void *buf, int32_t bufLen, SMergeVgroupReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId1) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId2) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMergeVgroupReq(void *buf, int32_t bufLen, SMergeVgroupReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId1) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId2) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

int32_t tSerializeSRedistributeVgroupReq(void *buf, int32_t bufLen, SRedistributeVgroupReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId1) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId2) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId3) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSRedistributeVgroupReq(void *buf, int32_t bufLen, SRedistributeVgroupReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId1) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId2) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId3) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929
int32_t tSerializeSSplitVgroupReq(void *buf, int32_t bufLen, SSplitVgroupReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSSplitVgroupReq(void *buf, int32_t bufLen, SSplitVgroupReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

C
cadem 已提交
4930
int32_t tSerializeSForceBecomeFollowerReq(void *buf, int32_t bufLen, SForceBecomeFollowerReq *pReq) {
C
cadem 已提交
4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

C
cadem 已提交
4943
int32_t tDeserializeSForceBecomeFollowerReq(void *buf, int32_t bufLen, SForceBecomeFollowerReq *pReq) {
C
cadem 已提交
4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
4955
int32_t tSerializeSDCreateMnodeReq(void *buf, int32_t bufLen, SDCreateMnodeReq *pReq) {
H
Hongze Cheng 已提交
4956 4957
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
4958 4959 4960 4961 4962 4963 4964

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->replica) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) {
    SReplica *pReplica = &pReq->replicas[i];
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
  }
C
cadem 已提交
4965 4966 4967 4968 4969 4970
  if (tEncodeI8(&encoder, pReq->learnerReplica) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_LEARNER_REPLICA; ++i) {
    SReplica *pReplica = &pReq->learnerReplicas[i];
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
  }
  if (tEncodeI64(&encoder, pReq->lastIndex) < 0) return -1;
S
Shengliang Guan 已提交
4971 4972 4973
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
4974
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
4975 4976 4977 4978
  return tlen;
}

int32_t tDeserializeSDCreateMnodeReq(void *buf, int32_t bufLen, SDCreateMnodeReq *pReq) {
H
Hongze Cheng 已提交
4979 4980
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
4981 4982 4983 4984 4985 4986 4987

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->replica) < 0) return -1;
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) {
    SReplica *pReplica = &pReq->replicas[i];
    if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
  }
C
cadem 已提交
4988
  if (!tDecodeIsEnd(&decoder)) {
C
cadem 已提交
4989 4990 4991 4992 4993 4994
    if (tDecodeI8(&decoder, &pReq->learnerReplica) < 0) return -1;
    for (int32_t i = 0; i < TSDB_MAX_LEARNER_REPLICA; ++i) {
      SReplica *pReplica = &pReq->learnerReplicas[i];
      if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
    }
    if (tDecodeI64(&decoder, &pReq->lastIndex) < 0) return -1;
C
cadem 已提交
4995
  }
S
Shengliang Guan 已提交
4996 4997
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
4998
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
4999 5000 5001 5002
  return 0;
}

int32_t tSerializeSAuthReq(void *buf, int32_t bufLen, SAuthReq *pReq) {
H
Hongze Cheng 已提交
5003 5004
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
5005 5006 5007 5008 5009 5010 5011 5012 5013 5014

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->spi) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->encrypt) < 0) return -1;
  if (tEncodeBinary(&encoder, pReq->secret, TSDB_PASSWORD_LEN) < 0) return -1;
  if (tEncodeBinary(&encoder, pReq->ckey, TSDB_PASSWORD_LEN) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
5015
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
5016 5017 5018 5019
  return tlen;
}

int32_t tDeserializeSAuthReq(void *buf, int32_t bufLen, SAuthReq *pReq) {
H
Hongze Cheng 已提交
5020 5021
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
5022 5023 5024 5025 5026 5027 5028 5029 5030

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->spi) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->encrypt) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->secret) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->ckey) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
5031
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
5032 5033
  return 0;
}
L
Liu Jicong 已提交
5034

S
Shengliang Guan 已提交
5035
int32_t tSerializeSServerStatusRsp(void *buf, int32_t bufLen, SServerStatusRsp *pRsp) {
H
Hongze Cheng 已提交
5036 5037
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
5038 5039 5040

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->statusCode) < 0) return -1;
S
Shengliang Guan 已提交
5041
  if (tEncodeCStr(&encoder, pRsp->details) < 0) return -1;
S
Shengliang Guan 已提交
5042 5043 5044 5045

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
5046
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
5047 5048 5049 5050
  return tlen;
}

int32_t tDeserializeSServerStatusRsp(void *buf, int32_t bufLen, SServerStatusRsp *pRsp) {
H
Hongze Cheng 已提交
5051 5052
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
5053 5054 5055

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->statusCode) < 0) return -1;
S
Shengliang Guan 已提交
5056
  if (tDecodeCStrTo(&decoder, pRsp->details) < 0) return -1;
S
Shengliang Guan 已提交
5057 5058

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
5059
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
5060 5061
  return 0;
}
H
Hongze Cheng 已提交
5062
int32_t tEncodeSMqOffset(SEncoder *encoder, const SMqOffset *pOffset) {
L
Liu Jicong 已提交
5063 5064 5065 5066 5067 5068 5069
  if (tEncodeI32(encoder, pOffset->vgId) < 0) return -1;
  if (tEncodeI64(encoder, pOffset->offset) < 0) return -1;
  if (tEncodeCStr(encoder, pOffset->topicName) < 0) return -1;
  if (tEncodeCStr(encoder, pOffset->cgroup) < 0) return -1;
  return encoder->pos;
}

H
Hongze Cheng 已提交
5070
int32_t tDecodeSMqOffset(SDecoder *decoder, SMqOffset *pOffset) {
L
Liu Jicong 已提交
5071 5072 5073 5074 5075 5076 5077
  if (tDecodeI32(decoder, &pOffset->vgId) < 0) return -1;
  if (tDecodeI64(decoder, &pOffset->offset) < 0) return -1;
  if (tDecodeCStrTo(decoder, pOffset->topicName) < 0) return -1;
  if (tDecodeCStrTo(decoder, pOffset->cgroup) < 0) return -1;
  return 0;
}

H
Hongze Cheng 已提交
5078
int32_t tEncodeSMqCMCommitOffsetReq(SEncoder *encoder, const SMqCMCommitOffsetReq *pReq) {
L
Liu Jicong 已提交
5079 5080 5081 5082 5083 5084 5085 5086 5087
  if (tStartEncode(encoder) < 0) return -1;
  if (tEncodeI32(encoder, pReq->num) < 0) return -1;
  for (int32_t i = 0; i < pReq->num; i++) {
    tEncodeSMqOffset(encoder, &pReq->offsets[i]);
  }
  tEndEncode(encoder);
  return encoder->pos;
}

H
Hongze Cheng 已提交
5088
int32_t tDecodeSMqCMCommitOffsetReq(SDecoder *decoder, SMqCMCommitOffsetReq *pReq) {
L
Liu Jicong 已提交
5089
  if (tStartDecode(decoder) < 0) return -1;
L
Liu Jicong 已提交
5090
  if (tDecodeI32(decoder, &pReq->num) < 0) return -1;
H
Hongze Cheng 已提交
5091
  pReq->offsets = (SMqOffset *)tDecoderMalloc(decoder, sizeof(SMqOffset) * pReq->num);
L
Liu Jicong 已提交
5092 5093 5094 5095
  if (pReq->offsets == NULL) return -1;
  for (int32_t i = 0; i < pReq->num; i++) {
    tDecodeSMqOffset(decoder, &pReq->offsets[i]);
  }
L
Liu Jicong 已提交
5096
  tEndDecode(decoder);
L
Liu Jicong 已提交
5097 5098
  return 0;
}
dengyihao's avatar
dengyihao 已提交
5099
int32_t tSerializeSExplainRsp(void *buf, int32_t bufLen, SExplainRsp *pRsp) {
H
Hongze Cheng 已提交
5100 5101
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
5102 5103 5104 5105 5106

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->numOfPlans) < 0) return -1;
  for (int32_t i = 0; i < pRsp->numOfPlans; ++i) {
    SExplainExecInfo *info = &pRsp->subplanInfo[i];
5107 5108
    if (tEncodeDouble(&encoder, info->startupCost) < 0) return -1;
    if (tEncodeDouble(&encoder, info->totalCost) < 0) return -1;
D
dapan1121 已提交
5109
    if (tEncodeU64(&encoder, info->numOfRows) < 0) return -1;
5110 5111
    if (tEncodeU32(&encoder, info->verboseLen) < 0) return -1;
    if (tEncodeBinary(&encoder, info->verboseInfo, info->verboseLen) < 0) return -1;
D
dapan1121 已提交
5112 5113 5114 5115 5116
  }

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
5117
  tEncoderClear(&encoder);
D
dapan1121 已提交
5118 5119 5120
  return tlen;
}

dengyihao's avatar
dengyihao 已提交
5121
int32_t tDeserializeSExplainRsp(void *buf, int32_t bufLen, SExplainRsp *pRsp) {
H
Hongze Cheng 已提交
5122 5123
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
5124 5125 5126 5127

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->numOfPlans) < 0) return -1;
  if (pRsp->numOfPlans > 0) {
5128
    pRsp->subplanInfo = taosMemoryCalloc(pRsp->numOfPlans, sizeof(SExplainExecInfo));
D
dapan1121 已提交
5129 5130 5131
    if (pRsp->subplanInfo == NULL) return -1;
  }
  for (int32_t i = 0; i < pRsp->numOfPlans; ++i) {
5132 5133
    if (tDecodeDouble(&decoder, &pRsp->subplanInfo[i].startupCost) < 0) return -1;
    if (tDecodeDouble(&decoder, &pRsp->subplanInfo[i].totalCost) < 0) return -1;
D
dapan1121 已提交
5134
    if (tDecodeU64(&decoder, &pRsp->subplanInfo[i].numOfRows) < 0) return -1;
5135
    if (tDecodeU32(&decoder, &pRsp->subplanInfo[i].verboseLen) < 0) return -1;
5136
    if (tDecodeBinaryAlloc(&decoder, &pRsp->subplanInfo[i].verboseInfo, NULL) < 0) return -1;
D
dapan1121 已提交
5137 5138 5139 5140
  }

  tEndDecode(&decoder);

H
Hongze Cheng 已提交
5141
  tDecoderClear(&decoder);
D
dapan1121 已提交
5142 5143 5144
  return 0;
}

5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157
void tFreeSExplainRsp(SExplainRsp *pRsp) {
  if (NULL == pRsp) {
    return;
  }

  for (int32_t i = 0; i < pRsp->numOfPlans; ++i) {
    SExplainExecInfo *pExec = pRsp->subplanInfo + i;
    taosMemoryFree(pExec->verboseInfo);
  }

  taosMemoryFreeClear(pRsp->subplanInfo);
}

D
dapan1121 已提交
5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209
int32_t tSerializeSBatchReq(void *buf, int32_t bufLen, SBatchReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  int32_t num = taosArrayGetSize(pReq->pMsgs);
  if (tEncodeI32(&encoder, num) < 0) return -1;
  for (int32_t i = 0; i < num; ++i) {
    SBatchMsg *pMsg = taosArrayGet(pReq->pMsgs, i);
    if (tEncodeI32(&encoder, pMsg->msgIdx) < 0) return -1;
    if (tEncodeI32(&encoder, pMsg->msgType) < 0) return -1;
    if (tEncodeI32(&encoder, pMsg->msgLen) < 0) return -1;
    if (tEncodeBinary(&encoder, pMsg->msg, pMsg->msgLen) < 0) return -1;
  }

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->header.vgId);
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
}

int32_t tDeserializeSBatchReq(void *buf, int32_t bufLen, SBatchReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);

  SMsgHead *pHead = buf;
  pHead->vgId = pReq->header.vgId;
  pHead->contLen = pReq->header.contLen;

  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);

  if (tStartDecode(&decoder) < 0) return -1;

  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
  if (num <= 0) {
    pReq->pMsgs = NULL;
    tEndDecode(&decoder);
L
Liu Jicong 已提交
5210

D
dapan1121 已提交
5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221
    tDecoderClear(&decoder);
    return 0;
  }

  pReq->pMsgs = taosArrayInit(num, sizeof(SBatchMsg));
  if (NULL == pReq->pMsgs) return -1;
  for (int32_t i = 0; i < num; ++i) {
    SBatchMsg msg = {0};
    if (tDecodeI32(&decoder, &msg.msgIdx) < 0) return -1;
    if (tDecodeI32(&decoder, &msg.msgType) < 0) return -1;
    if (tDecodeI32(&decoder, &msg.msgLen) < 0) return -1;
D
dapan1121 已提交
5222
    if (tDecodeBinaryAlloc(&decoder, &msg.msg, NULL) < 0) return -1;
D
dapan1121 已提交
5223 5224
    if (NULL == taosArrayPush(pReq->pMsgs, &msg)) return -1;
  }
L
Liu Jicong 已提交
5225

D
dapan1121 已提交
5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

int32_t tSerializeSBatchRsp(void *buf, int32_t bufLen, SBatchRsp *pRsp) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  int32_t num = taosArrayGetSize(pRsp->pRsps);
  if (tEncodeI32(&encoder, num) < 0) return -1;
  for (int32_t i = 0; i < num; ++i) {
    SBatchRspMsg *pMsg = taosArrayGet(pRsp->pRsps, i);
    if (tEncodeI32(&encoder, pMsg->reqType) < 0) return -1;
    if (tEncodeI32(&encoder, pMsg->msgIdx) < 0) return -1;
    if (tEncodeI32(&encoder, pMsg->msgLen) < 0) return -1;
    if (tEncodeI32(&encoder, pMsg->rspCode) < 0) return -1;
    if (tEncodeBinary(&encoder, pMsg->msg, pMsg->msgLen) < 0) return -1;
  }

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  return tlen;
}

int32_t tDeserializeSBatchRsp(void *buf, int32_t bufLen, SBatchRsp *pRsp) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;

  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
  if (num <= 0) {
    pRsp->pRsps = NULL;
    tEndDecode(&decoder);
L
Liu Jicong 已提交
5267

D
dapan1121 已提交
5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282
    tDecoderClear(&decoder);
    return 0;
  }

  pRsp->pRsps = taosArrayInit(num, sizeof(SBatchRspMsg));
  if (NULL == pRsp->pRsps) return -1;
  for (int32_t i = 0; i < num; ++i) {
    SBatchRspMsg msg = {0};
    if (tDecodeI32(&decoder, &msg.reqType) < 0) return -1;
    if (tDecodeI32(&decoder, &msg.msgIdx) < 0) return -1;
    if (tDecodeI32(&decoder, &msg.msgLen) < 0) return -1;
    if (tDecodeI32(&decoder, &msg.rspCode) < 0) return -1;
    if (tDecodeBinaryAlloc(&decoder, &msg.msg, NULL) < 0) return -1;
    if (NULL == taosArrayPush(pRsp->pRsps, &msg)) return -1;
  }
L
Liu Jicong 已提交
5283

D
dapan1121 已提交
5284 5285 5286 5287 5288 5289
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315
int32_t tSerializeSMqAskEpReq(void *buf, int32_t bufLen, SMqAskEpReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  if (tEncodeI64(&encoder, pReq->consumerId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->epoch) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->cgroup) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  return tlen;
}

int32_t tDeserializeSMqAskEpReq(void *buf, int32_t bufLen, SMqAskEpReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDecodeI64(&decoder, &pReq->consumerId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->epoch) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->cgroup) < 0) return -1;
L
Liu Jicong 已提交
5316

D
dapan1121 已提交
5317 5318 5319 5320 5321 5322
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

Y
yihaoDeng 已提交
5323 5324 5325 5326
int32_t tDeatroySMqHbReq(SMqHbReq *pReq) {
  for (int i = 0; i < taosArrayGetSize(pReq->topics); i++) {
    TopicOffsetRows *vgs = taosArrayGet(pReq->topics, i);
    if (vgs) taosArrayDestroy(vgs->offsetRows);
5327 5328 5329 5330 5331
  }
  taosArrayDestroy(pReq->topics);
  return 0;
}

D
dapan1121 已提交
5332 5333 5334 5335 5336 5337 5338 5339
int32_t tSerializeSMqHbReq(void *buf, int32_t bufLen, SMqHbReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  if (tEncodeI64(&encoder, pReq->consumerId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->epoch) < 0) return -1;

5340 5341 5342
  int32_t sz = taosArrayGetSize(pReq->topics);
  if (tEncodeI32(&encoder, sz) < 0) return -1;
  for (int32_t i = 0; i < sz; ++i) {
Y
yihaoDeng 已提交
5343
    TopicOffsetRows *vgs = (TopicOffsetRows *)taosArrayGet(pReq->topics, i);
5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
    if (tEncodeCStr(&encoder, vgs->topicName) < 0) return -1;
    int32_t szVgs = taosArrayGetSize(vgs->offsetRows);
    if (tEncodeI32(&encoder, szVgs) < 0) return -1;
    for (int32_t j = 0; j < szVgs; ++j) {
      OffsetRows *offRows = taosArrayGet(vgs->offsetRows, j);
      if (tEncodeI32(&encoder, offRows->vgId) < 0) return -1;
      if (tEncodeI64(&encoder, offRows->rows) < 0) return -1;
      if (tEncodeSTqOffsetVal(&encoder, &offRows->offset) < 0) return -1;
    }
  }

D
dapan1121 已提交
5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  return tlen;
}

int32_t tDeserializeSMqHbReq(void *buf, int32_t bufLen, SMqHbReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDecodeI64(&decoder, &pReq->consumerId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->epoch) < 0) return -1;
5371 5372
  int32_t sz = 0;
  if (tDecodeI32(&decoder, &sz) < 0) return -1;
Y
yihaoDeng 已提交
5373
  if (sz > 0) {
5374 5375 5376
    pReq->topics = taosArrayInit(sz, sizeof(TopicOffsetRows));
    if (NULL == pReq->topics) return -1;
    for (int32_t i = 0; i < sz; ++i) {
Y
yihaoDeng 已提交
5377
      TopicOffsetRows *data = taosArrayReserve(pReq->topics, 1);
5378 5379 5380
      tDecodeCStrTo(&decoder, data->topicName);
      int32_t szVgs = 0;
      if (tDecodeI32(&decoder, &szVgs) < 0) return -1;
Y
yihaoDeng 已提交
5381
      if (szVgs > 0) {
5382 5383
        data->offsetRows = taosArrayInit(szVgs, sizeof(OffsetRows));
        if (NULL == data->offsetRows) return -1;
Y
yihaoDeng 已提交
5384 5385
        for (int32_t j = 0; j < szVgs; ++j) {
          OffsetRows *offRows = taosArrayReserve(data->offsetRows, 1);
5386 5387 5388 5389 5390 5391 5392
          if (tDecodeI32(&decoder, &offRows->vgId) < 0) return -1;
          if (tDecodeI64(&decoder, &offRows->rows) < 0) return -1;
          if (tDecodeSTqOffsetVal(&decoder, &offRows->offset) < 0) return -1;
        }
      }
    }
  }
D
dapan1121 已提交
5393 5394 5395 5396 5397 5398
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439
int32_t tSerializeSMqSeekReq(void *buf, int32_t bufLen, SMqSeekReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->consumerId) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->subKey) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->head.vgId);
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
}

int32_t tDeserializeSMqSeekReq(void *buf, int32_t bufLen, SMqSeekReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);

  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->consumerId) < 0) return -1;
  tDecodeCStrTo(&decoder, pReq->subKey);

  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462
int32_t tSerializeSSubQueryMsg(void *buf, int32_t bufLen, SSubQueryMsg *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  if (tEncodeU64(&encoder, pReq->sId) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->queryId) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->taskId) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->refId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->execId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->msgMask) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->taskType) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->explain) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->needFetch) < 0) return -1;
  if (tEncodeU32(&encoder, pReq->sqlLen) < 0) return -1;
  if (tEncodeCStrWithLen(&encoder, pReq->sql, pReq->sqlLen) < 0) return -1;
  if (tEncodeU32(&encoder, pReq->msgLen) < 0) return -1;
L
Liu Jicong 已提交
5463
  if (tEncodeBinary(&encoder, (uint8_t *)pReq->msg, pReq->msgLen) < 0) return -1;
D
dapan1121 已提交
5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->header.vgId);
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
}

int32_t tDeserializeSSubQueryMsg(void *buf, int32_t bufLen, SSubQueryMsg *pReq) {
  int32_t headLen = sizeof(SMsgHead);

  SMsgHead *pHead = buf;
  pHead->vgId = pReq->header.vgId;
  pHead->contLen = pReq->header.contLen;

  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDecodeU64(&decoder, &pReq->sId) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->queryId) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->taskId) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->refId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->execId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->msgMask) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->taskType) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->explain) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->needFetch) < 0) return -1;
  if (tDecodeU32(&decoder, &pReq->sqlLen) < 0) return -1;
  if (tDecodeCStrAlloc(&decoder, &pReq->sql) < 0) return -1;
  if (tDecodeU32(&decoder, &pReq->msgLen) < 0) return -1;
L
Liu Jicong 已提交
5503 5504
  if (tDecodeBinaryAlloc(&decoder, (void **)&pReq->msg, NULL) < 0) return -1;

D
dapan1121 已提交
5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

void tFreeSSubQueryMsg(SSubQueryMsg *pReq) {
  if (NULL == pReq) {
    return;
  }

  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->msg);
}

D
dapan1121 已提交
5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565
int32_t tSerializeSResFetchReq(void *buf, int32_t bufLen, SResFetchReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  if (tEncodeU64(&encoder, pReq->sId) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->queryId) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->taskId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->execId) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->header.vgId);
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
}

int32_t tDeserializeSResFetchReq(void *buf, int32_t bufLen, SResFetchReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);

  SMsgHead *pHead = buf;
  pHead->vgId = pReq->header.vgId;
  pHead->contLen = pReq->header.contLen;

  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDecodeU64(&decoder, &pReq->sId) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->queryId) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->taskId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->execId) < 0) return -1;
L
Liu Jicong 已提交
5566

D
dapan1121 已提交
5567 5568 5569 5570 5571 5572
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615
int32_t tSerializeSTqOffsetVal(SEncoder *pEncoder, STqOffsetVal *pOffset) {
  if (tEncodeI8(pEncoder, pOffset->type) < 0) return -1;
  if (tEncodeI64(pEncoder, pOffset->uid) < 0) return -1;
  if (tEncodeI64(pEncoder, pOffset->ts) < 0) return -1;

  return 0;
}

int32_t tDerializeSTqOffsetVal(SDecoder *pDecoder, STqOffsetVal *pOffset) {
  if (tDecodeI8(pDecoder, &pOffset->type) < 0) return -1;
  if (tDecodeI64(pDecoder, &pOffset->uid) < 0) return -1;
  if (tDecodeI64(pDecoder, &pOffset->ts) < 0) return -1;

  return 0;
}

int32_t tSerializeSMqPollReq(void *buf, int32_t bufLen, SMqPollReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  if (tEncodeCStr(&encoder, pReq->subKey) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->withTbName) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->useSnapshot) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->epoch) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->reqId) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->consumerId) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->timeout) < 0) return -1;
  if (tSerializeSTqOffsetVal(&encoder, &pReq->reqOffset) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
D
dapan1121 已提交
5616
    pHead->vgId = htonl(pReq->head.vgId);
D
dapan1121 已提交
5617 5618 5619 5620 5621 5622 5623 5624 5625
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
}

int32_t tDeserializeSMqPollReq(void *buf, int32_t bufLen, SMqPollReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);

Y
yihaoDeng 已提交
5626 5627 5628
  //  SMsgHead *pHead = buf;
  //  pHead->vgId = pReq->head.vgId;
  //  pHead->contLen = pReq->head.contLen;
D
dapan1121 已提交
5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642

  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDecodeCStrTo(&decoder, pReq->subKey) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->withTbName) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->useSnapshot) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->epoch) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->reqId) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->consumerId) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->timeout) < 0) return -1;
  if (tDerializeSTqOffsetVal(&decoder, &pReq->reqOffset) < 0) return -1;
L
Liu Jicong 已提交
5643

D
dapan1121 已提交
5644 5645 5646 5647 5648 5649
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697
int32_t tSerializeSTaskDropReq(void *buf, int32_t bufLen, STaskDropReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  if (tEncodeU64(&encoder, pReq->sId) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->queryId) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->taskId) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->refId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->execId) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->header.vgId);
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
}

int32_t tDeserializeSTaskDropReq(void *buf, int32_t bufLen, STaskDropReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);

  SMsgHead *pHead = buf;
  pHead->vgId = pReq->header.vgId;
  pHead->contLen = pReq->header.contLen;

  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDecodeU64(&decoder, &pReq->sId) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->queryId) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->taskId) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->refId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->execId) < 0) return -1;
L
Liu Jicong 已提交
5698

D
dapan1121 已提交
5699 5700 5701 5702 5703 5704
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734
int32_t tSerializeSQueryTableRsp(void *buf, int32_t bufLen, SQueryTableRsp *pRsp) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;

  if (tEncodeI32(&encoder, pRsp->code) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->tbFName) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->sversion) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->tversion) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->affectedRows) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);

  return tlen;
}

int32_t tDeserializeSQueryTableRsp(void *buf, int32_t bufLen, SQueryTableRsp *pRsp) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;

  if (tDecodeI32(&decoder, &pRsp->code) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->tbFName) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->sversion) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->tversion) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->affectedRows) < 0) return -1;
L
Liu Jicong 已提交
5735

D
dapan1121 已提交
5736 5737 5738 5739 5740 5741
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
5742 5743 5744 5745 5746 5747 5748
int32_t tSerializeSSchedulerHbReq(void *buf, int32_t bufLen, SSchedulerHbReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

H
Hongze Cheng 已提交
5749 5750
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
5751 5752

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
5753 5754
  if (tEncodeU64(&encoder, pReq->sId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->epId.nodeId) < 0) return -1;
D
dapan1121 已提交
5755
  if (tEncodeU16(&encoder, pReq->epId.ep.port) < 0) return -1;
L
Liu Jicong 已提交
5756
  if (tEncodeCStr(&encoder, pReq->epId.ep.fqdn) < 0) return -1;
D
dapan1121 已提交
5757 5758
  if (pReq->taskAction) {
    int32_t num = taosArrayGetSize(pReq->taskAction);
L
Liu Jicong 已提交
5759
    if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
5760 5761
    for (int32_t i = 0; i < num; ++i) {
      STaskAction *action = taosArrayGet(pReq->taskAction, i);
L
Liu Jicong 已提交
5762 5763 5764
      if (tEncodeU64(&encoder, action->queryId) < 0) return -1;
      if (tEncodeU64(&encoder, action->taskId) < 0) return -1;
      if (tEncodeI8(&encoder, action->action) < 0) return -1;
D
dapan1121 已提交
5765 5766
    }
  } else {
L
Liu Jicong 已提交
5767
    if (tEncodeI32(&encoder, 0) < 0) return -1;
D
dapan1121 已提交
5768 5769 5770 5771
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
5772
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
5773

D
dapan1121 已提交
5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789
  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->header.vgId);
    pHead->contLen = htonl(tlen + headLen);
  }

  return tlen + headLen;
}

int32_t tDeserializeSSchedulerHbReq(void *buf, int32_t bufLen, SSchedulerHbReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);

  SMsgHead *pHead = buf;
  pHead->vgId = pReq->header.vgId;
  pHead->contLen = pReq->header.contLen;

H
Hongze Cheng 已提交
5790 5791
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);
D
dapan1121 已提交
5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->sId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->epId.nodeId) < 0) return -1;
  if (tDecodeU16(&decoder, &pReq->epId.ep.port) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->epId.ep.fqdn) < 0) return -1;
  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
  if (num > 0) {
    pReq->taskAction = taosArrayInit(num, sizeof(STaskStatus));
    if (NULL == pReq->taskAction) return -1;
    for (int32_t i = 0; i < num; ++i) {
      STaskAction action = {0};
      if (tDecodeU64(&decoder, &action.queryId) < 0) return -1;
      if (tDecodeU64(&decoder, &action.taskId) < 0) return -1;
      if (tDecodeI8(&decoder, &action.action) < 0) return -1;
      taosArrayPush(pReq->taskAction, &action);
    }
  } else {
    pReq->taskAction = NULL;
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
5815
  tDecoderClear(&decoder);
D
dapan1121 已提交
5816 5817 5818 5819 5820 5821
  return 0;
}

void tFreeSSchedulerHbReq(SSchedulerHbReq *pReq) { taosArrayDestroy(pReq->taskAction); }

int32_t tSerializeSSchedulerHbRsp(void *buf, int32_t bufLen, SSchedulerHbRsp *pRsp) {
H
Hongze Cheng 已提交
5822 5823
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
5824 5825

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
5826
  if (tEncodeI32(&encoder, pRsp->epId.nodeId) < 0) return -1;
D
dapan1121 已提交
5827
  if (tEncodeU16(&encoder, pRsp->epId.ep.port) < 0) return -1;
L
Liu Jicong 已提交
5828
  if (tEncodeCStr(&encoder, pRsp->epId.ep.fqdn) < 0) return -1;
D
dapan1121 已提交
5829 5830
  if (pRsp->taskStatus) {
    int32_t num = taosArrayGetSize(pRsp->taskStatus);
L
Liu Jicong 已提交
5831
    if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
5832 5833
    for (int32_t i = 0; i < num; ++i) {
      STaskStatus *status = taosArrayGet(pRsp->taskStatus, i);
L
Liu Jicong 已提交
5834 5835 5836
      if (tEncodeU64(&encoder, status->queryId) < 0) return -1;
      if (tEncodeU64(&encoder, status->taskId) < 0) return -1;
      if (tEncodeI64(&encoder, status->refId) < 0) return -1;
D
dapan1121 已提交
5837
      if (tEncodeI32(&encoder, status->execId) < 0) return -1;
L
Liu Jicong 已提交
5838
      if (tEncodeI8(&encoder, status->status) < 0) return -1;
D
dapan1121 已提交
5839 5840
    }
  } else {
L
Liu Jicong 已提交
5841
    if (tEncodeI32(&encoder, 0) < 0) return -1;
D
dapan1121 已提交
5842 5843 5844 5845
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
5846
  tEncoderClear(&encoder);
D
dapan1121 已提交
5847 5848 5849 5850
  return tlen;
}

int32_t tDeserializeSSchedulerHbRsp(void *buf, int32_t bufLen, SSchedulerHbRsp *pRsp) {
H
Hongze Cheng 已提交
5851 5852
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->epId.nodeId) < 0) return -1;
  if (tDecodeU16(&decoder, &pRsp->epId.ep.port) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->epId.ep.fqdn) < 0) return -1;
  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
  if (num > 0) {
    pRsp->taskStatus = taosArrayInit(num, sizeof(STaskStatus));
    if (NULL == pRsp->taskStatus) return -1;
    for (int32_t i = 0; i < num; ++i) {
      STaskStatus status = {0};
      if (tDecodeU64(&decoder, &status.queryId) < 0) return -1;
      if (tDecodeU64(&decoder, &status.taskId) < 0) return -1;
      if (tDecodeI64(&decoder, &status.refId) < 0) return -1;
D
dapan1121 已提交
5868
      if (tDecodeI32(&decoder, &status.execId) < 0) return -1;
D
dapan1121 已提交
5869 5870 5871 5872 5873 5874 5875 5876
      if (tDecodeI8(&decoder, &status.status) < 0) return -1;
      taosArrayPush(pRsp->taskStatus, &status);
    }
  } else {
    pRsp->taskStatus = NULL;
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
5877
  tDecoderClear(&decoder);
D
dapan1121 已提交
5878 5879 5880 5881 5882
  return 0;
}

void tFreeSSchedulerHbRsp(SSchedulerHbRsp *pRsp) { taosArrayDestroy(pRsp->taskStatus); }

D
dapan 已提交
5883
int32_t tSerializeSVCreateTbBatchRsp(void *buf, int32_t bufLen, SVCreateTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
5884 5885
  // SEncoder encoder = {0};
  // tEncoderInit(&encoder, buf, bufLen);
D
dapan 已提交
5886

H
Hongze Cheng 已提交
5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898
  // if (tStartEncode(&encoder) < 0) return -1;
  // if (pRsp->rspList) {
  //   int32_t num = taosArrayGetSize(pRsp->rspList);
  //   if (tEncodeI32(&encoder, num) < 0) return -1;
  //   for (int32_t i = 0; i < num; ++i) {
  //     SVCreateTbRsp *rsp = taosArrayGet(pRsp->rspList, i);
  //     if (tEncodeI32(&encoder, rsp->code) < 0) return -1;
  //   }
  // } else {
  //   if (tEncodeI32(&encoder, 0) < 0) return -1;
  // }
  // tEndEncode(&encoder);
D
dapan 已提交
5899

H
Hongze Cheng 已提交
5900
  // int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
5901
  // tEncoderClear(&encoder);
H
Hongze Cheng 已提交
5902 5903
  // reture tlen;
  return 0;
D
dapan 已提交
5904 5905 5906
}

int32_t tDeserializeSVCreateTbBatchRsp(void *buf, int32_t bufLen, SVCreateTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
5907
  // SDecoder  decoder = {0};
H
Hongze Cheng 已提交
5908
  // int32_t num = 0;
H
Hongze Cheng 已提交
5909
  // tDecoderInit(&decoder, buf, bufLen);
D
dapan 已提交
5910

H
Hongze Cheng 已提交
5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924
  // if (tStartDecode(&decoder) < 0) return -1;
  // if (tDecodeI32(&decoder, &num) < 0) return -1;
  // if (num > 0) {
  //   pRsp->rspList = taosArrayInit(num, sizeof(SVCreateTbRsp));
  //   if (NULL == pRsp->rspList) return -1;
  //   for (int32_t i = 0; i < num; ++i) {
  //     SVCreateTbRsp rsp = {0};
  //     if (tDecodeI32(&decoder, &rsp.code) < 0) return -1;
  //     if (NULL == taosArrayPush(pRsp->rspList, &rsp)) return -1;
  //   }
  // } else {
  //   pRsp->rspList = NULL;
  // }
  // tEndDecode(&decoder);
D
dapan 已提交
5925

H
Hongze Cheng 已提交
5926
  // tDecoderClear(&decoder);
D
dapan 已提交
5927 5928 5929
  return 0;
}

H
Hongze Cheng 已提交
5930
int tEncodeSVCreateTbBatchRsp(SEncoder *pCoder, const SVCreateTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
5931 5932 5933 5934 5935 5936 5937 5938 5939
  int32_t        nRsps = taosArrayGetSize(pRsp->pArray);
  SVCreateTbRsp *pCreateRsp;

  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32v(pCoder, nRsps) < 0) return -1;
  for (int32_t i = 0; i < nRsps; i++) {
    pCreateRsp = taosArrayGet(pRsp->pArray, i);
    if (tEncodeSVCreateTbRsp(pCoder, pCreateRsp) < 0) return -1;
D
dapan 已提交
5940 5941
  }

H
Hongze Cheng 已提交
5942 5943 5944 5945 5946
  tEndEncode(pCoder);

  return 0;
}

H
Hongze Cheng 已提交
5947
int tDecodeSVCreateTbBatchRsp(SDecoder *pCoder, SVCreateTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
5948 5949 5950
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pRsp->nRsps) < 0) return -1;
H
Hongze Cheng 已提交
5951
  pRsp->pRsps = (SVCreateTbRsp *)tDecoderMalloc(pCoder, sizeof(*pRsp->pRsps) * pRsp->nRsps);
H
Hongze Cheng 已提交
5952 5953 5954 5955 5956
  for (int32_t i = 0; i < pRsp->nRsps; i++) {
    if (tDecodeSVCreateTbRsp(pCoder, pRsp->pRsps + i) < 0) return -1;
  }

  tEndDecode(pCoder);
D
dapan 已提交
5957 5958 5959
  return 0;
}

5960 5961 5962 5963 5964
int32_t tEncodeTSma(SEncoder *pCoder, const STSma *pSma) {
  if (tEncodeI8(pCoder, pSma->version) < 0) return -1;
  if (tEncodeI8(pCoder, pSma->intervalUnit) < 0) return -1;
  if (tEncodeI8(pCoder, pSma->slidingUnit) < 0) return -1;
  if (tEncodeI8(pCoder, pSma->timezoneInt) < 0) return -1;
C
Cary Xu 已提交
5965
  if (tEncodeI32(pCoder, pSma->dstVgId) < 0) return -1;
5966 5967 5968 5969 5970
  if (tEncodeCStr(pCoder, pSma->indexName) < 0) return -1;
  if (tEncodeI32(pCoder, pSma->exprLen) < 0) return -1;
  if (tEncodeI32(pCoder, pSma->tagsFilterLen) < 0) return -1;
  if (tEncodeI64(pCoder, pSma->indexUid) < 0) return -1;
  if (tEncodeI64(pCoder, pSma->tableUid) < 0) return -1;
C
Cary Xu 已提交
5971 5972
  if (tEncodeI64(pCoder, pSma->dstTbUid) < 0) return -1;
  if (tEncodeCStr(pCoder, pSma->dstTbName) < 0) return -1;
5973 5974 5975 5976 5977 5978 5979 5980 5981
  if (tEncodeI64(pCoder, pSma->interval) < 0) return -1;
  if (tEncodeI64(pCoder, pSma->offset) < 0) return -1;
  if (tEncodeI64(pCoder, pSma->sliding) < 0) return -1;
  if (pSma->exprLen > 0) {
    if (tEncodeCStr(pCoder, pSma->expr) < 0) return -1;
  }
  if (pSma->tagsFilterLen > 0) {
    if (tEncodeCStr(pCoder, pSma->tagsFilter) < 0) return -1;
  }
C
Cary Xu 已提交
5982

C
Cary Xu 已提交
5983 5984
  tEncodeSSchemaWrapper(pCoder, &pSma->schemaRow);
  tEncodeSSchemaWrapper(pCoder, &pSma->schemaTag);
C
Cary Xu 已提交
5985

5986 5987 5988
  return 0;
}

C
Cary Xu 已提交
5989
int32_t tDecodeTSma(SDecoder *pCoder, STSma *pSma, bool deepCopy) {
5990 5991 5992 5993
  if (tDecodeI8(pCoder, &pSma->version) < 0) return -1;
  if (tDecodeI8(pCoder, &pSma->intervalUnit) < 0) return -1;
  if (tDecodeI8(pCoder, &pSma->slidingUnit) < 0) return -1;
  if (tDecodeI8(pCoder, &pSma->timezoneInt) < 0) return -1;
C
Cary Xu 已提交
5994
  if (tDecodeI32(pCoder, &pSma->dstVgId) < 0) return -1;
C
Cary Xu 已提交
5995
  if (tDecodeCStrTo(pCoder, pSma->indexName) < 0) return -1;
5996 5997 5998 5999
  if (tDecodeI32(pCoder, &pSma->exprLen) < 0) return -1;
  if (tDecodeI32(pCoder, &pSma->tagsFilterLen) < 0) return -1;
  if (tDecodeI64(pCoder, &pSma->indexUid) < 0) return -1;
  if (tDecodeI64(pCoder, &pSma->tableUid) < 0) return -1;
C
Cary Xu 已提交
6000
  if (tDecodeI64(pCoder, &pSma->dstTbUid) < 0) return -1;
C
Cary Xu 已提交
6001 6002 6003 6004 6005 6006
  if (deepCopy) {
    if (tDecodeCStrAlloc(pCoder, &pSma->dstTbName) < 0) return -1;
  } else {
    if (tDecodeCStr(pCoder, &pSma->dstTbName) < 0) return -1;
  }

6007 6008 6009 6010
  if (tDecodeI64(pCoder, &pSma->interval) < 0) return -1;
  if (tDecodeI64(pCoder, &pSma->offset) < 0) return -1;
  if (tDecodeI64(pCoder, &pSma->sliding) < 0) return -1;
  if (pSma->exprLen > 0) {
C
Cary Xu 已提交
6011 6012 6013 6014 6015
    if (deepCopy) {
      if (tDecodeCStrAlloc(pCoder, &pSma->expr) < 0) return -1;
    } else {
      if (tDecodeCStr(pCoder, &pSma->expr) < 0) return -1;
    }
6016 6017 6018 6019
  } else {
    pSma->expr = NULL;
  }
  if (pSma->tagsFilterLen > 0) {
C
Cary Xu 已提交
6020 6021 6022 6023 6024
    if (deepCopy) {
      if (tDecodeCStrAlloc(pCoder, &pSma->tagsFilter) < 0) return -1;
    } else {
      if (tDecodeCStr(pCoder, &pSma->tagsFilter) < 0) return -1;
    }
6025 6026 6027
  } else {
    pSma->tagsFilter = NULL;
  }
C
Cary Xu 已提交
6028 6029 6030
  // only needed in dstVgroup
  tDecodeSSchemaWrapperEx(pCoder, &pSma->schemaRow);
  tDecodeSSchemaWrapperEx(pCoder, &pSma->schemaTag);
6031 6032 6033 6034 6035 6036

  return 0;
}

int32_t tEncodeSVCreateTSmaReq(SEncoder *pCoder, const SVCreateTSmaReq *pReq) {
  if (tStartEncode(pCoder) < 0) return -1;
C
Cary Xu 已提交
6037

6038
  tEncodeTSma(pCoder, pReq);
C
Cary Xu 已提交
6039

6040 6041
  tEndEncode(pCoder);
  return 0;
C
Cary Xu 已提交
6042
}
D
dapan1121 已提交
6043

6044 6045 6046
int32_t tDecodeSVCreateTSmaReq(SDecoder *pCoder, SVCreateTSmaReq *pReq) {
  if (tStartDecode(pCoder) < 0) return -1;

C
Cary Xu 已提交
6047
  tDecodeTSma(pCoder, pReq, false);
D
dapan1121 已提交
6048

6049 6050
  tEndDecode(pCoder);
  return 0;
C
Cary Xu 已提交
6051 6052
}

6053 6054
int32_t tEncodeSVDropTSmaReq(SEncoder *pCoder, const SVDropTSmaReq *pReq) {
  if (tStartEncode(pCoder) < 0) return -1;
C
Cary Xu 已提交
6055

6056 6057
  if (tEncodeI64(pCoder, pReq->indexUid) < 0) return -1;
  if (tEncodeCStr(pCoder, pReq->indexName) < 0) return -1;
C
Cary Xu 已提交
6058

6059 6060
  tEndEncode(pCoder);
  return 0;
C
Cary Xu 已提交
6061 6062
}

6063 6064 6065 6066
int32_t tDecodeSVDropTSmaReq(SDecoder *pCoder, SVDropTSmaReq *pReq) {
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI64(pCoder, &pReq->indexUid) < 0) return -1;
C
Cary Xu 已提交
6067
  if (tDecodeCStrTo(pCoder, pReq->indexName) < 0) return -1;
6068 6069 6070

  tEndDecode(pCoder);
  return 0;
C
Cary Xu 已提交
6071
}
L
Liu Jicong 已提交
6072

D
dapan1121 已提交
6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088
int32_t tSerializeSVDeleteReq(void *buf, int32_t bufLen, SVDeleteReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->sId) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->queryId) < 0) return -1;
  if (tEncodeU64(&encoder, pReq->taskId) < 0) return -1;
  if (tEncodeU32(&encoder, pReq->sqlLen) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->sql) < 0) return -1;
6089
  if (tEncodeBinary(&encoder, pReq->msg, pReq->phyLen) < 0) return -1;
D
dapan1121 已提交
6090 6091 6092 6093
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
C
Cary Xu 已提交
6094

D
dapan1121 已提交
6095 6096 6097 6098
  if (buf != NULL) {
    SMsgHead *pHead = (SMsgHead *)((char *)buf - headLen);
    pHead->vgId = htonl(pReq->header.vgId);
    pHead->contLen = htonl(tlen + headLen);
C
Cary Xu 已提交
6099 6100
  }

D
dapan1121 已提交
6101
  return tlen + headLen;
C
Cary Xu 已提交
6102 6103
}

D
dapan1121 已提交
6104 6105
int32_t tDeserializeSVDeleteReq(void *buf, int32_t bufLen, SVDeleteReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
C
Cary Xu 已提交
6106

D
dapan1121 已提交
6107 6108 6109
  SMsgHead *pHead = buf;
  pHead->vgId = pReq->header.vgId;
  pHead->contLen = pReq->header.contLen;
C
Cary Xu 已提交
6110

D
dapan1121 已提交
6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->sId) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->queryId) < 0) return -1;
  if (tDecodeU64(&decoder, &pReq->taskId) < 0) return -1;
  if (tDecodeU32(&decoder, &pReq->sqlLen) < 0) return -1;
  pReq->sql = taosMemoryCalloc(1, pReq->sqlLen + 1);
  if (NULL == pReq->sql) return -1;
  if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
6122 6123 6124
  uint64_t msgLen = 0;
  if (tDecodeBinaryAlloc(&decoder, (void **)&pReq->msg, &msgLen) < 0) return -1;
  pReq->phyLen = msgLen;
D
dapan1121 已提交
6125 6126 6127 6128

  tEndDecode(&decoder);

  tDecoderClear(&decoder);
C
Cary Xu 已提交
6129 6130 6131
  return 0;
}

C
Cary Xu 已提交
6132
int32_t tEncodeSVDeleteRsp(SEncoder *pCoder, const SVDeleteRsp *pReq) {
C
Cary Xu 已提交
6133 6134 6135 6136 6137 6138 6139 6140
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI64(pCoder, pReq->affectedRows) < 0) return -1;

  tEndEncode(pCoder);
  return 0;
}

C
Cary Xu 已提交
6141
int32_t tDecodeSVDeleteRsp(SDecoder *pCoder, SVDeleteRsp *pReq) {
C
Cary Xu 已提交
6142 6143 6144 6145 6146 6147 6148 6149
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI64(pCoder, &pReq->affectedRows) < 0) return -1;

  tEndDecode(pCoder);
  return 0;
}

L
Liu Jicong 已提交
6150
int32_t tSerializeSCMCreateStreamReq(void *buf, int32_t bufLen, const SCMCreateStreamReq *pReq) {
L
Liu Jicong 已提交
6151 6152 6153 6154 6155
  int32_t sqlLen = 0;
  int32_t astLen = 0;
  if (pReq->sql != NULL) sqlLen = (int32_t)strlen(pReq->sql);
  if (pReq->ast != NULL) astLen = (int32_t)strlen(pReq->ast);

H
Hongze Cheng 已提交
6156 6157
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
L
Liu Jicong 已提交
6158 6159 6160

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
L
Liu Jicong 已提交
6161
  if (tEncodeCStr(&encoder, pReq->sourceDB) < 0) return -1;
L
Liu Jicong 已提交
6162
  if (tEncodeCStr(&encoder, pReq->targetStbFullName) < 0) return -1;
L
Liu Jicong 已提交
6163
  if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
6164
  if (tEncodeI8(&encoder, pReq->fillHistory) < 0) return -1;
L
Liu Jicong 已提交
6165 6166
  if (tEncodeI32(&encoder, sqlLen) < 0) return -1;
  if (tEncodeI32(&encoder, astLen) < 0) return -1;
L
Liu Jicong 已提交
6167
  if (tEncodeI8(&encoder, pReq->triggerType) < 0) return -1;
6168
  if (tEncodeI64(&encoder, pReq->maxDelay) < 0) return -1;
L
Liu Jicong 已提交
6169
  if (tEncodeI64(&encoder, pReq->watermark) < 0) return -1;
6170
  if (tEncodeI8(&encoder, pReq->igExpired) < 0) return -1;
L
Liu Jicong 已提交
6171 6172
  if (sqlLen > 0 && tEncodeCStr(&encoder, pReq->sql) < 0) return -1;
  if (astLen > 0 && tEncodeCStr(&encoder, pReq->ast) < 0) return -1;
6173 6174 6175 6176 6177 6178 6179 6180
  if (tEncodeI32(&encoder, pReq->numOfTags) < 0) return -1;
  for (int32_t i = 0; i < pReq->numOfTags; ++i) {
    SField *pField = taosArrayGet(pReq->pTags, i);
    if (tEncodeI8(&encoder, pField->type) < 0) return -1;
    if (tEncodeI8(&encoder, pField->flags) < 0) return -1;
    if (tEncodeI32(&encoder, pField->bytes) < 0) return -1;
    if (tEncodeCStr(&encoder, pField->name) < 0) return -1;
  }
X
Xiaoyu Wang 已提交
6181

6182
  if (tEncodeI8(&encoder, pReq->createStb) < 0) return -1;
6183
  if (tEncodeU64(&encoder, pReq->targetStbUid) < 0) return -1;
6184 6185 6186 6187 6188 6189 6190
  if (tEncodeI32(&encoder, taosArrayGetSize(pReq->fillNullCols)) < 0) return -1;
  for (int32_t i = 0; i < taosArrayGetSize(pReq->fillNullCols); ++i) {
    SColLocation *pCol = taosArrayGet(pReq->fillNullCols, i);
    if (tEncodeI16(&encoder, pCol->slotId) < 0) return -1;
    if (tEncodeI16(&encoder, pCol->colId) < 0) return -1;
    if (tEncodeI8(&encoder, pCol->type) < 0) return -1;
  }
6191
  if (tEncodeI64(&encoder, pReq->deleteMark) < 0) return -1;
6192
  if (tEncodeI8(&encoder, pReq->igUpdate) < 0) return -1;
6193
  if (tEncodeI64(&encoder, pReq->lastTs) < 0) return -1;
L
Liu Jicong 已提交
6194

L
Liu Jicong 已提交
6195 6196 6197
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
6198
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
6199 6200 6201 6202
  return tlen;
}

int32_t tDeserializeSCMCreateStreamReq(void *buf, int32_t bufLen, SCMCreateStreamReq *pReq) {
L
Liu Jicong 已提交
6203 6204 6205
  int32_t sqlLen = 0;
  int32_t astLen = 0;

H
Hongze Cheng 已提交
6206 6207
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
L
Liu Jicong 已提交
6208 6209 6210

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
L
Liu Jicong 已提交
6211
  if (tDecodeCStrTo(&decoder, pReq->sourceDB) < 0) return -1;
L
Liu Jicong 已提交
6212
  if (tDecodeCStrTo(&decoder, pReq->targetStbFullName) < 0) return -1;
L
Liu Jicong 已提交
6213
  if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
6214
  if (tDecodeI8(&decoder, &pReq->fillHistory) < 0) return -1;
L
Liu Jicong 已提交
6215 6216
  if (tDecodeI32(&decoder, &sqlLen) < 0) return -1;
  if (tDecodeI32(&decoder, &astLen) < 0) return -1;
X
Xiaoyu Wang 已提交
6217
  if (tDecodeI8(&decoder, &pReq->triggerType) < 0) return -1;
6218
  if (tDecodeI64(&decoder, &pReq->maxDelay) < 0) return -1;
X
Xiaoyu Wang 已提交
6219
  if (tDecodeI64(&decoder, &pReq->watermark) < 0) return -1;
6220
  if (tDecodeI8(&decoder, &pReq->igExpired) < 0) return -1;
L
Liu Jicong 已提交
6221 6222

  if (sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
6223
    pReq->sql = taosMemoryCalloc(1, sqlLen + 1);
L
Liu Jicong 已提交
6224 6225 6226 6227 6228
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }

  if (astLen > 0) {
wafwerar's avatar
wafwerar 已提交
6229
    pReq->ast = taosMemoryCalloc(1, astLen + 1);
L
Liu Jicong 已提交
6230 6231 6232
    if (pReq->ast == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
  }
6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253

  if (tDecodeI32(&decoder, &pReq->numOfTags) < 0) return -1;
  if (pReq->numOfTags > 0) {
    pReq->pTags = taosArrayInit(pReq->numOfTags, sizeof(SField));
    if (pReq->pTags == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    for (int32_t i = 0; i < pReq->numOfTags; ++i) {
      SField field = {0};
      if (tDecodeI8(&decoder, &field.type) < 0) return -1;
      if (tDecodeI8(&decoder, &field.flags) < 0) return -1;
      if (tDecodeI32(&decoder, &field.bytes) < 0) return -1;
      if (tDecodeCStrTo(&decoder, field.name) < 0) return -1;
      if (taosArrayPush(pReq->pTags, &field) == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
    }
  }
6254
  if (tDecodeI8(&decoder, &pReq->createStb) < 0) return -1;
6255
  if (tDecodeU64(&decoder, &pReq->targetStbUid) < 0) return -1;
6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275
  int32_t numOfFillNullCols = 0;
  if (tDecodeI32(&decoder, &numOfFillNullCols) < 0) return -1;
  if (numOfFillNullCols > 0) {
    pReq->fillNullCols = taosArrayInit(numOfFillNullCols, sizeof(SColLocation));
    if (pReq->fillNullCols == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    for (int32_t i = 0; i < numOfFillNullCols; ++i) {
      SColLocation col = {0};
      if (tDecodeI16(&decoder, &col.slotId) < 0) return -1;
      if (tDecodeI16(&decoder, &col.colId) < 0) return -1;
      if (tDecodeI8(&decoder, &col.type) < 0) return -1;
      if (taosArrayPush(pReq->fillNullCols, &col) == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
    }
  }
6276

6277
  if (tDecodeI64(&decoder, &pReq->deleteMark) < 0) return -1;
6278
  if (tDecodeI8(&decoder, &pReq->igUpdate) < 0) return -1;
6279
  if (tDecodeI64(&decoder, &pReq->lastTs) < 0) return -1;
6280

L
Liu Jicong 已提交
6281 6282
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
6283
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
6284 6285 6286
  return 0;
}

X
Xiaoyu Wang 已提交
6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315
int32_t tSerializeSMDropStreamReq(void *buf, int32_t bufLen, const SMDropStreamReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMDropStreamReq(void *buf, int32_t bufLen, SMDropStreamReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;

  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

L
Liu Jicong 已提交
6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344
int32_t tSerializeSMRecoverStreamReq(void *buf, int32_t bufLen, const SMRecoverStreamReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMRecoverStreamReq(void *buf, int32_t bufLen, SMRecoverStreamReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;

  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

L
Liu Jicong 已提交
6345
void tFreeSCMCreateStreamReq(SCMCreateStreamReq *pReq) {
6346 6347 6348
  if (NULL == pReq) {
    return;
  }
X
Xiaoyu Wang 已提交
6349
  taosArrayDestroy(pReq->pTags);
wafwerar's avatar
wafwerar 已提交
6350 6351
  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->ast);
6352
  taosArrayDestroy(pReq->fillNullCols);
L
Liu Jicong 已提交
6353
}
C
Cary Xu 已提交
6354

H
Hongze Cheng 已提交
6355
int32_t tEncodeSRSmaParam(SEncoder *pCoder, const SRSmaParam *pRSmaParam) {
6356 6357 6358 6359 6360 6361 6362 6363 6364
  for (int32_t i = 0; i < 2; ++i) {
    if (tEncodeI64v(pCoder, pRSmaParam->maxdelay[i]) < 0) return -1;
    if (tEncodeI64v(pCoder, pRSmaParam->watermark[i]) < 0) return -1;
    if (tEncodeI32v(pCoder, pRSmaParam->qmsgLen[i]) < 0) return -1;
    if (pRSmaParam->qmsgLen[i] > 0) {
      if (tEncodeBinary(pCoder, pRSmaParam->qmsg[i], (uint64_t)pRSmaParam->qmsgLen[i]) <
          0)  // qmsgLen contains len of '\0'
        return -1;
    }
C
Cary Xu 已提交
6365 6366 6367 6368 6369
  }

  return 0;
}

H
Hongze Cheng 已提交
6370
int32_t tDecodeSRSmaParam(SDecoder *pCoder, SRSmaParam *pRSmaParam) {
6371 6372 6373 6374 6375
  for (int32_t i = 0; i < 2; ++i) {
    if (tDecodeI64v(pCoder, &pRSmaParam->maxdelay[i]) < 0) return -1;
    if (tDecodeI64v(pCoder, &pRSmaParam->watermark[i]) < 0) return -1;
    if (tDecodeI32v(pCoder, &pRSmaParam->qmsgLen[i]) < 0) return -1;
    if (pRSmaParam->qmsgLen[i] > 0) {
C
Cary Xu 已提交
6376
      if (tDecodeBinary(pCoder, (uint8_t **)&pRSmaParam->qmsg[i], NULL) < 0) return -1;  // qmsgLen contains len of '\0'
6377 6378 6379
    } else {
      pRSmaParam->qmsg[i] = NULL;
    }
C
Cary Xu 已提交
6380
  }
6381

C
Cary Xu 已提交
6382 6383 6384
  return 0;
}

H
Hongze Cheng 已提交
6385
int tEncodeSVCreateStbReq(SEncoder *pCoder, const SVCreateStbReq *pReq) {
H
Hongze Cheng 已提交
6386 6387 6388 6389 6390
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeCStr(pCoder, pReq->name) < 0) return -1;
  if (tEncodeI64(pCoder, pReq->suid) < 0) return -1;
  if (tEncodeI8(pCoder, pReq->rollup) < 0) return -1;
6391
  if (tEncodeSSchemaWrapper(pCoder, &pReq->schemaRow) < 0) return -1;
H
Hongze Cheng 已提交
6392
  if (tEncodeSSchemaWrapper(pCoder, &pReq->schemaTag) < 0) return -1;
C
Cary Xu 已提交
6393
  if (pReq->rollup) {
C
Cary Xu 已提交
6394
    if (tEncodeSRSmaParam(pCoder, &pReq->rsmaParam) < 0) return -1;
C
Cary Xu 已提交
6395
  }
H
Hongze Cheng 已提交
6396

wmmhello's avatar
wmmhello 已提交
6397 6398 6399 6400 6401
  if (tEncodeI32(pCoder, pReq->alterOriDataLen) < 0) return -1;
  if (pReq->alterOriDataLen > 0) {
    if (tEncodeBinary(pCoder, pReq->alterOriData, pReq->alterOriDataLen) < 0) return -1;
  }

H
Hongze Cheng 已提交
6402 6403 6404 6405
  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6406
int tDecodeSVCreateStbReq(SDecoder *pCoder, SVCreateStbReq *pReq) {
H
Hongze Cheng 已提交
6407 6408 6409 6410 6411
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeCStr(pCoder, &pReq->name) < 0) return -1;
  if (tDecodeI64(pCoder, &pReq->suid) < 0) return -1;
  if (tDecodeI8(pCoder, &pReq->rollup) < 0) return -1;
M
Minglei Jin 已提交
6412 6413
  if (tDecodeSSchemaWrapperEx(pCoder, &pReq->schemaRow) < 0) return -1;
  if (tDecodeSSchemaWrapperEx(pCoder, &pReq->schemaTag) < 0) return -1;
C
Cary Xu 已提交
6414
  if (pReq->rollup) {
C
Cary Xu 已提交
6415
    if (tDecodeSRSmaParam(pCoder, &pReq->rsmaParam) < 0) return -1;
C
Cary Xu 已提交
6416
  }
H
Hongze Cheng 已提交
6417

wmmhello's avatar
wmmhello 已提交
6418 6419 6420 6421 6422
  if (tDecodeI32(pCoder, &pReq->alterOriDataLen) < 0) return -1;
  if (pReq->alterOriDataLen > 0) {
    if (tDecodeBinary(pCoder, (uint8_t **)&pReq->alterOriData, NULL) < 0) return -1;
  }

H
Hongze Cheng 已提交
6423 6424 6425
  tEndDecode(pCoder);
  return 0;
}
6426

H
Hongze Cheng 已提交
6427
int tEncodeSVCreateTbReq(SEncoder *pCoder, const SVCreateTbReq *pReq) {
H
Hongze Cheng 已提交
6428 6429
  if (tStartEncode(pCoder) < 0) return -1;

H
Hongze Cheng 已提交
6430
  if (tEncodeI32v(pCoder, pReq->flags) < 0) return -1;
H
Hongze Cheng 已提交
6431
  if (tEncodeCStr(pCoder, pReq->name) < 0) return -1;
H
Hongze Cheng 已提交
6432
  if (tEncodeI64(pCoder, pReq->uid) < 0) return -1;
6433
  if (tEncodeI64(pCoder, pReq->btime) < 0) return -1;
H
Hongze Cheng 已提交
6434 6435
  if (tEncodeI32(pCoder, pReq->ttl) < 0) return -1;
  if (tEncodeI8(pCoder, pReq->type) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
6436 6437 6438 6439
  if (tEncodeI32(pCoder, pReq->commentLen) < 0) return -1;
  if (pReq->commentLen > 0) {
    if (tEncodeCStr(pCoder, pReq->comment) < 0) return -1;
  }
H
Hongze Cheng 已提交
6440 6441

  if (pReq->type == TSDB_CHILD_TABLE) {
6442
    if (tEncodeCStr(pCoder, pReq->ctb.stbName) < 0) return -1;
6443
    if (tEncodeU8(pCoder, pReq->ctb.tagNum) < 0) return -1;
H
Hongze Cheng 已提交
6444
    if (tEncodeI64(pCoder, pReq->ctb.suid) < 0) return -1;
C
Cary Xu 已提交
6445
    if (tEncodeTag(pCoder, (const STag *)pReq->ctb.pTag) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
6446 6447
    int32_t len = taosArrayGetSize(pReq->ctb.tagName);
    if (tEncodeI32(pCoder, len) < 0) return -1;
L
Liu Jicong 已提交
6448 6449
    for (int32_t i = 0; i < len; i++) {
      char *name = taosArrayGet(pReq->ctb.tagName, i);
wmmhello's avatar
wmmhello 已提交
6450 6451
      if (tEncodeCStr(pCoder, name) < 0) return -1;
    }
H
Hongze Cheng 已提交
6452
  } else if (pReq->type == TSDB_NORMAL_TABLE) {
6453
    if (tEncodeSSchemaWrapper(pCoder, &pReq->ntb.schemaRow) < 0) return -1;
H
Hongze Cheng 已提交
6454
  } else {
6455
    ASSERT(0);
H
Hongze Cheng 已提交
6456 6457 6458 6459 6460 6461
  }

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6462
int tDecodeSVCreateTbReq(SDecoder *pCoder, SVCreateTbReq *pReq) {
H
Hongze Cheng 已提交
6463 6464
  if (tStartDecode(pCoder) < 0) return -1;

H
Hongze Cheng 已提交
6465
  if (tDecodeI32v(pCoder, &pReq->flags) < 0) return -1;
H
Hongze Cheng 已提交
6466
  if (tDecodeCStr(pCoder, &pReq->name) < 0) return -1;
H
Hongze Cheng 已提交
6467
  if (tDecodeI64(pCoder, &pReq->uid) < 0) return -1;
6468
  if (tDecodeI64(pCoder, &pReq->btime) < 0) return -1;
H
Hongze Cheng 已提交
6469 6470
  if (tDecodeI32(pCoder, &pReq->ttl) < 0) return -1;
  if (tDecodeI8(pCoder, &pReq->type) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
6471 6472
  if (tDecodeI32(pCoder, &pReq->commentLen) < 0) return -1;
  if (pReq->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
6473
    pReq->comment = taosMemoryMalloc(pReq->commentLen + 1);
wmmhello's avatar
wmmhello 已提交
6474 6475 6476
    if (pReq->comment == NULL) return -1;
    if (tDecodeCStrTo(pCoder, pReq->comment) < 0) return -1;
  }
H
Hongze Cheng 已提交
6477 6478

  if (pReq->type == TSDB_CHILD_TABLE) {
6479
    if (tDecodeCStr(pCoder, &pReq->ctb.stbName) < 0) return -1;
6480
    if (tDecodeU8(pCoder, &pReq->ctb.tagNum) < 0) return -1;
H
Hongze Cheng 已提交
6481
    if (tDecodeI64(pCoder, &pReq->ctb.suid) < 0) return -1;
C
Cary Xu 已提交
6482
    if (tDecodeTag(pCoder, (STag **)&pReq->ctb.pTag) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
6483 6484 6485
    int32_t len = 0;
    if (tDecodeI32(pCoder, &len) < 0) return -1;
    pReq->ctb.tagName = taosArrayInit(len, TSDB_COL_NAME_LEN);
L
Liu Jicong 已提交
6486 6487 6488
    if (pReq->ctb.tagName == NULL) return -1;
    for (int32_t i = 0; i < len; i++) {
      char  name[TSDB_COL_NAME_LEN] = {0};
wmmhello's avatar
wmmhello 已提交
6489 6490
      char *tmp = NULL;
      if (tDecodeCStr(pCoder, &tmp) < 0) return -1;
G
Ganlin Zhao 已提交
6491
      strncpy(name, tmp, TSDB_COL_NAME_LEN - 1);
wmmhello's avatar
wmmhello 已提交
6492 6493
      taosArrayPush(pReq->ctb.tagName, name);
    }
H
Hongze Cheng 已提交
6494
  } else if (pReq->type == TSDB_NORMAL_TABLE) {
6495
    if (tDecodeSSchemaWrapperEx(pCoder, &pReq->ntb.schemaRow) < 0) return -1;
H
Hongze Cheng 已提交
6496
  } else {
6497
    ASSERT(0);
H
Hongze Cheng 已提交
6498 6499
  }

H
Hongze Cheng 已提交
6500 6501 6502 6503
  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522
void tDestroySVCreateTbReq(SVCreateTbReq *pReq, int32_t flags) {
  if (pReq == NULL) return;

  if (flags & TSDB_MSG_FLG_ENCODE) {
    // TODO
  } else if (flags & TSDB_MSG_FLG_DECODE) {
    if (pReq->comment) {
      pReq->comment = NULL;
      taosMemoryFree(pReq->comment);
    }

    if (pReq->type == TSDB_CHILD_TABLE) {
      if (pReq->ctb.tagName) taosArrayDestroy(pReq->ctb.tagName);
    } else if (pReq->type == TSDB_NORMAL_TABLE) {
      if (pReq->ntb.schemaRow.pSchema) taosMemoryFree(pReq->ntb.schemaRow.pSchema);
    }
  }
}

H
Hongze Cheng 已提交
6523
int tEncodeSVCreateTbBatchReq(SEncoder *pCoder, const SVCreateTbBatchReq *pReq) {
H
Hongze Cheng 已提交
6524 6525
  int32_t nReq = taosArrayGetSize(pReq->pArray);

H
Hongze Cheng 已提交
6526 6527
  if (tStartEncode(pCoder) < 0) return -1;

H
Hongze Cheng 已提交
6528 6529
  if (tEncodeI32v(pCoder, nReq) < 0) return -1;
  for (int iReq = 0; iReq < nReq; iReq++) {
H
Hongze Cheng 已提交
6530 6531 6532 6533 6534 6535 6536
    if (tEncodeSVCreateTbReq(pCoder, (SVCreateTbReq *)taosArrayGet(pReq->pArray, iReq)) < 0) return -1;
  }

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6537
int tDecodeSVCreateTbBatchReq(SDecoder *pCoder, SVCreateTbBatchReq *pReq) {
H
Hongze Cheng 已提交
6538 6539 6540
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pReq->nReqs) < 0) return -1;
H
Hongze Cheng 已提交
6541
  pReq->pReqs = (SVCreateTbReq *)tDecoderMalloc(pCoder, sizeof(SVCreateTbReq) * pReq->nReqs);
H
Hongze Cheng 已提交
6542 6543 6544 6545 6546
  if (pReq->pReqs == NULL) return -1;
  for (int iReq = 0; iReq < pReq->nReqs; iReq++) {
    if (tDecodeSVCreateTbReq(pCoder, pReq->pReqs + iReq) < 0) return -1;
  }

H
Hongze Cheng 已提交
6547 6548 6549 6550
  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6551
int tEncodeSVCreateTbRsp(SEncoder *pCoder, const SVCreateTbRsp *pRsp) {
H
Hongze Cheng 已提交
6552 6553 6554
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32(pCoder, pRsp->code) < 0) return -1;
6555 6556 6557 6558
  if (tEncodeI32(pCoder, pRsp->pMeta ? 1 : 0) < 0) return -1;
  if (pRsp->pMeta) {
    if (tEncodeSTableMetaRsp(pCoder, pRsp->pMeta) < 0) return -1;
  }
H
Hongze Cheng 已提交
6559 6560 6561 6562 6563

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6564
int tDecodeSVCreateTbRsp(SDecoder *pCoder, SVCreateTbRsp *pRsp) {
H
Hongze Cheng 已提交
6565 6566 6567 6568
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32(pCoder, &pRsp->code) < 0) return -1;

6569 6570 6571 6572 6573 6574 6575 6576 6577
  int32_t meta = 0;
  if (tDecodeI32(pCoder, &meta) < 0) return -1;
  if (meta) {
    pRsp->pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
    if (NULL == pRsp->pMeta) return -1;
    if (tDecodeSTableMetaRsp(pCoder, pRsp->pMeta) < 0) return -1;
  } else {
    pRsp->pMeta = NULL;
  }
L
Liu Jicong 已提交
6578

H
Hongze Cheng 已提交
6579 6580 6581 6582
  tEndDecode(pCoder);
  return 0;
}

L
Liu Jicong 已提交
6583
void tFreeSVCreateTbRsp(void *param) {
D
dapan1121 已提交
6584 6585 6586
  if (NULL == param) {
    return;
  }
L
Liu Jicong 已提交
6587 6588

  SVCreateTbRsp *pRsp = (SVCreateTbRsp *)param;
D
dapan1121 已提交
6589 6590 6591 6592
  if (pRsp->pMeta) {
    taosMemoryFree(pRsp->pMeta->pSchemas);
    taosMemoryFree(pRsp->pMeta);
  }
6593 6594
}

H
Hongze Cheng 已提交
6595
// TDMT_VND_DROP_TABLE =================
H
Hongze Cheng 已提交
6596
static int32_t tEncodeSVDropTbReq(SEncoder *pCoder, const SVDropTbReq *pReq) {
H
Hongze Cheng 已提交
6597 6598 6599
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeCStr(pCoder, pReq->name) < 0) return -1;
6600
  if (tEncodeU64(pCoder, pReq->suid) < 0) return -1;
X
Xiaoyu Wang 已提交
6601
  if (tEncodeI8(pCoder, pReq->igNotExists) < 0) return -1;
H
Hongze Cheng 已提交
6602 6603 6604 6605 6606

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6607
static int32_t tDecodeSVDropTbReq(SDecoder *pCoder, SVDropTbReq *pReq) {
H
Hongze Cheng 已提交
6608 6609 6610
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeCStr(pCoder, &pReq->name) < 0) return -1;
6611
  if (tDecodeU64(pCoder, &pReq->suid) < 0) return -1;
X
Xiaoyu Wang 已提交
6612
  if (tDecodeI8(pCoder, &pReq->igNotExists) < 0) return -1;
H
Hongze Cheng 已提交
6613 6614 6615 6616 6617

  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6618
static int32_t tEncodeSVDropTbRsp(SEncoder *pCoder, const SVDropTbRsp *pReq) {
H
Hongze Cheng 已提交
6619 6620 6621 6622 6623 6624 6625 6626
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32(pCoder, pReq->code) < 0) return -1;

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6627
static int32_t tDecodeSVDropTbRsp(SDecoder *pCoder, SVDropTbRsp *pReq) {
H
Hongze Cheng 已提交
6628 6629 6630 6631 6632 6633 6634 6635
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32(pCoder, &pReq->code) < 0) return -1;

  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6636
int32_t tEncodeSVDropTbBatchReq(SEncoder *pCoder, const SVDropTbBatchReq *pReq) {
H
Hongze Cheng 已提交
6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651
  int32_t      nReqs = taosArrayGetSize(pReq->pArray);
  SVDropTbReq *pDropTbReq;

  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32v(pCoder, nReqs) < 0) return -1;
  for (int iReq = 0; iReq < nReqs; iReq++) {
    pDropTbReq = (SVDropTbReq *)taosArrayGet(pReq->pArray, iReq);
    if (tEncodeSVDropTbReq(pCoder, pDropTbReq) < 0) return -1;
  }

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6652
int32_t tDecodeSVDropTbBatchReq(SDecoder *pCoder, SVDropTbBatchReq *pReq) {
H
Hongze Cheng 已提交
6653 6654 6655
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pReq->nReqs) < 0) return -1;
H
Hongze Cheng 已提交
6656
  pReq->pReqs = (SVDropTbReq *)tDecoderMalloc(pCoder, sizeof(SVDropTbReq) * pReq->nReqs);
H
Hongze Cheng 已提交
6657 6658 6659 6660 6661 6662 6663 6664 6665
  if (pReq->pReqs == NULL) return -1;
  for (int iReq = 0; iReq < pReq->nReqs; iReq++) {
    if (tDecodeSVDropTbReq(pCoder, pReq->pReqs + iReq) < 0) return -1;
  }

  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6666
int32_t tEncodeSVDropTbBatchRsp(SEncoder *pCoder, const SVDropTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678
  int32_t nRsps = taosArrayGetSize(pRsp->pArray);
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32v(pCoder, nRsps) < 0) return -1;
  for (int iRsp = 0; iRsp < nRsps; iRsp++) {
    if (tEncodeSVDropTbRsp(pCoder, (SVDropTbRsp *)taosArrayGet(pRsp->pArray, iRsp)) < 0) return -1;
  }

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6679
int32_t tDecodeSVDropTbBatchRsp(SDecoder *pCoder, SVDropTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
6680 6681 6682
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pRsp->nRsps) < 0) return -1;
H
Hongze Cheng 已提交
6683
  pRsp->pRsps = (SVDropTbRsp *)tDecoderMalloc(pCoder, sizeof(SVDropTbRsp) * pRsp->nRsps);
H
Hongze Cheng 已提交
6684 6685 6686 6687 6688 6689 6690 6691 6692
  if (pRsp->pRsps == NULL) return -1;
  for (int iRsp = 0; iRsp < pRsp->nRsps; iRsp++) {
    if (tDecodeSVDropTbRsp(pCoder, pRsp->pRsps + iRsp) < 0) return -1;
  }

  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6693
int32_t tEncodeSVDropStbReq(SEncoder *pCoder, const SVDropStbReq *pReq) {
H
Hongze Cheng 已提交
6694 6695 6696 6697 6698 6699 6700 6701 6702
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeCStr(pCoder, pReq->name) < 0) return -1;
  if (tEncodeI64(pCoder, pReq->suid) < 0) return -1;

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6703
int32_t tDecodeSVDropStbReq(SDecoder *pCoder, SVDropStbReq *pReq) {
H
Hongze Cheng 已提交
6704 6705 6706 6707 6708
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeCStr(pCoder, &pReq->name) < 0) return -1;
  if (tDecodeI64(pCoder, &pReq->suid) < 0) return -1;

H
Hongze Cheng 已提交
6709 6710
  tEndDecode(pCoder);
  return 0;
6711
}
H
Hongze Cheng 已提交
6712

H
Hongze Cheng 已提交
6713
static int32_t tEncodeSVSubmitBlk(SEncoder *pCoder, const SVSubmitBlk *pBlock, int32_t flags) {
H
Hongze Cheng 已提交
6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI64(pCoder, pBlock->suid) < 0) return -1;
  if (tEncodeI64(pCoder, pBlock->uid) < 0) return -1;
  if (tEncodeI32v(pCoder, pBlock->sver) < 0) return -1;
  if (tEncodeBinary(pCoder, pBlock->pData, pBlock->nData) < 0) return -1;

  if (flags & TD_AUTO_CREATE_TABLE) {
    if (tEncodeSVCreateTbReq(pCoder, &pBlock->cTbReq) < 0) return -1;
  }

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6729
static int32_t tDecodeSVSubmitBlk(SDecoder *pCoder, SVSubmitBlk *pBlock, int32_t flags) {
H
Hongze Cheng 已提交
6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI64(pCoder, &pBlock->suid) < 0) return -1;
  if (tDecodeI64(pCoder, &pBlock->uid) < 0) return -1;
  if (tDecodeI32v(pCoder, &pBlock->sver) < 0) return -1;
  if (tDecodeBinary(pCoder, &pBlock->pData, &pBlock->nData) < 0) return -1;

  if (flags & TD_AUTO_CREATE_TABLE) {
    if (tDecodeSVCreateTbReq(pCoder, &pBlock->cTbReq) < 0) return -1;
  }

  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6745
int32_t tEncodeSVSubmitReq(SEncoder *pCoder, const SVSubmitReq *pReq) {
H
Hongze Cheng 已提交
6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759
  int32_t nBlocks = taosArrayGetSize(pReq->pArray);

  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32v(pCoder, pReq->flags) < 0) return -1;
  if (tEncodeI32v(pCoder, nBlocks) < 0) return -1;
  for (int32_t iBlock = 0; iBlock < nBlocks; iBlock++) {
    if (tEncodeSVSubmitBlk(pCoder, (SVSubmitBlk *)taosArrayGet(pReq->pArray, iBlock), pReq->flags) < 0) return -1;
  }

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
6760
int32_t tDecodeSVSubmitReq(SDecoder *pCoder, SVSubmitReq *pReq) {
H
Hongze Cheng 已提交
6761 6762 6763 6764
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pReq->flags) < 0) return -1;
  if (tDecodeI32v(pCoder, &pReq->nBlocks) < 0) return -1;
H
Hongze Cheng 已提交
6765
  pReq->pBlocks = tDecoderMalloc(pCoder, sizeof(SVSubmitBlk) * pReq->nBlocks);
H
Hongze Cheng 已提交
6766 6767 6768 6769 6770 6771 6772
  if (pReq->pBlocks == NULL) return -1;
  for (int32_t iBlock = 0; iBlock < pReq->nBlocks; iBlock++) {
    if (tDecodeSVSubmitBlk(pCoder, pReq->pBlocks + iBlock, pReq->flags) < 0) return -1;
  }

  tEndDecode(pCoder);
  return 0;
L
Liu Jicong 已提交
6773
}
H
Hongze Cheng 已提交
6774 6775 6776 6777

static int32_t tEncodeSSubmitBlkRsp(SEncoder *pEncoder, const SSubmitBlkRsp *pBlock) {
  if (tStartEncode(pEncoder) < 0) return -1;

H
Hongze Cheng 已提交
6778
  if (tEncodeI32(pEncoder, pBlock->code) < 0) return -1;
D
dapan1121 已提交
6779
  if (tEncodeI64(pEncoder, pBlock->uid) < 0) return -1;
D
dapan1121 已提交
6780 6781 6782 6783 6784
  if (pBlock->tblFName) {
    if (tEncodeCStr(pEncoder, pBlock->tblFName) < 0) return -1;
  } else {
    if (tEncodeCStr(pEncoder, "") < 0) return -1;
  }
H
Hongze Cheng 已提交
6785 6786
  if (tEncodeI32v(pEncoder, pBlock->numOfRows) < 0) return -1;
  if (tEncodeI32v(pEncoder, pBlock->affectedRows) < 0) return -1;
H
Hongze Cheng 已提交
6787
  if (tEncodeI64v(pEncoder, pBlock->sver) < 0) return -1;
6788 6789 6790 6791
  if (tEncodeI32(pEncoder, pBlock->pMeta ? 1 : 0) < 0) return -1;
  if (pBlock->pMeta) {
    if (tEncodeSTableMetaRsp(pEncoder, pBlock->pMeta) < 0) return -1;
  }
H
Hongze Cheng 已提交
6792 6793 6794 6795 6796 6797 6798 6799

  tEndEncode(pEncoder);
  return 0;
}

static int32_t tDecodeSSubmitBlkRsp(SDecoder *pDecoder, SSubmitBlkRsp *pBlock) {
  if (tStartDecode(pDecoder) < 0) return -1;

H
Hongze Cheng 已提交
6800
  if (tDecodeI32(pDecoder, &pBlock->code) < 0) return -1;
D
dapan1121 已提交
6801 6802 6803 6804
  if (tDecodeI64(pDecoder, &pBlock->uid) < 0) return -1;
  pBlock->tblFName = taosMemoryCalloc(TSDB_TABLE_FNAME_LEN, 1);
  if (NULL == pBlock->tblFName) return -1;
  if (tDecodeCStrTo(pDecoder, pBlock->tblFName) < 0) return -1;
H
Hongze Cheng 已提交
6805 6806
  if (tDecodeI32v(pDecoder, &pBlock->numOfRows) < 0) return -1;
  if (tDecodeI32v(pDecoder, &pBlock->affectedRows) < 0) return -1;
H
Hongze Cheng 已提交
6807
  if (tDecodeI64v(pDecoder, &pBlock->sver) < 0) return -1;
L
Liu Jicong 已提交
6808

6809 6810 6811 6812 6813 6814 6815 6816 6817
  int32_t meta = 0;
  if (tDecodeI32(pDecoder, &meta) < 0) return -1;
  if (meta) {
    pBlock->pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
    if (NULL == pBlock->pMeta) return -1;
    if (tDecodeSTableMetaRsp(pDecoder, pBlock->pMeta) < 0) return -1;
  } else {
    pBlock->pMeta = NULL;
  }
H
Hongze Cheng 已提交
6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844

  tEndDecode(pDecoder);
  return 0;
}

int32_t tEncodeSSubmitRsp(SEncoder *pEncoder, const SSubmitRsp *pRsp) {
  int32_t nBlocks = taosArrayGetSize(pRsp->pArray);

  if (tStartEncode(pEncoder) < 0) return -1;

  if (tEncodeI32v(pEncoder, pRsp->numOfRows) < 0) return -1;
  if (tEncodeI32v(pEncoder, pRsp->affectedRows) < 0) return -1;
  if (tEncodeI32v(pEncoder, nBlocks) < 0) return -1;
  for (int32_t iBlock = 0; iBlock < nBlocks; iBlock++) {
    if (tEncodeSSubmitBlkRsp(pEncoder, (SSubmitBlkRsp *)taosArrayGet(pRsp->pArray, iBlock)) < 0) return -1;
  }

  tEndEncode(pEncoder);
  return 0;
}

int32_t tDecodeSSubmitRsp(SDecoder *pDecoder, SSubmitRsp *pRsp) {
  if (tStartDecode(pDecoder) < 0) return -1;

  if (tDecodeI32v(pDecoder, &pRsp->numOfRows) < 0) return -1;
  if (tDecodeI32v(pDecoder, &pRsp->affectedRows) < 0) return -1;
  if (tDecodeI32v(pDecoder, &pRsp->nBlocks) < 0) return -1;
D
dapan 已提交
6845
  pRsp->pBlocks = taosMemoryCalloc(pRsp->nBlocks, sizeof(*pRsp->pBlocks));
H
Hongze Cheng 已提交
6846 6847 6848 6849 6850
  if (pRsp->pBlocks == NULL) return -1;
  for (int32_t iBlock = 0; iBlock < pRsp->nBlocks; iBlock++) {
    if (tDecodeSSubmitBlkRsp(pDecoder, pRsp->pBlocks + iBlock) < 0) return -1;
  }

H
Hongze Cheng 已提交
6851
  tEndDecode(pDecoder);
D
dapan 已提交
6852
  tDecoderClear(pDecoder);
H
Hongze Cheng 已提交
6853 6854
  return 0;
}
D
dapan 已提交
6855

L
Liu Jicong 已提交
6856
void tFreeSSubmitBlkRsp(void *param) {
6857 6858 6859
  if (NULL == param) {
    return;
  }
L
Liu Jicong 已提交
6860 6861

  SSubmitBlkRsp *pRsp = (SSubmitBlkRsp *)param;
6862 6863 6864 6865 6866 6867 6868 6869

  taosMemoryFree(pRsp->tblFName);
  if (pRsp->pMeta) {
    taosMemoryFree(pRsp->pMeta->pSchemas);
    taosMemoryFree(pRsp->pMeta);
  }
}

D
dapan 已提交
6870 6871 6872 6873 6874 6875 6876
void tFreeSSubmitRsp(SSubmitRsp *pRsp) {
  if (NULL == pRsp) return;

  if (pRsp->pBlocks) {
    for (int32_t i = 0; i < pRsp->nBlocks; ++i) {
      SSubmitBlkRsp *sRsp = pRsp->pBlocks + i;
      taosMemoryFree(sRsp->tblFName);
6877 6878
      tFreeSTableMetaRsp(sRsp->pMeta);
      taosMemoryFree(sRsp->pMeta);
D
dapan 已提交
6879 6880 6881 6882 6883 6884 6885
    }

    taosMemoryFree(pRsp->pBlocks);
  }

  taosMemoryFree(pRsp);
}
H
Hongze Cheng 已提交
6886 6887 6888 6889 6890 6891

int32_t tEncodeSVAlterTbReq(SEncoder *pEncoder, const SVAlterTbReq *pReq) {
  if (tStartEncode(pEncoder) < 0) return -1;

  if (tEncodeCStr(pEncoder, pReq->tbName) < 0) return -1;
  if (tEncodeI8(pEncoder, pReq->action) < 0) return -1;
6892
  if (tEncodeI32(pEncoder, pReq->colId) < 0) return -1;
H
Hongze Cheng 已提交
6893 6894
  switch (pReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
H
fix  
Hongze Cheng 已提交
6895
      if (tEncodeCStr(pEncoder, pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
6896
      if (tEncodeI8(pEncoder, pReq->type) < 0) return -1;
H
Hongze Cheng 已提交
6897
      if (tEncodeI8(pEncoder, pReq->flags) < 0) return -1;
H
Hongze Cheng 已提交
6898 6899 6900
      if (tEncodeI32v(pEncoder, pReq->bytes) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
H
fix  
Hongze Cheng 已提交
6901
      if (tEncodeCStr(pEncoder, pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
6902 6903
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
H
fix  
Hongze Cheng 已提交
6904
      if (tEncodeCStr(pEncoder, pReq->colName) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
6905
      if (tEncodeI8(pEncoder, pReq->colModType) < 0) return -1;
H
Hongze Cheng 已提交
6906 6907 6908
      if (tEncodeI32v(pEncoder, pReq->colModBytes) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
H
fix  
Hongze Cheng 已提交
6909
      if (tEncodeCStr(pEncoder, pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
6910 6911 6912 6913 6914
      if (tEncodeCStr(pEncoder, pReq->colNewName) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
      if (tEncodeCStr(pEncoder, pReq->tagName) < 0) return -1;
      if (tEncodeI8(pEncoder, pReq->isNull) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
6915
      if (tEncodeI8(pEncoder, pReq->tagType) < 0) return -1;
H
Hongze Cheng 已提交
6916 6917 6918 6919 6920 6921 6922 6923 6924
      if (!pReq->isNull) {
        if (tEncodeBinary(pEncoder, pReq->pTagVal, pReq->nTagVal) < 0) return -1;
      }
      break;
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
      if (tEncodeI8(pEncoder, pReq->updateTTL) < 0) return -1;
      if (pReq->updateTTL) {
        if (tEncodeI32v(pEncoder, pReq->newTTL) < 0) return -1;
      }
wmmhello's avatar
wmmhello 已提交
6925 6926
      if (tEncodeI32v(pEncoder, pReq->newCommentLen) < 0) return -1;
      if (pReq->newCommentLen > 0) {
H
Hongze Cheng 已提交
6927 6928 6929 6930 6931 6932
        if (tEncodeCStr(pEncoder, pReq->newComment) < 0) return -1;
      }
      break;
    default:
      break;
  }
6933
  if (tEncodeI64(pEncoder, pReq->ctimeMs) < 0) return -1;
H
Hongze Cheng 已提交
6934 6935 6936 6937 6938

  tEndEncode(pEncoder);
  return 0;
}

6939
static int32_t tDecodeSVAlterTbReqCommon(SDecoder *pDecoder, SVAlterTbReq *pReq) {
H
Hongze Cheng 已提交
6940 6941
  if (tDecodeCStr(pDecoder, &pReq->tbName) < 0) return -1;
  if (tDecodeI8(pDecoder, &pReq->action) < 0) return -1;
6942
  if (tDecodeI32(pDecoder, &pReq->colId) < 0) return -1;
H
Hongze Cheng 已提交
6943 6944
  switch (pReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
H
fix  
Hongze Cheng 已提交
6945
      if (tDecodeCStr(pDecoder, &pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
6946
      if (tDecodeI8(pDecoder, &pReq->type) < 0) return -1;
H
Hongze Cheng 已提交
6947
      if (tDecodeI8(pDecoder, &pReq->flags) < 0) return -1;
H
Hongze Cheng 已提交
6948 6949 6950
      if (tDecodeI32v(pDecoder, &pReq->bytes) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
H
fix  
Hongze Cheng 已提交
6951
      if (tDecodeCStr(pDecoder, &pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
6952 6953
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
H
fix  
Hongze Cheng 已提交
6954
      if (tDecodeCStr(pDecoder, &pReq->colName) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
6955
      if (tDecodeI8(pDecoder, &pReq->colModType) < 0) return -1;
H
Hongze Cheng 已提交
6956 6957 6958
      if (tDecodeI32v(pDecoder, &pReq->colModBytes) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
H
fix  
Hongze Cheng 已提交
6959
      if (tDecodeCStr(pDecoder, &pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
6960 6961 6962 6963 6964
      if (tDecodeCStr(pDecoder, &pReq->colNewName) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
      if (tDecodeCStr(pDecoder, &pReq->tagName) < 0) return -1;
      if (tDecodeI8(pDecoder, &pReq->isNull) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
6965
      if (tDecodeI8(pDecoder, &pReq->tagType) < 0) return -1;
H
Hongze Cheng 已提交
6966 6967 6968 6969 6970 6971 6972 6973 6974
      if (!pReq->isNull) {
        if (tDecodeBinary(pDecoder, &pReq->pTagVal, &pReq->nTagVal) < 0) return -1;
      }
      break;
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
      if (tDecodeI8(pDecoder, &pReq->updateTTL) < 0) return -1;
      if (pReq->updateTTL) {
        if (tDecodeI32v(pDecoder, &pReq->newTTL) < 0) return -1;
      }
wmmhello's avatar
wmmhello 已提交
6975 6976
      if (tDecodeI32v(pDecoder, &pReq->newCommentLen) < 0) return -1;
      if (pReq->newCommentLen > 0) {
H
Hongze Cheng 已提交
6977 6978 6979 6980 6981 6982
        if (tDecodeCStr(pDecoder, &pReq->newComment) < 0) return -1;
      }
      break;
    default:
      break;
  }
6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998
  return 0;
}

int32_t tDecodeSVAlterTbReq(SDecoder *pDecoder, SVAlterTbReq *pReq) {
  if (tStartDecode(pDecoder) < 0) return -1;
  if (tDecodeSVAlterTbReqCommon(pDecoder, pReq) < 0) return -1;

  pReq->ctimeMs = 0;
  if (!tDecodeIsEnd(pDecoder)) {
    if (tDecodeI64(pDecoder, &pReq->ctimeMs) < 0) return -1;
  }

  tEndDecode(pDecoder);
  return 0;
}

Y
yihaoDeng 已提交
6999
int32_t tDecodeSVAlterTbReqSetCtime(SDecoder *pDecoder, SVAlterTbReq *pReq, int64_t ctimeMs) {
7000 7001 7002
  if (tStartDecode(pDecoder) < 0) return -1;
  if (tDecodeSVAlterTbReqCommon(pDecoder, pReq) < 0) return -1;

S
Shungang Li 已提交
7003 7004 7005 7006 7007
  pReq->ctimeMs = 0;
  if (!tDecodeIsEnd(pDecoder)) {
    *(int64_t *)(pDecoder->data + pDecoder->pos) = ctimeMs;
    if (tDecodeI64(pDecoder, &pReq->ctimeMs) < 0) return -1;
  }
H
Hongze Cheng 已提交
7008 7009 7010 7011 7012 7013 7014 7015

  tEndDecode(pDecoder);
  return 0;
}

int32_t tEncodeSVAlterTbRsp(SEncoder *pEncoder, const SVAlterTbRsp *pRsp) {
  if (tStartEncode(pEncoder) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->code) < 0) return -1;
D
dapan1121 已提交
7016 7017 7018 7019
  if (tEncodeI32(pEncoder, pRsp->pMeta ? 1 : 0) < 0) return -1;
  if (pRsp->pMeta) {
    if (tEncodeSTableMetaRsp(pEncoder, pRsp->pMeta) < 0) return -1;
  }
H
Hongze Cheng 已提交
7020 7021 7022 7023 7024
  tEndEncode(pEncoder);
  return 0;
}

int32_t tDecodeSVAlterTbRsp(SDecoder *pDecoder, SVAlterTbRsp *pRsp) {
D
dapan1121 已提交
7025
  int32_t meta = 0;
H
Hongze Cheng 已提交
7026 7027
  if (tStartDecode(pDecoder) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->code) < 0) return -1;
D
dapan1121 已提交
7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038
  if (tDecodeI32(pDecoder, &meta) < 0) return -1;
  if (meta) {
    pRsp->pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
    if (NULL == pRsp->pMeta) return -1;
    if (tDecodeSTableMetaRsp(pDecoder, pRsp->pMeta) < 0) return -1;
  }
  tEndDecode(pDecoder);
  return 0;
}

int32_t tDeserializeSVAlterTbRsp(void *buf, int32_t bufLen, SVAlterTbRsp *pRsp) {
C
Cary Xu 已提交
7039
  int32_t  meta = 0;
D
dapan1121 已提交
7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->code) < 0) return -1;
  if (tDecodeI32(&decoder, &meta) < 0) return -1;
  if (meta) {
    pRsp->pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
    if (NULL == pRsp->pMeta) return -1;
    if (tDecodeSTableMetaRsp(&decoder, pRsp->pMeta) < 0) return -1;
  }
  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

int32_t tEncodeSMAlterStbRsp(SEncoder *pEncoder, const SMAlterStbRsp *pRsp) {
  if (tStartEncode(pEncoder) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->pMeta->pSchemas ? 1 : 0) < 0) return -1;
  if (pRsp->pMeta->pSchemas) {
    if (tEncodeSTableMetaRsp(pEncoder, pRsp->pMeta) < 0) return -1;
  }
  tEndEncode(pEncoder);
  return 0;
}

int32_t tDecodeSMAlterStbRsp(SDecoder *pDecoder, SMAlterStbRsp *pRsp) {
  int32_t meta = 0;
  if (tStartDecode(pDecoder) < 0) return -1;
  if (tDecodeI32(pDecoder, &meta) < 0) return -1;
  if (meta) {
    pRsp->pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
    if (NULL == pRsp->pMeta) return -1;
    if (tDecodeSTableMetaRsp(pDecoder, pRsp->pMeta) < 0) return -1;
  }
H
Hongze Cheng 已提交
7075 7076 7077
  tEndDecode(pDecoder);
  return 0;
}
D
dapan1121 已提交
7078 7079

int32_t tDeserializeSMAlterStbRsp(void *buf, int32_t bufLen, SMAlterStbRsp *pRsp) {
C
Cary Xu 已提交
7080
  int32_t  meta = 0;
D
dapan1121 已提交
7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &meta) < 0) return -1;
  if (meta) {
    pRsp->pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
    if (NULL == pRsp->pMeta) return -1;
    if (tDecodeSTableMetaRsp(&decoder, pRsp->pMeta) < 0) return -1;
  }
  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

C
Cary Xu 已提交
7096
void tFreeSMAlterStbRsp(SMAlterStbRsp *pRsp) {
D
dapan1121 已提交
7097 7098 7099 7100 7101 7102 7103 7104 7105
  if (NULL == pRsp) {
    return;
  }

  if (pRsp->pMeta) {
    taosMemoryFree(pRsp->pMeta->pSchemas);
    taosMemoryFree(pRsp->pMeta);
  }
}
7106

7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157
int32_t tEncodeSMCreateStbRsp(SEncoder *pEncoder, const SMCreateStbRsp *pRsp) {
  if (tStartEncode(pEncoder) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->pMeta->pSchemas ? 1 : 0) < 0) return -1;
  if (pRsp->pMeta->pSchemas) {
    if (tEncodeSTableMetaRsp(pEncoder, pRsp->pMeta) < 0) return -1;
  }
  tEndEncode(pEncoder);
  return 0;
}

int32_t tDecodeSMCreateStbRsp(SDecoder *pDecoder, SMCreateStbRsp *pRsp) {
  int32_t meta = 0;
  if (tStartDecode(pDecoder) < 0) return -1;
  if (tDecodeI32(pDecoder, &meta) < 0) return -1;
  if (meta) {
    pRsp->pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
    if (NULL == pRsp->pMeta) return -1;
    if (tDecodeSTableMetaRsp(pDecoder, pRsp->pMeta) < 0) return -1;
  }
  tEndDecode(pDecoder);
  return 0;
}

int32_t tDeserializeSMCreateStbRsp(void *buf, int32_t bufLen, SMCreateStbRsp *pRsp) {
  int32_t  meta = 0;
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &meta) < 0) return -1;
  if (meta) {
    pRsp->pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
    if (NULL == pRsp->pMeta) return -1;
    if (tDecodeSTableMetaRsp(&decoder, pRsp->pMeta) < 0) return -1;
  }
  tEndDecode(&decoder);
  tDecoderClear(&decoder);
  return 0;
}

void tFreeSMCreateStbRsp(SMCreateStbRsp *pRsp) {
  if (NULL == pRsp) {
    return;
  }

  if (pRsp->pMeta) {
    taosMemoryFree(pRsp->pMeta->pSchemas);
    taosMemoryFree(pRsp->pMeta);
  }
}

L
Liu Jicong 已提交
7158 7159
int32_t tEncodeSTqOffsetVal(SEncoder *pEncoder, const STqOffsetVal *pOffsetVal) {
  if (tEncodeI8(pEncoder, pOffsetVal->type) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
7160
  if (pOffsetVal->type == TMQ_OFFSET__SNAPSHOT_DATA || pOffsetVal->type == TMQ_OFFSET__SNAPSHOT_META) {
L
Liu Jicong 已提交
7161 7162 7163 7164
    if (tEncodeI64(pEncoder, pOffsetVal->uid) < 0) return -1;
    if (tEncodeI64(pEncoder, pOffsetVal->ts) < 0) return -1;
  } else if (pOffsetVal->type == TMQ_OFFSET__LOG) {
    if (tEncodeI64(pEncoder, pOffsetVal->version) < 0) return -1;
7165
  } else {
wmmhello's avatar
wmmhello 已提交
7166
    // do nothing
7167 7168 7169 7170
  }
  return 0;
}

L
Liu Jicong 已提交
7171 7172
int32_t tDecodeSTqOffsetVal(SDecoder *pDecoder, STqOffsetVal *pOffsetVal) {
  if (tDecodeI8(pDecoder, &pOffsetVal->type) < 0) return -1;
L
Liu Jicong 已提交
7173
  if (pOffsetVal->type == TMQ_OFFSET__SNAPSHOT_DATA || pOffsetVal->type == TMQ_OFFSET__SNAPSHOT_META) {
L
Liu Jicong 已提交
7174 7175 7176 7177 7178
    if (tDecodeI64(pDecoder, &pOffsetVal->uid) < 0) return -1;
    if (tDecodeI64(pDecoder, &pOffsetVal->ts) < 0) return -1;
  } else if (pOffsetVal->type == TMQ_OFFSET__LOG) {
    if (tDecodeI64(pDecoder, &pOffsetVal->version) < 0) return -1;
  } else {
wmmhello's avatar
wmmhello 已提交
7179
    // do nothing
L
Liu Jicong 已提交
7180 7181 7182 7183 7184 7185
  }
  return 0;
}

int32_t tFormatOffset(char *buf, int32_t maxLen, const STqOffsetVal *pVal) {
  if (pVal->type == TMQ_OFFSET__RESET_NONE) {
7186 7187 7188
    snprintf(buf, maxLen, "none");
  } else if (pVal->type == TMQ_OFFSET__RESET_EARLIEST) {
    snprintf(buf, maxLen, "earliest");
L
Liu Jicong 已提交
7189
  } else if (pVal->type == TMQ_OFFSET__RESET_LATEST) {
7190
    snprintf(buf, maxLen, "latest");
L
Liu Jicong 已提交
7191
  } else if (pVal->type == TMQ_OFFSET__LOG) {
wmmhello's avatar
wmmhello 已提交
7192
    snprintf(buf, maxLen, "wal:%" PRId64, pVal->version);
L
Liu Jicong 已提交
7193
  } else if (pVal->type == TMQ_OFFSET__SNAPSHOT_DATA || pVal->type == TMQ_OFFSET__SNAPSHOT_META) {
wmmhello's avatar
wmmhello 已提交
7194
    snprintf(buf, maxLen, "tsdb:%" PRId64 "|%" PRId64, pVal->uid, pVal->ts);
7195
  } else {
D
dapan1121 已提交
7196
    return TSDB_CODE_INVALID_PARA;
7197
  }
D
dapan1121 已提交
7198

L
Liu Jicong 已提交
7199 7200 7201 7202 7203 7204 7205 7206 7207 7208
  return 0;
}

bool tOffsetEqual(const STqOffsetVal *pLeft, const STqOffsetVal *pRight) {
  if (pLeft->type == pRight->type) {
    if (pLeft->type == TMQ_OFFSET__LOG) {
      return pLeft->version == pRight->version;
    } else if (pLeft->type == TMQ_OFFSET__SNAPSHOT_DATA) {
      return pLeft->uid == pRight->uid && pLeft->ts == pRight->ts;
    } else if (pLeft->type == TMQ_OFFSET__SNAPSHOT_META) {
wmmhello's avatar
wmmhello 已提交
7209
      return pLeft->uid == pRight->uid;
L
Liu Jicong 已提交
7210
    } else {
7211
      ASSERT(0);
7212
      /*ASSERT(pLeft->type == TMQ_OFFSET__RESET_NONE || pLeft->type == TMQ_OFFSET__RESET_EARLIEST ||*/
L
Liu Jicong 已提交
7213 7214
      /*pLeft->type == TMQ_OFFSET__RESET_LATEST);*/
      /*return true;*/
L
Liu Jicong 已提交
7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227
    }
  }
  return false;
}

int32_t tEncodeSTqOffset(SEncoder *pEncoder, const STqOffset *pOffset) {
  if (tEncodeSTqOffsetVal(pEncoder, &pOffset->val) < 0) return -1;
  if (tEncodeCStr(pEncoder, pOffset->subKey) < 0) return -1;
  return 0;
}

int32_t tDecodeSTqOffset(SDecoder *pDecoder, STqOffset *pOffset) {
  if (tDecodeSTqOffsetVal(pDecoder, &pOffset->val) < 0) return -1;
7228 7229 7230
  if (tDecodeCStrTo(pDecoder, pOffset->subKey) < 0) return -1;
  return 0;
}
L
Liu Jicong 已提交
7231

Y
yihaoDeng 已提交
7232
int32_t tEncodeMqVgOffset(SEncoder *pEncoder, const SMqVgOffset *pOffset) {
7233
  if (tEncodeSTqOffset(pEncoder, &pOffset->offset) < 0) return -1;
7234
  if (tEncodeI64(pEncoder, pOffset->consumerId) < 0) return -1;
7235 7236 7237
  return 0;
}

Y
yihaoDeng 已提交
7238
int32_t tDecodeMqVgOffset(SDecoder *pDecoder, SMqVgOffset *pOffset) {
7239
  if (tDecodeSTqOffset(pDecoder, &pOffset->offset) < 0) return -1;
7240
  if (tDecodeI64(pDecoder, &pOffset->consumerId) < 0) return -1;
7241 7242 7243
  return 0;
}

7244
int32_t tEncodeSTqCheckInfo(SEncoder *pEncoder, const STqCheckInfo *pInfo) {
L
Liu Jicong 已提交
7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255
  if (tEncodeCStr(pEncoder, pInfo->topic) < 0) return -1;
  if (tEncodeI64(pEncoder, pInfo->ntbUid) < 0) return -1;
  int32_t sz = taosArrayGetSize(pInfo->colIdList);
  if (tEncodeI32(pEncoder, sz) < 0) return -1;
  for (int32_t i = 0; i < sz; i++) {
    int16_t colId = *(int16_t *)taosArrayGet(pInfo->colIdList, i);
    if (tEncodeI16(pEncoder, colId) < 0) return -1;
  }
  return pEncoder->pos;
}

7256
int32_t tDecodeSTqCheckInfo(SDecoder *pDecoder, STqCheckInfo *pInfo) {
L
Liu Jicong 已提交
7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269
  if (tDecodeCStrTo(pDecoder, pInfo->topic) < 0) return -1;
  if (tDecodeI64(pDecoder, &pInfo->ntbUid) < 0) return -1;
  int32_t sz;
  if (tDecodeI32(pDecoder, &sz) < 0) return -1;
  pInfo->colIdList = taosArrayInit(sz, sizeof(int16_t));
  if (pInfo->colIdList == NULL) return -1;
  for (int32_t i = 0; i < sz; i++) {
    int16_t colId;
    if (tDecodeI16(pDecoder, &colId) < 0) return -1;
    taosArrayPush(pInfo->colIdList, &colId);
  }
  return 0;
}
L
Liu Jicong 已提交
7270
void tDeleteSTqCheckInfo(STqCheckInfo *pInfo) { taosArrayDestroy(pInfo->colIdList); }
L
Liu Jicong 已提交
7271

H
Hongze Cheng 已提交
7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283
int32_t tEncodeDeleteRes(SEncoder *pCoder, const SDeleteRes *pRes) {
  int32_t nUid = taosArrayGetSize(pRes->uidList);

  if (tEncodeU64(pCoder, pRes->suid) < 0) return -1;
  if (tEncodeI32v(pCoder, nUid) < 0) return -1;
  for (int32_t iUid = 0; iUid < nUid; iUid++) {
    if (tEncodeU64(pCoder, *(uint64_t *)taosArrayGet(pRes->uidList, iUid)) < 0) return -1;
  }
  if (tEncodeI64(pCoder, pRes->skey) < 0) return -1;
  if (tEncodeI64(pCoder, pRes->ekey) < 0) return -1;
  if (tEncodeI64v(pCoder, pRes->affectedRows) < 0) return -1;

wmmhello's avatar
wmmhello 已提交
7284
  if (tEncodeCStr(pCoder, pRes->tableFName) < 0) return -1;
7285
  if (tEncodeCStr(pCoder, pRes->tsColName) < 0) return -1;
7286
  if (tEncodeI64(pCoder, pRes->ctimeMs) < 0) return -1;
H
Hongze Cheng 已提交
7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297
  return 0;
}

int32_t tDecodeDeleteRes(SDecoder *pCoder, SDeleteRes *pRes) {
  int32_t  nUid;
  uint64_t uid;

  if (tDecodeU64(pCoder, &pRes->suid) < 0) return -1;
  if (tDecodeI32v(pCoder, &nUid) < 0) return -1;
  for (int32_t iUid = 0; iUid < nUid; iUid++) {
    if (tDecodeU64(pCoder, &uid) < 0) return -1;
wmmhello's avatar
wmmhello 已提交
7298
    if (pRes->uidList) taosArrayPush(pRes->uidList, &uid);
H
Hongze Cheng 已提交
7299 7300 7301 7302 7303
  }
  if (tDecodeI64(pCoder, &pRes->skey) < 0) return -1;
  if (tDecodeI64(pCoder, &pRes->ekey) < 0) return -1;
  if (tDecodeI64v(pCoder, &pRes->affectedRows) < 0) return -1;

wmmhello's avatar
wmmhello 已提交
7304
  if (tDecodeCStrTo(pCoder, pRes->tableFName) < 0) return -1;
7305
  if (tDecodeCStrTo(pCoder, pRes->tsColName) < 0) return -1;
7306 7307 7308 7309 7310

  pRes->ctimeMs = 0;
  if (!tDecodeIsEnd(pCoder)) {
    if (tDecodeI64(pCoder, &pRes->ctimeMs) < 0) return -1;
  }
H
Hongze Cheng 已提交
7311
  return 0;
7312
}
7313

7314
int32_t tEncodeMqMetaRsp(SEncoder *pEncoder, const SMqMetaRsp *pRsp) {
7315
  if (tEncodeSTqOffsetVal(pEncoder, &pRsp->rspOffset) < 0) return -1;
L
Liu Jicong 已提交
7316 7317
  if (tEncodeI16(pEncoder, pRsp->resMsgType)) return -1;
  if (tEncodeBinary(pEncoder, pRsp->metaRsp, pRsp->metaRspLen)) return -1;
7318 7319 7320
  return 0;
}

7321
int32_t tDecodeMqMetaRsp(SDecoder *pDecoder, SMqMetaRsp *pRsp) {
7322 7323
  if (tDecodeSTqOffsetVal(pDecoder, &pRsp->rspOffset) < 0) return -1;
  if (tDecodeI16(pDecoder, &pRsp->resMsgType) < 0) return -1;
L
Liu Jicong 已提交
7324
  if (tDecodeBinaryAlloc(pDecoder, &pRsp->metaRsp, (uint64_t *)&pRsp->metaRspLen) < 0) return -1;
7325 7326 7327
  return 0;
}

7328
int32_t tEncodeMqDataRsp(SEncoder *pEncoder, const SMqDataRsp *pRsp) {
L
Liu Jicong 已提交
7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352
  if (tEncodeSTqOffsetVal(pEncoder, &pRsp->reqOffset) < 0) return -1;
  if (tEncodeSTqOffsetVal(pEncoder, &pRsp->rspOffset) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->blockNum) < 0) return -1;
  if (pRsp->blockNum != 0) {
    if (tEncodeI8(pEncoder, pRsp->withTbName) < 0) return -1;
    if (tEncodeI8(pEncoder, pRsp->withSchema) < 0) return -1;

    for (int32_t i = 0; i < pRsp->blockNum; i++) {
      int32_t bLen = *(int32_t *)taosArrayGet(pRsp->blockDataLen, i);
      void   *data = taosArrayGetP(pRsp->blockData, i);
      if (tEncodeBinary(pEncoder, (const uint8_t *)data, bLen) < 0) return -1;
      if (pRsp->withSchema) {
        SSchemaWrapper *pSW = (SSchemaWrapper *)taosArrayGetP(pRsp->blockSchema, i);
        if (tEncodeSSchemaWrapper(pEncoder, pSW) < 0) return -1;
      }
      if (pRsp->withTbName) {
        char *tbName = (char *)taosArrayGetP(pRsp->blockTbName, i);
        if (tEncodeCStr(pEncoder, tbName) < 0) return -1;
      }
    }
  }
  return 0;
}

7353
int32_t tDecodeMqDataRsp(SDecoder *pDecoder, SMqDataRsp *pRsp) {
L
Liu Jicong 已提交
7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379
  if (tDecodeSTqOffsetVal(pDecoder, &pRsp->reqOffset) < 0) return -1;
  if (tDecodeSTqOffsetVal(pDecoder, &pRsp->rspOffset) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->blockNum) < 0) return -1;
  if (pRsp->blockNum != 0) {
    pRsp->blockData = taosArrayInit(pRsp->blockNum, sizeof(void *));
    pRsp->blockDataLen = taosArrayInit(pRsp->blockNum, sizeof(int32_t));
    if (tDecodeI8(pDecoder, &pRsp->withTbName) < 0) return -1;
    if (tDecodeI8(pDecoder, &pRsp->withSchema) < 0) return -1;
    if (pRsp->withTbName) {
      pRsp->blockTbName = taosArrayInit(pRsp->blockNum, sizeof(void *));
    }
    if (pRsp->withSchema) {
      pRsp->blockSchema = taosArrayInit(pRsp->blockNum, sizeof(void *));
    }

    for (int32_t i = 0; i < pRsp->blockNum; i++) {
      void    *data;
      uint64_t bLen;
      if (tDecodeBinaryAlloc(pDecoder, &data, &bLen) < 0) return -1;
      taosArrayPush(pRsp->blockData, &data);
      int32_t len = bLen;
      taosArrayPush(pRsp->blockDataLen, &len);

      if (pRsp->withSchema) {
        SSchemaWrapper *pSW = (SSchemaWrapper *)taosMemoryCalloc(1, sizeof(SSchemaWrapper));
        if (pSW == NULL) return -1;
L
Liu Jicong 已提交
7380 7381 7382 7383 7384
        if (tDecodeSSchemaWrapper(pDecoder, pSW) < 0) {
          taosMemoryFree(pSW);
          return -1;
        }

L
Liu Jicong 已提交
7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396
        taosArrayPush(pRsp->blockSchema, &pSW);
      }

      if (pRsp->withTbName) {
        char *tbName;
        if (tDecodeCStrAlloc(pDecoder, &tbName) < 0) return -1;
        taosArrayPush(pRsp->blockTbName, &tbName);
      }
    }
  }
  return 0;
}
7397

7398
void tDeleteMqDataRsp(SMqDataRsp *pRsp) {
7399
  pRsp->blockDataLen = taosArrayDestroy(pRsp->blockDataLen);
L
Liu Jicong 已提交
7400
  taosArrayDestroyP(pRsp->blockData, (FDelete)taosMemoryFree);
L
Liu Jicong 已提交
7401
  pRsp->blockData = NULL;
7402
  taosArrayDestroyP(pRsp->blockSchema, (FDelete)tDeleteSchemaWrapper);
L
Liu Jicong 已提交
7403
  pRsp->blockSchema = NULL;
L
Liu Jicong 已提交
7404
  taosArrayDestroyP(pRsp->blockTbName, (FDelete)taosMemoryFree);
L
Liu Jicong 已提交
7405
  pRsp->blockTbName = NULL;
L
Liu Jicong 已提交
7406 7407
}

L
Liu Jicong 已提交
7408
int32_t tEncodeSTaosxRsp(SEncoder *pEncoder, const STaosxRsp *pRsp) {
7409
  if (tEncodeMqDataRsp(pEncoder, (const SMqDataRsp *)pRsp) < 0) return -1;
L
Liu Jicong 已提交
7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422

  if (tEncodeI32(pEncoder, pRsp->createTableNum) < 0) return -1;
  if (pRsp->createTableNum) {
    for (int32_t i = 0; i < pRsp->createTableNum; i++) {
      void   *createTableReq = taosArrayGetP(pRsp->createTableReq, i);
      int32_t createTableLen = *(int32_t *)taosArrayGet(pRsp->createTableLen, i);
      if (tEncodeBinary(pEncoder, createTableReq, createTableLen) < 0) return -1;
    }
  }
  return 0;
}

int32_t tDecodeSTaosxRsp(SDecoder *pDecoder, STaosxRsp *pRsp) {
Y
yihaoDeng 已提交
7423
  if (tDecodeMqDataRsp(pDecoder, (SMqDataRsp *)pRsp) < 0) return -1;
L
Liu Jicong 已提交
7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439

  if (tDecodeI32(pDecoder, &pRsp->createTableNum) < 0) return -1;
  if (pRsp->createTableNum) {
    pRsp->createTableLen = taosArrayInit(pRsp->createTableNum, sizeof(int32_t));
    pRsp->createTableReq = taosArrayInit(pRsp->createTableNum, sizeof(void *));
    for (int32_t i = 0; i < pRsp->createTableNum; i++) {
      void    *pCreate = NULL;
      uint64_t len;
      if (tDecodeBinaryAlloc(pDecoder, &pCreate, &len) < 0) return -1;
      int32_t l = (int32_t)len;
      taosArrayPush(pRsp->createTableLen, &l);
      taosArrayPush(pRsp->createTableReq, &pCreate);
    }
  }
  return 0;
}
7440 7441

void tDeleteSTaosxRsp(STaosxRsp *pRsp) {
H
Haojun Liao 已提交
7442
  pRsp->blockDataLen = taosArrayDestroy(pRsp->blockDataLen);
7443
  taosArrayDestroyP(pRsp->blockData, (FDelete)taosMemoryFree);
7444
  pRsp->blockData = NULL;
7445
  taosArrayDestroyP(pRsp->blockSchema, (FDelete)tDeleteSchemaWrapper);
7446
  pRsp->blockSchema = NULL;
7447
  taosArrayDestroyP(pRsp->blockTbName, (FDelete)taosMemoryFree);
7448
  pRsp->blockTbName = NULL;
7449

H
Haojun Liao 已提交
7450
  pRsp->createTableLen = taosArrayDestroy(pRsp->createTableLen);
7451
  taosArrayDestroyP(pRsp->createTableReq, (FDelete)taosMemoryFree);
7452
  pRsp->createTableReq = NULL;
7453 7454
}

7455
int32_t tEncodeSSingleDeleteReq(SEncoder *pEncoder, const SSingleDeleteReq *pReq) {
7456
  if (tEncodeCStr(pEncoder, pReq->tbname) < 0) return -1;
L
Liu Jicong 已提交
7457 7458
  if (tEncodeI64(pEncoder, pReq->startTs) < 0) return -1;
  if (tEncodeI64(pEncoder, pReq->endTs) < 0) return -1;
7459 7460 7461 7462
  return 0;
}

int32_t tDecodeSSingleDeleteReq(SDecoder *pDecoder, SSingleDeleteReq *pReq) {
7463
  if (tDecodeCStrTo(pDecoder, pReq->tbname) < 0) return -1;
L
Liu Jicong 已提交
7464 7465
  if (tDecodeI64(pDecoder, &pReq->startTs) < 0) return -1;
  if (tDecodeI64(pDecoder, &pReq->endTs) < 0) return -1;
7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476
  return 0;
}

int32_t tEncodeSBatchDeleteReq(SEncoder *pEncoder, const SBatchDeleteReq *pReq) {
  if (tEncodeI64(pEncoder, pReq->suid) < 0) return -1;
  int32_t sz = taosArrayGetSize(pReq->deleteReqs);
  if (tEncodeI32(pEncoder, sz) < 0) return -1;
  for (int32_t i = 0; i < sz; i++) {
    SSingleDeleteReq *pOneReq = taosArrayGet(pReq->deleteReqs, i);
    if (tEncodeSSingleDeleteReq(pEncoder, pOneReq) < 0) return -1;
  }
7477
  if (tEncodeI64(pEncoder, pReq->ctimeMs) < 0) return -1;
7478 7479 7480
  return 0;
}

7481
static int32_t tDecodeSBatchDeleteReqCommon(SDecoder *pDecoder, SBatchDeleteReq *pReq) {
7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493
  if (tDecodeI64(pDecoder, &pReq->suid) < 0) return -1;
  int32_t sz;
  if (tDecodeI32(pDecoder, &sz) < 0) return -1;
  pReq->deleteReqs = taosArrayInit(0, sizeof(SSingleDeleteReq));
  if (pReq->deleteReqs == NULL) return -1;
  for (int32_t i = 0; i < sz; i++) {
    SSingleDeleteReq deleteReq;
    if (tDecodeSSingleDeleteReq(pDecoder, &deleteReq) < 0) return -1;
    taosArrayPush(pReq->deleteReqs, &deleteReq);
  }
  return 0;
}
H
Hongze Cheng 已提交
7494

7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507
int32_t tDecodeSBatchDeleteReq(SDecoder *pDecoder, SBatchDeleteReq *pReq) {
  if (tDecodeSBatchDeleteReqCommon(pDecoder, pReq)) return -1;

  pReq->ctimeMs = 0;
  if (!tDecodeIsEnd(pDecoder)) {
    if (tDecodeI64(pDecoder, &pReq->ctimeMs) < 0) return -1;
  }
  return 0;
}

int32_t tDecodeSBatchDeleteReqSetCtime(SDecoder *pDecoder, SBatchDeleteReq *pReq, int64_t ctimeMs) {
  if (tDecodeSBatchDeleteReqCommon(pDecoder, pReq)) return -1;

S
Shungang Li 已提交
7508 7509 7510 7511 7512
  pReq->ctimeMs = 0;
  if (!tDecodeIsEnd(pDecoder)) {
    *(int64_t *)(pDecoder->data + pDecoder->pos) = ctimeMs;
    if (tDecodeI64(pDecoder, &pReq->ctimeMs) < 0) return -1;
  }
7513 7514 7515
  return 0;
}

H
Hongze Cheng 已提交
7516
static int32_t tEncodeSSubmitTbData(SEncoder *pCoder, const SSubmitTbData *pSubmitTbData) {
H
Hongze Cheng 已提交
7517 7518
  if (tStartEncode(pCoder) < 0) return -1;

H
Hongze Cheng 已提交
7519 7520 7521 7522 7523 7524 7525 7526 7527
  if (tEncodeI32v(pCoder, pSubmitTbData->flags) < 0) return -1;

  // auto create table
  if (pSubmitTbData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
    ASSERT(pSubmitTbData->pCreateTbReq);
    if (tEncodeSVCreateTbReq(pCoder, pSubmitTbData->pCreateTbReq) < 0) return -1;
  }

  // submit data
H
Hongze Cheng 已提交
7528 7529 7530 7531
  if (tEncodeI64(pCoder, pSubmitTbData->suid) < 0) return -1;
  if (tEncodeI64(pCoder, pSubmitTbData->uid) < 0) return -1;
  if (tEncodeI32v(pCoder, pSubmitTbData->sver) < 0) return -1;

H
Hongze Cheng 已提交
7532
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
H
Hongze Cheng 已提交
7533 7534 7535 7536 7537 7538 7539 7540
    uint64_t  nColData = TARRAY_SIZE(pSubmitTbData->aCol);
    SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);

    if (tEncodeU64v(pCoder, nColData) < 0) return -1;

    for (uint64_t i = 0; i < nColData; i++) {
      pCoder->pos += tPutColData(pCoder->data ? pCoder->data + pCoder->pos : NULL, &aColData[i]);
    }
H
Hongze Cheng 已提交
7541
  } else {
H
Hongze Cheng 已提交
7542 7543 7544 7545 7546 7547
    if (tEncodeU64v(pCoder, TARRAY_SIZE(pSubmitTbData->aRowP)) < 0) return -1;

    SRow **rows = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
    for (int32_t iRow = 0; iRow < TARRAY_SIZE(pSubmitTbData->aRowP); ++iRow) {
      if (pCoder->data) memcpy(pCoder->data + pCoder->pos, rows[iRow], rows[iRow]->len);
      pCoder->pos += rows[iRow]->len;
H
Hongze Cheng 已提交
7548 7549
    }
  }
7550
  if (tEncodeI64(pCoder, pSubmitTbData->ctimeMs) < 0) return -1;
H
Hongze Cheng 已提交
7551 7552 7553 7554 7555

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
7556
static int32_t tDecodeSSubmitTbData(SDecoder *pCoder, SSubmitTbData *pSubmitTbData) {
H
Hongze Cheng 已提交
7557 7558 7559 7560 7561 7562 7563
  int32_t code = 0;

  if (tStartDecode(pCoder) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

H
Hongze Cheng 已提交
7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579
  if (tDecodeI32v(pCoder, &pSubmitTbData->flags) < 0) return -1;

  if (pSubmitTbData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
    pSubmitTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
    if (pSubmitTbData->pCreateTbReq == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    if (tDecodeSVCreateTbReq(pCoder, pSubmitTbData->pCreateTbReq) < 0) {
      code = TSDB_CODE_INVALID_MSG;
      goto _exit;
    }
  }

  // submit data
H
Hongze Cheng 已提交
7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592
  if (tDecodeI64(pCoder, &pSubmitTbData->suid) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }
  if (tDecodeI64(pCoder, &pSubmitTbData->uid) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }
  if (tDecodeI32v(pCoder, &pSubmitTbData->sver) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

H
Hongze Cheng 已提交
7593
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
H
Hongze Cheng 已提交
7594 7595 7596 7597 7598 7599 7600
    uint64_t nColData;

    if (tDecodeU64v(pCoder, &nColData) < 0) {
      code = TSDB_CODE_INVALID_MSG;
      goto _exit;
    }

H
Hongze Cheng 已提交
7601
    pSubmitTbData->aCol = taosArrayInit(nColData, sizeof(SColData));
H
Hongze Cheng 已提交
7602 7603 7604 7605 7606 7607 7608 7609
    if (pSubmitTbData->aCol == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    for (int32_t i = 0; i < nColData; ++i) {
      pCoder->pos += tGetColData(pCoder->data + pCoder->pos, taosArrayReserve(pSubmitTbData->aCol, 1));
    }
H
Hongze Cheng 已提交
7610
  } else {
H
Hongze Cheng 已提交
7611 7612
    uint64_t nRow;
    if (tDecodeU64v(pCoder, &nRow) < 0) {
H
Hongze Cheng 已提交
7613 7614 7615 7616
      code = TSDB_CODE_INVALID_MSG;
      goto _exit;
    }

H
Hongze Cheng 已提交
7617
    pSubmitTbData->aRowP = taosArrayInit(nRow, sizeof(SRow *));
H
Hongze Cheng 已提交
7618 7619 7620 7621 7622
    if (pSubmitTbData->aRowP == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

H
Hongze Cheng 已提交
7623
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
H
Hongze Cheng 已提交
7624
      SRow **ppRow = taosArrayReserve(pSubmitTbData->aRowP, 1);
H
Hongze Cheng 已提交
7625

H
Hongze Cheng 已提交
7626 7627
      *ppRow = (SRow *)(pCoder->data + pCoder->pos);
      pCoder->pos += (*ppRow)->len;
H
Hongze Cheng 已提交
7628 7629 7630
    }
  }

7631 7632 7633 7634 7635 7636 7637 7638
  pSubmitTbData->ctimeMs = 0;
  if (!tDecodeIsEnd(pCoder)) {
    if (tDecodeI64(pCoder, &pSubmitTbData->ctimeMs) < 0) {
      code = TSDB_CODE_INVALID_MSG;
      goto _exit;
    }
  }

H
Hongze Cheng 已提交
7639 7640 7641 7642
  tEndDecode(pCoder);

_exit:
  if (code) {
H
Hongze Cheng 已提交
7643
    // TODO: clear
H
Hongze Cheng 已提交
7644 7645 7646 7647
  }
  return 0;
}

7648
int32_t tEncodeSubmitReq(SEncoder *pCoder, const SSubmitReq2 *pReq) {
H
Hongze Cheng 已提交
7649 7650
  if (tStartEncode(pCoder) < 0) return -1;

H
Hongze Cheng 已提交
7651 7652 7653
  if (tEncodeU64v(pCoder, taosArrayGetSize(pReq->aSubmitTbData)) < 0) return -1;
  for (uint64_t i = 0; i < taosArrayGetSize(pReq->aSubmitTbData); i++) {
    if (tEncodeSSubmitTbData(pCoder, taosArrayGet(pReq->aSubmitTbData, i)) < 0) return -1;
H
Hongze Cheng 已提交
7654 7655 7656 7657 7658 7659
  }

  tEndEncode(pCoder);
  return 0;
}

7660
int32_t tDecodeSubmitReq(SDecoder *pCoder, SSubmitReq2 *pReq) {
H
Hongze Cheng 已提交
7661 7662
  int32_t code = 0;

H
Hongze Cheng 已提交
7663
  memset(pReq, 0, sizeof(*pReq));
H
Hongze Cheng 已提交
7664 7665 7666 7667 7668 7669 7670

  // decode
  if (tStartDecode(pCoder) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

H
Hongze Cheng 已提交
7671 7672
  uint64_t nSubmitTbData;
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
H
Hongze Cheng 已提交
7673 7674 7675 7676 7677 7678 7679 7680 7681 7682
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

  pReq->aSubmitTbData = taosArrayInit(nSubmitTbData, sizeof(SSubmitTbData));
  if (pReq->aSubmitTbData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
7683 7684
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
    if (tDecodeSSubmitTbData(pCoder, taosArrayReserve(pReq->aSubmitTbData, 1)) < 0) {
H
Hongze Cheng 已提交
7685 7686 7687 7688 7689 7690 7691 7692 7693
      code = TSDB_CODE_INVALID_MSG;
      goto _exit;
    }
  }

  tEndDecode(pCoder);

_exit:
  if (code) {
H
Hongze Cheng 已提交
7694 7695 7696 7697
    if (pReq->aSubmitTbData) {
      // todo
      taosArrayDestroy(pReq->aSubmitTbData);
      pReq->aSubmitTbData = NULL;
H
Hongze Cheng 已提交
7698 7699
    }
  }
H
Hongze Cheng 已提交
7700
  return code;
H
Hongze Cheng 已提交
7701 7702
}

7703
void tDestroySubmitTbData(SSubmitTbData *pTbData, int32_t flag) {
D
dapan1121 已提交
7704 7705 7706
  if (NULL == pTbData) {
    return;
  }
H
Hongze Cheng 已提交
7707

7708
  if (flag == TSDB_MSG_FLG_ENCODE || flag == TSDB_MSG_FLG_CMPT) {
D
dapan1121 已提交
7709
    if (pTbData->pCreateTbReq) {
7710 7711 7712 7713 7714
      if (flag == TSDB_MSG_FLG_ENCODE) {
        tdDestroySVCreateTbReq(pTbData->pCreateTbReq);
      } else {
        tDestroySVCreateTbReq(pTbData->pCreateTbReq, TSDB_MSG_FLG_DECODE);
      }
D
dapan1121 已提交
7715 7716 7717
      taosMemoryFree(pTbData->pCreateTbReq);
    }

H
Hongze Cheng 已提交
7718 7719 7720 7721 7722 7723 7724 7725
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
      int32_t   nColData = TARRAY_SIZE(pTbData->aCol);
      SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);

      for (int32_t i = 0; i < nColData; ++i) {
        tColDataDestroy(&aColData[i]);
      }
      taosArrayDestroy(pTbData->aCol);
H
Hongze Cheng 已提交
7726 7727 7728 7729 7730 7731 7732 7733
    } else {
      int32_t nRow = TARRAY_SIZE(pTbData->aRowP);
      SRow  **rows = (SRow **)TARRAY_DATA(pTbData->aRowP);

      for (int32_t i = 0; i < nRow; ++i) {
        tRowDestroy(rows[i]);
      }
      taosArrayDestroy(pTbData->aRowP);
H
Hongze Cheng 已提交
7734 7735
    }
  } else if (flag == TSDB_MSG_FLG_DECODE) {
D
dapan1121 已提交
7736
    if (pTbData->pCreateTbReq) {
H
Hongze Cheng 已提交
7737
      tDestroySVCreateTbReq(pTbData->pCreateTbReq, TSDB_MSG_FLG_DECODE);
D
dapan1121 已提交
7738 7739 7740
      taosMemoryFree(pTbData->pCreateTbReq);
    }

H
Hongze Cheng 已提交
7741 7742 7743 7744
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
      taosArrayDestroy(pTbData->aCol);
    } else {
      taosArrayDestroy(pTbData->aRowP);
H
Hongze Cheng 已提交
7745
    }
X
Xiaoyu Wang 已提交
7746
  }
X
Xiaoyu Wang 已提交
7747 7748
}

7749
void tDestroySubmitReq(SSubmitReq2 *pReq, int32_t flag) {
H
Hongze Cheng 已提交
7750 7751
  if (pReq->aSubmitTbData == NULL) return;

H
Hongze Cheng 已提交
7752 7753
  int32_t        nSubmitTbData = TARRAY_SIZE(pReq->aSubmitTbData);
  SSubmitTbData *aSubmitTbData = (SSubmitTbData *)TARRAY_DATA(pReq->aSubmitTbData);
H
Hongze Cheng 已提交
7754

H
Hongze Cheng 已提交
7755
  for (int32_t i = 0; i < nSubmitTbData; i++) {
7756
    tDestroySubmitTbData(&aSubmitTbData[i], flag);
H
Hongze Cheng 已提交
7757 7758
  }
  taosArrayDestroy(pReq->aSubmitTbData);
wmmhello's avatar
wmmhello 已提交
7759
  pReq->aSubmitTbData = NULL;
H
Hongze Cheng 已提交
7760
}
H
Hongze Cheng 已提交
7761 7762 7763 7764 7765

int32_t tEncodeSSubmitRsp2(SEncoder *pCoder, const SSubmitRsp2 *pRsp) {
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32v(pCoder, pRsp->affectedRows) < 0) return -1;
H
Hongze Cheng 已提交
7766 7767

  if (tEncodeU64v(pCoder, taosArrayGetSize(pRsp->aCreateTbRsp)) < 0) return -1;
H
Hongze Cheng 已提交
7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791
  for (int32_t i = 0; i < taosArrayGetSize(pRsp->aCreateTbRsp); ++i) {
    if (tEncodeSVCreateTbRsp(pCoder, taosArrayGet(pRsp->aCreateTbRsp, i)) < 0) return -1;
  }

  tEndEncode(pCoder);
  return 0;
}

int32_t tDecodeSSubmitRsp2(SDecoder *pCoder, SSubmitRsp2 *pRsp) {
  int32_t code = 0;

  memset(pRsp, 0, sizeof(SSubmitRsp2));

  // decode
  if (tStartDecode(pCoder) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

  if (tDecodeI32v(pCoder, &pRsp->affectedRows) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

H
Hongze Cheng 已提交
7792 7793
  uint64_t nCreateTbRsp;
  if (tDecodeU64v(pCoder, &nCreateTbRsp) < 0) {
H
Hongze Cheng 已提交
7794 7795 7796 7797
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

H
Hongze Cheng 已提交
7798 7799 7800 7801
  if (nCreateTbRsp) {
    pRsp->aCreateTbRsp = taosArrayInit(nCreateTbRsp, sizeof(SVCreateTbRsp));
    if (pRsp->aCreateTbRsp == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
7802 7803
      goto _exit;
    }
H
Hongze Cheng 已提交
7804 7805 7806 7807 7808 7809 7810 7811

    for (int32_t i = 0; i < nCreateTbRsp; ++i) {
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pRsp->aCreateTbRsp, 1);
      if (tDecodeSVCreateTbRsp(pCoder, pCreateTbRsp) < 0) {
        code = TSDB_CODE_INVALID_MSG;
        goto _exit;
      }
    }
H
Hongze Cheng 已提交
7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825
  }

  tEndDecode(pCoder);

_exit:
  if (code) {
    if (pRsp->aCreateTbRsp) {
      taosArrayDestroyEx(pRsp->aCreateTbRsp, NULL /* todo */);
    }
  }
  return code;
}

void tDestroySSubmitRsp2(SSubmitRsp2 *pRsp, int32_t flag) {
7826 7827 7828
  if (NULL == pRsp) {
    return;
  }
H
Hongze Cheng 已提交
7829

7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841
  if (flag & TSDB_MSG_FLG_ENCODE) {
    if (pRsp->aCreateTbRsp) {
      int32_t        nCreateTbRsp = TARRAY_SIZE(pRsp->aCreateTbRsp);
      SVCreateTbRsp *aCreateTbRsp = TARRAY_DATA(pRsp->aCreateTbRsp);
      for (int32_t i = 0; i < nCreateTbRsp; ++i) {
        if (aCreateTbRsp[i].pMeta) {
          taosMemoryFree(aCreateTbRsp[i].pMeta);
        }
      }
      taosArrayDestroy(pRsp->aCreateTbRsp);
    }
  } else if (flag & TSDB_MSG_FLG_DECODE) {
H
Hongze Cheng 已提交
7842 7843 7844 7845 7846 7847 7848 7849 7850 7851
    if (pRsp->aCreateTbRsp) {
      int32_t        nCreateTbRsp = TARRAY_SIZE(pRsp->aCreateTbRsp);
      SVCreateTbRsp *aCreateTbRsp = TARRAY_DATA(pRsp->aCreateTbRsp);
      for (int32_t i = 0; i < nCreateTbRsp; ++i) {
        if (aCreateTbRsp[i].pMeta) {
          taosMemoryFree(aCreateTbRsp[i].pMeta);
        }
      }
      taosArrayDestroy(pRsp->aCreateTbRsp);
    }
H
Hongze Cheng 已提交
7852 7853
  }
}
5
54liuyao 已提交
7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906

int32_t tSerializeSMPauseStreamReq(void *buf, int32_t bufLen, const SMPauseStreamReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMPauseStreamReq(void *buf, int32_t bufLen, SMPauseStreamReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

int32_t tSerializeSMResumeStreamReq(void *buf, int32_t bufLen, const SMResumeStreamReq *pReq) {
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igUntreated) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tEncoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMResumeStreamReq(void *buf, int32_t bufLen, SMResumeStreamReq *pReq) {
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igUntreated) < 0) return -1;
  tEndDecode(&decoder);

  tDecoderClear(&decoder);
  return 0;
}

7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942
int32_t tEncodeMqSubTopicEp(void **buf, const SMqSubTopicEp *pTopicEp) {
  int32_t tlen = 0;
  tlen += taosEncodeString(buf, pTopicEp->topic);
  tlen += taosEncodeString(buf, pTopicEp->db);
  int32_t sz = taosArrayGetSize(pTopicEp->vgs);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqSubVgEp *pVgEp = (SMqSubVgEp *)taosArrayGet(pTopicEp->vgs, i);
    tlen += tEncodeSMqSubVgEp(buf, pVgEp);
  }
  tlen += taosEncodeSSchemaWrapper(buf, &pTopicEp->schema);
  return tlen;
}

void *tDecodeMqSubTopicEp(void *buf, SMqSubTopicEp *pTopicEp) {
  buf = taosDecodeStringTo(buf, pTopicEp->topic);
  buf = taosDecodeStringTo(buf, pTopicEp->db);
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  pTopicEp->vgs = taosArrayInit(sz, sizeof(SMqSubVgEp));
  if (pTopicEp->vgs == NULL) {
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    SMqSubVgEp vgEp;
    buf = tDecodeSMqSubVgEp(buf, &vgEp);
    taosArrayPush(pTopicEp->vgs, &vgEp);
  }
  buf = taosDecodeSSchemaWrapper(buf, &pTopicEp->schema);
  return buf;
}

void tDeleteMqSubTopicEp(SMqSubTopicEp *pSubTopicEp) {
  taosMemoryFreeClear(pSubTopicEp->schema.pSchema);
  pSubTopicEp->schema.nCols = 0;
  taosArrayDestroy(pSubTopicEp->vgs);
B
Benguang Zhao 已提交
7943
}