diff --git a/src/vnode/tsdb/inc/tsdb.h b/src/vnode/tsdb/inc/tsdb.h index f225fde093d7c600073c945c10fb23390a1bc4c2..cc5d8ee5b6f612603873de1c462791a22351bcfc 100644 --- a/src/vnode/tsdb/inc/tsdb.h +++ b/src/vnode/tsdb/inc/tsdb.h @@ -121,7 +121,7 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo); * * @return 0 for success, -1 for failure and the error number is set */ -int32_t tsdbConfigRepo(tsdb_repo_t repo, STsdbCfg *pCfg); +int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg); /** * Get the TSDB repository information, including some statistics diff --git a/src/vnode/tsdb/inc/tsdbMeta.h b/src/vnode/tsdb/inc/tsdbMeta.h index 81c3594952ac9726464e6138c8aa0444afc1df56..30ac5433916fc2007f6f68557c94c23dee28f595 100644 --- a/src/vnode/tsdb/inc/tsdbMeta.h +++ b/src/vnode/tsdb/inc/tsdbMeta.h @@ -84,10 +84,10 @@ SSchema *tsdbGetTableSchema(STable *pTable); #define TSDB_GET_TABLE_OF_NAME(pHandle, name) /* TODO */ // Create a new meta handle with configuration -SMetaHandle * tsdbCreateMeta (int32_t numOfTables); -int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle); +STsdbMeta * tsdbCreateMeta (int32_t maxTables); +int32_t tsdbFreeMeta(STsdbMeta *pMeta); // Recover the meta handle from the file -SMetaHandle * tsdbOpenMetaHandle(char *tsdbDir); +STsdbMeta * tsdbOpenMetaHandle(char *tsdbDir); -int32_t tsdbCreateTableImpl(SMetaHandle *pHandle, STableCfg *pCfg); +int32_t tsdbCreateTableImpl(STsdbMeta *pHandle, STableCfg *pCfg); diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index 72761e834b60783222f55cf0805166dacb9c2f03..187547146468d786e283d71be46d13b0f1ab9be9 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,6 +6,8 @@ #include #include #include +#include +#include // #include "taosdef.h" // #include "disk.h" @@ -16,11 +19,12 @@ enum { TSDB_REPO_STATE_ACTIVE, TSDB_REPO_STATE_CLOSED, TSDB_REPO_STATE_CONFIGURING }; typedef struct _tsdb_repo { + char *rootDir; // TSDB configuration STsdbCfg config; // The meter meta handle of this TSDB repository - SMetaHandle *tsdbMeta; + STsdbMeta *tsdbMeta; // The cache Handle SCacheHandle *tsdbCache; @@ -41,7 +45,8 @@ typedef struct _tsdb_repo { } STsdbRepo; static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg); -static int32_t tsdbCreateRepoFiles(STsdbRepo *pRepo); +static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo); +static int32_t tsdbDestroyRepoEnv(STsdbRepo *pRepo); #define TSDB_GET_TABLE_BY_ID(pRepo, sid) (((STSDBRepo *)pRepo)->pTableList)[sid] #define TSDB_GET_TABLE_BY_NAME(pRepo, name) @@ -63,29 +68,30 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) { return NULL; } + pRepo->rootDir = strdup(rootDir); pRepo->config = *pCfg; pRepo->limiter = limiter; pRepo->tsdbMeta = tsdbCreateMeta(pCfg->maxTables); if (pRepo->tsdbMeta == NULL) { - // TODO: deal with error + free(pRepo->rootDir); free(pRepo); return NULL; } - // TODO: Initialize cache handle pRepo->tsdbCache = tsdbCreateCache(5); if (pRepo->tsdbCache == NULL) { - // TODO: free the object and return error - tsdbFreeMetaHandle(pRepo->tsdbCache); + free(pRepo->rootDir); + tsdbFreeMeta(pRepo->tsdbMeta); free(pRepo); return NULL; } // Create the Meta data file and data directory - if (tsdbCreateRepoFiles(pRepo) < 0) { - // Failed to create and save files - tsdbFreeMetaHandle(pRepo->tsdbCache); + if (tsdbSetRepoEnv(pRepo) < 0) { + free(pRepo->rootDir); + tsdbFreeMeta(pRepo->tsdbMeta); + tsdbFreeCache(pRepo->tsdbCache); free(pRepo); return NULL; } @@ -101,12 +107,16 @@ int32_t tsdbDropRepo(tsdb_repo_t *repo) { pRepo->state = TSDB_REPO_STATE_CLOSED; // Free the metaHandle - tsdbFreeMetaHandle(pRepo->tsdbMeta); + tsdbFreeMeta(pRepo->tsdbMeta); // Free the cache tsdbFreeCache(pRepo->tsdbCache); - tsdbClearFiles(pRepo); + // Destroy the repository info + tsdbDestroyRepoEnv(pRepo); + + free(pRepo->rootDir); + free(pRepo); return 0; } @@ -122,7 +132,7 @@ tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) { } // TODO: Initialize configuration from the file - pRepo->tsdbMeta = tsdbOpenMetaHandle(); + pRepo->tsdbMeta = tsdbCreateMeta(pRepo->config.maxTables); if (pRepo->tsdbMeta == NULL) { free(pRepo); return NULL; @@ -150,9 +160,9 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo) { pRepo->state = TSDB_REPO_STATE_CLOSED; - tsdbFreeMetaHandle(pRepo->tsdbMeta); + tsdbFreeMeta(pRepo->tsdbMeta); - tsdbFreeCache(pRepo->tsdbMeta); + tsdbFreeCache(pRepo->tsdbCache); return 0; } @@ -160,7 +170,7 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo) { int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg) { STsdbRepo *pRepo = (STsdbRepo *)repo; - pRepo->config = pCfg; + pRepo->config = *pCfg; // TODO return 0; } @@ -192,10 +202,61 @@ static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) { return 0; } -static int32_t tsdbCreateRepoFiles(STsdbRepo *pRepo) { - // TODO +static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo) { + char *metaFname = tsdbGetFileName(pRepo->rootDir, "tsdb", TSDB_FILE_TYPE_META); + + int fd = open(metaFname, O_WRONLY|O_CREAT); + if (fd < 0) { + return -1; + } + + if (write(fd, (void *)(&(pRepo->config)), sizeof(STsdbCfg)) < 0) { + return -1; + } + + // Create the data file + char *dirName = calloc(1, strlen(pRepo->rootDir) + strlen("tsdb") + 2); + if (dirName == NULL) { + return -1; + } + + sprintf(dirName, "%s/%s", pRepo->rootDir, dirName); + if (mkdir(dirName, 0755) < 0) { + free(dirName); + return -1; + } + + free(dirName); + + return 0; } -static int32_t tsdbClearFiles(STsdbRepo *pRepo) { - // TODO +static int32_t tsdbDestroyRepoEnv(STsdbRepo *pRepo) { + char fname[128]; + if (pRepo == NULL) return 0; + char *dirName = calloc(1, strlen(pRepo->rootDir) + strlen("tsdb") + 2); + if (dirName == NULL) { + return -1; + } + + sprintf(dirName, "%s/%s", pRepo->rootDir, "tsdb"); + + DIR *dir = opendir(dirName); + if (dir == NULL) return -1; + + struct dirent *dp; + while ((dp = readdir(dir)) != NULL) { + if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) continue; + sprintf(fname, "%s/%s", pRepo->rootDir, dp->d_name); + remove(fname); + } + + closedir(dir); + + rmdir(dirName); + + char *metaFname = tsdbGetFileName(pRepo->rootDir, "tsdb", TSDB_FILE_TYPE_META); + remove(metaFname); + + return 0; } \ No newline at end of file diff --git a/src/vnode/tsdb/src/tsdbMeta.c b/src/vnode/tsdb/src/tsdbMeta.c index f0abc2cd3c88f446bb4439d4282cd3e37e61e15a..6752c6e4c5dc71072f80f451deebd29ed500f4f0 100644 --- a/src/vnode/tsdb/src/tsdbMeta.c +++ b/src/vnode/tsdb/src/tsdbMeta.c @@ -4,7 +4,7 @@ #include "tsdb.h" #include "tsdbMeta.h" -STsdbMeta *tsdbCreateMeta(int32_t maxNumOfTables) { +STsdbMeta *tsdbCreateMeta(int32_t maxTables) { STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta)); if (pMeta == NULL) { return NULL; @@ -12,7 +12,7 @@ STsdbMeta *tsdbCreateMeta(int32_t maxNumOfTables) { pMeta->numOfTables = 0; pMeta->numOfSuperTables = 0; - pMeta->pTables = calloc(sizeof(STable *), numOfTables); + pMeta->pTables = calloc(sizeof(STable *), maxTables); if (pMeta->pTables == NULL) { free(pMeta); return NULL; @@ -29,9 +29,7 @@ STsdbMeta *tsdbCreateMeta(int32_t maxNumOfTables) { return pMeta; } -int32_t tsdbFreeMetaHandle(STsdbMeta *pMetaHandle) { - // TODO - +int32_t tsdbFreeMeta(STsdbMeta *pMeta) { } static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; } @@ -47,7 +45,7 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pHandle, STableCfg *pCfg) { return -1; } - pHandle->pTables[pCfg->tableId] = pTable; + pHandle->pTables[pCfg->tableId.tid] = pTable; // TODO: add name to it return 0;