diff --git a/src/vnode/common/inc/dataformat.h b/src/vnode/common/inc/dataformat.h index b0389955fb18cbac8af4fa280166390d3ce6aead..9e72ed5d6bb98a544cad61a2ec886ab22c873978 100644 --- a/src/vnode/common/inc/dataformat.h +++ b/src/vnode/common/inc/dataformat.h @@ -50,6 +50,8 @@ typedef char * SDataCols; #define TD_DATAROW_LEN(pDataRow) (*(int32_t *)(pDataRow)) #define TD_DATAROW_DATA(pDataRow) ((pDataRow) + sizeof(int32_t)) +SDataRow tdSDataRowDup(SDataRow rdata); + // ---- operation on SDataRows #define TD_DATAROWS_LEN(pDataRows) (*(int32_t *)(pDataRows)) #define TD_DATAROWS_ROWS(pDataRows) (*(int32_t *)(pDataRows + sizeof(int32_t))) diff --git a/src/vnode/common/inc/schema.h b/src/vnode/common/inc/schema.h index bda7e337c0b0350195360f21cc6424d6e52d38c2..5387dbf99bdf9d8f404733db66c1bddd3bafbc32 100644 --- a/src/vnode/common/inc/schema.h +++ b/src/vnode/common/inc/schema.h @@ -67,6 +67,7 @@ typedef char *SISchema; SISchema tdConvertSchemaToInline(SSchema *pSchema); int32_t tdGetColumnIdxByName(SSchema *pSchema, char *colName); int32_t tdGetColumnIdxById(SSchema *pSchema, int32_t colId); +SSchema *tdDupSchema(SSchema *pSchema); // ---- TODO: operations to modify schema diff --git a/src/vnode/tsdb/inc/tsdb.h b/src/vnode/tsdb/inc/tsdb.h index 4065533a1fc628f03454cc646c894b19dea0204c..bf27d127d3f2d4217b436a9c0aae8c904845362f 100644 --- a/src/vnode/tsdb/inc/tsdb.h +++ b/src/vnode/tsdb/inc/tsdb.h @@ -8,6 +8,7 @@ #include #include +#include "dataformat.h" #include "schema.h" #define TSDB_VERSION_MAJOR 1 @@ -35,8 +36,11 @@ typedef struct { char data[]; } SSubmitBlock; +enum { TSDB_PRECISION_MILLI, TSDB_PRECISION_MICRO, TSDB_PRECISION_NANO }; + // the TSDB repository configuration typedef struct { + int8_t precision; int32_t tsdbId; int32_t maxTables; // maximum number of tables this repository can have int32_t daysPerFile; // day per file sharding policy @@ -57,20 +61,18 @@ typedef struct STsdbRepoInfo { // the meter configuration typedef struct { - char * tableName; STableId tableId; - char *superTable; // if not NULL, the table is created from a super table, need to make sure the super - // table exists in this TSDB. int64_t stableUid; + int64_t createdTime; int32_t numOfCols; // number of columns. For table form super table, not includes the tag schema SSchema *schema; // If numOfCols == schema_->numOfCols, it is a normal table, stableName = NULL // If numOfCols < schema->numOfCols, it is a table created from super table // assert(numOfCols <= schema->numOfCols); - char *tagValues; // NULL if it is normal table - // otherwise, it contains the tag values. + SDataRow tagValues; // NULL if it is normal table + // otherwise, it contains the tag values. } STableCfg; // the meter information report structure @@ -154,7 +156,7 @@ int32_t tsdbAlterTable(tsdb_repo_t *repo, STableCfg *pCfg); * * @return 0 for success, -1 for failure and the error number is set */ -int32_t tsdbDropTable(tsdb_repo_t *pRepo, STableId tid, int32_t *error); +int32_t tsdbDropTable(tsdb_repo_t *pRepo, STableId tid); /** * Get the information of a table in the repository @@ -164,7 +166,7 @@ int32_t tsdbDropTable(tsdb_repo_t *pRepo, STableId tid, int32_t *error); * * @return a table information handle for success, NULL for failure and the error number is set */ -STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid, int32_t *error); +STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid); // -- FOR INSERT DATA /** @@ -176,7 +178,7 @@ STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid, int32_t *error); * * @return the number of points inserted, -1 for failure and the error number is set */ -int32_t tsdbInsertData(tsdb_repo_t *pRepo, STableId tid, char *pData, int32_t *error); +int32_t tsdbInsertData(tsdb_repo_t *pRepo, STableId tid, char *pData); // -- FOR QUERY TIME SERIES DATA diff --git a/src/vnode/tsdb/inc/tsdbMeta.h b/src/vnode/tsdb/inc/tsdbMeta.h index 6fdc446dee42804a6bb4b9eff16384cfb1146f8b..21c75212caf71a5f6f3a74d0a4641fa29f87fbd7 100644 --- a/src/vnode/tsdb/inc/tsdbMeta.h +++ b/src/vnode/tsdb/inc/tsdbMeta.h @@ -4,6 +4,8 @@ #include +#include "dataformat.h" + // #include "taosdef.h" // Initially, there are 4 tables @@ -16,14 +18,15 @@ typedef enum { } TSDB_TABLE_TYPE; typedef struct STable { - int32_t tableId; - int64_t uid; + STableId tableId; TSDB_TABLE_TYPE type; int64_t createdTime; - // super table UID - int32_t superTableId; + // super table UID -1 for normal table + int32_t stableUid; + + int32_t numOfCols; // Schema for this table // For TSDB_SUPER_TABLE, it is the schema including tags @@ -34,7 +37,7 @@ typedef struct STable { // Tag value for this table // For TSDB_SUPER_TABLE and TSDB_NTABLE, it is NULL // For TSDB_STABLE, it is the tag value string - char *pTagVal; + SDataRow pTagVal; // Object content; // For TSDB_SUPER_TABLE, it is the index of tables created from it @@ -45,10 +48,10 @@ typedef struct STable { } content; // A handle to deal with event - void *eventHandle; + void *eventHandler; // A handle to deal with stream - void *streamHandle; + void *streamHandler; struct STable *next; @@ -56,7 +59,6 @@ typedef struct STable { typedef struct { int32_t maxTables; - int32_t numOfSuperTables; // Number of super tables (#TSDB_SUPER_TABLE) STable ** tables; // array of normal tables STable * stables; // linked list of super tables void * tableMap; // hash map of uid ==> STable * @@ -89,3 +91,5 @@ int32_t tsdbFreeMeta(STsdbMeta *pMeta); STsdbMeta *tsdbOpenMetaHandle(char *tsdbDir); int32_t tsdbCreateTableImpl(STsdbMeta *pHandle, STableCfg *pCfg); + +int32_t tsdbInsertDataImpl(STsdbMeta *pMeta, STableId tableId, char *pData); \ No newline at end of file diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index 793f67d176cd0dd063841ef4b460e138b85c0152..f1a0701669876e2e9818cda0b918a6364c25e26d 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -200,13 +200,20 @@ int32_t tsdbAlterTable(tsdb_repo_t *pRepo, STableCfg *pCfg) { return 0; } -STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid, int32_t *error) { +int32_t tsdbDropTable(tsdb_repo_t *pRepo, STableId tid) { + return 0; +} + +STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid) { // TODO return NULL; } -int32_t tsdbInsertData(tsdb_repo_t *pRepo, STableId tid, char *pData, int32_t *error) { - // TODO +int32_t tsdbInsertData(tsdb_repo_t *repo, STableId tableId, char *pData) { + STsdbRepo *pRepo = (STsdbRepo *)repo; + + tsdbInsertDataImpl(pRepo->tsdbMeta, tableId, pData); + return 0; } diff --git a/src/vnode/tsdb/src/tsdbMeta.c b/src/vnode/tsdb/src/tsdbMeta.c index b149cf97953c7d2c46dfd3c5b2b163c6ed329120..72781810e11b88871d5c18c6646fb3a6c81d48db 100644 --- a/src/vnode/tsdb/src/tsdbMeta.c +++ b/src/vnode/tsdb/src/tsdbMeta.c @@ -1,9 +1,10 @@ #include // #include "taosdef.h" +#include "hash.h" +#include "tskiplist.h" #include "tsdb.h" #include "tsdbMeta.h" -#include "hash.h" #define TSDB_MIN_TABLES 10 #define TSDB_MAX_TABLES 100000 @@ -11,7 +12,12 @@ #define IS_VALID_MAX_TABLES(maxTables) (((maxTables) >= TSDB_MIN_TABLES) && ((maxTables) >= TSDB_MAX_TABLES)) -static int tsdbFreeTable(STable *pTable); +static int tsdbFreeTable(STable *pTable); +static int32_t tsdbCheckTableCfg(STableCfg *pCfg); +static STable *tsdbGetTableByUid(int64_t uid); +static int tsdbAddTable(STsdbMeta *pMeta, STable *pTable); +static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable); +static int tsdbAddTableIntoIndex(pMeta, pTable); STsdbMeta *tsdbCreateMeta(int32_t maxTables) { if (!IS_VALID_MAX_TABLES(maxTables)) return NULL; @@ -22,7 +28,6 @@ STsdbMeta *tsdbCreateMeta(int32_t maxTables) { } pMeta->maxTables = maxTables; - pMeta->numOfSuperTables = 0; pMeta->stables = NULL; pMeta->tables = (STable **)calloc(maxTables, sizeof(STable *)); if (pMeta->tables == NULL) { @@ -65,19 +70,52 @@ int32_t tsdbFreeMeta(STsdbMeta *pMeta) { return 0; } -static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; } - int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) { if (tsdbCheckTableCfg(pCfg) < 0) { return -1; } + STable *pSTable = NULL; + + if (pCfg->stableUid > 0) { // to create a TSDB_STABLE + pSTable = tsdbGetTableByUid(pCfg->stableUid); + if (pSTable == NULL) { // super table not exists, try to create it + pSTable = (STable *)calloc(1, sizeof(STable)); + if (pSTable == NULL) return -1; + + pSTable->tableId.uid = pCfg->stableUid; + pSTable->tableId.tid = -1; + pSTable->type = TSDB_SUPER_TABLE; + pSTable->createdTime = pCfg->createdTime; // The created time is not required + pSTable->stableUid = -1; + pSTable->numOfCols = pCfg->numOfCols; + pSTable->pSchema = tdDupSchema(pCfg->schema); + pSTable->content.pIndex = tSkipListCreate(5, 0, 10); // TODO: change here + tsdbAddTable(pMeta, pSTable); + } else { + if (pSTable->type != TSDB_SUPER_TABLE) return NULL; + } + } + STable *pTable = (STable *)malloc(sizeof(STable)); if (pTable == NULL) { return -1; } - pMeta->tables[pCfg->tableId.tid] = pTable; + pTable->tableId = pCfg->tableId; + pTable->createdTime = pCfg->createdTime; + if (1 /* */) { // TSDB_STABLE + pTable->type = TSDB_STABLE; + pTable->stableUid = pCfg->stableUid; + pTable->pTagVal = tdSDataRowDup(pCfg->tagValues); + } else { // TSDB_NTABLE + pTable->type = TSDB_NTABLE; + pTable->stableUid = -1; + pTable->pSchema = tdDupSchema(pCfg->schema); + } + pTable->content.pData = tSkipListCreate(5, 0, 10); // TODO: change here + + tsdbAddTable(pMeta, pTable); return 0; } @@ -93,4 +131,52 @@ STsdbMeta *tsdbOpenMetaHandle(char *tsdbDir) { return pMeta; } -static int tsdbFreeTable(STable *pTable) { return 0; } \ No newline at end of file +int32_t tsdbInsertDataImpl(STsdbMeta *pMeta, STableId tableId, char *pData) { + STable *pTable = pMeta->tables[tableId.tid]; + if (pTable == NULL) { + // TODO: deal with the error here + return 0; + } + + if (pTable->tableId.uid != tableId.uid) { + // TODO: deal with the error here + return 0; + } + + return 0; +} + +static int tsdbFreeTable(STable *pTable) { return 0; } + +static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; } + +static STable *tsdbGetTableByUid(int64_t uid) { return NULL; } + +static int tsdbAddTable(STsdbMeta *pMeta, STable *pTable) { + if (pTable->type == TSDB_SUPER_TABLE) { + if (pMeta->stables == NULL) { + pMeta->stables = pTable; + pTable->next = NULL; + } else { + STable *pTemp = pMeta->stables; + pMeta->stables = pTable; + pTable->next = pTemp; + } + } else { + pMeta->tables[pTable->tableId.tid] = pTable; + if (pTable->type == TSDB_STABLE) { + tsdbAddTableIntoIndex(pMeta, pTable); + } + } + + return tsdbAddTableIntoMap(pMeta, pTable); +} + +static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable) { + // TODO: add the table to the map + return 0; +} +static int tsdbAddTableIntoIndex(pMeta, pTable) { + // TODO + return 0; +} \ No newline at end of file