diff --git a/src/vnode/common/inc/schema.h b/src/vnode/common/inc/schema.h index c8ac6e7f6740043b31c44907e98665894a315c34..869c069295cacc59fc5a5cffee8b393268f0b7e8 100644 --- a/src/vnode/common/inc/schema.h +++ b/src/vnode/common/inc/schema.h @@ -2,6 +2,7 @@ #define _TD_SCHEMA_H_ #include +#include #include "type.h" @@ -30,27 +31,41 @@ typedef struct { * | len | SSchema | SColumn | ... | SColumn | col1_name | ... | colN_name | * +---------+---------+---------+-----+---------+-----------+-----+-----------+ */ -typedef char *SISchema - -// ---- operation on SColumn -#define TD_COLUMN_TYPE(pCol) ((pCol)->type) -#define TD_COLUMN_ID(pCol) ((pCol)->colId) -#define TD_COLUMN_BYTES(pCol) ((pCol)->bytes) -#define TD_COLUMN_NAME(pCol) ((pCol)->colName) - -// ---- operation on SSchema -#define TD_SCHEMA_VERSION(pSchema) ((pSchema)->version) -#define TD_SCHEMA_NCOLS(pSchema) ((pSchema)->numOfCols) -#define TD_SCHEMA_NTAGS(pSchema) ((pSchema)->numOfTags) -#define TD_SCHEMA_TOTAL_COLS(pSchema) (TD_SCHEMA_NCOLS(pSchema) + TD_SCHEMA_NTAGS(pSchema)) +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) #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)) -#define TD_SCHEMA_COLUMN_AT(pSchema, idx) TD_SCHEMA_COLS(pSchema)[idx] -#define TD_SCHEMA_TAG_AT(pSchema, idx) TD_SCHEMA_TAGS(pSchema)[idx] +#define TD_SCHEMA_COLUMN_AT(pSchema, idx) (TD_SCHEMA_COLS(pSchema) + idx) +#define TD_SCHEMA_TAG_AT(pSchema, idx) (TD_SCHEMA_TAGS(pSchema) + idx) -// ---- operation on SISchema +// ---- operations on SISchema #define TD_ISCHEMA_LEN(pISchema) *((int32_t *)(pISchema)) #define TD_ISCHEMA_SCHEMA(pISchema) ((SSchema *)((pISchema) + sizeof(int32_t))) +#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 #endif // _TD_SCHEMA_H_ diff --git a/src/vnode/common/inc/type.h b/src/vnode/common/inc/type.h index c7cf10c52c3456b44696341a09222eda27a61d5e..f44bc982c111c4cad0fc90e6e32ce98808687dee 100644 --- a/src/vnode/common/inc/type.h +++ b/src/vnode/common/inc/type.h @@ -3,7 +3,7 @@ #include -typedef enum : uint8_t { +typedef enum { TD_DATATYPE_INVLD = 0, // invalid data type TD_DATATYPE_BOOL, TD_DATATYPE_TINYINT, diff --git a/src/vnode/common/src/schema.c b/src/vnode/common/src/schema.c new file mode 100644 index 0000000000000000000000000000000000000000..35374a431172ec2842c92d8a502bcba7cca62504 --- /dev/null +++ b/src/vnode/common/src/schema.c @@ -0,0 +1,68 @@ +#include + +#include "schema.h" + +static size_t tdGetEstimatedISchemaLen(SSchema *pSchema) { + size_t colNameLen = 0; + for (size_t i = 0; i < TD_SCHEMA_NCOLS(pSchema); i++) { + colNameLen += (strlen(TD_COLUMN_NAME(TD_SCHEMA_COLUMN_AT(pSchema, i))) + 1); + } + + for (size_t i = 0; i < TD_SCHEMA_NCOLS(pSchema); i++) { + colNameLen += (strlen(TD_COLUMN_NAME(TD_SCHEMA_COLUMN_AT(pSchema, i))) + 1); + } + + return TD_ISCHEMA_HEADER_SIZE + (size_t)TD_SCHEMA_TOTAL_COLS(pSchema) + colNameLen; +} + +SISchema tdConvertSchemaToInline(SSchema *pSchema) { + size_t len = tdGetEstimatedISchemaLen(pSchema); + int32_t totalCols = TD_SCHEMA_TOTAL_COLS(pSchema); + // TODO: if use pISchema is reasonable? + SISchema pISchema = malloc(len); + if (pSchema == NULL) { + // TODO: add error handling + return NULL; + } + + TD_ISCHEMA_LEN(pISchema) = (int32_t)len; + memcpy((void *)TD_ISCHEMA_SCHEMA(pISchema), (void *)pSchema, sizeof(SSchema)); + TD_SCHEMA_COLS(TD_ISCHEMA_SCHEMA(pISchema)) = (SColumn *)(pISchema + TD_ISCHEMA_HEADER_SIZE); + memcpy((void *)TD_SCHEMA_COLS(TD_ISCHEMA_SCHEMA(pISchema)), (void *)TD_SCHEMA_COLS(pSchema), + sizeof(SColumn) * totalCols); + + char *pName = TD_ISCHEMA_COL_NAMES(pISchema); + for (int32_t i = 0; i < totalCols; i++) { + SColumn *pCol = TD_SCHEMA_COLUMN_AT(TD_ISCHEMA_SCHEMA(pISchema), i); + char * colName = TD_COLUMN_NAME(TD_SCHEMA_COLUMN_AT(pSchema, i), i); + + TD_COLUMN_NAME(pCol) = pName; + + size_t tlen = strlen(colName) + 1; + memcpy((void *)pName, (void *)colName, tlen); + pName += tlen; + } + + return pISchema; +} + +int32_t tdGetColumnIdxByName(SSchema *pSchema, char *colName) { + for (int32_t i = 0; i < TD_SCHEMA_TOTAL_COLS(pSchema); i++) { + SColumn *pCol = TD_SCHEMA_COLUMN_AT(pSchema, i); + if (strcmp(colName, TD_COLUMN_NAME(pCol)) == 0) { + return i; + } + } + + return -1; +} + +int32_t tdGetColumnIdxById(SSchema *pSchema, int32_t colId) { + for (int32_t i = 0; i < TD_SCHEMA_TOTAL_COLS(pSchema); i++) { + SColumn *pCol = TD_SCHEMA_COLUMN_AT(pSchema, i); + if (TD_COLUMN_ID(pCol) == colId) { + return i; + } + } + return -1; +} \ No newline at end of file