From c80686b9614c31de6a7c243f64fc5caba760cdee Mon Sep 17 00:00:00 2001 From: hzcheng Date: Thu, 12 Mar 2020 10:39:44 +0800 Subject: [PATCH] add more and refactor --- src/vnode/tsdb/inc/tsdbMeta.h | 28 +++++++++------- src/vnode/tsdb/inc/tsdbMetaFile.h | 3 +- src/vnode/tsdb/src/tsdbMain.c | 18 +++++------ src/vnode/tsdb/src/tsdbMeta.c | 53 ++++++++++++++++--------------- src/vnode/tsdb/src/tsdbMetaFile.c | 2 +- 5 files changed, 53 insertions(+), 51 deletions(-) diff --git a/src/vnode/tsdb/inc/tsdbMeta.h b/src/vnode/tsdb/inc/tsdbMeta.h index 73bafb112f..a86a206950 100644 --- a/src/vnode/tsdb/inc/tsdbMeta.h +++ b/src/vnode/tsdb/inc/tsdbMeta.h @@ -20,6 +20,7 @@ #include "tsdb.h" #include "dataformat.h" #include "tskiplist.h" +#include "tsdbMetaFile.h" #ifdef __cplusplus extern "C" { @@ -78,14 +79,24 @@ typedef struct STable { } STable; +// ---------- TSDB META HANDLE DEFINITION typedef struct { - int32_t maxTables; - int32_t nTables; - STable **tables; // array of normal tables - STable * stables; // linked list of super tables // TODO use container to implement this - void * tableMap; // hash map of uid ==> STable * + int32_t maxTables; // Max number of tables + + int32_t nTables; // Tables created + + STable **tables; // table array + + STable *superList; // super table list TODO: change it to list container + + void *map; // table map of (uid ===> table) + + SMetaFile *mfh; // meta file handle } STsdbMeta; +STsdbMeta *tsdbInitMeta(const char *rootDir, int32_t maxTables); +int32_t tsdbFreeMeta(STsdbMeta *pMeta); + // ---- Operation on STable #define TSDB_TABLE_ID(pTable) ((pTable)->tableId) #define TSDB_TABLE_UID(pTable) ((pTable)->uid) @@ -105,13 +116,6 @@ STSchema *tsdbGetTableSchema(STable *pTable); #define TSDB_TABLE_OF_ID(pHandle, id) ((pHandle)->pTables)[id] #define TSDB_GET_TABLE_OF_NAME(pHandle, name) /* TODO */ -// Create a new meta handle with configuration -STsdbMeta *tsdbInitMeta(int32_t maxTables); -int32_t tsdbFreeMeta(STsdbMeta *pMeta); - -// Recover the meta handle from the file -STsdbMeta *tsdbOpenMeta(char *tsdbDir); - int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg); int32_t tsdbDropTableImpl(STsdbMeta *pMeta, STableId tableId); STable *tsdbIsValidTableToInsert(STsdbMeta *pMeta, STableId tableId); diff --git a/src/vnode/tsdb/inc/tsdbMetaFile.h b/src/vnode/tsdb/inc/tsdbMetaFile.h index 758cb57958..9fad703842 100644 --- a/src/vnode/tsdb/inc/tsdbMetaFile.h +++ b/src/vnode/tsdb/inc/tsdbMetaFile.h @@ -18,13 +18,12 @@ #include -#include "tsdbMeta.h" - #ifdef __cplusplus extern "C" { #endif #define TSDB_META_FILE_NAME "META" +#define TSDB_META_HASH_FRACTION 1.1 typedef struct { int fd; // File descriptor diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index 5db2545c53..c608353645 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -145,8 +145,15 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO pRepo->config = *pCfg; pRepo->limiter = limiter; + // Create the environment files and directories + if (tsdbSetRepoEnv(pRepo) < 0) { + free(pRepo->rootDir); + free(pRepo); + return NULL; + } + // Initialize meta - STsdbMeta *pMeta = tsdbInitMeta(pCfg->maxTables); + STsdbMeta *pMeta = tsdbInitMeta(rootDir, pCfg->maxTables); if (pMeta == NULL) { free(pRepo->rootDir); free(pRepo); @@ -164,15 +171,6 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO } pRepo->tsdbCache = pCache; - // Create the environment files and directories - if (tsdbSetRepoEnv(pRepo) < 0) { - free(pRepo->rootDir); - tsdbFreeMeta(pRepo->tsdbMeta); - tsdbFreeCache(pRepo->tsdbCache); - free(pRepo); - return NULL; - } - pRepo->state = TSDB_REPO_STATE_ACTIVE; return (tsdb_repo_t *)pRepo; diff --git a/src/vnode/tsdb/src/tsdbMeta.c b/src/vnode/tsdb/src/tsdbMeta.c index 23442ed1db..4f943c3b41 100644 --- a/src/vnode/tsdb/src/tsdbMeta.c +++ b/src/vnode/tsdb/src/tsdbMeta.c @@ -18,23 +18,33 @@ static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable); static int tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable); static int tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable); -STsdbMeta *tsdbInitMeta(int32_t maxTables) { +/** + * Initialize the meta handle + * ASSUMPTIONS: VALID PARAMETER + */ +STsdbMeta *tsdbInitMeta(const char *rootDir, int32_t maxTables) { STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta)); - if (pMeta == NULL) { - return NULL; - } + if (pMeta == NULL) return NULL; pMeta->maxTables = maxTables; pMeta->nTables = 0; - pMeta->stables = NULL; + pMeta->superList = NULL; pMeta->tables = (STable **)calloc(maxTables, sizeof(STable *)); if (pMeta->tables == NULL) { free(pMeta); return NULL; } - pMeta->tableMap = taosInitHashTable(maxTables + maxTables / 10, taosGetDefaultHashFunction, false); - if (pMeta->tableMap == NULL) { + pMeta->map = taosInitHashTable(maxTables * TSDB_META_HASH_FRACTION, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false); + if (pMeta->map == NULL) { + free(pMeta->tables); + free(pMeta); + return NULL; + } + + pMeta->mfh = tsdbInitMetaFile(rootDir, maxTables); + if (pMeta->mfh == NULL) { + taosCleanUpHashTable(pMeta->map); free(pMeta->tables); free(pMeta); return NULL; @@ -46,6 +56,8 @@ STsdbMeta *tsdbInitMeta(int32_t maxTables) { int32_t tsdbFreeMeta(STsdbMeta *pMeta) { if (pMeta == NULL) return 0; + tsdbCloseMetaFile(pMeta->mfh); + for (int i = 0; i < pMeta->maxTables; i++) { if (pMeta->tables[i] != NULL) { tsdbFreeTable(pMeta->tables[i]); @@ -54,14 +66,14 @@ int32_t tsdbFreeMeta(STsdbMeta *pMeta) { free(pMeta->tables); - STable *pTable = pMeta->stables; + STable *pTable = pMeta->superList; while (pTable != NULL) { STable *pTemp = pTable; pTable = pTemp->next; tsdbFreeTable(pTemp); } - taosCleanUpHashTable(pMeta->tableMap); + taosCleanUpHashTable(pMeta->map); free(pMeta); @@ -126,17 +138,6 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) { return 0; } -STsdbMeta *tsdbOpenMeta(char *tsdbDir) { - // TODO : Open meta file for reading - - STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta)); - if (pMeta == NULL) { - return NULL; - } - - return pMeta; -} - /** * Check if a table is valid to insert. * @return NULL for invalid and the pointer to the table if valid @@ -206,7 +207,7 @@ static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { } STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t uid) { - void *ptr = taosGetDataFromHashTable(pMeta->tableMap, (char *)(&uid), sizeof(uid)); + void *ptr = taosGetDataFromHashTable(pMeta->map, (char *)(&uid), sizeof(uid)); if (ptr == NULL) return NULL; @@ -216,12 +217,12 @@ STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t 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; + if (pMeta->superList == NULL) { + pMeta->superList = pTable; pTable->next = NULL; } else { - STable *pTemp = pMeta->stables; - pMeta->stables = pTable; + STable *pTemp = pMeta->superList; + pMeta->superList = pTable; pTable->next = pTemp; } } else { @@ -245,7 +246,7 @@ static int tsdbRemoveTableFromMeta(STsdbMeta *pMeta, STable *pTable) { 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) { + if (taosAddToHashTable(pMeta->map, (char *)(&uid), sizeof(uid), (void *)(&pTable), sizeof(pTable)) < 0) { return -1; } return 0; diff --git a/src/vnode/tsdb/src/tsdbMetaFile.c b/src/vnode/tsdb/src/tsdbMetaFile.c index 828d722389..8f99afdcdd 100644 --- a/src/vnode/tsdb/src/tsdbMetaFile.c +++ b/src/vnode/tsdb/src/tsdbMetaFile.c @@ -15,11 +15,11 @@ #include #include +#include "taosdef.h" #include "hash.h" #include "tsdbMetaFile.h" #define TSDB_META_FILE_HEADER_SIZE 512 -#define TSDB_META_HASH_FRACTION 1.1 typedef struct { int32_t offset; -- GitLab