提交 6d2dac39 编写于 作者: 陶建辉(Jeff)'s avatar 陶建辉(Jeff)

Merge branch '2.0' of https://github.com/taosdata/TDengine into 2.0

...@@ -6,10 +6,7 @@ ...@@ -6,10 +6,7 @@
TEST(TsdbTest, createTsdbRepo) { TEST(TsdbTest, createTsdbRepo) {
STSDBCfg *pCfg = (STSDBCfg *)malloc(sizeof(STSDBCfg)); STSDBCfg *pCfg = (STSDBCfg *)malloc(sizeof(STSDBCfg));
pCfg->rootDir = "/var/lib/taos/"; free(pCfg);
int32_t err_num = 0; ASSERT_EQ(1, 2/2);
tsdb_repo_t *pRepo = tsdbCreateRepo(pCfg, &err_num);
ASSERT_EQ(pRepo, NULL);
} }
\ No newline at end of file
...@@ -21,6 +21,7 @@ typedef int16_t tsdb_id_t; // TSDB repository ID ...@@ -21,6 +21,7 @@ typedef int16_t tsdb_id_t; // TSDB repository ID
// Submit message // Submit message
typedef struct { typedef struct {
int32_t numOfTables; int32_t numOfTables;
int32_t compressed;
char data[]; char data[];
} SSubmitMsg; } SSubmitMsg;
...@@ -111,49 +112,44 @@ typedef struct { ...@@ -111,49 +112,44 @@ typedef struct {
/** /**
* Create a new TSDB repository * Create a new TSDB repository
* @param pCfg the TSDB repository configuration, upper layer to free the pointer * @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 * @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 * 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. * 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 * @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 * Open an existing TSDB storage repository
* @param tsdbDir the existing TSDB root directory * @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 * @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. * 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. * 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 * @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 * Change the configuration of a repository
* @param pCfg the repository configuration, the upper layer should free the pointer * @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 * @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 * Get the TSDB repository information, including some statistics
...@@ -163,20 +159,19 @@ int32_t tsdbConfigRepo(STSDBCfg *pCfg, int32_t *error); ...@@ -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 * @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 * 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 // -- For table manipulation
/** /**
* Create/Alter a table in a TSDB repository handle * 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 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 * @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 tsdbCreateTable(tsdb_repo_t *repo, STableCfg *pCfg);
int32_t tsdbAlterTable(tsdb_repo_t *pRepo, STableCfg *pCfg, int32_t *error); int32_t tsdbAlterTable(tsdb_repo_t *repo, STableCfg *pCfg);
/** /**
* Drop a table in a repository and free all the resources it takes * Drop a table in a repository and free all the resources it takes
......
...@@ -36,5 +36,6 @@ typedef struct STSDBCache { ...@@ -36,5 +36,6 @@ typedef struct STSDBCache {
#define TSDB_PREV_CACHE_BLOCK(pBlock) ((pBlock)->prev) #define TSDB_PREV_CACHE_BLOCK(pBlock) ((pBlock)->prev)
SCacheHandle *tsdbCreateCache(int32_t numOfBlocks); SCacheHandle *tsdbCreateCache(int32_t numOfBlocks);
int32_t tsdbFreeCache(SCacheHandle *pHandle);
#endif // _TD_TSDBCACHE_H_ #endif // _TD_TSDBCACHE_H_
...@@ -29,12 +29,12 @@ typedef struct { ...@@ -29,12 +29,12 @@ typedef struct {
SFileInfo fInfo; SFileInfo fInfo;
} SFILE; } SFILE;
typedef struct { // typedef struct {
int64_t offset; // int64_t offset;
int64_t skey; // int64_t skey;
int64_t ekey; // int64_t ekey;
int16_t numOfBlocks; // int16_t numOfBlocks;
} SDataBlock; // } SDataBlock;
char *tsdbGetFileName(char *dirName, char *fname, TSDB_FILE_TYPE type); char *tsdbGetFileName(char *dirName, char *fname, TSDB_FILE_TYPE type);
......
...@@ -84,9 +84,10 @@ SSchema *tsdbGetTableSchema(STable *pTable); ...@@ -84,9 +84,10 @@ SSchema *tsdbGetTableSchema(STable *pTable);
#define TSDB_GET_TABLE_OF_NAME(pHandle, name) /* TODO */ #define TSDB_GET_TABLE_OF_NAME(pHandle, name) /* TODO */
// Create a new meta handle with configuration // 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 // 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);
...@@ -13,7 +13,13 @@ ...@@ -13,7 +13,13 @@
#include "tsdbCache.h" #include "tsdbCache.h"
#include "tsdbMeta.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 // TSDB configuration
STSDBCfg *pCfg; STSDBCfg *pCfg;
...@@ -31,10 +37,14 @@ typedef struct STSDBRepo { ...@@ -31,10 +37,14 @@ typedef struct STSDBRepo {
pthread_mutex_t tsdbMutex; pthread_mutex_t tsdbMutex;
int8_t state;
} STSDBRepo; } STSDBRepo;
#define TSDB_GET_TABLE_BY_ID(pRepo, sid) (((STSDBRepo *)pRepo)->pTableList)[sid] #define TSDB_GET_TABLE_BY_ID(pRepo, sid) (((STSDBRepo *)pRepo)->pTableList)[sid]
#define TSDB_GET_TABLE_BY_NAME(pRepo, name) #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 // Check the correctness of the TSDB configuration
static int32_t tsdbCheckCfg(STSDBCfg *pCfg) { static int32_t tsdbCheckCfg(STSDBCfg *pCfg) {
...@@ -47,11 +57,18 @@ static int32_t tsdbCheckCfg(STSDBCfg *pCfg) { ...@@ -47,11 +57,18 @@ static int32_t tsdbCheckCfg(STSDBCfg *pCfg) {
return 0; return 0;
} }
tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg, int32_t *error) { static int32_t tsdbCreateFiles(STSDBRepo *pRepo) {
int32_t err = 0; // TODO
err = tsdbCheckCfg(pCfg); }
if (err != 0) {
// TODO: deal with the error here static int32_t tsdbClearFiles(STSDBRepo *pRepo) {
// TODO
}
tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg) {
// Check the configuration
if (tsdbCheckCfg(pCfg) < 0) {
return NULL; return NULL;
} }
...@@ -73,74 +90,108 @@ tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg, int32_t *error) { ...@@ -73,74 +90,108 @@ tsdb_repo_t *tsdbCreateRepo(STSDBCfg *pCfg, int32_t *error) {
pRepo->pCacheHandle = tsdbCreateCache(5); pRepo->pCacheHandle = tsdbCreateCache(5);
if (pRepo->pCacheHandle == NULL) { if (pRepo->pCacheHandle == NULL) {
// TODO: free the object and return error // TODO: free the object and return error
tsdbFreeMetaHandle(pRepo->pCacheHandle);
free(pRepo);
return NULL; 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); // Create the Meta data file and data directory
// int fd = open(pTsdbMetaFName, ) if (tsdbCreateFiles(pRepo) < 0) {
// if (open) // Failed to create and save files
tsdbFreeMetaHandle(pRepo->pCacheHandle);
free(pRepo);
return NULL;
}
pRepo->state = TSDB_REPO_STATE_ACTIVE;
return (tsdb_repo_t *)pRepo; return (tsdb_repo_t *)pRepo;
} }
int32_t tsdbDropRepo(tsdb_repo_t *pRepo, int32_t *error) { int32_t tsdbDropRepo(tsdb_repo_t *repo) {
STSDBRepo *pTRepo = (STSDBRepo *)pRepo; 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; 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)); STSDBRepo *pRepo = (STSDBRepo *)malloc(sizeof(STSDBRepo));
if (pRepo == NULL) { if (pRepo == NULL) {
return NULL; return NULL;
} }
// TODO: Initialize configuration from the file // TODO: Initialize configuration from the file
pRepo->pMetaHandle = tsdbOpenMetaHandle();
{
// TODO: Initialize the pMetaHandle
}
if (pRepo->pMetaHandle == NULL) { if (pRepo->pMetaHandle == NULL) {
free(pRepo); free(pRepo);
return NULL; return NULL;
} }
{ pRepo->pCacheHandle = tsdbCreateCache(5);
// TODO: initialize the pCacheHandle
}
if (pRepo->pCacheHandle == NULL) { if (pRepo->pCacheHandle == NULL) {
// TODO: deal with error // TODO: deal with error
return NULL; return NULL;
} }
pRepo->state = TSDB_REPO_STATE_ACTIVE;
return (tsdb_repo_t *)pRepo; return (tsdb_repo_t *)pRepo;
} }
int32_t tsdbCloseRepo(tsdb_repo_t *pRepo, int32_t *error) { static int32_t tsdbFlushCache(STSDBRepo *pRepo) {
STSDBRepo *pTRepo = (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; 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 // TODO
return 0;
} }
STSDBRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo, int32_t *error) { STSDBRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo) {
// TODO // TODO
} }
int32_t tsdbCreateTable(tsdb_repo_t *pRepo, STableCfg *pCfg, int32_t *error) { int32_t tsdbCreateTable(tsdb_repo_t *repo, STableCfg *pCfg) {
// TODO 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 // TODO
} }
......
...@@ -12,4 +12,7 @@ SCacheHandle *tsdbCreateCache(int32_t numOfBlocks) { ...@@ -12,4 +12,7 @@ SCacheHandle *tsdbCreateCache(int32_t numOfBlocks) {
return pCacheHandle; return pCacheHandle;
}
int32_t tsdbFreeCache(SCacheHandle *pHandle) {
} }
\ No newline at end of file
...@@ -29,9 +29,14 @@ SMetaHandle *tsdbCreateMetaHandle(int32_t numOfTables) { ...@@ -29,9 +29,14 @@ SMetaHandle *tsdbCreateMetaHandle(int32_t numOfTables) {
return pMetahandle; return pMetahandle;
} }
int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle) {
// TODO
}
static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; } 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) { if (tsdbCheckTableCfg(pCfg) < 0) {
return -1; return -1;
} }
...@@ -46,4 +51,15 @@ int32_t tsdbCreateMeterImpl(SMetaHandle *pHandle, STableCfg *pCfg) { ...@@ -46,4 +51,15 @@ int32_t tsdbCreateMeterImpl(SMetaHandle *pHandle, STableCfg *pCfg) {
// TODO: add name to it // TODO: add name to it
return 0; 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册