dataformat.h 4.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 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
hzcheng 已提交
22 23
#include "taosdef.h"

H
more  
Hongze Cheng 已提交
24

H
more  
hzcheng 已提交
25 26 27
#ifdef __cplusplus
extern "C" {
#endif
H
hzcheng 已提交
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

// ----------------- TSDB COLUMN DEFINITION
typedef struct {
  int8_t  type;    // Column type
  int16_t colId;   // column ID
  int32_t bytes;   // column bytes
  int32_t offset;  // point offset in a row data
} 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))

STColumn *tdNewCol(int8_t type, int16_t colId, int16_t bytes);
void      tdFreeCol(STColumn *pCol);
void      tdColCpy(STColumn *dst, STColumn *src);
void      tdSetCol(STColumn *pCol, int8_t type, int16_t colId, int32_t bytes);

// ----------------- TSDB SCHEMA DEFINITION
typedef struct {
H
hzcheng 已提交
54 55
  int      numOfCols;  // Number of columns appended
  int      totalCols;  // Total columns allocated
H
hzcheng 已提交
56 57 58 59
  STColumn columns[];
} STSchema;

#define schemaNCols(s) ((s)->numOfCols)
H
hzcheng 已提交
60
#define schemaTCols(s) ((s)->totalCols)
H
hzcheng 已提交
61 62 63
#define schemaColAt(s, i) ((s)->columns + i)

STSchema *tdNewSchema(int32_t nCols);
H
hzcheng 已提交
64
int       tdSchemaAppendCol(STSchema *pSchema, int8_t type, int16_t colId, int16_t bytes);
H
hzcheng 已提交
65
STSchema *tdDupSchema(STSchema *pSchema);
H
hzcheng 已提交
66 67
void      tdFreeSchema(STSchema *pSchema);
void      tdUpdateSchema(STSchema *pSchema);
H
hzcheng 已提交
68

H
more  
Hongze Cheng 已提交
69 70
// ----------------- Data row structure

H
hzcheng 已提交
71
/* A data row, the format is like below:
H
more  
Hongze Cheng 已提交
72 73 74
 * +---------+---------------------------------+
 * | int32_t |                                 |
 * +---------+---------------------------------+
H
hzcheng 已提交
75
 * |   len   |                row              |
H
more  
Hongze Cheng 已提交
76
 * +---------+---------------------------------+
H
hzcheng 已提交
77 78
 * len: the length including sizeof(row) + sizeof(len)
 * row: actual row data encoding
H
more  
Hongze Cheng 已提交
79
 */
H
hzcheng 已提交
80 81 82 83 84 85
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 已提交
86
#define dataRowCpy(dst, r) memcpy((dst), (r), dataRowLen(r))
H
hzcheng 已提交
87 88

SDataRow tdNewDataRow(int32_t bytes);
H
hzcheng 已提交
89
// SDataRow tdNewDdataFromSchema(SSchema *pSchema);
H
hzcheng 已提交
90
void     tdFreeDataRow(SDataRow row);
H
hzcheng 已提交
91
// int32_t  tdAppendColVal(SDataRow row, void *value, SColumn *pCol, int32_t suffixOffset);
H
hzcheng 已提交
92 93
void     tdDataRowCpy(void *dst, SDataRow row);
void     tdDataRowReset(SDataRow row);
H
hzcheng 已提交
94
SDataRow tdDataRowDup(SDataRow row);
H
more  
Hongze Cheng 已提交
95 96

/* Data rows definition, the format of it is like below:
H
hzcheng 已提交
97 98 99 100 101
 * +---------+-----------------------+--------+-----------------------+
 * | int32_t |                       |        |                       |
 * +---------+-----------------------+--------+-----------------------+
 * |   len   |        SDataRow       |  ....  |        SDataRow       |
 * +---------+-----------------------+--------+-----------------------+
H
more  
Hongze Cheng 已提交
102
 */
H
hzcheng 已提交
103
typedef void *SDataRows;
H
more  
Hongze Cheng 已提交
104

H
hzcheng 已提交
105 106
#define TD_DATA_ROWS_HEAD_LEN sizeof(int32_t)

H
hzcheng 已提交
107 108 109 110
#define dataRowsLen(rs) (*(int32_t *)(rs))
#define dataRowsSetLen(rs, l) (dataRowsLen(rs) = (l))
#define dataRowsInit(rs) dataRowsSetLen(rs, sizeof(int32_t))

H
hzcheng 已提交
111 112 113 114 115 116 117 118 119 120 121 122
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 已提交
123 124 125 126 127 128 129
/* Data column definition
 * +---------+---------+-----------------------+
 * | int32_t | int32_t |                       |
 * +---------+---------+-----------------------+
 * |   len   | npoints |          data         |
 * +---------+---------+-----------------------+
 */
H
hzcheng 已提交
130
typedef char *SDataCol;
H
more  
Hongze Cheng 已提交
131 132 133 134 135 136 137 138

/* Data columns definition
 * +---------+---------+-----------------------+--------+-----------------------+
 * | int32_t | int32_t |                       |        |                       |
 * +---------+---------+-----------------------+--------+-----------------------+
 * |   len   | npoints |        SDataCol       |  ....  |        SDataCol       |
 * +---------+---------+-----------------------+--------+-----------------------+
 */
H
hzcheng 已提交
139
typedef char *SDataCols;
H
more  
Hongze Cheng 已提交
140

H
more  
hzcheng 已提交
141 142 143 144
#ifdef __cplusplus
}
#endif

H
hzcheng 已提交
145
#endif  // _TD_DATA_FORMAT_H_