tdataformat.h 8.8 KB
Newer Older
H
more  
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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
Shengliang Guan 已提交
15

16 17
#ifndef _TD_COMMON_DATA_FORMAT_H_
#define _TD_COMMON_DATA_FORMAT_H_
H
more  
Hongze Cheng 已提交
18

S
TD-4088  
Shengliang Guan 已提交
19
#include "os.h"
H
Hongze Cheng 已提交
20
#include "talgo.h"
H
Hongze Cheng 已提交
21
#include "tarray.h"
H
Hongze Cheng 已提交
22
#include "tencode.h"
H
Haojun Liao 已提交
23
#include "ttypes.h"
H
TD-166  
hzcheng 已提交
24
#include "tutil.h"
H
hzcheng 已提交
25

H
more  
hzcheng 已提交
26 27 28
#ifdef __cplusplus
extern "C" {
#endif
H
hzcheng 已提交
29

H
Hongze Cheng 已提交
30 31 32
typedef struct SSchema       SSchema;
typedef struct STColumn      STColumn;
typedef struct STSchema      STSchema;
H
Hongze Cheng 已提交
33
typedef struct SValue        SValue;
H
Hongze Cheng 已提交
34
typedef struct SColVal       SColVal;
H
Hongze Cheng 已提交
35 36
typedef struct STSRow2       STSRow2;
typedef struct STSRowBuilder STSRowBuilder;
H
Hongze Cheng 已提交
37 38
typedef struct STagVal       STagVal;
typedef struct STag          STag;
H
Hongze Cheng 已提交
39

H
Hongze Cheng 已提交
40
// bitmap
H
Hongze Cheng 已提交
41 42 43 44
const static uint8_t BIT1_MAP[8][2] = {
    {0b00000000, 0b00000001}, {0b00000001, 0b00000011}, {0b00000011, 0b00000111}, {0b00000111, 0b00001111},
    {0b00001111, 0b00011111}, {0b00011111, 0b00111111}, {0b00111111, 0b01111111}, {0b01111111, 0b11111111},
};
H
Hongze Cheng 已提交
45

H
Hongze Cheng 已提交
46 47 48 49 50
const static uint8_t BIT2_MAP[4][4] = {{0b00000000, 0b00000001, 0b00000010, 0},
                                       {0b00000011, 0b00000111, 0b00001011, 2},
                                       {0b00001111, 0b00011111, 0b00101111, 4},
                                       {0b00111111, 0b01111111, 0b10111111, 6}};

H
Hongze Cheng 已提交
51 52
#define BIT1_SIZE(n)      ((((n)-1) >> 3) + 1)
#define BIT2_SIZE(n)      ((((n)-1) >> 2) + 1)
H
Hongze Cheng 已提交
53
#define SET_BIT1(p, i, v) ((p)[(i) >> 3] |= BIT1_MAP[(i)&7][v])
H
more  
Hongze Cheng 已提交
54
#define GET_BIT1(p, i)    (((p)[(i) >> 3] >> ((i)&7)) & ((uint8_t)1))
H
Hongze Cheng 已提交
55
#define SET_BIT2(p, i, v) ((p)[(i) >> 2] |= BIT2_MAP[(i)&3][v])
H
more  
Hongze Cheng 已提交
56
#define GET_BIT2(p, i)    (((p)[(i) >> 2] >> BIT2_MAP[(i)&3][3]) & ((uint8_t)3))
H
Hongze Cheng 已提交
57

H
Hongze Cheng 已提交
58
// STSchema
H
Hongze Cheng 已提交
59
int32_t tTSchemaCreate(int32_t sver, SSchema *pSchema, int32_t nCols, STSchema **ppTSchema);
H
Hongze Cheng 已提交
60
void    tTSchemaDestroy(STSchema *pTSchema);
H
Hongze Cheng 已提交
61

H
more  
Hongze Cheng 已提交
62
// SValue
H
Hongze Cheng 已提交
63 64 65
int32_t tPutValue(uint8_t *p, SValue *pValue, int8_t type);
int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type);
int     tValueCmprFn(const SValue *pValue1, const SValue *pValue2, int8_t type);
H
more  
Hongze Cheng 已提交
66

H
Hongze Cheng 已提交
67
// SColVal
H
Hongze Cheng 已提交
68 69 70
#define COL_VAL_NONE(CID, TYPE)     ((SColVal){.cid = (CID), .type = (TYPE), .isNone = 1})
#define COL_VAL_NULL(CID, TYPE)     ((SColVal){.cid = (CID), .type = (TYPE), .isNull = 1})
#define COL_VAL_VALUE(CID, TYPE, V) ((SColVal){.cid = (CID), .type = (TYPE), .value = (V)})
H
more  
Hongze Cheng 已提交
71

H
Hongze Cheng 已提交
72 73 74 75
// STSRow2
#define TSROW_LEN(PROW, V)  tGetI32v((uint8_t *)(PROW)->data, (V) ? &(V) : NULL)
#define TSROW_SVER(PROW, V) tGetI32v((PROW)->data + TSROW_LEN(PROW, NULL), (V) ? &(V) : NULL)

H
more  
Hongze Cheng 已提交
76
int32_t tTSRowNew(STSRowBuilder *pBuilder, SArray *pArray, STSchema *pTSchema, STSRow2 **ppRow);
H
Hongze Cheng 已提交
77
int32_t tTSRowClone(const STSRow2 *pRow, STSRow2 **ppRow);
H
more  
Hongze Cheng 已提交
78
void    tTSRowFree(STSRow2 *pRow);
H
Hongze Cheng 已提交
79 80
void    tTSRowGet(STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal);
int32_t tTSRowToArray(STSRow2 *pRow, STSchema *pTSchema, SArray **ppArray);
H
Hongze Cheng 已提交
81
int32_t tPutTSRow(uint8_t *p, STSRow2 *pRow);
H
Hongze Cheng 已提交
82
int32_t tGetTSRow(uint8_t *p, STSRow2 **ppRow);
H
Hongze Cheng 已提交
83

H
Hongze Cheng 已提交
84
// STSRowBuilder
H
more  
Hongze Cheng 已提交
85 86 87 88 89 90 91
#define tsRowBuilderInit() ((STSRowBuilder){0})
#define tsRowBuilderClear(B)     \
  do {                           \
    if ((B)->pBuf) {             \
      taosMemoryFree((B)->pBuf); \
    }                            \
  } while (0)
H
Hongze Cheng 已提交
92

H
Hongze Cheng 已提交
93
// STag
H
Hongze Cheng 已提交
94
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag);
H
Hongze Cheng 已提交
95
void    tTagFree(STag *pTag);
wmmhello's avatar
wmmhello 已提交
96 97
bool    tTagIsJson(const void *pTag);
bool    tTagIsJsonNull(void *tagVal);
C
Cary Xu 已提交
98
bool    tTagGet(const STag *pTag, STagVal *pTagVal);
H
more  
Hongze Cheng 已提交
99
char   *tTagValToData(const STagVal *pTagVal, bool isJson);
H
more  
Hongze Cheng 已提交
100
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag);
H
Hongze Cheng 已提交
101
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag);
C
Cary Xu 已提交
102
int32_t tTagToValArray(const STag *pTag, SArray **ppArray);
H
more  
Hongze Cheng 已提交
103
void    debugPrintSTag(STag *pTag, const char *tag, int32_t ln);  // TODO: remove
H
Hongze Cheng 已提交
104
int32_t parseJsontoTagData(const char *json, SArray *pTagVals, STag **ppTag, void *pMsgBuf);
H
Hongze Cheng 已提交
105

H
Hongze Cheng 已提交
106 107 108 109 110 111 112 113 114 115
// STRUCT =================
struct STColumn {
  col_id_t colId;
  int8_t   type;
  int8_t   flags;
  int32_t  bytes;
  int32_t  offset;
};

struct STSchema {
H
Hongze Cheng 已提交
116 117 118 119 120 121
  int32_t  numOfCols;
  int32_t  version;
  int32_t  flen;
  int32_t  vlen;
  int32_t  tlen;
  STColumn columns[];
H
Hongze Cheng 已提交
122 123
};

H
Hongze Cheng 已提交
124 125 126
#define TSROW_HAS_NONE ((uint8_t)0x1)
#define TSROW_HAS_NULL ((uint8_t)0x2U)
#define TSROW_HAS_VAL  ((uint8_t)0x4U)
H
Hongze Cheng 已提交
127 128 129
#define TSROW_KV_SMALL ((uint8_t)0x10U)
#define TSROW_KV_MID   ((uint8_t)0x20U)
#define TSROW_KV_BIG   ((uint8_t)0x40U)
H
Hongze Cheng 已提交
130
#pragma pack(push, 1)
H
Hongze Cheng 已提交
131
struct STSRow2 {
H
Hongze Cheng 已提交
132 133 134
  TSKEY   ts;
  uint8_t flags;
  uint8_t data[];
H
Hongze Cheng 已提交
135
};
H
Hongze Cheng 已提交
136
#pragma pack(pop)
H
Hongze Cheng 已提交
137 138

struct STSRowBuilder {
H
Hongze Cheng 已提交
139
  // STSRow2  tsRow;
H
more  
Hongze Cheng 已提交
140 141
  int32_t  szBuf;
  uint8_t *pBuf;
H
Hongze Cheng 已提交
142 143
};

H
Hongze Cheng 已提交
144 145
struct SValue {
  union {
H
more  
Hongze Cheng 已提交
146 147 148 149 150 151 152 153 154 155 156
    int8_t   i8;   // TSDB_DATA_TYPE_BOOL||TSDB_DATA_TYPE_TINYINT
    uint8_t  u8;   // TSDB_DATA_TYPE_UTINYINT
    int16_t  i16;  // TSDB_DATA_TYPE_SMALLINT
    uint16_t u16;  // TSDB_DATA_TYPE_USMALLINT
    int32_t  i32;  // TSDB_DATA_TYPE_INT
    uint32_t u32;  // TSDB_DATA_TYPE_UINT
    int64_t  i64;  // TSDB_DATA_TYPE_BIGINT
    uint64_t u64;  // TSDB_DATA_TYPE_UBIGINT
    TSKEY    ts;   // TSDB_DATA_TYPE_TIMESTAMP
    float    f;    // TSDB_DATA_TYPE_FLOAT
    double   d;    // TSDB_DATA_TYPE_DOUBLE
H
Hongze Cheng 已提交
157 158 159 160 161 162 163
    struct {
      uint32_t nData;
      uint8_t *pData;
    };
  };
};

H
Hongze Cheng 已提交
164
struct SColVal {
H
Hongze Cheng 已提交
165
  int16_t cid;
H
Hongze Cheng 已提交
166
  int8_t  type;
H
Hongze Cheng 已提交
167 168 169
  int8_t  isNone;
  int8_t  isNull;
  SValue  value;
H
Hongze Cheng 已提交
170 171
};

wmmhello's avatar
wmmhello 已提交
172
#pragma pack(push, 1)
H
Hongze Cheng 已提交
173
struct STagVal {
H
Hongze Cheng 已提交
174
  //  char colName[TSDB_COL_NAME_LEN]; // only used for tmq_get_meta
H
Hongze Cheng 已提交
175 176 177 178
  union {
    int16_t cid;
    char   *pKey;
  };
H
Hongze Cheng 已提交
179 180
  int8_t type;
  union {
H
more  
Hongze Cheng 已提交
181
    int64_t i64;
H
Hongze Cheng 已提交
182 183 184 185 186
    struct {
      uint32_t nData;
      uint8_t *pData;
    };
  };
H
Hongze Cheng 已提交
187 188
};

H
more  
Hongze Cheng 已提交
189
#define TD_TAG_JSON  ((int8_t)0x40)  // distinguish JSON string and JSON value with the highest bit
wmmhello's avatar
wmmhello 已提交
190
#define TD_TAG_LARGE ((int8_t)0x20)
C
Cary Xu 已提交
191
struct STag {
C
Cary Xu 已提交
192
  int8_t  flags;
C
Cary Xu 已提交
193 194 195
  int16_t len;
  int16_t nTag;
  int32_t ver;
C
Cary Xu 已提交
196
  int8_t  idx[];
C
Cary Xu 已提交
197 198 199
};
#pragma pack(pop)

H
Hongze Cheng 已提交
200
#if 1  //================================================================================================================================================
C
Cary Xu 已提交
201 202
// Imported since 3.0 and use bitmap to demonstrate None/Null/Norm, while use Null/Norm below 3.0 without of bitmap.
#define TD_SUPPORT_BITMAP
K
Kaili Xu 已提交
203 204

#define TASSERT(x) ASSERT(x)
C
Cary Xu 已提交
205

206 207 208 209 210
#define STR_TO_VARSTR(x, str)                     \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = __len;                  \
    memcpy(varDataVal(x), (str), __len);          \
H
Hongze Cheng 已提交
211 212
  } while (0);

H
Hongze Cheng 已提交
213 214 215 216 217 218
#define STR_TO_NET_VARSTR(x, str)                 \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = htons(__len);           \
    memcpy(varDataVal(x), (str), __len);          \
  } while (0);
D
dapan1121 已提交
219

220 221
#define STR_WITH_MAXSIZE_TO_VARSTR(x, str, _maxs)                         \
  do {                                                                    \
H
Hui Li 已提交
222
    char *_e = stpncpy(varDataVal(x), (str), (_maxs)-VARSTR_HEADER_SIZE); \
223
    varDataSetLen(x, (_e - (x)-VARSTR_HEADER_SIZE));                      \
H
Hongze Cheng 已提交
224 225
  } while (0)

226 227 228 229
#define STR_WITH_SIZE_TO_VARSTR(x, str, _size)  \
  do {                                          \
    *(VarDataLenT *)(x) = (VarDataLenT)(_size); \
    memcpy(varDataVal(x), (str), (_size));      \
H
Hongze Cheng 已提交
230
  } while (0);
H
hjxilinx 已提交
231

232 233
// ----------------- SCHEMA BUILDER DEFINITION
typedef struct {
C
Cary Xu 已提交
234 235 236 237 238 239 240
  int32_t      tCols;
  int32_t      nCols;
  schema_ver_t version;
  uint16_t     flen;
  int32_t      vlen;
  int32_t      tlen;
  STColumn    *columns;
241 242
} STSchemaBuilder;

C
Cary Xu 已提交
243 244 245 246 247 248 249 250 251 252 253
// use 2 bits for bitmap(default: STSRow/sub block)
#define TD_VTYPE_BITS        2
#define TD_VTYPE_PARTS       4  // PARTITIONS: 1 byte / 2 bits
#define TD_VTYPE_OPTR        3  // OPERATOR: 4 - 1, utilize to get remainder
#define TD_BITMAP_BYTES(cnt) (((cnt) + TD_VTYPE_OPTR) >> 2)

// use 1 bit for bitmap(super block)
#define TD_VTYPE_BITS_I        1
#define TD_VTYPE_PARTS_I       8  // PARTITIONS: 1 byte / 1 bit
#define TD_VTYPE_OPTR_I        7  // OPERATOR: 8 - 1, utilize to get remainder
#define TD_BITMAP_BYTES_I(cnt) (((cnt) + TD_VTYPE_OPTR_I) >> 3)
C
Cary Xu 已提交
254

C
Cary Xu 已提交
255
int32_t   tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version);
256
void      tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder);
C
Cary Xu 已提交
257
void      tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version);
C
Cary Xu 已提交
258
int32_t   tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes);
259
STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder);
H
Hongze Cheng 已提交
260

H
Hongze Cheng 已提交
261
#endif
262

H
more  
hzcheng 已提交
263 264 265 266
#ifdef __cplusplus
}
#endif

H
Hongze Cheng 已提交
267
#endif /*_TD_COMMON_DATA_FORMAT_H_*/