tdataformat.h 24.9 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

C
Cary Xu 已提交
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
#pragma pack(push, 1)
typedef struct {
  VarDataLenT len;
  uint8_t     data;
} SBinaryNullT;

typedef struct {
  VarDataLenT len;
  uint32_t    data;
} SNCharNullT;
#pragma pack(pop)

extern const uint8_t      BoolNull;
extern const uint8_t      TinyintNull;
extern const uint16_t     SmallintNull;
extern const uint32_t     IntNull;
extern const uint64_t     BigintNull;
extern const uint64_t     TimestampNull;
extern const uint8_t      UTinyintNull;
extern const uint16_t     USmallintNull;
extern const uint32_t     UIntNull;
extern const uint64_t     UBigintNull;
extern const uint32_t     FloatNull;
extern const uint64_t     DoubleNull;
extern const SBinaryNullT BinaryNull;
extern const SNCharNullT  NcharNull;

54 55 56 57 58
#define STR_TO_VARSTR(x, str)                     \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = __len;                  \
    memcpy(varDataVal(x), (str), __len);          \
H
Hongze Cheng 已提交
59 60
  } while (0);

61 62
#define STR_WITH_MAXSIZE_TO_VARSTR(x, str, _maxs)                         \
  do {                                                                    \
H
Hui Li 已提交
63
    char *_e = stpncpy(varDataVal(x), (str), (_maxs)-VARSTR_HEADER_SIZE); \
64
    varDataSetLen(x, (_e - (x)-VARSTR_HEADER_SIZE));                      \
H
Hongze Cheng 已提交
65 66
  } while (0)

67 68 69 70
#define STR_WITH_SIZE_TO_VARSTR(x, str, _size)  \
  do {                                          \
    *(VarDataLenT *)(x) = (VarDataLenT)(_size); \
    memcpy(varDataVal(x), (str), (_size));      \
H
Hongze Cheng 已提交
71
  } while (0);
H
hjxilinx 已提交
72

H
hzcheng 已提交
73 74
// ----------------- TSDB COLUMN DEFINITION
typedef struct {
C
Cary Xu 已提交
75 76 77
  int8_t   type;    // Column type
  int16_t  colId;   // column ID
  uint16_t bytes;   // column bytes
C
Cary Xu 已提交
78
  uint16_t offset;  // point offset in SDataRow after the header part.
H
hzcheng 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92
} 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 已提交
93
  int      version;    // version
H
hzcheng 已提交
94
  int      numOfCols;  // Number of columns appended
H
refact  
Hongze Cheng 已提交
95
  int      tlen;       // maximum length of a SDataRow without the header part (sizeof(VarDataOffsetT) + sizeof(VarDataLenT) + (bytes))
H
TD-353  
Hongze Cheng 已提交
96
  uint16_t flen;       // First part length in a SDataRow after the header part
H
refact  
Hongze Cheng 已提交
97
  uint16_t vlen;       // pure value part length, excluded the overhead (bytes only)
H
hzcheng 已提交
98 99 100 101
  STColumn columns[];
} STSchema;

#define schemaNCols(s) ((s)->numOfCols)
H
Hongze Cheng 已提交
102
#define schemaVersion(s) ((s)->version)
H
TD-166  
hzcheng 已提交
103 104
#define schemaTLen(s) ((s)->tlen)
#define schemaFLen(s) ((s)->flen)
T
Tao Liu 已提交
105
#define schemaVLen(s) ((s)->vlen)
H
hzcheng 已提交
106
#define schemaColAt(s, i) ((s)->columns + i)
S
TD-1848  
Shengliang Guan 已提交
107
#define tdFreeSchema(s) tfree((s))
H
hzcheng 已提交
108 109

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

H
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
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 已提交
129 130 131 132 133
// ----------------- SCHEMA BUILDER DEFINITION
typedef struct {
  int       tCols;
  int       nCols;
  int       tlen;
H
TD-353  
Hongze Cheng 已提交
134 135
  uint16_t  flen;
  uint16_t  vlen;
H
Hongze Cheng 已提交
136 137 138 139 140 141 142
  int       version;
  STColumn *columns;
} STSchemaBuilder;

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

H
TD-1548  
Hongze Cheng 已提交
146 147 148 149 150
// ----------------- Semantic timestamp key definition
typedef uint64_t TKEY;

#define TKEY_INVALID UINT64_MAX
#define TKEY_NULL TKEY_INVALID
H
Hongze Cheng 已提交
151 152
#define TKEY_NEGATIVE_FLAG (((TKEY)1) << 63)
#define TKEY_DELETE_FLAG (((TKEY)1) << 62)
H
TD-1548  
Hongze Cheng 已提交
153 154 155 156 157 158 159 160
#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 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#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 已提交
177 178 179 180 181 182 183 184 185 186 187 188
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;
  }
}
C
Cary Xu 已提交
189 190 191 192 193 194 195 196 197 198 199 200
/* A memory data row, the format is like below:
 *|---------+---------------------+--------------------------- len ---------------------------------->|
 *|<- type->|<--     Head      -->|<---------   flen -------------->|                                 |
 *|---------+---------------------+---------------------------------+---------------------------------+
 *| uint8_t | uint16_t |  int16_t |                                 |                                 |
 *|---------+----------+----------+---------------------------------+---------------------------------+
 *| flag    |   len    | sversion |           First part            |             Second part         |
 *|---------+----------+----------+---------------------------------+---------------------------------+
 *
 * NOTE: timestamp in this row structure is TKEY instead of TSKEY
 */
typedef void *SMemRow;
H
hzcheng 已提交
201

C
Cary Xu 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
// ----------------- Data row structure

/* A data row, the format is like below:
 * |<--------------------+--------------------------- len ---------------------------------->|
 * |<--     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 已提交
217
#define TD_DATA_ROW_HEAD_SIZE (sizeof(uint16_t) + sizeof(int16_t))
H
hzcheng 已提交
218

B
Bomin Zhang 已提交
219
#define dataRowLen(r) (*(uint16_t *)(r))
C
Cary Xu 已提交
220
#define dataRowVersion(r) (*(int16_t *)POINTER_SHIFT(r, sizeof(int16_t)))
H
hzcheng 已提交
221
#define dataRowTuple(r) POINTER_SHIFT(r, TD_DATA_ROW_HEAD_SIZE)
H
TD-1548  
Hongze Cheng 已提交
222 223
#define dataRowTKey(r) (*(TKEY *)(dataRowTuple(r)))
#define dataRowKey(r) tdGetKey(dataRowTKey(r))
H
hzcheng 已提交
224
#define dataRowSetLen(r, l) (dataRowLen(r) = (l))
H
TD-90  
Hongze Cheng 已提交
225
#define dataRowSetVersion(r, v) (dataRowVersion(r) = (v))
H
hzcheng 已提交
226
#define dataRowCpy(dst, r) memcpy((dst), (r), dataRowLen(r))
H
TD-166  
hzcheng 已提交
227
#define dataRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_DATA_ROW_HEAD_SIZE)
H
TD-1548  
Hongze Cheng 已提交
228
#define dataRowDeleted(r) TKEY_IS_DELETED(dataRowTKey(r))
H
hzcheng 已提交
229

C
Cary Xu 已提交
230 231
SDataRow tdNewDataRowFromSchema(STSchema *pSchema);
void     tdFreeDataRow(SDataRow row);
H
TD-166  
hzcheng 已提交
232
void     tdInitDataRow(SDataRow row, STSchema *pSchema);
C
Cary Xu 已提交
233
SDataRow tdDataRowDup(SDataRow row);
C
Cary Xu 已提交
234
SMemRow tdMemRowDup(SMemRow row);
H
more  
Hongze Cheng 已提交
235

236
// offset here not include dataRow header length
C
Cary Xu 已提交
237
static FORCE_INLINE int tdAppendColVal(SDataRow row, const void *value, int8_t type, int32_t offset) {
238 239 240 241
  ASSERT(value != NULL);
  int32_t toffset = offset + TD_DATA_ROW_HEAD_SIZE;
  char *  ptr = (char *)POINTER_SHIFT(row, dataRowLen(row));

H
TD-1548  
Hongze Cheng 已提交
242 243 244 245 246 247 248 249
  if (IS_VAR_DATA_TYPE(type)) {
    *(VarDataOffsetT *)POINTER_SHIFT(row, toffset) = dataRowLen(row);
    memcpy(ptr, value, varDataTLen(value));
    dataRowLen(row) += varDataTLen(value);
  } else {
    if (offset == 0) {
      ASSERT(type == TSDB_DATA_TYPE_TIMESTAMP);
      TKEY tvalue = tdGetTKEY(*(TSKEY *)value);
H
Hongze Cheng 已提交
250
      memcpy(POINTER_SHIFT(row, toffset), (void *)(&tvalue), TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
251
    } else {
252
      memcpy(POINTER_SHIFT(row, toffset), value, TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
253
    }
254 255 256 257 258
  }

  return 0;
}

C
Cary Xu 已提交
259
// NOTE: offset here including the header size
C
Cary Xu 已提交
260
static FORCE_INLINE void *tdGetRowDataOfCol(SDataRow row, int8_t type, int32_t offset) {
C
Cary Xu 已提交
261 262 263 264 265 266 267
  if (IS_VAR_DATA_TYPE(type)) {
    return POINTER_SHIFT(row, *(VarDataOffsetT *)POINTER_SHIFT(row, offset));
  } else {
    return POINTER_SHIFT(row, offset);
  }
}

H
TD-34  
hzcheng 已提交
268 269
// ----------------- Data column structure
typedef struct SDataCol {
H
TD-166  
hzcheng 已提交
270 271 272
  int8_t          type;       // column type
  int16_t         colId;      // column ID
  int             bytes;      // column data bytes defined
H
TD-166  
hzcheng 已提交
273
  int             offset;     // data offset in a SDataRow (including the header size)
H
TD-166  
hzcheng 已提交
274 275 276 277
  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 已提交
278
  TSKEY           ts;         // only used in last NULL column
H
TD-34  
hzcheng 已提交
279 280
} SDataCol;

H
TD-166  
hzcheng 已提交
281 282 283
static FORCE_INLINE void dataColReset(SDataCol *pDataCol) { pDataCol->len = 0; }

void dataColInit(SDataCol *pDataCol, STColumn *pCol, void **pBuf, int maxPoints);
C
Cary Xu 已提交
284
void dataColAppendVal(SDataCol *pCol, void *value, int numOfRows, int maxPoints);
H
TD-166  
hzcheng 已提交
285 286
void dataColSetOffset(SDataCol *pCol, int nEle);

H
TD-166  
hzcheng 已提交
287 288
bool isNEleNull(SDataCol *pCol, int nEle);
void dataColSetNEleNull(SDataCol *pCol, int nEle, int maxPoints);
H
TD-166  
hzcheng 已提交
289

C
Cary Xu 已提交
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 323 324 325
static FORCE_INLINE const void *tdGetNullVal(int8_t type) {
  switch (type) {
    case TSDB_DATA_TYPE_BOOL:
      return &BoolNull;
    case TSDB_DATA_TYPE_TINYINT:
      return &TinyintNull;
    case TSDB_DATA_TYPE_SMALLINT:
      return &SmallintNull;
    case TSDB_DATA_TYPE_INT:
      return &IntNull;
    case TSDB_DATA_TYPE_BIGINT:
      return &BigintNull;
    case TSDB_DATA_TYPE_FLOAT:
      return &FloatNull;
    case TSDB_DATA_TYPE_DOUBLE:
      return &DoubleNull;
    case TSDB_DATA_TYPE_BINARY:
      return &BinaryNull;
    case TSDB_DATA_TYPE_TIMESTAMP:
      return &TimestampNull;
    case TSDB_DATA_TYPE_NCHAR:
      return &NcharNull;
    case TSDB_DATA_TYPE_UTINYINT:
      return &UTinyintNull;
    case TSDB_DATA_TYPE_USMALLINT:
      return &USmallintNull;
    case TSDB_DATA_TYPE_UINT:
      return &UIntNull;
    case TSDB_DATA_TYPE_UBIGINT:
      return &UBigintNull;
    default:
      ASSERT(0);
      return NULL;
  }
}

H
TD-166  
hzcheng 已提交
326
// Get the data pointer from a column-wised data
C
Cary Xu 已提交
327
static FORCE_INLINE void *tdGetColDataOfRow(SDataCol *pCol, int row) {
H
Hongze Cheng 已提交
328 329 330 331
  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 已提交
332 333 334
  }
}

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

H
Hongze Cheng 已提交
338 339 340 341
  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 已提交
342 343 344
  }
}

H
TD-34  
hzcheng 已提交
345
typedef struct {
H
Hongze Cheng 已提交
346 347 348 349
  int maxRowSize;
  int maxCols;    // max number of columns
  int maxPoints;  // max number of points
  int bufSize;
H
TD-166  
hzcheng 已提交
350

H
Hongze Cheng 已提交
351 352 353 354 355
  int       numOfRows;
  int       numOfCols;  // Total number of cols
  int       sversion;   // TODO: set sversion
  void *    buf;
  SDataCol *cols;
H
TD-34  
hzcheng 已提交
356 357
} SDataCols;

H
TD-34  
hzcheng 已提交
358
#define keyCol(pCols) (&((pCols)->cols[0]))  // Key column
C
Cary Xu 已提交
359
#define dataColsTKeyAt(pCols, idx) ((TKEY *)(keyCol(pCols)->pData))[(idx)]  // the idx row of column-wised data
H
TD-1548  
Hongze Cheng 已提交
360
#define dataColsKeyAt(pCols, idx) tdGetKey(dataColsTKeyAt(pCols, idx))
361 362 363 364 365 366 367 368
static FORCE_INLINE TKEY dataColsTKeyFirst(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsTKeyAt(pCols, 0);
  } else {
    return TKEY_INVALID;
  }
}

369 370 371 372 373
static FORCE_INLINE TSKEY dataColsKeyAtRow(SDataCols *pCols, int row) {
  ASSERT(row < pCols->numOfRows);
  return dataColsKeyAt(pCols, row);
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
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 已提交
397

H
TD-166  
hzcheng 已提交
398
SDataCols *tdNewDataCols(int maxRowSize, int maxCols, int maxRows);
H
TD-34  
hzcheng 已提交
399
void       tdResetDataCols(SDataCols *pCols);
H
Hongze Cheng 已提交
400
int        tdInitDataCols(SDataCols *pCols, STSchema *pSchema);
H
TD-100  
hzcheng 已提交
401
SDataCols *tdDupDataCols(SDataCols *pCols, bool keepData);
H
Hongze Cheng 已提交
402
SDataCols *tdFreeDataCols(SDataCols *pCols);
C
Cary Xu 已提交
403
void       tdAppendMemRowToDataCol(SMemRow row, STSchema *pSchema, SDataCols *pCols);
H
Hongze Cheng 已提交
404
int        tdMergeDataCols(SDataCols *target, SDataCols *source, int rowsToMerge, int *pOffset);
H
more  
Hongze Cheng 已提交
405

H
Hongze Cheng 已提交
406 407 408
// ----------------- K-V data row structure
/*
 * +----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
409
 * | uint16_t |  int16_t |                                 |                                 |
H
Hongze Cheng 已提交
410 411 412 413
 * +----------+----------+---------------------------------+---------------------------------+
 * |    len   |   ncols  |           cols index            |             data part           |
 * +----------+----------+---------------------------------+---------------------------------+
 */
H
Hongze Cheng 已提交
414
typedef void *SKVRow;
H
Hongze Cheng 已提交
415 416

typedef struct {
C
Cary Xu 已提交
417 418
  int16_t  colId;
  uint16_t offset;
H
Hongze Cheng 已提交
419 420
} SColIdx;

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

C
Cary Xu 已提交
423
#define kvRowLen(r) (*(uint16_t *)(r))
C
Cary Xu 已提交
424
#define kvRowNCols(r) (*(int16_t *)POINTER_SHIFT(r, sizeof(uint16_t)))
H
Hongze Cheng 已提交
425 426
#define kvRowSetLen(r, len) kvRowLen(r) = (len)
#define kvRowSetNCols(r, n) kvRowNCols(r) = (n)
C
Cary Xu 已提交
427
#define kvRowColIdx(r) (SColIdx *)POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE)
H
Hongze Cheng 已提交
428 429 430 431
#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 已提交
432
#define kvRowFree(r) tfree(r)
H
TD-90  
Hongze Cheng 已提交
433
#define kvRowEnd(r) POINTER_SHIFT(r, kvRowLen(r))
C
Cary Xu 已提交
434 435 436
#define kvRowTKey(r) (*(TKEY *)(kvRowValues(r)))
#define kvRowKey(r) tdGetKey(kvRowTKey(r))
#define kvRowDeleted(r) TKEY_IS_DELETED(kvRowTKey(r))
H
Hongze Cheng 已提交
437

H
Hongze Cheng 已提交
438
SKVRow tdKVRowDup(SKVRow row);
H
TD-90  
Hongze Cheng 已提交
439
int    tdSetKVRowDataOfCol(SKVRow *orow, int16_t colId, int8_t type, void *value);
H
TD-353  
Hongze Cheng 已提交
440
int    tdEncodeKVRow(void **buf, SKVRow row);
H
Hongze Cheng 已提交
441
void * tdDecodeKVRow(void *buf, SKVRow *row);
B
Bomin Zhang 已提交
442
void   tdSortKVRowByColIdx(SKVRow row);
H
Hongze Cheng 已提交
443 444 445 446 447 448 449 450 451 452 453

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 已提交
454
static FORCE_INLINE void *tdGetKVRowValOfCol(SKVRow row, int16_t colId) {
H
Hongze Cheng 已提交
455
  void *ret = taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_EQ);
H
Hongze Cheng 已提交
456
  if (ret == NULL) return NULL;
H
Hongze Cheng 已提交
457
  return kvRowColVal(row, (SColIdx *)ret);
H
Hongze Cheng 已提交
458 459
}

L
liuyq-617 已提交
460 461 462 463
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 已提交
464 465 466 467 468 469 470 471
// offset here not include kvRow header length
static FORCE_INLINE int tdAppendKvColVal(SKVRow row, const void *value, int16_t colId, int8_t type, int32_t offset) {
  ASSERT(value != NULL);
  int32_t  toffset = offset + TD_KV_ROW_HEAD_SIZE;
  SColIdx *pColIdx = (SColIdx *)POINTER_SHIFT(row, toffset);
  char *   ptr = (char *)POINTER_SHIFT(row, kvRowLen(row));

  pColIdx->colId = colId;
C
Cary Xu 已提交
472
  pColIdx->offset = kvRowLen(row);  // offset of pColIdx including the TD_KV_ROW_HEAD_SIZE
C
Cary Xu 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

  if (IS_VAR_DATA_TYPE(type)) {
    memcpy(ptr, value, varDataTLen(value));
    kvRowLen(row) += varDataTLen(value);
  } else {
    if (offset == 0) {
      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;
}

H
Hongze Cheng 已提交
491 492 493 494 495
// ----------------- K-V data row builder
typedef struct {
  int16_t  tCols;
  int16_t  nCols;
  SColIdx *pColIdx;
C
Cary Xu 已提交
496 497
  uint16_t alloc;
  uint16_t size;
H
Hongze Cheng 已提交
498
  void *   buf;
H
Hongze Cheng 已提交
499
} SKVRowBuilder;
H
Hongze Cheng 已提交
500

H
Hongze Cheng 已提交
501 502 503 504
int    tdInitKVRowBuilder(SKVRowBuilder *pBuilder);
void   tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder);
void   tdResetKVRowBuilder(SKVRowBuilder *pBuilder);
SKVRow tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder);
H
Hongze Cheng 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

static FORCE_INLINE int tdAddColToKVRow(SKVRowBuilder *pBuilder, int16_t colId, int8_t type, void *value) {
  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
    pBuilder->pColIdx = (SColIdx *)realloc((void *)(pBuilder->pColIdx), sizeof(SColIdx) * pBuilder->tCols);
    if (pBuilder->pColIdx == NULL) return -1;
  }

  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;
    }
    pBuilder->buf = realloc(pBuilder->buf, pBuilder->alloc);
    if (pBuilder->buf == NULL) return -1;
  }

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

  return 0;
}
H
Hongze Cheng 已提交
532

C
Cary Xu 已提交
533
// ----------------- SMemRow appended with sequential data row structure
C
Cary Xu 已提交
534 535 536 537 538 539 540 541
/*
 * |-------------------------------+--------------------------- len ---------------------------------->|
 * |<--------     Head      ------>|<---------   flen -------------->|                                 |
 * |---------+---------------------+---------------------------------+---------------------------------+
 * | uint8_t | uint16_t |  int16_t |                                 |                                 |
 * |---------+----------+----------+---------------------------------+---------------------------------+
 * |  flag   |   len    | sversion |           First part            |             Second part         |
 * +---------+----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
542 543 544
 *
 * NOTE: timestamp in this row structure is TKEY instead of TSKEY
 */
C
Cary Xu 已提交
545

C
Cary Xu 已提交
546 547 548 549 550 551 552
// ----------------- SMemRow appended with extended K-V data row structure
/* |
 * |--------------------+----------+--------------------------------------------+---------------------------------+
 * | uint8_t | int16_t  | uint16_t |  int16_t |                                 |                                 |
 * |---------+----------+----------+----------+---------------------------------+---------------------------------+
 * |   flag  | sversion |   len    |   ncols  |           cols index            |             data part           |
 * |---------+----------+----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
553 554 555
 */

#define TD_MEM_ROW_TYPE_SIZE sizeof(uint8_t)
C
Cary Xu 已提交
556 557 558 559
#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)
#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 已提交
560 561 562 563 564 565 566 567

#define SMEM_ROW_DATA 0U  // SDataRow
#define SMEM_ROW_KV 1U    // SKVRow

#define memRowType(r) (*(uint8_t *)(r))
#define isDataRow(r) (SMEM_ROW_DATA == memRowType(r))
#define isKvRow(r) (SMEM_ROW_KV == memRowType(r))

C
Cary Xu 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
#define memRowDataBody(r) POINTER_SHIFT(r, TD_MEM_ROW_TYPE_SIZE)  // section after flag
#define memRowKvBody(r) \
  POINTER_SHIFT(r, TD_MEM_ROW_KV_TYPE_VER_SIZE)  // section after flag + sversion as to reuse of SKVRow
// #define memRowBody(r) (isDataRow(r) ? memRowDataBody(r) : memRowKvBody(r))

#define memRowDataLen(r) (*(TDRowLenT *)memRowDataBody(r))
#define memRowKvLen(r) (*(TDRowLenT *)memRowKvBody(r))
#define memRowDataTLen(r) (memRowDataLen(r) + (TDRowLenT)TD_MEM_ROW_TYPE_SIZE)
#define memRowKvTLen(r) (memRowKvLen(r) + (TDRowLenT)TD_MEM_ROW_KV_TYPE_VER_SIZE)

#define memRowLen(r) (isDataRow(r) ? memRowDataLen(r) : memRowKvLen(r))
#define memRowTLen(r) (isDataRow(r) ? memRowDataTLen(r) : memRowKvTLen(r))

#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
#define memRowKvSetVersion(r, v) (memRowKvVersion(r) = (v))
#define memRowTuple(r) (isDataRow(r) ? dataRowTuple(memRowDataBody(r)) : kvRowValues(memRowKvBody(r)))
C
Cary Xu 已提交
586

C
Cary Xu 已提交
587 588
#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 已提交
589 590

#define memRowSetType(r, t) (memRowType(r) = (t))
C
Cary Xu 已提交
591 592
#define memRowSetLen(r, l) (isDataRow(r) ? memRowDataLen(r) = (l) : memRowKvLen(r) = (l))
#define memRowSetVersion(r, v) (isDataRow(r) ? dataRowSetVersion(memRowDataBody(r), v) : memRowKvSetVersion(r, v))
C
Cary Xu 已提交
593
#define memRowCpy(dst, r) memcpy((dst), (r), memRowTLen(r))
C
Cary Xu 已提交
594
#define memRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_MEM_ROW_DATA_HEAD_SIZE)
C
Cary Xu 已提交
595 596 597
#define memRowDeleted(r) TKEY_IS_DELETED(memRowTKey(r))

// NOTE: offset here including the header size
L
liuyq-617 已提交
598
static FORCE_INLINE void *tdGetKvRowDataOfCol(void *row, int32_t offset) { return POINTER_SHIFT(row, offset); }
C
Cary Xu 已提交
599
// NOTE: offset here including the header size
C
Cary Xu 已提交
600 601 602 603
static FORCE_INLINE void *tdGetMemRowDataOfCol(void *row, int8_t type, int32_t offset) {
  if (isDataRow(row)) {
    return tdGetRowDataOfCol(row, type, offset);
  } else if (isKvRow(row)) {
L
liuyq-617 已提交
604
    return tdGetKvRowDataOfCol(row, offset);
C
Cary Xu 已提交
605 606 607 608 609 610
  } else {
    ASSERT(0);
  }
  return NULL;
}

C
Cary Xu 已提交
611 612 613 614 615 616 617 618 619 620 621 622
//  RawRow payload structure:
//  |<---------- header ------------->|<---- body: column data tuple ---->|
//  |SMemRowType|  dataLen |  nCols   |  colId  | colType | value |...|...|
//  +-----------+----------+----------+---------------------------------->|
//  | uint8_t   | uint16_t | uint16_t | int16_t | uint8_t |  ???  |...|...|
//  +-----------+----------+----------+---------------------------------->|

#define PAYLOAD_NCOLS_LEN sizeof(uint16_t)
#define PAYLOAD_NCOLS_OFFSET (sizeof(uint8_t) + sizeof(TDRowLenT))
#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 已提交
623
#define PLAYLOAD_PRIMARY_COL_LEN (PAYLOAD_ID_TYPE_LEN + sizeof(TSKEY))
C
Cary Xu 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

#define payloadBody(r) POINTER_SHIFT(r, PAYLOAD_HEADER_LEN)
#define payloadType(r) (*(uint8_t *)(r))
#define payloadSetType(r, t) (payloadType(r) = (t))
#define payloadTLen(r) (*(TDRowLenT *)POINTER_SHIFT(r, TD_MEM_ROW_TYPE_SIZE))  // including total header
#define payloadSetTLen(r, l) (payloadTLen(r) = (l))
#define payloadNCols(r) (*(TDRowLenT *)POINTER_SHIFT(r, PAYLOAD_NCOLS_OFFSET))
#define payloadSetNCols(r, n) (payloadNCols(r) = (n))

#define payloadColId(r) (*(int16_t *)(r))
#define payloadColType(r) (*(uint8_t *)POINTER_SHIFT(r, PAYLOAD_ID_LEN))
#define payloadColValue(r) POINTER_SHIFT(r, PAYLOAD_ID_TYPE_LEN)

#define payloadColSetId(r, i) (payloadColId(r) = (i))
#define payloadColSetType(r, t) (payloadColType(r) = (t))

#define payloadKeyAddr(r) POINTER_SHIFT(r, PAYLOAD_HEADER_LEN + PAYLOAD_ID_TYPE_LEN)
#define payloadTKey(r) (*(TKEY *)(payloadKeyAddr(r)))
#define payloadKey(r) tdGetKey(payloadTKey(r))

static FORCE_INLINE char *skipToNextEles(char *p) {
  uint8_t colType = payloadColType(p);
  if (IS_VAR_DATA_TYPE(colType)) {
C
Cary Xu 已提交
647
    return (char *)POINTER_SHIFT(p, PAYLOAD_ID_TYPE_LEN + varDataTLen(payloadColValue(p)));
C
Cary Xu 已提交
648
  } else {
C
Cary Xu 已提交
649
    return (char *)POINTER_SHIFT(p, PAYLOAD_ID_TYPE_LEN + TYPE_BYTES[colType]);
C
Cary Xu 已提交
650 651 652
  }
}

H
more  
hzcheng 已提交
653 654 655 656
#ifdef __cplusplus
}
#endif

H
hzcheng 已提交
657
#endif  // _TD_DATA_FORMAT_H_