TDengine/include/common/tdataformat.h

300 lines
9.9 KiB
C
Raw Normal View History

2020-03-03 08:17:48 +00:00
/*
* 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/>.
*/
2022-02-28 09:39:07 +00:00
2021-10-04 12:42:53 +00:00
#ifndef _TD_COMMON_DATA_FORMAT_H_
#define _TD_COMMON_DATA_FORMAT_H_
2020-02-11 07:29:08 +00:00
2021-05-09 07:47:07 +00:00
#include "os.h"
2020-05-30 10:00:49 +00:00
#include "talgo.h"
2022-05-27 14:05:14 +00:00
#include "tarray.h"
2022-05-11 02:29:53 +00:00
#include "tencode.h"
2021-10-08 12:33:46 +00:00
#include "ttypes.h"
2020-04-28 02:11:22 +00:00
#include "tutil.h"
2020-03-10 06:39:11 +00:00
2020-03-03 08:17:48 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2020-03-10 06:39:11 +00:00
2022-10-07 15:03:31 +00:00
typedef struct SBuffer SBuffer;
2022-05-11 02:29:53 +00:00
typedef struct SSchema SSchema;
typedef struct STColumn STColumn;
typedef struct STSchema STSchema;
2022-05-31 02:48:45 +00:00
typedef struct SValue SValue;
2022-05-14 06:18:07 +00:00
typedef struct SColVal SColVal;
2022-05-11 02:29:53 +00:00
typedef struct STSRow2 STSRow2;
typedef struct STSRowBuilder STSRowBuilder;
2022-05-17 09:50:21 +00:00
typedef struct STagVal STagVal;
typedef struct STag STag;
2022-09-13 06:21:43 +00:00
typedef struct SColData SColData;
2022-05-11 02:29:53 +00:00
2022-09-13 10:17:24 +00:00
#define HAS_NONE ((uint8_t)0x1)
#define HAS_NULL ((uint8_t)0x2)
#define HAS_VALUE ((uint8_t)0x4)
2022-09-13 06:21:43 +00:00
// bitmap ================================
2022-08-18 02:09:31 +00:00
const static uint8_t BIT2_MAP[4][4] = {{0b00000000, 0b00000001, 0b00000010, 0},
2022-08-18 11:37:05 +00:00
{0b00000000, 0b00000100, 0b00001000, 2},
{0b00000000, 0b00010000, 0b00100000, 4},
{0b00000000, 0b01000000, 0b10000000, 6}};
2022-08-18 02:09:31 +00:00
2022-08-19 07:30:54 +00:00
#define N1(n) ((((uint8_t)1) << (n)) - 1)
2022-08-18 11:40:28 +00:00
#define BIT1_SIZE(n) ((((n)-1) >> 3) + 1)
#define BIT2_SIZE(n) ((((n)-1) >> 2) + 1)
2022-08-19 07:30:54 +00:00
#define SET_BIT1(p, i, v) ((p)[(i) >> 3] = (p)[(i) >> 3] & N1((i)&7) | (((uint8_t)(v)) << ((i)&7)))
2022-08-18 02:29:49 +00:00
#define GET_BIT1(p, i) (((p)[(i) >> 3] >> ((i)&7)) & ((uint8_t)1))
2022-08-19 07:30:54 +00:00
#define SET_BIT2(p, i, v) ((p)[(i) >> 2] = (p)[(i) >> 2] & N1(BIT2_MAP[(i)&3][3]) | BIT2_MAP[(i)&3][(v)])
2022-08-18 02:29:49 +00:00
#define GET_BIT2(p, i) (((p)[(i) >> 2] >> BIT2_MAP[(i)&3][3]) & ((uint8_t)3))
2022-06-18 03:35:49 +00:00
2022-10-07 15:03:31 +00:00
// SBuffer ================================
struct SBuffer {
int64_t nBuf;
uint8_t *pBuf;
};
#define tBufferCreate() \
(SBuffer) { .nBuf = 0, .pBuf = NULL }
void tBufferDestroy(SBuffer *pBuffer);
int32_t tBufferInit(SBuffer *pBuffer, int64_t size);
int32_t tBufferPut(SBuffer *pBuffer, const void *pData, int64_t nData);
2022-09-13 06:21:43 +00:00
// STSchema ================================
2022-05-11 03:11:52 +00:00
int32_t tTSchemaCreate(int32_t sver, SSchema *pSchema, int32_t nCols, STSchema **ppTSchema);
2022-05-11 02:37:52 +00:00
void tTSchemaDestroy(STSchema *pTSchema);
2022-05-11 02:29:53 +00:00
2022-09-13 06:21:43 +00:00
// SValue ================================
2022-11-08 06:36:18 +00:00
static FORCE_INLINE int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type);
2022-06-04 01:26:05 +00:00
2022-09-13 06:21:43 +00:00
// SColVal ================================
2022-09-23 10:55:27 +00:00
#define CV_FLAG_VALUE ((int8_t)0x0)
#define CV_FLAG_NONE ((int8_t)0x1)
#define CV_FLAG_NULL ((int8_t)0x2)
#define COL_VAL_NONE(CID, TYPE) ((SColVal){.cid = (CID), .type = (TYPE), .flag = CV_FLAG_NONE})
#define COL_VAL_NULL(CID, TYPE) ((SColVal){.cid = (CID), .type = (TYPE), .flag = CV_FLAG_NULL})
2022-06-22 08:45:26 +00:00
#define COL_VAL_VALUE(CID, TYPE, V) ((SColVal){.cid = (CID), .type = (TYPE), .value = (V)})
2022-06-01 07:28:57 +00:00
2022-09-23 10:55:27 +00:00
#define COL_VAL_IS_NONE(CV) ((CV)->flag == CV_FLAG_NONE)
#define COL_VAL_IS_NULL(CV) ((CV)->flag == CV_FLAG_NULL)
#define COL_VAL_IS_VALUE(CV) ((CV)->flag == CV_FLAG_VALUE)
2022-09-13 06:21:43 +00:00
// STSRow2 ================================
2022-07-11 11:43:16 +00:00
#define TSROW_LEN(PROW, V) tGetI32v((uint8_t *)(PROW)->data, (V) ? &(V) : NULL)
#define TSROW_SVER(PROW, V) tGetI32v((PROW)->data + TSROW_LEN(PROW, NULL), (V) ? &(V) : NULL)
2022-06-02 06:04:31 +00:00
int32_t tTSRowNew(STSRowBuilder *pBuilder, SArray *pArray, STSchema *pTSchema, STSRow2 **ppRow);
2022-06-01 07:10:33 +00:00
int32_t tTSRowClone(const STSRow2 *pRow, STSRow2 **ppRow);
2022-06-01 03:08:46 +00:00
void tTSRowFree(STSRow2 *pRow);
2022-06-01 07:10:33 +00:00
void tTSRowGet(STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal);
int32_t tTSRowToArray(STSRow2 *pRow, STSchema *pTSchema, SArray **ppArray);
2022-05-17 05:46:22 +00:00
int32_t tPutTSRow(uint8_t *p, STSRow2 *pRow);
2022-07-11 11:43:16 +00:00
int32_t tGetTSRow(uint8_t *p, STSRow2 **ppRow);
2022-05-14 06:18:07 +00:00
2022-09-13 06:21:43 +00:00
// STSRowBuilder ================================
2022-06-02 06:04:31 +00:00
#define tsRowBuilderInit() ((STSRowBuilder){0})
#define tsRowBuilderClear(B) \
do { \
if ((B)->pBuf) { \
taosMemoryFree((B)->pBuf); \
} \
} while (0)
2022-05-11 03:11:52 +00:00
2022-09-13 06:21:43 +00:00
// STag ================================
2022-05-27 14:18:31 +00:00
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag);
2022-05-17 09:50:21 +00:00
void tTagFree(STag *pTag);
2022-06-22 08:32:10 +00:00
bool tTagIsJson(const void *pTag);
bool tTagIsJsonNull(void *tagVal);
2022-05-28 09:16:04 +00:00
bool tTagGet(const STag *pTag, STagVal *pTagVal);
2022-06-01 12:42:29 +00:00
char *tTagValToData(const STagVal *pTagVal, bool isJson);
2022-05-18 08:30:39 +00:00
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag);
2022-05-24 06:50:47 +00:00
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag);
2022-05-28 09:16:04 +00:00
int32_t tTagToValArray(const STag *pTag, SArray **ppArray);
2022-08-19 07:12:04 +00:00
void tTagSetCid(const STag *pTag, int16_t iTag, int16_t cid);
2022-06-02 06:04:31 +00:00
void debugPrintSTag(STag *pTag, const char *tag, int32_t ln); // TODO: remove
2022-07-11 11:43:16 +00:00
int32_t parseJsontoTagData(const char *json, SArray *pTagVals, STag **ppTag, void *pMsgBuf);
2022-05-17 09:50:21 +00:00
2022-09-13 06:21:43 +00:00
// SColData ================================
2022-09-14 10:05:57 +00:00
void tColDataDestroy(void *ph);
2022-09-13 06:21:43 +00:00
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t smaOn);
2022-09-13 09:30:20 +00:00
void tColDataClear(SColData *pColData);
2022-09-13 06:21:43 +00:00
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal);
2022-09-15 01:56:56 +00:00
void tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal);
2022-11-09 02:39:32 +00:00
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal);
2022-09-13 06:21:43 +00:00
int32_t tColDataCopy(SColData *pColDataSrc, SColData *pColDataDest);
2022-11-07 07:42:53 +00:00
extern void (*tColDataCalcSMA[])(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min, int16_t *numOfNull);
2022-09-13 06:21:43 +00:00
// STRUCT ================================
2022-05-11 02:29:53 +00:00
struct STColumn {
col_id_t colId;
int8_t type;
int8_t flags;
int32_t bytes;
int32_t offset;
};
struct STSchema {
2022-05-11 02:37:52 +00:00
int32_t numOfCols;
int32_t version;
int32_t flen;
int32_t vlen;
int32_t tlen;
STColumn columns[];
2022-05-11 02:29:53 +00:00
};
2022-05-12 10:38:35 +00:00
#define TSROW_HAS_NONE ((uint8_t)0x1)
#define TSROW_HAS_NULL ((uint8_t)0x2U)
#define TSROW_HAS_VAL ((uint8_t)0x4U)
2022-06-01 07:10:33 +00:00
#define TSROW_KV_SMALL ((uint8_t)0x10U)
#define TSROW_KV_MID ((uint8_t)0x20U)
#define TSROW_KV_BIG ((uint8_t)0x40U)
2022-07-11 11:43:16 +00:00
#pragma pack(push, 1)
2022-05-11 02:29:53 +00:00
struct STSRow2 {
2022-07-11 11:43:16 +00:00
TSKEY ts;
uint8_t flags;
uint8_t data[];
2022-05-11 02:29:53 +00:00
};
2022-07-11 11:43:16 +00:00
#pragma pack(pop)
2022-05-11 02:29:53 +00:00
struct STSRowBuilder {
2022-07-12 01:47:47 +00:00
// STSRow2 tsRow;
2022-06-02 06:04:31 +00:00
int32_t szBuf;
uint8_t *pBuf;
2022-05-11 02:29:53 +00:00
};
2022-05-31 02:48:45 +00:00
struct SValue {
union {
2022-09-21 07:28:04 +00:00
int64_t val;
2022-05-31 02:48:45 +00:00
struct {
uint32_t nData;
uint8_t *pData;
};
};
};
2022-05-14 06:18:07 +00:00
struct SColVal {
2022-06-01 07:10:33 +00:00
int16_t cid;
2022-06-22 06:34:39 +00:00
int8_t type;
2022-09-23 10:55:27 +00:00
int8_t flag;
2022-05-31 03:08:29 +00:00
SValue value;
2022-05-14 06:18:07 +00:00
};
2022-09-13 06:21:43 +00:00
struct SColData {
int16_t cid;
int8_t type;
int8_t smaOn;
int32_t nVal;
uint8_t flag;
uint8_t *pBitMap;
int32_t *aOffset;
int32_t nData;
uint8_t *pData;
};
2022-05-31 15:38:21 +00:00
#pragma pack(push, 1)
2022-05-17 09:50:21 +00:00
struct STagVal {
2022-08-18 02:09:31 +00:00
// char colName[TSDB_COL_NAME_LEN]; // only used for tmq_get_meta
2022-05-27 13:59:53 +00:00
union {
int16_t cid;
char *pKey;
};
2022-05-30 03:51:36 +00:00
int8_t type;
union {
2022-06-01 12:42:29 +00:00
int64_t i64;
2022-05-30 03:51:36 +00:00
struct {
uint32_t nData;
uint8_t *pData;
};
};
2022-05-17 09:50:21 +00:00
};
2022-06-01 12:42:29 +00:00
#define TD_TAG_JSON ((int8_t)0x40) // distinguish JSON string and JSON value with the highest bit
2022-05-31 13:05:43 +00:00
#define TD_TAG_LARGE ((int8_t)0x20)
2022-05-28 09:16:04 +00:00
struct STag {
2022-05-28 10:10:16 +00:00
int8_t flags;
2022-05-28 09:16:04 +00:00
int16_t len;
int16_t nTag;
int32_t ver;
2022-05-28 10:10:16 +00:00
int8_t idx[];
2022-05-28 09:16:04 +00:00
};
#pragma pack(pop)
2022-05-17 09:50:21 +00:00
#if 1 //================================================================================================================================================
2022-01-24 12:53:04 +00:00
// 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
2022-02-03 09:01:38 +00:00
2021-01-25 03:30:41 +00:00
#define STR_TO_VARSTR(x, str) \
do { \
VarDataLenT __len = (VarDataLenT)strlen(str); \
*(VarDataLenT *)(x) = __len; \
memcpy(varDataVal(x), (str), __len); \
2020-06-01 01:57:55 +00:00
} while (0);
2021-01-24 02:24:44 +00:00
#define STR_WITH_MAXSIZE_TO_VARSTR(x, str, _maxs) \
do { \
2020-06-16 07:22:58 +00:00
char *_e = stpncpy(varDataVal(x), (str), (_maxs)-VARSTR_HEADER_SIZE); \
2021-01-24 02:24:44 +00:00
varDataSetLen(x, (_e - (x)-VARSTR_HEADER_SIZE)); \
2020-06-01 01:57:55 +00:00
} while (0)
2021-01-25 03:30:41 +00:00
#define STR_WITH_SIZE_TO_VARSTR(x, str, _size) \
do { \
*(VarDataLenT *)(x) = (VarDataLenT)(_size); \
memcpy(varDataVal(x), (str), (_size)); \
2020-06-01 01:57:55 +00:00
} while (0);
2020-05-02 09:41:38 +00:00
// ----------------- SCHEMA BUILDER DEFINITION
typedef struct {
2022-03-28 09:58:55 +00:00
int32_t tCols;
int32_t nCols;
schema_ver_t version;
uint16_t flen;
int32_t vlen;
int32_t tlen;
STColumn *columns;
} STSchemaBuilder;
2022-04-09 10:21:03 +00:00
// use 2 bits for bitmap(default: STSRow/sub block)
#define TD_VTYPE_BITS 2
#define TD_VTYPE_PARTS 4 // PARTITIONS: 1 byte / 2 bits
#define TD_VTYPE_OPTR 3 // OPERATOR: 4 - 1, utilize to get remainder
#define TD_BITMAP_BYTES(cnt) (((cnt) + TD_VTYPE_OPTR) >> 2)
// use 1 bit for bitmap(super block)
#define TD_VTYPE_BITS_I 1
#define TD_VTYPE_PARTS_I 8 // PARTITIONS: 1 byte / 1 bit
#define TD_VTYPE_OPTR_I 7 // OPERATOR: 8 - 1, utilize to get remainder
#define TD_BITMAP_BYTES_I(cnt) (((cnt) + TD_VTYPE_OPTR_I) >> 3)
2022-01-26 00:29:13 +00:00
2022-03-28 09:58:55 +00:00
int32_t tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version);
void tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder);
2022-03-28 09:58:55 +00:00
void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version);
2022-04-29 13:32:54 +00:00
int32_t tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes);
STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder);
2020-06-01 09:51:40 +00:00
2022-11-08 06:36:18 +00:00
static FORCE_INLINE int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type) {
if (IS_VAR_DATA_TYPE(type)) {
return tGetBinary(p, &pValue->pData, pValue ? &pValue->nData : NULL);
} else {
memcpy(&pValue->val, p, tDataTypes[type].bytes);
return tDataTypes[type].bytes;
}
}
2022-05-11 02:29:53 +00:00
#endif
2020-03-03 08:17:48 +00:00
#ifdef __cplusplus
}
#endif
2022-01-06 10:11:28 +00:00
#endif /*_TD_COMMON_DATA_FORMAT_H_*/