tmsg.c 143.8 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

H
Hongze Cheng 已提交
31
int32_t tInitSubmitMsgIter(SSubmitReq *pMsg, SSubmitMsgIter *pIter) {
C
Cary Xu 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  if (pMsg == NULL) {
    terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP;
    return -1;
  }

  pIter->totalLen = htonl(pMsg->length);
  ASSERT(pIter->totalLen > 0);
  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 已提交
49
int32_t tGetSubmitMsgNext(SSubmitMsgIter *pIter, SSubmitBlk **pPBlock) {
C
Cary Xu 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  ASSERT(pIter->len >= 0);

  if (pIter->len == 0) {
    pIter->len += sizeof(SSubmitReq);
  } else {
    if (pIter->len >= pIter->totalLen) {
      ASSERT(0);
    }

    pIter->len += (sizeof(SSubmitBlk) + pIter->dataLen + pIter->schemaLen);
    ASSERT(pIter->len > 0);
  }

  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);
    pIter->numOfRows = htons((*pPBlock)->numOfRows);
  }
  return 0;
}

C
Cary Xu 已提交
83
int32_t tInitSubmitBlkIter(SSubmitMsgIter *pMsgIter, SSubmitBlk *pBlock, SSubmitBlkIter *pIter) {
C
Cary Xu 已提交
84 85 86 87 88 89 90
  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 已提交
91
STSRow *tGetSubmitBlkNext(SSubmitBlkIter *pIter) {
C
Cary Xu 已提交
92 93 94 95 96 97 98 99 100 101 102 103
  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 已提交
104

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

H
Hongze Cheng 已提交
124
int32_t tEncodeSEpSet(SEncoder *pEncoder, const SEpSet *pEp) {
S
Shengliang Guan 已提交
125 126 127 128 129 130 131 132 133
  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;
    if (tEncodeCStr(pEncoder, pEp->eps[i].fqdn) < 0) return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
134
int32_t tDecodeSEpSet(SDecoder *pDecoder, SEpSet *pEp) {
S
Shengliang Guan 已提交
135 136 137 138 139 140 141 142 143
  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 已提交
144
int32_t tEncodeSQueryNodeAddr(SEncoder *pEncoder, SQueryNodeAddr *pAddr) {
D
dapan1121 已提交
145 146 147 148 149
  if (tEncodeI32(pEncoder, pAddr->nodeId) < 0) return -1;
  if (tEncodeSEpSet(pEncoder, &pAddr->epSet) < 0) return -1;
  return 0;
}

H
Hongze Cheng 已提交
150
int32_t tDecodeSQueryNodeAddr(SDecoder *pDecoder, SQueryNodeAddr *pAddr) {
D
dapan1121 已提交
151 152 153 154 155
  if (tDecodeI32(pDecoder, &pAddr->nodeId) < 0) return -1;
  if (tDecodeSEpSet(pDecoder, &pAddr->epSet) < 0) return -1;
  return 0;
}

S
Shengliang Guan 已提交
156 157 158 159 160 161 162 163 164 165 166
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;
}

167
void *taosDecodeSEpSet(const void *buf, SEpSet *pEp) {
S
Shengliang Guan 已提交
168 169 170 171 172 173
  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);
  }
174
  return (void *)buf;
S
Shengliang Guan 已提交
175 176
}

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

D
dapan1121 已提交
180
  if (pReq->connKey.connType == CONN_TYPE__QUERY) {
D
dapan1121 已提交
181 182 183 184 185 186 187
    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;
      if (tEncodeI32(pEncoder, pReq->query->pid) < 0) return -1;
      if (tEncodeCStr(pEncoder, pReq->query->app) < 0) return -1;
188

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

D
dapan1121 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
      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;
        if (tEncodeI32(pEncoder, desc->pid) < 0) return -1;
        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;
          if (tEncodeI32(pEncoder, sDesc->status) < 0) return -1;
        }
      }
    } else {
      if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
    }
  }
215

L
Liu Jicong 已提交
216
  int32_t kvNum = taosHashGetSize(pReq->info);
S
Shengliang Guan 已提交
217
  if (tEncodeI32(pEncoder, kvNum) < 0) return -1;
S
Shengliang Guan 已提交
218
  void *pIter = taosHashIterate(pReq->info, NULL);
L
Liu Jicong 已提交
219
  while (pIter != NULL) {
S
Shengliang Guan 已提交
220 221
    SKv *kv = pIter;
    if (tEncodeSKv(pEncoder, kv) < 0) return -1;
L
Liu Jicong 已提交
222
    pIter = taosHashIterate(pReq->info, pIter);
L
Liu Jicong 已提交
223
  }
S
Shengliang Guan 已提交
224 225

  return 0;
L
Liu Jicong 已提交
226 227
}

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

D
dapan1121 已提交
231
  if (pReq->connKey.connType == CONN_TYPE__QUERY) {
D
dapan1121 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245
    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;
      if (tDecodeI32(pDecoder, &pReq->query->pid) < 0) return -1;
      if (tDecodeCStrTo(pDecoder, pReq->query->app) < 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;
246

D
dapan1121 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        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;
          if (tDecodeI32(pDecoder, &desc.pid) < 0) return -1;
          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;
263

D
dapan1121 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277
            for (int32_t m = 0; m < snum; ++m) {
              SQuerySubDesc sDesc = {0};
              if (tDecodeI64(pDecoder, &sDesc.tid) < 0) return -1;
              if (tDecodeI32(pDecoder, &sDesc.status) < 0) return -1;
              taosArrayPush(desc.subDesc, &sDesc);
            }
          }

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

S
Shengliang Guan 已提交
278 279
  int32_t kvNum = 0;
  if (tDecodeI32(pDecoder, &kvNum) < 0) return -1;
L
Liu Jicong 已提交
280 281 282
  if (pReq->info == NULL) {
    pReq->info = taosHashInit(kvNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
  }
S
Shengliang Guan 已提交
283
  if (pReq->info == NULL) return -1;
S
Shengliang Guan 已提交
284
  for (int32_t i = 0; i < kvNum; i++) {
S
Shengliang Guan 已提交
285 286
    SKv kv = {0};
    if (tDecodeSKv(pDecoder, &kv) < 0) return -1;
D
dapan1121 已提交
287
    taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
L
Liu Jicong 已提交
288 289
  }

S
Shengliang Guan 已提交
290
  return 0;
L
Liu Jicong 已提交
291 292
}

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

D
dapan1121 已提交
297 298 299
  int32_t queryNum = 0;
  if (pRsp->query) {
    queryNum = 1;
300
    if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
D
dapan1121 已提交
301 302 303 304 305 306 307
    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;
  } else {
308
    if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
D
dapan1121 已提交
309
  }
310

D
dapan1121 已提交
311
  int32_t kvNum = taosArrayGetSize(pRsp->info);
S
Shengliang Guan 已提交
312
  if (tEncodeI32(pEncoder, kvNum) < 0) return -1;
S
Shengliang Guan 已提交
313
  for (int32_t i = 0; i < kvNum; i++) {
S
Shengliang Guan 已提交
314 315
    SKv *kv = taosArrayGet(pRsp->info, i);
    if (tEncodeSKv(pEncoder, kv) < 0) return -1;
D
dapan1121 已提交
316
  }
S
Shengliang Guan 已提交
317 318

  return 0;
L
Liu Jicong 已提交
319
}
S
Shengliang Guan 已提交
320

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

D
dapan1121 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337
  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 已提交
338
  int32_t kvNum = 0;
S
Shengliang Guan 已提交
339
  if (tDecodeI32(pDecoder, &kvNum) < 0) return -1;
D
dapan1121 已提交
340
  pRsp->info = taosArrayInit(kvNum, sizeof(SKv));
S
Shengliang Guan 已提交
341
  if (pRsp->info == NULL) return -1;
S
Shengliang Guan 已提交
342
  for (int32_t i = 0; i < kvNum; i++) {
D
dapan1121 已提交
343
    SKv kv = {0};
S
Shengliang Guan 已提交
344
    tDecodeSKv(pDecoder, &kv);
D
dapan1121 已提交
345 346
    taosArrayPush(pRsp->info, &kv);
  }
S
Shengliang Guan 已提交
347

S
Shengliang Guan 已提交
348
  return 0;
L
Liu Jicong 已提交
349 350
}

S
Shengliang Guan 已提交
351
int32_t tSerializeSClientHbBatchReq(void *buf, int32_t bufLen, const SClientHbBatchReq *pBatchReq) {
H
Hongze Cheng 已提交
352 353
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
354 355 356 357

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

L
Liu Jicong 已提交
358
  int32_t reqNum = taosArrayGetSize(pBatchReq->reqs);
S
Shengliang Guan 已提交
359
  if (tEncodeI32(&encoder, reqNum) < 0) return -1;
S
Shengliang Guan 已提交
360 361
  for (int32_t i = 0; i < reqNum; i++) {
    SClientHbReq *pReq = taosArrayGet(pBatchReq->reqs, i);
S
Shengliang Guan 已提交
362
    if (tSerializeSClientHbReq(&encoder, pReq) < 0) return -1;
L
Liu Jicong 已提交
363
  }
S
Shengliang Guan 已提交
364 365 366
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
367
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
368 369 370
  return tlen;
}

S
Shengliang Guan 已提交
371
int32_t tDeserializeSClientHbBatchReq(void *buf, int32_t bufLen, SClientHbBatchReq *pBatchReq) {
H
Hongze Cheng 已提交
372 373
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
374 375 376 377 378 379

  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 已提交
380
  if (reqNum > 0) {
S
Shengliang Guan 已提交
381
    pBatchReq->reqs = taosArrayInit(reqNum, sizeof(SClientHbReq));
D
dapan1121 已提交
382
    if (NULL == pBatchReq->reqs) return -1;
L
Liu Jicong 已提交
383
  }
S
Shengliang Guan 已提交
384
  for (int32_t i = 0; i < reqNum; i++) {
L
Liu Jicong 已提交
385
    SClientHbReq req = {0};
S
Shengliang Guan 已提交
386
    tDeserializeSClientHbReq(&decoder, &req);
L
Liu Jicong 已提交
387 388
    taosArrayPush(pBatchReq->reqs, &req);
  }
S
Shengliang Guan 已提交
389 390

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
391
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
392
  return 0;
L
Liu Jicong 已提交
393 394
}

S
Shengliang Guan 已提交
395
int32_t tSerializeSClientHbBatchRsp(void *buf, int32_t bufLen, const SClientHbBatchRsp *pBatchRsp) {
H
Hongze Cheng 已提交
396 397
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
398 399 400 401 402 403 404 405

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

  int32_t rspNum = taosArrayGetSize(pBatchRsp->rsps);
  if (tEncodeI32(&encoder, rspNum) < 0) return -1;
  for (int32_t i = 0; i < rspNum; i++) {
S
Shengliang Guan 已提交
406
    SClientHbRsp *pRsp = taosArrayGet(pBatchRsp->rsps, i);
S
Shengliang Guan 已提交
407
    if (tSerializeSClientHbRsp(&encoder, pRsp) < 0) return -1;
L
Liu Jicong 已提交
408
  }
S
Shengliang Guan 已提交
409 410 411
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
412
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
413 414 415
  return tlen;
}

S
Shengliang Guan 已提交
416
int32_t tDeserializeSClientHbBatchRsp(void *buf, int32_t bufLen, SClientHbBatchRsp *pBatchRsp) {
H
Hongze Cheng 已提交
417 418
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
419 420 421 422 423 424 425 426

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

  int32_t rspNum = 0;
  if (tDecodeI32(&decoder, &rspNum) < 0) return -1;
  if (pBatchRsp->rsps == NULL) {
L
Liu Jicong 已提交
427
    pBatchRsp->rsps = taosArrayInit(rspNum, sizeof(SClientHbRsp));
S
Shengliang Guan 已提交
428 429
  }
  for (int32_t i = 0; i < rspNum; i++) {
L
Liu Jicong 已提交
430
    SClientHbRsp rsp = {0};
S
Shengliang Guan 已提交
431
    tDeserializeSClientHbRsp(&decoder, &rsp);
L
Liu Jicong 已提交
432 433
    taosArrayPush(pBatchRsp->rsps, &rsp);
  }
S
Shengliang Guan 已提交
434 435

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
436
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
437
  return 0;
L
Liu Jicong 已提交
438 439
}

S
Shengliang Guan 已提交
440
int32_t tSerializeSMCreateStbReq(void *buf, int32_t bufLen, SMCreateStbReq *pReq) {
H
Hongze Cheng 已提交
441 442
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
443

S
Shengliang Guan 已提交
444 445 446
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
S
sma  
Shengliang Guan 已提交
447 448
  if (tEncodeFloat(&encoder, pReq->xFilesFactor) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->delay) < 0) return -1;
S
sma  
Shengliang Guan 已提交
449
  if (tEncodeI32(&encoder, pReq->ttl) < 0) return -1;
S
Shengliang Guan 已提交
450 451
  if (tEncodeI32(&encoder, pReq->numOfColumns) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfTags) < 0) return -1;
S
sma  
Shengliang Guan 已提交
452
  if (tEncodeI32(&encoder, pReq->commentLen) < 0) return -1;
453 454
  if (tEncodeI32(&encoder, pReq->ast1Len) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->ast2Len) < 0) return -1;
S
Shengliang Guan 已提交
455 456 457

  for (int32_t i = 0; i < pReq->numOfColumns; ++i) {
    SField *pField = taosArrayGet(pReq->pColumns, i);
S
Shengliang Guan 已提交
458 459 460
    if (tEncodeI8(&encoder, pField->type) < 0) return -1;
    if (tEncodeI32(&encoder, pField->bytes) < 0) return -1;
    if (tEncodeCStr(&encoder, pField->name) < 0) return -1;
461
    if (tEncodeI8(&encoder, pField->flags) < 0) return -1;
S
Shengliang Guan 已提交
462 463 464 465
  }

  for (int32_t i = 0; i < pReq->numOfTags; ++i) {
    SField *pField = taosArrayGet(pReq->pTags, i);
S
Shengliang Guan 已提交
466 467 468
    if (tEncodeI8(&encoder, pField->type) < 0) return -1;
    if (tEncodeI32(&encoder, pField->bytes) < 0) return -1;
    if (tEncodeCStr(&encoder, pField->name) < 0) return -1;
469
    if (tEncodeI8(&encoder, pField->flags) < 0) return -1;
S
sma  
Shengliang Guan 已提交
470 471 472 473 474
  }

  if (pReq->commentLen > 0) {
    if (tEncodeBinary(&encoder, pReq->comment, pReq->commentLen) < 0) return -1;
  }
475 476 477 478 479 480
  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;
  }
S
Shengliang Guan 已提交
481 482 483
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
484
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
485 486 487
  return tlen;
}

S
Shengliang Guan 已提交
488
int32_t tDeserializeSMCreateStbReq(void *buf, int32_t bufLen, SMCreateStbReq *pReq) {
H
Hongze Cheng 已提交
489 490
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
491 492 493 494

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
S
sma  
Shengliang Guan 已提交
495 496
  if (tDecodeFloat(&decoder, &pReq->xFilesFactor) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->delay) < 0) return -1;
S
sma  
Shengliang Guan 已提交
497
  if (tDecodeI32(&decoder, &pReq->ttl) < 0) return -1;
S
Shengliang Guan 已提交
498 499
  if (tDecodeI32(&decoder, &pReq->numOfColumns) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfTags) < 0) return -1;
S
sma  
Shengliang Guan 已提交
500
  if (tDecodeI32(&decoder, &pReq->commentLen) < 0) return -1;
501 502
  if (tDecodeI32(&decoder, &pReq->ast1Len) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->ast2Len) < 0) return -1;
S
Shengliang Guan 已提交
503 504 505

  pReq->pColumns = taosArrayInit(pReq->numOfColumns, sizeof(SField));
  pReq->pTags = taosArrayInit(pReq->numOfTags, sizeof(SField));
506
  if (pReq->pColumns == NULL || pReq->pTags == NULL) {
S
Shengliang Guan 已提交
507
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
508
    return -1;
S
Shengliang Guan 已提交
509 510 511 512
  }

  for (int32_t i = 0; i < pReq->numOfColumns; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
513 514 515
    if (tDecodeI8(&decoder, &field.type) < 0) return -1;
    if (tDecodeI32(&decoder, &field.bytes) < 0) return -1;
    if (tDecodeCStrTo(&decoder, field.name) < 0) return -1;
516
    if (tDecodeI8(&decoder, &field.flags) < 0) return -1;
S
Shengliang Guan 已提交
517 518
    if (taosArrayPush(pReq->pColumns, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
519
      return -1;
S
Shengliang Guan 已提交
520 521 522 523 524
    }
  }

  for (int32_t i = 0; i < pReq->numOfTags; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
525 526 527
    if (tDecodeI8(&decoder, &field.type) < 0) return -1;
    if (tDecodeI32(&decoder, &field.bytes) < 0) return -1;
    if (tDecodeCStrTo(&decoder, field.name) < 0) return -1;
528
    if (tDecodeI8(&decoder, &field.flags) < 0) return -1;
S
Shengliang Guan 已提交
529 530
    if (taosArrayPush(pReq->pTags, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
531
      return -1;
S
Shengliang Guan 已提交
532 533 534
    }
  }

S
sma  
Shengliang Guan 已提交
535
  if (pReq->commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
536
    pReq->comment = taosMemoryMalloc(pReq->commentLen);
S
sma  
Shengliang Guan 已提交
537 538 539 540
    if (pReq->comment == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->comment) < 0) return -1;
  }

541 542 543 544 545 546 547 548 549 550 551 552
  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;
  }

S
Shengliang Guan 已提交
553 554
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
555
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
556
  return 0;
S
Shengliang Guan 已提交
557
}
S
Shengliang Guan 已提交
558

S
Shengliang Guan 已提交
559 560 561
void tFreeSMCreateStbReq(SMCreateStbReq *pReq) {
  taosArrayDestroy(pReq->pColumns);
  taosArrayDestroy(pReq->pTags);
wafwerar's avatar
wafwerar 已提交
562
  taosMemoryFreeClear(pReq->comment);
563 564
  taosMemoryFreeClear(pReq->pAst1);
  taosMemoryFreeClear(pReq->pAst2);
S
Shengliang Guan 已提交
565 566
  pReq->pColumns = NULL;
  pReq->pTags = NULL;
S
Shengliang Guan 已提交
567 568
}

S
Shengliang Guan 已提交
569
int32_t tSerializeSMDropStbReq(void *buf, int32_t bufLen, SMDropStbReq *pReq) {
H
Hongze Cheng 已提交
570 571
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
572

S
Shengliang Guan 已提交
573 574 575 576
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  tEndEncode(&encoder);
S
Shengliang Guan 已提交
577

S
Shengliang Guan 已提交
578
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
579
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
580 581 582
  return tlen;
}

S
Shengliang Guan 已提交
583
int32_t tDeserializeSMDropStbReq(void *buf, int32_t bufLen, SMDropStbReq *pReq) {
H
Hongze Cheng 已提交
584 585
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
586

S
Shengliang Guan 已提交
587 588 589 590
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
  tEndDecode(&decoder);
S
Shengliang Guan 已提交
591

H
Hongze Cheng 已提交
592
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
593 594
  return 0;
}
S
Shengliang Guan 已提交
595

S
Shengliang Guan 已提交
596
int32_t tSerializeSMAlterStbReq(void *buf, int32_t bufLen, SMAlterStbReq *pReq) {
H
Hongze Cheng 已提交
597 598
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
599

S
Shengliang Guan 已提交
600 601 602
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->alterType) < 0) return -1;
603
  if (tEncodeI32(&encoder, pReq->verInBlock) < 0) return -1;
S
Shengliang Guan 已提交
604
  if (tEncodeI32(&encoder, pReq->numOfFields) < 0) return -1;
S
Shengliang Guan 已提交
605 606
  for (int32_t i = 0; i < pReq->numOfFields; ++i) {
    SField *pField = taosArrayGet(pReq->pFields, i);
S
Shengliang Guan 已提交
607 608 609
    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 已提交
610
  }
X
Xiaoyu Wang 已提交
611 612 613 614 615
  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 已提交
616
  tEndEncode(&encoder);
S
Shengliang Guan 已提交
617

S
Shengliang Guan 已提交
618
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
619
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
620 621 622
  return tlen;
}

S
Shengliang Guan 已提交
623
int32_t tDeserializeSMAlterStbReq(void *buf, int32_t bufLen, SMAlterStbReq *pReq) {
H
Hongze Cheng 已提交
624 625
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
626

S
Shengliang Guan 已提交
627 628 629
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->alterType) < 0) return -1;
630
  if (tDecodeI32(&decoder, &pReq->verInBlock) < 0) return -1;
S
Shengliang Guan 已提交
631
  if (tDecodeI32(&decoder, &pReq->numOfFields) < 0) return -1;
S
Shengliang Guan 已提交
632 633 634
  pReq->pFields = taosArrayInit(pReq->numOfFields, sizeof(SField));
  if (pReq->pFields == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
635
    return -1;
S
Shengliang Guan 已提交
636 637 638 639
  }

  for (int32_t i = 0; i < pReq->numOfFields; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
640 641 642
    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 已提交
643 644
    if (taosArrayPush(pReq->pFields, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
645
      return -1;
S
Shengliang Guan 已提交
646 647 648
    }
  }

X
Xiaoyu Wang 已提交
649 650 651 652 653 654 655 656
  if (tDecodeI32(&decoder, &pReq->ttl) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->commentLen) < 0) return -1;
  if (pReq->commentLen > 0) {
    pReq->comment = taosMemoryMalloc(pReq->commentLen);
    if (pReq->comment == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->comment) < 0) return -1;
  }

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

S
Shengliang Guan 已提交
662
void tFreeSMAltertbReq(SMAlterStbReq *pReq) {
S
Shengliang Guan 已提交
663 664
  taosArrayDestroy(pReq->pFields);
  pReq->pFields = NULL;
S
Shengliang Guan 已提交
665
}
dengyihao's avatar
dengyihao 已提交
666
int32_t tSerializeSMEpSet(void *buf, int32_t bufLen, SMEpSet *pReq) {
H
Hongze Cheng 已提交
667 668
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
669 670 671
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeSEpSet(&encoder, &pReq->epSet) < 0) return -1;

dengyihao's avatar
dengyihao 已提交
672 673
  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
674
  tEncoderClear(&encoder);
dengyihao's avatar
dengyihao 已提交
675 676 677
  return tlen;
}
int32_t tDeserializeSMEpSet(void *buf, int32_t bufLen, SMEpSet *pReq) {
H
Hongze Cheng 已提交
678 679
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
680 681
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeSEpSet(&decoder, &pReq->epSet) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
682 683

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
684
  tDecoderClear(&decoder);
dengyihao's avatar
dengyihao 已提交
685 686
  return 0;
}
S
Shengliang Guan 已提交
687

S
Shengliang Guan 已提交
688
int32_t tSerializeSMCreateSmaReq(void *buf, int32_t bufLen, SMCreateSmaReq *pReq) {
H
Hongze Cheng 已提交
689 690
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
691 692 693 694 695 696 697 698

  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 已提交
699
  if (tEncodeI32(&encoder, pReq->dstVgId) < 0) return -1;
S
Shengliang Guan 已提交
700 701 702 703
  if (tEncodeI64(&encoder, pReq->interval) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->offset) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->sliding) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->exprLen) < 0) return -1;
S
sma  
Shengliang Guan 已提交
704 705 706
  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 已提交
707 708 709 710 711 712
  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 已提交
713 714 715 716 717 718
  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;
  }
S
Shengliang Guan 已提交
719 720 721
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
722
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
723 724 725 726
  return tlen;
}

int32_t tDeserializeSMCreateSmaReq(void *buf, int32_t bufLen, SMCreateSmaReq *pReq) {
H
Hongze Cheng 已提交
727 728
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
729 730 731 732 733 734 735 736

  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 已提交
737
  if (tDecodeI32(&decoder, &pReq->dstVgId) < 0) return -1;
S
Shengliang Guan 已提交
738 739 740 741
  if (tDecodeI64(&decoder, &pReq->interval) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->offset) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->sliding) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->exprLen) < 0) return -1;
S
sma  
Shengliang Guan 已提交
742 743 744
  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 已提交
745
  if (pReq->exprLen > 0) {
wafwerar's avatar
wafwerar 已提交
746
    pReq->expr = taosMemoryMalloc(pReq->exprLen);
S
Shengliang Guan 已提交
747 748 749 750
    if (pReq->expr == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->expr) < 0) return -1;
  }
  if (pReq->tagsFilterLen > 0) {
wafwerar's avatar
wafwerar 已提交
751
    pReq->tagsFilter = taosMemoryMalloc(pReq->tagsFilterLen);
S
Shengliang Guan 已提交
752 753 754
    if (pReq->tagsFilter == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->tagsFilter) < 0) return -1;
  }
S
sma  
Shengliang Guan 已提交
755
  if (pReq->sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
756
    pReq->sql = taosMemoryMalloc(pReq->sqlLen);
S
sma  
Shengliang Guan 已提交
757 758 759 760
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }
  if (pReq->astLen > 0) {
wafwerar's avatar
wafwerar 已提交
761
    pReq->ast = taosMemoryMalloc(pReq->astLen);
S
sma  
Shengliang Guan 已提交
762 763 764
    if (pReq->ast == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
  }
S
Shengliang Guan 已提交
765 766

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
767
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
768 769 770 771
  return 0;
}

void tFreeSMCreateSmaReq(SMCreateSmaReq *pReq) {
wafwerar's avatar
wafwerar 已提交
772 773 774 775
  taosMemoryFreeClear(pReq->expr);
  taosMemoryFreeClear(pReq->tagsFilter);
  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->ast);
S
Shengliang Guan 已提交
776 777 778
}

int32_t tSerializeSMDropSmaReq(void *buf, int32_t bufLen, SMDropSmaReq *pReq) {
H
Hongze Cheng 已提交
779 780
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
781 782 783

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

S
Shengliang Guan 已提交
785 786 787 788
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
789
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
790 791 792 793
  return tlen;
}

int32_t tDeserializeSMDropSmaReq(void *buf, int32_t bufLen, SMDropSmaReq *pReq) {
H
Hongze Cheng 已提交
794 795
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
796 797 798 799 800 801

  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 已提交
802
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
803 804
  return 0;
}
dengyihao's avatar
dengyihao 已提交
805
int32_t tSerializeSMCreateFullTextReq(void *buf, int32_t bufLen, SMCreateFullTextReq *pReq) {
H
Hongze Cheng 已提交
806 807
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
808 809 810 811 812

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

  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
813
  tEncoderClear(&encoder);
dengyihao's avatar
dengyihao 已提交
814 815 816
  return tlen;
}
int32_t tDeserializeSMCreateFullTextReq(void *buf, int32_t bufLen, SMCreateFullTextReq *pReq) {
H
Hongze Cheng 已提交
817 818
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
819 820 821
  if (tStartDecode(&decoder) < 0) return -1;

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
822
  tDecoderClear(&decoder);
dengyihao's avatar
dengyihao 已提交
823 824 825 826 827 828
  return 0;
}
void tFreeSMCreateFullTextReq(SMCreateFullTextReq *pReq) {
  // impl later
  return;
}
dengyihao's avatar
dengyihao 已提交
829
int32_t tSerializeSMDropFullTextReq(void *buf, int32_t bufLen, SMDropFullTextReq *pReq) {
H
Hongze Cheng 已提交
830 831
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
832 833 834

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

dengyihao's avatar
dengyihao 已提交
835 836 837 838
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;

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

dengyihao's avatar
dengyihao 已提交
839 840
  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
841
  tEncoderClear(&encoder);
dengyihao's avatar
dengyihao 已提交
842 843 844
  return tlen;
}
int32_t tDeserializeSMDropFullTextReq(void *buf, int32_t bufLen, SMDropFullTextReq *pReq) {
H
Hongze Cheng 已提交
845 846
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
dengyihao's avatar
dengyihao 已提交
847
  if (tStartDecode(&decoder) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
848 849
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
850 851

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
852
  tDecoderClear(&decoder);
dengyihao's avatar
dengyihao 已提交
853 854
  return 0;
}
S
Shengliang Guan 已提交
855

S
Shengliang Guan 已提交
856
int32_t tSerializeSStatusReq(void *buf, int32_t bufLen, SStatusReq *pReq) {
H
Hongze Cheng 已提交
857 858
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
859 860

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

  // status
S
Shengliang Guan 已提交
863
  if (tEncodeI32(&encoder, pReq->sver) < 0) return -1;
S
Shengliang Guan 已提交
864
  if (tEncodeI64(&encoder, pReq->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
865 866 867 868 869 870 871
  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;
  if (tEncodeI32(&encoder, pReq->numOfCores) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfSupportVnodes) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->dnodeEp) < 0) return -1;
S
Shengliang Guan 已提交
872 873

  // cluster cfg
S
Shengliang Guan 已提交
874 875 876 877 878
  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 已提交
879 880 881

  // vnode loads
  int32_t vlen = (int32_t)taosArrayGetSize(pReq->pVloads);
S
Shengliang Guan 已提交
882
  if (tEncodeI32(&encoder, vlen) < 0) return -1;
S
Shengliang Guan 已提交
883 884
  for (int32_t i = 0; i < vlen; ++i) {
    SVnodeLoad *pload = taosArrayGet(pReq->pVloads, i);
S
Shengliang Guan 已提交
885
    if (tEncodeI32(&encoder, pload->vgId) < 0) return -1;
S
Shengliang Guan 已提交
886
    if (tEncodeI32(&encoder, pload->syncState) < 0) return -1;
S
Shengliang Guan 已提交
887 888 889 890 891
    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;
S
Shengliang Guan 已提交
892 893
  }

894 895 896
  // mnode loads
  if (tEncodeI32(&encoder, pReq->mload.syncState) < 0) return -1;

S
Shengliang Guan 已提交
897 898 899
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
900
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
901 902 903
  return tlen;
}

S
Shengliang Guan 已提交
904
int32_t tDeserializeSStatusReq(void *buf, int32_t bufLen, SStatusReq *pReq) {
H
Hongze Cheng 已提交
905 906
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
907 908 909

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

S
Shengliang Guan 已提交
910
  // status
S
Shengliang Guan 已提交
911
  if (tDecodeI32(&decoder, &pReq->sver) < 0) return -1;
S
Shengliang Guan 已提交
912
  if (tDecodeI64(&decoder, &pReq->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
913 914 915 916 917 918 919
  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;
  if (tDecodeI32(&decoder, &pReq->numOfCores) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfSupportVnodes) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->dnodeEp) < 0) return -1;
S
Shengliang Guan 已提交
920 921

  // cluster cfg
S
Shengliang Guan 已提交
922 923 924 925 926
  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 已提交
927 928 929

  // vnode loads
  int32_t vlen = 0;
S
Shengliang Guan 已提交
930
  if (tDecodeI32(&decoder, &vlen) < 0) return -1;
S
Shengliang Guan 已提交
931 932 933
  pReq->pVloads = taosArrayInit(vlen, sizeof(SVnodeLoad));
  if (pReq->pVloads == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
934
    return -1;
S
Shengliang Guan 已提交
935 936 937 938
  }

  for (int32_t i = 0; i < vlen; ++i) {
    SVnodeLoad vload = {0};
S
Shengliang Guan 已提交
939
    if (tDecodeI32(&decoder, &vload.vgId) < 0) return -1;
S
Shengliang Guan 已提交
940
    if (tDecodeI32(&decoder, &vload.syncState) < 0) return -1;
S
Shengliang Guan 已提交
941 942 943 944 945
    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;
S
Shengliang Guan 已提交
946 947
    if (taosArrayPush(pReq->pVloads, &vload) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
948
      return -1;
S
Shengliang Guan 已提交
949 950 951
    }
  }

952 953
  if (tDecodeI32(&decoder, &pReq->mload.syncState) < 0) return -1;

S
Shengliang Guan 已提交
954
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
955
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
956
  return 0;
S
Shengliang Guan 已提交
957
}
S
Shengliang Guan 已提交
958

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

S
Shengliang Guan 已提交
961
int32_t tSerializeSStatusRsp(void *buf, int32_t bufLen, SStatusRsp *pRsp) {
H
Hongze Cheng 已提交
962 963
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
964

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

S
Shengliang Guan 已提交
967
  // status
S
Shengliang Guan 已提交
968
  if (tEncodeI64(&encoder, pRsp->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
969 970

  // dnode cfg
S
Shengliang Guan 已提交
971 972
  if (tEncodeI32(&encoder, pRsp->dnodeCfg.dnodeId) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->dnodeCfg.clusterId) < 0) return -1;
S
Shengliang Guan 已提交
973 974 975

  // dnode eps
  int32_t dlen = (int32_t)taosArrayGetSize(pRsp->pDnodeEps);
S
Shengliang Guan 已提交
976
  if (tEncodeI32(&encoder, dlen) < 0) return -1;
S
Shengliang Guan 已提交
977 978
  for (int32_t i = 0; i < dlen; ++i) {
    SDnodeEp *pDnodeEp = taosArrayGet(pRsp->pDnodeEps, i);
S
Shengliang Guan 已提交
979 980 981 982
    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 已提交
983 984
  }

S
Shengliang Guan 已提交
985 986 987
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
988
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
989 990 991
  return tlen;
}

S
Shengliang Guan 已提交
992
int32_t tDeserializeSStatusRsp(void *buf, int32_t bufLen, SStatusRsp *pRsp) {
H
Hongze Cheng 已提交
993 994
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
995 996 997

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

S
Shengliang Guan 已提交
998
  // status
S
Shengliang Guan 已提交
999
  if (tDecodeI64(&decoder, &pRsp->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1000 1001

  // cluster cfg
S
Shengliang Guan 已提交
1002 1003
  if (tDecodeI32(&decoder, &pRsp->dnodeCfg.dnodeId) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->dnodeCfg.clusterId) < 0) return -1;
S
Shengliang Guan 已提交
1004 1005 1006

  // dnode eps
  int32_t dlen = 0;
S
Shengliang Guan 已提交
1007
  if (tDecodeI32(&decoder, &dlen) < 0) return -1;
S
Shengliang Guan 已提交
1008 1009 1010
  pRsp->pDnodeEps = taosArrayInit(dlen, sizeof(SDnodeEp));
  if (pRsp->pDnodeEps == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1011
    return -1;
S
Shengliang Guan 已提交
1012 1013 1014 1015
  }

  for (int32_t i = 0; i < dlen; ++i) {
    SDnodeEp dnodeEp = {0};
S
Shengliang Guan 已提交
1016 1017 1018 1019
    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 已提交
1020 1021
    if (taosArrayPush(pRsp->pDnodeEps, &dnodeEp) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1022
      return -1;
S
Shengliang Guan 已提交
1023 1024 1025
    }
  }

S
Shengliang Guan 已提交
1026
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
1027
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1028
  return 0;
S
Shengliang Guan 已提交
1029
}
S
Shengliang Guan 已提交
1030

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

S
Shengliang Guan 已提交
1033
int32_t tSerializeSCreateAcctReq(void *buf, int32_t bufLen, SCreateAcctReq *pReq) {
H
Hongze Cheng 已提交
1034 1035
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

  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 已提交
1049
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1050 1051 1052
  return tlen;
}

S
Shengliang Guan 已提交
1053
int32_t tDeserializeSCreateAcctReq(void *buf, int32_t bufLen, SCreateAcctReq *pReq) {
H
Hongze Cheng 已提交
1054 1055
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067

  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 已提交
1068
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1069
  return 0;
S
Shengliang Guan 已提交
1070 1071
}

S
Shengliang Guan 已提交
1072
int32_t tSerializeSDropUserReq(void *buf, int32_t bufLen, SDropUserReq *pReq) {
H
Hongze Cheng 已提交
1073 1074
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1075 1076 1077 1078 1079 1080

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1081
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1082 1083 1084
  return tlen;
}

S
Shengliang Guan 已提交
1085
int32_t tDeserializeSDropUserReq(void *buf, int32_t bufLen, SDropUserReq *pReq) {
H
Hongze Cheng 已提交
1086 1087
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1088 1089 1090 1091 1092

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

H
Hongze Cheng 已提交
1093
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1094
  return 0;
S
Shengliang Guan 已提交
1095 1096
}

S
Shengliang Guan 已提交
1097
int32_t tSerializeSCreateUserReq(void *buf, int32_t bufLen, SCreateUserReq *pReq) {
H
Hongze Cheng 已提交
1098 1099
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->createType) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->superUser) < 0) return -1;
  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 已提交
1109
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1110 1111 1112
  return tlen;
}

S
Shengliang Guan 已提交
1113
int32_t tDeserializeSCreateUserReq(void *buf, int32_t bufLen, SCreateUserReq *pReq) {
H
Hongze Cheng 已提交
1114 1115
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1116 1117 1118 1119 1120 1121 1122 1123

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

H
Hongze Cheng 已提交
1124
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1125
  return 0;
S
Shengliang Guan 已提交
1126 1127
}

S
Shengliang Guan 已提交
1128
int32_t tSerializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq) {
H
Hongze Cheng 已提交
1129 1130
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->alterType) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->superUser) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->pass) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->dbname) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1141
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1142 1143 1144
  return tlen;
}

S
Shengliang Guan 已提交
1145
int32_t tDeserializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq) {
H
Hongze Cheng 已提交
1146 1147
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->alterType) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->superUser) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->pass) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->dbname) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1157
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1158
  return 0;
S
Shengliang Guan 已提交
1159 1160
}

S
Shengliang Guan 已提交
1161
int32_t tSerializeSGetUserAuthReq(void *buf, int32_t bufLen, SGetUserAuthReq *pReq) {
H
Hongze Cheng 已提交
1162 1163
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1164 1165 1166 1167 1168 1169

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1170
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1171 1172 1173
  return tlen;
}

S
Shengliang Guan 已提交
1174
int32_t tDeserializeSGetUserAuthReq(void *buf, int32_t bufLen, SGetUserAuthReq *pReq) {
H
Hongze Cheng 已提交
1175 1176
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1177 1178 1179 1180 1181

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

H
Hongze Cheng 已提交
1182
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1183
  return 0;
S
Shengliang Guan 已提交
1184 1185
}

H
Hongze Cheng 已提交
1186
int32_t tSerializeSGetUserAuthRspImpl(SEncoder *pEncoder, SGetUserAuthRsp *pRsp) {
D
dapan 已提交
1187 1188 1189
  if (tEncodeCStr(pEncoder, pRsp->user) < 0) return -1;
  if (tEncodeI8(pEncoder, pRsp->superAuth) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->version) < 0) return -1;
S
Shengliang Guan 已提交
1190

D
dapan 已提交
1191
  int32_t numOfCreatedDbs = taosHashGetSize(pRsp->createdDbs);
S
Shengliang Guan 已提交
1192 1193
  int32_t numOfReadDbs = taosHashGetSize(pRsp->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pRsp->writeDbs);
D
dapan 已提交
1194 1195 1196
  if (tEncodeI32(pEncoder, numOfCreatedDbs) < 0) return -1;
  if (tEncodeI32(pEncoder, numOfReadDbs) < 0) return -1;
  if (tEncodeI32(pEncoder, numOfWriteDbs) < 0) return -1;
S
Shengliang Guan 已提交
1197

D
dapan 已提交
1198
  char *db = taosHashIterate(pRsp->createdDbs, NULL);
S
Shengliang Guan 已提交
1199
  while (db != NULL) {
D
dapan 已提交
1200 1201 1202 1203 1204 1205 1206
    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 已提交
1207
    db = taosHashIterate(pRsp->readDbs, db);
S
Shengliang Guan 已提交
1208 1209
  }

S
Shengliang Guan 已提交
1210
  db = taosHashIterate(pRsp->writeDbs, NULL);
S
Shengliang Guan 已提交
1211
  while (db != NULL) {
D
dapan 已提交
1212
    if (tEncodeCStr(pEncoder, db) < 0) return -1;
S
Shengliang Guan 已提交
1213
    db = taosHashIterate(pRsp->writeDbs, db);
S
Shengliang Guan 已提交
1214 1215
  }

D
dapan 已提交
1216 1217 1218 1219
  return 0;
}

int32_t tSerializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pRsp) {
H
Hongze Cheng 已提交
1220 1221
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan 已提交
1222 1223 1224 1225 1226

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

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

S
Shengliang Guan 已提交
1227 1228 1229
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1230
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1231 1232 1233
  return tlen;
}

H
Hongze Cheng 已提交
1234
int32_t tDeserializeSGetUserAuthRspImpl(SDecoder *pDecoder, SGetUserAuthRsp *pRsp) {
D
dapan 已提交
1235 1236 1237
  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);
S
Shengliang Guan 已提交
1238
  if (pRsp->readDbs == NULL || pRsp->writeDbs == NULL) {
S
Shengliang Guan 已提交
1239
    return -1;
S
Shengliang Guan 已提交
1240 1241
  }

D
dapan 已提交
1242 1243 1244
  if (tDecodeCStrTo(pDecoder, pRsp->user) < 0) return -1;
  if (tDecodeI8(pDecoder, &pRsp->superAuth) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->version) < 0) return -1;
S
Shengliang Guan 已提交
1245

D
dapan 已提交
1246
  int32_t numOfCreatedDbs = 0;
S
Shengliang Guan 已提交
1247 1248
  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
D
dapan 已提交
1249 1250 1251 1252 1253 1254 1255
  if (tDecodeI32(pDecoder, &numOfCreatedDbs) < 0) return -1;
  if (tDecodeI32(pDecoder, &numOfReadDbs) < 0) return -1;
  if (tDecodeI32(pDecoder, &numOfWriteDbs) < 0) return -1;

  for (int32_t i = 0; i < numOfCreatedDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
    if (tDecodeCStrTo(pDecoder, db) < 0) return -1;
D
fix bug  
dapan 已提交
1256
    int32_t len = strlen(db);
D
dapan 已提交
1257 1258
    taosHashPut(pRsp->createdDbs, db, len, db, len);
  }
S
Shengliang Guan 已提交
1259 1260 1261

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
D
dapan 已提交
1262
    if (tDecodeCStrTo(pDecoder, db) < 0) return -1;
D
fix bug  
dapan 已提交
1263
    int32_t len = strlen(db);
S
Shengliang Guan 已提交
1264
    taosHashPut(pRsp->readDbs, db, len, db, len);
S
Shengliang Guan 已提交
1265 1266 1267 1268
  }

  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
D
dapan 已提交
1269
    if (tDecodeCStrTo(pDecoder, db) < 0) return -1;
D
fix bug  
dapan 已提交
1270
    int32_t len = strlen(db);
S
Shengliang Guan 已提交
1271
    taosHashPut(pRsp->writeDbs, db, len, db, len);
S
Shengliang Guan 已提交
1272 1273
  }

D
dapan 已提交
1274 1275 1276 1277
  return 0;
}

int32_t tDeserializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pRsp) {
H
Hongze Cheng 已提交
1278 1279
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan 已提交
1280 1281 1282 1283 1284

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

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

S
Shengliang Guan 已提交
1285 1286
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1287
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1288
  return 0;
S
Shengliang Guan 已提交
1289
}
S
Shengliang Guan 已提交
1290

S
Shengliang Guan 已提交
1291
void tFreeSGetUserAuthRsp(SGetUserAuthRsp *pRsp) {
D
dapan 已提交
1292
  taosHashCleanup(pRsp->createdDbs);
S
Shengliang Guan 已提交
1293 1294 1295 1296
  taosHashCleanup(pRsp->readDbs);
  taosHashCleanup(pRsp->writeDbs);
}

1297
int32_t tSerializeSCreateDropMQSBNodeReq(void *buf, int32_t bufLen, SMCreateQnodeReq *pReq) {
H
Hongze Cheng 已提交
1298 1299
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1300 1301 1302 1303 1304 1305

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1306
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1307 1308 1309
  return tlen;
}

1310
int32_t tDeserializeSCreateDropMQSBNodeReq(void *buf, int32_t bufLen, SMCreateQnodeReq *pReq) {
H
Hongze Cheng 已提交
1311 1312
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1313 1314 1315 1316 1317

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

H
Hongze Cheng 已提交
1318
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1319 1320
  return 0;
}
S
Shengliang Guan 已提交
1321 1322

int32_t tSerializeSDropDnodeReq(void *buf, int32_t bufLen, SDropDnodeReq *pReq) {
H
Hongze Cheng 已提交
1323 1324
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
1325 1326 1327 1328 1329 1330 1331 1332

  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;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1333
  tEncoderClear(&encoder);
1334
  return tlen;
S
Shengliang Guan 已提交
1335 1336 1337
}

int32_t tDeserializeSDropDnodeReq(void *buf, int32_t bufLen, SDropDnodeReq *pReq) {
H
Hongze Cheng 已提交
1338 1339
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
1340 1341 1342 1343 1344 1345 1346

  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;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1347
  tDecoderClear(&decoder);
1348
  return 0;
S
Shengliang Guan 已提交
1349 1350 1351
}

int32_t tSerializeSMCfgDnodeReq(void *buf, int32_t bufLen, SMCfgDnodeReq *pReq) {
H
Hongze Cheng 已提交
1352 1353
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1354 1355 1356 1357 1358 1359 1360 1361

  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 已提交
1362
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1363 1364 1365 1366
  return tlen;
}

int32_t tDeserializeSMCfgDnodeReq(void *buf, int32_t bufLen, SMCfgDnodeReq *pReq) {
H
Hongze Cheng 已提交
1367 1368
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1369 1370 1371 1372 1373 1374 1375

  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 已提交
1376
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1377 1378 1379 1380
  return 0;
}

int32_t tSerializeSCreateDnodeReq(void *buf, int32_t bufLen, SCreateDnodeReq *pReq) {
H
Hongze Cheng 已提交
1381 1382
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1383 1384 1385 1386 1387 1388 1389

  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 已提交
1390
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1391 1392 1393 1394
  return tlen;
}

int32_t tDeserializeSCreateDnodeReq(void *buf, int32_t bufLen, SCreateDnodeReq *pReq) {
H
Hongze Cheng 已提交
1395 1396
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1397 1398 1399 1400 1401 1402

  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 已提交
1403
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1404 1405
  return 0;
}
S
Shengliang Guan 已提交
1406 1407

int32_t tSerializeSCreateFuncReq(void *buf, int32_t bufLen, SCreateFuncReq *pReq) {
H
Hongze Cheng 已提交
1408 1409
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1410 1411 1412 1413 1414 1415 1416 1417 1418

  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;
1419
  if (tEncodeI32(&encoder, pReq->codeLen) < 0) return -1;
S
Shengliang Guan 已提交
1420
  if (tEncodeI64(&encoder, pReq->signature) < 0) return -1;
1421 1422

  if (pReq->pCode != NULL) {
S
Shengliang Guan 已提交
1423
    if (tEncodeBinary(&encoder, pReq->pCode, pReq->codeLen) < 0) return -1;
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  }

  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;
  }

S
Shengliang Guan 已提交
1435 1436 1437
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1438
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1439 1440 1441 1442
  return tlen;
}

int32_t tDeserializeSCreateFuncReq(void *buf, int32_t bufLen, SCreateFuncReq *pReq) {
H
Hongze Cheng 已提交
1443 1444
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453

  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;
1454
  if (tDecodeI32(&decoder, &pReq->codeLen) < 0) return -1;
S
Shengliang Guan 已提交
1455
  if (tDecodeI64(&decoder, &pReq->signature) < 0) return -1;
1456

S
Shengliang Guan 已提交
1457 1458
  if (pReq->codeLen > 0) {
    pReq->pCode = taosMemoryCalloc(1, pReq->codeLen);
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
    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;
  }

S
Shengliang Guan 已提交
1477 1478
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1479
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1480 1481 1482
  return 0;
}

1483 1484 1485 1486 1487
void tFreeSCreateFuncReq(SCreateFuncReq *pReq) {
  taosMemoryFree(pReq->pCode);
  taosMemoryFree(pReq->pComment);
}

S
Shengliang Guan 已提交
1488
int32_t tSerializeSDropFuncReq(void *buf, int32_t bufLen, SDropFuncReq *pReq) {
H
Hongze Cheng 已提交
1489 1490
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1491 1492 1493 1494 1495 1496 1497

  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 已提交
1498
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1499 1500 1501 1502
  return tlen;
}

int32_t tDeserializeSDropFuncReq(void *buf, int32_t bufLen, SDropFuncReq *pReq) {
H
Hongze Cheng 已提交
1503 1504
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1505 1506 1507 1508 1509 1510

  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 已提交
1511
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1512 1513 1514 1515
  return 0;
}

int32_t tSerializeSRetrieveFuncReq(void *buf, int32_t bufLen, SRetrieveFuncReq *pReq) {
H
Hongze Cheng 已提交
1516 1517
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1518 1519 1520

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfFuncs) < 0) return -1;
D
dapan1121 已提交
1521
  if (tEncodeI8(&encoder, pReq->ignoreCodeComment) < 0) return -1;
S
Shengliang Guan 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531

  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 已提交
1532
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1533 1534 1535 1536
  return tlen;
}

int32_t tDeserializeSRetrieveFuncReq(void *buf, int32_t bufLen, SRetrieveFuncReq *pReq) {
H
Hongze Cheng 已提交
1537 1538
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1539 1540 1541

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfFuncs) < 0) return -1;
D
dapan1121 已提交
1542
  if (tDecodeI8(&decoder, (int8_t *)&pReq->ignoreCodeComment) < 0) return -1;
S
Shengliang Guan 已提交
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553

  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 已提交
1554
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1555 1556 1557
  return 0;
}

1558 1559
void tFreeSRetrieveFuncReq(SRetrieveFuncReq *pReq) { taosArrayDestroy(pReq->pFuncNames); }

S
Shengliang Guan 已提交
1560
int32_t tSerializeSRetrieveFuncRsp(void *buf, int32_t bufLen, SRetrieveFuncRsp *pRsp) {
H
Hongze Cheng 已提交
1561 1562
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

  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;
1579
    if (tEncodeI32(&encoder, pInfo->commentSize) < 0) return -1;
D
dapan1121 已提交
1580
    if (pInfo->codeSize) {
S
Shengliang Guan 已提交
1581
      if (tEncodeBinary(&encoder, pInfo->pCode, pInfo->codeSize) < 0) return -1;
D
dapan1121 已提交
1582 1583 1584 1585
    }
    if (pInfo->commentSize) {
      if (tEncodeCStr(&encoder, pInfo->pComment) < 0) return -1;
    }
S
Shengliang Guan 已提交
1586 1587 1588 1589 1590
  }

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1591
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1592 1593 1594 1595
  return tlen;
}

int32_t tDeserializeSRetrieveFuncRsp(void *buf, int32_t bufLen, SRetrieveFuncRsp *pRsp) {
H
Hongze Cheng 已提交
1596 1597
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614

  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;
1615
    if (tDecodeI32(&decoder, &fInfo.commentSize) < 0) return -1;
D
dapan1121 已提交
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
    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;
1631 1632
    }

S
Shengliang Guan 已提交
1633 1634 1635 1636
    taosArrayPush(pRsp->pFuncInfos, &fInfo);
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1637
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1638 1639
  return 0;
}
S
Shengliang Guan 已提交
1640

D
dapan1121 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649
void tFreeSFuncInfo(SFuncInfo *pInfo) {
  if (NULL == pInfo) {
    return;
  }

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

1650 1651 1652 1653
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 已提交
1654
    tFreeSFuncInfo(pInfo);
1655 1656 1657 1658
  }
  taosArrayDestroy(pRsp->pFuncInfos);
}

S
Shengliang Guan 已提交
1659
int32_t tSerializeSCreateDbReq(void *buf, int32_t bufLen, SCreateDbReq *pReq) {
H
Hongze Cheng 已提交
1660 1661
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1662 1663 1664 1665

  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 已提交
1666 1667 1668 1669
  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;
S
Shengliang Guan 已提交
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
  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;
  if (tEncodeI32(&encoder, pReq->fsyncPeriod) < 0) return -1;
  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 已提交
1681
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
1682
  if (tEncodeI8(&encoder, pReq->cacheLastRow) < 0) return -1;
X
Xiaoyu Wang 已提交
1683
  if (tEncodeI8(&encoder, pReq->schemaless) < 0) return -1;
S
Shengliang Guan 已提交
1684
  if (tEncodeI8(&encoder, pReq->ignoreExist) < 0) return -1;
S
Shengliang Guan 已提交
1685 1686 1687
  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 已提交
1688 1689
    if (tEncodeI64(&encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI64(&encoder, pRetension->keep) < 0) return -1;
S
sma  
Shengliang Guan 已提交
1690 1691
    if (tEncodeI8(&encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->keepUnit) < 0) return -1;
S
Shengliang Guan 已提交
1692
  }
S
Shengliang Guan 已提交
1693 1694 1695
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1696
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1697 1698 1699 1700
  return tlen;
}

int32_t tDeserializeSCreateDbReq(void *buf, int32_t bufLen, SCreateDbReq *pReq) {
H
Hongze Cheng 已提交
1701 1702
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1703 1704 1705 1706

  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 已提交
1707 1708 1709 1710
  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;
S
Shengliang Guan 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
  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;
  if (tDecodeI32(&decoder, &pReq->fsyncPeriod) < 0) return -1;
  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 已提交
1722
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
1723
  if (tDecodeI8(&decoder, &pReq->cacheLastRow) < 0) return -1;
X
Xiaoyu Wang 已提交
1724
  if (tDecodeI8(&decoder, &pReq->schemaless) < 0) return -1;
S
Shengliang Guan 已提交
1725
  if (tDecodeI8(&decoder, &pReq->ignoreExist) < 0) return -1;
S
Shengliang Guan 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734
  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 已提交
1735 1736
    if (tDecodeI64(&decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI64(&decoder, &rentension.keep) < 0) return -1;
S
sma  
Shengliang Guan 已提交
1737 1738
    if (tDecodeI8(&decoder, &rentension.freqUnit) < 0) return -1;
    if (tDecodeI8(&decoder, &rentension.keepUnit) < 0) return -1;
S
Shengliang Guan 已提交
1739 1740 1741 1742 1743 1744
    if (taosArrayPush(pReq->pRetensions, &rentension) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

S
Shengliang Guan 已提交
1745 1746
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1747
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1748 1749 1750
  return 0;
}

S
Shengliang Guan 已提交
1751 1752 1753 1754 1755
void tFreeSCreateDbReq(SCreateDbReq *pReq) {
  taosArrayDestroy(pReq->pRetensions);
  pReq->pRetensions = NULL;
}

S
Shengliang Guan 已提交
1756
int32_t tSerializeSAlterDbReq(void *buf, int32_t bufLen, SAlterDbReq *pReq) {
H
Hongze Cheng 已提交
1757 1758
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1759 1760 1761

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
S
Shengliang Guan 已提交
1762 1763 1764
  if (tEncodeI32(&encoder, pReq->buffer) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pageSize) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pages) < 0) return -1;
S
Shengliang Guan 已提交
1765
  if (tEncodeI32(&encoder, pReq->daysPerFile) < 0) return -1;
S
Shengliang Guan 已提交
1766 1767 1768 1769 1770
  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->fsyncPeriod) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->walLevel) < 0) return -1;
S
Shengliang Guan 已提交
1771
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
1772
  if (tEncodeI8(&encoder, pReq->cacheLastRow) < 0) return -1;
X
Xiaoyu Wang 已提交
1773
  if (tEncodeI8(&encoder, pReq->replications) < 0) return -1;
S
Shengliang Guan 已提交
1774 1775 1776
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1777
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1778 1779 1780 1781
  return tlen;
}

int32_t tDeserializeSAlterDbReq(void *buf, int32_t bufLen, SAlterDbReq *pReq) {
H
Hongze Cheng 已提交
1782 1783
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1784 1785 1786

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
S
Shengliang Guan 已提交
1787 1788 1789
  if (tDecodeI32(&decoder, &pReq->buffer) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pageSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pages) < 0) return -1;
S
Shengliang Guan 已提交
1790
  if (tDecodeI32(&decoder, &pReq->daysPerFile) < 0) return -1;
S
Shengliang Guan 已提交
1791 1792 1793 1794 1795
  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->fsyncPeriod) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->walLevel) < 0) return -1;
S
Shengliang Guan 已提交
1796
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
1797
  if (tDecodeI8(&decoder, &pReq->cacheLastRow) < 0) return -1;
X
Xiaoyu Wang 已提交
1798
  if (tDecodeI8(&decoder, &pReq->replications) < 0) return -1;
S
Shengliang Guan 已提交
1799 1800
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1801
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1802 1803 1804 1805
  return 0;
}

int32_t tSerializeSDropDbReq(void *buf, int32_t bufLen, SDropDbReq *pReq) {
H
Hongze Cheng 已提交
1806 1807
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1808 1809 1810 1811 1812 1813 1814

  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 已提交
1815
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1816 1817 1818 1819
  return tlen;
}

int32_t tDeserializeSDropDbReq(void *buf, int32_t bufLen, SDropDbReq *pReq) {
H
Hongze Cheng 已提交
1820 1821
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1822 1823 1824 1825 1826 1827

  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 已提交
1828
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1829 1830 1831 1832
  return 0;
}

int32_t tSerializeSDropDbRsp(void *buf, int32_t bufLen, SDropDbRsp *pRsp) {
H
Hongze Cheng 已提交
1833 1834
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1835 1836 1837

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
1838
  if (tEncodeI64(&encoder, pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
1839 1840 1841
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1842
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1843 1844 1845 1846
  return tlen;
}

int32_t tDeserializeSDropDbRsp(void *buf, int32_t bufLen, SDropDbRsp *pRsp) {
H
Hongze Cheng 已提交
1847 1848
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1849 1850 1851

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

H
Hongze Cheng 已提交
1855
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1856 1857 1858 1859
  return 0;
}

int32_t tSerializeSUseDbReq(void *buf, int32_t bufLen, SUseDbReq *pReq) {
H
Hongze Cheng 已提交
1860 1861
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1862 1863 1864

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
D
dapan1121 已提交
1865
  if (tEncodeI64(&encoder, pReq->dbId) < 0) return -1;
S
Shengliang Guan 已提交
1866
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
D
dapan1121 已提交
1867
  if (tEncodeI32(&encoder, pReq->numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
1868 1869 1870
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1871
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1872 1873 1874 1875
  return tlen;
}

int32_t tDeserializeSUseDbReq(void *buf, int32_t bufLen, SUseDbReq *pReq) {
H
Hongze Cheng 已提交
1876 1877
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1878 1879 1880

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
D
dapan1121 已提交
1881
  if (tDecodeI64(&decoder, &pReq->dbId) < 0) return -1;
S
Shengliang Guan 已提交
1882
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
D
dapan1121 已提交
1883
  if (tDecodeI32(&decoder, &pReq->numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
1884 1885
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1886
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1887 1888 1889
  return 0;
}

L
Liu Jicong 已提交
1890
int32_t tSerializeSQnodeListReq(void *buf, int32_t bufLen, SQnodeListReq *pReq) {
H
Hongze Cheng 已提交
1891 1892
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
1893 1894 1895 1896 1897 1898

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1899
  tEncoderClear(&encoder);
D
dapan1121 已提交
1900 1901 1902 1903
  return tlen;
}

int32_t tDeserializeSQnodeListReq(void *buf, int32_t bufLen, SQnodeListReq *pReq) {
H
Hongze Cheng 已提交
1904 1905
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
1906 1907 1908 1909 1910

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

H
Hongze Cheng 已提交
1911
  tDecoderClear(&decoder);
D
dapan1121 已提交
1912 1913 1914 1915
  return 0;
}

int32_t tSerializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp) {
H
Hongze Cheng 已提交
1916 1917
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
1918 1919

  if (tStartEncode(&encoder) < 0) return -1;
D
dapan1121 已提交
1920
  int32_t num = taosArrayGetSize(pRsp->addrsList);
L
Liu Jicong 已提交
1921
  if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
1922
  for (int32_t i = 0; i < num; ++i) {
D
dapan1121 已提交
1923 1924
    SQueryNodeAddr *addr = taosArrayGet(pRsp->addrsList, i);
    if (tEncodeSQueryNodeAddr(&encoder, addr) < 0) return -1;
D
dapan1121 已提交
1925 1926 1927 1928
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1929
  tEncoderClear(&encoder);
D
dapan1121 已提交
1930 1931 1932 1933
  return tlen;
}

int32_t tDeserializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp) {
H
Hongze Cheng 已提交
1934 1935
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
1936 1937 1938 1939

  if (tStartDecode(&decoder) < 0) return -1;
  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
D
dapan 已提交
1940 1941 1942 1943
  if (NULL == pRsp->addrsList) {
    pRsp->addrsList = taosArrayInit(num, sizeof(SQueryNodeAddr));
    if (NULL == pRsp->addrsList) return -1;
  }
1944

D
dapan1121 已提交
1945
  for (int32_t i = 0; i < num; ++i) {
D
dapan 已提交
1946 1947 1948
    SQueryNodeAddr addr = {0};
    if (tDecodeSQueryNodeAddr(&decoder, &addr) < 0) return -1;
    taosArrayPush(pRsp->addrsList, &addr);
D
dapan1121 已提交
1949 1950 1951
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
1952
  tDecoderClear(&decoder);
D
dapan1121 已提交
1953 1954 1955
  return 0;
}

D
dapan1121 已提交
1956
void tFreeSQnodeListRsp(SQnodeListRsp *pRsp) { taosArrayDestroy(pRsp->addrsList); }
D
dapan1121 已提交
1957

S
Shengliang Guan 已提交
1958
int32_t tSerializeSCompactDbReq(void *buf, int32_t bufLen, SCompactDbReq *pReq) {
H
Hongze Cheng 已提交
1959 1960
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
1961 1962 1963 1964 1965 1966

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
1967
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
1968 1969 1970
  return tlen;
}

S
Shengliang Guan 已提交
1971
int32_t tDeserializeSCompactDbReq(void *buf, int32_t bufLen, SCompactDbReq *pReq) {
H
Hongze Cheng 已提交
1972 1973
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
1974 1975 1976 1977 1978

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

H
Hongze Cheng 已提交
1979
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
1980
  return 0;
S
Shengliang Guan 已提交
1981 1982
}

H
Hongze Cheng 已提交
1983
int32_t tSerializeSUseDbRspImp(SEncoder *pEncoder, const SUseDbRsp *pRsp) {
S
Shengliang Guan 已提交
1984
  if (tEncodeCStr(pEncoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
1985
  if (tEncodeI64(pEncoder, pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
1986 1987 1988 1989 1990 1991 1992 1993 1994
  if (tEncodeI32(pEncoder, pRsp->vgVersion) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->vgNum) < 0) return -1;
  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 已提交
1995
    if (tEncodeSEpSet(pEncoder, &pVgInfo->epSet) < 0) return -1;
D
dapan1121 已提交
1996
    if (tEncodeI32(pEncoder, pVgInfo->numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
1997 1998 1999 2000 2001
  }

  return 0;
}

L
Liu Jicong 已提交
2002
int32_t tSerializeSUseDbRsp(void *buf, int32_t bufLen, const SUseDbRsp *pRsp) {
H
Hongze Cheng 已提交
2003 2004
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2005 2006 2007 2008 2009 2010

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2011
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2012 2013 2014 2015
  return tlen;
}

int32_t tSerializeSUseDbBatchRsp(void *buf, int32_t bufLen, SUseDbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
2016 2017
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029

  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) {
    SUseDbRsp *pUsedbRsp = taosArrayGet(pRsp->pArray, i);
    if (tSerializeSUseDbRspImp(&encoder, pUsedbRsp) < 0) return -1;
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2030
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2031 2032 2033
  return tlen;
}

H
Hongze Cheng 已提交
2034
int32_t tDeserializeSUseDbRspImp(SDecoder *pDecoder, SUseDbRsp *pRsp) {
S
Shengliang Guan 已提交
2035
  if (tDecodeCStrTo(pDecoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
2036
  if (tDecodeI64(pDecoder, &pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
2037 2038 2039 2040
  if (tDecodeI32(pDecoder, &pRsp->vgVersion) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->vgNum) < 0) return -1;
  if (tDecodeI8(pDecoder, &pRsp->hashMethod) < 0) return -1;

D
dapan1121 已提交
2041 2042 2043
  if (pRsp->vgNum <= 0) {
    return 0;
  }
L
Liu Jicong 已提交
2044

S
Shengliang Guan 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
  pRsp->pVgroupInfos = taosArrayInit(pRsp->vgNum, sizeof(SVgroupInfo));
  if (pRsp->pVgroupInfos == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  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;
L
Liu Jicong 已提交
2056
    if (tDecodeSEpSet(pDecoder, &vgInfo.epSet) < 0) return -1;
D
dapan1121 已提交
2057
    if (tDecodeI32(pDecoder, &vgInfo.numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
2058 2059 2060 2061 2062 2063 2064
    taosArrayPush(pRsp->pVgroupInfos, &vgInfo);
  }

  return 0;
}

int32_t tDeserializeSUseDbRsp(void *buf, int32_t bufLen, SUseDbRsp *pRsp) {
H
Hongze Cheng 已提交
2065 2066
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2067 2068 2069 2070 2071

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

H
Hongze Cheng 已提交
2072
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2073 2074 2075 2076
  return 0;
}

int32_t tDeserializeSUseDbBatchRsp(void *buf, int32_t bufLen, SUseDbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
2077 2078
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2079 2080 2081 2082 2083 2084

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

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

D
dapan1121 已提交
2085
  pRsp->pArray = taosArrayInit(numOfBatch, sizeof(SUseDbRsp));
S
Shengliang Guan 已提交
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
  if (pRsp->pArray == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

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

H
Hongze Cheng 已提交
2098
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
  return 0;
}

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

void tFreeSUseDbBatchRsp(SUseDbBatchRsp *pRsp) {
  int32_t numOfBatch = taosArrayGetSize(pRsp->pArray);
  for (int32_t i = 0; i < numOfBatch; ++i) {
    SUseDbRsp *pUsedbRsp = taosArrayGet(pRsp->pArray, i);
    tFreeSUsedbRsp(pUsedbRsp);
  }

  taosArrayDestroy(pRsp->pArray);
S
Shengliang Guan 已提交
2112 2113
}

H
Hongze Cheng 已提交
2114
int32_t tSerializeSUserAuthBatchRsp(void *buf, int32_t bufLen, SUserAuthBatchRsp *pRsp) {
H
Hongze Cheng 已提交
2115 2116
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128

  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 已提交
2129
  tEncoderClear(&encoder);
D
dapan 已提交
2130 2131 2132
  return tlen;
}

H
Hongze Cheng 已提交
2133
int32_t tDeserializeSUserAuthBatchRsp(void *buf, int32_t bufLen, SUserAuthBatchRsp *pRsp) {
H
Hongze Cheng 已提交
2134 2135
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan 已提交
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154

  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 已提交
2155
  tDecoderClear(&decoder);
D
dapan 已提交
2156 2157 2158
  return 0;
}

H
Hongze Cheng 已提交
2159
void tFreeSUserAuthBatchRsp(SUserAuthBatchRsp *pRsp) {
D
dapan 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168
  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 已提交
2169
int32_t tSerializeSDbCfgReq(void *buf, int32_t bufLen, SDbCfgReq *pReq) {
H
Hongze Cheng 已提交
2170 2171
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
2172 2173 2174 2175 2176 2177

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2178
  tEncoderClear(&encoder);
D
dapan1121 已提交
2179 2180 2181
  return tlen;
}

S
Shengliang Guan 已提交
2182
int32_t tDeserializeSDbCfgReq(void *buf, int32_t bufLen, SDbCfgReq *pReq) {
H
Hongze Cheng 已提交
2183 2184
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
2185 2186 2187 2188 2189

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

H
Hongze Cheng 已提交
2190
  tDecoderClear(&decoder);
D
dapan1121 已提交
2191 2192 2193
  return 0;
}

S
Shengliang Guan 已提交
2194
int32_t tSerializeSDbCfgRsp(void *buf, int32_t bufLen, const SDbCfgRsp *pRsp) {
H
Hongze Cheng 已提交
2195 2196
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
2197 2198 2199

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->numOfVgroups) < 0) return -1;
S
Shengliang Guan 已提交
2200 2201 2202 2203
  if (tEncodeI32(&encoder, pRsp->numOfStables) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->buffer) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->pageSize) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->pages) < 0) return -1;
D
dapan1121 已提交
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
  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->fsyncPeriod) < 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;
S
Shengliang Guan 已提交
2215
  if (tEncodeI8(&encoder, pRsp->strict) < 0) return -1;
D
dapan1121 已提交
2216
  if (tEncodeI8(&encoder, pRsp->cacheLastRow) < 0) return -1;
2217 2218 2219
  if (tEncodeI32(&encoder, pRsp->numOfRetensions) < 0) return -1;
  for (int32_t i = 0; i < pRsp->numOfRetensions; ++i) {
    SRetention *pRetension = taosArrayGet(pRsp->pRetensions, i);
C
Cary Xu 已提交
2220 2221
    if (tEncodeI64(&encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI64(&encoder, pRetension->keep) < 0) return -1;
2222 2223 2224
    if (tEncodeI8(&encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->keepUnit) < 0) return -1;
  }
D
dapan1121 已提交
2225 2226 2227
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2228
  tEncoderClear(&encoder);
D
dapan1121 已提交
2229 2230 2231
  return tlen;
}

S
Shengliang Guan 已提交
2232
int32_t tDeserializeSDbCfgRsp(void *buf, int32_t bufLen, SDbCfgRsp *pRsp) {
H
Hongze Cheng 已提交
2233 2234
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
2235 2236 2237

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->numOfVgroups) < 0) return -1;
S
Shengliang Guan 已提交
2238 2239 2240 2241
  if (tDecodeI32(&decoder, &pRsp->numOfStables) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->buffer) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->pageSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->pages) < 0) return -1;
D
dapan1121 已提交
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
  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->fsyncPeriod) < 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;
S
Shengliang Guan 已提交
2253
  if (tDecodeI8(&decoder, &pRsp->strict) < 0) return -1;
D
dapan1121 已提交
2254
  if (tDecodeI8(&decoder, &pRsp->cacheLastRow) < 0) return -1;
2255 2256 2257 2258 2259 2260
  if (tDecodeI32(&decoder, &pRsp->numOfRetensions) < 0) return -1;
  pRsp->pRetensions = taosArrayInit(pRsp->numOfRetensions, sizeof(SRetention));
  if (pRsp->pRetensions == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
D
dapan1121 已提交
2261

2262 2263
  for (int32_t i = 0; i < pRsp->numOfRetensions; ++i) {
    SRetention rentension = {0};
C
Cary Xu 已提交
2264 2265
    if (tDecodeI64(&decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI64(&decoder, &rentension.keep) < 0) return -1;
2266 2267 2268 2269 2270 2271 2272
    if (tDecodeI8(&decoder, &rentension.freqUnit) < 0) return -1;
    if (tDecodeI8(&decoder, &rentension.keepUnit) < 0) return -1;
    if (taosArrayPush(pRsp->pRetensions, &rentension) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }
D
dapan1121 已提交
2273 2274
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2275
  tDecoderClear(&decoder);
D
dapan1121 已提交
2276 2277 2278
  return 0;
}

S
Shengliang Guan 已提交
2279
int32_t tSerializeSUserIndexReq(void *buf, int32_t bufLen, SUserIndexReq *pReq) {
H
Hongze Cheng 已提交
2280 2281
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
2282 2283 2284 2285 2286 2287

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2288
  tEncoderClear(&encoder);
D
dapan1121 已提交
2289 2290 2291
  return tlen;
}

S
Shengliang Guan 已提交
2292
int32_t tDeserializeSUserIndexReq(void *buf, int32_t bufLen, SUserIndexReq *pReq) {
H
Hongze Cheng 已提交
2293 2294
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
2295 2296 2297 2298 2299

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

H
Hongze Cheng 已提交
2300
  tDecoderClear(&decoder);
D
dapan1121 已提交
2301 2302 2303
  return 0;
}

S
Shengliang Guan 已提交
2304
int32_t tSerializeSUserIndexRsp(void *buf, int32_t bufLen, const SUserIndexRsp *pRsp) {
H
Hongze Cheng 已提交
2305 2306
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316

  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 已提交
2317
  tEncoderClear(&encoder);
D
dapan1121 已提交
2318 2319 2320
  return tlen;
}

S
Shengliang Guan 已提交
2321
int32_t tDeserializeSUserIndexRsp(void *buf, int32_t bufLen, SUserIndexRsp *pRsp) {
H
Hongze Cheng 已提交
2322 2323
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
2324 2325 2326 2327 2328 2329 2330 2331 2332

  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 已提交
2333
  tDecoderClear(&decoder);
D
dapan1121 已提交
2334 2335 2336
  return 0;
}

S
Shengliang Guan 已提交
2337
int32_t tSerializeSShowReq(void *buf, int32_t bufLen, SShowReq *pReq) {
H
Hongze Cheng 已提交
2338 2339
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2340 2341 2342 2343 2344 2345

  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 已提交
2346
    if (tEncodeBinary(&encoder, pReq->payload, pReq->payloadLen) < 0) return -1;
S
Shengliang Guan 已提交
2347 2348 2349 2350
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2351
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2352 2353 2354 2355
  return tlen;
}

int32_t tDeserializeSShowReq(void *buf, int32_t bufLen, SShowReq *pReq) {
H
Hongze Cheng 已提交
2356 2357
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2358 2359 2360 2361 2362 2363

  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 已提交
2364
    pReq->payload = taosMemoryMalloc(pReq->payloadLen);
S
Shengliang Guan 已提交
2365 2366 2367 2368 2369
    if (pReq->payload == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->payload) < 0) return -1;
  }

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
2370
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2371 2372 2373
  return 0;
}

wafwerar's avatar
wafwerar 已提交
2374
void tFreeSShowReq(SShowReq *pReq) { taosMemoryFreeClear(pReq->payload); }
2375 2376

int32_t tSerializeSRetrieveTableReq(void *buf, int32_t bufLen, SRetrieveTableReq *pReq) {
H
Hongze Cheng 已提交
2377 2378
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
2379 2380 2381

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->showId) < 0) return -1;
H
Haojun Liao 已提交
2382 2383
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->tb) < 0) return -1;
2384 2385 2386
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2387
  tEncoderClear(&encoder);
2388 2389 2390 2391
  return tlen;
}

int32_t tDeserializeSRetrieveTableReq(void *buf, int32_t bufLen, SRetrieveTableReq *pReq) {
H
Hongze Cheng 已提交
2392 2393
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
2394 2395 2396

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->showId) < 0) return -1;
H
Haojun Liao 已提交
2397 2398
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->tb) < 0) return -1;
2399
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
2400
  tDecoderClear(&decoder);
2401 2402
  return 0;
}
S
Shengliang Guan 已提交
2403

H
Hongze Cheng 已提交
2404
static int32_t tEncodeSTableMetaRsp(SEncoder *pEncoder, STableMetaRsp *pRsp) {
S
Shengliang Guan 已提交
2405 2406 2407
  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 已提交
2408
  if (tEncodeI64(pEncoder, pRsp->dbId) < 0) return -1;
S
Shengliang Guan 已提交
2409 2410 2411 2412 2413 2414
  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 已提交
2415 2416
  if (tEncodeU64(pEncoder, pRsp->suid) < 0) return -1;
  if (tEncodeU64(pEncoder, pRsp->tuid) < 0) return -1;
S
Shengliang Guan 已提交
2417 2418 2419 2420 2421 2422 2423 2424 2425
  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 已提交
2426
static int32_t tDecodeSTableMetaRsp(SDecoder *pDecoder, STableMetaRsp *pRsp) {
S
Shengliang Guan 已提交
2427 2428 2429
  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 已提交
2430
  if (tDecodeI64(pDecoder, &pRsp->dbId) < 0) return -1;
S
Shengliang Guan 已提交
2431 2432 2433 2434 2435 2436
  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 已提交
2437 2438
  if (tDecodeU64(pDecoder, &pRsp->suid) < 0) return -1;
  if (tDecodeU64(pDecoder, &pRsp->tuid) < 0) return -1;
S
Shengliang Guan 已提交
2439 2440 2441
  if (tDecodeI32(pDecoder, &pRsp->vgId) < 0) return -1;

  int32_t totalCols = pRsp->numOfTags + pRsp->numOfColumns;
wafwerar's avatar
wafwerar 已提交
2442
  pRsp->pSchemas = taosMemoryMalloc(sizeof(SSchema) * totalCols);
S
Shengliang Guan 已提交
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
  if (pRsp->pSchemas == NULL) return -1;

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

  return 0;
}

int32_t tSerializeSTableMetaRsp(void *buf, int32_t bufLen, STableMetaRsp *pRsp) {
H
Hongze Cheng 已提交
2454 2455
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2456 2457 2458 2459 2460 2461

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

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

int32_t tSerializeSTableMetaBatchRsp(void *buf, int32_t bufLen, STableMetaBatchRsp *pRsp) {
H
Hongze Cheng 已提交
2467 2468
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480

  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) {
    STableMetaRsp *pMetaRsp = taosArrayGet(pRsp->pArray, i);
    if (tEncodeSTableMetaRsp(&encoder, pMetaRsp) < 0) return -1;
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2481
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2482 2483 2484 2485
  return tlen;
}

int32_t tDeserializeSTableMetaRsp(void *buf, int32_t bufLen, STableMetaRsp *pRsp) {
H
Hongze Cheng 已提交
2486 2487
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2488 2489 2490 2491 2492

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

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
2493
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2494 2495 2496 2497
  return 0;
}

int32_t tDeserializeSTableMetaBatchRsp(void *buf, int32_t bufLen, STableMetaBatchRsp *pRsp) {
H
Hongze Cheng 已提交
2498 2499
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518

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

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

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

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

H
Hongze Cheng 已提交
2519
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2520 2521 2522
  return 0;
}

wafwerar's avatar
wafwerar 已提交
2523
void tFreeSTableMetaRsp(STableMetaRsp *pRsp) { taosMemoryFreeClear(pRsp->pSchemas); }
S
Shengliang Guan 已提交
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535

void tFreeSTableMetaBatchRsp(STableMetaBatchRsp *pRsp) {
  int32_t numOfBatch = taosArrayGetSize(pRsp->pArray);
  for (int32_t i = 0; i < numOfBatch; ++i) {
    STableMetaRsp *pMetaRsp = taosArrayGet(pRsp->pArray, i);
    tFreeSTableMetaRsp(pMetaRsp);
  }

  taosArrayDestroy(pRsp->pArray);
}

int32_t tSerializeSShowRsp(void *buf, int32_t bufLen, SShowRsp *pRsp) {
H
Hongze Cheng 已提交
2536 2537
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2538 2539 2540 2541 2542 2543 2544

  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 已提交
2545
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2546 2547 2548 2549
  return tlen;
}

int32_t tDeserializeSShowRsp(void *buf, int32_t bufLen, SShowRsp *pRsp) {
H
Hongze Cheng 已提交
2550 2551
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2552 2553 2554 2555 2556 2557

  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 已提交
2558
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2559 2560 2561 2562 2563 2564 2565
  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 已提交
2566 2567 2568 2569
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }
S
Shengliang Guan 已提交
2570

H
Hongze Cheng 已提交
2571 2572
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2573 2574 2575 2576 2577 2578 2579

  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 已提交
2580
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2581 2582 2583 2584 2585 2586 2587 2588

  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 已提交
2589 2590 2591
}

int32_t tDeserializeSTableInfoReq(void *buf, int32_t bufLen, STableInfoReq *pReq) {
S
Shengliang Guan 已提交
2592 2593 2594 2595 2596
  int32_t headLen = sizeof(SMsgHead);

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

H
Hongze Cheng 已提交
2598 2599
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);
S
Shengliang Guan 已提交
2600 2601 2602 2603 2604 2605

  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 已提交
2606
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2607 2608
  return 0;
}
S
Shengliang Guan 已提交
2609

S
Shengliang Guan 已提交
2610
int32_t tSerializeSMDropTopicReq(void *buf, int32_t bufLen, SMDropTopicReq *pReq) {
H
Hongze Cheng 已提交
2611 2612
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2613 2614 2615 2616 2617 2618 2619

  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 已提交
2620
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2621 2622 2623 2624
  return tlen;
}

int32_t tDeserializeSMDropTopicReq(void *buf, int32_t bufLen, SMDropTopicReq *pReq) {
H
Hongze Cheng 已提交
2625 2626
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2627 2628 2629 2630 2631 2632

  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 已提交
2633
  tDecoderClear(&decoder);
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
  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 已提交
2663 2664
  return 0;
}
S
Shengliang Guan 已提交
2665

L
Liu Jicong 已提交
2666
int32_t tSerializeSCMCreateTopicReq(void *buf, int32_t bufLen, const SCMCreateTopicReq *pReq) {
S
Shengliang Guan 已提交
2667
  int32_t sqlLen = 0;
2668
  int32_t astLen = 0;
S
Shengliang Guan 已提交
2669
  if (pReq->sql != NULL) sqlLen = (int32_t)strlen(pReq->sql);
2670
  if (pReq->ast != NULL) astLen = (int32_t)strlen(pReq->ast);
S
Shengliang Guan 已提交
2671

H
Hongze Cheng 已提交
2672 2673
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2674 2675 2676 2677

  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 已提交
2678 2679 2680
  if (tEncodeI8(&encoder, pReq->withTbName) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->withSchema) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->withTag) < 0) return -1;
2681
  if (tEncodeCStr(&encoder, pReq->subscribeDbName) < 0) return -1;
S
Shengliang Guan 已提交
2682
  if (tEncodeI32(&encoder, sqlLen) < 0) return -1;
2683 2684 2685
  if (tEncodeI32(&encoder, astLen) < 0) return -1;
  if (sqlLen > 0 && tEncodeCStr(&encoder, pReq->sql) < 0) return -1;
  if (astLen > 0 && tEncodeCStr(&encoder, pReq->ast) < 0) return -1;
S
Shengliang Guan 已提交
2686 2687 2688 2689

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2690
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2691 2692 2693
  return tlen;
}

L
Liu Jicong 已提交
2694
int32_t tDeserializeSCMCreateTopicReq(void *buf, int32_t bufLen, SCMCreateTopicReq *pReq) {
S
Shengliang Guan 已提交
2695
  int32_t sqlLen = 0;
2696
  int32_t astLen = 0;
S
Shengliang Guan 已提交
2697

H
Hongze Cheng 已提交
2698 2699
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2700 2701 2702 2703

  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 已提交
2704 2705 2706
  if (tDecodeI8(&decoder, &pReq->withTbName) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->withSchema) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->withTag) < 0) return -1;
2707
  if (tDecodeCStrTo(&decoder, pReq->subscribeDbName) < 0) return -1;
S
Shengliang Guan 已提交
2708
  if (tDecodeI32(&decoder, &sqlLen) < 0) return -1;
2709
  if (tDecodeI32(&decoder, &astLen) < 0) return -1;
S
Shengliang Guan 已提交
2710

2711
  if (sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
2712
    pReq->sql = taosMemoryCalloc(1, sqlLen + 1);
2713 2714 2715 2716 2717
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }

  if (astLen > 0) {
wafwerar's avatar
wafwerar 已提交
2718
    pReq->ast = taosMemoryCalloc(1, astLen + 1);
2719 2720
    if (pReq->ast == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
X
Xiaoyu Wang 已提交
2721
  } else {
2722
  }
S
Shengliang Guan 已提交
2723 2724 2725

  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2726
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2727 2728 2729
  return 0;
}

L
Liu Jicong 已提交
2730
void tFreeSCMCreateTopicReq(SCMCreateTopicReq *pReq) {
wafwerar's avatar
wafwerar 已提交
2731 2732
  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->ast);
S
Shengliang Guan 已提交
2733 2734
}

L
Liu Jicong 已提交
2735
int32_t tSerializeSCMCreateTopicRsp(void *buf, int32_t bufLen, const SCMCreateTopicRsp *pRsp) {
H
Hongze Cheng 已提交
2736 2737
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2738 2739 2740 2741 2742 2743

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2744
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2745 2746 2747
  return tlen;
}

L
Liu Jicong 已提交
2748
int32_t tDeserializeSCMCreateTopicRsp(void *buf, int32_t bufLen, SCMCreateTopicRsp *pRsp) {
H
Hongze Cheng 已提交
2749 2750
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2751 2752 2753 2754 2755

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

H
Hongze Cheng 已提交
2756
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2757 2758
  return 0;
}
S
Shengliang Guan 已提交
2759 2760

int32_t tSerializeSConnectReq(void *buf, int32_t bufLen, SConnectReq *pReq) {
H
Hongze Cheng 已提交
2761 2762
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2763 2764

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
2765
  if (tEncodeI8(&encoder, pReq->connType) < 0) return -1;
S
Shengliang Guan 已提交
2766 2767 2768
  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 已提交
2769 2770
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->passwd) < 0) return -1;
S
Shengliang Guan 已提交
2771 2772 2773 2774
  if (tEncodeI64(&encoder, pReq->startTime) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2775
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2776 2777 2778 2779
  return tlen;
}

int32_t tDeserializeSConnectReq(void *buf, int32_t bufLen, SConnectReq *pReq) {
H
Hongze Cheng 已提交
2780 2781
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2782 2783

  if (tStartDecode(&decoder) < 0) return -1;
L
Liu Jicong 已提交
2784
  if (tDecodeI8(&decoder, &pReq->connType) < 0) return -1;
S
Shengliang Guan 已提交
2785 2786 2787
  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 已提交
2788 2789
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->passwd) < 0) return -1;
S
Shengliang Guan 已提交
2790 2791 2792
  if (tDecodeI64(&decoder, &pReq->startTime) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2793
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2794 2795 2796 2797
  return 0;
}

int32_t tSerializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) {
H
Hongze Cheng 已提交
2798 2799
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2800 2801 2802 2803

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->acctId) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->clusterId) < 0) return -1;
D
dapan1121 已提交
2804
  if (tEncodeU32(&encoder, pRsp->connId) < 0) return -1;
D
dapan 已提交
2805
  if (tEncodeI32(&encoder, pRsp->dnodeNum) < 0) return -1;
S
Shengliang Guan 已提交
2806
  if (tEncodeI8(&encoder, pRsp->superUser) < 0) return -1;
L
Liu Jicong 已提交
2807
  if (tEncodeI8(&encoder, pRsp->connType) < 0) return -1;
S
Shengliang Guan 已提交
2808 2809 2810 2811 2812
  if (tEncodeSEpSet(&encoder, &pRsp->epSet) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->sVersion) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2813
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2814 2815 2816 2817
  return tlen;
}

int32_t tDeserializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) {
H
Hongze Cheng 已提交
2818 2819
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2820 2821 2822 2823

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->acctId) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->clusterId) < 0) return -1;
D
dapan1121 已提交
2824
  if (tDecodeU32(&decoder, &pRsp->connId) < 0) return -1;
D
dapan 已提交
2825
  if (tDecodeI32(&decoder, &pRsp->dnodeNum) < 0) return -1;
S
Shengliang Guan 已提交
2826
  if (tDecodeI8(&decoder, &pRsp->superUser) < 0) return -1;
L
Liu Jicong 已提交
2827
  if (tDecodeI8(&decoder, &pRsp->connType) < 0) return -1;
S
Shengliang Guan 已提交
2828 2829 2830 2831
  if (tDecodeSEpSet(&decoder, &pRsp->epSet) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->sVersion) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
2832
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2833 2834
  return 0;
}
S
Shengliang Guan 已提交
2835 2836

int32_t tSerializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) {
H
Hongze Cheng 已提交
2837 2838
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2839 2840 2841 2842 2843 2844

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2845
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2846 2847 2848 2849
  return tlen;
}

int32_t tDeserializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) {
H
Hongze Cheng 已提交
2850 2851
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2852 2853 2854 2855 2856

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

H
Hongze Cheng 已提交
2857
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2858 2859
  return 0;
}
S
Shengliang Guan 已提交
2860

H
Hongze Cheng 已提交
2861
int32_t tEncodeSReplica(SEncoder *pEncoder, SReplica *pReplica) {
S
Shengliang Guan 已提交
2862 2863 2864 2865 2866 2867
  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 已提交
2868
int32_t tDecodeSReplica(SDecoder *pDecoder, SReplica *pReplica) {
S
Shengliang Guan 已提交
2869 2870 2871 2872 2873 2874
  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 已提交
2875
int32_t tSerializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) {
H
Hongze Cheng 已提交
2876 2877
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2878 2879 2880 2881 2882

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
L
Liu Jicong 已提交
2883
  if (tEncodeI64(&encoder, pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
2884
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
S
Shengliang Guan 已提交
2885 2886 2887 2888
  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;
S
Shengliang Guan 已提交
2889 2890 2891 2892 2893 2894 2895
  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;
  if (tEncodeI32(&encoder, pReq->fsyncPeriod) < 0) return -1;
D
dapan1121 已提交
2896 2897 2898
  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 已提交
2899 2900 2901
  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 已提交
2902
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
2903 2904 2905 2906 2907
  if (tEncodeI8(&encoder, pReq->cacheLastRow) < 0) return -1;
  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 已提交
2908
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
S
Shengliang Guan 已提交
2909
  }
S
sma  
Shengliang Guan 已提交
2910 2911 2912
  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 已提交
2913 2914
    if (tEncodeI64(&encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI64(&encoder, pRetension->keep) < 0) return -1;
S
sma  
Shengliang Guan 已提交
2915 2916 2917
    if (tEncodeI8(&encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->keepUnit) < 0) return -1;
  }
S
Shengliang Guan 已提交
2918 2919 2920
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
2921
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
2922 2923 2924 2925
  return tlen;
}

int32_t tDeserializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) {
H
Hongze Cheng 已提交
2926 2927
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
2928 2929 2930 2931 2932

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
L
Liu Jicong 已提交
2933
  if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
2934
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
S
Shengliang Guan 已提交
2935 2936 2937 2938
  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;
S
Shengliang Guan 已提交
2939 2940 2941 2942 2943 2944 2945
  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;
  if (tDecodeI32(&decoder, &pReq->fsyncPeriod) < 0) return -1;
D
dapan1121 已提交
2946 2947 2948
  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 已提交
2949 2950 2951
  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 已提交
2952
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
2953 2954 2955 2956 2957
  if (tDecodeI8(&decoder, &pReq->cacheLastRow) < 0) return -1;
  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 已提交
2958
    if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
S
Shengliang Guan 已提交
2959 2960
  }

S
sma  
Shengliang Guan 已提交
2961 2962 2963 2964 2965 2966 2967 2968 2969
  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 已提交
2970 2971
    if (tDecodeI64(&decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI64(&decoder, &rentension.keep) < 0) return -1;
S
sma  
Shengliang Guan 已提交
2972 2973 2974 2975 2976 2977 2978 2979
    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 已提交
2980
  tEndDecode(&decoder);
H
Hongze Cheng 已提交
2981
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
2982 2983 2984
  return 0;
}

S
sma  
Shengliang Guan 已提交
2985 2986 2987
int32_t tFreeSCreateVnodeReq(SCreateVnodeReq *pReq) {
  taosArrayDestroy(pReq->pRetensions);
  pReq->pRetensions = NULL;
2988
  return 0;
S
sma  
Shengliang Guan 已提交
2989 2990
}

S
Shengliang Guan 已提交
2991
int32_t tSerializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) {
H
Hongze Cheng 已提交
2992 2993
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
2994 2995 2996 2997

  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 已提交
2998
  if (tEncodeI64(&encoder, pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
2999 3000 3001 3002
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3003
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3004 3005 3006 3007
  return tlen;
}

int32_t tDeserializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) {
H
Hongze Cheng 已提交
3008 3009
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3010 3011 3012 3013

  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 已提交
3014
  if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
3015 3016 3017
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3018
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3019 3020 3021
  return 0;
}

S
Shengliang Guan 已提交
3022
int32_t tSerializeSCompactVnodeReq(void *buf, int32_t bufLen, SCompactVnodeReq *pReq) {
H
Hongze Cheng 已提交
3023 3024
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3025 3026 3027 3028 3029 3030 3031

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3032
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3033 3034 3035 3036
  return tlen;
}

int32_t tDeserializeSCompactVnodeReq(void *buf, int32_t bufLen, SCompactVnodeReq *pReq) {
H
Hongze Cheng 已提交
3037 3038
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3039 3040 3041 3042 3043 3044

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

H
Hongze Cheng 已提交
3045
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3046 3047 3048 3049
  return 0;
}

int32_t tSerializeSAlterVnodeReq(void *buf, int32_t bufLen, SAlterVnodeReq *pReq) {
H
Hongze Cheng 已提交
3050 3051
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3052 3053 3054

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
S
Shengliang Guan 已提交
3055 3056 3057
  if (tEncodeI32(&encoder, pReq->buffer) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pageSize) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->pages) < 0) return -1;
S
Shengliang Guan 已提交
3058
  if (tEncodeI32(&encoder, pReq->daysPerFile) < 0) return -1;
S
Shengliang Guan 已提交
3059 3060 3061
  if (tEncodeI32(&encoder, pReq->daysToKeep0) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep1) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->daysToKeep2) < 0) return -1;
S
Shengliang Guan 已提交
3062
  if (tEncodeI32(&encoder, pReq->fsyncPeriod) < 0) return -1;
S
Shengliang Guan 已提交
3063 3064 3065 3066 3067 3068 3069 3070 3071
  if (tEncodeI8(&encoder, pReq->walLevel) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->cacheLastRow) < 0) return -1;
  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];
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
  }
dengyihao's avatar
dengyihao 已提交
3072

S
Shengliang Guan 已提交
3073 3074 3075
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3076
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3077 3078 3079 3080
  return tlen;
}

int32_t tDeserializeSAlterVnodeReq(void *buf, int32_t bufLen, SAlterVnodeReq *pReq) {
H
Hongze Cheng 已提交
3081 3082
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3083 3084 3085

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
S
Shengliang Guan 已提交
3086 3087 3088
  if (tDecodeI32(&decoder, &pReq->buffer) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pageSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->pages) < 0) return -1;
S
Shengliang Guan 已提交
3089
  if (tDecodeI32(&decoder, &pReq->daysPerFile) < 0) return -1;
S
Shengliang Guan 已提交
3090 3091 3092
  if (tDecodeI32(&decoder, &pReq->daysToKeep0) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep1) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->daysToKeep2) < 0) return -1;
S
Shengliang Guan 已提交
3093
  if (tDecodeI32(&decoder, &pReq->fsyncPeriod) < 0) return -1;
S
Shengliang Guan 已提交
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104
  if (tDecodeI8(&decoder, &pReq->walLevel) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->cacheLastRow) < 0) return -1;
  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];
    if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
  }

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
3105
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3106 3107 3108
  return 0;
}

S
Shengliang Guan 已提交
3109
int32_t tSerializeSKillQueryReq(void *buf, int32_t bufLen, SKillQueryReq *pReq) {
H
Hongze Cheng 已提交
3110 3111
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3112 3113 3114 3115 3116 3117 3118

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3119
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3120 3121 3122 3123
  return tlen;
}

int32_t tDeserializeSKillQueryReq(void *buf, int32_t bufLen, SKillQueryReq *pReq) {
H
Hongze Cheng 已提交
3124 3125
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3126 3127 3128 3129 3130 3131

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

H
Hongze Cheng 已提交
3132
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3133 3134 3135 3136
  return 0;
}

int32_t tSerializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) {
H
Hongze Cheng 已提交
3137 3138
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3139 3140 3141 3142 3143 3144

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3145
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3146 3147 3148 3149
  return tlen;
}

int32_t tDeserializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) {
H
Hongze Cheng 已提交
3150 3151
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3152 3153 3154 3155 3156

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

H
Hongze Cheng 已提交
3157
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3158
  return 0;
S
Shengliang Guan 已提交
3159 3160
}

S
Shengliang Guan 已提交
3161
int32_t tSerializeSKillTransReq(void *buf, int32_t bufLen, SKillTransReq *pReq) {
H
Hongze Cheng 已提交
3162 3163
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3164 3165 3166 3167 3168 3169

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

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3170
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3171 3172 3173 3174
  return tlen;
}

int32_t tDeserializeSKillTransReq(void *buf, int32_t bufLen, SKillTransReq *pReq) {
H
Hongze Cheng 已提交
3175 3176
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3177 3178 3179 3180 3181

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

H
Hongze Cheng 已提交
3182
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3183 3184 3185
  return 0;
}

S
Shengliang Guan 已提交
3186
int32_t tSerializeSDCreateMnodeReq(void *buf, int32_t bufLen, SDCreateMnodeReq *pReq) {
H
Hongze Cheng 已提交
3187 3188
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3189 3190 3191 3192 3193 3194 3195 3196 3197 3198

  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;
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3199
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3200 3201 3202 3203
  return tlen;
}

int32_t tDeserializeSDCreateMnodeReq(void *buf, int32_t bufLen, SDCreateMnodeReq *pReq) {
H
Hongze Cheng 已提交
3204 3205
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3206 3207 3208 3209 3210 3211 3212 3213 3214

  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;
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3215
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3216 3217 3218 3219
  return 0;
}

int32_t tSerializeSAuthReq(void *buf, int32_t bufLen, SAuthReq *pReq) {
H
Hongze Cheng 已提交
3220 3221
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231

  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 已提交
3232
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3233 3234 3235 3236
  return tlen;
}

int32_t tDeserializeSAuthReq(void *buf, int32_t bufLen, SAuthReq *pReq) {
H
Hongze Cheng 已提交
3237 3238
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3239 3240 3241 3242 3243 3244 3245 3246 3247

  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 已提交
3248
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3249 3250
  return 0;
}
L
Liu Jicong 已提交
3251

S
Shengliang Guan 已提交
3252
int32_t tSerializeSServerStatusRsp(void *buf, int32_t bufLen, SServerStatusRsp *pRsp) {
H
Hongze Cheng 已提交
3253 3254
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
S
Shengliang Guan 已提交
3255 3256 3257

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->statusCode) < 0) return -1;
S
Shengliang Guan 已提交
3258
  if (tEncodeCStr(&encoder, pRsp->details) < 0) return -1;
S
Shengliang Guan 已提交
3259 3260 3261 3262

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3263
  tEncoderClear(&encoder);
S
Shengliang Guan 已提交
3264 3265 3266 3267
  return tlen;
}

int32_t tDeserializeSServerStatusRsp(void *buf, int32_t bufLen, SServerStatusRsp *pRsp) {
H
Hongze Cheng 已提交
3268 3269
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
S
Shengliang Guan 已提交
3270 3271 3272

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->statusCode) < 0) return -1;
S
Shengliang Guan 已提交
3273
  if (tDecodeCStrTo(&decoder, pRsp->details) < 0) return -1;
S
Shengliang Guan 已提交
3274 3275

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
3276
  tDecoderClear(&decoder);
S
Shengliang Guan 已提交
3277 3278 3279
  return 0;
}

H
Hongze Cheng 已提交
3280
int32_t tEncodeSMqOffset(SEncoder *encoder, const SMqOffset *pOffset) {
L
Liu Jicong 已提交
3281 3282 3283 3284 3285 3286 3287
  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 已提交
3288
int32_t tDecodeSMqOffset(SDecoder *decoder, SMqOffset *pOffset) {
L
Liu Jicong 已提交
3289 3290 3291 3292 3293 3294 3295
  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 已提交
3296
int32_t tEncodeSMqCMCommitOffsetReq(SEncoder *encoder, const SMqCMCommitOffsetReq *pReq) {
L
Liu Jicong 已提交
3297 3298 3299 3300 3301 3302 3303 3304 3305
  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 已提交
3306
int32_t tDecodeSMqCMCommitOffsetReq(SDecoder *decoder, SMqCMCommitOffsetReq *pReq) {
L
Liu Jicong 已提交
3307
  if (tStartDecode(decoder) < 0) return -1;
L
Liu Jicong 已提交
3308
  if (tDecodeI32(decoder, &pReq->num) < 0) return -1;
H
Hongze Cheng 已提交
3309
  pReq->offsets = (SMqOffset *)tDecoderMalloc(decoder, sizeof(SMqOffset) * pReq->num);
L
Liu Jicong 已提交
3310 3311 3312 3313
  if (pReq->offsets == NULL) return -1;
  for (int32_t i = 0; i < pReq->num; i++) {
    tDecodeSMqOffset(decoder, &pReq->offsets[i]);
  }
L
Liu Jicong 已提交
3314
  tEndDecode(decoder);
L
Liu Jicong 已提交
3315 3316
  return 0;
}
D
dapan1121 已提交
3317

dengyihao's avatar
dengyihao 已提交
3318
int32_t tSerializeSExplainRsp(void *buf, int32_t bufLen, SExplainRsp *pRsp) {
H
Hongze Cheng 已提交
3319 3320
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
3321 3322 3323 3324 3325

  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];
3326 3327
    if (tEncodeDouble(&encoder, info->startupCost) < 0) return -1;
    if (tEncodeDouble(&encoder, info->totalCost) < 0) return -1;
D
dapan1121 已提交
3328
    if (tEncodeU64(&encoder, info->numOfRows) < 0) return -1;
3329 3330
    if (tEncodeU32(&encoder, info->verboseLen) < 0) return -1;
    if (tEncodeBinary(&encoder, info->verboseInfo, info->verboseLen) < 0) return -1;
D
dapan1121 已提交
3331 3332 3333 3334 3335
  }

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3336
  tEncoderClear(&encoder);
D
dapan1121 已提交
3337 3338 3339
  return tlen;
}

dengyihao's avatar
dengyihao 已提交
3340
int32_t tDeserializeSExplainRsp(void *buf, int32_t bufLen, SExplainRsp *pRsp) {
H
Hongze Cheng 已提交
3341 3342
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
3343 3344 3345 3346 3347 3348 3349 3350

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->numOfPlans) < 0) return -1;
  if (pRsp->numOfPlans > 0) {
    pRsp->subplanInfo = taosMemoryMalloc(pRsp->numOfPlans * sizeof(SExplainExecInfo));
    if (pRsp->subplanInfo == NULL) return -1;
  }
  for (int32_t i = 0; i < pRsp->numOfPlans; ++i) {
3351 3352
    if (tDecodeDouble(&decoder, &pRsp->subplanInfo[i].startupCost) < 0) return -1;
    if (tDecodeDouble(&decoder, &pRsp->subplanInfo[i].totalCost) < 0) return -1;
D
dapan1121 已提交
3353
    if (tDecodeU64(&decoder, &pRsp->subplanInfo[i].numOfRows) < 0) return -1;
3354
    if (tDecodeU32(&decoder, &pRsp->subplanInfo[i].verboseLen) < 0) return -1;
X
Xiaoyu Wang 已提交
3355 3356
    if (tDecodeBinary(&decoder, (uint8_t **)&pRsp->subplanInfo[i].verboseInfo, &pRsp->subplanInfo[i].verboseLen) < 0)
      return -1;
D
dapan1121 已提交
3357 3358 3359 3360
  }

  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3361
  tDecoderClear(&decoder);
D
dapan1121 已提交
3362 3363 3364
  return 0;
}

D
dapan1121 已提交
3365 3366 3367 3368 3369 3370 3371
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 已提交
3372 3373
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
3374 3375

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
3376 3377
  if (tEncodeU64(&encoder, pReq->sId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->epId.nodeId) < 0) return -1;
D
dapan1121 已提交
3378
  if (tEncodeU16(&encoder, pReq->epId.ep.port) < 0) return -1;
L
Liu Jicong 已提交
3379
  if (tEncodeCStr(&encoder, pReq->epId.ep.fqdn) < 0) return -1;
D
dapan1121 已提交
3380 3381
  if (pReq->taskAction) {
    int32_t num = taosArrayGetSize(pReq->taskAction);
L
Liu Jicong 已提交
3382
    if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
3383 3384
    for (int32_t i = 0; i < num; ++i) {
      STaskAction *action = taosArrayGet(pReq->taskAction, i);
L
Liu Jicong 已提交
3385 3386 3387
      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 已提交
3388 3389
    }
  } else {
L
Liu Jicong 已提交
3390
    if (tEncodeI32(&encoder, 0) < 0) return -1;
D
dapan1121 已提交
3391 3392 3393 3394
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3395
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
3396

D
dapan1121 已提交
3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
  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 已提交
3413 3414
  SDecoder decoder = {0};
  tDecoderInit(&decoder, (char *)buf + headLen, bufLen - headLen);
D
dapan1121 已提交
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437

  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 已提交
3438
  tDecoderClear(&decoder);
D
dapan1121 已提交
3439 3440 3441 3442 3443 3444
  return 0;
}

void tFreeSSchedulerHbReq(SSchedulerHbReq *pReq) { taosArrayDestroy(pReq->taskAction); }

int32_t tSerializeSSchedulerHbRsp(void *buf, int32_t bufLen, SSchedulerHbRsp *pRsp) {
H
Hongze Cheng 已提交
3445 3446
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
3447 3448

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
3449
  if (tEncodeI32(&encoder, pRsp->epId.nodeId) < 0) return -1;
D
dapan1121 已提交
3450
  if (tEncodeU16(&encoder, pRsp->epId.ep.port) < 0) return -1;
L
Liu Jicong 已提交
3451
  if (tEncodeCStr(&encoder, pRsp->epId.ep.fqdn) < 0) return -1;
D
dapan1121 已提交
3452 3453
  if (pRsp->taskStatus) {
    int32_t num = taosArrayGetSize(pRsp->taskStatus);
L
Liu Jicong 已提交
3454
    if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
3455 3456
    for (int32_t i = 0; i < num; ++i) {
      STaskStatus *status = taosArrayGet(pRsp->taskStatus, i);
L
Liu Jicong 已提交
3457 3458 3459 3460
      if (tEncodeU64(&encoder, status->queryId) < 0) return -1;
      if (tEncodeU64(&encoder, status->taskId) < 0) return -1;
      if (tEncodeI64(&encoder, status->refId) < 0) return -1;
      if (tEncodeI8(&encoder, status->status) < 0) return -1;
D
dapan1121 已提交
3461 3462
    }
  } else {
L
Liu Jicong 已提交
3463
    if (tEncodeI32(&encoder, 0) < 0) return -1;
D
dapan1121 已提交
3464 3465 3466 3467
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3468
  tEncoderClear(&encoder);
D
dapan1121 已提交
3469 3470 3471 3472
  return tlen;
}

int32_t tDeserializeSSchedulerHbRsp(void *buf, int32_t bufLen, SSchedulerHbRsp *pRsp) {
H
Hongze Cheng 已提交
3473 3474
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497

  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;
      if (tDecodeI8(&decoder, &status.status) < 0) return -1;
      taosArrayPush(pRsp->taskStatus, &status);
    }
  } else {
    pRsp->taskStatus = NULL;
  }
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3498
  tDecoderClear(&decoder);
D
dapan1121 已提交
3499 3500 3501 3502 3503
  return 0;
}

void tFreeSSchedulerHbRsp(SSchedulerHbRsp *pRsp) { taosArrayDestroy(pRsp->taskStatus); }

D
dapan1121 已提交
3504
int32_t tSerializeSQueryTableRsp(void *buf, int32_t bufLen, SQueryTableRsp *pRsp) {
H
Hongze Cheng 已提交
3505 3506
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
D
dapan1121 已提交
3507 3508

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
3509
  if (tEncodeI32(&encoder, pRsp->code) < 0) return -1;
D
dapan1121 已提交
3510 3511 3512
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3513
  tEncoderClear(&encoder);
D
dapan1121 已提交
3514 3515 3516 3517
  return tlen;
}

int32_t tDeserializeSQueryTableRsp(void *buf, int32_t bufLen, SQueryTableRsp *pRsp) {
H
Hongze Cheng 已提交
3518 3519
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
D
dapan1121 已提交
3520 3521 3522 3523 3524

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

H
Hongze Cheng 已提交
3525
  tDecoderClear(&decoder);
D
dapan1121 已提交
3526 3527 3528
  return 0;
}

D
dapan 已提交
3529
int32_t tSerializeSVCreateTbBatchRsp(void *buf, int32_t bufLen, SVCreateTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
3530 3531
  // SEncoder encoder = {0};
  // tEncoderInit(&encoder, buf, bufLen);
D
dapan 已提交
3532

H
Hongze Cheng 已提交
3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544
  // 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 已提交
3545

H
Hongze Cheng 已提交
3546
  // int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3547
  // tEncoderClear(&encoder);
H
Hongze Cheng 已提交
3548 3549
  // reture tlen;
  return 0;
D
dapan 已提交
3550 3551 3552
}

int32_t tDeserializeSVCreateTbBatchRsp(void *buf, int32_t bufLen, SVCreateTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
3553
  // SDecoder  decoder = {0};
H
Hongze Cheng 已提交
3554
  // int32_t num = 0;
H
Hongze Cheng 已提交
3555
  // tDecoderInit(&decoder, buf, bufLen);
D
dapan 已提交
3556

H
Hongze Cheng 已提交
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
  // 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 已提交
3571

H
Hongze Cheng 已提交
3572
  // tDecoderClear(&decoder);
D
dapan 已提交
3573 3574 3575
  return 0;
}

H
Hongze Cheng 已提交
3576
int tEncodeSVCreateTbBatchRsp(SEncoder *pCoder, const SVCreateTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
3577 3578 3579 3580 3581 3582 3583 3584 3585
  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 已提交
3586 3587
  }

H
Hongze Cheng 已提交
3588 3589 3590 3591 3592
  tEndEncode(pCoder);

  return 0;
}

H
Hongze Cheng 已提交
3593
int tDecodeSVCreateTbBatchRsp(SDecoder *pCoder, SVCreateTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
3594 3595 3596
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pRsp->nRsps) < 0) return -1;
H
Hongze Cheng 已提交
3597
  pRsp->pRsps = (SVCreateTbRsp *)tDecoderMalloc(pCoder, sizeof(*pRsp->pRsps) * pRsp->nRsps);
H
Hongze Cheng 已提交
3598 3599 3600 3601 3602
  for (int32_t i = 0; i < pRsp->nRsps; i++) {
    if (tDecodeSVCreateTbRsp(pCoder, pRsp->pRsps + i) < 0) return -1;
  }

  tEndDecode(pCoder);
D
dapan 已提交
3603 3604 3605
  return 0;
}

3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
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;
  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;
  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 已提交
3625

3626 3627 3628 3629 3630 3631 3632 3633
  return 0;
}

int32_t tDecodeTSma(SDecoder *pCoder, STSma *pSma) {
  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 已提交
3634
  if (tDecodeCStrTo(pCoder, pSma->indexName) < 0) return -1;
3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657
  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;
  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) {
    if (tDecodeCStr(pCoder, &pSma->expr) < 0) return -1;
  } else {
    pSma->expr = NULL;
  }
  if (pSma->tagsFilterLen > 0) {
    if (tDecodeCStr(pCoder, &pSma->tagsFilter) < 0) return -1;
  } else {
    pSma->tagsFilter = NULL;
  }

  return 0;
}

int32_t tEncodeSVCreateTSmaReq(SEncoder *pCoder, const SVCreateTSmaReq *pReq) {
  if (tStartEncode(pCoder) < 0) return -1;
C
Cary Xu 已提交
3658

3659
  tEncodeTSma(pCoder, pReq);
C
Cary Xu 已提交
3660

3661 3662
  tEndEncode(pCoder);
  return 0;
C
Cary Xu 已提交
3663
}
D
dapan1121 已提交
3664

3665 3666 3667 3668
int32_t tDecodeSVCreateTSmaReq(SDecoder *pCoder, SVCreateTSmaReq *pReq) {
  if (tStartDecode(pCoder) < 0) return -1;

  tDecodeTSma(pCoder, pReq);
D
dapan1121 已提交
3669

3670 3671
  tEndDecode(pCoder);
  return 0;
C
Cary Xu 已提交
3672 3673
}

3674 3675
int32_t tEncodeSVDropTSmaReq(SEncoder *pCoder, const SVDropTSmaReq *pReq) {
  if (tStartEncode(pCoder) < 0) return -1;
C
Cary Xu 已提交
3676

3677 3678
  if (tEncodeI64(pCoder, pReq->indexUid) < 0) return -1;
  if (tEncodeCStr(pCoder, pReq->indexName) < 0) return -1;
C
Cary Xu 已提交
3679

3680 3681
  tEndEncode(pCoder);
  return 0;
C
Cary Xu 已提交
3682 3683
}

3684 3685 3686 3687
int32_t tDecodeSVDropTSmaReq(SDecoder *pCoder, SVDropTSmaReq *pReq) {
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI64(pCoder, &pReq->indexUid) < 0) return -1;
C
Cary Xu 已提交
3688
  if (tDecodeCStrTo(pCoder, pReq->indexName) < 0) return -1;
3689 3690 3691

  tEndDecode(pCoder);
  return 0;
C
Cary Xu 已提交
3692
}
L
Liu Jicong 已提交
3693 3694

int32_t tSerializeSCMCreateStreamReq(void *buf, int32_t bufLen, const SCMCreateStreamReq *pReq) {
L
Liu Jicong 已提交
3695 3696 3697 3698 3699
  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 已提交
3700 3701
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
L
Liu Jicong 已提交
3702 3703 3704

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
L
Liu Jicong 已提交
3705
  if (tEncodeCStr(&encoder, pReq->sourceDB) < 0) return -1;
L
Liu Jicong 已提交
3706
  if (tEncodeCStr(&encoder, pReq->targetStbFullName) < 0) return -1;
L
Liu Jicong 已提交
3707
  if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
L
Liu Jicong 已提交
3708 3709
  if (tEncodeI32(&encoder, sqlLen) < 0) return -1;
  if (tEncodeI32(&encoder, astLen) < 0) return -1;
L
Liu Jicong 已提交
3710 3711
  if (tEncodeI8(&encoder, pReq->triggerType) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->watermark) < 0) return -1;
L
Liu Jicong 已提交
3712 3713 3714
  if (sqlLen > 0 && tEncodeCStr(&encoder, pReq->sql) < 0) return -1;
  if (astLen > 0 && tEncodeCStr(&encoder, pReq->ast) < 0) return -1;

L
Liu Jicong 已提交
3715 3716 3717
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
3718
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
3719 3720 3721 3722
  return tlen;
}

int32_t tDeserializeSCMCreateStreamReq(void *buf, int32_t bufLen, SCMCreateStreamReq *pReq) {
L
Liu Jicong 已提交
3723 3724 3725
  int32_t sqlLen = 0;
  int32_t astLen = 0;

H
Hongze Cheng 已提交
3726 3727
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
L
Liu Jicong 已提交
3728 3729 3730

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
L
Liu Jicong 已提交
3731
  if (tDecodeCStrTo(&decoder, pReq->sourceDB) < 0) return -1;
L
Liu Jicong 已提交
3732
  if (tDecodeCStrTo(&decoder, pReq->targetStbFullName) < 0) return -1;
L
Liu Jicong 已提交
3733
  if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
L
Liu Jicong 已提交
3734 3735
  if (tDecodeI32(&decoder, &sqlLen) < 0) return -1;
  if (tDecodeI32(&decoder, &astLen) < 0) return -1;
X
Xiaoyu Wang 已提交
3736 3737
  if (tDecodeI8(&decoder, &pReq->triggerType) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->watermark) < 0) return -1;
L
Liu Jicong 已提交
3738 3739

  if (sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
3740
    pReq->sql = taosMemoryCalloc(1, sqlLen + 1);
L
Liu Jicong 已提交
3741 3742 3743 3744 3745
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }

  if (astLen > 0) {
wafwerar's avatar
wafwerar 已提交
3746
    pReq->ast = taosMemoryCalloc(1, astLen + 1);
L
Liu Jicong 已提交
3747 3748 3749
    if (pReq->ast == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
  }
L
Liu Jicong 已提交
3750 3751
  tEndDecode(&decoder);

H
Hongze Cheng 已提交
3752
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
3753 3754 3755 3756
  return 0;
}

void tFreeSCMCreateStreamReq(SCMCreateStreamReq *pReq) {
wafwerar's avatar
wafwerar 已提交
3757 3758
  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->ast);
L
Liu Jicong 已提交
3759
}
C
Cary Xu 已提交
3760

H
Hongze Cheng 已提交
3761
int32_t tEncodeSRSmaParam(SEncoder *pCoder, const SRSmaParam *pRSmaParam) {
C
Cary Xu 已提交
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777
  if (tEncodeFloat(pCoder, pRSmaParam->xFilesFactor) < 0) return -1;
  if (tEncodeI32v(pCoder, pRSmaParam->delay) < 0) return -1;
  if (tEncodeI32v(pCoder, pRSmaParam->qmsg1Len) < 0) return -1;
  if (tEncodeI32v(pCoder, pRSmaParam->qmsg2Len) < 0) return -1;
  if (pRSmaParam->qmsg1Len > 0) {
    if (tEncodeBinary(pCoder, pRSmaParam->qmsg1, (uint64_t)pRSmaParam->qmsg1Len) < 0)  // qmsg1Len contains len of '\0'
      return -1;
  }
  if (pRSmaParam->qmsg2Len > 0) {
    if (tEncodeBinary(pCoder, pRSmaParam->qmsg2, (uint64_t)pRSmaParam->qmsg2Len) < 0)  // qmsg2Len contains len of '\0'
      return -1;
  }

  return 0;
}

H
Hongze Cheng 已提交
3778
int32_t tDecodeSRSmaParam(SDecoder *pCoder, SRSmaParam *pRSmaParam) {
C
Cary Xu 已提交
3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
  if (tDecodeFloat(pCoder, &pRSmaParam->xFilesFactor) < 0) return -1;
  if (tDecodeI32v(pCoder, &pRSmaParam->delay) < 0) return -1;
  if (tDecodeI32v(pCoder, &pRSmaParam->qmsg1Len) < 0) return -1;
  if (tDecodeI32v(pCoder, &pRSmaParam->qmsg2Len) < 0) return -1;
  if (pRSmaParam->qmsg1Len > 0) {
    uint64_t len;
    if (tDecodeBinaryAlloc(pCoder, (void **)&pRSmaParam->qmsg1, &len) < 0) return -1;  // qmsg1Len contains len of '\0'
  } else {
    pRSmaParam->qmsg1 = NULL;
  }
  if (pRSmaParam->qmsg2Len > 0) {
    uint64_t len;
    if (tDecodeBinaryAlloc(pCoder, (void **)&pRSmaParam->qmsg2, &len) < 0) return -1;  // qmsg2Len contains len of '\0'
  } else {
    pRSmaParam->qmsg2 = NULL;
  }
  return 0;
}

H
Hongze Cheng 已提交
3798
int tEncodeSVCreateStbReq(SEncoder *pCoder, const SVCreateStbReq *pReq) {
H
Hongze Cheng 已提交
3799 3800 3801 3802 3803
  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;
3804
  if (tEncodeSSchemaWrapper(pCoder, &pReq->schemaRow) < 0) return -1;
H
Hongze Cheng 已提交
3805
  if (tEncodeSSchemaWrapper(pCoder, &pReq->schemaTag) < 0) return -1;
C
Cary Xu 已提交
3806 3807 3808
  if (pReq->rollup) {
    if (tEncodeSRSmaParam(pCoder, &pReq->pRSmaParam) < 0) return -1;
  }
H
Hongze Cheng 已提交
3809 3810 3811 3812 3813

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3814
int tDecodeSVCreateStbReq(SDecoder *pCoder, SVCreateStbReq *pReq) {
H
Hongze Cheng 已提交
3815 3816 3817 3818 3819
  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;
3820
  if (tDecodeSSchemaWrapper(pCoder, &pReq->schemaRow) < 0) return -1;
H
Hongze Cheng 已提交
3821
  if (tDecodeSSchemaWrapper(pCoder, &pReq->schemaTag) < 0) return -1;
C
Cary Xu 已提交
3822 3823 3824
  if (pReq->rollup) {
    if (tDecodeSRSmaParam(pCoder, &pReq->pRSmaParam) < 0) return -1;
  }
H
Hongze Cheng 已提交
3825 3826 3827 3828

  tEndDecode(pCoder);
  return 0;
}
3829

C
Cary Xu 已提交
3830 3831
STSchema *tdGetSTSChemaFromSSChema(SSchema **pSchema, int32_t nCols) {
  STSchemaBuilder schemaBuilder = {0};
C
Cary Xu 已提交
3832
  if (tdInitTSchemaBuilder(&schemaBuilder, 1) < 0) {
C
Cary Xu 已提交
3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852
    return NULL;
  }

  for (int i = 0; i < nCols; i++) {
    SSchema *schema = *pSchema + i;
    if (tdAddColToSchema(&schemaBuilder, schema->type, schema->flags, schema->colId, schema->bytes) < 0) {
      tdDestroyTSchemaBuilder(&schemaBuilder);
      return NULL;
    }
  }

  STSchema *pNSchema = tdGetSchemaFromBuilder(&schemaBuilder);
  if (pNSchema == NULL) {
    tdDestroyTSchemaBuilder(&schemaBuilder);
    return NULL;
  }

  tdDestroyTSchemaBuilder(&schemaBuilder);
  return pNSchema;
}
H
Hongze Cheng 已提交
3853

H
Hongze Cheng 已提交
3854
int tEncodeSVCreateTbReq(SEncoder *pCoder, const SVCreateTbReq *pReq) {
H
Hongze Cheng 已提交
3855 3856
  if (tStartEncode(pCoder) < 0) return -1;

H
Hongze Cheng 已提交
3857
  if (tEncodeI32v(pCoder, pReq->flags) < 0) return -1;
H
Hongze Cheng 已提交
3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
  if (tEncodeI64(pCoder, pReq->uid) < 0) return -1;
  if (tEncodeI64(pCoder, pReq->ctime) < 0) return -1;

  if (tEncodeCStr(pCoder, pReq->name) < 0) return -1;
  if (tEncodeI32(pCoder, pReq->ttl) < 0) return -1;
  if (tEncodeI8(pCoder, pReq->type) < 0) return -1;

  if (pReq->type == TSDB_CHILD_TABLE) {
    if (tEncodeI64(pCoder, pReq->ctb.suid) < 0) return -1;
    if (tEncodeBinary(pCoder, pReq->ctb.pTag, kvRowLen(pReq->ctb.pTag)) < 0) return -1;
  } else if (pReq->type == TSDB_NORMAL_TABLE) {
3869
    if (tEncodeSSchemaWrapper(pCoder, &pReq->ntb.schemaRow) < 0) return -1;
H
Hongze Cheng 已提交
3870 3871 3872 3873 3874 3875 3876 3877
  } else {
    ASSERT(0);
  }

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3878
int tDecodeSVCreateTbReq(SDecoder *pCoder, SVCreateTbReq *pReq) {
H
Hongze Cheng 已提交
3879
  uint32_t len;
H
Hongze Cheng 已提交
3880

H
Hongze Cheng 已提交
3881 3882
  if (tStartDecode(pCoder) < 0) return -1;

H
Hongze Cheng 已提交
3883
  if (tDecodeI32v(pCoder, &pReq->flags) < 0) return -1;
H
Hongze Cheng 已提交
3884 3885 3886 3887 3888 3889 3890 3891 3892
  if (tDecodeI64(pCoder, &pReq->uid) < 0) return -1;
  if (tDecodeI64(pCoder, &pReq->ctime) < 0) return -1;

  if (tDecodeCStr(pCoder, &pReq->name) < 0) return -1;
  if (tDecodeI32(pCoder, &pReq->ttl) < 0) return -1;
  if (tDecodeI8(pCoder, &pReq->type) < 0) return -1;

  if (pReq->type == TSDB_CHILD_TABLE) {
    if (tDecodeI64(pCoder, &pReq->ctb.suid) < 0) return -1;
H
Hongze Cheng 已提交
3893
    if (tDecodeBinary(pCoder, &pReq->ctb.pTag, &len) < 0) return -1;
H
Hongze Cheng 已提交
3894
  } else if (pReq->type == TSDB_NORMAL_TABLE) {
3895
    if (tDecodeSSchemaWrapper(pCoder, &pReq->ntb.schemaRow) < 0) return -1;
H
Hongze Cheng 已提交
3896 3897 3898 3899
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
3900 3901 3902 3903
  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3904
int tEncodeSVCreateTbBatchReq(SEncoder *pCoder, const SVCreateTbBatchReq *pReq) {
H
Hongze Cheng 已提交
3905 3906
  int32_t nReq = taosArrayGetSize(pReq->pArray);

H
Hongze Cheng 已提交
3907 3908
  if (tStartEncode(pCoder) < 0) return -1;

H
Hongze Cheng 已提交
3909 3910
  if (tEncodeI32v(pCoder, nReq) < 0) return -1;
  for (int iReq = 0; iReq < nReq; iReq++) {
H
Hongze Cheng 已提交
3911 3912 3913 3914 3915 3916 3917
    if (tEncodeSVCreateTbReq(pCoder, (SVCreateTbReq *)taosArrayGet(pReq->pArray, iReq)) < 0) return -1;
  }

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3918
int tDecodeSVCreateTbBatchReq(SDecoder *pCoder, SVCreateTbBatchReq *pReq) {
H
Hongze Cheng 已提交
3919 3920 3921
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pReq->nReqs) < 0) return -1;
H
Hongze Cheng 已提交
3922
  pReq->pReqs = (SVCreateTbReq *)tDecoderMalloc(pCoder, sizeof(SVCreateTbReq) * pReq->nReqs);
H
Hongze Cheng 已提交
3923 3924 3925 3926 3927
  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 已提交
3928 3929 3930 3931
  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3932
int tEncodeSVCreateTbRsp(SEncoder *pCoder, const SVCreateTbRsp *pRsp) {
H
Hongze Cheng 已提交
3933 3934 3935 3936 3937 3938 3939 3940
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32(pCoder, pRsp->code) < 0) return -1;

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3941
int tDecodeSVCreateTbRsp(SDecoder *pCoder, SVCreateTbRsp *pRsp) {
H
Hongze Cheng 已提交
3942 3943 3944 3945
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32(pCoder, &pRsp->code) < 0) return -1;

H
Hongze Cheng 已提交
3946 3947 3948 3949 3950
  tEndDecode(pCoder);
  return 0;
}

// TDMT_VND_DROP_TABLE =================
H
Hongze Cheng 已提交
3951
static int32_t tEncodeSVDropTbReq(SEncoder *pCoder, const SVDropTbReq *pReq) {
H
Hongze Cheng 已提交
3952 3953 3954
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeCStr(pCoder, pReq->name) < 0) return -1;
X
Xiaoyu Wang 已提交
3955
  if (tEncodeI8(pCoder, pReq->igNotExists) < 0) return -1;
H
Hongze Cheng 已提交
3956 3957 3958 3959 3960

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3961
static int32_t tDecodeSVDropTbReq(SDecoder *pCoder, SVDropTbReq *pReq) {
H
Hongze Cheng 已提交
3962 3963 3964
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeCStr(pCoder, &pReq->name) < 0) return -1;
X
Xiaoyu Wang 已提交
3965
  if (tDecodeI8(pCoder, &pReq->igNotExists) < 0) return -1;
H
Hongze Cheng 已提交
3966 3967 3968 3969 3970

  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3971
static int32_t tEncodeSVDropTbRsp(SEncoder *pCoder, const SVDropTbRsp *pReq) {
H
Hongze Cheng 已提交
3972 3973 3974 3975 3976 3977 3978 3979
  if (tStartEncode(pCoder) < 0) return -1;

  if (tEncodeI32(pCoder, pReq->code) < 0) return -1;

  tEndEncode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3980
static int32_t tDecodeSVDropTbRsp(SDecoder *pCoder, SVDropTbRsp *pReq) {
H
Hongze Cheng 已提交
3981 3982 3983 3984 3985 3986 3987 3988
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32(pCoder, &pReq->code) < 0) return -1;

  tEndDecode(pCoder);
  return 0;
}

H
Hongze Cheng 已提交
3989
int32_t tEncodeSVDropTbBatchReq(SEncoder *pCoder, const SVDropTbBatchReq *pReq) {
H
Hongze Cheng 已提交
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004
  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 已提交
4005
int32_t tDecodeSVDropTbBatchReq(SDecoder *pCoder, SVDropTbBatchReq *pReq) {
H
Hongze Cheng 已提交
4006 4007 4008
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pReq->nReqs) < 0) return -1;
H
Hongze Cheng 已提交
4009
  pReq->pReqs = (SVDropTbReq *)tDecoderMalloc(pCoder, sizeof(SVDropTbReq) * pReq->nReqs);
H
Hongze Cheng 已提交
4010 4011 4012 4013 4014 4015 4016 4017 4018
  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 已提交
4019
int32_t tEncodeSVDropTbBatchRsp(SEncoder *pCoder, const SVDropTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
  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 已提交
4032
int32_t tDecodeSVDropTbBatchRsp(SDecoder *pCoder, SVDropTbBatchRsp *pRsp) {
H
Hongze Cheng 已提交
4033 4034 4035
  if (tStartDecode(pCoder) < 0) return -1;

  if (tDecodeI32v(pCoder, &pRsp->nRsps) < 0) return -1;
H
Hongze Cheng 已提交
4036
  pRsp->pRsps = (SVDropTbRsp *)tDecoderMalloc(pCoder, sizeof(SVDropTbRsp) * pRsp->nRsps);
H
Hongze Cheng 已提交
4037 4038 4039 4040 4041 4042 4043 4044 4045
  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 已提交
4046
int32_t tEncodeSVDropStbReq(SEncoder *pCoder, const SVDropStbReq *pReq) {
H
Hongze Cheng 已提交
4047 4048 4049 4050 4051 4052 4053 4054 4055
  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 已提交
4056
int32_t tDecodeSVDropStbReq(SDecoder *pCoder, SVDropStbReq *pReq) {
H
Hongze Cheng 已提交
4057 4058 4059 4060 4061
  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 已提交
4062 4063
  tEndDecode(pCoder);
  return 0;
4064
}
H
Hongze Cheng 已提交
4065

H
Hongze Cheng 已提交
4066
static int32_t tEncodeSVSubmitBlk(SEncoder *pCoder, const SVSubmitBlk *pBlock, int32_t flags) {
H
Hongze Cheng 已提交
4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081
  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 已提交
4082
static int32_t tDecodeSVSubmitBlk(SDecoder *pCoder, SVSubmitBlk *pBlock, int32_t flags) {
H
Hongze Cheng 已提交
4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097
  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 已提交
4098
int32_t tEncodeSVSubmitReq(SEncoder *pCoder, const SVSubmitReq *pReq) {
H
Hongze Cheng 已提交
4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112
  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 已提交
4113
int32_t tDecodeSVSubmitReq(SDecoder *pCoder, SVSubmitReq *pReq) {
H
Hongze Cheng 已提交
4114 4115 4116 4117
  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 已提交
4118
  pReq->pBlocks = tDecoderMalloc(pCoder, sizeof(SVSubmitBlk) * pReq->nBlocks);
H
Hongze Cheng 已提交
4119 4120 4121 4122 4123 4124 4125
  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 已提交
4126
}
H
Hongze Cheng 已提交
4127 4128 4129 4130

static int32_t tEncodeSSubmitBlkRsp(SEncoder *pEncoder, const SSubmitBlkRsp *pBlock) {
  if (tStartEncode(pEncoder) < 0) return -1;

H
Hongze Cheng 已提交
4131
  if (tEncodeI32(pEncoder, pBlock->code) < 0) return -1;
H
Hongze Cheng 已提交
4132
  if (tEncodeI8(pEncoder, pBlock->hashMeta) < 0) return -1;
D
dapan1121 已提交
4133 4134
  if (tEncodeI64(pEncoder, pBlock->uid) < 0) return -1;
  if (tEncodeCStr(pEncoder, pBlock->tblFName) < 0) return -1;
H
Hongze Cheng 已提交
4135 4136
  if (tEncodeI32v(pEncoder, pBlock->numOfRows) < 0) return -1;
  if (tEncodeI32v(pEncoder, pBlock->affectedRows) < 0) return -1;
H
Hongze Cheng 已提交
4137
  if (tEncodeI64v(pEncoder, pBlock->sver) < 0) return -1;
H
Hongze Cheng 已提交
4138 4139 4140 4141 4142 4143 4144 4145

  tEndEncode(pEncoder);
  return 0;
}

static int32_t tDecodeSSubmitBlkRsp(SDecoder *pDecoder, SSubmitBlkRsp *pBlock) {
  if (tStartDecode(pDecoder) < 0) return -1;

H
Hongze Cheng 已提交
4146
  if (tDecodeI32(pDecoder, &pBlock->code) < 0) return -1;
H
Hongze Cheng 已提交
4147
  if (tDecodeI8(pDecoder, &pBlock->hashMeta) < 0) return -1;
D
dapan1121 已提交
4148 4149 4150 4151
  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 已提交
4152 4153
  if (tDecodeI32v(pDecoder, &pBlock->numOfRows) < 0) return -1;
  if (tDecodeI32v(pDecoder, &pBlock->affectedRows) < 0) return -1;
H
Hongze Cheng 已提交
4154
  if (tDecodeI64v(pDecoder, &pBlock->sver) < 0) return -1;
H
Hongze Cheng 已提交
4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181

  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 已提交
4182
  pRsp->pBlocks = taosMemoryCalloc(pRsp->nBlocks, sizeof(*pRsp->pBlocks));
H
Hongze Cheng 已提交
4183 4184 4185 4186 4187
  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 已提交
4188
  tEndDecode(pDecoder);
D
dapan 已提交
4189
  tDecoderClear(pDecoder);
H
Hongze Cheng 已提交
4190 4191
  return 0;
}
D
dapan 已提交
4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206

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);
    }

    taosMemoryFree(pRsp->pBlocks);
  }

  taosMemoryFree(pRsp);
}
H
Hongze Cheng 已提交
4207 4208 4209 4210 4211 4212 4213 4214

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;
  switch (pReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
H
fix  
Hongze Cheng 已提交
4215
      if (tEncodeCStr(pEncoder, pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
4216
      if (tEncodeI8(pEncoder, pReq->type) < 0) return -1;
H
Hongze Cheng 已提交
4217
      if (tEncodeI8(pEncoder, pReq->flags) < 0) return -1;
H
Hongze Cheng 已提交
4218 4219 4220
      if (tEncodeI32v(pEncoder, pReq->bytes) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
H
fix  
Hongze Cheng 已提交
4221
      if (tEncodeCStr(pEncoder, pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
4222 4223
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
H
fix  
Hongze Cheng 已提交
4224
      if (tEncodeCStr(pEncoder, pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
4225 4226 4227
      if (tEncodeI32v(pEncoder, pReq->colModBytes) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
H
fix  
Hongze Cheng 已提交
4228
      if (tEncodeCStr(pEncoder, pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262
      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;
      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;
      }
      if (tEncodeI8(pEncoder, pReq->updateComment) < 0) return -1;
      if (pReq->updateComment) {
        if (tEncodeCStr(pEncoder, pReq->newComment) < 0) return -1;
      }
      break;
    default:
      break;
  }

  tEndEncode(pEncoder);
  return 0;
}

int32_t tDecodeSVAlterTbReq(SDecoder *pDecoder, SVAlterTbReq *pReq) {
  if (tStartDecode(pDecoder) < 0) return -1;

  if (tDecodeCStr(pDecoder, &pReq->tbName) < 0) return -1;
  if (tDecodeI8(pDecoder, &pReq->action) < 0) return -1;
  switch (pReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
H
fix  
Hongze Cheng 已提交
4263
      if (tDecodeCStr(pDecoder, &pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
4264
      if (tDecodeI8(pDecoder, &pReq->type) < 0) return -1;
H
Hongze Cheng 已提交
4265
      if (tDecodeI8(pDecoder, &pReq->flags) < 0) return -1;
H
Hongze Cheng 已提交
4266 4267 4268
      if (tDecodeI32v(pDecoder, &pReq->bytes) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
H
fix  
Hongze Cheng 已提交
4269
      if (tDecodeCStr(pDecoder, &pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
4270 4271
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
H
fix  
Hongze Cheng 已提交
4272
      if (tDecodeCStr(pDecoder, &pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
4273 4274 4275
      if (tDecodeI32v(pDecoder, &pReq->colModBytes) < 0) return -1;
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
H
fix  
Hongze Cheng 已提交
4276
      if (tDecodeCStr(pDecoder, &pReq->colName) < 0) return -1;
H
Hongze Cheng 已提交
4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316
      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;
      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;
      }
      if (tDecodeI8(pDecoder, &pReq->updateComment) < 0) return -1;
      if (pReq->updateComment) {
        if (tDecodeCStr(pDecoder, &pReq->newComment) < 0) return -1;
      }
      break;
    default:
      break;
  }

  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;
  tEndEncode(pEncoder);
  return 0;
}

int32_t tDecodeSVAlterTbRsp(SDecoder *pDecoder, SVAlterTbRsp *pRsp) {
  if (tStartDecode(pDecoder) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->code) < 0) return -1;
  tEndDecode(pDecoder);
  return 0;
}