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

S
TD-4088  
Shengliang Guan 已提交
18
#include "os.h"
H
Hongze Cheng 已提交
19
#include "talgo.h"
H
Haojun Liao 已提交
20
#include "ttypes.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
// 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 已提交
29 30 31
#define TD_SUPPORT_BACK2  // suppport back compatibility of 2.0

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

33 34 35 36 37
#define STR_TO_VARSTR(x, str)                     \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = __len;                  \
    memcpy(varDataVal(x), (str), __len);          \
H
Hongze Cheng 已提交
38 39
  } while (0);

H
Hongze Cheng 已提交
40 41 42 43 44 45
#define STR_TO_NET_VARSTR(x, str)                 \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = htons(__len);           \
    memcpy(varDataVal(x), (str), __len);          \
  } while (0);
D
dapan1121 已提交
46

47 48
#define STR_WITH_MAXSIZE_TO_VARSTR(x, str, _maxs)                         \
  do {                                                                    \
H
Hui Li 已提交
49
    char *_e = stpncpy(varDataVal(x), (str), (_maxs)-VARSTR_HEADER_SIZE); \
50
    varDataSetLen(x, (_e - (x)-VARSTR_HEADER_SIZE));                      \
H
Hongze Cheng 已提交
51 52
  } while (0)

53 54 55 56
#define STR_WITH_SIZE_TO_VARSTR(x, str, _size)  \
  do {                                          \
    *(VarDataLenT *)(x) = (VarDataLenT)(_size); \
    memcpy(varDataVal(x), (str), (_size));      \
H
Hongze Cheng 已提交
57
  } while (0);
H
hjxilinx 已提交
58

59 60 61
// ----------------- TSDB COLUMN DEFINITION
typedef struct {
  int8_t   type;    // Column type
C
Cary Xu 已提交
62
  col_id_t colId;   // column ID(start from PRIMARYKEY_TIMESTAMP_COL_ID(1))
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  int16_t  bytes;   // column bytes (restore to int16_t in case of misuse)
  uint16_t offset;  // point offset in SDataRow after the header part.
} 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 已提交
79 80 81 82 83 84
  int version;    // version
  int numOfCols;  // Number of columns appended
  int tlen;  // maximum length of a SDataRow without the header part (sizeof(VarDataOffsetT) + sizeof(VarDataLenT) +
             // (bytes))
  uint16_t flen;  // First part length in a SDataRow after the header part
  uint16_t vlen;  // pure value part length, excluded the overhead (bytes only)
85 86 87 88 89 90 91 92 93 94 95
  STColumn columns[];
} STSchema;

#define schemaNCols(s) ((s)->numOfCols)
#define schemaVersion(s) ((s)->version)
#define schemaTLen(s) ((s)->tlen)
#define schemaFLen(s) ((s)->flen)
#define schemaVLen(s) ((s)->vlen)
#define schemaColAt(s, i) ((s)->columns + i)
#define tdFreeSchema(s) tfree((s))

H
Hongze Cheng 已提交
96
STSchema *tdDupSchema(const STSchema *pSchema);
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 125 126
int       tdEncodeSchema(void **buf, STSchema *pSchema);
void *    tdDecodeSchema(void *buf, STSchema **pRSchema);

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;
}

// ----------------- SCHEMA BUILDER DEFINITION
typedef struct {
  int       tCols;
  int       nCols;
  int       tlen;
  uint16_t  flen;
  uint16_t  vlen;
  int       version;
  STColumn *columns;
} STSchemaBuilder;

C
Cary Xu 已提交
127 128 129 130
#define TD_VTYPE_BITS 2   // val type
#define TD_VTYPE_PARTS 4  // 8 bits / TD_VTYPE_BITS = 4
#define TD_VTYPE_OPTR 3   // TD_VTYPE_PARTS - 1, utilize to get remainder

C
Cary Xu 已提交
131
#define TD_BITMAP_BYTES(cnt) (ceil((double)cnt / TD_VTYPE_PARTS))
C
update  
Cary Xu 已提交
132
#define TD_BIT_TO_BYTES(cnt) (ceil((double)cnt / 8))
C
Cary Xu 已提交
133

134 135 136 137 138
int       tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, int32_t version);
void      tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder);
void      tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, int32_t version);
int       tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int16_t colId, int16_t bytes);
STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder);
H
Hongze Cheng 已提交
139

H
TD-1548  
Hongze Cheng 已提交
140
// ----------------- Semantic timestamp key definition
C
update  
Cary Xu 已提交
141
#ifdef TD_2_0
C
Cary Xu 已提交
142

H
TD-1548  
Hongze Cheng 已提交
143 144 145 146
typedef uint64_t TKEY;

#define TKEY_INVALID UINT64_MAX
#define TKEY_NULL TKEY_INVALID
H
Hongze Cheng 已提交
147 148
#define TKEY_NEGATIVE_FLAG (((TKEY)1) << 63)
#define TKEY_DELETE_FLAG (((TKEY)1) << 62)
H
TD-1548  
Hongze Cheng 已提交
149 150 151 152 153
#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)
dengyihao's avatar
dengyihao 已提交
154
#define tdGetTKEY(key) (((TKEY)TABS(key)) | (TKEY_NEGATIVE_FLAG & (TKEY)(key)))
H
TD-1548  
Hongze Cheng 已提交
155 156
#define tdGetKey(tkey) (((TSKEY)((tkey)&TKEY_VALUE_FILTER)) * (TKEY_IS_NEGATIVE(tkey) ? -1 : 1))

D
dapan1121 已提交
157 158 159 160 161
#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))

C
Cary Xu 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174
#else

typedef uint64_t TKEY;

#define TKEY_INVALID UINT64_MAX
#define TKEY_NULL TKEY_INVALID
#define TKEY_NEGATIVE_FLAG (((TKEY)1) << 63)
#define TKEY_DELETE_FLAG (((TKEY)1) << 62)
#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)
C
update  
Cary Xu 已提交
175
#define tdGetTKEY(key) (((TKEY)TABS(key)) | (TKEY_NEGATIVE_FLAG & (TKEY)(key)))
C
Cary Xu 已提交
176 177 178 179 180 181 182 183 184
#define tdGetKey(tkey) (((TSKEY)((tkey)&TKEY_VALUE_FILTER)) * (TKEY_IS_NEGATIVE(tkey) ? -1 : 1))

#define MIN_TS_KEY ((TSKEY)0x8000000000000001)
#define MAX_TS_KEY ((TSKEY)0x7fffffffffffffff)

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

#endif

D
dapan1121 已提交
185 186 187 188 189 190 191 192 193 194 195
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 已提交
196 197 198 199 200 201 202 203 204 205 206 207
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 已提交
208

C
Cary Xu 已提交
209 210 211
// ----------------- Data row structure

/* A data row, the format is like below:
K
kailixu 已提交
212
 * |<------------------------------------------------ len ---------------------------------->|
C
Cary Xu 已提交
213 214 215 216 217 218 219 220 221 222 223
 * |<--     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 已提交
224
#define TD_DATA_ROW_HEAD_SIZE (sizeof(uint16_t) + sizeof(int16_t))
H
hzcheng 已提交
225

C
Cary Xu 已提交
226
#define dataRowLen(r) (*(TDRowLenT *)(r))  // 0~65535
227
#define dataRowEnd(r) POINTER_SHIFT(r, dataRowLen(r))
C
Cary Xu 已提交
228
#define dataRowVersion(r) (*(int16_t *)POINTER_SHIFT(r, sizeof(int16_t)))
H
hzcheng 已提交
229
#define dataRowTuple(r) POINTER_SHIFT(r, TD_DATA_ROW_HEAD_SIZE)
H
TD-1548  
Hongze Cheng 已提交
230 231
#define dataRowTKey(r) (*(TKEY *)(dataRowTuple(r)))
#define dataRowKey(r) tdGetKey(dataRowTKey(r))
H
hzcheng 已提交
232
#define dataRowSetLen(r, l) (dataRowLen(r) = (l))
H
TD-90  
Hongze Cheng 已提交
233
#define dataRowSetVersion(r, v) (dataRowVersion(r) = (v))
H
hzcheng 已提交
234
#define dataRowCpy(dst, r) memcpy((dst), (r), dataRowLen(r))
H
TD-166  
hzcheng 已提交
235
#define dataRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_DATA_ROW_HEAD_SIZE)
H
TD-1548  
Hongze Cheng 已提交
236
#define dataRowDeleted(r) TKEY_IS_DELETED(dataRowTKey(r))
H
hzcheng 已提交
237

C
Cary Xu 已提交
238 239
SDataRow tdNewDataRowFromSchema(STSchema *pSchema);
void     tdFreeDataRow(SDataRow row);
H
TD-166  
hzcheng 已提交
240
void     tdInitDataRow(SDataRow row, STSchema *pSchema);
C
Cary Xu 已提交
241
SDataRow tdDataRowDup(SDataRow row);
H
more  
Hongze Cheng 已提交
242

243
// offset here not include dataRow header length
244 245
static FORCE_INLINE int tdAppendDataColVal(SDataRow row, const void *value, bool isCopyVarData, int8_t type,
                                           int32_t offset) {
246
  assert(value != NULL);
247 248
  int32_t toffset = offset + TD_DATA_ROW_HEAD_SIZE;

H
TD-1548  
Hongze Cheng 已提交
249 250
  if (IS_VAR_DATA_TYPE(type)) {
    *(VarDataOffsetT *)POINTER_SHIFT(row, toffset) = dataRowLen(row);
251 252 253
    if (isCopyVarData) {
      memcpy(POINTER_SHIFT(row, dataRowLen(row)), value, varDataTLen(value));
    }
H
TD-1548  
Hongze Cheng 已提交
254 255 256
    dataRowLen(row) += varDataTLen(value);
  } else {
    if (offset == 0) {
257
      assert(type == TSDB_DATA_TYPE_TIMESTAMP);
H
TD-1548  
Hongze Cheng 已提交
258
      TKEY tvalue = tdGetTKEY(*(TSKEY *)value);
C
Cary Xu 已提交
259
      memcpy(POINTER_SHIFT(row, toffset), (const void *)(&tvalue), TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
260
    } else {
261
      memcpy(POINTER_SHIFT(row, toffset), value, TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
262
    }
263 264 265 266 267
  }

  return 0;
}

268 269 270 271 272
// 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 已提交
273
// NOTE: offset here including the header size
C
Cary Xu 已提交
274
static FORCE_INLINE void *tdGetRowDataOfCol(SDataRow row, int8_t type, int32_t offset) {
C
Cary Xu 已提交
275 276 277 278 279 280 281
  if (IS_VAR_DATA_TYPE(type)) {
    return POINTER_SHIFT(row, *(VarDataOffsetT *)POINTER_SHIFT(row, offset));
  } else {
    return POINTER_SHIFT(row, offset);
  }
}

282 283 284 285 286 287
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;
H
Hongze Cheng 已提交
288
  int8_t  type = pSchema->columns[idx].type;
289 290 291 292 293 294

  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;
H
Hongze Cheng 已提交
295
  int8_t  type = pSchema->columns[idx].type;
296 297 298 299 300 301

  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;
H
Hongze Cheng 已提交
302
  int8_t  type = pSchema->columns[idx].type;
303 304 305 306 307
  int16_t bytes = pSchema->columns[idx].bytes;

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

H
Hongze Cheng 已提交
308 309
static FORCE_INLINE void tdCopyColOfRowBySchema(SDataRow dst, STSchema *pDstSchema, int dstIdx, SDataRow src,
                                                STSchema *pSrcSchema, int srcIdx) {
310
  int8_t type = pDstSchema->columns[dstIdx].type;
311
  assert(type == pSrcSchema->columns[srcIdx].type);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  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:
348
      if (pSrcSchema->columns[srcIdx].colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
349 350 351 352 353 354 355 356 357 358
        *(TSKEY *)pData = tdGetKey(*(TKEY *)value);
      } else {
        *(TSKEY *)pData = *(TSKEY *)value;
      }
      break;
    default:
      memcpy(pData, value, pSrcSchema->columns[srcIdx].bytes);
  }
}

H
TD-34  
hzcheng 已提交
359 360
// ----------------- Data column structure
typedef struct SDataCol {
H
TD-166  
hzcheng 已提交
361 362 363
  int8_t          type;       // column type
  int16_t         colId;      // column ID
  int             bytes;      // column data bytes defined
H
TD-166  
hzcheng 已提交
364
  int             offset;     // data offset in a SDataRow (including the header size)
H
TD-166  
hzcheng 已提交
365 366 367 368
  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
C
update  
Cary Xu 已提交
369
  void *          pBitmap;    // Bitmap pointer to mark Null/Norm(1 bit for each row)
L
lichuang 已提交
370
  TSKEY           ts;         // only used in last NULL column
H
TD-34  
hzcheng 已提交
371 372
} SDataCol;

K
kailixu 已提交
373
#define isAllRowsNull(pCol) ((pCol)->len == 0)
K
Kaili Xu 已提交
374
#define isAllRowsNone(pCol) ((pCol)->len == 0)
H
TD-166  
hzcheng 已提交
375 376
static FORCE_INLINE void dataColReset(SDataCol *pDataCol) { pDataCol->len = 0; }

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

379
void dataColInit(SDataCol *pDataCol, STColumn *pCol, int maxPoints);
H
Hongze Cheng 已提交
380
int  dataColAppendVal(SDataCol *pCol, const void *value, int numOfRows, int maxPoints);
H
TD-166  
hzcheng 已提交
381 382
void dataColSetOffset(SDataCol *pCol, int nEle);

H
TD-166  
hzcheng 已提交
383
bool isNEleNull(SDataCol *pCol, int nEle);
H
TD-166  
hzcheng 已提交
384 385

// Get the data pointer from a column-wised data
K
kailixu 已提交
386 387
static FORCE_INLINE const void *tdGetColDataOfRow(SDataCol *pCol, int row) {
  if (isAllRowsNull(pCol)) {
C
Cary Xu 已提交
388
    return getNullValue(pCol->type);
K
kailixu 已提交
389
  }
H
Hongze Cheng 已提交
390 391 392 393
  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 已提交
394 395 396
  }
}

H
TD-166  
hzcheng 已提交
397
static FORCE_INLINE int32_t dataColGetNEleLen(SDataCol *pDataCol, int rows) {
398
  assert(rows > 0);
H
TD-166  
hzcheng 已提交
399

H
Hongze Cheng 已提交
400 401 402 403
  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 已提交
404 405 406
  }
}

H
TD-34  
hzcheng 已提交
407
typedef struct {
C
update  
Cary Xu 已提交
408 409
  col_id_t  maxCols;    // max number of columns
  col_id_t  numOfCols;  // Total number of cols
H
Hongze Cheng 已提交
410 411
  int       maxPoints;  // max number of points
  int       numOfRows;
C
update  
Cary Xu 已提交
412
  int       sversion;  // TODO: set sversion
H
Hongze Cheng 已提交
413
  SDataCol *cols;
H
TD-34  
hzcheng 已提交
414 415
} SDataCols;

H
Hongze Cheng 已提交
416
#define keyCol(pCols) (&((pCols)->cols[0]))                                 // Key column
C
Cary Xu 已提交
417
#define dataColsTKeyAt(pCols, idx) ((TKEY *)(keyCol(pCols)->pData))[(idx)]  // the idx row of column-wised data
H
TD-1548  
Hongze Cheng 已提交
418
#define dataColsKeyAt(pCols, idx) tdGetKey(dataColsTKeyAt(pCols, idx))
419 420 421 422 423 424 425 426
static FORCE_INLINE TKEY dataColsTKeyFirst(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsTKeyAt(pCols, 0);
  } else {
    return TKEY_INVALID;
  }
}

427
static FORCE_INLINE TSKEY dataColsKeyAtRow(SDataCols *pCols, int row) {
428
  assert(row < pCols->numOfRows);
429 430 431
  return dataColsKeyAt(pCols, row);
}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
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 已提交
455

L
Liu Jicong 已提交
456
SDataCols *tdNewDataCols(int maxCols, int maxRows);
H
TD-34  
hzcheng 已提交
457
void       tdResetDataCols(SDataCols *pCols);
H
Hongze Cheng 已提交
458
int        tdInitDataCols(SDataCols *pCols, STSchema *pSchema);
H
TD-100  
hzcheng 已提交
459
SDataCols *tdDupDataCols(SDataCols *pCols, bool keepData);
H
Hongze Cheng 已提交
460
SDataCols *tdFreeDataCols(SDataCols *pCols);
461
int        tdMergeDataCols(SDataCols *target, SDataCols *source, int rowsToMerge, int *pOffset, bool forceSetNull);
H
more  
Hongze Cheng 已提交
462

H
Hongze Cheng 已提交
463
// ----------------- K-V data row structure
C
Cary Xu 已提交
464 465
/* |<-------------------------------------- len -------------------------------------------->|
 * |<----- header  ----->|<--------------------------- body -------------------------------->|
H
Hongze Cheng 已提交
466
 * +----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
467
 * | uint16_t |  int16_t |                                 |                                 |
H
Hongze Cheng 已提交
468 469 470 471
 * +----------+----------+---------------------------------+---------------------------------+
 * |    len   |   ncols  |           cols index            |             data part           |
 * +----------+----------+---------------------------------+---------------------------------+
 */
H
Hongze Cheng 已提交
472
typedef void *SKVRow;
H
Hongze Cheng 已提交
473 474

typedef struct {
C
Cary Xu 已提交
475 476
  int16_t  colId;
  uint16_t offset;
H
Hongze Cheng 已提交
477 478
} SColIdx;

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

C
Cary Xu 已提交
481
#define kvRowLen(r) (*(TDRowLenT *)(r))
C
Cary Xu 已提交
482
#define kvRowNCols(r) (*(int16_t *)POINTER_SHIFT(r, sizeof(uint16_t)))
H
Hongze Cheng 已提交
483 484
#define kvRowSetLen(r, len) kvRowLen(r) = (len)
#define kvRowSetNCols(r, n) kvRowNCols(r) = (n)
C
Cary Xu 已提交
485
#define kvRowColIdx(r) (SColIdx *)POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE)
H
Hongze Cheng 已提交
486 487 488 489
#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 已提交
490
#define kvRowFree(r) tfree(r)
H
TD-90  
Hongze Cheng 已提交
491
#define kvRowEnd(r) POINTER_SHIFT(r, kvRowLen(r))
492
#define kvRowValLen(r) (kvRowLen(r) - TD_KV_ROW_HEAD_SIZE - sizeof(SColIdx) * kvRowNCols(r))
C
Cary Xu 已提交
493 494
#define kvRowTKey(r) (*(TKEY *)(kvRowValues(r)))
#define kvRowKey(r) tdGetKey(kvRowTKey(r))
H
Hongze Cheng 已提交
495
#define kvRowKeys(r) POINTER_SHIFT(r, *(uint16_t *)POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE + sizeof(int16_t)))
C
Cary Xu 已提交
496
#define kvRowDeleted(r) TKEY_IS_DELETED(kvRowTKey(r))
H
Hongze Cheng 已提交
497

H
Hongze Cheng 已提交
498
SKVRow tdKVRowDup(SKVRow row);
H
TD-90  
Hongze Cheng 已提交
499
int    tdSetKVRowDataOfCol(SKVRow *orow, int16_t colId, int8_t type, void *value);
H
TD-353  
Hongze Cheng 已提交
500
int    tdEncodeKVRow(void **buf, SKVRow row);
H
Hongze Cheng 已提交
501
void * tdDecodeKVRow(void *buf, SKVRow *row);
B
Bomin Zhang 已提交
502
void   tdSortKVRowByColIdx(SKVRow row);
H
Hongze Cheng 已提交
503 504 505 506 507 508 509 510 511 512 513

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 已提交
514
static FORCE_INLINE void *tdGetKVRowValOfCol(SKVRow row, int16_t colId) {
H
Hongze Cheng 已提交
515
  void *ret = taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_EQ);
H
Hongze Cheng 已提交
516
  if (ret == NULL) return NULL;
H
Hongze Cheng 已提交
517
  return kvRowColVal(row, (SColIdx *)ret);
H
Hongze Cheng 已提交
518 519
}

L
liuyq-617 已提交
520 521 522 523
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 已提交
524
// offset here not include kvRow header length
525 526
static FORCE_INLINE int tdAppendKvColVal(SKVRow row, const void *value, bool isCopyValData, int16_t colId, int8_t type,
                                         int32_t offset) {
527
  assert(value != NULL);
528
  int32_t  toffset = offset + TD_KV_ROW_HEAD_SIZE;
C
Cary Xu 已提交
529 530 531 532
  SColIdx *pColIdx = (SColIdx *)POINTER_SHIFT(row, toffset);
  char *   ptr = (char *)POINTER_SHIFT(row, kvRowLen(row));

  pColIdx->colId = colId;
C
Cary Xu 已提交
533
  pColIdx->offset = kvRowLen(row);  // offset of pColIdx including the TD_KV_ROW_HEAD_SIZE
C
Cary Xu 已提交
534 535

  if (IS_VAR_DATA_TYPE(type)) {
536 537 538
    if (isCopyValData) {
      memcpy(ptr, value, varDataTLen(value));
    }
C
Cary Xu 已提交
539 540
    kvRowLen(row) += varDataTLen(value);
  } else {
541
    if (offset == 0) {
542
      assert(type == TSDB_DATA_TYPE_TIMESTAMP);
C
Cary Xu 已提交
543 544 545 546 547 548 549 550 551 552
      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;
}
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
// 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 已提交
570

H
Hongze Cheng 已提交
571 572 573 574 575
// ----------------- K-V data row builder
typedef struct {
  int16_t  tCols;
  int16_t  nCols;
  SColIdx *pColIdx;
C
Cary Xu 已提交
576 577
  uint16_t alloc;
  uint16_t size;
H
Hongze Cheng 已提交
578
  void *   buf;
H
Hongze Cheng 已提交
579
} SKVRowBuilder;
H
Hongze Cheng 已提交
580

H
Hongze Cheng 已提交
581 582 583 584
int    tdInitKVRowBuilder(SKVRowBuilder *pBuilder);
void   tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder);
void   tdResetKVRowBuilder(SKVRowBuilder *pBuilder);
SKVRow tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder);
H
Hongze Cheng 已提交
585

586
static FORCE_INLINE int tdAddColToKVRow(SKVRowBuilder *pBuilder, int16_t colId, int8_t type, const void *value) {
H
Hongze Cheng 已提交
587 588
  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
H
Hongze Cheng 已提交
589
    SColIdx *pColIdx = (SColIdx *)realloc((void *)(pBuilder->pColIdx), sizeof(SColIdx) * pBuilder->tCols);
T
tickduan 已提交
590 591
    if (pColIdx == NULL) return -1;
    pBuilder->pColIdx = pColIdx;
H
Hongze Cheng 已提交
592 593 594 595 596 597 598 599 600 601 602 603
  }

  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;
    }
H
Hongze Cheng 已提交
604
    void *buf = realloc(pBuilder->buf, pBuilder->alloc);
T
tickduan 已提交
605 606
    if (buf == NULL) return -1;
    pBuilder->buf = buf;
H
Hongze Cheng 已提交
607 608 609 610 611 612 613
  }

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

  return 0;
}
H
Hongze Cheng 已提交
614

C
update  
Cary Xu 已提交
615
// ----------------- SMemRow appended with tuple row structure
C
Cary Xu 已提交
616
/*
C
Cary Xu 已提交
617
 * |---------|------------------------------------------------- len ---------------------------------->|
C
Cary Xu 已提交
618 619 620 621 622 623
 * |<--------     Head      ------>|<---------   flen -------------->|                                 |
 * |---------+---------------------+---------------------------------+---------------------------------+
 * | uint8_t | uint16_t |  int16_t |                                 |                                 |
 * |---------+----------+----------+---------------------------------+---------------------------------+
 * |  flag   |   len    | sversion |           First part            |             Second part         |
 * +---------+----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
624 625 626
 *
 * NOTE: timestamp in this row structure is TKEY instead of TSKEY
 */
C
Cary Xu 已提交
627

C
update  
Cary Xu 已提交
628
// ----------------- SMemRow appended with extended K-V data row structure
C
Cary Xu 已提交
629 630
/* |--------------------|------------------------------------------------  len ---------------------------------->|
 * |<-------------     Head      ------------>|<---------   flen -------------->|                                 |
C
Cary Xu 已提交
631 632 633 634 635
 * |--------------------+----------+--------------------------------------------+---------------------------------+
 * | uint8_t | int16_t  | uint16_t |  int16_t |                                 |                                 |
 * |---------+----------+----------+----------+---------------------------------+---------------------------------+
 * |   flag  | sversion |   len    |   ncols  |           cols index            |             data part           |
 * |---------+----------+----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
636 637
 */

C
Cary Xu 已提交
638 639
typedef void *SMemRow;

C
Cary Xu 已提交
640
#define TD_MEM_ROW_TYPE_SIZE sizeof(uint8_t)
C
Cary Xu 已提交
641 642 643
#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)
644
#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 已提交
645

646 647 648 649 650
#define SMEM_ROW_DATA 0x0U      // SDataRow
#define SMEM_ROW_KV 0x01U       // SKVRow

#define KVRatioConvert (0.9f)

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

C
bug fix  
Cary Xu 已提交
653
#define memRowSetType(r, t) ((*(uint8_t *)(r)) = (t))  // set the total byte in case of dirty memory
654
#define isDataRowT(t) (SMEM_ROW_DATA == (((uint8_t)(t)) & 0x01))
C
Cary Xu 已提交
655
#define isDataRow(r) (SMEM_ROW_DATA == memRowType(r))
656
#define isKvRowT(t) (SMEM_ROW_KV == (((uint8_t)(t)) & 0x01))
C
Cary Xu 已提交
657
#define isKvRow(r) (SMEM_ROW_KV == memRowType(r))
C
Cary Xu 已提交
658
#define isUtilizeKVRow(k, d) ((k) < ((d)*KVRatioConvert))
C
Cary Xu 已提交
659

C
Cary Xu 已提交
660 661
#define memRowDataBody(r) POINTER_SHIFT(r, TD_MEM_ROW_TYPE_SIZE)  // section after flag
#define memRowKvBody(r) \
C
Cary Xu 已提交
662 663 664 665
  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 已提交
666

K
kailixu 已提交
667
#define memRowDataTLen(r) \
C
Cary Xu 已提交
668
  ((TDRowLenT)(memRowDataLen(r) + TD_MEM_ROW_TYPE_SIZE))  // using uint32_t/int32_t to store the TLen
C
Cary Xu 已提交
669

C
Cary Xu 已提交
670
#define memRowKvTLen(r) ((TDRowLenT)(memRowKvLen(r) + TD_MEM_ROW_KV_TYPE_VER_SIZE))
C
Cary Xu 已提交
671 672

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

675 676 677 678 679 680 681 682
static FORCE_INLINE char *memRowEnd(SMemRow row) {
  if (isDataRow(row)) {
    return (char *)dataRowEnd(memRowDataBody(row));
  } else {
    return (char *)kvRowEnd(memRowKvBody(row));
  }
}

C
Cary Xu 已提交
683 684 685
#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 已提交
686
#define memRowSetKvVersion(r, v) (memRowKvVersion(r) = (v))
C
Cary Xu 已提交
687
#define memRowTuple(r) (isDataRow(r) ? dataRowTuple(memRowDataBody(r)) : kvRowValues(memRowKvBody(r)))
C
Cary Xu 已提交
688

C
Cary Xu 已提交
689 690
#define memRowTKey(r) (isDataRow(r) ? dataRowTKey(memRowDataBody(r)) : kvRowTKey(memRowKvBody(r)))
#define memRowKey(r) (isDataRow(r) ? dataRowKey(memRowDataBody(r)) : kvRowKey(memRowKvBody(r)))
H
Hongze Cheng 已提交
691
#define memRowKeys(r) (isDataRow(r) ? dataRowTuple(memRowDataBody(r)) : kvRowKeys(memRowKvBody(r)))
C
Cary Xu 已提交
692 693 694 695 696 697 698 699
#define memRowSetTKey(r, k)                 \
  do {                                      \
    if (isDataRow(r)) {                     \
      dataRowTKey(memRowDataBody(r)) = (k); \
    } else {                                \
      kvRowTKey(memRowKvBody(r)) = (k);     \
    }                                       \
  } while (0)
C
Cary Xu 已提交
700

C
Cary Xu 已提交
701
#define memRowSetLen(r, l) (isDataRow(r) ? memRowDataLen(r) = (l) : memRowKvLen(r) = (l))
702
#define memRowSetVersion(r, v) (isDataRow(r) ? dataRowSetVersion(memRowDataBody(r), v) : memRowSetKvVersion(r, v))
C
Cary Xu 已提交
703
#define memRowCpy(dst, r) memcpy((dst), (r), memRowTLen(r))
C
Cary Xu 已提交
704
#define memRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_MEM_ROW_DATA_HEAD_SIZE)
C
Cary Xu 已提交
705 706
#define memRowDeleted(r) TKEY_IS_DELETED(memRowTKey(r))

C
Cary Xu 已提交
707
SMemRow tdMemRowDup(SMemRow row);
708 709
void    tdAppendMemRowToDataCol(SMemRow row, STSchema *pSchema, SDataCols *pCols, bool forceSetNull);

C
Cary Xu 已提交
710
// NOTE: offset here including the header size
711
static FORCE_INLINE void *tdGetMemRowDataOfCol(void *row, int16_t colId, int8_t colType, uint16_t offset) {
C
Cary Xu 已提交
712
  if (isDataRow(row)) {
713
    return tdGetRowDataOfCol(memRowDataBody(row), colType, offset);
C
Cary Xu 已提交
714
  } else {
715
    return tdGetKVRowValOfCol(memRowKvBody(row), colId);
C
Cary Xu 已提交
716 717 718
  }
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732
/**
 * 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);
  }
}

733 734
static FORCE_INLINE int tdAppendMemRowColVal(SMemRow row, const void *value, bool isCopyVarData, int16_t colId,
                                             int8_t type, int32_t offset) {
735
  if (isDataRow(row)) {
736
    tdAppendDataColVal(memRowDataBody(row), value, isCopyVarData, type, offset);
737
  } else {
738
    tdAppendKvColVal(memRowKvBody(row), value, isCopyVarData, colId, type, offset);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
  }
  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;
}

typedef struct {
  int16_t colId;
  uint8_t colType;
H
Hongze Cheng 已提交
763
  char *  colVal;
764 765
} SColInfo;

H
Hongze Cheng 已提交
766
static FORCE_INLINE void setSColInfo(SColInfo *colInfo, int16_t colId, uint8_t colType, char *colVal) {
767 768 769 770 771 772 773
  colInfo->colId = colId;
  colInfo->colType = colType;
  colInfo->colVal = colVal;
}

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

H
more  
hzcheng 已提交
774 775 776 777
#ifdef __cplusplus
}
#endif

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