From f4959b038aae0f6bdcb3a1b74c634a72290d7294 Mon Sep 17 00:00:00 2001 From: hzcheng Date: Wed, 19 Feb 2020 13:45:18 +0000 Subject: [PATCH] more --- src/vnode/tests/tsdb/tsdbTests.cpp | 7 +- src/vnode/tsdb/inc/tsdb.h | 29 ++++---- src/vnode/tsdb/inc/tsdbCache.h | 1 + src/vnode/tsdb/inc/tsdbFile.h | 12 ++-- src/vnode/tsdb/inc/tsdbMeta.h | 7 +- src/vnode/tsdb/src/tsdb.c | 109 +++++++++++++++++++++-------- src/vnode/tsdb/src/tsdbCache.c | 3 + src/vnode/tsdb/src/tsdbMeta.c | 18 ++++- 8 files changed, 125 insertions(+), 61 deletions(-) diff --git a/src/vnode/tests/tsdb/tsdbTests.cpp b/src/vnode/tests/tsdb/tsdbTests.cpp index b3e6e22dad..580279e566 100644 --- a/src/vnode/tests/tsdb/tsdbTests.cpp +++ b/src/vnode/tests/tsdb/tsdbTests.cpp @@ -6,10 +6,7 @@ TEST(TsdbTest, createTsdbRepo) { STSDBCfg *pCfg = (STSDBCfg *)malloc(sizeof(STSDBCfg)); - pCfg->rootDir = "/var/lib/taos/"; + free(pCfg); - int32_t err_num = 0; - - tsdb_repo_t *pRepo = tsdbCreateRepo(pCfg, &err_num); - ASSERT_EQ(pRepo, NULL); + ASSERT_EQ(1, 2/2); } \ No newline at end of file diff --git a/src/vnode/tsdb/inc/tsdb.h b/src/vnode/tsdb/inc/tsdb.h index b04f0148f0..b46f886b5d 100644 --- a/src/vnode/tsdb/inc/tsdb.h +++ b/src/vnode/tsdb/inc/tsdb.h @@ -21,6 +21,7 @@ typedef int16_t tsdb_id_t; // TSDB repository ID // Submit message typedef struct { int32_t numOfTables; + int32_t compressed; char data[]; } SSubmitMsg; @@ -111,49 +112,44 @@ typedef struct { /** * Create a new TSDB repository * @param pCfg the TSDB repository configuration, upper layer to free the pointer - * @param error the error number to set when failure occurs * * @return a TSDB repository handle on success, NULL for failure and the error number is set */ -tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg, int32_t *error); +tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg); /** * Close and free all resources taken by the repository - * @param pRepo the TSDB repository handle. The interface will free the handle too, so upper + * @param repo the TSDB repository handle. The interface will free the handle too, so upper * layer do NOT need to free the repo handle again. - * @param error the error number to set when failure occurs * * @return 0 for success, -1 for failure and the error number is set */ -int32_t tsdbDropRepo(tsdb_repo_t *pRepo, int32_t *error); +int32_t tsdbDropRepo(tsdb_repo_t *repo); /** * Open an existing TSDB storage repository * @param tsdbDir the existing TSDB root directory - * @param error the error number to set when failure occurs * * @return a TSDB repository handle on success, NULL for failure and the error number is set */ -tsdb_repo_t *tsdbOpenRepo(char *tsdbDir, int32_t *error); +tsdb_repo_t *tsdbOpenRepo(char *tsdbDir); /** * Close a TSDB repository. Only free memory resources, and keep the files. - * @param pRepo the opened TSDB repository handle. The interface will free the handle too, so upper + * @param repo the opened TSDB repository handle. The interface will free the handle too, so upper * layer do NOT need to free the repo handle again. - * @param error the error number to set when failure occurs * * @return 0 for success, -1 for failure and the error number is set */ -int32_t tsdbCloseRepo(tsdb_repo_t *pRepo, int32_t *error); +int32_t tsdbCloseRepo(tsdb_repo_t *repo); /** * Change the configuration of a repository * @param pCfg the repository configuration, the upper layer should free the pointer - * @param error the error number to set when failure occurs * * @return 0 for success, -1 for failure and the error number is set */ -int32_t tsdbConfigRepo(STSDBCfg *pCfg, int32_t *error); +int32_t tsdbConfigRepo(tsdb_repo_t repo, STSDBCfg *pCfg); /** * Get the TSDB repository information, including some statistics @@ -163,20 +159,19 @@ int32_t tsdbConfigRepo(STSDBCfg *pCfg, int32_t *error); * @return a info struct handle on success, NULL for failure and the error number is set. The upper * layers should free the info handle themselves or memory leak will occur */ -STSDBRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo, int32_t *error); +STSDBRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo); // -- For table manipulation /** * Create/Alter a table in a TSDB repository handle - * @param pRepo the TSDB repository handle + * @param repo the TSDB repository handle * @param pCfg the table configurations, the upper layer should free the pointer - * @param error the error number to set when failure occurs * * @return 0 for success, -1 for failure and the error number is set */ -int32_t tsdbCreateTable(tsdb_repo_t *pRepo, STableCfg *pCfg, int32_t *error); -int32_t tsdbAlterTable(tsdb_repo_t *pRepo, STableCfg *pCfg, int32_t *error); +int32_t tsdbCreateTable(tsdb_repo_t *repo, STableCfg *pCfg); +int32_t tsdbAlterTable(tsdb_repo_t *repo, STableCfg *pCfg); /** * Drop a table in a repository and free all the resources it takes diff --git a/src/vnode/tsdb/inc/tsdbCache.h b/src/vnode/tsdb/inc/tsdbCache.h index b439be08aa..6521b0fd9f 100644 --- a/src/vnode/tsdb/inc/tsdbCache.h +++ b/src/vnode/tsdb/inc/tsdbCache.h @@ -36,5 +36,6 @@ typedef struct STSDBCache { #define TSDB_PREV_CACHE_BLOCK(pBlock) ((pBlock)->prev) SCacheHandle *tsdbCreateCache(int32_t numOfBlocks); +int32_t tsdbFreeCache(SCacheHandle *pHandle); #endif // _TD_TSDBCACHE_H_ diff --git a/src/vnode/tsdb/inc/tsdbFile.h b/src/vnode/tsdb/inc/tsdbFile.h index 02eb0c7881..8ea0a8b0f7 100644 --- a/src/vnode/tsdb/inc/tsdbFile.h +++ b/src/vnode/tsdb/inc/tsdbFile.h @@ -29,12 +29,12 @@ typedef struct { SFileInfo fInfo; } SFILE; -typedef struct { - int64_t offset; - int64_t skey; - int64_t ekey; - int16_t numOfBlocks; -} SDataBlock; +// typedef struct { +// int64_t offset; +// int64_t skey; +// int64_t ekey; +// int16_t numOfBlocks; +// } SDataBlock; char *tsdbGetFileName(char *dirName, char *fname, TSDB_FILE_TYPE type); diff --git a/src/vnode/tsdb/inc/tsdbMeta.h b/src/vnode/tsdb/inc/tsdbMeta.h index 588394459e..17d49ba38a 100644 --- a/src/vnode/tsdb/inc/tsdbMeta.h +++ b/src/vnode/tsdb/inc/tsdbMeta.h @@ -84,9 +84,10 @@ SSchema *tsdbGetTableSchema(STable *pTable); #define TSDB_GET_TABLE_OF_NAME(pHandle, name) /* TODO */ // Create a new meta handle with configuration -SMetaHandle * tsdbCreateMetaHandle (int32_t numOfTables); +SMetaHandle * tsdbCreateMetaHandle (int32_t numOfTables); +int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle); // Recover the meta handle from the file -SMetaHandle * tsdbOpenMetaHandle(int fd); +SMetaHandle * tsdbOpenMetaHandle(char *tsdbDir); -int32_t tsdbCreateMeterImpl(SMetaHandle *pHandle, STableCfg *pCfg); +int32_t tsdbCreateTableImpl(SMetaHandle *pHandle, STableCfg *pCfg); diff --git a/src/vnode/tsdb/src/tsdb.c b/src/vnode/tsdb/src/tsdb.c index 031dd135c6..dec46bea97 100644 --- a/src/vnode/tsdb/src/tsdb.c +++ b/src/vnode/tsdb/src/tsdb.c @@ -13,7 +13,13 @@ #include "tsdbCache.h" #include "tsdbMeta.h" -typedef struct STSDBRepo { +enum { + TSDB_REPO_STATE_ACTIVE, + TSDB_REPO_STATE_CLOSED, + TSDB_REPO_STATE_CONFIGURING +}; + +typedef struct _tsdb_repo { // TSDB configuration STSDBCfg *pCfg; @@ -31,10 +37,14 @@ typedef struct STSDBRepo { pthread_mutex_t tsdbMutex; + int8_t state; + } STSDBRepo; #define TSDB_GET_TABLE_BY_ID(pRepo, sid) (((STSDBRepo *)pRepo)->pTableList)[sid] #define TSDB_GET_TABLE_BY_NAME(pRepo, name) +#define TSDB_IS_REPO_ACTIVE(pRepo) ((pRepo)->state == TSDB_REPO_STATE_ACTIVE) +#define TSDB_IS_REPO_CLOSED(pRepo) ((pRepo)->state == TSDB_REPO_STATE_CLOSED) // Check the correctness of the TSDB configuration static int32_t tsdbCheckCfg(STSDBCfg *pCfg) { @@ -47,11 +57,18 @@ static int32_t tsdbCheckCfg(STSDBCfg *pCfg) { return 0; } -tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg, int32_t *error) { - int32_t err = 0; - err = tsdbCheckCfg(pCfg); - if (err != 0) { - // TODO: deal with the error here +static int32_t tsdbCreateFiles(STSDBRepo *pRepo) { + // TODO +} + +static int32_t tsdbClearFiles(STSDBRepo *pRepo) { + // TODO +} + +tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg) { + + // Check the configuration + if (tsdbCheckCfg(pCfg) < 0) { return NULL; } @@ -73,74 +90,108 @@ tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg, int32_t *error) { pRepo->pCacheHandle = tsdbCreateCache(5); if (pRepo->pCacheHandle == NULL) { // TODO: free the object and return error + tsdbFreeMetaHandle(pRepo->pCacheHandle); + free(pRepo); return NULL; } - // Create the Meta data file and data directory + // Set configuration + pRepo->pCfg = pCfg; - char *pTsdbMetaFName = tsdbGetFileName(pCfg->rootDir, "tsdb", TSDB_FILE_TYPE_META); - // int fd = open(pTsdbMetaFName, ) - // if (open) + // Create the Meta data file and data directory + if (tsdbCreateFiles(pRepo) < 0) { + // Failed to create and save files + tsdbFreeMetaHandle(pRepo->pCacheHandle); + free(pRepo); + return NULL; + } + + pRepo->state = TSDB_REPO_STATE_ACTIVE; return (tsdb_repo_t *)pRepo; } -int32_t tsdbDropRepo(tsdb_repo_t *pRepo, int32_t *error) { - STSDBRepo *pTRepo = (STSDBRepo *)pRepo; +int32_t tsdbDropRepo(tsdb_repo_t *repo) { + STSDBRepo *pRepo = (STSDBRepo *)repo; + + pRepo->state = TSDB_REPO_STATE_CLOSED; - // TODO: Close the metaHandle + // Free the metaHandle + tsdbFreeMetaHandle(pRepo->pMetaHandle); - // TODO: Close the cache + // Free the cache + tsdbFreeCache(pRepo->pCacheHandle); + + tsdbClearFiles(pRepo); return 0; } -tsdb_repo_t *tsdbOpenRepo(char *tsdbDir, int32_t *error) { +tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) { + + if (access(tsdbDir, F_OK|W_OK|R_OK) < 0) { + return NULL; + } + STSDBRepo *pRepo = (STSDBRepo *)malloc(sizeof(STSDBRepo)); if (pRepo == NULL) { return NULL; } // TODO: Initialize configuration from the file - - { - // TODO: Initialize the pMetaHandle - } + pRepo->pMetaHandle = tsdbOpenMetaHandle(); if (pRepo->pMetaHandle == NULL) { free(pRepo); return NULL; } - { - // TODO: initialize the pCacheHandle - } + pRepo->pCacheHandle = tsdbCreateCache(5); if (pRepo->pCacheHandle == NULL) { // TODO: deal with error return NULL; } + pRepo->state = TSDB_REPO_STATE_ACTIVE; + return (tsdb_repo_t *)pRepo; } -int32_t tsdbCloseRepo(tsdb_repo_t *pRepo, int32_t *error) { - STSDBRepo *pTRepo = (STSDBRepo *)pRepo; +static int32_t tsdbFlushCache(STSDBRepo *pRepo) { + // TODO +} + +int32_t tsdbCloseRepo(tsdb_repo_t *repo) { + STSDBRepo *pRepo = (STSDBRepo *)repo; + + tsdbFlushCache(pRepo); + + pRepo->state = TSDB_REPO_STATE_CLOSED; + + tsdbFreeMetaHandle(pRepo->pMetaHandle); + + tsdbFreeCache(pRepo->pMetaHandle); return 0; } -int32_t tsdbConfigRepo(STSDBCfg *pCfg, int32_t *error) { +int32_t tsdbConfigRepo(tsdb_repo_t *repo, STSDBCfg *pCfg) { + STSDBRepo *pRepo = (STSDBRepo *)repo; + + pRepo->pCfg = pCfg; // TODO + return 0; } -STSDBRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo, int32_t *error) { +STSDBRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo) { // TODO } -int32_t tsdbCreateTable(tsdb_repo_t *pRepo, STableCfg *pCfg, int32_t *error) { - // TODO +int32_t tsdbCreateTable(tsdb_repo_t *repo, STableCfg *pCfg) { + STSDBRepo *pRepo = (STSDBRepo *)repo; + return tsdbCreateTableImpl(pRepo->pMetaHandle, pCfg); } -int32_t tsdbAlterTable(tsdb_repo_t *pRepo, STableCfg *pCfg, int32_t *error) { +int32_t tsdbAlterTable(tsdb_repo_t *pRepo, STableCfg *pCfg) { // TODO } diff --git a/src/vnode/tsdb/src/tsdbCache.c b/src/vnode/tsdb/src/tsdbCache.c index feb70dc23c..e957778eb6 100644 --- a/src/vnode/tsdb/src/tsdbCache.c +++ b/src/vnode/tsdb/src/tsdbCache.c @@ -12,4 +12,7 @@ SCacheHandle *tsdbCreateCache(int32_t numOfBlocks) { return pCacheHandle; +} + +int32_t tsdbFreeCache(SCacheHandle *pHandle) { } \ No newline at end of file diff --git a/src/vnode/tsdb/src/tsdbMeta.c b/src/vnode/tsdb/src/tsdbMeta.c index 9fad7b1269..a302498a24 100644 --- a/src/vnode/tsdb/src/tsdbMeta.c +++ b/src/vnode/tsdb/src/tsdbMeta.c @@ -29,9 +29,14 @@ SMetaHandle *tsdbCreateMetaHandle(int32_t numOfTables) { return pMetahandle; } +int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle) { + // TODO + +} + static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; } -int32_t tsdbCreateMeterImpl(SMetaHandle *pHandle, STableCfg *pCfg) { +int32_t tsdbCreateTableImpl(SMetaHandle *pHandle, STableCfg *pCfg) { if (tsdbCheckTableCfg(pCfg) < 0) { return -1; } @@ -46,4 +51,15 @@ int32_t tsdbCreateMeterImpl(SMetaHandle *pHandle, STableCfg *pCfg) { // TODO: add name to it return 0; +} + +SMetaHandle * tsdbOpenMetaHandle(char *tsdbDir) { + // Open meta file for reading + + SMetaHandle *pHandle = (SMetaHandle *)malloc(sizeof(SMetaHandle)); + if (pHandle == NULL) { + return NULL; + } + + return pHandle; } \ No newline at end of file -- GitLab