schema.h 2.9 KB
Newer Older
H
more  
Hongze Cheng 已提交
1 2 3 4
#if !defined(_TD_SCHEMA_H_)
#define _TD_SCHEMA_H_

#include <stdint.h>
H
more  
Hongze Cheng 已提交
5
#include <string.h>
H
more  
Hongze Cheng 已提交
6 7 8

#include "type.h"

H
more  
Hongze Cheng 已提交
9 10 11 12 13 14 15
// Column definition
// TODO: if we need to align the structure
typedef struct {
  td_datatype_t type;     // Column type
  int32_t       colId;    // column ID
  int32_t       bytes;    // column bytes
  char *        colName;  // the column name
H
more  
Hongze Cheng 已提交
16 17
} SColumn;

H
more  
Hongze Cheng 已提交
18 19 20 21 22 23
// Schema definition
typedef struct {
  int32_t  version;  // schema version, it is used to change the schema
  int32_t  numOfCols;
  int32_t  numOfTags;
  int32_t  colIdCounter;
H
more  
Hongze Cheng 已提交
24 25 26
  SColumn *columns;
} SSchema;

H
more  
Hongze Cheng 已提交
27 28 29 30 31 32 33
/* Inline schema definition
 * +---------+---------+---------+-----+---------+-----------+-----+-----------+
 * | int32_t |         |         |     |         |           |     |           |
 * +---------+---------+---------+-----+---------+-----------+-----+-----------+
 * |   len   | SSchema | SColumn | ... | SColumn | col1_name | ... | colN_name |
 * +---------+---------+---------+-----+---------+-----------+-----+-----------+
 */
H
more  
Hongze Cheng 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
typedef char *SISchema;

// TODO: decide if the space is allowed
#define TD_ISCHEMA_HEADER_SIZE sizeof(int32_t) + sizeof(SSchema)

// ---- operations on SColumn
#define TD_COLUMN_TYPE(pCol) ((pCol)->type)     // column type
#define TD_COLUMN_ID(pCol) ((pCol)->colId)      // column ID
#define TD_COLUMN_BYTES(pCol) ((pCol)->bytes)   // column bytes
#define TD_COLUMN_NAME(pCol) ((pCol)->colName)  // column name
#define TD_COLUMN_INLINE_SIZE(pCol) (sizeof(SColumn) + TD_COLUMN_NAME(pCol) + 1)

// ---- operations on SSchema
#define TD_SCHEMA_VERSION(pSchema) ((pSchema)->version)      // schema version
#define TD_SCHEMA_NCOLS(pSchema) ((pSchema)->numOfCols)      // schema number of columns
#define TD_SCHEMA_NTAGS(pSchema) ((pSchema)->numOfTags)      // schema number of tags
#define TD_SCHEMA_TOTAL_COLS(pSchema) (TD_SCHEMA_NCOLS(pSchema) + TD_SCHEMA_NTAGS(pSchema)) // schema total number of SColumns (#columns + #tags)
H
more  
Hongze Cheng 已提交
51 52 53
#define TD_SCHEMA_NEXT_COLID(pSchema) ((pSchema)->colIdCounter++)
#define TD_SCHEMA_COLS(pSchema) ((pSchema)->columns)
#define TD_SCHEMA_TAGS(pSchema) (TD_SCHEMA_COLS(pSchema) + TD_SCHEMA_NCOLS(pSchema))
H
more  
Hongze Cheng 已提交
54 55
#define TD_SCHEMA_COLUMN_AT(pSchema, idx) (TD_SCHEMA_COLS(pSchema) + idx)
#define TD_SCHEMA_TAG_AT(pSchema, idx) (TD_SCHEMA_TAGS(pSchema) + idx)
H
more  
Hongze Cheng 已提交
56

H
more  
Hongze Cheng 已提交
57
// ---- operations on SISchema
H
more  
Hongze Cheng 已提交
58 59
#define TD_ISCHEMA_LEN(pISchema) *((int32_t *)(pISchema))
#define TD_ISCHEMA_SCHEMA(pISchema) ((SSchema *)((pISchema) + sizeof(int32_t)))
H
more  
Hongze Cheng 已提交
60 61 62 63 64 65 66 67 68 69
#define TD_ISCHEMA_COL_NAMES(pISchema) ((pISchema) + TD_ISCHEMA_HEADER_SIZE + sizeof(SColumn) * TD_SCHEMA_TOTAL_COLS(TD_ISCHEMA_SCHEMA(pISchema)))

// ----
/* Convert a schema structure to an inline schema structure
 */
SISchema tdConvertSchemaToInline(SSchema *pSchema);
int32_t tdGetColumnIdxByName(SSchema *pSchema, char *colName);
int32_t tdGetColumnIdxById(SSchema *pSchema, int32_t colId);

// ---- TODO: operations to change schema
H
more  
Hongze Cheng 已提交
70

H
more  
Hongze Cheng 已提交
71
#endif  // _TD_SCHEMA_H_