tmsg.c 129.6 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 30
#include "tmsgdef.h"

C
Cary Xu 已提交
31
int32_t tInitSubmitMsgIter(const SSubmitReq *pMsg, SSubmitMsgIter *pIter) {
H
Hongze Cheng 已提交
32 33 34 35 36 37
  if (pMsg == NULL) {
    terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP;
    return -1;
  }

  pIter->totalLen = pMsg->length;
L
Liu Jicong 已提交
38
  ASSERT(pIter->totalLen > 0);
H
Hongze Cheng 已提交
39 40
  pIter->len = 0;
  pIter->pMsg = pMsg;
S
Shengliang Guan 已提交
41
  if (pMsg->length <= sizeof(SSubmitReq)) {
H
Hongze Cheng 已提交
42 43 44 45 46 47 48
    terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
49
int32_t tGetSubmitMsgNext(SSubmitMsgIter *pIter, SSubmitBlk **pPBlock) {
L
Liu Jicong 已提交
50 51
  ASSERT(pIter->len >= 0);

H
Hongze Cheng 已提交
52
  if (pIter->len == 0) {
S
Shengliang Guan 已提交
53
    pIter->len += sizeof(SSubmitReq);
H
Hongze Cheng 已提交
54
  } else {
55 56 57 58
    if (pIter->len >= pIter->totalLen) {
      ASSERT(0);
    }

H
Hongze Cheng 已提交
59 60
    SSubmitBlk *pSubmitBlk = (SSubmitBlk *)POINTER_SHIFT(pIter->pMsg, pIter->len);
    pIter->len += (sizeof(SSubmitBlk) + pSubmitBlk->dataLen + pSubmitBlk->schemaLen);
L
Liu Jicong 已提交
61
    ASSERT(pIter->len > 0);
H
Hongze Cheng 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
  }

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

  *pPBlock = (pIter->len == pIter->totalLen) ? NULL : (SSubmitBlk *)POINTER_SHIFT(pIter->pMsg, pIter->len);

  return 0;
}

S
Shengliang Guan 已提交
75
int32_t tInitSubmitBlkIter(SSubmitBlk *pBlock, SSubmitBlkIter *pIter) {
H
Hongze Cheng 已提交
76 77 78
  if (pBlock->dataLen <= 0) return -1;
  pIter->totalLen = pBlock->dataLen;
  pIter->len = 0;
C
Cary Xu 已提交
79
  pIter->row = (STSRow *)(pBlock->data + pBlock->schemaLen);
H
Hongze Cheng 已提交
80 81 82
  return 0;
}

C
Cary Xu 已提交
83 84
STSRow *tGetSubmitBlkNext(SSubmitBlkIter *pIter) {
  STSRow *row = pIter->row;
H
Hongze Cheng 已提交
85

H
Hongze Cheng 已提交
86 87
  if (pIter->len >= pIter->totalLen) {
    return NULL;
H
Hongze Cheng 已提交
88
  } else {
C
Cary Xu 已提交
89
    pIter->len += TD_ROW_LEN(row);
H
Hongze Cheng 已提交
90
    if (pIter->len < pIter->totalLen) {
C
Cary Xu 已提交
91
      pIter->row = POINTER_SHIFT(row, TD_ROW_LEN(row));
H
Hongze Cheng 已提交
92 93
    }
    return row;
H
Hongze Cheng 已提交
94
  }
H
Hongze Cheng 已提交
95 96
}

S
Shengliang Guan 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
int32_t tEncodeSEpSet(SCoder *pEncoder, const SEpSet *pEp) {
  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;
}

int32_t tDecodeSEpSet(SCoder *pDecoder, SEpSet *pEp) {
  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;
}

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

128
void *taosDecodeSEpSet(const void *buf, SEpSet *pEp) {
S
Shengliang Guan 已提交
129 130 131 132 133 134
  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);
  }
135
  return (void *)buf;
S
Shengliang Guan 已提交
136 137
}

S
Shengliang Guan 已提交
138 139
static int32_t tSerializeSClientHbReq(SCoder *pEncoder, const SClientHbReq *pReq) {
  if (tEncodeSClientHbKey(pEncoder, &pReq->connKey) < 0) return -1;
L
Liu Jicong 已提交
140

D
dapan1121 已提交
141
  if (pReq->connKey.connType == CONN_TYPE__QUERY) {
D
dapan1121 已提交
142 143 144 145 146 147 148
    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;
149

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

D
dapan1121 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
      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;
    }
  }
176

L
Liu Jicong 已提交
177
  int32_t kvNum = taosHashGetSize(pReq->info);
S
Shengliang Guan 已提交
178
  if (tEncodeI32(pEncoder, kvNum) < 0) return -1;
S
Shengliang Guan 已提交
179
  void *pIter = taosHashIterate(pReq->info, NULL);
L
Liu Jicong 已提交
180
  while (pIter != NULL) {
S
Shengliang Guan 已提交
181 182
    SKv *kv = pIter;
    if (tEncodeSKv(pEncoder, kv) < 0) return -1;
L
Liu Jicong 已提交
183
    pIter = taosHashIterate(pReq->info, pIter);
L
Liu Jicong 已提交
184
  }
S
Shengliang Guan 已提交
185 186

  return 0;
L
Liu Jicong 已提交
187 188
}

S
Shengliang Guan 已提交
189 190
static int32_t tDeserializeSClientHbReq(SCoder *pDecoder, SClientHbReq *pReq) {
  if (tDecodeSClientHbKey(pDecoder, &pReq->connKey) < 0) return -1;
L
Liu Jicong 已提交
191

D
dapan1121 已提交
192
  if (pReq->connKey.connType == CONN_TYPE__QUERY) {
D
dapan1121 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206
    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;
207

D
dapan1121 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        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;
224

D
dapan1121 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238
            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 已提交
239 240
  int32_t kvNum = 0;
  if (tDecodeI32(pDecoder, &kvNum) < 0) return -1;
L
Liu Jicong 已提交
241 242 243
  if (pReq->info == NULL) {
    pReq->info = taosHashInit(kvNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
  }
S
Shengliang Guan 已提交
244
  if (pReq->info == NULL) return -1;
S
Shengliang Guan 已提交
245
  for (int32_t i = 0; i < kvNum; i++) {
S
Shengliang Guan 已提交
246 247
    SKv kv = {0};
    if (tDecodeSKv(pDecoder, &kv) < 0) return -1;
D
dapan1121 已提交
248
    taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
L
Liu Jicong 已提交
249 250
  }

S
Shengliang Guan 已提交
251
  return 0;
L
Liu Jicong 已提交
252 253
}

S
Shengliang Guan 已提交
254 255 256 257
static int32_t tSerializeSClientHbRsp(SCoder *pEncoder, const SClientHbRsp *pRsp) {
  if (tEncodeSClientHbKey(pEncoder, &pRsp->connKey) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->status) < 0) return -1;

D
dapan1121 已提交
258 259 260
  int32_t queryNum = 0;
  if (pRsp->query) {
    queryNum = 1;
261
    if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
D
dapan1121 已提交
262 263 264 265 266 267 268
    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 {
269
    if (tEncodeI32(pEncoder, queryNum) < 0) return -1;
D
dapan1121 已提交
270
  }
271

D
dapan1121 已提交
272
  int32_t kvNum = taosArrayGetSize(pRsp->info);
S
Shengliang Guan 已提交
273
  if (tEncodeI32(pEncoder, kvNum) < 0) return -1;
S
Shengliang Guan 已提交
274
  for (int32_t i = 0; i < kvNum; i++) {
S
Shengliang Guan 已提交
275 276
    SKv *kv = taosArrayGet(pRsp->info, i);
    if (tEncodeSKv(pEncoder, kv) < 0) return -1;
D
dapan1121 已提交
277
  }
S
Shengliang Guan 已提交
278 279

  return 0;
L
Liu Jicong 已提交
280
}
S
Shengliang Guan 已提交
281

S
Shengliang Guan 已提交
282 283 284 285
static int32_t tDeserializeSClientHbRsp(SCoder *pDecoder, SClientHbRsp *pRsp) {
  if (tDecodeSClientHbKey(pDecoder, &pRsp->connKey) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->status) < 0) return -1;

D
dapan1121 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298
  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 已提交
299
  int32_t kvNum = 0;
S
Shengliang Guan 已提交
300
  if (tDecodeI32(pDecoder, &kvNum) < 0) return -1;
D
dapan1121 已提交
301
  pRsp->info = taosArrayInit(kvNum, sizeof(SKv));
S
Shengliang Guan 已提交
302
  if (pRsp->info == NULL) return -1;
S
Shengliang Guan 已提交
303
  for (int32_t i = 0; i < kvNum; i++) {
D
dapan1121 已提交
304
    SKv kv = {0};
S
Shengliang Guan 已提交
305
    tDecodeSKv(pDecoder, &kv);
D
dapan1121 已提交
306 307
    taosArrayPush(pRsp->info, &kv);
  }
S
Shengliang Guan 已提交
308

S
Shengliang Guan 已提交
309
  return 0;
L
Liu Jicong 已提交
310 311
}

S
Shengliang Guan 已提交
312 313 314 315 316 317 318
int32_t tSerializeSClientHbBatchReq(void *buf, int32_t bufLen, const SClientHbBatchReq *pBatchReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

L
Liu Jicong 已提交
319
  int32_t reqNum = taosArrayGetSize(pBatchReq->reqs);
S
Shengliang Guan 已提交
320
  if (tEncodeI32(&encoder, reqNum) < 0) return -1;
S
Shengliang Guan 已提交
321 322
  for (int32_t i = 0; i < reqNum; i++) {
    SClientHbReq *pReq = taosArrayGet(pBatchReq->reqs, i);
S
Shengliang Guan 已提交
323
    if (tSerializeSClientHbReq(&encoder, pReq) < 0) return -1;
L
Liu Jicong 已提交
324
  }
S
Shengliang Guan 已提交
325 326 327 328
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
L
Liu Jicong 已提交
329 330 331
  return tlen;
}

S
Shengliang Guan 已提交
332 333 334 335 336 337 338 339 340
int32_t tDeserializeSClientHbBatchReq(void *buf, int32_t bufLen, SClientHbBatchReq *pBatchReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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 已提交
341
  if (reqNum > 0) {
S
Shengliang Guan 已提交
342
    pBatchReq->reqs = taosArrayInit(reqNum, sizeof(SClientHbReq));
D
dapan1121 已提交
343
    if (NULL == pBatchReq->reqs) return -1;
L
Liu Jicong 已提交
344
  }
S
Shengliang Guan 已提交
345
  for (int32_t i = 0; i < reqNum; i++) {
L
Liu Jicong 已提交
346
    SClientHbReq req = {0};
S
Shengliang Guan 已提交
347
    tDeserializeSClientHbReq(&decoder, &req);
L
Liu Jicong 已提交
348 349
    taosArrayPush(pBatchReq->reqs, &req);
  }
S
Shengliang Guan 已提交
350 351 352 353

  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
L
Liu Jicong 已提交
354 355
}

S
Shengliang Guan 已提交
356 357 358 359 360 361 362 363 364 365 366
int32_t tSerializeSClientHbBatchRsp(void *buf, int32_t bufLen, const SClientHbBatchRsp *pBatchRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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 已提交
367
    SClientHbRsp *pRsp = taosArrayGet(pBatchRsp->rsps, i);
S
Shengliang Guan 已提交
368
    if (tSerializeSClientHbRsp(&encoder, pRsp) < 0) return -1;
L
Liu Jicong 已提交
369
  }
S
Shengliang Guan 已提交
370 371 372 373
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
L
Liu Jicong 已提交
374 375 376
  return tlen;
}

S
Shengliang Guan 已提交
377 378 379 380 381 382 383 384 385 386 387
int32_t tDeserializeSClientHbBatchRsp(void *buf, int32_t bufLen, SClientHbBatchRsp *pBatchRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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 已提交
388
    pBatchRsp->rsps = taosArrayInit(rspNum, sizeof(SClientHbRsp));
S
Shengliang Guan 已提交
389 390
  }
  for (int32_t i = 0; i < rspNum; i++) {
L
Liu Jicong 已提交
391
    SClientHbRsp rsp = {0};
S
Shengliang Guan 已提交
392
    tDeserializeSClientHbRsp(&decoder, &rsp);
L
Liu Jicong 已提交
393 394
    taosArrayPush(pBatchRsp->rsps, &rsp);
  }
S
Shengliang Guan 已提交
395 396 397 398

  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
L
Liu Jicong 已提交
399 400
}

S
Shengliang Guan 已提交
401 402
int32_t tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) {
  int32_t tlen = 0;
H
Hongze Cheng 已提交
403 404 405 406

  tlen += taosEncodeString(buf, pReq->name);
  tlen += taosEncodeFixedU32(buf, pReq->ttl);
  tlen += taosEncodeFixedU32(buf, pReq->keep);
C
Cary Xu 已提交
407
  tlen += taosEncodeFixedU8(buf, pReq->info);
H
Hongze Cheng 已提交
408 409 410

  switch (pReq->type) {
    case TD_SUPER_TABLE:
L
Liu Jicong 已提交
411
      tlen += taosEncodeFixedI64(buf, pReq->stbCfg.suid);
C
Cary Xu 已提交
412 413 414
      tlen += taosEncodeFixedI16(buf, pReq->stbCfg.nCols);
      tlen += taosEncodeFixedI16(buf, pReq->stbCfg.nBSmaCols);
      for (col_id_t i = 0; i < pReq->stbCfg.nCols; ++i) {
H
Hongze Cheng 已提交
415
        tlen += taosEncodeFixedI8(buf, pReq->stbCfg.pSchema[i].type);
H
Hongze Cheng 已提交
416
        tlen += taosEncodeFixedI8(buf, pReq->stbCfg.pSchema[i].flags);
417
        tlen += taosEncodeFixedI16(buf, pReq->stbCfg.pSchema[i].colId);
H
Hongze Cheng 已提交
418 419 420
        tlen += taosEncodeFixedI32(buf, pReq->stbCfg.pSchema[i].bytes);
        tlen += taosEncodeString(buf, pReq->stbCfg.pSchema[i].name);
      }
C
Cary Xu 已提交
421 422
      tlen += taosEncodeFixedI16(buf, pReq->stbCfg.nTagCols);
      for (col_id_t i = 0; i < pReq->stbCfg.nTagCols; ++i) {
H
Hongze Cheng 已提交
423
        tlen += taosEncodeFixedI8(buf, pReq->stbCfg.pTagSchema[i].type);
H
Hongze Cheng 已提交
424
        tlen += taosEncodeFixedI8(buf, pReq->stbCfg.pTagSchema[i].flags);
425
        tlen += taosEncodeFixedI16(buf, pReq->stbCfg.pTagSchema[i].colId);
H
Hongze Cheng 已提交
426 427 428
        tlen += taosEncodeFixedI32(buf, pReq->stbCfg.pTagSchema[i].bytes);
        tlen += taosEncodeString(buf, pReq->stbCfg.pTagSchema[i].name);
      }
L
Liu Jicong 已提交
429
      if (pReq->rollup && pReq->stbCfg.pRSmaParam) {
C
Cary Xu 已提交
430
        SRSmaParam *param = pReq->stbCfg.pRSmaParam;
C
Cary Xu 已提交
431 432
        tlen += taosEncodeBinary(buf, (const void *)&param->xFilesFactor, sizeof(param->xFilesFactor));
        tlen += taosEncodeFixedI32(buf, param->delay);
C
Cary Xu 已提交
433
        tlen += taosEncodeFixedI8(buf, param->nFuncIds);
L
Liu Jicong 已提交
434
        for (int8_t i = 0; i < param->nFuncIds; ++i) {
C
Cary Xu 已提交
435 436
          tlen += taosEncodeFixedI32(buf, param->pFuncIds[i]);
        }
C
Cary Xu 已提交
437 438 439 440 441 442 443 444 445
        tlen += taosEncodeFixedI32(buf, param->qmsg1Len);
        if (param->qmsg1Len > 0) {
          tlen += taosEncodeString(buf, param->qmsg1);
        }

        tlen += taosEncodeFixedI32(buf, param->qmsg2Len);
        if (param->qmsg2Len > 0) {
          tlen += taosEncodeString(buf, param->qmsg2);
        }
C
Cary Xu 已提交
446
      }
H
Hongze Cheng 已提交
447 448
      break;
    case TD_CHILD_TABLE:
L
Liu Jicong 已提交
449
      tlen += taosEncodeFixedI64(buf, pReq->ctbCfg.suid);
H
Hongze Cheng 已提交
450 451 452
      tlen += tdEncodeKVRow(buf, pReq->ctbCfg.pTag);
      break;
    case TD_NORMAL_TABLE:
C
Cary Xu 已提交
453 454 455
      tlen += taosEncodeFixedI16(buf, pReq->ntbCfg.nCols);
      tlen += taosEncodeFixedI16(buf, pReq->ntbCfg.nBSmaCols);
      for (col_id_t i = 0; i < pReq->ntbCfg.nCols; ++i) {
H
Hongze Cheng 已提交
456
        tlen += taosEncodeFixedI8(buf, pReq->ntbCfg.pSchema[i].type);
H
Hongze Cheng 已提交
457
        tlen += taosEncodeFixedI8(buf, pReq->ntbCfg.pSchema[i].flags);
458
        tlen += taosEncodeFixedI16(buf, pReq->ntbCfg.pSchema[i].colId);
H
Hongze Cheng 已提交
459 460 461
        tlen += taosEncodeFixedI32(buf, pReq->ntbCfg.pSchema[i].bytes);
        tlen += taosEncodeString(buf, pReq->ntbCfg.pSchema[i].name);
      }
L
Liu Jicong 已提交
462
      if (pReq->rollup && pReq->ntbCfg.pRSmaParam) {
C
Cary Xu 已提交
463
        SRSmaParam *param = pReq->ntbCfg.pRSmaParam;
C
Cary Xu 已提交
464 465
        tlen += taosEncodeBinary(buf, (const void *)&param->xFilesFactor, sizeof(param->xFilesFactor));
        tlen += taosEncodeFixedI32(buf, param->delay);
C
Cary Xu 已提交
466
        tlen += taosEncodeFixedI8(buf, param->nFuncIds);
L
Liu Jicong 已提交
467
        for (int8_t i = 0; i < param->nFuncIds; ++i) {
C
Cary Xu 已提交
468 469 470
          tlen += taosEncodeFixedI32(buf, param->pFuncIds[i]);
        }
      }
H
Hongze Cheng 已提交
471 472 473 474 475 476 477 478 479 480 481 482
      break;
    default:
      ASSERT(0);
  }

  return tlen;
}

void *tDeserializeSVCreateTbReq(void *buf, SVCreateTbReq *pReq) {
  buf = taosDecodeString(buf, &(pReq->name));
  buf = taosDecodeFixedU32(buf, &(pReq->ttl));
  buf = taosDecodeFixedU32(buf, &(pReq->keep));
C
Cary Xu 已提交
483
  buf = taosDecodeFixedU8(buf, &(pReq->info));
H
Hongze Cheng 已提交
484 485 486

  switch (pReq->type) {
    case TD_SUPER_TABLE:
L
Liu Jicong 已提交
487
      buf = taosDecodeFixedI64(buf, &(pReq->stbCfg.suid));
C
Cary Xu 已提交
488 489
      buf = taosDecodeFixedI16(buf, &(pReq->stbCfg.nCols));
      buf = taosDecodeFixedI16(buf, &(pReq->stbCfg.nBSmaCols));
H
Hongze Cheng 已提交
490
      pReq->stbCfg.pSchema = (SSchema *)taosMemoryMalloc(pReq->stbCfg.nCols * sizeof(SSchema));
491
      for (col_id_t i = 0; i < pReq->stbCfg.nCols; ++i) {
H
Hongze Cheng 已提交
492
        buf = taosDecodeFixedI8(buf, &(pReq->stbCfg.pSchema[i].type));
H
Hongze Cheng 已提交
493
        buf = taosDecodeFixedI8(buf, &(pReq->stbCfg.pSchema[i].flags));
494
        buf = taosDecodeFixedI16(buf, &(pReq->stbCfg.pSchema[i].colId));
H
Hongze Cheng 已提交
495 496 497
        buf = taosDecodeFixedI32(buf, &(pReq->stbCfg.pSchema[i].bytes));
        buf = taosDecodeStringTo(buf, pReq->stbCfg.pSchema[i].name);
      }
C
Cary Xu 已提交
498
      buf = taosDecodeFixedI16(buf, &pReq->stbCfg.nTagCols);
wafwerar's avatar
wafwerar 已提交
499
      pReq->stbCfg.pTagSchema = (SSchema *)taosMemoryMalloc(pReq->stbCfg.nTagCols * sizeof(SSchema));
500
      for (col_id_t i = 0; i < pReq->stbCfg.nTagCols; ++i) {
H
Hongze Cheng 已提交
501
        buf = taosDecodeFixedI8(buf, &(pReq->stbCfg.pTagSchema[i].type));
H
Hongze Cheng 已提交
502
        buf = taosDecodeFixedI8(buf, &(pReq->stbCfg.pTagSchema[i].flags));
503
        buf = taosDecodeFixedI16(buf, &pReq->stbCfg.pTagSchema[i].colId);
H
Hongze Cheng 已提交
504 505 506
        buf = taosDecodeFixedI32(buf, &pReq->stbCfg.pTagSchema[i].bytes);
        buf = taosDecodeStringTo(buf, pReq->stbCfg.pTagSchema[i].name);
      }
L
Liu Jicong 已提交
507
      if (pReq->rollup) {
C
Cary Xu 已提交
508
        pReq->stbCfg.pRSmaParam = (SRSmaParam *)taosMemoryCalloc(1, sizeof(SRSmaParam));
C
Cary Xu 已提交
509
        SRSmaParam *param = pReq->stbCfg.pRSmaParam;
dengyihao's avatar
dengyihao 已提交
510
        buf = taosDecodeBinaryTo(buf, (void *)&param->xFilesFactor, sizeof(param->xFilesFactor));
C
Cary Xu 已提交
511
        buf = taosDecodeFixedI32(buf, &param->delay);
C
Cary Xu 已提交
512
        buf = taosDecodeFixedI8(buf, &param->nFuncIds);
L
Liu Jicong 已提交
513
        if (param->nFuncIds > 0) {
C
Cary Xu 已提交
514
          param->pFuncIds = (func_id_t *)taosMemoryCalloc(param->nFuncIds, sizeof(func_id_t));
L
Liu Jicong 已提交
515
          for (int8_t i = 0; i < param->nFuncIds; ++i) {
C
Cary Xu 已提交
516 517 518
            buf = taosDecodeFixedI32(buf, param->pFuncIds + i);
          }
        }
C
Cary Xu 已提交
519 520 521 522 523 524 525 526
        buf = taosDecodeFixedI32(buf, &param->qmsg1Len);
        if (param->qmsg1Len > 0) {
          buf = taosDecodeString(buf, &param->qmsg1);
        }

        buf = taosDecodeFixedI32(buf, &param->qmsg2Len);
        if (param->qmsg2Len > 0) {
          buf = taosDecodeString(buf, &param->qmsg2);
C
Cary Xu 已提交
527 528 529 530
        }
      } else {
        pReq->stbCfg.pRSmaParam = NULL;
      }
H
Hongze Cheng 已提交
531 532
      break;
    case TD_CHILD_TABLE:
L
Liu Jicong 已提交
533
      buf = taosDecodeFixedI64(buf, &pReq->ctbCfg.suid);
H
Hongze Cheng 已提交
534 535 536
      buf = tdDecodeKVRow(buf, &pReq->ctbCfg.pTag);
      break;
    case TD_NORMAL_TABLE:
C
Cary Xu 已提交
537 538
      buf = taosDecodeFixedI16(buf, &pReq->ntbCfg.nCols);
      buf = taosDecodeFixedI16(buf, &(pReq->ntbCfg.nBSmaCols));
H
Hongze Cheng 已提交
539
      pReq->ntbCfg.pSchema = (SSchema *)taosMemoryMalloc(pReq->ntbCfg.nCols * sizeof(SSchema));
540
      for (col_id_t i = 0; i < pReq->ntbCfg.nCols; ++i) {
H
Hongze Cheng 已提交
541
        buf = taosDecodeFixedI8(buf, &pReq->ntbCfg.pSchema[i].type);
H
Hongze Cheng 已提交
542
        buf = taosDecodeFixedI8(buf, &pReq->ntbCfg.pSchema[i].flags);
543
        buf = taosDecodeFixedI16(buf, &pReq->ntbCfg.pSchema[i].colId);
H
Hongze Cheng 已提交
544 545 546
        buf = taosDecodeFixedI32(buf, &pReq->ntbCfg.pSchema[i].bytes);
        buf = taosDecodeStringTo(buf, pReq->ntbCfg.pSchema[i].name);
      }
L
Liu Jicong 已提交
547
      if (pReq->rollup) {
wafwerar's avatar
wafwerar 已提交
548
        pReq->ntbCfg.pRSmaParam = (SRSmaParam *)taosMemoryMalloc(sizeof(SRSmaParam));
C
Cary Xu 已提交
549
        SRSmaParam *param = pReq->ntbCfg.pRSmaParam;
dengyihao's avatar
dengyihao 已提交
550
        buf = taosDecodeBinaryTo(buf, (void *)&param->xFilesFactor, sizeof(param->xFilesFactor));
C
Cary Xu 已提交
551
        buf = taosDecodeFixedI32(buf, &param->delay);
C
Cary Xu 已提交
552
        buf = taosDecodeFixedI8(buf, &param->nFuncIds);
L
Liu Jicong 已提交
553
        if (param->nFuncIds > 0) {
C
Cary Xu 已提交
554
          param->pFuncIds = (func_id_t *)taosMemoryMalloc(param->nFuncIds * sizeof(func_id_t));
L
Liu Jicong 已提交
555
          for (int8_t i = 0; i < param->nFuncIds; ++i) {
C
Cary Xu 已提交
556 557 558 559 560 561
            buf = taosDecodeFixedI32(buf, param->pFuncIds + i);
          }
        } else {
          param->pFuncIds = NULL;
        }
      } else {
C
Cary Xu 已提交
562
        pReq->ntbCfg.pRSmaParam = NULL;
C
Cary Xu 已提交
563
      }
H
Hongze Cheng 已提交
564 565 566 567 568 569
      break;
    default:
      ASSERT(0);
  }

  return buf;
H
Hongze Cheng 已提交
570 571
}

S
Shengliang Guan 已提交
572 573
int32_t tSerializeSVCreateTbBatchReq(void **buf, SVCreateTbBatchReq *pReq) {
  int32_t tlen = 0;
H
Hongze Cheng 已提交
574

L
Liu Jicong 已提交
575
  tlen += taosEncodeFixedI64(buf, pReq->ver);
H
Hongze Cheng 已提交
576 577 578 579 580 581 582 583 584
  tlen += taosEncodeFixedU32(buf, taosArrayGetSize(pReq->pArray));
  for (size_t i = 0; i < taosArrayGetSize(pReq->pArray); i++) {
    SVCreateTbReq *pCreateTbReq = taosArrayGet(pReq->pArray, i);
    tlen += tSerializeSVCreateTbReq(buf, pCreateTbReq);
  }

  return tlen;
}

S
Shengliang Guan 已提交
585
void *tDeserializeSVCreateTbBatchReq(void *buf, SVCreateTbBatchReq *pReq) {
H
Hongze Cheng 已提交
586 587
  uint32_t nsize = 0;

L
Liu Jicong 已提交
588
  buf = taosDecodeFixedI64(buf, &pReq->ver);
H
Hongze Cheng 已提交
589
  buf = taosDecodeFixedU32(buf, &nsize);
H
Hongze Cheng 已提交
590
  pReq->pArray = taosArrayInit(nsize, sizeof(SVCreateTbReq));
H
Hongze Cheng 已提交
591
  for (size_t i = 0; i < nsize; i++) {
X
Xiaoyu Wang 已提交
592
    SVCreateTbReq req = {0};
H
Hongze Cheng 已提交
593 594 595 596 597
    buf = tDeserializeSVCreateTbReq(buf, &req);
    taosArrayPush(pReq->pArray, &req);
  }

  return buf;
L
Liu Jicong 已提交
598
}
S
Shengliang Guan 已提交
599 600

int32_t tSerializeSVDropTbReq(void **buf, SVDropTbReq *pReq) {
S
Shengliang Guan 已提交
601
  int32_t tlen = 0;
L
Liu Jicong 已提交
602
  tlen += taosEncodeFixedI64(buf, pReq->ver);
S
Shengliang Guan 已提交
603 604 605 606 607 608
  tlen += taosEncodeString(buf, pReq->name);
  tlen += taosEncodeFixedU8(buf, pReq->type);
  return tlen;
}

void *tDeserializeSVDropTbReq(void *buf, SVDropTbReq *pReq) {
L
Liu Jicong 已提交
609
  buf = taosDecodeFixedI64(buf, &pReq->ver);
S
Shengliang Guan 已提交
610 611 612 613
  buf = taosDecodeString(buf, &pReq->name);
  buf = taosDecodeFixedU8(buf, &pReq->type);
  return buf;
}
S
Shengliang Guan 已提交
614

S
Shengliang Guan 已提交
615 616 617
int32_t tSerializeSMCreateStbReq(void *buf, int32_t bufLen, SMCreateStbReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
S
Shengliang Guan 已提交
618

S
Shengliang Guan 已提交
619 620 621
  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 已提交
622 623 624
  if (tEncodeFloat(&encoder, pReq->xFilesFactor) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->aggregationMethod) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->delay) < 0) return -1;
S
sma  
Shengliang Guan 已提交
625
  if (tEncodeI32(&encoder, pReq->ttl) < 0) return -1;
S
Shengliang Guan 已提交
626 627
  if (tEncodeI32(&encoder, pReq->numOfColumns) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfTags) < 0) return -1;
S
sma  
Shengliang Guan 已提交
628
  if (tEncodeI32(&encoder, pReq->numOfSmas) < 0) return -1;
S
sma  
Shengliang Guan 已提交
629
  if (tEncodeI32(&encoder, pReq->commentLen) < 0) return -1;
630 631
  if (tEncodeI32(&encoder, pReq->ast1Len) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->ast2Len) < 0) return -1;
S
Shengliang Guan 已提交
632 633 634

  for (int32_t i = 0; i < pReq->numOfColumns; ++i) {
    SField *pField = taosArrayGet(pReq->pColumns, i);
S
Shengliang Guan 已提交
635 636 637
    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 已提交
638 639 640 641
  }

  for (int32_t i = 0; i < pReq->numOfTags; ++i) {
    SField *pField = taosArrayGet(pReq->pTags, i);
S
Shengliang Guan 已提交
642 643 644
    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 已提交
645 646
  }

S
sma  
Shengliang Guan 已提交
647 648 649 650 651 652 653 654 655 656
  for (int32_t i = 0; i < pReq->numOfSmas; ++i) {
    SField *pField = taosArrayGet(pReq->pSmas, i);
    if (tEncodeI8(&encoder, pField->type) < 0) return -1;
    if (tEncodeI32(&encoder, pField->bytes) < 0) return -1;
    if (tEncodeCStr(&encoder, pField->name) < 0) return -1;
  }

  if (pReq->commentLen > 0) {
    if (tEncodeBinary(&encoder, pReq->comment, pReq->commentLen) < 0) return -1;
  }
657 658 659 660 661 662
  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 已提交
663 664 665 666
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
667 668 669
  return tlen;
}

S
Shengliang Guan 已提交
670 671 672 673 674 675 676
int32_t tDeserializeSMCreateStbReq(void *buf, int32_t bufLen, SMCreateStbReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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 已提交
677 678 679
  if (tDecodeFloat(&decoder, &pReq->xFilesFactor) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->aggregationMethod) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->delay) < 0) return -1;
S
sma  
Shengliang Guan 已提交
680
  if (tDecodeI32(&decoder, &pReq->ttl) < 0) return -1;
S
Shengliang Guan 已提交
681 682
  if (tDecodeI32(&decoder, &pReq->numOfColumns) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfTags) < 0) return -1;
S
sma  
Shengliang Guan 已提交
683
  if (tDecodeI32(&decoder, &pReq->numOfSmas) < 0) return -1;
S
sma  
Shengliang Guan 已提交
684
  if (tDecodeI32(&decoder, &pReq->commentLen) < 0) return -1;
685 686
  if (tDecodeI32(&decoder, &pReq->ast1Len) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->ast2Len) < 0) return -1;
S
Shengliang Guan 已提交
687 688 689

  pReq->pColumns = taosArrayInit(pReq->numOfColumns, sizeof(SField));
  pReq->pTags = taosArrayInit(pReq->numOfTags, sizeof(SField));
S
sma  
Shengliang Guan 已提交
690 691
  pReq->pSmas = taosArrayInit(pReq->numOfSmas, sizeof(SField));
  if (pReq->pColumns == NULL || pReq->pTags == NULL || pReq->pSmas == NULL) {
S
Shengliang Guan 已提交
692
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
693
    return -1;
S
Shengliang Guan 已提交
694 695 696 697
  }

  for (int32_t i = 0; i < pReq->numOfColumns; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
698 699 700
    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 已提交
701 702
    if (taosArrayPush(pReq->pColumns, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
703
      return -1;
S
Shengliang Guan 已提交
704 705 706 707 708
    }
  }

  for (int32_t i = 0; i < pReq->numOfTags; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
709 710 711
    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 已提交
712 713
    if (taosArrayPush(pReq->pTags, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
714
      return -1;
S
Shengliang Guan 已提交
715 716 717
    }
  }

S
sma  
Shengliang Guan 已提交
718 719 720 721 722 723 724 725 726 727 728
  for (int32_t i = 0; i < pReq->numOfSmas; ++i) {
    SField field = {0};
    if (tDecodeI8(&decoder, &field.type) < 0) return -1;
    if (tDecodeI32(&decoder, &field.bytes) < 0) return -1;
    if (tDecodeCStrTo(&decoder, field.name) < 0) return -1;
    if (taosArrayPush(pReq->pSmas, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

S
sma  
Shengliang Guan 已提交
729
  if (pReq->commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
730
    pReq->comment = taosMemoryMalloc(pReq->commentLen);
S
sma  
Shengliang Guan 已提交
731 732 733 734
    if (pReq->comment == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->comment) < 0) return -1;
  }

735 736 737 738 739 740 741 742 743 744 745 746
  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 已提交
747 748 749 750
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
751
}
S
Shengliang Guan 已提交
752

S
Shengliang Guan 已提交
753 754 755
void tFreeSMCreateStbReq(SMCreateStbReq *pReq) {
  taosArrayDestroy(pReq->pColumns);
  taosArrayDestroy(pReq->pTags);
S
sma  
Shengliang Guan 已提交
756
  taosArrayDestroy(pReq->pSmas);
wafwerar's avatar
wafwerar 已提交
757
  taosMemoryFreeClear(pReq->comment);
758 759
  taosMemoryFreeClear(pReq->pAst1);
  taosMemoryFreeClear(pReq->pAst2);
S
Shengliang Guan 已提交
760 761
  pReq->pColumns = NULL;
  pReq->pTags = NULL;
S
sma  
Shengliang Guan 已提交
762
  pReq->pSmas = NULL;
S
Shengliang Guan 已提交
763 764
}

S
Shengliang Guan 已提交
765 766 767
int32_t tSerializeSMDropStbReq(void *buf, int32_t bufLen, SMDropStbReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
S
Shengliang Guan 已提交
768

S
Shengliang Guan 已提交
769 770 771 772
  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 已提交
773

S
Shengliang Guan 已提交
774 775
  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
776 777 778
  return tlen;
}

S
Shengliang Guan 已提交
779 780 781
int32_t tDeserializeSMDropStbReq(void *buf, int32_t bufLen, SMDropStbReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
S
Shengliang Guan 已提交
782

S
Shengliang Guan 已提交
783 784 785 786
  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 已提交
787

S
Shengliang Guan 已提交
788 789 790
  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
791

S
Shengliang Guan 已提交
792 793 794
int32_t tSerializeSMAlterStbReq(void *buf, int32_t bufLen, SMAltertbReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
S
Shengliang Guan 已提交
795

S
Shengliang Guan 已提交
796 797 798 799
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->alterType) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfFields) < 0) return -1;
S
Shengliang Guan 已提交
800 801
  for (int32_t i = 0; i < pReq->numOfFields; ++i) {
    SField *pField = taosArrayGet(pReq->pFields, i);
S
Shengliang Guan 已提交
802 803 804
    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 已提交
805
  }
S
Shengliang Guan 已提交
806
  tEndEncode(&encoder);
S
Shengliang Guan 已提交
807

S
Shengliang Guan 已提交
808 809
  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
810 811 812
  return tlen;
}

S
Shengliang Guan 已提交
813 814 815
int32_t tDeserializeSMAlterStbReq(void *buf, int32_t bufLen, SMAltertbReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
S
Shengliang Guan 已提交
816

S
Shengliang Guan 已提交
817 818 819 820
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->alterType) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfFields) < 0) return -1;
S
Shengliang Guan 已提交
821 822 823
  pReq->pFields = taosArrayInit(pReq->numOfFields, sizeof(SField));
  if (pReq->pFields == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
824
    return -1;
S
Shengliang Guan 已提交
825 826 827 828
  }

  for (int32_t i = 0; i < pReq->numOfFields; ++i) {
    SField field = {0};
S
Shengliang Guan 已提交
829 830 831
    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 已提交
832 833
    if (taosArrayPush(pReq->pFields, &field) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
834
      return -1;
S
Shengliang Guan 已提交
835 836 837
    }
  }

S
Shengliang Guan 已提交
838 839 840 841 842 843 844 845
  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
}

void tFreeSMAltertbReq(SMAltertbReq *pReq) {
  taosArrayDestroy(pReq->pFields);
  pReq->pFields = NULL;
S
Shengliang Guan 已提交
846
}
dengyihao's avatar
dengyihao 已提交
847 848 849
int32_t tSerializeSMEpSet(void *buf, int32_t bufLen, SMEpSet *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
dengyihao's avatar
dengyihao 已提交
850 851 852
  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeSEpSet(&encoder, &pReq->epSet) < 0) return -1;

dengyihao's avatar
dengyihao 已提交
853 854 855 856 857 858 859 860
  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
  return tlen;
}
int32_t tDeserializeSMEpSet(void *buf, int32_t bufLen, SMEpSet *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
dengyihao's avatar
dengyihao 已提交
861 862
  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeSEpSet(&decoder, &pReq->epSet) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
863 864 865 866 867

  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
868

S
Shengliang Guan 已提交
869 870 871 872 873 874 875 876 877 878 879
int32_t tSerializeSMCreateSmaReq(void *buf, int32_t bufLen, SMCreateSmaReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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 已提交
880
  if (tEncodeI32(&encoder, pReq->dstVgId) < 0) return -1;
S
Shengliang Guan 已提交
881 882 883 884
  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 已提交
885 886 887
  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 已提交
888 889 890 891 892 893
  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 已提交
894 895 896 897 898 899
  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 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
  tEndEncode(&encoder);

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

int32_t tDeserializeSMCreateSmaReq(void *buf, int32_t bufLen, SMCreateSmaReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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 已提交
918
  if (tDecodeI32(&decoder, &pReq->dstVgId) < 0) return -1;
S
Shengliang Guan 已提交
919 920 921 922
  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 已提交
923 924 925
  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 已提交
926
  if (pReq->exprLen > 0) {
wafwerar's avatar
wafwerar 已提交
927
    pReq->expr = taosMemoryMalloc(pReq->exprLen);
S
Shengliang Guan 已提交
928 929 930 931
    if (pReq->expr == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->expr) < 0) return -1;
  }
  if (pReq->tagsFilterLen > 0) {
wafwerar's avatar
wafwerar 已提交
932
    pReq->tagsFilter = taosMemoryMalloc(pReq->tagsFilterLen);
S
Shengliang Guan 已提交
933 934 935
    if (pReq->tagsFilter == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->tagsFilter) < 0) return -1;
  }
S
sma  
Shengliang Guan 已提交
936
  if (pReq->sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
937
    pReq->sql = taosMemoryMalloc(pReq->sqlLen);
S
sma  
Shengliang Guan 已提交
938 939 940 941
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }
  if (pReq->astLen > 0) {
wafwerar's avatar
wafwerar 已提交
942
    pReq->ast = taosMemoryMalloc(pReq->astLen);
S
sma  
Shengliang Guan 已提交
943 944 945
    if (pReq->ast == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
  }
S
Shengliang Guan 已提交
946 947 948 949 950 951 952

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

void tFreeSMCreateSmaReq(SMCreateSmaReq *pReq) {
wafwerar's avatar
wafwerar 已提交
953 954 955 956
  taosMemoryFreeClear(pReq->expr);
  taosMemoryFreeClear(pReq->tagsFilter);
  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->ast);
S
Shengliang Guan 已提交
957 958 959 960 961 962 963 964
}

int32_t tSerializeSMDropSmaReq(void *buf, int32_t bufLen, SMDropSmaReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

S
Shengliang Guan 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
  if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
  tEndEncode(&encoder);

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

int32_t tDeserializeSMDropSmaReq(void *buf, int32_t bufLen, SMDropSmaReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}
dengyihao's avatar
dengyihao 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
int32_t tSerializeSMCreateFullTextReq(void *buf, int32_t bufLen, SMCreateFullTextReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
  return tlen;
}
int32_t tDeserializeSMCreateFullTextReq(void *buf, int32_t bufLen, SMCreateFullTextReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
  if (tStartDecode(&decoder) < 0) return -1;

  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
}
void tFreeSMCreateFullTextReq(SMCreateFullTextReq *pReq) {
  // impl later
  return;
}
dengyihao's avatar
dengyihao 已提交
1010 1011 1012 1013 1014 1015
int32_t tSerializeSMDropFullTextReq(void *buf, int32_t bufLen, SMDropFullTextReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

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

dengyihao's avatar
dengyihao 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028
  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
  return tlen;
}
int32_t tDeserializeSMDropFullTextReq(void *buf, int32_t bufLen, SMDropFullTextReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
  if (tStartDecode(&decoder) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
1029 1030
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
dengyihao's avatar
dengyihao 已提交
1031 1032 1033 1034 1035

  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
1036

S
Shengliang Guan 已提交
1037 1038 1039 1040 1041
int32_t tSerializeSStatusReq(void *buf, int32_t bufLen, SStatusReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

  // status
S
Shengliang Guan 已提交
1044
  if (tEncodeI32(&encoder, pReq->sver) < 0) return -1;
S
Shengliang Guan 已提交
1045
  if (tEncodeI64(&encoder, pReq->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1046 1047 1048 1049 1050 1051 1052
  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 已提交
1053 1054

  // cluster cfg
S
Shengliang Guan 已提交
1055 1056 1057 1058 1059
  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 已提交
1060 1061 1062

  // vnode loads
  int32_t vlen = (int32_t)taosArrayGetSize(pReq->pVloads);
S
Shengliang Guan 已提交
1063
  if (tEncodeI32(&encoder, vlen) < 0) return -1;
S
Shengliang Guan 已提交
1064 1065
  for (int32_t i = 0; i < vlen; ++i) {
    SVnodeLoad *pload = taosArrayGet(pReq->pVloads, i);
S
Shengliang Guan 已提交
1066
    if (tEncodeI32(&encoder, pload->vgId) < 0) return -1;
S
Shengliang Guan 已提交
1067
    if (tEncodeI32(&encoder, pload->syncState) < 0) return -1;
S
Shengliang Guan 已提交
1068 1069 1070 1071 1072
    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 已提交
1073 1074
  }

S
Shengliang Guan 已提交
1075 1076 1077 1078
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
1079 1080 1081
  return tlen;
}

S
Shengliang Guan 已提交
1082 1083 1084 1085 1086 1087
int32_t tDeserializeSStatusReq(void *buf, int32_t bufLen, SStatusReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

S
Shengliang Guan 已提交
1088
  // status
S
Shengliang Guan 已提交
1089
  if (tDecodeI32(&decoder, &pReq->sver) < 0) return -1;
S
Shengliang Guan 已提交
1090
  if (tDecodeI64(&decoder, &pReq->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1091 1092 1093 1094 1095 1096 1097
  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 已提交
1098 1099

  // cluster cfg
S
Shengliang Guan 已提交
1100 1101 1102 1103 1104
  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 已提交
1105 1106 1107

  // vnode loads
  int32_t vlen = 0;
S
Shengliang Guan 已提交
1108
  if (tDecodeI32(&decoder, &vlen) < 0) return -1;
S
Shengliang Guan 已提交
1109 1110 1111
  pReq->pVloads = taosArrayInit(vlen, sizeof(SVnodeLoad));
  if (pReq->pVloads == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1112
    return -1;
S
Shengliang Guan 已提交
1113 1114 1115 1116
  }

  for (int32_t i = 0; i < vlen; ++i) {
    SVnodeLoad vload = {0};
S
Shengliang Guan 已提交
1117
    if (tDecodeI32(&decoder, &vload.vgId) < 0) return -1;
S
Shengliang Guan 已提交
1118
    if (tDecodeI32(&decoder, &vload.syncState) < 0) return -1;
S
Shengliang Guan 已提交
1119 1120 1121 1122 1123
    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 已提交
1124 1125
    if (taosArrayPush(pReq->pVloads, &vload) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1126
      return -1;
S
Shengliang Guan 已提交
1127 1128 1129
    }
  }

S
Shengliang Guan 已提交
1130 1131 1132
  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1133
}
S
Shengliang Guan 已提交
1134

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

S
Shengliang Guan 已提交
1137 1138 1139
int32_t tSerializeSStatusRsp(void *buf, int32_t bufLen, SStatusRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
S
Shengliang Guan 已提交
1140

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

S
Shengliang Guan 已提交
1143
  // status
S
Shengliang Guan 已提交
1144
  if (tEncodeI64(&encoder, pRsp->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1145 1146

  // dnode cfg
S
Shengliang Guan 已提交
1147 1148
  if (tEncodeI32(&encoder, pRsp->dnodeCfg.dnodeId) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->dnodeCfg.clusterId) < 0) return -1;
S
Shengliang Guan 已提交
1149 1150 1151

  // dnode eps
  int32_t dlen = (int32_t)taosArrayGetSize(pRsp->pDnodeEps);
S
Shengliang Guan 已提交
1152
  if (tEncodeI32(&encoder, dlen) < 0) return -1;
S
Shengliang Guan 已提交
1153 1154
  for (int32_t i = 0; i < dlen; ++i) {
    SDnodeEp *pDnodeEp = taosArrayGet(pRsp->pDnodeEps, i);
S
Shengliang Guan 已提交
1155 1156 1157 1158
    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 已提交
1159 1160
  }

S
Shengliang Guan 已提交
1161 1162 1163 1164
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
1165 1166 1167
  return tlen;
}

S
Shengliang Guan 已提交
1168 1169 1170 1171 1172 1173
int32_t tDeserializeSStatusRsp(void *buf, int32_t bufLen, SStatusRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

S
Shengliang Guan 已提交
1174
  // status
S
Shengliang Guan 已提交
1175
  if (tDecodeI64(&decoder, &pRsp->dnodeVer) < 0) return -1;
S
Shengliang Guan 已提交
1176 1177

  // cluster cfg
S
Shengliang Guan 已提交
1178 1179
  if (tDecodeI32(&decoder, &pRsp->dnodeCfg.dnodeId) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->dnodeCfg.clusterId) < 0) return -1;
S
Shengliang Guan 已提交
1180 1181 1182

  // dnode eps
  int32_t dlen = 0;
S
Shengliang Guan 已提交
1183
  if (tDecodeI32(&decoder, &dlen) < 0) return -1;
S
Shengliang Guan 已提交
1184 1185 1186
  pRsp->pDnodeEps = taosArrayInit(dlen, sizeof(SDnodeEp));
  if (pRsp->pDnodeEps == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1187
    return -1;
S
Shengliang Guan 已提交
1188 1189 1190 1191
  }

  for (int32_t i = 0; i < dlen; ++i) {
    SDnodeEp dnodeEp = {0};
S
Shengliang Guan 已提交
1192 1193 1194 1195
    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 已提交
1196 1197
    if (taosArrayPush(pRsp->pDnodeEps, &dnodeEp) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
1198
      return -1;
S
Shengliang Guan 已提交
1199 1200 1201
    }
  }

S
Shengliang Guan 已提交
1202 1203 1204
  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1205
}
S
Shengliang Guan 已提交
1206

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

S
Shengliang Guan 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
int32_t tSerializeSCreateAcctReq(void *buf, int32_t bufLen, SCreateAcctReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
1226 1227 1228
  return tlen;
}

S
Shengliang Guan 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
int32_t tDeserializeSCreateAcctReq(void *buf, int32_t bufLen, SCreateAcctReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1246 1247
}

S
Shengliang Guan 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
int32_t tSerializeSDropUserReq(void *buf, int32_t bufLen, SDropUserReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
1258 1259 1260
  return tlen;
}

S
Shengliang Guan 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
int32_t tDeserializeSDropUserReq(void *buf, int32_t bufLen, SDropUserReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1271 1272
}

S
Shengliang Guan 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
int32_t tSerializeSCreateUserReq(void *buf, int32_t bufLen, SCreateUserReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
1286 1287 1288
  return tlen;
}

S
Shengliang Guan 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
int32_t tDeserializeSCreateUserReq(void *buf, int32_t bufLen, SCreateUserReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1302 1303
}

S
Shengliang Guan 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
int32_t tSerializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
1318 1319 1320
  return tlen;
}

S
Shengliang Guan 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
int32_t tDeserializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1335 1336
}

S
Shengliang Guan 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
int32_t tSerializeSGetUserAuthReq(void *buf, int32_t bufLen, SGetUserAuthReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
1347 1348 1349
  return tlen;
}

S
Shengliang Guan 已提交
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
int32_t tDeserializeSGetUserAuthReq(void *buf, int32_t bufLen, SGetUserAuthReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1360 1361
}

S
Shengliang Guan 已提交
1362
int32_t tSerializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pRsp) {
S
Shengliang Guan 已提交
1363 1364 1365 1366
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
S
Shengliang Guan 已提交
1367 1368
  if (tEncodeCStr(&encoder, pRsp->user) < 0) return -1;
  if (tEncodeI8(&encoder, pRsp->superAuth) < 0) return -1;
S
Shengliang Guan 已提交
1369

S
Shengliang Guan 已提交
1370 1371
  int32_t numOfReadDbs = taosHashGetSize(pRsp->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pRsp->writeDbs);
S
Shengliang Guan 已提交
1372 1373
  if (tEncodeI32(&encoder, numOfReadDbs) < 0) return -1;
  if (tEncodeI32(&encoder, numOfWriteDbs) < 0) return -1;
S
Shengliang Guan 已提交
1374

S
Shengliang Guan 已提交
1375
  char *db = taosHashIterate(pRsp->readDbs, NULL);
S
Shengliang Guan 已提交
1376
  while (db != NULL) {
S
Shengliang Guan 已提交
1377
    if (tEncodeCStr(&encoder, db) < 0) return -1;
S
Shengliang Guan 已提交
1378
    db = taosHashIterate(pRsp->readDbs, db);
S
Shengliang Guan 已提交
1379 1380
  }

S
Shengliang Guan 已提交
1381
  db = taosHashIterate(pRsp->writeDbs, NULL);
S
Shengliang Guan 已提交
1382
  while (db != NULL) {
S
Shengliang Guan 已提交
1383
    if (tEncodeCStr(&encoder, db) < 0) return -1;
S
Shengliang Guan 已提交
1384
    db = taosHashIterate(pRsp->writeDbs, db);
S
Shengliang Guan 已提交
1385 1386
  }

S
Shengliang Guan 已提交
1387 1388 1389 1390
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
1391 1392 1393
  return tlen;
}

S
Shengliang Guan 已提交
1394 1395 1396 1397
int32_t tDeserializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pRsp) {
  pRsp->readDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false);
  pRsp->writeDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false);
  if (pRsp->readDbs == NULL || pRsp->writeDbs == NULL) {
S
Shengliang Guan 已提交
1398
    return -1;
S
Shengliang Guan 已提交
1399 1400
  }

S
Shengliang Guan 已提交
1401 1402 1403 1404
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
S
Shengliang Guan 已提交
1405 1406
  if (tDecodeCStrTo(&decoder, pRsp->user) < 0) return -1;
  if (tDecodeI8(&decoder, &pRsp->superAuth) < 0) return -1;
S
Shengliang Guan 已提交
1407

S
Shengliang Guan 已提交
1408 1409
  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
S
Shengliang Guan 已提交
1410 1411
  if (tDecodeI32(&decoder, &numOfReadDbs) < 0) return -1;
  if (tDecodeI32(&decoder, &numOfWriteDbs) < 0) return -1;
S
Shengliang Guan 已提交
1412 1413 1414

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
S
Shengliang Guan 已提交
1415
    if (tDecodeCStrTo(&decoder, db) < 0) return -1;
S
Shengliang Guan 已提交
1416
    int32_t len = strlen(db) + 1;
S
Shengliang Guan 已提交
1417
    taosHashPut(pRsp->readDbs, db, len, db, len);
S
Shengliang Guan 已提交
1418 1419 1420 1421
  }

  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
S
Shengliang Guan 已提交
1422
    if (tDecodeCStrTo(&decoder, db) < 0) return -1;
S
Shengliang Guan 已提交
1423
    int32_t len = strlen(db) + 1;
S
Shengliang Guan 已提交
1424
    taosHashPut(pRsp->writeDbs, db, len, db, len);
S
Shengliang Guan 已提交
1425 1426
  }

S
Shengliang Guan 已提交
1427 1428 1429 1430
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1431
}
S
Shengliang Guan 已提交
1432

1433
int32_t tSerializeSCreateDropMQSBNodeReq(void *buf, int32_t bufLen, SMCreateQnodeReq *pReq) {
S
Shengliang Guan 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

1446
int32_t tDeserializeSCreateDropMQSBNodeReq(void *buf, int32_t bufLen, SMCreateQnodeReq *pReq) {
S
Shengliang Guan 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
1457 1458

int32_t tSerializeSDropDnodeReq(void *buf, int32_t bufLen, SDropDnodeReq *pReq) {
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
S
Shengliang Guan 已提交
1471 1472 1473
}

int32_t tDeserializeSDropDnodeReq(void *buf, int32_t bufLen, SDropDnodeReq *pReq) {
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
}

int32_t tSerializeSMCfgDnodeReq(void *buf, int32_t bufLen, SMCfgDnodeReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMCfgDnodeReq(void *buf, int32_t bufLen, SMCfgDnodeReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSCreateDnodeReq(void *buf, int32_t bufLen, SCreateDnodeReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSCreateDnodeReq(void *buf, int32_t bufLen, SCreateDnodeReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554

int32_t tSerializeSCreateFuncReq(void *buf, int32_t bufLen, SCreateFuncReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
1555
  if (tEncodeI32(&encoder, pReq->codeLen) < 0) return -1;
S
Shengliang Guan 已提交
1556
  if (tEncodeI64(&encoder, pReq->signature) < 0) return -1;
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575

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

  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 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
  tEndEncode(&encoder);

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

int32_t tDeserializeSCreateFuncReq(void *buf, int32_t bufLen, SCreateFuncReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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;
1595
  if (tDecodeI32(&decoder, &pReq->codeLen) < 0) return -1;
S
Shengliang Guan 已提交
1596
  if (tDecodeI64(&decoder, &pReq->signature) < 0) return -1;
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619

  int32_t codeSize = 0;
  if (tDecodeI32(&decoder, &codeSize) < 0) return -1;
  if (codeSize > 0) {
    pReq->pCode = taosMemoryCalloc(1, codeSize);
    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 已提交
1620 1621 1622 1623 1624 1625
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

1626 1627 1628 1629 1630
void tFreeSCreateFuncReq(SCreateFuncReq *pReq) {
  taosMemoryFree(pReq->pCode);
  taosMemoryFree(pReq->pComment);
}

S
Shengliang Guan 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
int32_t tSerializeSDropFuncReq(void *buf, int32_t bufLen, SDropFuncReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSDropFuncReq(void *buf, int32_t bufLen, SDropFuncReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSRetrieveFuncReq(void *buf, int32_t bufLen, SRetrieveFuncReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfFuncs) < 0) return -1;
D
dapan1121 已提交
1664
  if (tEncodeI8(&encoder, pReq->ignoreCodeComment) < 0) return -1;
S
Shengliang Guan 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSRetrieveFuncReq(void *buf, int32_t bufLen, SRetrieveFuncReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfFuncs) < 0) return -1;
D
dapan1121 已提交
1685
  if (tDecodeI8(&decoder, (int8_t *)&pReq->ignoreCodeComment) < 0) return -1;
S
Shengliang Guan 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700

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

  tCoderClear(&decoder);
  return 0;
}

1701 1702
void tFreeSRetrieveFuncReq(SRetrieveFuncReq *pReq) { taosArrayDestroy(pReq->pFuncNames); }

S
Shengliang Guan 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
int32_t tSerializeSRetrieveFuncRsp(void *buf, int32_t bufLen, SRetrieveFuncRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
1722
    if (tEncodeI32(&encoder, pInfo->commentSize) < 0) return -1;
D
dapan1121 已提交
1723 1724 1725 1726 1727 1728
    if (pInfo->codeSize) {
      if (tEncodeCStr(&encoder, pInfo->pCode) < 0) return -1;
    }
    if (pInfo->commentSize) {
      if (tEncodeCStr(&encoder, pInfo->pComment) < 0) return -1;
    }
S
Shengliang Guan 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
  }

  tEndEncode(&encoder);

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

int32_t tDeserializeSRetrieveFuncRsp(void *buf, int32_t bufLen, SRetrieveFuncRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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;
1758
    if (tDecodeI32(&decoder, &fInfo.commentSize) < 0) return -1;
D
dapan1121 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
    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;
1774 1775
    }

S
Shengliang Guan 已提交
1776 1777 1778 1779 1780 1781 1782
    taosArrayPush(pRsp->pFuncInfos, &fInfo);
  }
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
1783

D
dapan1121 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792
void tFreeSFuncInfo(SFuncInfo *pInfo) {
  if (NULL == pInfo) {
    return;
  }

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

1793 1794 1795 1796
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 已提交
1797
    tFreeSFuncInfo(pInfo);
1798 1799 1800 1801
  }
  taosArrayDestroy(pRsp->pFuncInfos);
}

S
Shengliang Guan 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
int32_t tSerializeSCreateDbReq(void *buf, int32_t bufLen, SCreateDbReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->numOfVgroups) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->cacheBlockSize) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->totalBlocks) < 0) return -1;
  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->commitTime) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->fsyncPeriod) < 0) return -1;
D
dapan1121 已提交
1819
  if (tEncodeI32(&encoder, pReq->ttl) < 0) return -1;
S
Shengliang Guan 已提交
1820 1821 1822 1823
  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 已提交
1824
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
1825 1826 1827
  if (tEncodeI8(&encoder, pReq->update) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->cacheLastRow) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->ignoreExist) < 0) return -1;
X
Xiaoyu Wang 已提交
1828
  if (tEncodeI8(&encoder, pReq->streamMode) < 0) return -1;
D
dapan1121 已提交
1829
  if (tEncodeI8(&encoder, pReq->singleSTable) < 0) return -1;
S
Shengliang Guan 已提交
1830 1831 1832
  if (tEncodeI32(&encoder, pReq->numOfRetensions) < 0) return -1;
  for (int32_t i = 0; i < pReq->numOfRetensions; ++i) {
    SRetention *pRetension = taosArrayGet(pReq->pRetensions, i);
S
sma  
Shengliang Guan 已提交
1833 1834 1835 1836
    if (tEncodeI32(&encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI32(&encoder, pRetension->keep) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->keepUnit) < 0) return -1;
S
Shengliang Guan 已提交
1837
  }
S
Shengliang Guan 已提交
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
  tEndEncode(&encoder);

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

int32_t tDeserializeSCreateDbReq(void *buf, int32_t bufLen, SCreateDbReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->numOfVgroups) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->cacheBlockSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->totalBlocks) < 0) return -1;
  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->commitTime) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->fsyncPeriod) < 0) return -1;
D
dapan1121 已提交
1862
  if (tDecodeI32(&decoder, &pReq->ttl) < 0) return -1;
S
Shengliang Guan 已提交
1863 1864 1865 1866
  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 已提交
1867
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
1868 1869 1870
  if (tDecodeI8(&decoder, &pReq->update) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->cacheLastRow) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->ignoreExist) < 0) return -1;
X
Xiaoyu Wang 已提交
1871
  if (tDecodeI8(&decoder, &pReq->streamMode) < 0) return -1;
D
dapan1121 已提交
1872
  if (tDecodeI8(&decoder, &pReq->singleSTable) < 0) return -1;
S
Shengliang Guan 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881
  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};
S
sma  
Shengliang Guan 已提交
1882 1883 1884 1885
    if (tDecodeI32(&decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI32(&decoder, &rentension.keep) < 0) return -1;
    if (tDecodeI8(&decoder, &rentension.freqUnit) < 0) return -1;
    if (tDecodeI8(&decoder, &rentension.keepUnit) < 0) return -1;
S
Shengliang Guan 已提交
1886 1887 1888 1889 1890 1891
    if (taosArrayPush(pReq->pRetensions, &rentension) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

S
Shengliang Guan 已提交
1892 1893 1894 1895 1896 1897
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
1898 1899 1900 1901 1902
void tFreeSCreateDbReq(SCreateDbReq *pReq) {
  taosArrayDestroy(pReq->pRetensions);
  pReq->pRetensions = NULL;
}

S
Shengliang Guan 已提交
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
int32_t tSerializeSAlterDbReq(void *buf, int32_t bufLen, SAlterDbReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->totalBlocks) < 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->fsyncPeriod) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->walLevel) < 0) return -1;
S
Shengliang Guan 已提交
1915
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
1916
  if (tEncodeI8(&encoder, pReq->cacheLastRow) < 0) return -1;
X
Xiaoyu Wang 已提交
1917
  if (tEncodeI8(&encoder, pReq->replications) < 0) return -1;
S
Shengliang Guan 已提交
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
  tEndEncode(&encoder);

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

int32_t tDeserializeSAlterDbReq(void *buf, int32_t bufLen, SAlterDbReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->totalBlocks) < 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->fsyncPeriod) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->walLevel) < 0) return -1;
S
Shengliang Guan 已提交
1937
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
1938
  if (tDecodeI8(&decoder, &pReq->cacheLastRow) < 0) return -1;
X
Xiaoyu Wang 已提交
1939
  if (tDecodeI8(&decoder, &pReq->replications) < 0) return -1;
S
Shengliang Guan 已提交
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSDropDbReq(void *buf, int32_t bufLen, SDropDbReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSDropDbReq(void *buf, int32_t bufLen, SDropDbReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSDropDbRsp(void *buf, int32_t bufLen, SDropDbRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
1979
  if (tEncodeI64(&encoder, pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
  tEndEncode(&encoder);

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

int32_t tDeserializeSDropDbRsp(void *buf, int32_t bufLen, SDropDbRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
1993
  if (tDecodeI64(&decoder, &pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSUseDbReq(void *buf, int32_t bufLen, SUseDbReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
D
dapan1121 已提交
2006
  if (tEncodeI64(&encoder, pReq->dbId) < 0) return -1;
S
Shengliang Guan 已提交
2007
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
D
dapan1121 已提交
2008
  if (tEncodeI32(&encoder, pReq->numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
  tEndEncode(&encoder);

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

int32_t tDeserializeSUseDbReq(void *buf, int32_t bufLen, SUseDbReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
D
dapan1121 已提交
2022
  if (tDecodeI64(&decoder, &pReq->dbId) < 0) return -1;
S
Shengliang Guan 已提交
2023
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
D
dapan1121 已提交
2024
  if (tDecodeI32(&decoder, &pReq->numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
2025 2026 2027 2028 2029 2030
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

L
Liu Jicong 已提交
2031
int32_t tSerializeSQnodeListReq(void *buf, int32_t bufLen, SQnodeListReq *pReq) {
D
dapan1121 已提交
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

int32_t tDeserializeSQnodeListReq(void *buf, int32_t bufLen, SQnodeListReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  int32_t num = taosArrayGetSize(pRsp->epSetList);
L
Liu Jicong 已提交
2062
  if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
  for (int32_t i = 0; i < num; ++i) {
    SEpSet *epSet = taosArrayGet(pRsp->epSetList, i);
    if (tEncodeSEpSet(&encoder, epSet) < 0) return -1;
  }
  tEndEncode(&encoder);

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

int32_t tDeserializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  int32_t num = 0;
  if (tDecodeI32(&decoder, &num) < 0) return -1;
  pRsp->epSetList = taosArrayInit(num, sizeof(SEpSet));
  if (NULL == pRsp->epSetList) return -1;
  for (int32_t i = 0; i < num; ++i) {
    if (tDecodeSEpSet(&decoder, TARRAY_GET_ELEM(pRsp->epSetList, i)) < 0) return -1;
  }
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

void tFreeSQnodeListRsp(SQnodeListRsp *pRsp) { taosArrayDestroy(pRsp->epSetList); }

S
Shengliang Guan 已提交
2094
int32_t tSerializeSCompactDbReq(void *buf, int32_t bufLen, SCompactDbReq *pReq) {
S
Shengliang Guan 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

S
Shengliang Guan 已提交
2107
int32_t tDeserializeSCompactDbReq(void *buf, int32_t bufLen, SCompactDbReq *pReq) {
S
Shengliang Guan 已提交
2108 2109 2110 2111 2112 2113 2114 2115 2116
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
2117 2118
}

L
Liu Jicong 已提交
2119
int32_t tSerializeSUseDbRspImp(SCoder *pEncoder, const SUseDbRsp *pRsp) {
S
Shengliang Guan 已提交
2120
  if (tEncodeCStr(pEncoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
2121
  if (tEncodeI64(pEncoder, pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
2122 2123 2124 2125 2126 2127 2128 2129 2130
  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 已提交
2131
    if (tEncodeSEpSet(pEncoder, &pVgInfo->epSet) < 0) return -1;
D
dapan1121 已提交
2132
    if (tEncodeI32(pEncoder, pVgInfo->numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
2133 2134 2135 2136 2137
  }

  return 0;
}

L
Liu Jicong 已提交
2138
int32_t tSerializeSUseDbRsp(void *buf, int32_t bufLen, const SUseDbRsp *pRsp) {
S
Shengliang Guan 已提交
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

int32_t tSerializeSUseDbBatchRsp(void *buf, int32_t bufLen, SUseDbBatchRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSUseDbRspImp(SCoder *pDecoder, SUseDbRsp *pRsp) {
  if (tDecodeCStrTo(pDecoder, pRsp->db) < 0) return -1;
L
Liu Jicong 已提交
2172
  if (tDecodeI64(pDecoder, &pRsp->uid) < 0) return -1;
S
Shengliang Guan 已提交
2173 2174 2175 2176
  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 已提交
2177 2178 2179
  if (pRsp->vgNum <= 0) {
    return 0;
  }
L
Liu Jicong 已提交
2180

S
Shengliang Guan 已提交
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
  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 已提交
2192
    if (tDecodeSEpSet(pDecoder, &vgInfo.epSet) < 0) return -1;
D
dapan1121 已提交
2193
    if (tDecodeI32(pDecoder, &vgInfo.numOfTable) < 0) return -1;
S
Shengliang Guan 已提交
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
    taosArrayPush(pRsp->pVgroupInfos, &vgInfo);
  }

  return 0;
}

int32_t tDeserializeSUseDbRsp(void *buf, int32_t bufLen, SUseDbRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

int32_t tDeserializeSUseDbBatchRsp(void *buf, int32_t bufLen, SUseDbBatchRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

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

D
dapan1121 已提交
2221
  pRsp->pArray = taosArrayInit(numOfBatch, sizeof(SUseDbRsp));
S
Shengliang Guan 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
  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);

  tCoderClear(&decoder);
  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 已提交
2248 2249
}

S
Shengliang Guan 已提交
2250
int32_t tSerializeSDbCfgReq(void *buf, int32_t bufLen, SDbCfgReq *pReq) {
D
dapan1121 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

S
Shengliang Guan 已提交
2263
int32_t tDeserializeSDbCfgReq(void *buf, int32_t bufLen, SDbCfgReq *pReq) {
D
dapan1121 已提交
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
2275
int32_t tSerializeSDbCfgRsp(void *buf, int32_t bufLen, const SDbCfgRsp *pRsp) {
D
dapan1121 已提交
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->numOfVgroups) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->cacheBlockSize) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->totalBlocks) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->daysPerFile) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->daysToKeep0) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->daysToKeep1) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->daysToKeep2) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->minRows) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->maxRows) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->commitTime) < 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 已提交
2295
  if (tEncodeI8(&encoder, pRsp->strict) < 0) return -1;
D
dapan1121 已提交
2296 2297 2298
  if (tEncodeI8(&encoder, pRsp->update) < 0) return -1;
  if (tEncodeI8(&encoder, pRsp->cacheLastRow) < 0) return -1;
  if (tEncodeI8(&encoder, pRsp->streamMode) < 0) return -1;
2299 2300 2301 2302 2303 2304 2305 2306
  if (tEncodeI32(&encoder, pRsp->numOfRetensions) < 0) return -1;
  for (int32_t i = 0; i < pRsp->numOfRetensions; ++i) {
    SRetention *pRetension = taosArrayGet(pRsp->pRetensions, i);
    if (tEncodeI32(&encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI32(&encoder, pRetension->keep) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->keepUnit) < 0) return -1;
  }
D
dapan1121 已提交
2307 2308 2309 2310 2311 2312 2313
  tEndEncode(&encoder);

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

S
Shengliang Guan 已提交
2314
int32_t tDeserializeSDbCfgRsp(void *buf, int32_t bufLen, SDbCfgRsp *pRsp) {
D
dapan1121 已提交
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->numOfVgroups) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->cacheBlockSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->totalBlocks) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->daysPerFile) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->daysToKeep0) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->daysToKeep1) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->daysToKeep2) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->minRows) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->maxRows) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->commitTime) < 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 已提交
2334
  if (tDecodeI8(&decoder, &pRsp->strict) < 0) return -1;
D
dapan1121 已提交
2335 2336 2337
  if (tDecodeI8(&decoder, &pRsp->update) < 0) return -1;
  if (tDecodeI8(&decoder, &pRsp->cacheLastRow) < 0) return -1;
  if (tDecodeI8(&decoder, &pRsp->streamMode) < 0) return -1;
2338 2339 2340 2341 2342 2343
  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 已提交
2344

2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
  for (int32_t i = 0; i < pRsp->numOfRetensions; ++i) {
    SRetention rentension = {0};
    if (tDecodeI32(&decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI32(&decoder, &rentension.keep) < 0) return -1;
    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 已提交
2356 2357 2358 2359 2360 2361
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
2362
int32_t tSerializeSUserIndexReq(void *buf, int32_t bufLen, SUserIndexReq *pReq) {
D
dapan1121 已提交
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

S
Shengliang Guan 已提交
2375
int32_t tDeserializeSUserIndexReq(void *buf, int32_t bufLen, SUserIndexReq *pReq) {
D
dapan1121 已提交
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
2387
int32_t tSerializeSUserIndexRsp(void *buf, int32_t bufLen, const SUserIndexRsp *pRsp) {
D
dapan1121 已提交
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

S
Shengliang Guan 已提交
2404
int32_t tDeserializeSUserIndexRsp(void *buf, int32_t bufLen, SUserIndexRsp *pRsp) {
D
dapan1121 已提交
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
2420 2421 2422 2423 2424 2425 2426 2427 2428
int32_t tSerializeSShowReq(void *buf, int32_t bufLen, SShowReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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 已提交
2429
    if (tEncodeBinary(&encoder, pReq->payload, pReq->payloadLen) < 0) return -1;
S
Shengliang Guan 已提交
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
  }
  tEndEncode(&encoder);

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

int32_t tDeserializeSShowReq(void *buf, int32_t bufLen, SShowReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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 已提交
2447
    pReq->payload = taosMemoryMalloc(pReq->payloadLen);
S
Shengliang Guan 已提交
2448 2449 2450 2451 2452 2453 2454 2455 2456
    if (pReq->payload == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->payload) < 0) return -1;
  }

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

wafwerar's avatar
wafwerar 已提交
2457
void tFreeSShowReq(SShowReq *pReq) { taosMemoryFreeClear(pReq->payload); }
2458 2459 2460 2461 2462 2463 2464

int32_t tSerializeSRetrieveTableReq(void *buf, int32_t bufLen, SRetrieveTableReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI64(&encoder, pReq->showId) < 0) return -1;
H
Haojun Liao 已提交
2465 2466
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->tb) < 0) return -1;
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
  tEndEncode(&encoder);

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

int32_t tDeserializeSRetrieveTableReq(void *buf, int32_t bufLen, SRetrieveTableReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->showId) < 0) return -1;
H
Haojun Liao 已提交
2480 2481
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->tb) < 0) return -1;
2482 2483 2484 2485
  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
2486 2487 2488 2489 2490

static int32_t tEncodeSTableMetaRsp(SCoder *pEncoder, STableMetaRsp *pRsp) {
  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 已提交
2491
  if (tEncodeI64(pEncoder, pRsp->dbId) < 0) return -1;
S
Shengliang Guan 已提交
2492 2493 2494 2495 2496 2497 2498
  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 (tEncodeI8(pEncoder, pRsp->update) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->sversion) < 0) return -1;
  if (tEncodeI32(pEncoder, pRsp->tversion) < 0) return -1;
L
Liu Jicong 已提交
2499 2500
  if (tEncodeU64(pEncoder, pRsp->suid) < 0) return -1;
  if (tEncodeU64(pEncoder, pRsp->tuid) < 0) return -1;
S
Shengliang Guan 已提交
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
  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;
}

static int32_t tDecodeSTableMetaRsp(SCoder *pDecoder, STableMetaRsp *pRsp) {
  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 已提交
2514
  if (tDecodeI64(pDecoder, &pRsp->dbId) < 0) return -1;
S
Shengliang Guan 已提交
2515 2516 2517 2518 2519 2520 2521
  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 (tDecodeI8(pDecoder, &pRsp->update) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->sversion) < 0) return -1;
  if (tDecodeI32(pDecoder, &pRsp->tversion) < 0) return -1;
L
Liu Jicong 已提交
2522 2523
  if (tDecodeU64(pDecoder, &pRsp->suid) < 0) return -1;
  if (tDecodeU64(pDecoder, &pRsp->tuid) < 0) return -1;
S
Shengliang Guan 已提交
2524 2525 2526
  if (tDecodeI32(pDecoder, &pRsp->vgId) < 0) return -1;

  int32_t totalCols = pRsp->numOfTags + pRsp->numOfColumns;
wafwerar's avatar
wafwerar 已提交
2527
  pRsp->pSchemas = taosMemoryMalloc(sizeof(SSchema) * totalCols);
S
Shengliang Guan 已提交
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
  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) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

int32_t tSerializeSTableMetaBatchRsp(void *buf, int32_t bufLen, STableMetaBatchRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSTableMetaRsp(void *buf, int32_t bufLen, STableMetaRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

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

int32_t tDeserializeSTableMetaBatchRsp(void *buf, int32_t bufLen, STableMetaBatchRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

wafwerar's avatar
wafwerar 已提交
2608
void tFreeSTableMetaRsp(STableMetaRsp *pRsp) { taosMemoryFreeClear(pRsp->pSchemas); }
S
Shengliang Guan 已提交
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650

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) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSShowRsp(void *buf, int32_t bufLen, SShowRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tEndDecode(&decoder);
  tCoderClear(&decoder);
  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 已提交
2651 2652 2653 2654
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }
S
Shengliang Guan 已提交
2655

S
Shengliang Guan 已提交
2656 2657
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
S
Shengliang Guan 已提交
2658 2659 2660 2661 2662 2663 2664 2665

  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;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
2666 2667 2668 2669 2670 2671 2672 2673

  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 已提交
2674 2675 2676
}

int32_t tDeserializeSTableInfoReq(void *buf, int32_t bufLen, STableInfoReq *pReq) {
S
Shengliang Guan 已提交
2677 2678 2679 2680 2681
  int32_t headLen = sizeof(SMsgHead);

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

  SCoder decoder = {0};
S
Shengliang Guan 已提交
2684
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, (char *)buf + headLen, bufLen - headLen, TD_DECODER);
S
Shengliang Guan 已提交
2685 2686 2687 2688 2689 2690 2691 2692 2693

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

  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
2694

S
Shengliang Guan 已提交
2695
int32_t tSerializeSMDropTopicReq(void *buf, int32_t bufLen, SMDropTopicReq *pReq) {
S
Shengliang Guan 已提交
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSMDropTopicReq(void *buf, int32_t bufLen, SMDropTopicReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
2721

L
Liu Jicong 已提交
2722
int32_t tSerializeSCMCreateTopicReq(void *buf, int32_t bufLen, const SCMCreateTopicReq *pReq) {
S
Shengliang Guan 已提交
2723
  int32_t sqlLen = 0;
2724
  int32_t astLen = 0;
S
Shengliang Guan 已提交
2725
  if (pReq->sql != NULL) sqlLen = (int32_t)strlen(pReq->sql);
2726
  if (pReq->ast != NULL) astLen = (int32_t)strlen(pReq->ast);
S
Shengliang Guan 已提交
2727 2728 2729 2730 2731 2732 2733

  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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 已提交
2734 2735 2736 2737
  if (tEncodeI8(&encoder, pReq->withTbName) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->withSchema) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->withTag) < 0) return -1;
  if (tEncodeI8(&encoder, pReq->withTagSchema) < 0) return -1;
S
Shengliang Guan 已提交
2738
  if (tEncodeI32(&encoder, sqlLen) < 0) return -1;
2739 2740 2741
  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 已提交
2742 2743 2744 2745 2746

  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
S
Shengliang Guan 已提交
2747 2748 2749
  return tlen;
}

L
Liu Jicong 已提交
2750
int32_t tDeserializeSCMCreateTopicReq(void *buf, int32_t bufLen, SCMCreateTopicReq *pReq) {
S
Shengliang Guan 已提交
2751
  int32_t sqlLen = 0;
2752
  int32_t astLen = 0;
S
Shengliang Guan 已提交
2753 2754 2755 2756 2757 2758 2759

  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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 已提交
2760 2761 2762 2763
  if (tDecodeI8(&decoder, &pReq->withTbName) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->withSchema) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->withTag) < 0) return -1;
  if (tDecodeI8(&decoder, &pReq->withTagSchema) < 0) return -1;
S
Shengliang Guan 已提交
2764
  if (tDecodeI32(&decoder, &sqlLen) < 0) return -1;
2765
  if (tDecodeI32(&decoder, &astLen) < 0) return -1;
S
Shengliang Guan 已提交
2766

2767
  if (sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
2768
    pReq->sql = taosMemoryCalloc(1, sqlLen + 1);
2769 2770 2771 2772 2773
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }

  if (astLen > 0) {
wafwerar's avatar
wafwerar 已提交
2774
    pReq->ast = taosMemoryCalloc(1, astLen + 1);
2775 2776 2777
    if (pReq->ast == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
  }
S
Shengliang Guan 已提交
2778 2779 2780 2781 2782 2783 2784

  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

L
Liu Jicong 已提交
2785
void tFreeSCMCreateTopicReq(SCMCreateTopicReq *pReq) {
wafwerar's avatar
wafwerar 已提交
2786 2787
  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->ast);
S
Shengliang Guan 已提交
2788 2789
}

L
Liu Jicong 已提交
2790
int32_t tSerializeSCMCreateTopicRsp(void *buf, int32_t bufLen, const SCMCreateTopicRsp *pRsp) {
S
Shengliang Guan 已提交
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

L
Liu Jicong 已提交
2803
int32_t tDeserializeSCMCreateTopicRsp(void *buf, int32_t bufLen, SCMCreateTopicRsp *pRsp) {
S
Shengliang Guan 已提交
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
2814 2815 2816 2817 2818 2819

int32_t tSerializeSConnectReq(void *buf, int32_t bufLen, SConnectReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
2820
  if (tEncodeI8(&encoder, pReq->connType) < 0) return -1;
S
Shengliang Guan 已提交
2821 2822 2823
  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 已提交
2824 2825
  if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->passwd) < 0) return -1;
S
Shengliang Guan 已提交
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
  if (tEncodeI64(&encoder, pReq->startTime) < 0) return -1;
  tEndEncode(&encoder);

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

int32_t tDeserializeSConnectReq(void *buf, int32_t bufLen, SConnectReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
L
Liu Jicong 已提交
2839
  if (tDecodeI8(&decoder, &pReq->connType) < 0) return -1;
S
Shengliang Guan 已提交
2840 2841 2842
  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 已提交
2843 2844
  if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->passwd) < 0) return -1;
S
Shengliang Guan 已提交
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
  if (tDecodeI64(&decoder, &pReq->startTime) < 0) return -1;
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->acctId) < 0) return -1;
  if (tEncodeI64(&encoder, pRsp->clusterId) < 0) return -1;
D
dapan1121 已提交
2859
  if (tEncodeU32(&encoder, pRsp->connId) < 0) return -1;
S
Shengliang Guan 已提交
2860
  if (tEncodeI8(&encoder, pRsp->superUser) < 0) return -1;
L
Liu Jicong 已提交
2861
  if (tEncodeI8(&encoder, pRsp->connType) < 0) return -1;
S
Shengliang Guan 已提交
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
  if (tEncodeSEpSet(&encoder, &pRsp->epSet) < 0) return -1;
  if (tEncodeCStr(&encoder, pRsp->sVersion) < 0) return -1;
  tEndEncode(&encoder);

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

int32_t tDeserializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->acctId) < 0) return -1;
  if (tDecodeI64(&decoder, &pRsp->clusterId) < 0) return -1;
D
dapan1121 已提交
2878
  if (tDecodeU32(&decoder, &pRsp->connId) < 0) return -1;
S
Shengliang Guan 已提交
2879
  if (tDecodeI8(&decoder, &pRsp->superUser) < 0) return -1;
L
Liu Jicong 已提交
2880
  if (tDecodeI8(&decoder, &pRsp->connType) < 0) return -1;
S
Shengliang Guan 已提交
2881 2882 2883 2884 2885 2886 2887
  if (tDecodeSEpSet(&decoder, &pRsp->epSet) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pRsp->sVersion) < 0) return -1;
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912

int32_t tSerializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

int32_t tDeserializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}
S
Shengliang Guan 已提交
2913

S
Shengliang Guan 已提交
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927
int32_t tEncodeSReplica(SCoder *pEncoder, SReplica *pReplica) {
  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;
}

int32_t tDecodeSReplica(SCoder *pDecoder, SReplica *pReplica) {
  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 已提交
2928 2929 2930 2931 2932 2933 2934 2935
int32_t tSerializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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 已提交
2936
  if (tEncodeI64(&encoder, pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->cacheBlockSize) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->totalBlocks) < 0) return -1;
  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->commitTime) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->fsyncPeriod) < 0) return -1;
D
dapan1121 已提交
2948 2949 2950
  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 已提交
2951 2952 2953
  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 已提交
2954
  if (tEncodeI8(&encoder, pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
2955 2956 2957 2958
  if (tEncodeI8(&encoder, pReq->update) < 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;
L
Liu Jicong 已提交
2959
  if (tEncodeI8(&encoder, pReq->streamMode) < 0) return -1;
S
Shengliang Guan 已提交
2960 2961
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) {
    SReplica *pReplica = &pReq->replicas[i];
S
Shengliang Guan 已提交
2962
    if (tEncodeSReplica(&encoder, pReplica) < 0) return -1;
S
Shengliang Guan 已提交
2963
  }
S
sma  
Shengliang Guan 已提交
2964 2965 2966 2967 2968 2969 2970 2971
  if (tEncodeI32(&encoder, pReq->numOfRetensions) < 0) return -1;
  for (int32_t i = 0; i < pReq->numOfRetensions; ++i) {
    SRetention *pRetension = taosArrayGet(pReq->pRetensions, i);
    if (tEncodeI32(&encoder, pRetension->freq) < 0) return -1;
    if (tEncodeI32(&encoder, pRetension->keep) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->freqUnit) < 0) return -1;
    if (tEncodeI8(&encoder, pRetension->keepUnit) < 0) return -1;
  }
S
Shengliang Guan 已提交
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
  tEndEncode(&encoder);

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

int32_t tDeserializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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 已提交
2987
  if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->cacheBlockSize) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->totalBlocks) < 0) return -1;
  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->commitTime) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->fsyncPeriod) < 0) return -1;
D
dapan1121 已提交
2999 3000 3001
  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 已提交
3002 3003 3004
  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 已提交
3005
  if (tDecodeI8(&decoder, &pReq->strict) < 0) return -1;
S
Shengliang Guan 已提交
3006 3007 3008 3009
  if (tDecodeI8(&decoder, &pReq->update) < 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;
L
Liu Jicong 已提交
3010
  if (tDecodeI8(&decoder, &pReq->streamMode) < 0) return -1;
S
Shengliang Guan 已提交
3011 3012
  for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) {
    SReplica *pReplica = &pReq->replicas[i];
S
Shengliang Guan 已提交
3013
    if (tDecodeSReplica(&decoder, pReplica) < 0) return -1;
S
Shengliang Guan 已提交
3014 3015
  }

S
sma  
Shengliang Guan 已提交
3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
  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};
    if (tDecodeI32(&decoder, &rentension.freq) < 0) return -1;
    if (tDecodeI32(&decoder, &rentension.keep) < 0) return -1;
    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 已提交
3035 3036 3037 3038 3039
  tEndDecode(&decoder);
  tCoderClear(&decoder);
  return 0;
}

S
sma  
Shengliang Guan 已提交
3040 3041 3042
int32_t tFreeSCreateVnodeReq(SCreateVnodeReq *pReq) {
  taosArrayDestroy(pReq->pRetensions);
  pReq->pRetensions = NULL;
3043
  return 0;
S
sma  
Shengliang Guan 已提交
3044 3045
}

S
Shengliang Guan 已提交
3046 3047 3048 3049 3050 3051 3052
int32_t tSerializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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 已提交
3053
  if (tEncodeI64(&encoder, pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068
  if (tEncodeCStr(&encoder, pReq->db) < 0) return -1;
  tEndEncode(&encoder);

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

int32_t tDeserializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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 已提交
3069
  if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1;
S
Shengliang Guan 已提交
3070 3071 3072
  if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1;
  tEndDecode(&decoder);

S
Shengliang Guan 已提交
3073 3074 3075 3076
  tCoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
int32_t tSerializeSCompactVnodeReq(void *buf, int32_t bufLen, SCompactVnodeReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSCompactVnodeReq(void *buf, int32_t bufLen, SCompactVnodeReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSAlterVnodeReq(void *buf, int32_t bufLen, SAlterVnodeReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->totalBlocks) < 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 (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 已提交
3123

S
Shengliang Guan 已提交
3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155
  tEndEncode(&encoder);

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

int32_t tDeserializeSAlterVnodeReq(void *buf, int32_t bufLen, SAlterVnodeReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->totalBlocks) < 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 (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);
  tCoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
int32_t tSerializeSKillQueryReq(void *buf, int32_t bufLen, SKillQueryReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSKillQueryReq(void *buf, int32_t bufLen, SKillQueryReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

int32_t tDeserializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

S
Shengliang Guan 已提交
3204 3205
  tCoderClear(&decoder);
  return 0;
S
Shengliang Guan 已提交
3206 3207
}

S
Shengliang Guan 已提交
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232
int32_t tSerializeSKillTransReq(void *buf, int32_t bufLen, SKillTransReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

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

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

int32_t tDeserializeSKillTransReq(void *buf, int32_t bufLen, SKillTransReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

S
Shengliang Guan 已提交
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
int32_t tSerializeSDCreateMnodeReq(void *buf, int32_t bufLen, SDCreateMnodeReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->dnodeId) < 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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSDCreateMnodeReq(void *buf, int32_t bufLen, SDCreateMnodeReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pReq->dnodeId) < 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);

  tCoderClear(&decoder);
  return 0;
}

int32_t tSerializeSAuthReq(void *buf, int32_t bufLen, SAuthReq *pReq) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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;
  tCoderClear(&encoder);
  return tlen;
}

int32_t tDeserializeSAuthReq(void *buf, int32_t bufLen, SAuthReq *pReq) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}
L
Liu Jicong 已提交
3300

S
Shengliang Guan 已提交
3301 3302 3303 3304 3305 3306
int32_t tSerializeSServerStatusRsp(void *buf, int32_t bufLen, SServerStatusRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeI32(&encoder, pRsp->statusCode) < 0) return -1;
S
Shengliang Guan 已提交
3307
  if (tEncodeCStr(&encoder, pRsp->details) < 0) return -1;
S
Shengliang Guan 已提交
3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321

  tEndEncode(&encoder);

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

int32_t tDeserializeSServerStatusRsp(void *buf, int32_t bufLen, SServerStatusRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeI32(&decoder, &pRsp->statusCode) < 0) return -1;
S
Shengliang Guan 已提交
3322
  if (tDecodeCStrTo(&decoder, pRsp->details) < 0) return -1;
S
Shengliang Guan 已提交
3323 3324 3325 3326 3327 3328

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

L
Liu Jicong 已提交
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344
int32_t tEncodeSMqOffset(SCoder *encoder, const SMqOffset *pOffset) {
  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;
}

int32_t tDecodeSMqOffset(SCoder *decoder, SMqOffset *pOffset) {
  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;
}

L
Liu Jicong 已提交
3345
int32_t tEncodeSMqCMCommitOffsetReq(SCoder *encoder, const SMqCMCommitOffsetReq *pReq) {
L
Liu Jicong 已提交
3346 3347 3348 3349 3350 3351 3352 3353 3354
  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;
}

L
Liu Jicong 已提交
3355
int32_t tDecodeSMqCMCommitOffsetReq(SCoder *decoder, SMqCMCommitOffsetReq *pReq) {
L
Liu Jicong 已提交
3356
  if (tStartDecode(decoder) < 0) return -1;
L
Liu Jicong 已提交
3357
  if (tDecodeI32(decoder, &pReq->num) < 0) return -1;
L
Liu Jicong 已提交
3358
  TCODER_MALLOC(pReq->offsets, SMqOffset *, pReq->num * sizeof(SMqOffset), decoder);
L
Liu Jicong 已提交
3359 3360 3361 3362
  if (pReq->offsets == NULL) return -1;
  for (int32_t i = 0; i < pReq->num; i++) {
    tDecodeSMqOffset(decoder, &pReq->offsets[i]);
  }
L
Liu Jicong 已提交
3363
  tEndDecode(decoder);
L
Liu Jicong 已提交
3364 3365
  return 0;
}
D
dapan1121 已提交
3366

dengyihao's avatar
dengyihao 已提交
3367
int32_t tSerializeSExplainRsp(void *buf, int32_t bufLen, SExplainRsp *pRsp) {
D
dapan1121 已提交
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  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];
    if (tEncodeU64(&encoder, info->startupCost) < 0) return -1;
    if (tEncodeU64(&encoder, info->totalCost) < 0) return -1;
    if (tEncodeU64(&encoder, info->numOfRows) < 0) return -1;
  }

  tEndEncode(&encoder);

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

dengyihao's avatar
dengyihao 已提交
3387
int32_t tDeserializeSExplainRsp(void *buf, int32_t bufLen, SExplainRsp *pRsp) {
D
dapan1121 已提交
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  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) {
    if (tDecodeU64(&decoder, &pRsp->subplanInfo[i].startupCost) < 0) return -1;
    if (tDecodeU64(&decoder, &pRsp->subplanInfo[i].totalCost) < 0) return -1;
    if (tDecodeU64(&decoder, &pRsp->subplanInfo[i].numOfRows) < 0) return -1;
  }

  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

D
dapan1121 已提交
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419
int32_t tSerializeSSchedulerHbReq(void *buf, int32_t bufLen, SSchedulerHbReq *pReq) {
  int32_t headLen = sizeof(SMsgHead);
  if (buf != NULL) {
    buf = (char *)buf + headLen;
    bufLen -= headLen;
  }

  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
3420 3421
  if (tEncodeU64(&encoder, pReq->sId) < 0) return -1;
  if (tEncodeI32(&encoder, pReq->epId.nodeId) < 0) return -1;
D
dapan1121 已提交
3422
  if (tEncodeU16(&encoder, pReq->epId.ep.port) < 0) return -1;
L
Liu Jicong 已提交
3423
  if (tEncodeCStr(&encoder, pReq->epId.ep.fqdn) < 0) return -1;
D
dapan1121 已提交
3424 3425
  if (pReq->taskAction) {
    int32_t num = taosArrayGetSize(pReq->taskAction);
L
Liu Jicong 已提交
3426
    if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
3427 3428
    for (int32_t i = 0; i < num; ++i) {
      STaskAction *action = taosArrayGet(pReq->taskAction, i);
L
Liu Jicong 已提交
3429 3430 3431
      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 已提交
3432 3433
    }
  } else {
L
Liu Jicong 已提交
3434
    if (tEncodeI32(&encoder, 0) < 0) return -1;
D
dapan1121 已提交
3435 3436 3437 3438 3439
  }
  tEndEncode(&encoder);

  int32_t tlen = encoder.pos;
  tCoderClear(&encoder);
L
Liu Jicong 已提交
3440

D
dapan1121 已提交
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492
  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;

  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, (char *)buf + headLen, bufLen - headLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

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

int32_t tSerializeSSchedulerHbRsp(void *buf, int32_t bufLen, SSchedulerHbRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
3493
  if (tEncodeI32(&encoder, pRsp->epId.nodeId) < 0) return -1;
D
dapan1121 已提交
3494
  if (tEncodeU16(&encoder, pRsp->epId.ep.port) < 0) return -1;
L
Liu Jicong 已提交
3495
  if (tEncodeCStr(&encoder, pRsp->epId.ep.fqdn) < 0) return -1;
D
dapan1121 已提交
3496 3497
  if (pRsp->taskStatus) {
    int32_t num = taosArrayGetSize(pRsp->taskStatus);
L
Liu Jicong 已提交
3498
    if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan1121 已提交
3499 3500
    for (int32_t i = 0; i < num; ++i) {
      STaskStatus *status = taosArrayGet(pRsp->taskStatus, i);
L
Liu Jicong 已提交
3501 3502 3503 3504
      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 已提交
3505 3506
    }
  } else {
L
Liu Jicong 已提交
3507
    if (tEncodeI32(&encoder, 0) < 0) return -1;
D
dapan1121 已提交
3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547
  }
  tEndEncode(&encoder);

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

int32_t tDeserializeSSchedulerHbRsp(void *buf, int32_t bufLen, SSchedulerHbRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

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

D
dapan1121 已提交
3548 3549 3550 3551 3552
int32_t tSerializeSQueryTableRsp(void *buf, int32_t bufLen, SQueryTableRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
L
Liu Jicong 已提交
3553
  if (tEncodeI32(&encoder, pRsp->code) < 0) return -1;
D
dapan1121 已提交
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572
  tEndEncode(&encoder);

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

int32_t tDeserializeSQueryTableRsp(void *buf, int32_t bufLen, SQueryTableRsp *pRsp) {
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

D
dapan 已提交
3573 3574 3575 3576 3577 3578 3579
int32_t tSerializeSVCreateTbBatchRsp(void *buf, int32_t bufLen, SVCreateTbBatchRsp *pRsp) {
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (pRsp->rspList) {
    int32_t num = taosArrayGetSize(pRsp->rspList);
L
Liu Jicong 已提交
3580
    if (tEncodeI32(&encoder, num) < 0) return -1;
D
dapan 已提交
3581 3582
    for (int32_t i = 0; i < num; ++i) {
      SVCreateTbRsp *rsp = taosArrayGet(pRsp->rspList, i);
L
Liu Jicong 已提交
3583
      if (tEncodeI32(&encoder, rsp->code) < 0) return -1;
D
dapan 已提交
3584 3585
    }
  } else {
L
Liu Jicong 已提交
3586
    if (tEncodeI32(&encoder, 0) < 0) return -1;
D
dapan 已提交
3587 3588 3589 3590 3591 3592 3593 3594 3595
  }
  tEndEncode(&encoder);

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

int32_t tDeserializeSVCreateTbBatchRsp(void *buf, int32_t bufLen, SVCreateTbBatchRsp *pRsp) {
L
Liu Jicong 已提交
3596
  SCoder  decoder = {0};
D
dapan 已提交
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618
  int32_t num = 0;
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

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

  tCoderClear(&decoder);
  return 0;
}

C
Cary Xu 已提交
3619 3620 3621 3622 3623 3624 3625 3626
int32_t tSerializeSVCreateTSmaReq(void **buf, SVCreateTSmaReq *pReq) {
  int32_t tlen = 0;

  tlen += taosEncodeFixedI64(buf, pReq->ver);
  tlen += tEncodeTSma(buf, &pReq->tSma);

  return tlen;
}
D
dapan1121 已提交
3627

C
Cary Xu 已提交
3628 3629
void *tDeserializeSVCreateTSmaReq(void *buf, SVCreateTSmaReq *pReq) {
  buf = taosDecodeFixedI64(buf, &(pReq->ver));
D
dapan1121 已提交
3630

C
Cary Xu 已提交
3631 3632 3633 3634 3635 3636 3637 3638 3639 3640
  if ((buf = tDecodeTSma(buf, &pReq->tSma)) == NULL) {
    tdDestroyTSma(&pReq->tSma);
  }
  return buf;
}

int32_t tSerializeSVDropTSmaReq(void **buf, SVDropTSmaReq *pReq) {
  int32_t tlen = 0;

  tlen += taosEncodeFixedI64(buf, pReq->ver);
C
Cary Xu 已提交
3641
  tlen += taosEncodeFixedI64(buf, pReq->indexUid);
C
Cary Xu 已提交
3642 3643 3644 3645 3646 3647
  tlen += taosEncodeString(buf, pReq->indexName);

  return tlen;
}
void *tDeserializeSVDropTSmaReq(void *buf, SVDropTSmaReq *pReq) {
  buf = taosDecodeFixedI64(buf, &(pReq->ver));
C
Cary Xu 已提交
3648
  buf = taosDecodeFixedI64(buf, &(pReq->indexUid));
C
Cary Xu 已提交
3649 3650 3651 3652
  buf = taosDecodeStringTo(buf, pReq->indexName);

  return buf;
}
L
Liu Jicong 已提交
3653 3654

int32_t tSerializeSCMCreateStreamReq(void *buf, int32_t bufLen, const SCMCreateStreamReq *pReq) {
L
Liu Jicong 已提交
3655 3656 3657 3658 3659
  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);

L
Liu Jicong 已提交
3660 3661 3662 3663 3664
  SCoder encoder = {0};
  tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);

  if (tStartEncode(&encoder) < 0) return -1;
  if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
L
Liu Jicong 已提交
3665
  if (tEncodeCStr(&encoder, pReq->outputSTbName) < 0) return -1;
L
Liu Jicong 已提交
3666
  if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
L
Liu Jicong 已提交
3667 3668
  if (tEncodeI32(&encoder, sqlLen) < 0) return -1;
  if (tEncodeI32(&encoder, astLen) < 0) return -1;
L
Liu Jicong 已提交
3669 3670 3671
  if (sqlLen > 0 && tEncodeCStr(&encoder, pReq->sql) < 0) return -1;
  if (astLen > 0 && tEncodeCStr(&encoder, pReq->ast) < 0) return -1;

L
Liu Jicong 已提交
3672 3673 3674 3675 3676 3677 3678 3679
  tEndEncode(&encoder);

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

int32_t tDeserializeSCMCreateStreamReq(void *buf, int32_t bufLen, SCMCreateStreamReq *pReq) {
L
Liu Jicong 已提交
3680 3681 3682
  int32_t sqlLen = 0;
  int32_t astLen = 0;

L
Liu Jicong 已提交
3683 3684 3685 3686 3687
  SCoder decoder = {0};
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);

  if (tStartDecode(&decoder) < 0) return -1;
  if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
L
Liu Jicong 已提交
3688
  if (tDecodeCStrTo(&decoder, pReq->outputSTbName) < 0) return -1;
L
Liu Jicong 已提交
3689
  if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
L
Liu Jicong 已提交
3690 3691
  if (tDecodeI32(&decoder, &sqlLen) < 0) return -1;
  if (tDecodeI32(&decoder, &astLen) < 0) return -1;
X
Xiaoyu Wang 已提交
3692 3693
  if (tDecodeI8(&decoder, &pReq->triggerType) < 0) return -1;
  if (tDecodeI64(&decoder, &pReq->watermark) < 0) return -1;
L
Liu Jicong 已提交
3694 3695

  if (sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
3696
    pReq->sql = taosMemoryCalloc(1, sqlLen + 1);
L
Liu Jicong 已提交
3697 3698 3699 3700 3701
    if (pReq->sql == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->sql) < 0) return -1;
  }

  if (astLen > 0) {
wafwerar's avatar
wafwerar 已提交
3702
    pReq->ast = taosMemoryCalloc(1, astLen + 1);
L
Liu Jicong 已提交
3703 3704 3705
    if (pReq->ast == NULL) return -1;
    if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1;
  }
L
Liu Jicong 已提交
3706 3707 3708 3709 3710 3711 3712
  tEndDecode(&decoder);

  tCoderClear(&decoder);
  return 0;
}

void tFreeSCMCreateStreamReq(SCMCreateStreamReq *pReq) {
wafwerar's avatar
wafwerar 已提交
3713 3714
  taosMemoryFreeClear(pReq->sql);
  taosMemoryFreeClear(pReq->ast);
L
Liu Jicong 已提交
3715
}
C
Cary Xu 已提交
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739

STSchema *tdGetSTSChemaFromSSChema(SSchema **pSchema, int32_t nCols) {
  STSchemaBuilder schemaBuilder = {0};
  if (tdInitTSchemaBuilder(&schemaBuilder, 0) < 0) {
    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;
}