clientSmlJson.c 14.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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/>.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "clientSml.h"

wmmhello's avatar
wmmhello 已提交
22 23
#define JUMP_JSON_SPACE(start) \
while(*(start)){\
wmmhello's avatar
wmmhello 已提交
24
  if(unlikely(*(start) > 32))\
wmmhello's avatar
wmmhello 已提交
25 26
    break;\
  else\
wmmhello's avatar
wmmhello 已提交
27
    (start)++;\
wmmhello's avatar
wmmhello 已提交
28 29 30 31 32 33 34 35 36
  }

static SArray *smlJsonParseTags(char *start, char *end){
  SArray *tags = taosArrayInit(4, sizeof(SSmlKv));
  while(start < end){
    SSmlKv kv = {0};
    kv.type = TSDB_DATA_TYPE_NCHAR;
    bool isInQuote = false;
    while(start < end){
wmmhello's avatar
wmmhello 已提交
37
      if(unlikely(!isInQuote && *start == '"')){
wmmhello's avatar
wmmhello 已提交
38 39 40 41
        start++;
        kv.key = start;
        isInQuote = true;
        continue;
42
      }
wmmhello's avatar
wmmhello 已提交
43
      if(unlikely(isInQuote && *start == '"')){
wmmhello's avatar
wmmhello 已提交
44 45 46
        kv.keyLen = start - kv.key;
        start++;
        break;
47
      }
wmmhello's avatar
wmmhello 已提交
48 49 50 51
      start++;
    }
    bool hasColon = false;
    while(start < end){
wmmhello's avatar
wmmhello 已提交
52
      if(unlikely(!hasColon && *start == ':')){
wmmhello's avatar
wmmhello 已提交
53 54 55
        start++;
        hasColon = true;
        continue;
56
      }
wmmhello's avatar
wmmhello 已提交
57
      if(unlikely(hasColon && kv.value == NULL && (*start > 32 && *start != '"'))){
wmmhello's avatar
wmmhello 已提交
58 59 60
        kv.value = start;
        start++;
        continue;
61 62
      }

wmmhello's avatar
wmmhello 已提交
63
      if(unlikely(hasColon && kv.value != NULL && (*start == '"' || *start == ',' || *start == '}'))){
wmmhello's avatar
wmmhello 已提交
64 65 66 67
        kv.length = start - kv.value;
        taosArrayPush(tags, &kv);
        start++;
        break;
68
      }
wmmhello's avatar
wmmhello 已提交
69
      start++;
70 71
    }
  }
wmmhello's avatar
wmmhello 已提交
72
  return tags;
73 74
}

wmmhello's avatar
wmmhello 已提交
75
static int32_t smlParseTagsFromJSON(SSmlHandle *info, SSmlLineInfo *elements) {
76 77
  int32_t ret = TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
78
  if(is_same_child_table_telnet(elements, &info->preLine) == 0){
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    return TSDB_CODE_SUCCESS;
  }

  bool isSameMeasure = IS_SAME_SUPER_TABLE;

  int     cnt = 0;
  SArray *preLineKV = info->preLineTagKV;
  bool    isSuperKVInit = true;
  SArray *superKV = NULL;
  if(info->dataFormat){
    if(unlikely(!isSameMeasure)){
      SSmlSTableMeta *sMeta = (SSmlSTableMeta *)nodeListGet(info->superTables, elements->measure, elements->measureLen, NULL);

      if(unlikely(sMeta == NULL)){
        sMeta = smlBuildSTableMeta(info->dataFormat);
        STableMeta * pTableMeta = smlGetMeta(info, elements->measure, elements->measureLen);
        sMeta->tableMeta = pTableMeta;
        if(pTableMeta == NULL){
          info->dataFormat = false;
          info->reRun      = true;
          return TSDB_CODE_SUCCESS;
        }
        nodeListSet(&info->superTables, elements->measure, elements->measureLen, sMeta, NULL);
      }
      info->currSTableMeta = sMeta->tableMeta;
      superKV = sMeta->tags;

      if(unlikely(taosArrayGetSize(superKV) == 0)){
        isSuperKVInit = false;
      }
      taosArraySetSize(preLineKV, 0);
    }
  }else{
    taosArraySetSize(preLineKV, 0);
  }

wmmhello's avatar
wmmhello 已提交
115 116
  SArray *tags = smlJsonParseTags(elements->tags, elements->tags + elements->tagsLen);
  int32_t tagNum = taosArrayGetSize(tags);
117
  for (int32_t i = 0; i < tagNum; ++i) {
wmmhello's avatar
wmmhello 已提交
118
    SSmlKv kv = *(SSmlKv*)taosArrayGet(tags, i);
119 120 121 122 123

    if(info->dataFormat){
      if(unlikely(cnt + 1 > info->currSTableMeta->tableInfo.numOfTags)){
        info->dataFormat = false;
        info->reRun      = true;
wmmhello's avatar
wmmhello 已提交
124
        taosArrayDestroy(tags);
125 126 127 128 129 130 131
        return TSDB_CODE_SUCCESS;
      }

      if(isSameMeasure){
        if(unlikely(cnt >= taosArrayGetSize(preLineKV))) {
          info->dataFormat = false;
          info->reRun      = true;
wmmhello's avatar
wmmhello 已提交
132
          taosArrayDestroy(tags);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
          return TSDB_CODE_SUCCESS;
        }
        SSmlKv *preKV = (SSmlKv *)taosArrayGet(preLineKV, cnt);
        if(unlikely(kv.length > preKV->length)){
          preKV->length = kv.length;
          SSmlSTableMeta *tableMeta = (SSmlSTableMeta *)nodeListGet(info->superTables, elements->measure, elements->measureLen, NULL);
          ASSERT(tableMeta != NULL);

          SSmlKv *oldKV = (SSmlKv *)taosArrayGet(tableMeta->tags, cnt);
          oldKV->length = kv.length;
          info->needModifySchema = true;
        }
        if(unlikely(!IS_SAME_KEY)){
          info->dataFormat = false;
          info->reRun      = true;
wmmhello's avatar
wmmhello 已提交
148
          taosArrayDestroy(tags);
149 150 151 152 153 154 155
          return TSDB_CODE_SUCCESS;
        }
      }else{
        if(isSuperKVInit){
          if(unlikely(cnt >= taosArrayGetSize(superKV))) {
            info->dataFormat = false;
            info->reRun      = true;
wmmhello's avatar
wmmhello 已提交
156
            taosArrayDestroy(tags);
157 158 159 160 161 162 163 164 165 166 167 168 169
            return TSDB_CODE_SUCCESS;
          }
          SSmlKv *preKV = (SSmlKv *)taosArrayGet(superKV, cnt);
          if(unlikely(kv.length > preKV->length)) {
            preKV->length = kv.length;
          }else{
            kv.length = preKV->length;
          }
          info->needModifySchema = true;

          if(unlikely(!IS_SAME_KEY)){
            info->dataFormat = false;
            info->reRun      = true;
wmmhello's avatar
wmmhello 已提交
170
            taosArrayDestroy(tags);
171 172 173 174 175 176 177 178 179 180 181 182
            return TSDB_CODE_SUCCESS;
          }
        }else{
          taosArrayPush(superKV, &kv);
        }
        taosArrayPush(preLineKV, &kv);
      }
    }else{
      taosArrayPush(preLineKV, &kv);
    }
    cnt++;
  }
wmmhello's avatar
wmmhello 已提交
183
  taosArrayDestroy(tags);
184

wmmhello's avatar
wmmhello 已提交
185
  SSmlTableInfo *tinfo = (SSmlTableInfo *)nodeListGet(info->childTables, elements, POINTER_BYTES, is_same_child_table_telnet);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  if (unlikely(tinfo == NULL)) {
    tinfo = smlBuildTableInfo(1, elements->measure, elements->measureLen);
    if (unlikely(!tinfo)) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    tinfo->tags = taosArrayDup(preLineKV, NULL);

    smlSetCTableName(tinfo);
    if (info->dataFormat) {
      info->currSTableMeta->uid = tinfo->uid;
      tinfo->tableDataCtx = smlInitTableDataCtx(info->pQuery, info->currSTableMeta);
      if (tinfo->tableDataCtx == NULL) {
        smlBuildInvalidDataMsg(&info->msgBuf, "smlInitTableDataCtx error", NULL);
        return TSDB_CODE_SML_INVALID_DATA;
      }
    }

wmmhello's avatar
wmmhello 已提交
203 204 205 206
    SSmlLineInfo *key = (SSmlLineInfo *)taosMemoryMalloc(sizeof(SSmlLineInfo));
    *key = *elements;
    tinfo->key = key;
    nodeListSet(&info->childTables, key, POINTER_BYTES, tinfo, is_same_child_table_telnet);
207 208 209 210 211 212
  }
  if (info->dataFormat) info->currTableDataCtx = tinfo->tableDataCtx;

  return ret;
}

wmmhello's avatar
wmmhello 已提交
213 214 215
static char* smlJsonGetObj(char *payload){
  int   leftBracketCnt = 0;
  while(*payload) {
wmmhello's avatar
wmmhello 已提交
216
    if (unlikely(*payload == '{')) {
wmmhello's avatar
wmmhello 已提交
217 218 219 220
      leftBracketCnt++;
      payload++;
      continue;
    }
wmmhello's avatar
wmmhello 已提交
221
    if (unlikely(*payload == '}')) {
wmmhello's avatar
wmmhello 已提交
222 223 224 225 226 227
      leftBracketCnt--;
      payload++;
      if (leftBracketCnt == 0) {
        return payload;
      } else if (leftBracketCnt < 0) {
        return NULL;
228
      }
wmmhello's avatar
wmmhello 已提交
229
      continue;
230
    }
wmmhello's avatar
wmmhello 已提交
231 232 233 234 235
    payload++;
  }
  return NULL;
}

wmmhello's avatar
wmmhello 已提交
236 237
static inline void smlJsonParseObjFirst(char **start, SSmlLineInfo *element, int8_t *offset){
  int index = 0;
wmmhello's avatar
wmmhello 已提交
238
  while(*(*start)){
wmmhello's avatar
wmmhello 已提交
239 240 241 242
    if((*start)[0] != '"'){
      (*start)++;
      continue;
    }
wmmhello's avatar
wmmhello 已提交
243 244 245 246 247 248

    if(unlikely(index >= 4)) {
      uError("index >= 4, %s", *start)
      break;
    }
    char *sTmp = *start;
wmmhello's avatar
wmmhello 已提交
249
    if((*start)[1] == 'm' && (*start)[2] == 'e' && (*start)[3] == 't'
wmmhello's avatar
wmmhello 已提交
250
       && (*start)[4] == 'r' &&  (*start)[5] == 'i' && (*start)[6] == 'c' && (*start)[7] == '"'){
wmmhello's avatar
wmmhello 已提交
251

wmmhello's avatar
wmmhello 已提交
252
      (*start) += 8;
wmmhello's avatar
wmmhello 已提交
253
      bool isInQuote = false;
wmmhello's avatar
wmmhello 已提交
254 255 256
      while(*(*start)){
        if(unlikely(!isInQuote && *(*start) == '"')){
          (*start)++;
wmmhello's avatar
wmmhello 已提交
257
          offset[index++] = *start - sTmp;
wmmhello's avatar
wmmhello 已提交
258
          element->measure = (*start);
wmmhello's avatar
wmmhello 已提交
259 260 261
          isInQuote = true;
          continue;
        }
wmmhello's avatar
wmmhello 已提交
262 263
        if(unlikely(isInQuote && *(*start) == '"')){
          element->measureLen = (*start) - element->measure;
wmmhello's avatar
wmmhello 已提交
264 265
          break;
        }
wmmhello's avatar
wmmhello 已提交
266
        (*start)++;
wmmhello's avatar
wmmhello 已提交
267
      }
wmmhello's avatar
wmmhello 已提交
268
    }else if((*start)[1] == 't' && (*start)[2] == 'i' && (*start)[3] == 'm'
wmmhello's avatar
wmmhello 已提交
269 270
             && (*start)[4] == 'e' &&  (*start)[5] == 's' && (*start)[6] == 't'
             && (*start)[7] == 'a' &&  (*start)[8] == 'm' && (*start)[9] == 'p' && (*start)[10] == '"'){
wmmhello's avatar
wmmhello 已提交
271

wmmhello's avatar
wmmhello 已提交
272
      (*start) += 11;
wmmhello's avatar
wmmhello 已提交
273
      bool hasColon = false;
wmmhello's avatar
wmmhello 已提交
274 275 276 277
      while(*(*start)){
        if(unlikely(!hasColon && *(*start) == ':')){
          (*start)++;
          JUMP_JSON_SPACE((*start))
wmmhello's avatar
wmmhello 已提交
278
          offset[index++] = *start - sTmp;
wmmhello's avatar
wmmhello 已提交
279
          element->timestamp = (*start);
wmmhello's avatar
wmmhello 已提交
280 281 282
          hasColon = true;
          continue;
        }
wmmhello's avatar
wmmhello 已提交
283
        if(unlikely(hasColon && (*(*start) == ',' || *(*start) == '}' || (*(*start)) <= 32))){
wmmhello's avatar
wmmhello 已提交
284
          element->timestampLen = (*start) - element->timestamp;
wmmhello's avatar
wmmhello 已提交
285 286
          break;
        }
wmmhello's avatar
wmmhello 已提交
287
        (*start)++;
wmmhello's avatar
wmmhello 已提交
288
      }
wmmhello's avatar
wmmhello 已提交
289
    }else if((*start)[1] == 'v' && (*start)[2] == 'a' && (*start)[3] == 'l'
wmmhello's avatar
wmmhello 已提交
290
             && (*start)[4] == 'u' &&  (*start)[5] == 'e' && (*start)[6] == '"'){
wmmhello's avatar
wmmhello 已提交
291

wmmhello's avatar
wmmhello 已提交
292
      (*start) += 7;
wmmhello's avatar
wmmhello 已提交
293 294

      bool hasColon = false;
wmmhello's avatar
wmmhello 已提交
295 296 297 298
      while(*(*start)){
        if(unlikely(!hasColon && *(*start) == ':')){
          (*start)++;
          JUMP_JSON_SPACE((*start))
wmmhello's avatar
wmmhello 已提交
299
          offset[index++] = *start - sTmp;
wmmhello's avatar
wmmhello 已提交
300
          element->cols = (*start);
wmmhello's avatar
wmmhello 已提交
301 302 303
          hasColon = true;
          continue;
        }
wmmhello's avatar
wmmhello 已提交
304
        if(unlikely(hasColon && (*(*start) == ',' || *(*start) == '}' || (*(*start)) <= 32))){
wmmhello's avatar
wmmhello 已提交
305
          element->colsLen = (*start) - element->cols;
wmmhello's avatar
wmmhello 已提交
306 307
          break;
        }
wmmhello's avatar
wmmhello 已提交
308
        (*start)++;
wmmhello's avatar
wmmhello 已提交
309
      }
wmmhello's avatar
wmmhello 已提交
310
    }else if((*start)[1] == 't' && (*start)[2] == 'a' && (*start)[3] == 'g'
wmmhello's avatar
wmmhello 已提交
311 312 313 314 315 316 317
             && (*start)[4] == 's' && (*start)[5] == '"'){
      (*start) += 6;

      while(*(*start)){
        if(unlikely(*(*start) == ':')){
          (*start)++;
          JUMP_JSON_SPACE((*start))
wmmhello's avatar
wmmhello 已提交
318
          offset[index++] = *start - sTmp;
wmmhello's avatar
wmmhello 已提交
319 320 321 322 323 324
          element->tags = (*start);
          char* tmp = smlJsonGetObj((*start));
          if(tmp){
            element->tagsLen = tmp - (*start);
            *start = tmp;
          }
wmmhello's avatar
wmmhello 已提交
325 326
          break;
        }
wmmhello's avatar
wmmhello 已提交
327
        (*start)++;
wmmhello's avatar
wmmhello 已提交
328
      }
329
    }
wmmhello's avatar
wmmhello 已提交
330 331 332 333 334
    if(*(*start) == '}'){
      (*start)++;
      break;
    }
    (*start)++;
335 336 337
  }
}

wmmhello's avatar
wmmhello 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
static inline void smlJsonParseObj(char **start, SSmlLineInfo *element, int8_t *offset){
  int index = 0;
  while(*(*start)){
    if((*start)[0] != '"'){
      (*start)++;
      continue;
    }

    if(unlikely(index >= 4)) {
      uError("index >= 4, %s", *start)
      break;
    }
    if((*start)[1] == 'm'){
      (*start) += offset[index++];
      element->measure = *start;
      while(*(*start)){
        if(unlikely(*(*start) == '"')){
          element->measureLen = (*start) - element->measure;
          break;
        }
        (*start)++;
      }
    }else if((*start)[1] == 't' && (*start)[2] == 'i'){
      (*start) += offset[index++];
      element->timestamp = *start;
      while(*(*start)){
        if(unlikely(*(*start) == ',' || *(*start) == '}' || (*(*start)) <= 32)){
          element->timestampLen = (*start) - element->timestamp;
          break;
        }
        (*start)++;
      }
    }else if((*start)[1] == 'v'){
      (*start) += offset[index++];
      element->cols = *start;
      while(*(*start)){
        if(unlikely( *(*start) == ',' || *(*start) == '}' || (*(*start)) <= 32)){
          element->colsLen = (*start) - element->cols;
          break;
        }
        (*start)++;
      }
    }else if((*start)[1] == 't' && (*start)[2] == 'a'){
      (*start) += offset[index++];
      element->tags = (*start);
      char* tmp = smlJsonGetObj((*start));
      if(tmp){
        element->tagsLen = tmp - (*start);
        *start = tmp;
      }
      break;
    }
    if(*(*start) == '}'){
      (*start)++;
      break;
    }
    (*start)++;
  }
}

wmmhello's avatar
wmmhello 已提交
398
static int32_t smlParseJSONString(SSmlHandle *info, char **start, SSmlLineInfo *elements) {
399 400
  int32_t ret = TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
401 402 403 404 405
  if(info->offset[0] == 0){
    smlJsonParseObjFirst(start, elements, info->offset);
  }else{
    smlJsonParseObj(start, elements, info->offset);
  }
wmmhello's avatar
wmmhello 已提交
406 407
  if(**start == '\0') return TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
408 409 410 411 412
  SSmlKv kv = {.key = VALUE, .keyLen = VALUE_LEN, .value = elements->cols, .length = (size_t)elements->colsLen};
  if (smlParseNumber(&kv, &info->msgBuf)) {
    kv.length = (int16_t)tDataTypes[kv.type].bytes;
  }else{
    return TSDB_CODE_TSC_INVALID_VALUE;
413 414 415
  }

  // Parse tags
wmmhello's avatar
wmmhello 已提交
416
  ret = smlParseTagsFromJSON(info, elements);
417 418 419 420 421 422 423 424 425 426 427
  if (unlikely(ret)) {
    uError("OTD:0x%" PRIx64 " Unable to parse tags from JSON payload", info->id);
    return ret;
  }

  if(unlikely(info->reRun)){
    return TSDB_CODE_SUCCESS;
  }

  // Parse timestamp
  // notice!!! put ts back to tag to ensure get meta->precision
wmmhello's avatar
wmmhello 已提交
428
  int64_t ts = smlParseOpenTsdbTime(info, elements->timestamp, elements->timestampLen);
429 430 431 432
  if (unlikely(ts < 0)) {
    uError("OTD:0x%" PRIx64 " Unable to parse timestamp from JSON payload", info->id);
    return TSDB_CODE_INVALID_TIMESTAMP;
  }
wmmhello's avatar
wmmhello 已提交
433
  SSmlKv kvTs = { .key = TS, .keyLen = TS_LEN, .type = TSDB_DATA_TYPE_TIMESTAMP, .i = ts, .length = (size_t)tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes};
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

  if(info->dataFormat){
    ret = smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, &kvTs, 0);
    if(ret == TSDB_CODE_SUCCESS){
      ret = smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, &kv, 1);
    }
    if(ret == TSDB_CODE_SUCCESS){
      ret = smlBuildRow(info->currTableDataCtx);
    }
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
      smlBuildInvalidDataMsg(&info->msgBuf, "smlBuildCol error", NULL);
      return ret;
    }
  }else{
    if(elements->colArray == NULL){
      elements->colArray = taosArrayInit(16, sizeof(SSmlKv));
    }
    taosArrayPush(elements->colArray, &kvTs);
    taosArrayPush(elements->colArray, &kv);
  }
  info->preLine = *elements;

  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
459 460
int32_t smlParseJSON(SSmlHandle *info, char *payload) {
  int32_t payloadNum = 1 << 15;
461 462 463
  int32_t ret = TSDB_CODE_SUCCESS;

  int cnt = 0;
wmmhello's avatar
wmmhello 已提交
464 465
  char *dataPointStart = payload;
  while (1) {
466 467
    if(info->dataFormat) {
      SSmlLineInfo element = {0};
wmmhello's avatar
wmmhello 已提交
468
      ret = smlParseJSONString(info, &dataPointStart, &element);
469
    }else{
wmmhello's avatar
wmmhello 已提交
470 471 472 473 474 475 476
      if(cnt >= payloadNum){
        payloadNum = payloadNum << 1;
        void* tmp = taosMemoryRealloc(info->lines, payloadNum * sizeof(SSmlLineInfo));
        if(tmp != NULL){
          info->lines = (SSmlLineInfo*)tmp;
        }
      }
wmmhello's avatar
wmmhello 已提交
477
      ret = smlParseJSONString(info, &dataPointStart, info->lines + cnt);
478 479 480 481 482 483
    }
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
      uError("SML:0x%" PRIx64 " Invalid JSON Payload", info->id);
      return ret;
    }

wmmhello's avatar
wmmhello 已提交
484 485
    if(*dataPointStart == '\0') break;

486
    if(unlikely(info->reRun)){
487
      cnt = 0;
wmmhello's avatar
wmmhello 已提交
488
      dataPointStart = payload;
489 490 491 492 493 494 495
      info->lineNum = payloadNum;
      ret = smlClearForRerun(info);
      if(ret != TSDB_CODE_SUCCESS){
        return ret;
      }
      continue;
    }
496
    cnt++;
497
  }
wmmhello's avatar
wmmhello 已提交
498
  info->lineNum = cnt;
499 500

  return TSDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
501
}