tmsg.c 8.7 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/>.
 */

H
Hongze Cheng 已提交
16
#include "tmsg.h"
D
dapan1121 已提交
17

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

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

H
Hongze Cheng 已提交
30 31 32 33 34
static int  tmsgStartEncode(SMsgEncoder *pME);
static void tmsgEndEncode(SMsgEncoder *pME);
static int  tmsgStartDecode(SMsgDecoder *pMD);
static void tmsgEndDecode(SMsgDecoder *pMD);

H
Hongze Cheng 已提交
35
/* ------------------------ ENCODE/DECODE FUNCTIONS ------------------------ */
H
Hongze Cheng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
void tmsgInitMsgEncoder(SMsgEncoder *pME, td_endian_t endian, uint8_t *data, int64_t size) {
  tInitEncoder(&(pME->coder), endian, data, size);
  TD_SLIST_INIT(&(pME->eStack));
}

void tmsgClearMsgEncoder(SMsgEncoder *pME) {
  struct SMEListNode *pNode;
  for (;;) {
    pNode = TD_SLIST_HEAD(&(pME->eStack));
    if (TD_IS_NULL(pNode)) break;
    TD_SLIST_POP(&(pME->eStack));
    free(pNode);
  }
}

H
Hongze Cheng 已提交
51 52
void tmsgInitMsgDecoder(SMsgDecoder *pMD, td_endian_t endian, uint8_t *data, int64_t size) {
  tInitDecoder(&pMD->coder, endian, data, size);
H
Hongze Cheng 已提交
53
  TD_SLIST_INIT(&(pMD->dStack));
H
Hongze Cheng 已提交
54 55 56 57
  TD_SLIST_INIT(&(pMD->freeList));
}

void tmsgClearMsgDecoder(SMsgDecoder *pMD) {
H
more  
Hongze Cheng 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  {
    struct SMDFreeListNode *pNode;
    for (;;) {
      pNode = TD_SLIST_HEAD(&(pMD->freeList));
      if (TD_IS_NULL(pNode)) break;
      TD_SLIST_POP(&(pMD->freeList));
      free(pNode);
    }
  }
  {
    struct SMDListNode *pNode;
    for (;;) {
      pNode = TD_SLIST_HEAD(&(pMD->dStack));
      if (TD_IS_NULL(pNode)) break;
      TD_SLIST_POP(&(pMD->dStack));
      free(pNode);
    }
H
Hongze Cheng 已提交
75 76 77 78 79
  }
}

/* ------------------------ MESSAGE ENCODE/DECODE ------------------------ */
int tmsgSVCreateTbReqEncode(SMsgEncoder *pCoder, SVCreateTbReq *pReq) {
H
Hongze Cheng 已提交
80
  tmsgStartEncode(pCoder);
H
Hongze Cheng 已提交
81
  // TODO
H
Hongze Cheng 已提交
82 83

  tmsgEndEncode(pCoder);
H
Hongze Cheng 已提交
84 85 86 87
  return 0;
}

int tmsgSVCreateTbReqDecode(SMsgDecoder *pCoder, SVCreateTbReq *pReq) {
H
Hongze Cheng 已提交
88 89 90 91 92 93 94 95 96 97
  tmsgStartDecode(pCoder);

  // TODO: decode

  // Decode is not end
  if (pCoder->coder.pos != pCoder->coder.size) {
    // Continue decode
  }

  tmsgEndDecode(pCoder);
H
Hongze Cheng 已提交
98 99 100
  return 0;
}

H
Hongze Cheng 已提交
101
int tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) {
H
Hongze Cheng 已提交
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  int tlen = 0;

  tlen += taosEncodeFixedU64(buf, pReq->ver);
  tlen += taosEncodeString(buf, pReq->name);
  tlen += taosEncodeFixedU32(buf, pReq->ttl);
  tlen += taosEncodeFixedU32(buf, pReq->keep);
  tlen += taosEncodeFixedU8(buf, pReq->type);

  switch (pReq->type) {
    case TD_SUPER_TABLE:
      tlen += taosEncodeFixedU64(buf, pReq->stbCfg.suid);
      tlen += taosEncodeFixedU32(buf, pReq->stbCfg.nCols);
      for (uint32_t i = 0; i < pReq->stbCfg.nCols; i++) {
        tlen += taosEncodeFixedI8(buf, pReq->stbCfg.pSchema[i].type);
        tlen += taosEncodeFixedI32(buf, pReq->stbCfg.pSchema[i].colId);
        tlen += taosEncodeFixedI32(buf, pReq->stbCfg.pSchema[i].bytes);
        tlen += taosEncodeString(buf, pReq->stbCfg.pSchema[i].name);
      }
      tlen += taosEncodeFixedU32(buf, pReq->stbCfg.nTagCols);
      for (uint32_t i = 0; i < pReq->stbCfg.nTagCols; i++) {
        tlen += taosEncodeFixedI8(buf, pReq->stbCfg.pTagSchema[i].type);
        tlen += taosEncodeFixedI32(buf, pReq->stbCfg.pTagSchema[i].colId);
        tlen += taosEncodeFixedI32(buf, pReq->stbCfg.pTagSchema[i].bytes);
        tlen += taosEncodeString(buf, pReq->stbCfg.pTagSchema[i].name);
      }
      break;
    case TD_CHILD_TABLE:
      tlen += taosEncodeFixedU64(buf, pReq->ctbCfg.suid);
      tlen += tdEncodeKVRow(buf, pReq->ctbCfg.pTag);
      break;
    case TD_NORMAL_TABLE:
      tlen += taosEncodeFixedU32(buf, pReq->ntbCfg.nCols);
      for (uint32_t i = 0; i < pReq->ntbCfg.nCols; i++) {
        tlen += taosEncodeFixedI8(buf, pReq->ntbCfg.pSchema[i].type);
        tlen += taosEncodeFixedI32(buf, pReq->ntbCfg.pSchema[i].colId);
        tlen += taosEncodeFixedI32(buf, pReq->ntbCfg.pSchema[i].bytes);
        tlen += taosEncodeString(buf, pReq->ntbCfg.pSchema[i].name);
      }
      break;
    default:
      ASSERT(0);
  }

  return tlen;
}

void *tDeserializeSVCreateTbReq(void *buf, SVCreateTbReq *pReq) {
  buf = taosDecodeFixedU64(buf, &(pReq->ver));
  buf = taosDecodeString(buf, &(pReq->name));
  buf = taosDecodeFixedU32(buf, &(pReq->ttl));
  buf = taosDecodeFixedU32(buf, &(pReq->keep));
  buf = taosDecodeFixedU8(buf, &(pReq->type));

  switch (pReq->type) {
    case TD_SUPER_TABLE:
      buf = taosDecodeFixedU64(buf, &(pReq->stbCfg.suid));
      buf = taosDecodeFixedU32(buf, &(pReq->stbCfg.nCols));
      pReq->stbCfg.pSchema = (SSchema *)malloc(pReq->stbCfg.nCols * sizeof(SSchema));
      for (uint32_t i = 0; i < pReq->stbCfg.nCols; i++) {
        buf = taosDecodeFixedI8(buf, &(pReq->stbCfg.pSchema[i].type));
        buf = taosDecodeFixedI32(buf, &(pReq->stbCfg.pSchema[i].colId));
        buf = taosDecodeFixedI32(buf, &(pReq->stbCfg.pSchema[i].bytes));
        buf = taosDecodeStringTo(buf, pReq->stbCfg.pSchema[i].name);
      }
      buf = taosDecodeFixedU32(buf, &pReq->stbCfg.nTagCols);
      pReq->stbCfg.pTagSchema = (SSchema *)malloc(pReq->stbCfg.nTagCols * sizeof(SSchema));
      for (uint32_t i = 0; i < pReq->stbCfg.nTagCols; i++) {
        buf = taosDecodeFixedI8(buf, &(pReq->stbCfg.pTagSchema[i].type));
        buf = taosDecodeFixedI32(buf, &pReq->stbCfg.pTagSchema[i].colId);
        buf = taosDecodeFixedI32(buf, &pReq->stbCfg.pTagSchema[i].bytes);
        buf = taosDecodeStringTo(buf, pReq->stbCfg.pTagSchema[i].name);
      }
      break;
    case TD_CHILD_TABLE:
      buf = taosDecodeFixedU64(buf, &pReq->ctbCfg.suid);
      buf = tdDecodeKVRow(buf, &pReq->ctbCfg.pTag);
      break;
    case TD_NORMAL_TABLE:
      buf = taosDecodeFixedU32(buf, &pReq->ntbCfg.nCols);
      pReq->ntbCfg.pSchema = (SSchema *)malloc(pReq->ntbCfg.nCols * sizeof(SSchema));
      for (uint32_t i = 0; i < pReq->ntbCfg.nCols; i++) {
        buf = taosDecodeFixedI8(buf, &pReq->ntbCfg.pSchema[i].type);
        buf = taosDecodeFixedI32(buf, &pReq->ntbCfg.pSchema[i].colId);
        buf = taosDecodeFixedI32(buf, &pReq->ntbCfg.pSchema[i].bytes);
        buf = taosDecodeStringTo(buf, pReq->ntbCfg.pSchema[i].name);
      }
      break;
    default:
      ASSERT(0);
  }

  return buf;
H
Hongze Cheng 已提交
194 195
}

H
Hongze Cheng 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
int tSVCreateTbBatchReqSerialize(void **buf, SVCreateTbBatchReq *pReq) {
  int tlen = 0;

  tlen += taosEncodeFixedU64(buf, pReq->ver);
  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;
}

void *tSVCreateTbBatchReqDeserialize(void *buf, SVCreateTbBatchReq *pReq) {
  uint32_t nsize = 0;

  buf = taosDecodeFixedU64(buf, &pReq->ver);
  buf = taosDecodeFixedU32(buf, &nsize);
  for (size_t i = 0; i < nsize; i++) {
    SVCreateTbReq req;
    buf = tDeserializeSVCreateTbReq(buf, &req);
    taosArrayPush(pReq->pArray, &req);
  }

  return buf;
}

H
Hongze Cheng 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
/* ------------------------ STATIC METHODS ------------------------ */
static int tmsgStartEncode(SMsgEncoder *pME) {
  struct SMEListNode *pNode = (struct SMEListNode *)malloc(sizeof(*pNode));
  if (TD_IS_NULL(pNode)) return -1;

  pNode->coder = pME->coder;
  TD_SLIST_PUSH(&(pME->eStack), pNode);
  TD_CODER_MOVE_POS(&(pME->coder), sizeof(int32_t));

  return 0;
}

static void tmsgEndEncode(SMsgEncoder *pME) {
  int32_t             size;
  struct SMEListNode *pNode;

  pNode = TD_SLIST_HEAD(&(pME->eStack));
  ASSERT(pNode);
  TD_SLIST_POP(&(pME->eStack));

  size = pME->coder.pos - pNode->coder.pos;
  tEncodeI32(&(pNode->coder), size);

  free(pNode);
}

static int tmsgStartDecode(SMsgDecoder *pMD) {
  struct SMDListNode *pNode;
  int32_t             size;

  pNode = (struct SMDListNode *)malloc(sizeof(*pNode));
  if (pNode == NULL) return -1;

  tDecodeI32(&(pMD->coder), &size);

  pNode->coder = pMD->coder;
  TD_SLIST_PUSH(&(pMD->dStack), pNode);

  pMD->coder.pos = 0;
  pMD->coder.size = size - sizeof(int32_t);
  pMD->coder.data = TD_CODER_CURRENT(&(pNode->coder));

  return 0;
}

static void tmsgEndDecode(SMsgDecoder *pMD) {
  ASSERT(pMD->coder.pos == pMD->coder.size);
  struct SMDListNode *pNode;

  pNode = TD_SLIST_HEAD(&(pMD->dStack));
  ASSERT(pNode);
  TD_SLIST_POP(&(pMD->dStack));

  pNode->coder.pos += pMD->coder.size;

  pMD->coder = pNode->coder;

  free(pNode);
H
Hongze Cheng 已提交
281
}