tdataformat.h 15.0 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 18
#define _TD_DATA_FORMAT_H_

#include <stdint.h>
H
hzcheng 已提交
19
#include <stdlib.h>
H
hzcheng 已提交
20
#include <string.h>
H
more  
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22
#include "talgo.h"
H
Haojun Liao 已提交
23
#include "ttype.h"
H
TD-166  
hzcheng 已提交
24
#include "tutil.h"
H
hzcheng 已提交
25

H
more  
hzcheng 已提交
26 27 28
#ifdef __cplusplus
extern "C" {
#endif
H
hzcheng 已提交
29

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

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


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

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

H
hzcheng 已提交
57 58 59 60
// ----------------- TSDB COLUMN DEFINITION
typedef struct {
  int8_t  type;    // Column type
  int16_t colId;   // column ID
61 62
  int16_t bytes;   // column bytes
  int16_t offset;  // point offset in SDataRow after the header part
H
hzcheng 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76
} 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 已提交
77
  int      version;    // version
H
hzcheng 已提交
78
  int      numOfCols;  // Number of columns appended
H
refact  
Hongze Cheng 已提交
79
  int      tlen;       // maximum length of a SDataRow without the header part (sizeof(VarDataOffsetT) + sizeof(VarDataLenT) + (bytes))
H
TD-353  
Hongze Cheng 已提交
80
  uint16_t flen;       // First part length in a SDataRow after the header part
H
refact  
Hongze Cheng 已提交
81
  uint16_t vlen;       // pure value part length, excluded the overhead (bytes only)
H
hzcheng 已提交
82 83 84 85
  STColumn columns[];
} STSchema;

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

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

H
Hongze Cheng 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
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 已提交
113 114 115 116 117
// ----------------- SCHEMA BUILDER DEFINITION
typedef struct {
  int       tCols;
  int       nCols;
  int       tlen;
H
TD-353  
Hongze Cheng 已提交
118 119
  uint16_t  flen;
  uint16_t  vlen;
H
Hongze Cheng 已提交
120 121 122 123 124 125 126
  int       version;
  STColumn *columns;
} STSchemaBuilder;

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

H
TD-1548  
Hongze Cheng 已提交
130 131 132 133 134
// ----------------- Semantic timestamp key definition
typedef uint64_t TKEY;

#define TKEY_INVALID UINT64_MAX
#define TKEY_NULL TKEY_INVALID
H
Hongze Cheng 已提交
135 136
#define TKEY_NEGATIVE_FLAG (((TKEY)1) << 63)
#define TKEY_DELETE_FLAG (((TKEY)1) << 62)
H
TD-1548  
Hongze Cheng 已提交
137 138 139 140 141 142 143 144
#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 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#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 已提交
161 162 163 164 165 166 167 168 169 170 171 172
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
more  
Hongze Cheng 已提交
173 174
// ----------------- Data row structure

H
hzcheng 已提交
175
/* A data row, the format is like below:
H
TD-90  
Hongze Cheng 已提交
176 177 178
 * |<--------------------+--------------------------- len ---------------------------------->|
 * |<--     Head      -->|<---------   flen -------------->|                                 |
 * +---------------------+---------------------------------+---------------------------------+
B
Bomin Zhang 已提交
179
 * | uint16_t |  int16_t |                                 |                                 |
H
TD-90  
Hongze Cheng 已提交
180 181 182
 * +----------+----------+---------------------------------+---------------------------------+
 * |   len    | sversion |           First part            |             Second part         |
 * +----------+----------+---------------------------------+---------------------------------+
183
 *
H
TD-1548  
Hongze Cheng 已提交
184
 * NOTE: timestamp in this row structure is TKEY instead of TSKEY
H
more  
Hongze Cheng 已提交
185
 */
H
hzcheng 已提交
186 187
typedef void *SDataRow;

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

B
Bomin Zhang 已提交
190
#define dataRowLen(r) (*(uint16_t *)(r))
H
TD-90  
Hongze Cheng 已提交
191
#define dataRowVersion(r) *(int16_t *)POINTER_SHIFT(r, sizeof(int16_t))
H
hzcheng 已提交
192
#define dataRowTuple(r) POINTER_SHIFT(r, TD_DATA_ROW_HEAD_SIZE)
H
TD-1548  
Hongze Cheng 已提交
193 194
#define dataRowTKey(r) (*(TKEY *)(dataRowTuple(r)))
#define dataRowKey(r) tdGetKey(dataRowTKey(r))
H
hzcheng 已提交
195
#define dataRowSetLen(r, l) (dataRowLen(r) = (l))
H
TD-90  
Hongze Cheng 已提交
196
#define dataRowSetVersion(r, v) (dataRowVersion(r) = (v))
H
hzcheng 已提交
197
#define dataRowCpy(dst, r) memcpy((dst), (r), dataRowLen(r))
H
TD-166  
hzcheng 已提交
198
#define dataRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_DATA_ROW_HEAD_SIZE)
H
TD-1548  
Hongze Cheng 已提交
199
#define dataRowDeleted(r) TKEY_IS_DELETED(dataRowTKey(r))
H
hzcheng 已提交
200

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

206
// offset here not include dataRow header length
207 208 209 210 211
static FORCE_INLINE int tdAppendColVal(SDataRow row, void *value, int8_t type, int32_t bytes, int32_t offset) {
  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 已提交
212 213 214 215 216 217 218 219
  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 已提交
220
      memcpy(POINTER_SHIFT(row, toffset), (void *)(&tvalue), TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
221
    } else {
222
      memcpy(POINTER_SHIFT(row, toffset), value, TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
223
    }
224 225 226 227 228
  }

  return 0;
}

H
TD-166  
hzcheng 已提交
229
// NOTE: offset here including the header size
H
TD-166  
hzcheng 已提交
230
static FORCE_INLINE void *tdGetRowDataOfCol(SDataRow row, int8_t type, int32_t offset) {
H
TD-1548  
Hongze Cheng 已提交
231 232 233 234
  if (IS_VAR_DATA_TYPE(type)) {
    return POINTER_SHIFT(row, *(VarDataOffsetT *)POINTER_SHIFT(row, offset));
  } else {
    return POINTER_SHIFT(row, offset);
H
TD-166  
hzcheng 已提交
235 236 237
  }
}

H
TD-34  
hzcheng 已提交
238 239
// ----------------- Data column structure
typedef struct SDataCol {
H
TD-166  
hzcheng 已提交
240 241 242
  int8_t          type;       // column type
  int16_t         colId;      // column ID
  int             bytes;      // column data bytes defined
H
TD-166  
hzcheng 已提交
243
  int             offset;     // data offset in a SDataRow (including the header size)
H
TD-166  
hzcheng 已提交
244 245 246 247
  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
H
TD-34  
hzcheng 已提交
248 249
} SDataCol;

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

void dataColInit(SDataCol *pDataCol, STColumn *pCol, void **pBuf, int maxPoints);
H
Haojun Liao 已提交
253
void dataColAppendVal(SDataCol *pCol, void *value, int numOfRows, int maxPoints);
H
TD-166  
hzcheng 已提交
254 255
void dataColSetOffset(SDataCol *pCol, int nEle);

H
TD-166  
hzcheng 已提交
256 257
bool isNEleNull(SDataCol *pCol, int nEle);
void dataColSetNEleNull(SDataCol *pCol, int nEle, int maxPoints);
H
TD-166  
hzcheng 已提交
258 259 260

// Get the data pointer from a column-wised data
static FORCE_INLINE void *tdGetColDataOfRow(SDataCol *pCol, int row) {
H
Hongze Cheng 已提交
261 262 263 264
  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 已提交
265 266 267
  }
}

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

H
Hongze Cheng 已提交
271 272 273 274
  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 已提交
275 276 277
  }
}

H
TD-34  
hzcheng 已提交
278
typedef struct {
H
Hongze Cheng 已提交
279 280 281 282
  int maxRowSize;
  int maxCols;    // max number of columns
  int maxPoints;  // max number of points
  int bufSize;
H
TD-166  
hzcheng 已提交
283

H
Hongze Cheng 已提交
284 285 286 287 288
  int       numOfRows;
  int       numOfCols;  // Total number of cols
  int       sversion;   // TODO: set sversion
  void *    buf;
  SDataCol *cols;
H
TD-34  
hzcheng 已提交
289 290
} SDataCols;

H
TD-34  
hzcheng 已提交
291
#define keyCol(pCols) (&((pCols)->cols[0]))  // Key column
H
TD-1548  
Hongze Cheng 已提交
292 293
#define dataColsTKeyAt(pCols, idx) ((TKEY *)(keyCol(pCols)->pData))[(idx)]
#define dataColsKeyAt(pCols, idx) tdGetKey(dataColsTKeyAt(pCols, idx))
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
static FORCE_INLINE TKEY dataColsTKeyFirst(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsTKeyAt(pCols, 0);
  } else {
    return TKEY_INVALID;
  }
}

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 已提交
325

H
TD-166  
hzcheng 已提交
326
SDataCols *tdNewDataCols(int maxRowSize, int maxCols, int maxRows);
H
TD-34  
hzcheng 已提交
327
void       tdResetDataCols(SDataCols *pCols);
H
Hongze Cheng 已提交
328
int        tdInitDataCols(SDataCols *pCols, STSchema *pSchema);
H
TD-100  
hzcheng 已提交
329
SDataCols *tdDupDataCols(SDataCols *pCols, bool keepData);
H
Hongze Cheng 已提交
330
SDataCols *tdFreeDataCols(SDataCols *pCols);
H
TD-90  
Hongze Cheng 已提交
331
void       tdAppendDataRowToDataCol(SDataRow row, STSchema *pSchema, SDataCols *pCols);
H
hzcheng 已提交
332
int        tdMergeDataCols(SDataCols *target, SDataCols *src, int rowsToMerge);
H
more  
Hongze Cheng 已提交
333

H
Hongze Cheng 已提交
334 335 336 337 338 339 340 341
// ----------------- K-V data row structure
/*
 * +----------+----------+---------------------------------+---------------------------------+
 * |  int16_t |  int16_t |                                 |                                 |
 * +----------+----------+---------------------------------+---------------------------------+
 * |    len   |   ncols  |           cols index            |             data part           |
 * +----------+----------+---------------------------------+---------------------------------+
 */
H
Hongze Cheng 已提交
342
typedef void *SKVRow;
H
Hongze Cheng 已提交
343 344 345 346 347 348

typedef struct {
  int16_t colId;
  int16_t offset;
} SColIdx;

B
Bomin Zhang 已提交
349
#define TD_KV_ROW_HEAD_SIZE (2 * sizeof(int16_t))
H
Hongze Cheng 已提交
350

H
Hongze Cheng 已提交
351 352
#define kvRowLen(r) (*(int16_t *)(r))
#define kvRowNCols(r) (*(int16_t *)POINTER_SHIFT(r, sizeof(int16_t)))
H
Hongze Cheng 已提交
353 354
#define kvRowSetLen(r, len) kvRowLen(r) = (len)
#define kvRowSetNCols(r, n) kvRowNCols(r) = (n)
H
Hongze Cheng 已提交
355 356 357 358 359
#define kvRowColIdx(r) (SColIdx *)POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE)
#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 已提交
360
#define kvRowFree(r) tfree(r)
H
TD-90  
Hongze Cheng 已提交
361
#define kvRowEnd(r) POINTER_SHIFT(r, kvRowLen(r))
H
Hongze Cheng 已提交
362

H
Hongze Cheng 已提交
363
SKVRow tdKVRowDup(SKVRow row);
H
TD-90  
Hongze Cheng 已提交
364
int    tdSetKVRowDataOfCol(SKVRow *orow, int16_t colId, int8_t type, void *value);
H
TD-353  
Hongze Cheng 已提交
365
int    tdEncodeKVRow(void **buf, SKVRow row);
H
Hongze Cheng 已提交
366
void * tdDecodeKVRow(void *buf, SKVRow *row);
B
Bomin Zhang 已提交
367
void   tdSortKVRowByColIdx(SKVRow row);
H
Hongze Cheng 已提交
368 369 370 371 372 373 374 375 376 377 378

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 已提交
379
static FORCE_INLINE void *tdGetKVRowValOfCol(SKVRow row, int16_t colId) {
H
Hongze Cheng 已提交
380
  void *ret = taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_EQ);
H
Hongze Cheng 已提交
381
  if (ret == NULL) return NULL;
H
Hongze Cheng 已提交
382
  return kvRowColVal(row, (SColIdx *)ret);
H
Hongze Cheng 已提交
383 384
}

H
Hongze Cheng 已提交
385 386 387 388 389 390 391 392
// ----------------- K-V data row builder
typedef struct {
  int16_t  tCols;
  int16_t  nCols;
  SColIdx *pColIdx;
  int16_t  alloc;
  int16_t  size;
  void *   buf;
H
Hongze Cheng 已提交
393
} SKVRowBuilder;
H
Hongze Cheng 已提交
394

H
Hongze Cheng 已提交
395 396 397 398
int    tdInitKVRowBuilder(SKVRowBuilder *pBuilder);
void   tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder);
void   tdResetKVRowBuilder(SKVRowBuilder *pBuilder);
SKVRow tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder);
H
Hongze Cheng 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

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 已提交
426

H
more  
hzcheng 已提交
427 428 429 430
#ifdef __cplusplus
}
#endif

H
hzcheng 已提交
431
#endif  // _TD_DATA_FORMAT_H_