clientSml.h 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/*
 * 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/>.
 */
#ifndef TDENGINE_CLIENTSML_H
#define TDENGINE_CLIENTSML_H
#ifdef __cplusplus
extern "C" {
#endif

#include "catalog.h"
#include "clientInt.h"
#include "osThread.h"
#include "query.h"
#include "taos.h"
#include "taoserror.h"
#include "tcommon.h"
#include "tdef.h"
#include "tglobal.h"
#include "tlog.h"
#include "tmsg.h"
#include "tname.h"
#include "ttime.h"
#include "ttypes.h"
#include "cJSON.h"

#if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__)
#  define expect(expr,value)    (__builtin_expect ((expr),(value)) )
#else
#  define expect(expr,value)    (expr)
#endif

#ifndef likely
#define likely(expr)     expect((expr) != 0, 1)
#endif
#ifndef unlikely
#define unlikely(expr)   expect((expr) != 0, 0)
#endif

#define SPACE ' '
#define COMMA ','
#define EQUAL '='
#define QUOTE '"'
#define SLASH '\\'

#define JUMP_SPACE(sql, sqlEnd) \
  while (sql < sqlEnd) {        \
    if (unlikely(*sql == SPACE))          \
      sql++;                    \
    else                        \
      break;                    \
  }

#define IS_INVALID_COL_LEN(len)   ((len) <= 0 || (len) >= TSDB_COL_NAME_LEN)
#define IS_INVALID_TABLE_LEN(len) ((len) <= 0 || (len) >= TSDB_TABLE_NAME_LEN)

#define TS        "_ts"
#define TS_LEN    3
#define VALUE     "_value"
#define VALUE_LEN 6

wmmhello's avatar
wmmhello 已提交
72
#define OTD_JSON_FIELDS_NUM     4
D
dapan1121 已提交
73
#define MAX_RETRY_TIMES 100
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
typedef TSDB_SML_PROTOCOL_TYPE SMLProtocolType;

typedef enum {
  SCHEMA_ACTION_NULL,
  SCHEMA_ACTION_CREATE_STABLE,
  SCHEMA_ACTION_ADD_COLUMN,
  SCHEMA_ACTION_ADD_TAG,
  SCHEMA_ACTION_CHANGE_COLUMN_SIZE,
  SCHEMA_ACTION_CHANGE_TAG_SIZE,
} ESchemaAction;

typedef struct {
  const void  *key;
  int32_t      keyLen;
  void        *value;
  bool         used;
}Node;

typedef struct NodeList{
  Node             data;
  struct NodeList* next;
}NodeList;

typedef struct {
  char *measure;
  char *tags;
  char *cols;
  char *timestamp;
102
  char *measureTag;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

  int32_t measureLen;
  int32_t measureTagsLen;
  int32_t tagsLen;
  int32_t colsLen;
  int32_t timestampLen;

  SArray *colArray;
} SSmlLineInfo;

typedef struct {
  const char *sTableName;  // super table name
  int32_t     sTableNameLen;
  char        childTableName[TSDB_TABLE_NAME_LEN];
  uint64_t    uid;
118
//  void       *key;        // for openTsdb
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

  SArray *tags;

  // elements are SHashObj<cols key string, SSmlKv*> for find by key quickly
  SArray *cols;
  STableDataCxt *tableDataCtx;
} SSmlTableInfo;

typedef struct {
  SArray   *tags;     // save the origin order to create table
  SHashObj *tagHash;  // elements are <key, index in tags>

  SArray   *cols;
  SHashObj *colHash;

  STableMeta *tableMeta;
} SSmlSTableMeta;

typedef struct {
  int32_t len;
  char   *buf;
} SSmlMsgBuf;

typedef struct {
  int32_t code;
  int32_t lineNum;

  int32_t numOfSTables;
  int32_t numOfCTables;
  int32_t numOfCreateSTables;
  int32_t numOfAlterColSTables;
  int32_t numOfAlterTagSTables;

  int64_t parseTime;
  int64_t schemaTime;
  int64_t insertBindTime;
  int64_t insertRpcTime;
  int64_t endTime;
} SSmlCostInfo;

typedef struct {
  int64_t id;

  SMLProtocolType protocol;
  int8_t          precision;
  bool            reRun;
  bool            dataFormat;  // true means that the name and order of keys in each line are the same(only for influx protocol)
  bool            isRawLine;
  int32_t         ttl;
wmmhello's avatar
wmmhello 已提交
168
  int32_t         uid; // used for automatic create child table
169

170 171
  SHashObj *childTables;
  SHashObj *superTables;
172 173 174 175 176 177 178 179 180 181 182
  SHashObj *pVgHash;

  STscObj     *taos;
  SCatalog    *pCatalog;
  SRequestObj *pRequest;
  SQuery      *pQuery;

  SSmlCostInfo cost;
  int32_t      lineNum;
  SSmlMsgBuf   msgBuf;

wmmhello's avatar
wmmhello 已提交
183
  cJSON       *root;  // for parse json
wmmhello's avatar
wmmhello 已提交
184
  int8_t             offset[OTD_JSON_FIELDS_NUM];
185
  SSmlLineInfo      *lines; // element is SSmlLineInfo
wmmhello's avatar
wmmhello 已提交
186
  bool               parseJsonByLib;
wmmhello's avatar
wmmhello 已提交
187
  SArray      *tagJsonArray;
188
  SArray      *valueJsonArray;
189 190 191

  //
  SArray      *preLineTagKV;
wmmhello's avatar
wmmhello 已提交
192
  SArray      *maxTagKVs;
193
  SArray      *masColKVs;
194 195 196 197 198 199 200 201 202 203 204 205 206

  SSmlLineInfo preLine;
  STableMeta  *currSTableMeta;
  STableDataCxt *currTableDataCtx;
  bool         needModifySchema;
} SSmlHandle;

#define IS_SAME_CHILD_TABLE (elements->measureTagsLen == info->preLine.measureTagsLen \
&& memcmp(elements->measure, info->preLine.measure, elements->measureTagsLen) == 0)

#define IS_SAME_SUPER_TABLE (elements->measureLen == info->preLine.measureLen \
&& memcmp(elements->measure, info->preLine.measure, elements->measureLen) == 0)

wmmhello's avatar
wmmhello 已提交
207
#define IS_SAME_KEY (maxKV->keyLen == kv.keyLen && memcmp(maxKV->key, kv.key, kv.keyLen) == 0)
208 209 210 211 212 213

extern int64_t smlFactorNS[3];
extern int64_t smlFactorS[3];

typedef int32_t (*_equal_fn_sml)(const void *, const void *);

wmmhello's avatar
wmmhello 已提交
214 215
SSmlHandle   *smlBuildSmlInfo(TAOS *taos);
void          smlDestroyInfo(SSmlHandle *info);
wmmhello's avatar
wmmhello 已提交
216 217
int           smlJsonParseObjFirst(char **start, SSmlLineInfo *element, int8_t *offset);
int           smlJsonParseObj(char **start, SSmlLineInfo *element, int8_t *offset);
wmmhello's avatar
wmmhello 已提交
218
//SArray       *smlJsonParseTags(char *start, char *end);
wmmhello's avatar
wmmhello 已提交
219
bool          smlParseNumberOld(SSmlKv *kvVal, SSmlMsgBuf *msg);
220 221 222 223 224 225 226 227 228 229 230 231
void*         nodeListGet(NodeList* list, const void *key, int32_t len, _equal_fn_sml fn);
int           nodeListSet(NodeList** list, const void *key, int32_t len, void* value, _equal_fn_sml fn);
int           nodeListSize(NodeList* list);
bool          smlDoubleToInt64OverFlow(double num);
int32_t       smlBuildInvalidDataMsg(SSmlMsgBuf *pBuf, const char *msg1, const char *msg2);
bool          smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg);
int64_t       smlGetTimeValue(const char *value, int32_t len, uint8_t fromPrecision, uint8_t toPrecision);
int8_t        smlGetTsTypeByLen(int32_t len);
SSmlTableInfo*    smlBuildTableInfo(int numRows, const char* measure, int32_t measureLen);
SSmlSTableMeta*   smlBuildSTableMeta(bool isDataFormat);
int32_t           smlSetCTableName(SSmlTableInfo *oneTable);
STableMeta*       smlGetMeta(SSmlHandle *info, const void* measure, int32_t measureLen);
232
int32_t           is_same_child_table_telnet(const void *a, const void *b);
wmmhello's avatar
wmmhello 已提交
233
int64_t           smlParseOpenTsdbTime(SSmlHandle *info, const char *data, int32_t len);
234
int32_t           smlClearForRerun(SSmlHandle *info);
wmmhello's avatar
wmmhello 已提交
235
int32_t           smlParseValue(SSmlKv *pVal, SSmlMsgBuf *msg);
wmmhello's avatar
wmmhello 已提交
236
uint8_t           smlGetTimestampLen(int64_t num);
wmmhello's avatar
wmmhello 已提交
237
void              clearColValArray(SArray* pCols);
238
void              smlDestroyTableInfo(SSmlHandle *info, SSmlTableInfo *tag);
239 240 241 242 243 244 245 246 247 248

int32_t smlParseInfluxString(SSmlHandle *info, char *sql, char *sqlEnd, SSmlLineInfo *elements);
int32_t smlParseTelnetString(SSmlHandle *info, char *sql, char *sqlEnd, SSmlLineInfo *elements);
int32_t smlParseJSON(SSmlHandle *info, char *payload);

#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_CLIENTSML_H