schema.h 3.0 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
// 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
H
Hongze Cheng 已提交
15
  int32_t       offset;   // point offset in a row data
H
more  
Hongze Cheng 已提交
16
  char *        colName;  // the column name
H
more  
Hongze Cheng 已提交
17 18
} SColumn;

H
more  
Hongze Cheng 已提交
19 20 21 22 23 24
// 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 已提交
25 26 27
  SColumn *columns;
} SSchema;

H
more  
Hongze Cheng 已提交
28 29 30 31 32 33 34
/* Inline schema definition
 * +---------+---------+---------+-----+---------+-----------+-----+-----------+
 * | int32_t |         |         |     |         |           |     |           |
 * +---------+---------+---------+-----+---------+-----------+-----+-----------+
 * |   len   | SSchema | SColumn | ... | SColumn | col1_name | ... | colN_name |
 * +---------+---------+---------+-----+---------+-----------+-----+-----------+
 */
H
more  
Hongze Cheng 已提交
35 36 37 38 39 40 41 42 43
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
H
Hongze Cheng 已提交
44
#define TD_COLUMN_OFFSET(pCol) ((pCol)->offset)   // column bytes
H
more  
Hongze Cheng 已提交
45 46 47 48 49 50 51 52
#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 已提交
53 54 55
#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 已提交
56 57
#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 已提交
58

H
more  
Hongze Cheng 已提交
59
// ---- operations on SISchema
H
more  
Hongze Cheng 已提交
60 61
#define TD_ISCHEMA_LEN(pISchema) *((int32_t *)(pISchema))
#define TD_ISCHEMA_SCHEMA(pISchema) ((SSchema *)((pISchema) + sizeof(int32_t)))
H
more  
Hongze Cheng 已提交
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);
H
more  
hzcheng 已提交
70
SSchema *tdDupSchema(SSchema *pSchema);
H
more  
Hongze Cheng 已提交
71

H
Hongze Cheng 已提交
72
// ---- TODO: operations to modify schema
H
more  
Hongze Cheng 已提交
73

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