提交 4ecdb6d5 编写于 作者: H Hongze Cheng

more

上级 8676d969
......@@ -2,6 +2,7 @@
#define _TD_SCHEMA_H_
#include <stdint.h>
#include <string.h>
#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_
......@@ -3,7 +3,7 @@
#include <stdint.h>
typedef enum : uint8_t {
typedef enum {
TD_DATATYPE_INVLD = 0, // invalid data type
TD_DATATYPE_BOOL,
TD_DATATYPE_TINYINT,
......
#include <stdlib.h>
#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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册