tdataformat.h 31.4 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/>.
 */
H
hzcheng 已提交
15
#ifndef _TD_DATA_FORMAT_H_
H
more  
Hongze Cheng 已提交
16 17
#define _TD_DATA_FORMAT_H_

S
TD-4088  
Shengliang Guan 已提交
18
#include "os.h"
H
Hongze Cheng 已提交
19
#include "talgo.h"
H
Haojun Liao 已提交
20
#include "ttype.h"
H
TD-166  
hzcheng 已提交
21
#include "tutil.h"
H
hzcheng 已提交
22

H
more  
hzcheng 已提交
23 24 25
#ifdef __cplusplus
extern "C" {
#endif
H
hzcheng 已提交
26

27 28 29 30 31
#define STR_TO_VARSTR(x, str)                     \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = __len;                  \
    memcpy(varDataVal(x), (str), __len);          \
H
Hongze Cheng 已提交
32 33
  } while (0);

D
dapan1121 已提交
34 35 36 37 38 39 40 41
#define STR_TO_NET_VARSTR(x, str)                   \
    do {                                            \
      VarDataLenT __len = (VarDataLenT)strlen(str); \
      *(VarDataLenT *)(x) = htons(__len);           \
      memcpy(varDataVal(x), (str), __len);          \
    } while (0);


42 43
#define STR_WITH_MAXSIZE_TO_VARSTR(x, str, _maxs)                         \
  do {                                                                    \
H
Hui Li 已提交
44
    char *_e = stpncpy(varDataVal(x), (str), (_maxs)-VARSTR_HEADER_SIZE); \
45
    varDataSetLen(x, (_e - (x)-VARSTR_HEADER_SIZE));                      \
H
Hongze Cheng 已提交
46 47
  } while (0)

48 49 50 51
#define STR_WITH_SIZE_TO_VARSTR(x, str, _size)  \
  do {                                          \
    *(VarDataLenT *)(x) = (VarDataLenT)(_size); \
    memcpy(varDataVal(x), (str), (_size));      \
H
Hongze Cheng 已提交
52
  } while (0);
H
hjxilinx 已提交
53

H
hzcheng 已提交
54 55
// ----------------- TSDB COLUMN DEFINITION
typedef struct {
C
Cary Xu 已提交
56 57
  int8_t   type;    // Column type
  int16_t  colId;   // column ID
58
  int16_t  bytes;   // column bytes (restore to int16_t in case of misuse)
C
Cary Xu 已提交
59
  uint16_t offset;  // point offset in SDataRow after the header part.
H
hzcheng 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
} STColumn;

#define colType(col) ((col)->type)
#define colColId(col) ((col)->colId)
#define colBytes(col) ((col)->bytes)
#define colOffset(col) ((col)->offset)

#define colSetType(col, t) (colType(col) = (t))
#define colSetColId(col, id) (colColId(col) = (id))
#define colSetBytes(col, b) (colBytes(col) = (b))
#define colSetOffset(col, o) (colOffset(col) = (o))

// ----------------- TSDB SCHEMA DEFINITION
typedef struct {
H
Hongze Cheng 已提交
74
  int      version;    // version
H
hzcheng 已提交
75
  int      numOfCols;  // Number of columns appended
H
refact  
Hongze Cheng 已提交
76
  int      tlen;       // maximum length of a SDataRow without the header part (sizeof(VarDataOffsetT) + sizeof(VarDataLenT) + (bytes))
H
TD-353  
Hongze Cheng 已提交
77
  uint16_t flen;       // First part length in a SDataRow after the header part
H
refact  
Hongze Cheng 已提交
78
  uint16_t vlen;       // pure value part length, excluded the overhead (bytes only)
H
hzcheng 已提交
79 80 81 82
  STColumn columns[];
} STSchema;

#define schemaNCols(s) ((s)->numOfCols)
H
Hongze Cheng 已提交
83
#define schemaVersion(s) ((s)->version)
H
TD-166  
hzcheng 已提交
84 85
#define schemaTLen(s) ((s)->tlen)
#define schemaFLen(s) ((s)->flen)
T
Tao Liu 已提交
86
#define schemaVLen(s) ((s)->vlen)
H
hzcheng 已提交
87
#define schemaColAt(s, i) ((s)->columns + i)
S
TD-1848  
Shengliang Guan 已提交
88
#define tdFreeSchema(s) tfree((s))
H
hzcheng 已提交
89 90

STSchema *tdDupSchema(STSchema *pSchema);
H
TD-353  
Hongze Cheng 已提交
91
int       tdEncodeSchema(void **buf, STSchema *pSchema);
H
TD-353  
Hongze Cheng 已提交
92
void *    tdDecodeSchema(void *buf, STSchema **pRSchema);
H
hzcheng 已提交
93

H
Hongze Cheng 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static FORCE_INLINE int comparColId(const void *key1, const void *key2) {
  if (*(int16_t *)key1 > ((STColumn *)key2)->colId) {
    return 1;
  } else if (*(int16_t *)key1 < ((STColumn *)key2)->colId) {
    return -1;
  } else {
    return 0;
  }
}

static FORCE_INLINE STColumn *tdGetColOfID(STSchema *pSchema, int16_t colId) {
  void *ptr = bsearch(&colId, (void *)pSchema->columns, schemaNCols(pSchema), sizeof(STColumn), comparColId);
  if (ptr == NULL) return NULL;
  return (STColumn *)ptr;
}

H
Hongze Cheng 已提交
110 111 112 113 114
// ----------------- SCHEMA BUILDER DEFINITION
typedef struct {
  int       tCols;
  int       nCols;
  int       tlen;
H
TD-353  
Hongze Cheng 已提交
115 116
  uint16_t  flen;
  uint16_t  vlen;
H
Hongze Cheng 已提交
117 118 119 120 121 122 123
  int       version;
  STColumn *columns;
} STSchemaBuilder;

int       tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, int32_t version);
void      tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder);
void      tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, int32_t version);
124
int       tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int16_t colId, int16_t bytes);
H
Hongze Cheng 已提交
125 126
STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder);

H
TD-1548  
Hongze Cheng 已提交
127 128 129 130 131
// ----------------- Semantic timestamp key definition
typedef uint64_t TKEY;

#define TKEY_INVALID UINT64_MAX
#define TKEY_NULL TKEY_INVALID
H
Hongze Cheng 已提交
132 133
#define TKEY_NEGATIVE_FLAG (((TKEY)1) << 63)
#define TKEY_DELETE_FLAG (((TKEY)1) << 62)
H
TD-1548  
Hongze Cheng 已提交
134 135 136 137 138 139 140 141
#define TKEY_VALUE_FILTER (~(TKEY_NEGATIVE_FLAG | TKEY_DELETE_FLAG))

#define TKEY_IS_NEGATIVE(tkey) (((tkey)&TKEY_NEGATIVE_FLAG) != 0)
#define TKEY_IS_DELETED(tkey) (((tkey)&TKEY_DELETE_FLAG) != 0)
#define tdSetTKEYDeleted(tkey) ((tkey) | TKEY_DELETE_FLAG)
#define tdGetTKEY(key) (((TKEY)ABS(key)) | (TKEY_NEGATIVE_FLAG & (TKEY)(key)))
#define tdGetKey(tkey) (((TSKEY)((tkey)&TKEY_VALUE_FILTER)) * (TKEY_IS_NEGATIVE(tkey) ? -1 : 1))

D
dapan1121 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#define MIN_TS_KEY ((TSKEY)0x8000000000000001)
#define MAX_TS_KEY ((TSKEY)0x3fffffffffffffff)

#define TD_TO_TKEY(key) tdGetTKEY(((key) < MIN_TS_KEY) ? MIN_TS_KEY : (((key) > MAX_TS_KEY) ? MAX_TS_KEY : key))

static FORCE_INLINE TKEY keyToTkey(TSKEY key) {
  TSKEY lkey = key;
  if (key > MAX_TS_KEY) {
    lkey = MAX_TS_KEY;
  } else if (key < MIN_TS_KEY) {
    lkey = MIN_TS_KEY;
  }

  return tdGetTKEY(lkey);
}

H
TD-1548  
Hongze Cheng 已提交
158 159 160 161 162 163 164 165 166 167 168 169
static FORCE_INLINE int tkeyComparFn(const void *tkey1, const void *tkey2) {
  TSKEY key1 = tdGetKey(*(TKEY *)tkey1);
  TSKEY key2 = tdGetKey(*(TKEY *)tkey2);

  if (key1 < key2) {
    return -1;
  } else if (key1 > key2) {
    return 1;
  } else {
    return 0;
  }
}
H
hzcheng 已提交
170

C
Cary Xu 已提交
171 172 173
// ----------------- Data row structure

/* A data row, the format is like below:
K
kailixu 已提交
174
 * |<------------------------------------------------ len ---------------------------------->|
C
Cary Xu 已提交
175 176 177 178 179 180 181 182 183 184 185
 * |<--     Head      -->|<---------   flen -------------->|                                 |
 * +---------------------+---------------------------------+---------------------------------+
 * | uint16_t |  int16_t |                                 |                                 |
 * +----------+----------+---------------------------------+---------------------------------+
 * |   len    | sversion |           First part            |             Second part         |
 * +----------+----------+---------------------------------+---------------------------------+
 *
 * NOTE: timestamp in this row structure is TKEY instead of TSKEY
 */
typedef void *SDataRow;

B
Bomin Zhang 已提交
186
#define TD_DATA_ROW_HEAD_SIZE (sizeof(uint16_t) + sizeof(int16_t))
H
hzcheng 已提交
187

C
Cary Xu 已提交
188
#define dataRowLen(r) (*(TDRowLenT *)(r))  // 0~65535
189
#define dataRowEnd(r) POINTER_SHIFT(r, dataRowLen(r))
C
Cary Xu 已提交
190
#define dataRowVersion(r) (*(int16_t *)POINTER_SHIFT(r, sizeof(int16_t)))
H
hzcheng 已提交
191
#define dataRowTuple(r) POINTER_SHIFT(r, TD_DATA_ROW_HEAD_SIZE)
H
TD-1548  
Hongze Cheng 已提交
192 193
#define dataRowTKey(r) (*(TKEY *)(dataRowTuple(r)))
#define dataRowKey(r) tdGetKey(dataRowTKey(r))
H
hzcheng 已提交
194
#define dataRowSetLen(r, l) (dataRowLen(r) = (l))
H
TD-90  
Hongze Cheng 已提交
195
#define dataRowSetVersion(r, v) (dataRowVersion(r) = (v))
H
hzcheng 已提交
196
#define dataRowCpy(dst, r) memcpy((dst), (r), dataRowLen(r))
H
TD-166  
hzcheng 已提交
197
#define dataRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_DATA_ROW_HEAD_SIZE)
H
TD-1548  
Hongze Cheng 已提交
198
#define dataRowDeleted(r) TKEY_IS_DELETED(dataRowTKey(r))
H
hzcheng 已提交
199

C
Cary Xu 已提交
200 201
SDataRow tdNewDataRowFromSchema(STSchema *pSchema);
void     tdFreeDataRow(SDataRow row);
H
TD-166  
hzcheng 已提交
202
void     tdInitDataRow(SDataRow row, STSchema *pSchema);
C
Cary Xu 已提交
203
SDataRow tdDataRowDup(SDataRow row);
H
more  
Hongze Cheng 已提交
204

205

206
// offset here not include dataRow header length
207 208
static FORCE_INLINE int tdAppendDataColVal(SDataRow row, const void *value, bool isCopyVarData, int8_t type,
                                           int32_t offset) {
209 210 211
  ASSERT(value != NULL);
  int32_t toffset = offset + TD_DATA_ROW_HEAD_SIZE;

H
TD-1548  
Hongze Cheng 已提交
212 213
  if (IS_VAR_DATA_TYPE(type)) {
    *(VarDataOffsetT *)POINTER_SHIFT(row, toffset) = dataRowLen(row);
214 215 216
    if (isCopyVarData) {
      memcpy(POINTER_SHIFT(row, dataRowLen(row)), value, varDataTLen(value));
    }
H
TD-1548  
Hongze Cheng 已提交
217 218 219 220 221
    dataRowLen(row) += varDataTLen(value);
  } else {
    if (offset == 0) {
      ASSERT(type == TSDB_DATA_TYPE_TIMESTAMP);
      TKEY tvalue = tdGetTKEY(*(TSKEY *)value);
C
Cary Xu 已提交
222
      memcpy(POINTER_SHIFT(row, toffset), (const void *)(&tvalue), TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
223
    } else {
224
      memcpy(POINTER_SHIFT(row, toffset), value, TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
225
    }
226 227 228 229 230
  }

  return 0;
}

231 232 233 234 235 236

// offset here not include dataRow header length
static FORCE_INLINE int tdAppendColVal(SDataRow row, const void *value, int8_t type, int32_t offset) {
  return tdAppendDataColVal(row, value, true, type, offset);
}

C
Cary Xu 已提交
237
// NOTE: offset here including the header size
C
Cary Xu 已提交
238
static FORCE_INLINE void *tdGetRowDataOfCol(SDataRow row, int8_t type, int32_t offset) {
C
Cary Xu 已提交
239 240 241 242 243 244 245
  if (IS_VAR_DATA_TYPE(type)) {
    return POINTER_SHIFT(row, *(VarDataOffsetT *)POINTER_SHIFT(row, offset));
  } else {
    return POINTER_SHIFT(row, offset);
  }
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
static FORCE_INLINE void *tdGetPtrToCol(SDataRow row, STSchema *pSchema, int idx) {
  return POINTER_SHIFT(row, TD_DATA_ROW_HEAD_SIZE + pSchema->columns[idx].offset);
}

static FORCE_INLINE void *tdGetColOfRowBySchema(SDataRow row, STSchema *pSchema, int idx) {
  int16_t offset = TD_DATA_ROW_HEAD_SIZE + pSchema->columns[idx].offset;
  int8_t type = pSchema->columns[idx].type;

  return tdGetRowDataOfCol(row, type, offset);
}

static FORCE_INLINE bool tdIsColOfRowNullBySchema(SDataRow row, STSchema *pSchema, int idx) {
  int16_t offset = TD_DATA_ROW_HEAD_SIZE + pSchema->columns[idx].offset;
  int8_t type = pSchema->columns[idx].type;

  return isNull(tdGetRowDataOfCol(row, type, offset), type);
}

static FORCE_INLINE void tdSetColOfRowNullBySchema(SDataRow row, STSchema *pSchema, int idx) {
  int16_t offset = TD_DATA_ROW_HEAD_SIZE + pSchema->columns[idx].offset;
  int8_t type = pSchema->columns[idx].type;
  int16_t bytes = pSchema->columns[idx].bytes;

  setNull(tdGetRowDataOfCol(row, type, offset), type, bytes);
}

static FORCE_INLINE void tdCopyColOfRowBySchema(SDataRow dst, STSchema *pDstSchema, int dstIdx, SDataRow src, STSchema *pSrcSchema, int srcIdx) {
  int8_t type = pDstSchema->columns[dstIdx].type;
  ASSERT(type == pSrcSchema->columns[srcIdx].type);
  void *pData = tdGetPtrToCol(dst, pDstSchema, dstIdx);
  void *value = tdGetPtrToCol(src, pSrcSchema, srcIdx);

  switch (type) {
    case TSDB_DATA_TYPE_BINARY:
    case TSDB_DATA_TYPE_NCHAR:
      *(VarDataOffsetT *)pData = *(VarDataOffsetT *)value;
      pData = POINTER_SHIFT(dst, *(VarDataOffsetT *)pData);
      value = POINTER_SHIFT(src, *(VarDataOffsetT *)value);
      memcpy(pData, value, varDataTLen(value));
      break;
    case TSDB_DATA_TYPE_NULL:
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_UTINYINT:
      *(uint8_t *)pData = *(uint8_t *)value;
      break;
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_USMALLINT:
      *(uint16_t *)pData = *(uint16_t *)value;
      break;
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_UINT:
      *(uint32_t *)pData = *(uint32_t *)value;
      break;
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_UBIGINT:
      *(uint64_t *)pData = *(uint64_t *)value;
      break;
    case TSDB_DATA_TYPE_FLOAT:
      SET_FLOAT_PTR(pData, value);
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      SET_DOUBLE_PTR(pData, value);
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      if (pSrcSchema->columns[srcIdx].colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) {
        *(TSKEY *)pData = tdGetKey(*(TKEY *)value);
      } else {
        *(TSKEY *)pData = *(TSKEY *)value;
      }
      break;
    default:
      memcpy(pData, value, pSrcSchema->columns[srcIdx].bytes);
  }
}


H
TD-34  
hzcheng 已提交
323 324
// ----------------- Data column structure
typedef struct SDataCol {
H
TD-166  
hzcheng 已提交
325 326 327
  int8_t          type;       // column type
  int16_t         colId;      // column ID
  int             bytes;      // column data bytes defined
H
TD-166  
hzcheng 已提交
328
  int             offset;     // data offset in a SDataRow (including the header size)
H
TD-166  
hzcheng 已提交
329 330 331 332
  int             spaceSize;  // Total space size for this column
  int             len;        // column data length
  VarDataOffsetT *dataOff;    // For binary and nchar data, the offset in the data column
  void *          pData;      // Actual data pointer
L
lichuang 已提交
333
  TSKEY           ts;         // only used in last NULL column
H
TD-34  
hzcheng 已提交
334 335
} SDataCol;

K
kailixu 已提交
336
#define isAllRowsNull(pCol) ((pCol)->len == 0)
H
TD-166  
hzcheng 已提交
337 338
static FORCE_INLINE void dataColReset(SDataCol *pDataCol) { pDataCol->len = 0; }

L
Liu Jicong 已提交
339
int tdAllocMemForCol(SDataCol *pCol, int maxPoints);
340

341
void dataColInit(SDataCol *pDataCol, STColumn *pCol, int maxPoints);
342 343 344 345 346 347 348 349 350

// value from timestamp should be TKEY here instead of TSKEY
int dataColAppendValEx(SDataCol *pCol, const void *value, int numOfRows, int maxPoints, bool isAppend);

// value from timestamp should be TKEY here instead of TSKEY
FORCE_INLINE int dataColAppendVal(SDataCol *pCol, const void *value, int numOfRows, int maxPoints) {
  return dataColAppendValEx(pCol, value, numOfRows, maxPoints, true);
}

H
TD-166  
hzcheng 已提交
351 352
void dataColSetOffset(SDataCol *pCol, int nEle);

H
TD-166  
hzcheng 已提交
353
bool isNEleNull(SDataCol *pCol, int nEle);
H
TD-166  
hzcheng 已提交
354 355

// Get the data pointer from a column-wised data
K
kailixu 已提交
356 357
static FORCE_INLINE const void *tdGetColDataOfRow(SDataCol *pCol, int row) {
  if (isAllRowsNull(pCol)) {
C
Cary Xu 已提交
358
    return getNullValue(pCol->type);
K
kailixu 已提交
359
  }
H
Hongze Cheng 已提交
360 361 362 363
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    return POINTER_SHIFT(pCol->pData, pCol->dataOff[row]);
  } else {
    return POINTER_SHIFT(pCol->pData, TYPE_BYTES[pCol->type] * row);
H
TD-166  
hzcheng 已提交
364 365 366
  }
}

H
TD-166  
hzcheng 已提交
367
static FORCE_INLINE int32_t dataColGetNEleLen(SDataCol *pDataCol, int rows) {
H
TD-166  
hzcheng 已提交
368 369
  ASSERT(rows > 0);

H
Hongze Cheng 已提交
370 371 372 373
  if (IS_VAR_DATA_TYPE(pDataCol->type)) {
    return pDataCol->dataOff[rows - 1] + varDataTLen(tdGetColDataOfRow(pDataCol, rows - 1));
  } else {
    return TYPE_BYTES[pDataCol->type] * rows;
H
TD-166  
hzcheng 已提交
374 375 376
  }
}

H
TD-34  
hzcheng 已提交
377
typedef struct {
L
Liu Jicong 已提交
378 379 380 381 382
  int      maxCols;    // max number of columns
  int      maxPoints;  // max number of points
  int      numOfRows;
  int      numOfCols;  // Total number of cols
  int      sversion;   // TODO: set sversion
H
Hongze Cheng 已提交
383
  SDataCol *cols;
H
TD-34  
hzcheng 已提交
384 385
} SDataCols;

H
TD-34  
hzcheng 已提交
386
#define keyCol(pCols) (&((pCols)->cols[0]))  // Key column
C
Cary Xu 已提交
387
#define dataColsTKeyAt(pCols, idx) ((TKEY *)(keyCol(pCols)->pData))[(idx)]  // the idx row of column-wised data
H
TD-1548  
Hongze Cheng 已提交
388
#define dataColsKeyAt(pCols, idx) tdGetKey(dataColsTKeyAt(pCols, idx))
389 390 391 392 393 394 395 396
static FORCE_INLINE TKEY dataColsTKeyFirst(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsTKeyAt(pCols, 0);
  } else {
    return TKEY_INVALID;
  }
}

397 398 399 400 401
static FORCE_INLINE TSKEY dataColsKeyAtRow(SDataCols *pCols, int row) {
  ASSERT(row < pCols->numOfRows);
  return dataColsKeyAt(pCols, row);
}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static FORCE_INLINE TSKEY dataColsKeyFirst(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsKeyAt(pCols, 0);
  } else {
    return TSDB_DATA_TIMESTAMP_NULL;
  }
}

static FORCE_INLINE TKEY dataColsTKeyLast(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsTKeyAt(pCols, pCols->numOfRows - 1);
  } else {
    return TKEY_INVALID;
  }
}

static FORCE_INLINE TSKEY dataColsKeyLast(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsKeyAt(pCols, pCols->numOfRows - 1);
  } else {
    return TSDB_DATA_TIMESTAMP_NULL;
  }
}
H
TD-34  
hzcheng 已提交
425

L
Liu Jicong 已提交
426
SDataCols *tdNewDataCols(int maxCols, int maxRows);
H
TD-34  
hzcheng 已提交
427
void       tdResetDataCols(SDataCols *pCols);
H
Hongze Cheng 已提交
428
int        tdInitDataCols(SDataCols *pCols, STSchema *pSchema);
H
TD-100  
hzcheng 已提交
429
SDataCols *tdDupDataCols(SDataCols *pCols, bool keepData);
H
Hongze Cheng 已提交
430
SDataCols *tdFreeDataCols(SDataCols *pCols);
431
int        tdMergeDataCols(SDataCols *target, SDataCols *source, int rowsToMerge, int *pOffset, bool forceSetNull);
H
more  
Hongze Cheng 已提交
432

H
Hongze Cheng 已提交
433
// ----------------- K-V data row structure
C
Cary Xu 已提交
434 435
/* |<-------------------------------------- len -------------------------------------------->|
 * |<----- header  ----->|<--------------------------- body -------------------------------->|
H
Hongze Cheng 已提交
436
 * +----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
437
 * | uint16_t |  int16_t |                                 |                                 |
H
Hongze Cheng 已提交
438 439 440 441
 * +----------+----------+---------------------------------+---------------------------------+
 * |    len   |   ncols  |           cols index            |             data part           |
 * +----------+----------+---------------------------------+---------------------------------+
 */
H
Hongze Cheng 已提交
442
typedef void *SKVRow;
H
Hongze Cheng 已提交
443 444

typedef struct {
C
Cary Xu 已提交
445 446
  int16_t  colId;
  uint16_t offset;
H
Hongze Cheng 已提交
447 448
} SColIdx;

C
Cary Xu 已提交
449
#define TD_KV_ROW_HEAD_SIZE (sizeof(uint16_t) + sizeof(int16_t))
H
Hongze Cheng 已提交
450

C
Cary Xu 已提交
451
#define kvRowLen(r) (*(TDRowLenT *)(r))
C
Cary Xu 已提交
452
#define kvRowNCols(r) (*(int16_t *)POINTER_SHIFT(r, sizeof(uint16_t)))
H
Hongze Cheng 已提交
453 454
#define kvRowSetLen(r, len) kvRowLen(r) = (len)
#define kvRowSetNCols(r, n) kvRowNCols(r) = (n)
C
Cary Xu 已提交
455
#define kvRowColIdx(r) (SColIdx *)POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE)
H
Hongze Cheng 已提交
456 457 458 459
#define kvRowValues(r) POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE + sizeof(SColIdx) * kvRowNCols(r))
#define kvRowCpy(dst, r) memcpy((dst), (r), kvRowLen(r))
#define kvRowColVal(r, colIdx) POINTER_SHIFT(kvRowValues(r), (colIdx)->offset)
#define kvRowColIdxAt(r, i) (kvRowColIdx(r) + (i))
S
TD-1848  
Shengliang Guan 已提交
460
#define kvRowFree(r) tfree(r)
H
TD-90  
Hongze Cheng 已提交
461
#define kvRowEnd(r) POINTER_SHIFT(r, kvRowLen(r))
462
#define kvRowValLen(r) (kvRowLen(r) - TD_KV_ROW_HEAD_SIZE - sizeof(SColIdx) * kvRowNCols(r))
C
Cary Xu 已提交
463 464 465
#define kvRowTKey(r) (*(TKEY *)(kvRowValues(r)))
#define kvRowKey(r) tdGetKey(kvRowTKey(r))
#define kvRowDeleted(r) TKEY_IS_DELETED(kvRowTKey(r))
H
Hongze Cheng 已提交
466

H
Hongze Cheng 已提交
467
SKVRow tdKVRowDup(SKVRow row);
H
TD-90  
Hongze Cheng 已提交
468
int    tdSetKVRowDataOfCol(SKVRow *orow, int16_t colId, int8_t type, void *value);
H
TD-353  
Hongze Cheng 已提交
469
int    tdEncodeKVRow(void **buf, SKVRow row);
H
Hongze Cheng 已提交
470
void * tdDecodeKVRow(void *buf, SKVRow *row);
B
Bomin Zhang 已提交
471
void   tdSortKVRowByColIdx(SKVRow row);
H
Hongze Cheng 已提交
472 473 474 475 476 477 478 479 480 481 482

static FORCE_INLINE int comparTagId(const void *key1, const void *key2) {
  if (*(int16_t *)key1 > ((SColIdx *)key2)->colId) {
    return 1;
  } else if (*(int16_t *)key1 < ((SColIdx *)key2)->colId) {
    return -1;
  } else {
    return 0;
  }
}

H
Hongze Cheng 已提交
483
static FORCE_INLINE void *tdGetKVRowValOfCol(SKVRow row, int16_t colId) {
H
Hongze Cheng 已提交
484
  void *ret = taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_EQ);
H
Hongze Cheng 已提交
485
  if (ret == NULL) return NULL;
H
Hongze Cheng 已提交
486
  return kvRowColVal(row, (SColIdx *)ret);
H
Hongze Cheng 已提交
487 488
}

L
liuyq-617 已提交
489 490 491 492
static FORCE_INLINE void *tdGetKVRowIdxOfCol(SKVRow row, int16_t colId) {
  return taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_EQ);
}

C
Cary Xu 已提交
493
// offset here not include kvRow header length
494 495
static FORCE_INLINE int tdAppendKvColVal(SKVRow row, const void *value, bool isCopyValData, int16_t colId, int8_t type,
                                         int32_t offset) {
C
Cary Xu 已提交
496
  ASSERT(value != NULL);
497
  int32_t  toffset = offset + TD_KV_ROW_HEAD_SIZE;
C
Cary Xu 已提交
498 499 500 501
  SColIdx *pColIdx = (SColIdx *)POINTER_SHIFT(row, toffset);
  char *   ptr = (char *)POINTER_SHIFT(row, kvRowLen(row));

  pColIdx->colId = colId;
C
Cary Xu 已提交
502
  pColIdx->offset = kvRowLen(row);  // offset of pColIdx including the TD_KV_ROW_HEAD_SIZE
C
Cary Xu 已提交
503 504

  if (IS_VAR_DATA_TYPE(type)) {
505 506 507
    if (isCopyValData) {
      memcpy(ptr, value, varDataTLen(value));
    }
C
Cary Xu 已提交
508 509
    kvRowLen(row) += varDataTLen(value);
  } else {
510
    if (offset == 0) {
C
Cary Xu 已提交
511 512 513 514 515 516 517 518 519 520 521
      ASSERT(type == TSDB_DATA_TYPE_TIMESTAMP);
      TKEY tvalue = tdGetTKEY(*(TSKEY *)value);
      memcpy(ptr, (void *)(&tvalue), TYPE_BYTES[type]);
    } else {
      memcpy(ptr, value, TYPE_BYTES[type]);
    }
    kvRowLen(row) += TYPE_BYTES[type];
  }

  return 0;
}
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
// NOTE: offset here including the header size
static FORCE_INLINE void *tdGetKvRowDataOfCol(void *row, int32_t offset) { return POINTER_SHIFT(row, offset); }

static FORCE_INLINE void *tdGetKVRowValOfColEx(SKVRow row, int16_t colId, int32_t *nIdx) {
  while (*nIdx < kvRowNCols(row)) {
    SColIdx *pColIdx = kvRowColIdxAt(row, *nIdx);
    if (pColIdx->colId == colId) {
      ++(*nIdx);
      return tdGetKvRowDataOfCol(row, pColIdx->offset);
    } else if (pColIdx->colId > colId) {
      return NULL;
    } else {
      ++(*nIdx);
    }
  }
  return NULL;
}
C
Cary Xu 已提交
539

H
Hongze Cheng 已提交
540 541 542 543 544
// ----------------- K-V data row builder
typedef struct {
  int16_t  tCols;
  int16_t  nCols;
  SColIdx *pColIdx;
C
Cary Xu 已提交
545 546
  uint16_t alloc;
  uint16_t size;
H
Hongze Cheng 已提交
547
  void *   buf;
H
Hongze Cheng 已提交
548
} SKVRowBuilder;
H
Hongze Cheng 已提交
549

H
Hongze Cheng 已提交
550 551 552 553
int    tdInitKVRowBuilder(SKVRowBuilder *pBuilder);
void   tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder);
void   tdResetKVRowBuilder(SKVRowBuilder *pBuilder);
SKVRow tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder);
H
Hongze Cheng 已提交
554 555 556 557

static FORCE_INLINE int tdAddColToKVRow(SKVRowBuilder *pBuilder, int16_t colId, int8_t type, void *value) {
  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
T
tickduan 已提交
558 559 560
    SColIdx* pColIdx = (SColIdx *)realloc((void *)(pBuilder->pColIdx), sizeof(SColIdx) * pBuilder->tCols);
    if (pColIdx == NULL) return -1;
    pBuilder->pColIdx = pColIdx;
H
Hongze Cheng 已提交
561 562 563 564 565 566 567 568 569 570 571 572
  }

  pBuilder->pColIdx[pBuilder->nCols].colId = colId;
  pBuilder->pColIdx[pBuilder->nCols].offset = pBuilder->size;

  pBuilder->nCols++;

  int tlen = IS_VAR_DATA_TYPE(type) ? varDataTLen(value) : TYPE_BYTES[type];
  if (tlen > pBuilder->alloc - pBuilder->size) {
    while (tlen > pBuilder->alloc - pBuilder->size) {
      pBuilder->alloc *= 2;
    }
T
tickduan 已提交
573 574 575
    void* buf = realloc(pBuilder->buf, pBuilder->alloc);
    if (buf == NULL) return -1;
    pBuilder->buf = buf;
H
Hongze Cheng 已提交
576 577 578 579 580 581 582
  }

  memcpy(POINTER_SHIFT(pBuilder->buf, pBuilder->size), value, tlen);
  pBuilder->size += tlen;

  return 0;
}
H
Hongze Cheng 已提交
583

C
Cary Xu 已提交
584
// ----------------- SMemRow appended with sequential data row structure
C
Cary Xu 已提交
585
/*
C
Cary Xu 已提交
586
 * |---------|------------------------------------------------- len ---------------------------------->|
C
Cary Xu 已提交
587 588 589 590 591 592
 * |<--------     Head      ------>|<---------   flen -------------->|                                 |
 * |---------+---------------------+---------------------------------+---------------------------------+
 * | uint8_t | uint16_t |  int16_t |                                 |                                 |
 * |---------+----------+----------+---------------------------------+---------------------------------+
 * |  flag   |   len    | sversion |           First part            |             Second part         |
 * +---------+----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
593 594 595
 *
 * NOTE: timestamp in this row structure is TKEY instead of TSKEY
 */
C
Cary Xu 已提交
596

C
Cary Xu 已提交
597
// ----------------- SMemRow appended with extended K-V data row structure
C
Cary Xu 已提交
598 599
/* |--------------------|------------------------------------------------  len ---------------------------------->|
 * |<-------------     Head      ------------>|<---------   flen -------------->|                                 |
C
Cary Xu 已提交
600 601 602 603 604
 * |--------------------+----------+--------------------------------------------+---------------------------------+
 * | uint8_t | int16_t  | uint16_t |  int16_t |                                 |                                 |
 * |---------+----------+----------+----------+---------------------------------+---------------------------------+
 * |   flag  | sversion |   len    |   ncols  |           cols index            |             data part           |
 * |---------+----------+----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
605 606
 */

C
Cary Xu 已提交
607 608
typedef void *SMemRow;

C
Cary Xu 已提交
609
#define TD_MEM_ROW_TYPE_SIZE sizeof(uint8_t)
C
Cary Xu 已提交
610 611 612
#define TD_MEM_ROW_KV_VER_SIZE sizeof(int16_t)
#define TD_MEM_ROW_KV_TYPE_VER_SIZE (TD_MEM_ROW_TYPE_SIZE + TD_MEM_ROW_KV_VER_SIZE)
#define TD_MEM_ROW_DATA_HEAD_SIZE (TD_MEM_ROW_TYPE_SIZE + TD_DATA_ROW_HEAD_SIZE)
613
#define TD_MEM_ROW_KV_HEAD_SIZE (TD_MEM_ROW_TYPE_SIZE + TD_MEM_ROW_KV_VER_SIZE + TD_KV_ROW_HEAD_SIZE)
C
Cary Xu 已提交
614

615 616 617 618 619 620 621 622 623
#define SMEM_ROW_DATA 0x0U      // SDataRow
#define SMEM_ROW_KV 0x01U       // SKVRow
#define SMEM_ROW_CONVERT 0x80U  // SMemRow convert flag

#define KVRatioKV (0.2f)  // all bool
#define KVRatioPredict (0.4f)
#define KVRatioData (0.75f)  // all bigint
#define KVRatioConvert (0.9f)

C
Cary Xu 已提交
624
#define memRowType(r) ((*(uint8_t *)(r)) & 0x01)
C
Cary Xu 已提交
625

C
bug fix  
Cary Xu 已提交
626
#define memRowSetType(r, t) ((*(uint8_t *)(r)) = (t))  // set the total byte in case of dirty memory
627 628
#define memRowSetConvert(r) ((*(uint8_t *)(r)) = (((*(uint8_t *)(r)) & 0x7F) | SMEM_ROW_CONVERT))  // highest bit
#define isDataRowT(t) (SMEM_ROW_DATA == (((uint8_t)(t)) & 0x01))
C
Cary Xu 已提交
629
#define isDataRow(r) (SMEM_ROW_DATA == memRowType(r))
630
#define isKvRowT(t) (SMEM_ROW_KV == (((uint8_t)(t)) & 0x01))
C
Cary Xu 已提交
631
#define isKvRow(r) (SMEM_ROW_KV == memRowType(r))
C
Cary Xu 已提交
632
#define isNeedConvertRow(r) (((*(uint8_t *)(r)) & 0x80) == SMEM_ROW_CONVERT)
C
Cary Xu 已提交
633

C
Cary Xu 已提交
634 635
#define memRowDataBody(r) POINTER_SHIFT(r, TD_MEM_ROW_TYPE_SIZE)  // section after flag
#define memRowKvBody(r) \
C
Cary Xu 已提交
636 637 638 639
  POINTER_SHIFT(r, TD_MEM_ROW_KV_TYPE_VER_SIZE)  // section after flag + sversion as to reuse SKVRow

#define memRowDataLen(r) (*(TDRowLenT *)memRowDataBody(r))  //  0~65535
#define memRowKvLen(r) (*(TDRowLenT *)memRowKvBody(r))      //  0~65535
C
Cary Xu 已提交
640

K
kailixu 已提交
641 642
#define memRowDataTLen(r) \
  ((TDRowTLenT)(memRowDataLen(r) + TD_MEM_ROW_TYPE_SIZE))  // using uint32_t/int32_t to store the TLen
C
Cary Xu 已提交
643

K
kailixu 已提交
644
#define memRowKvTLen(r) ((TDRowTLenT)(memRowKvLen(r) + TD_MEM_ROW_KV_TYPE_VER_SIZE))
C
Cary Xu 已提交
645 646

#define memRowLen(r) (isDataRow(r) ? memRowDataLen(r) : memRowKvLen(r))
C
Cary Xu 已提交
647
#define memRowTLen(r) (isDataRow(r) ? memRowDataTLen(r) : memRowKvTLen(r))  // using uint32_t/int32_t to store the TLen
C
Cary Xu 已提交
648

649 650 651 652 653 654 655 656
static FORCE_INLINE char *memRowEnd(SMemRow row) {
  if (isDataRow(row)) {
    return (char *)dataRowEnd(memRowDataBody(row));
  } else {
    return (char *)kvRowEnd(memRowKvBody(row));
  }
}

C
Cary Xu 已提交
657 658 659
#define memRowDataVersion(r) dataRowVersion(memRowDataBody(r))
#define memRowKvVersion(r) (*(int16_t *)POINTER_SHIFT(r, TD_MEM_ROW_TYPE_SIZE))
#define memRowVersion(r) (isDataRow(r) ? memRowDataVersion(r) : memRowKvVersion(r))  // schema version
C
Cary Xu 已提交
660
#define memRowSetKvVersion(r, v) (memRowKvVersion(r) = (v))
C
Cary Xu 已提交
661
#define memRowTuple(r) (isDataRow(r) ? dataRowTuple(memRowDataBody(r)) : kvRowValues(memRowKvBody(r)))
C
Cary Xu 已提交
662

C
Cary Xu 已提交
663 664
#define memRowTKey(r) (isDataRow(r) ? dataRowTKey(memRowDataBody(r)) : kvRowTKey(memRowKvBody(r)))
#define memRowKey(r) (isDataRow(r) ? dataRowKey(memRowDataBody(r)) : kvRowKey(memRowKvBody(r)))
C
Cary Xu 已提交
665 666 667 668 669 670 671 672
#define memRowSetTKey(r, k)                 \
  do {                                      \
    if (isDataRow(r)) {                     \
      dataRowTKey(memRowDataBody(r)) = (k); \
    } else {                                \
      kvRowTKey(memRowKvBody(r)) = (k);     \
    }                                       \
  } while (0)
C
Cary Xu 已提交
673

C
Cary Xu 已提交
674
#define memRowSetLen(r, l) (isDataRow(r) ? memRowDataLen(r) = (l) : memRowKvLen(r) = (l))
675
#define memRowSetVersion(r, v) (isDataRow(r) ? dataRowSetVersion(memRowDataBody(r), v) : memRowSetKvVersion(r, v))
C
Cary Xu 已提交
676
#define memRowCpy(dst, r) memcpy((dst), (r), memRowTLen(r))
C
Cary Xu 已提交
677
#define memRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_MEM_ROW_DATA_HEAD_SIZE)
C
Cary Xu 已提交
678 679
#define memRowDeleted(r) TKEY_IS_DELETED(memRowTKey(r))

C
Cary Xu 已提交
680
SMemRow tdMemRowDup(SMemRow row);
681
void    tdAppendMemRowToDataCol(SMemRow row, STSchema *pSchema, SDataCols *pCols, bool forceSetNull, bool isAppend);
682

C
Cary Xu 已提交
683
// NOTE: offset here including the header size
684
static FORCE_INLINE void *tdGetMemRowDataOfCol(void *row, int16_t colId, int8_t colType, uint16_t offset) {
C
Cary Xu 已提交
685
  if (isDataRow(row)) {
686
    return tdGetRowDataOfCol(memRowDataBody(row), colType, offset);
C
Cary Xu 已提交
687
  } else {
688
    return tdGetKVRowValOfCol(memRowKvBody(row), colId);
C
Cary Xu 已提交
689 690 691
  }
}

692 693 694 695 696 697 698 699 700 701 702 703 704 705
/**
 * NOTE:
 *  1. Applicable to scan columns one by one
 *  2. offset here including the header size
 */
static FORCE_INLINE void *tdGetMemRowDataOfColEx(void *row, int16_t colId, int8_t colType, int32_t offset,
                                                 int32_t *kvNIdx) {
  if (isDataRow(row)) {
    return tdGetRowDataOfCol(memRowDataBody(row), colType, offset);
  } else {
    return tdGetKVRowValOfColEx(memRowKvBody(row), colId, kvNIdx);
  }
}

706 707
static FORCE_INLINE int tdAppendMemRowColVal(SMemRow row, const void *value, bool isCopyVarData, int16_t colId,
                                             int8_t type, int32_t offset) {
708
  if (isDataRow(row)) {
709
    tdAppendDataColVal(memRowDataBody(row), value, isCopyVarData, type, offset);
710
  } else {
711
    tdAppendKvColVal(memRowKvBody(row), value, isCopyVarData, colId, type, offset);
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
  }
  return 0;
}

// make sure schema->flen appended for SDataRow
static FORCE_INLINE int32_t tdGetColAppendLen(uint8_t rowType, const void *value, int8_t colType) {
  int32_t len = 0;
  if (IS_VAR_DATA_TYPE(colType)) {
    len += varDataTLen(value);
    if (rowType == SMEM_ROW_KV) {
      len += sizeof(SColIdx);
    }
  } else {
    if (rowType == SMEM_ROW_KV) {
      len += TYPE_BYTES[colType];
      len += sizeof(SColIdx);
    }
  }
  return len;
}

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
/**
 * 1. calculate the delta of AllNullLen for SDataRow.
 * 2. calculate the real len for SKVRow.
 */
static FORCE_INLINE void tdGetColAppendDeltaLen(const void *value, int8_t colType, int32_t *dataLen, int32_t *kvLen) {
  switch (colType) {
    case TSDB_DATA_TYPE_BINARY: {
      int32_t varLen = varDataLen(value);
      *dataLen += (varLen - CHAR_BYTES);
      *kvLen += (varLen + sizeof(SColIdx));
      break;
    }
    case TSDB_DATA_TYPE_NCHAR: {
      int32_t varLen = varDataLen(value);
      *dataLen += (varLen - TSDB_NCHAR_SIZE);
      *kvLen += (varLen + sizeof(SColIdx));
      break;
    }
    default: {
      *kvLen += (TYPE_BYTES[colType] + sizeof(SColIdx));
      break;
    }
  }
}
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

typedef struct {
  int16_t colId;
  uint8_t colType;
  char*   colVal;
} SColInfo;

static FORCE_INLINE void setSColInfo(SColInfo* colInfo, int16_t colId, uint8_t colType, char* colVal) {
  colInfo->colId = colId;
  colInfo->colType = colType;
  colInfo->colVal = colVal;
}

SMemRow mergeTwoMemRows(void *buffer, SMemRow row1, SMemRow row2, STSchema *pSchema1, STSchema *pSchema2);

772
#if 0
C
Cary Xu 已提交
773 774 775 776 777 778 779 780 781 782
// ----------------- Raw payload structure for row:
/* |<------------ Head ------------->|<----------- body of column data tuple ------------------->|
 * |                                 |<----------------- flen ------------->|<--- value part --->|
 * |SMemRowType| dataTLen |  nCols   |  colId  | colType | offset   |  ...  | value |...|...|... |
 * +-----------+----------+----------+--------------------------------------|--------------------|
 * | uint8_t   | uint32_t | uint16_t | int16_t | uint8_t | uint16_t |  ...  |.......|...|...|... |
 * +-----------+----------+----------+--------------------------------------+--------------------|
 *  1. offset in column data tuple starts from the value part in case of uint16_t overflow.
 *  2. dataTLen: total length including the header and body.
 */
C
Cary Xu 已提交
783 784

#define PAYLOAD_NCOLS_LEN sizeof(uint16_t)
C
Cary Xu 已提交
785
#define PAYLOAD_NCOLS_OFFSET (sizeof(uint8_t) + sizeof(TDRowTLenT))
C
Cary Xu 已提交
786 787 788
#define PAYLOAD_HEADER_LEN (PAYLOAD_NCOLS_OFFSET + PAYLOAD_NCOLS_LEN)
#define PAYLOAD_ID_LEN sizeof(int16_t)
#define PAYLOAD_ID_TYPE_LEN (sizeof(int16_t) + sizeof(uint8_t))
C
Cary Xu 已提交
789
#define PAYLOAD_COL_HEAD_LEN (PAYLOAD_ID_TYPE_LEN + sizeof(uint16_t))
C
Cary Xu 已提交
790
#define PAYLOAD_PRIMARY_COL_LEN (PAYLOAD_ID_TYPE_LEN + sizeof(TSKEY))
C
Cary Xu 已提交
791 792 793 794

#define payloadBody(r) POINTER_SHIFT(r, PAYLOAD_HEADER_LEN)
#define payloadType(r) (*(uint8_t *)(r))
#define payloadSetType(r, t) (payloadType(r) = (t))
C
Cary Xu 已提交
795
#define payloadTLen(r) (*(TDRowTLenT *)POINTER_SHIFT(r, TD_MEM_ROW_TYPE_SIZE))  // including total header
C
Cary Xu 已提交
796 797 798
#define payloadSetTLen(r, l) (payloadTLen(r) = (l))
#define payloadNCols(r) (*(TDRowLenT *)POINTER_SHIFT(r, PAYLOAD_NCOLS_OFFSET))
#define payloadSetNCols(r, n) (payloadNCols(r) = (n))
C
Cary Xu 已提交
799 800 801 802 803 804 805 806 807 808 809 810
#define payloadValuesOffset(r) \
  (PAYLOAD_HEADER_LEN + payloadNCols(r) * PAYLOAD_COL_HEAD_LEN)    // avoid using the macro in loop
#define payloadValues(r) POINTER_SHIFT(r, payloadValuesOffset(r))  // avoid using the macro in loop
#define payloadColId(c) (*(int16_t *)(c))
#define payloadColType(c) (*(uint8_t *)POINTER_SHIFT(c, PAYLOAD_ID_LEN))
#define payloadColOffset(c) (*(uint16_t *)POINTER_SHIFT(c, PAYLOAD_ID_TYPE_LEN))
#define payloadColValue(c) POINTER_SHIFT(c, payloadColOffset(c))

#define payloadColSetId(c, i) (payloadColId(c) = (i))
#define payloadColSetType(c, t) (payloadColType(c) = (t))
#define payloadColSetOffset(c, o) (payloadColOffset(c) = (o))

C
Cary Xu 已提交
811 812
#define payloadTSKey(r) (*(TSKEY *)POINTER_SHIFT(r, payloadValuesOffset(r)))
#define payloadTKey(r) (*(TKEY *)POINTER_SHIFT(r, payloadValuesOffset(r)))
C
Cary Xu 已提交
813 814
#define payloadKey(r) tdGetKey(payloadTKey(r))

C
Cary Xu 已提交
815

C
Cary Xu 已提交
816
static FORCE_INLINE char *payloadNextCol(char *pCol) { return (char *)POINTER_SHIFT(pCol, PAYLOAD_COL_HEAD_LEN); }
C
Cary Xu 已提交
817

818 819
#endif

H
more  
hzcheng 已提交
820 821 822 823
#ifdef __cplusplus
}
#endif

824
#endif  // _TD_DATA_FORMAT_H_