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 33 34 35 36 37 38 39 40
typedef struct SBuffer  SBuffer;
typedef struct SSchema  SSchema;
typedef struct STColumn STColumn;
typedef struct STSchema STSchema;
typedef struct SValue   SValue;
typedef struct SColVal  SColVal;
typedef struct SRow     SRow;
typedef struct SRowIter SRowIter;
typedef struct STagVal  STagVal;
typedef struct STag     STag;
typedef struct SColData SColData;
H
Hongze Cheng 已提交
41

H
Hongze Cheng 已提交
42 43 44
#define HAS_NONE  ((uint8_t)0x1)
#define HAS_NULL  ((uint8_t)0x2)
#define HAS_VALUE ((uint8_t)0x4)
H
refact  
Hongze Cheng 已提交
45 46

// bitmap ================================
H
Hongze Cheng 已提交
47 48 49 50
const static uint8_t BIT1_MAP[8] = {0b11111110, 0b11111101, 0b11111011, 0b11110111,
                                    0b11101111, 0b11011111, 0b10111111, 0b01111111};

const static uint8_t BIT2_MAP[4] = {0b11111100, 0b11110011, 0b11001111, 0b00111111};
H
Hongze Cheng 已提交
51

H
Hongze Cheng 已提交
52 53 54 55 56 57 58 59 60 61
#define ONE               ((uint8_t)1)
#define THREE             ((uint8_t)3)
#define DIV_8(i)          ((i) >> 3)
#define MOD_8(i)          ((i)&7)
#define DIV_4(i)          ((i) >> 2)
#define MOD_4(i)          ((i)&3)
#define MOD_4_TIME_2(i)   (MOD_4(i) << 1)
#define BIT1_SIZE(n)      (DIV_8((n)-1) + 1)
#define BIT2_SIZE(n)      (DIV_4((n)-1) + 1)
#define SET_BIT1(p, i, v) ((p)[DIV_8(i)] = (p)[DIV_8(i)] & BIT1_MAP[MOD_8(i)] | ((v) << MOD_8(i)))
H
Hongze Cheng 已提交
62 63 64 65 66 67 68
#define SET_BIT1_EX(p, i, v) \
  do {                       \
    if (MOD_8(i) == 0) {     \
      (p)[DIV_8(i)] = 0;     \
    }                        \
    SET_BIT1(p, i, v);       \
  } while (0)
H
Hongze Cheng 已提交
69 70
#define GET_BIT1(p, i)    (((p)[DIV_8(i)] >> MOD_8(i)) & ONE)
#define SET_BIT2(p, i, v) ((p)[DIV_4(i)] = (p)[DIV_4(i)] & BIT2_MAP[MOD_4(i)] | ((v) << MOD_4_TIME_2(i)))
H
Hongze Cheng 已提交
71 72 73 74 75 76 77 78
#define SET_BIT2_EX(p, i, v) \
  do {                       \
    if (MOD_4(i) == 0) {     \
      (p)[DIV_4(i)] = 0;     \
    }                        \
    SET_BIT2(p, i, v);       \
  } while (0)
#define GET_BIT2(p, i) (((p)[DIV_4(i)] >> MOD_4_TIME_2(i)) & THREE)
H
Hongze Cheng 已提交
79

H
more  
Hongze Cheng 已提交
80 81 82 83 84 85 86 87 88 89 90
// SBuffer ================================
struct SBuffer {
  int64_t  nBuf;
  uint8_t *pBuf;
};

#define tBufferCreate() \
  (SBuffer) { .nBuf = 0, .pBuf = NULL }
void    tBufferDestroy(SBuffer *pBuffer);
int32_t tBufferInit(SBuffer *pBuffer, int64_t size);
int32_t tBufferPut(SBuffer *pBuffer, const void *pData, int64_t nData);
H
Hongze Cheng 已提交
91
int32_t tBufferReserve(SBuffer *pBuffer, int64_t nData, void **ppData);
H
more  
Hongze Cheng 已提交
92

H
refact  
Hongze Cheng 已提交
93
// SColVal ================================
H
Hongze Cheng 已提交
94 95 96 97 98 99
#define CV_FLAG_VALUE ((int8_t)0x0)
#define CV_FLAG_NONE  ((int8_t)0x1)
#define CV_FLAG_NULL  ((int8_t)0x2)

#define COL_VAL_NONE(CID, TYPE)     ((SColVal){.cid = (CID), .type = (TYPE), .flag = CV_FLAG_NONE})
#define COL_VAL_NULL(CID, TYPE)     ((SColVal){.cid = (CID), .type = (TYPE), .flag = CV_FLAG_NULL})
H
Hongze Cheng 已提交
100
#define COL_VAL_VALUE(CID, TYPE, V) ((SColVal){.cid = (CID), .type = (TYPE), .value = (V)})
H
more  
Hongze Cheng 已提交
101

H
Hongze Cheng 已提交
102 103 104 105
#define COL_VAL_IS_NONE(CV)  ((CV)->flag == CV_FLAG_NONE)
#define COL_VAL_IS_NULL(CV)  ((CV)->flag == CV_FLAG_NULL)
#define COL_VAL_IS_VALUE(CV) ((CV)->flag == CV_FLAG_VALUE)

H
Hongze Cheng 已提交
106
// SRow ================================
107
int32_t tRowBuild(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow);
H
Hongze Cheng 已提交
108
void    tRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal);
H
Hongze Cheng 已提交
109
void    tRowDestroy(SRow *pRow);
H
Hongze Cheng 已提交
110 111
void    tRowSort(SArray *aRowP);
int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, int8_t flag);
H
Hongze Cheng 已提交
112
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag);
H
Hongze Cheng 已提交
113 114

// SRowIter ================================
H
Hongze Cheng 已提交
115 116 117
int32_t  tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter);
void     tRowIterClose(SRowIter **ppIter);
SColVal *tRowIterNext(SRowIter *pIter);
H
Hongze Cheng 已提交
118

H
refact  
Hongze Cheng 已提交
119
// STag ================================
H
Hongze Cheng 已提交
120
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag);
H
Hongze Cheng 已提交
121
void    tTagFree(STag *pTag);
wmmhello's avatar
wmmhello 已提交
122 123
bool    tTagIsJson(const void *pTag);
bool    tTagIsJsonNull(void *tagVal);
C
Cary Xu 已提交
124
bool    tTagGet(const STag *pTag, STagVal *pTagVal);
H
more  
Hongze Cheng 已提交
125
char   *tTagValToData(const STagVal *pTagVal, bool isJson);
H
more  
Hongze Cheng 已提交
126
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag);
H
Hongze Cheng 已提交
127
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag);
C
Cary Xu 已提交
128
int32_t tTagToValArray(const STag *pTag, SArray **ppArray);
wmmhello's avatar
wmmhello 已提交
129
void    tTagSetCid(const STag *pTag, int16_t iTag, int16_t cid);
H
more  
Hongze Cheng 已提交
130
void    debugPrintSTag(STag *pTag, const char *tag, int32_t ln);  // TODO: remove
H
Hongze Cheng 已提交
131
int32_t parseJsontoTagData(const char *json, SArray *pTagVals, STag **ppTag, void *pMsgBuf);
H
Hongze Cheng 已提交
132

H
refact  
Hongze Cheng 已提交
133
// SColData ================================
H
Hongze Cheng 已提交
134
typedef void *(*xMallocFn)(void *, int32_t);
H
Hongze Cheng 已提交
135
void    tColDataDestroy(void *ph);
H
refact  
Hongze Cheng 已提交
136
void    tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t smaOn);
H
Hongze Cheng 已提交
137
void    tColDataClear(SColData *pColData);
D
dapan1121 已提交
138
void    tColDataDeepClear(SColData *pColData);
H
refact  
Hongze Cheng 已提交
139
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal);
H
Hongze Cheng 已提交
140
int32_t tColDataUpdateValue(SColData *pColData, SColVal *pColVal, bool forward);
H
Hongze Cheng 已提交
141
void    tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal);
H
Haojun Liao 已提交
142
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal);
H
Hongze Cheng 已提交
143
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg);
H
Hongze Cheng 已提交
144
extern void (*tColDataCalcSMA[])(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min, int16_t *numOfNull);
H
refact  
Hongze Cheng 已提交
145

H
Hongze Cheng 已提交
146 147
// for stmt bind
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind);
H
Hongze Cheng 已提交
148
void    tColDataSortMerge(SArray *colDataArr);
H
Hongze Cheng 已提交
149

H
Hongze Cheng 已提交
150 151 152
// for raw block
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
                                    char *data);
H
Hongze Cheng 已提交
153 154 155
// for encode/decode
int32_t tPutColData(uint8_t *pBuf, SColData *pColData);
int32_t tGetColData(uint8_t *pBuf, SColData *pColData);
H
Hongze Cheng 已提交
156

H
refact  
Hongze Cheng 已提交
157
// STRUCT ================================
H
Hongze Cheng 已提交
158 159 160 161 162 163 164 165 166
struct STColumn {
  col_id_t colId;
  int8_t   type;
  int8_t   flags;
  int32_t  bytes;
  int32_t  offset;
};

struct STSchema {
H
Hongze Cheng 已提交
167 168 169 170 171
  int32_t  numOfCols;
  int32_t  version;
  int32_t  flen;
  int32_t  tlen;
  STColumn columns[];
H
Hongze Cheng 已提交
172 173
};

H
Hongze Cheng 已提交
174 175 176 177 178
struct SRow {
  uint8_t  flag;
  uint8_t  rsv;
  uint16_t sver;
  uint32_t len;
H
Hongze Cheng 已提交
179
  TSKEY    ts;
H
Hongze Cheng 已提交
180
  uint8_t  data[];
H
Hongze Cheng 已提交
181 182
};

H
Hongze Cheng 已提交
183 184
struct SValue {
  union {
H
Hongze Cheng 已提交
185
    int64_t val;
H
Hongze Cheng 已提交
186 187 188 189 190 191 192
    struct {
      uint32_t nData;
      uint8_t *pData;
    };
  };
};

H
Hongze Cheng 已提交
193
struct SColVal {
H
Hongze Cheng 已提交
194
  int16_t cid;
H
Hongze Cheng 已提交
195
  int8_t  type;
H
Hongze Cheng 已提交
196
  int8_t  flag;
H
Hongze Cheng 已提交
197
  SValue  value;
H
Hongze Cheng 已提交
198 199
};

H
refact  
Hongze Cheng 已提交
200 201 202 203 204 205 206 207 208 209 210 211
struct SColData {
  int16_t  cid;
  int8_t   type;
  int8_t   smaOn;
  int32_t  nVal;
  uint8_t  flag;
  uint8_t *pBitMap;
  int32_t *aOffset;
  int32_t  nData;
  uint8_t *pData;
};

wmmhello's avatar
wmmhello 已提交
212
#pragma pack(push, 1)
H
Hongze Cheng 已提交
213
struct STagVal {
H
Hongze Cheng 已提交
214
  //  char colName[TSDB_COL_NAME_LEN]; // only used for tmq_get_meta
H
Hongze Cheng 已提交
215 216 217 218
  union {
    int16_t cid;
    char   *pKey;
  };
H
Hongze Cheng 已提交
219 220
  int8_t type;
  union {
H
more  
Hongze Cheng 已提交
221
    int64_t i64;
H
Hongze Cheng 已提交
222 223 224 225 226
    struct {
      uint32_t nData;
      uint8_t *pData;
    };
  };
H
Hongze Cheng 已提交
227 228
};

H
more  
Hongze Cheng 已提交
229
#define TD_TAG_JSON  ((int8_t)0x40)  // distinguish JSON string and JSON value with the highest bit
wmmhello's avatar
wmmhello 已提交
230
#define TD_TAG_LARGE ((int8_t)0x20)
C
Cary Xu 已提交
231
struct STag {
C
Cary Xu 已提交
232
  int8_t  flags;
C
Cary Xu 已提交
233 234 235
  int16_t len;
  int16_t nTag;
  int32_t ver;
C
Cary Xu 已提交
236
  int8_t  idx[];
C
Cary Xu 已提交
237 238 239
};
#pragma pack(pop)

H
Hongze Cheng 已提交
240
#if 1  //================================================================================================================================================
C
Cary Xu 已提交
241 242
// 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 已提交
243

244 245 246 247 248
#define STR_TO_VARSTR(x, str)                     \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = __len;                  \
    memcpy(varDataVal(x), (str), __len);          \
H
Hongze Cheng 已提交
249 250
  } while (0);

251 252
#define STR_WITH_MAXSIZE_TO_VARSTR(x, str, _maxs)                         \
  do {                                                                    \
H
Hui Li 已提交
253
    char *_e = stpncpy(varDataVal(x), (str), (_maxs)-VARSTR_HEADER_SIZE); \
254
    varDataSetLen(x, (_e - (x)-VARSTR_HEADER_SIZE));                      \
H
Hongze Cheng 已提交
255 256
  } while (0)

257 258 259 260
#define STR_WITH_SIZE_TO_VARSTR(x, str, _size)  \
  do {                                          \
    *(VarDataLenT *)(x) = (VarDataLenT)(_size); \
    memcpy(varDataVal(x), (str), (_size));      \
H
Hongze Cheng 已提交
261
  } while (0);
H
hjxilinx 已提交
262

H
Hongze Cheng 已提交
263
// STSchema ================================
H
Hongze Cheng 已提交
264
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version);
H
Hongze Cheng 已提交
265
void      tDestroyTSchema(STSchema *pTSchema);
H
Hongze Cheng 已提交
266

H
Hongze Cheng 已提交
267
#endif
268

H
more  
hzcheng 已提交
269 270 271 272
#ifdef __cplusplus
}
#endif

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