diff --git a/src/vnode/common/inc/dataformat.h b/src/vnode/common/inc/dataformat.h index 9399d38023f2a8cff6ba5b840575d9ed40d8b7c5..db2ab72a836f6e37baaccb34e210660e054029c9 100644 --- a/src/vnode/common/inc/dataformat.h +++ b/src/vnode/common/inc/dataformat.h @@ -68,6 +68,7 @@ typedef char * SDataCols; #define TD_DATAROW_DATA(pDataRow) ((pDataRow) + sizeof(int32_t)) SDataRow tdSDataRowDup(SDataRow rdata); +void tdFreeSDataRow(SDataRow rdata); // ---- operation on SDataRows #define TD_DATAROWS_LEN(pDataRows) (*(int32_t *)(pDataRows)) diff --git a/src/vnode/common/inc/schema.h b/src/vnode/common/inc/schema.h index 5387dbf99bdf9d8f404733db66c1bddd3bafbc32..46610135b91545aeeda4a5294ef3055d1c78fc6b 100644 --- a/src/vnode/common/inc/schema.h +++ b/src/vnode/common/inc/schema.h @@ -68,6 +68,7 @@ SISchema tdConvertSchemaToInline(SSchema *pSchema); int32_t tdGetColumnIdxByName(SSchema *pSchema, char *colName); int32_t tdGetColumnIdxById(SSchema *pSchema, int32_t colId); SSchema *tdDupSchema(SSchema *pSchema); +void tdFreeSchema(SSchema *pSchema); // ---- TODO: operations to modify schema diff --git a/src/vnode/common/src/dataformat.c b/src/vnode/common/src/dataformat.c index 1f2dfc7dad573e75e0066ccd4819142ee452e166..23351e93656ac60855bfde1c910987e515d086f1 100644 --- a/src/vnode/common/src/dataformat.c +++ b/src/vnode/common/src/dataformat.c @@ -31,4 +31,8 @@ int32_t tdGetMaxDataRowSize(SSchema *pSchema) { return nbytes; } -SDataRow tdSDataRowDup(SDataRow rdata) { return NULL; } \ No newline at end of file +SDataRow tdSDataRowDup(SDataRow rdata) { return NULL; } +void tdFreeSDataRow(SDataRow rdata) { + if (rdata == NULL) return; + free(rdata); +} \ No newline at end of file diff --git a/src/vnode/common/src/schema.c b/src/vnode/common/src/schema.c index eb1b4eb84b3d716c7091553ffde83135b75fc691..0f1923dbcaaf493bacb712cbe1973736901505ec 100644 --- a/src/vnode/common/src/schema.c +++ b/src/vnode/common/src/schema.c @@ -13,6 +13,10 @@ const int32_t rowDataLen[] = { sizeof(int32_t), // TD_DATATYPE_NCHAR, sizeof(int32_t) // TD_DATATYPE_BINARY }; +void tdFreeSchema(SSchema *pSchema) { + // TODO + return; +} static size_t tdGetEstimatedISchemaLen(SSchema *pSchema) { size_t colNameLen = 0; diff --git a/src/vnode/tsdb/CMakeLists.txt b/src/vnode/tsdb/CMakeLists.txt index 4e66b98528b10685163fcf99880fe6c025709b10..91c7d571e8ce8937d75df1289d4c059c60e0c618 100644 --- a/src/vnode/tsdb/CMakeLists.txt +++ b/src/vnode/tsdb/CMakeLists.txt @@ -5,6 +5,7 @@ target_link_libraries(tsdb common tutil) target_include_directories(tsdb PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/inc" + PUBLIC "${CMAKE_SOURCE_DIR}/src/inc" PUBLIC "${CMAKE_SOURCE_DIR}/src/util/inc" PUBLIC "${CMAKE_SOURCE_DIR}/src/os/linux/inc" ) \ No newline at end of file diff --git a/src/vnode/tsdb/inc/tsdb.h b/src/vnode/tsdb/inc/tsdb.h index 91e159a1a46a1523a567dd67422b8635352bd02a..53644e4b43b5048cff8b7c29f677dee4bb4547e6 100644 --- a/src/vnode/tsdb/inc/tsdb.h +++ b/src/vnode/tsdb/inc/tsdb.h @@ -182,7 +182,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 tsdbDropTable(tsdb_repo_t *pRepo, STableId tableId); /** * Get the information of a table in the repository diff --git a/src/vnode/tsdb/inc/tsdbMeta.h b/src/vnode/tsdb/inc/tsdbMeta.h index bbd4ae7f737b2c4fc974d0cfd2cdbc3e71905027..240716121a3fdcdbc6de36b86f50ab0ad203e2b6 100644 --- a/src/vnode/tsdb/inc/tsdbMeta.h +++ b/src/vnode/tsdb/inc/tsdbMeta.h @@ -17,6 +17,7 @@ #include +#include "tsdb.h" #include "dataformat.h" #ifdef __cplusplus @@ -34,6 +35,8 @@ typedef enum { TSDB_STABLE // table created from super table } TSDB_TABLE_TYPE; +#define IS_CREATE_STABLE(pCfg) ((pCfg)->tagValues != NULL) + typedef struct STable { STableId tableId; TSDB_TABLE_TYPE type; @@ -106,9 +109,10 @@ STsdbMeta *tsdbCreateMeta(int32_t maxTables); int32_t tsdbFreeMeta(STsdbMeta *pMeta); // Recover the meta handle from the file -STsdbMeta *tsdbOpenMetaHandle(char *tsdbDir); +STsdbMeta *tsdbOpenMeta(char *tsdbDir); -int32_t tsdbCreateTableImpl(STsdbMeta *pHandle, STableCfg *pCfg); +int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg); +int32_t tsdbDropTableImpl(STsdbMeta *pMeta, STableId tableId); int32_t tsdbInsertDataImpl(STsdbMeta *pMeta, STableId tableId, char *pData); diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index e73fa8672d621c341aabf331bb4cee3853ebe6dd..254bd4b01661d33ee52cd72e1d5d73911a2c15d4 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -244,9 +244,15 @@ int32_t tsdbAlterTable(tsdb_repo_t *pRepo, STableCfg *pCfg) { return 0; } -int32_t tsdbDropTable(tsdb_repo_t *pRepo, STableId tid) { return 0; } +int32_t tsdbDropTable(tsdb_repo_t *repo, STableId tableId) { + // TODO + if (repo == NULL) return -1; + STsdbRepo *pRepo = (STsdbRepo *)repo; + + return tsdbDropTableImpl(pRepo->tsdbMeta, tableId); +} -STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid) { +STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tableId) { // TODO return NULL; } diff --git a/src/vnode/tsdb/src/tsdbMeta.c b/src/vnode/tsdb/src/tsdbMeta.c index a6cf5d2d1be3c904d0af3efcda2dc95880510d9e..00a9469cc65a1f00edcb15f58f8355b501ff0e70 100644 --- a/src/vnode/tsdb/src/tsdbMeta.c +++ b/src/vnode/tsdb/src/tsdbMeta.c @@ -1,19 +1,21 @@ #include // #include "taosdef.h" -#include "hash.h" #include "tskiplist.h" #include "tsdb.h" +#include "taosdef.h" #include "tsdbMeta.h" +#include "hash.h" #define TSDB_SUPER_TABLE_SL_LEVEL 5 // TODO: may change here 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 STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t uid); +static int tsdbAddTableToMeta(STsdbMeta *pMeta, STable *pTable); static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable); static int tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable); +static int tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable); STsdbMeta *tsdbCreateMeta(int32_t maxTables) { STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta)); @@ -22,6 +24,7 @@ STsdbMeta *tsdbCreateMeta(int32_t maxTables) { } pMeta->maxTables = maxTables; + pMeta->nTables = 0; pMeta->stables = NULL; pMeta->tables = (STable **)calloc(maxTables, sizeof(STable *)); if (pMeta->tables == NULL) { @@ -70,23 +73,28 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) { } STable *pSTable = NULL; + int newSuper = 0; - if (pCfg->stableUid > 0) { // to create a TSDB_STABLE - pSTable = tsdbGetTableByUid(pCfg->stableUid); + if (IS_CREATE_STABLE(pCfg)) { // to create a TSDB_STABLE, check if super table exists + pSTable = tsdbGetTableByUid(pMeta, pCfg->stableUid); if (pSTable == NULL) { // super table not exists, try to create it + newSuper = 1; 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->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(TSDB_SUPER_TABLE_SL_LEVEL, 0, 8, 0, 0, NULL); // TODO: change here - tsdbAddTable(pMeta, pSTable); + pSTable->content.pIndex = tSkipListCreate(TSDB_SUPER_TABLE_SL_LEVEL, TSDB_DATA_TYPE_TIMESTAMP, sizeof(int64_t), 1, + 0, NULL); // Allow duplicate key, no lock + if (pSTable->content.pIndex == NULL) { + free(pSTable); + return NULL; + } } else { if (pSTable->type != TSDB_SUPER_TABLE) return NULL; } @@ -94,12 +102,13 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) { STable *pTable = (STable *)malloc(sizeof(STable)); if (pTable == NULL) { + if (newSuper) tsdbFreeTable(pSTable); return -1; } pTable->tableId = pCfg->tableId; pTable->createdTime = pCfg->createdTime; - if (1 /* */) { // TSDB_STABLE + if (IS_CREATE_STABLE(pCfg)) { // TSDB_STABLE pTable->type = TSDB_STABLE; pTable->stableUid = pCfg->stableUid; pTable->pTagVal = tdSDataRowDup(pCfg->tagValues); @@ -110,13 +119,14 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) { } pTable->content.pData = tSkipListCreate(TSDB_SUPER_TABLE_SL_LEVEL, 0, 8, 0, 0, NULL); - tsdbAddTable(pMeta, pTable); + if (newSuper) tsdbAddTableToMeta(pMeta, pSTable); + tsdbAddTableToMeta(pMeta, pTable); return 0; } -STsdbMeta *tsdbOpenMetaHandle(char *tsdbDir) { - // Open meta file for reading +STsdbMeta *tsdbOpenMeta(char *tsdbDir) { + // TODO : Open meta file for reading STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta)); if (pMeta == NULL) { @@ -141,14 +151,59 @@ int32_t tsdbInsertDataImpl(STsdbMeta *pMeta, STableId tableId, char *pData) { return 0; } -static int tsdbFreeTable(STable *pTable) { return 0; } - -static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; } +int32_t tsdbDropTableImpl(STsdbMeta *pMeta, STableId tableId) { + if (pMeta == NULL) return -1; -static STable *tsdbGetTableByUid(int64_t uid) { return NULL; } + STable *pTable = tsdbGetTableByUid(pMeta, tableId.uid); + if (pTable == NULL) return -1; -static int tsdbAddTable(STsdbMeta *pMeta, STable *pTable) { if (pTable->type == TSDB_SUPER_TABLE) { + // TODO: implement drop super table + return -1; + } else { + pMeta->tables[pTable->tableId.tid] = NULL; + pMeta->nTables--; + assert(pMeta->nTables >= 0); + if (pTable->type == TSDB_STABLE) { + tsdbRemoveTableFromIndex(pMeta, pTable); + } + + tsdbFreeTable(pTable); + } +} + +static int tsdbFreeTable(STable *pTable) { + // TODO: finish this function + if (pTable->type == TSDB_STABLE) { + tdFreeSDataRow(pTable->pTagVal); + + } else { + tdFreeSchema(pTable->pSchema); + } + + // Free content + if (TSDB_TABLE_IS_SUPER_TABLE(pTable)) { + tSkipListDestroy(pTable->content.pIndex); + } else { + tSkipListDestroy(pTable->content.pData); + } + + free(pTable); + return 0; +} + +static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { + // TODO + return 0; +} + +static STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t uid) { + return (STable *)taosGetDataFromHashTable(pMeta->tableMap, (char *)(&uid), sizeof(uid)); +} + +static int tsdbAddTableToMeta(STsdbMeta *pMeta, STable *pTable) { + if (pTable->type == TSDB_SUPER_TABLE) { + // add super table to the linked list if (pMeta->stables == NULL) { pMeta->stables = pTable; pTable->next = NULL; @@ -158,20 +213,38 @@ static int tsdbAddTable(STsdbMeta *pMeta, STable *pTable) { pTable->next = pTemp; } } else { + // add non-super table to the array pMeta->tables[pTable->tableId.tid] = pTable; if (pTable->type == TSDB_STABLE) { + // add STABLE to the index tsdbAddTableIntoIndex(pMeta, pTable); } + pMeta->nTables++; } return tsdbAddTableIntoMap(pMeta, pTable); } +static int tsdbRemoveTableFromMeta(STsdbMeta *pMeta, STable *pTable) { + // TODO +} + static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable) { // TODO: add the table to the map + int64_t uid = pTable->tableId.uid; + if (taosAddToHashTable(pMeta->tableMap, (char *)(&uid), sizeof(uid), (void *)(&pTable), sizeof(pTable)) < 0) { + return -1; + } return 0; } static int tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable) { + assert(pTable->type == TSDB_STABLE); + // TODO + return 0; +} + +static int tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable) { + assert(pTable->type == TSDB_STABLE); // TODO return 0; } \ No newline at end of file