trow.h 13.1 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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/>.
 */

16 17
#ifndef _TD_COMMON_ROW_H_
#define _TD_COMMON_ROW_H_
H
refact  
Hongze Cheng 已提交
18

H
Hongze Cheng 已提交
19
#include "os.h"
H
Haojun Liao 已提交
20
#include "talgo.h"
S
Shengliang Guan 已提交
21
#include "taosdef.h"
H
Haojun Liao 已提交
22
#include "taoserror.h"
H
more  
Hongze Cheng 已提交
23
#include "tbuffer.h"
H
Haojun Liao 已提交
24
#include "tdataformat.h"
S
Shengliang Guan 已提交
25
#include "tdef.h"
H
Haojun Liao 已提交
26 27
#include "ttypes.h"
#include "tutil.h"
H
Hongze Cheng 已提交
28

H
refact  
Hongze Cheng 已提交
29 30 31 32
#ifdef __cplusplus
extern "C" {
#endif

X
Xiaoyu Wang 已提交
33
typedef struct STSRow {
H
refact  
Hongze Cheng 已提交
34 35 36 37 38 39 40
  TSKEY ts;
  union {
    uint32_t info;
    struct {
      uint16_t type : 2;
      uint16_t del : 1;
      uint16_t endian : 1;
41
      uint16_t statis : 1;  // 0 all normal, 1 has null or none
42
      uint16_t reserve : 11;
H
refact  
Hongze Cheng 已提交
43 44 45 46 47 48 49
      uint16_t sver;
    };
  };
  uint32_t len;
  char     data[];
} STSRow;

H
Haojun Liao 已提交
50 51 52 53 54 55 56 57
// Target of tdataformat.h:
// 1. Row related definition in dataformat.h of 2.0 could be replaced with tdataformat.h of 3.0.
// 2. The basic definition in dataformat.h is shared with tdataformat.h of 3.0.

// row type
#define TD_ROW_TP 0x0U  // default
#define TD_ROW_KV 0x01U

H
Hongze Cheng 已提交
58 59 60 61 62 63 64 65
#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)

#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)

H
Haojun Liao 已提交
66
/**
C
Cary Xu 已提交
67 68
 * @brief value type
 *  - for data from client input and STSRow in memory, 3 types of value none/null/norm available
H
Haojun Liao 已提交
69
 */
C
Cary Xu 已提交
70
#define TD_VTYPE_NORM 0x00U  // normal val: not none, not null
H
Haojun Liao 已提交
71
#define TD_VTYPE_NULL 0x01U  // null val
C
Cary Xu 已提交
72 73
#define TD_VTYPE_NONE 0x02U  // none or unknown/undefined
#define TD_VTYPE_MAX  0x03U  //
H
Haojun Liao 已提交
74

C
Cary Xu 已提交
75 76 77 78 79 80
#define TD_VTYPE_NORM_BYTE_I 0x0U
#define TD_VTYPE_NULL_BYTE_I 0xFFU

#define TD_VTYPE_NORM_BYTE_II 0x0U
#define TD_VTYPE_NULL_BYTE_II 0x55U
#define TD_VTYPE_NONE_BYTE_II 0xAAU
H
Haojun Liao 已提交
81

C
Cary Xu 已提交
82 83
#define TD_ROWS_ALL_NORM  0x00U
#define TD_ROWS_NULL_NORM 0x01U
H
Haojun Liao 已提交
84

H
Hongze Cheng 已提交
85
#define TD_COL_ROWS_NORM(c)          ((c)->bitmap == TD_ROWS_ALL_NORM)  // all rows of SDataCol/SBlockCol is NORM
H
Haojun Liao 已提交
86
#define TD_SET_COL_ROWS_BTIMAP(c, v) ((c)->bitmap = (v))
H
Hongze Cheng 已提交
87 88
#define TD_SET_COL_ROWS_NORM(c)      TD_SET_COL_ROWS_BTIMAP((c), TD_ROWS_ALL_NORM)
#define TD_SET_COL_ROWS_MISC(c)      TD_SET_COL_ROWS_BTIMAP((c), TD_ROWS_NULL_NORM)
H
Haojun Liao 已提交
89

H
Hongze Cheng 已提交
90
#define KvConvertRatio            (0.9f)
H
Haojun Liao 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#define isSelectKVRow(klen, tlen) ((klen) < ((tlen)*KvConvertRatio))

#ifdef TD_SUPPORT_BITMAP
static FORCE_INLINE bool tdValTypeIsNone(TDRowValT valType) { return (valType & 0x03U) == TD_VTYPE_NONE; }
static FORCE_INLINE bool tdValTypeIsNull(TDRowValT valType) { return (valType & 0x03U) == TD_VTYPE_NULL; }
static FORCE_INLINE bool tdValTypeIsNorm(TDRowValT valType) { return (valType & 0x03U) == TD_VTYPE_NORM; }
#endif

static FORCE_INLINE bool tdValIsNorm(TDRowValT valType, const void *val, int32_t colType) {
#ifdef TD_SUPPORT_BITMAP
  return tdValTypeIsNorm(valType);
#else
  return !isNull(val, colType);
#endif
}

static FORCE_INLINE bool tdValIsNone(TDRowValT valType) {
#ifdef TD_SUPPORT_BITMAP
  return tdValTypeIsNone(valType);
#else
  return false;
#endif
}

static FORCE_INLINE bool tdValIsNull(TDRowValT valType, const void *val, int32_t colType) {
#ifdef TD_SUPPORT_BITMAP
  return tdValTypeIsNull(valType);
#else
  return isNull(val, colType);
#endif
}

typedef struct {
  TDRowValT valType;
L
fix  
Liu Jicong 已提交
125
  void     *val;
H
Haojun Liao 已提交
126
} SCellVal;
H
Hongze Cheng 已提交
127

H
more  
Hongze Cheng 已提交
128
typedef struct {
H
more  
Hongze Cheng 已提交
129
  // TODO
C
Cary Xu 已提交
130 131
  int tmp;  // TODO: to avoid compile error
} STpRow;   // tuple
H
Hongze Cheng 已提交
132

H
Haojun Liao 已提交
133
#pragma pack(push, 1)
H
more  
Hongze Cheng 已提交
134
typedef struct {
H
Haojun Liao 已提交
135
  col_id_t colId;
H
more  
Hongze Cheng 已提交
136 137
  uint32_t offset;
} SKvRowIdx;
H
Haojun Liao 已提交
138
#pragma pack(pop)
H
Hongze Cheng 已提交
139

H
more  
Hongze Cheng 已提交
140 141 142 143 144
typedef struct {
  uint16_t  ncols;
  SKvRowIdx cidx[];
} SKvRow;

H
Haojun Liao 已提交
145 146
typedef struct {
  // basic info
147 148 149
  int8_t       rowType;
  schema_ver_t sver;
  STSRow      *pBuf;
H
Haojun Liao 已提交
150 151

  // extended info
152 153 154 155 156 157 158 159 160
  int32_t  flen;
  col_id_t nBoundCols;
  col_id_t nCols;
  col_id_t nBitmaps;
  col_id_t nBoundBitmaps;
  int32_t  offset;
  void    *pBitmap;
  void    *pOffset;
  int32_t  extendedRowSize;
C
Cary Xu 已提交
161 162
  bool     hasNone;
  bool     hasNull;
H
Haojun Liao 已提交
163 164
} SRowBuilder;

C
Cary Xu 已提交
165
#define TD_ROW_HEAD_LEN  (sizeof(STSRow))
H
Haojun Liao 已提交
166 167
#define TD_ROW_NCOLS_LEN (sizeof(col_id_t))

H
Hongze Cheng 已提交
168 169 170 171 172 173 174 175 176
#define TD_ROW_INFO(r)   ((r)->info)
#define TD_ROW_TYPE(r)   ((r)->type)
#define TD_ROW_DELETE(r) ((r)->del)
#define TD_ROW_ENDIAN(r) ((r)->endian)
#define TD_ROW_SVER(r)   ((r)->sver)
#define TD_ROW_NCOLS(r)  ((r)->data)  // only valid for SKvRow
#define TD_ROW_DATA(r)   ((r)->data)
#define TD_ROW_LEN(r)    ((r)->len)
#define TD_ROW_KEY(r)    ((r)->ts)
C
Cary Xu 已提交
177
// #define TD_ROW_VER(r)      ((r)->ver)
C
Cary Xu 已提交
178
#define TD_ROW_KEY_ADDR(r) (r)
H
Haojun Liao 已提交
179

X
Xiaoyu Wang 已提交
180
// N.B. If without STSchema, insGetExtendedRowSize() is used to get the rowMaxBytes and
S
Shengliang Guan 已提交
181
// (int32_t)ceil((double)nCols/TD_VTYPE_PARTS) should be added if TD_SUPPORT_BITMAP defined.
H
Hongze Cheng 已提交
182
#define TD_ROW_MAX_BYTES_FROM_SCHEMA(s) ((s)->tlen + TD_ROW_HEAD_LEN)
H
Haojun Liao 已提交
183

C
Cary Xu 已提交
184 185 186 187 188
#define TD_ROW_SET_INFO(r, i)  (TD_ROW_INFO(r) = (i))
#define TD_ROW_SET_TYPE(r, t)  (TD_ROW_TYPE(r) = (t))
#define TD_ROW_SET_DELETE(r)   (TD_ROW_DELETE(r) = 1)
#define TD_ROW_SET_SVER(r, v)  (TD_ROW_SVER(r) = (v))
#define TD_ROW_SET_LEN(r, l)   (TD_ROW_LEN(r) = (l))
H
Haojun Liao 已提交
189 190 191
#define TD_ROW_SET_NCOLS(r, n) (*(col_id_t *)TD_ROW_NCOLS(r) = (n))

#define TD_ROW_IS_DELETED(r) (TD_ROW_DELETE(r) == 1)
C
Cary Xu 已提交
192 193 194 195
#define TD_IS_TP_ROW(r)      (TD_ROW_TYPE(r) == TD_ROW_TP)
#define TD_IS_KV_ROW(r)      (TD_ROW_TYPE(r) == TD_ROW_KV)
#define TD_IS_TP_ROW_T(t)    ((t) == TD_ROW_TP)
#define TD_IS_KV_ROW_T(t)    ((t) == TD_ROW_KV)
H
Haojun Liao 已提交
196

C
Cary Xu 已提交
197
#define TD_BOOL_STR(b)       ((b) ? "true" : "false")
H
Haojun Liao 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
#define isUtilizeKVRow(k, d) ((k) < ((d)*KVRatioConvert))

#define TD_ROW_COL_IDX(r) POINTER_SHIFT(TD_ROW_DATA(r), sizeof(col_id_t))

static FORCE_INLINE void tdRowSetVal(SCellVal *pVal, uint8_t valType, void *val) {
  pVal->valType = valType;
  pVal->val = val;
}
static FORCE_INLINE col_id_t    tdRowGetNCols(STSRow *pRow) { return *(col_id_t *)TD_ROW_NCOLS(pRow); }
static FORCE_INLINE void        tdRowCpy(void *dst, const STSRow *pRow) { memcpy(dst, pRow, TD_ROW_LEN(pRow)); }
static FORCE_INLINE const char *tdRowEnd(STSRow *pRow) { return (const char *)POINTER_SHIFT(pRow, TD_ROW_LEN(pRow)); }

STSRow *tdRowDup(STSRow *row);

static FORCE_INLINE SKvRowIdx *tdKvRowColIdxAt(STSRow *pRow, col_id_t idx) {
  return (SKvRowIdx *)TD_ROW_COL_IDX(pRow) + idx;
}
D
stmt  
dapan1121 已提交
215

C
Cary Xu 已提交
216 217 218 219 220 221 222 223
static FORCE_INLINE int16_t tdKvRowColIdAt(STSRow *pRow, col_id_t idx) {
  ASSERT(idx >= 0);
  if (idx == 0) {
    return PRIMARYKEY_TIMESTAMP_COL_ID;
  }

  return ((SKvRowIdx *)TD_ROW_COL_IDX(pRow) + idx - 1)->colId;
}
D
stmt  
dapan1121 已提交
224

H
Haojun Liao 已提交
225 226 227 228
static FORCE_INLINE void *tdKVRowColVal(STSRow *pRow, SKvRowIdx *pIdx) { return POINTER_SHIFT(pRow, pIdx->offset); }

#define TD_ROW_OFFSET(p) ((p)->toffset);  // During ParseInsert when without STSchema, how to get the offset for STpRow?

C
Cary Xu 已提交
229
void                        tdMergeBitmap(uint8_t *srcBitmap, int32_t nBits, uint8_t *dstBitmap);
H
Haojun Liao 已提交
230
static FORCE_INLINE void    tdRowCopy(void *dst, STSRow *row) { memcpy(dst, row, TD_ROW_LEN(row)); }
C
Cary Xu 已提交
231 232 233
static FORCE_INLINE int32_t tdSetBitmapValTypeII(void *pBitmap, int16_t colIdx, TDRowValT valType);
static FORCE_INLINE int32_t tdSetBitmapValType(void *pBitmap, int16_t colIdx, TDRowValT valType, int8_t bitmapMode);
int32_t                     tdSetBitmapValTypeN(void *pBitmap, int16_t nEle, TDRowValT valType, int8_t bitmapMode);
C
Cary Xu 已提交
234 235 236
static FORCE_INLINE int32_t tdGetBitmapValType(const void *pBitmap, int16_t colIdx, TDRowValT *pValType,
                                               int8_t bitmapMode);
bool                        tdIsBitmapBlkNorm(const void *pBitmap, int32_t numOfBits, int8_t bitmapMode);
H
Hongze Cheng 已提交
237 238 239 240
// int32_t tdAppendValToDataCol(SDataCol *pCol, TDRowValT valType, const void *val, int32_t numOfRows, int32_t
// maxPoints,
//                              int8_t bitmapMode, bool isMerge);
// int32_t tdAppendSTSRowToDataCol(STSRow *pRow, STSchema *pSchema, SDataCols *pCols, bool isMerge);
H
Hongze Cheng 已提交
241 242 243 244

int32_t tdGetBitmapValTypeII(const void *pBitmap, int16_t colIdx, TDRowValT *pValType);
int32_t tdSetBitmapValTypeI(void *pBitmap, int16_t colIdx, TDRowValT valType);
int32_t tdGetBitmapValTypeI(const void *pBitmap, int16_t colIdx, TDRowValT *pValType);
H
Haojun Liao 已提交
245 246 247 248 249 250 251 252 253 254

/**
 * @brief
 *
 * @param pRow
 * @param flen flen in STSchema
 * @return FORCE_INLINE*
 */
static FORCE_INLINE void *tdGetBitmapAddrTp(STSRow *pRow, uint32_t flen) {
  // The primary TS key is stored separatedly.
H
Hongze Cheng 已提交
255
  return POINTER_SHIFT(TD_ROW_DATA(pRow), flen);
H
Haojun Liao 已提交
256 257 258 259 260
  // return POINTER_SHIFT(pRow->ts, flen);
}

static FORCE_INLINE void *tdGetBitmapAddrKv(STSRow *pRow, col_id_t nKvCols) {
  // The primary TS key is stored separatedly and is Norm value, thus should minus 1 firstly
C
Cary Xu 已提交
261
  return POINTER_SHIFT(TD_ROW_COL_IDX(pRow), (--nKvCols) * sizeof(SKvRowIdx));
H
Haojun Liao 已提交
262
}
H
Hongze Cheng 已提交
263 264 265
void   *tdGetBitmapAddr(STSRow *pRow, uint8_t rowType, uint32_t flen, col_id_t nKvCols);
int32_t tdSetBitmapValType(void *pBitmap, int16_t colIdx, TDRowValT valType, int8_t bitmapMode);
int32_t tdSetBitmapValTypeII(void *pBitmap, int16_t colIdx, TDRowValT valType);
C
Cary Xu 已提交
266
// bool    tdIsBitmapValTypeNorm(const void *pBitmap, int16_t idx, int8_t bitmapMode);
H
Hongze Cheng 已提交
267
int32_t tdGetBitmapValType(const void *pBitmap, int16_t colIdx, TDRowValT *pValType, int8_t bitmapMode);
C
Cary Xu 已提交
268

H
Haojun Liao 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
// ----------------- Tuple row structure(STpRow)
/*
 * |<----------------------------- tlen ---------------------------------->|
 * |<---------   flen  ------------->|<-- blen  -->|                       |
 * +---------------------------------+-------------+-----------------------+
 * |                                 |             |                       |
 * +---------------------------------+-------------------------------------+
 * |           first part            |   bitmap    |    second part        |
 * +---------------------------------+-------------+-----------------------+
 *
 */

// -----------------  K-V row structure(SKvRow)
/*
 * |<--------- colsIdxLen ---------->|<-- blen -->|                        |
 * +---------------------------------+------------+------------------------+
 * |                                 |            |                        |
 * +-----------------------------------------------------------------------+
 * |           cols index            |   bitmap   |     data part          |
 * +---------------------------------+------------+------------------------+
 *
 */

static FORCE_INLINE void tdSRowInit(SRowBuilder *pBuilder, int16_t sver) {
  pBuilder->rowType = TD_ROW_TP;  // default STpRow
  pBuilder->sver = sver;
}
H
Hongze Cheng 已提交
296 297 298 299 300
int32_t                  tdSRowSetInfo(SRowBuilder *pBuilder, int32_t nCols, int32_t nBoundCols, int32_t flen);
int32_t                  tdSRowSetTpInfo(SRowBuilder *pBuilder, int32_t nCols, int32_t flen);
int32_t                  tdSRowSetExtendedInfo(SRowBuilder *pBuilder, int32_t nCols, int32_t nBoundCols, int32_t flen,
                                               int32_t allNullLen, int32_t boundNullLen);
int32_t                  tdSRowResetBuf(SRowBuilder *pBuilder, void *pBuf);
C
Cary Xu 已提交
301 302
static FORCE_INLINE void tdSRowEnd(SRowBuilder *pBuilder) {
  STSRow *pRow = (STSRow *)pBuilder->pBuf;
C
Cary Xu 已提交
303
  if (pBuilder->hasNull || pBuilder->hasNone) {
C
Cary Xu 已提交
304 305 306
    pRow->statis = 1;
  }
}
H
Hongze Cheng 已提交
307 308 309 310 311 312 313 314 315 316 317
int32_t tdSRowGetBuf(SRowBuilder *pBuilder, void *pBuf);
void    tdSRowReset(SRowBuilder *pBuilder);
int32_t tdAppendColValToTpRow(SRowBuilder *pBuilder, TDRowValT valType, const void *val, bool isCopyVarData,
                              int8_t colType, int16_t colIdx, int32_t offset);
int32_t tdAppendColValToKvRow(SRowBuilder *pBuilder, TDRowValT valType, const void *val, bool isCopyVarData,
                              int8_t colType, int16_t colIdx, int32_t offset, col_id_t colId);
int32_t tdAppendColValToRow(SRowBuilder *pBuilder, col_id_t colId, int8_t colType, TDRowValT valType, const void *val,
                            bool isCopyVarData, int32_t offset, col_id_t colIdx);
int32_t tdGetTpRowValOfCol(SCellVal *output, STSRow *pRow, void *pBitmap, int8_t colType, int32_t offset,
                           int16_t colIdx);
int32_t tdGetKvRowValOfCol(SCellVal *output, STSRow *pRow, void *pBitmap, int32_t offset, int16_t colIdx);
H
Hongze Cheng 已提交
318
void    tTSRowGetVal(STSRow *pRow, STSchema *pTSchema, int16_t iCol, SColVal *pColVal);
H
Haojun Liao 已提交
319 320 321

typedef struct {
  STSchema *pSchema;
L
fix  
Liu Jicong 已提交
322 323
  STSRow   *pRow;
  void     *pBitmap;
H
Haojun Liao 已提交
324 325 326 327 328 329
  uint32_t  offset;
  col_id_t  maxColId;
  col_id_t  colIdx;  // [PRIMARYKEY_TIMESTAMP_COL_ID, nSchemaCols], PRIMARYKEY_TIMESTAMP_COL_ID equals 1
  col_id_t  kvIdx;   // [0, nKvCols)
} STSRowIter;

C
Cary Xu 已提交
330 331 332 333 334
void tdSTSRowIterInit(STSRowIter *pIter, STSchema *pSchema);
void tdSTSRowIterReset(STSRowIter *pIter, STSRow *pRow);
bool tdSTSRowIterFetch(STSRowIter *pIter, col_id_t colId, col_type_t colType, SCellVal *pVal);
bool tdSTSRowIterNext(STSRowIter *pIter, SCellVal *pVal);

335
int32_t tdSTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow **ppRow);
C
Cary Xu 已提交
336
bool    tdSTSRowGetVal(STSRowIter *pIter, col_id_t colId, col_type_t colType, SCellVal *pVal);
H
Hongze Cheng 已提交
337
void    tdSRowPrint(STSRow *row, STSchema *pSchema, const char *tag);
H
refact  
Hongze Cheng 已提交
338

H
refact  
Hongze Cheng 已提交
339 340 341 342
#ifdef __cplusplus
}
#endif

C
Cary Xu 已提交
343
#endif /*_TD_COMMON_ROW_H_*/