tdataformat.h 5.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/>.
 */
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  
hzcheng 已提交
24 25 26
#ifdef __cplusplus
extern "C" {
#endif
H
hzcheng 已提交
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

// ----------------- 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 已提交
53
  int      numOfCols;  // Number of columns appended
H
TD-27  
hzcheng 已提交
54
  int      padding;  // Total columns allocated
H
hzcheng 已提交
55 56 57 58 59 60 61
  STColumn columns[];
} STSchema;

#define schemaNCols(s) ((s)->numOfCols)
#define schemaColAt(s, i) ((s)->columns + i)

STSchema *tdNewSchema(int32_t nCols);
H
TD-27  
hzcheng 已提交
62
int       tdSchemaAppendCol(STSchema *pSchema, int8_t type, int16_t colId, int32_t bytes);
H
hzcheng 已提交
63
STSchema *tdDupSchema(STSchema *pSchema);
H
hzcheng 已提交
64 65
void      tdFreeSchema(STSchema *pSchema);
void      tdUpdateSchema(STSchema *pSchema);
H
TD-27  
hzcheng 已提交
66 67 68
int       tdGetSchemaEncodeSize(STSchema *pSchema);
void *    tdEncodeSchema(void *dst, STSchema *pSchema);
STSchema *tdDecodeSchema(void **psrc);
H
hzcheng 已提交
69

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

H
hzcheng 已提交
72
/* A data row, the format is like below:
H
hzcheng 已提交
73 74 75 76 77 78
 * +----------+---------+---------------------------------+---------------------------------+
 * | int32_t  | int32_t |                                 |                                 |
 * +----------+---------+---------------------------------+---------------------------------+
 * |   len    |  flen   |           First part            |             Second part         |
 * +----------+---------+---------------------------------+---------------------------------+
 * plen: first part length
H
hzcheng 已提交
79 80
 * len: the length including sizeof(row) + sizeof(len)
 * row: actual row data encoding
H
more  
Hongze Cheng 已提交
81
 */
H
hzcheng 已提交
82 83
typedef void *SDataRow;

H
TD-34  
hzcheng 已提交
84

H
hzcheng 已提交
85
#define TD_DATA_ROW_HEAD_SIZE (2 * sizeof(int32_t))
H
hzcheng 已提交
86

H
hzcheng 已提交
87
#define dataRowLen(r) (*(int32_t *)(r))
H
hzcheng 已提交
88
#define dataRowFLen(r) (*(int32_t *)((char *)(r) + sizeof(int32_t)))
H
hzcheng 已提交
89
#define dataRowTuple(r) ((char *)(r) + TD_DATA_ROW_HEAD_SIZE)
H
TD-34  
hzcheng 已提交
90
#define dataRowKey(r) (*(TSKEY *)(dataRowTuple(r)))
H
hzcheng 已提交
91
#define dataRowSetLen(r, l) (dataRowLen(r) = (l))
H
hzcheng 已提交
92
#define dataRowSetFLen(r, l) (dataRowFLen(r) = (l))
H
hzcheng 已提交
93
#define dataRowIdx(r, i) ((char *)(r) + i)
H
hzcheng 已提交
94
#define dataRowCpy(dst, r) memcpy((dst), (r), dataRowLen(r))
H
hzcheng 已提交
95
#define dataRowAt(r, idx) ((char *)(r) + (idx))
H
hzcheng 已提交
96

H
hzcheng 已提交
97
void     tdInitDataRow(SDataRow row, STSchema *pSchema);
H
hzcheng 已提交
98
int      tdMaxRowBytesFromSchema(STSchema *pSchema);
H
hzcheng 已提交
99
SDataRow tdNewDataRow(int32_t bytes, STSchema *pSchema);
H
hzcheng 已提交
100
SDataRow tdNewDataRowFromSchema(STSchema *pSchema);
H
hzcheng 已提交
101
void     tdFreeDataRow(SDataRow row);
H
hzcheng 已提交
102 103
int      tdAppendColVal(SDataRow row, void *value, STColumn *pCol);
void     tdDataRowReset(SDataRow row, STSchema *pSchema);
H
hzcheng 已提交
104
SDataRow tdDataRowDup(SDataRow row);
H
more  
Hongze Cheng 已提交
105

H
TD-34  
hzcheng 已提交
106 107
// ----------------- Data column structure
typedef struct SDataCol {
H
TD-34  
hzcheng 已提交
108 109 110 111 112
  int8_t  type;
  int16_t colId;
  int     bytes;
  int     len;
  int     offset;
H
TD-100  
hzcheng 已提交
113
  void *  pData; // Original data
H
TD-34  
hzcheng 已提交
114 115
} SDataCol;

H
TD-34  
hzcheng 已提交
116
typedef struct {
H
TD-34  
hzcheng 已提交
117 118 119
  int      maxRowSize;
  int      maxCols;    // max number of columns
  int      maxPoints;  // max number of points
H
TD-34  
hzcheng 已提交
120
  int      numOfPoints;
H
TD-34  
hzcheng 已提交
121
  int      numOfCols;  // Total number of cols
H
TD-34  
hzcheng 已提交
122
  int      sversion;   // TODO: set sversion
H
TD-34  
hzcheng 已提交
123 124 125 126
  void *   buf;
  SDataCol cols[];
} SDataCols;

H
TD-34  
hzcheng 已提交
127
#define keyCol(pCols) (&((pCols)->cols[0]))  // Key column
H
TD-34  
hzcheng 已提交
128 129 130
#define dataColsKeyAt(pCols, idx) ((int64_t *)(keyCol(pCols)->pData))[(idx)]
#define dataColsKeyFirst(pCols) dataColsKeyAt(pCols, 0)
#define dataColsKeyLast(pCols) dataColsKeyAt(pCols, (pCols)->numOfPoints - 1)
H
TD-34  
hzcheng 已提交
131

H
TD-34  
hzcheng 已提交
132
SDataCols *tdNewDataCols(int maxRowSize, int maxCols, int maxRows);
H
TD-34  
hzcheng 已提交
133
void       tdResetDataCols(SDataCols *pCols);
H
TD-34  
hzcheng 已提交
134
void       tdInitDataCols(SDataCols *pCols, STSchema *pSchema);
H
TD-100  
hzcheng 已提交
135
SDataCols *tdDupDataCols(SDataCols *pCols, bool keepData);
H
TD-34  
hzcheng 已提交
136
void       tdFreeDataCols(SDataCols *pCols);
H
TD-34  
hzcheng 已提交
137
void       tdAppendDataRowToDataCol(SDataRow row, SDataCols *pCols);
H
TD-34  
hzcheng 已提交
138
void       tdPopDataColsPoints(SDataCols *pCols, int pointsToPop);
H
hzcheng 已提交
139
int        tdMergeDataCols(SDataCols *target, SDataCols *src, int rowsToMerge);
H
TD-100  
hzcheng 已提交
140
void       tdMergeTwoDataCols(SDataCols *target, SDataCols *src1, int *iter1, SDataCols *src2, int *iter2, int tRows);
H
more  
Hongze Cheng 已提交
141

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

H
hzcheng 已提交
146
#endif  // _TD_DATA_FORMAT_H_