dataformat.h 4.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 18
#define _TD_DATA_FORMAT_H_

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

#include "schema.h"

H
more  
hzcheng 已提交
23 24 25
#ifdef __cplusplus
extern "C" {
#endif
H
more  
Hongze Cheng 已提交
26 27
// ----------------- Data row structure

H
hzcheng 已提交
28
/* A data row, the format is like below:
H
more  
Hongze Cheng 已提交
29 30 31
 * +---------+---------------------------------+
 * | int32_t |                                 |
 * +---------+---------------------------------+
H
hzcheng 已提交
32
 * |   len   |                row              |
H
more  
Hongze Cheng 已提交
33
 * +---------+---------------------------------+
H
hzcheng 已提交
34 35
 * len: the length including sizeof(row) + sizeof(len)
 * row: actual row data encoding
H
more  
Hongze Cheng 已提交
36
 */
H
hzcheng 已提交
37 38 39 40 41 42
typedef void *SDataRow;

#define dataRowLen(r) (*(int32_t *)(r))
#define dataRowTuple(r) ((char *)(r) + sizeof(int32_t))
#define dataRowSetLen(r, l) (dataRowLen(r) = (l))
#define dataRowIdx(r, i) ((char *)(r) + i)
H
hzcheng 已提交
43
#define dataRowCpy(dst, r) memcpy((dst), (r), dataRowLen(r))
H
hzcheng 已提交
44 45 46 47 48

SDataRow tdNewDataRow(int32_t bytes);
SDataRow tdNewDdataFromSchema(SSchema *pSchema);
void     tdFreeDataRow(SDataRow row);
int32_t  tdAppendColVal(SDataRow row, void *value, SColumn *pCol, int32_t suffixOffset);
H
hzcheng 已提交
49 50
void     tdDataRowCpy(void *dst, SDataRow row);
void     tdDataRowReset(SDataRow row);
H
more  
Hongze Cheng 已提交
51 52

/* Data rows definition, the format of it is like below:
H
hzcheng 已提交
53 54 55 56 57
 * +---------+-----------------------+--------+-----------------------+
 * | int32_t |                       |        |                       |
 * +---------+-----------------------+--------+-----------------------+
 * |   len   |        SDataRow       |  ....  |        SDataRow       |
 * +---------+-----------------------+--------+-----------------------+
H
more  
Hongze Cheng 已提交
58
 */
H
hzcheng 已提交
59
typedef void *SDataRows;
H
more  
Hongze Cheng 已提交
60

H
hzcheng 已提交
61 62
#define TD_DATA_ROWS_HEAD_LEN sizeof(int32_t)

H
hzcheng 已提交
63 64 65 66
#define dataRowsLen(rs) (*(int32_t *)(rs))
#define dataRowsSetLen(rs, l) (dataRowsLen(rs) = (l))
#define dataRowsInit(rs) dataRowsSetLen(rs, sizeof(int32_t))

H
hzcheng 已提交
67 68 69 70 71 72 73 74 75 76 77 78
void tdDataRowsAppendRow(SDataRows rows, SDataRow row);

// Data rows iterator
typedef struct {
  int32_t totalLen;
  int32_t len;
  SDataRow row;
} SDataRowsIter;

void tdInitSDataRowsIter(SDataRows rows, SDataRowsIter *pIter);
SDataRow tdDataRowsNext(SDataRowsIter *pIter);

H
more  
Hongze Cheng 已提交
79 80 81 82 83 84 85
/* Data column definition
 * +---------+---------+-----------------------+
 * | int32_t | int32_t |                       |
 * +---------+---------+-----------------------+
 * |   len   | npoints |          data         |
 * +---------+---------+-----------------------+
 */
H
hzcheng 已提交
86
typedef char *SDataCol;
H
more  
Hongze Cheng 已提交
87 88 89 90 91 92 93 94

/* Data columns definition
 * +---------+---------+-----------------------+--------+-----------------------+
 * | int32_t | int32_t |                       |        |                       |
 * +---------+---------+-----------------------+--------+-----------------------+
 * |   len   | npoints |        SDataCol       |  ....  |        SDataCol       |
 * +---------+---------+-----------------------+--------+-----------------------+
 */
H
hzcheng 已提交
95
typedef char *SDataCols;
H
more  
Hongze Cheng 已提交
96 97 98 99

// ----------------- Data column structure

// ---- operation on SDataRow;
H
hzcheng 已提交
100
#define TD_DATA_ROW_HEADER_SIZE sizeof(int32_t)
H
more  
Hongze Cheng 已提交
101 102 103
#define TD_DATAROW_LEN(pDataRow) (*(int32_t *)(pDataRow))
#define TD_DATAROW_DATA(pDataRow) ((pDataRow) + sizeof(int32_t))

H
more  
hzcheng 已提交
104
SDataRow tdSDataRowDup(SDataRow rdata);
H
hzcheng 已提交
105 106
void     tdSDataRowCpy(SDataRow src, void *dst);
void     tdFreeSDataRow(SDataRow rdata);
H
more  
hzcheng 已提交
107

H
more  
Hongze Cheng 已提交
108 109
// ---- operation on SDataRows
#define TD_DATAROWS_LEN(pDataRows) (*(int32_t *)(pDataRows))
H
more  
hzcheng 已提交
110 111
#define TD_DATAROWS_ROWS(pDataRows) (*(int32_t *)((pDataRows) + sizeof(int32_t)))
#define TD_DATAROWS_DATA(pDataRows) (SDataRow)((pDataRows) + 2 * sizeof(int32_t))
H
more  
Hongze Cheng 已提交
112 113 114 115 116 117 118 119 120

// ---- operation on SDataCol
#define TD_DATACOL_LEN(pDataCol) (*(int32_t *)(pDataCol))
#define TD_DATACOL_NPOINTS(pDataCol) (*(int32_t *)(pDataCol + sizeof(int32_t)))

// ---- operation on SDataCols
#define TD_DATACOLS_LEN(pDataCols) (*(int32_t *)(pDataCols))
#define TD_DATACOLS_NPOINTS(pDataCols) (*(int32_t *)(pDataCols + sizeof(int32_t)))

H
more  
hzcheng 已提交
121 122 123 124
#ifdef __cplusplus
}
#endif

H
hzcheng 已提交
125
#endif  // _TD_DATA_FORMAT_H_