提交 d022f5e8 编写于 作者: H hzcheng

refactor

上级 21009436
...@@ -30,9 +30,9 @@ extern "C" { ...@@ -30,9 +30,9 @@ extern "C" {
#define TSDB_VERSION_MAJOR 1 #define TSDB_VERSION_MAJOR 1
#define TSDB_VERSION_MINOR 0 #define TSDB_VERSION_MINOR 0
typedef void tsdb_repo_t; // use void to hide implementation details from outside
// --------- TSDB REPOSITORY CONFIGURATION DEFINITION // --------- TSDB REPOSITORY CONFIGURATION DEFINITION
enum { TSDB_PRECISION_MILLI, TSDB_PRECISION_MICRO, TSDB_PRECISION_NANO };
typedef struct { typedef struct {
int8_t precision; int8_t precision;
int32_t tsdbId; int32_t tsdbId;
...@@ -49,7 +49,15 @@ STsdbCfg *tsdbCreateDefaultCfg(); ...@@ -49,7 +49,15 @@ STsdbCfg *tsdbCreateDefaultCfg();
void tsdbFreeCfg(STsdbCfg *pCfg); void tsdbFreeCfg(STsdbCfg *pCfg);
// --------- TSDB REPOSITORY DEFINITION // --------- TSDB REPOSITORY DEFINITION
typedef void tsdb_repo_t; // use void to hide implementation details from outside
tsdb_repo_t * tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter);
int32_t tsdbDropRepo(tsdb_repo_t *repo);
tsdb_repo_t * tsdbOpenRepo(char *tsdbDir);
int32_t tsdbCloseRepo(tsdb_repo_t *repo);
int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg);
// --------- TSDB TABLE DEFINITION
typedef struct { typedef struct {
int64_t uid; // the unique table ID int64_t uid; // the unique table ID
int32_t tid; // the table ID in the repository. int32_t tid; // the table ID in the repository.
...@@ -71,7 +79,6 @@ typedef struct { ...@@ -71,7 +79,6 @@ typedef struct {
char data[]; char data[];
} SSubmitBlock; } SSubmitBlock;
enum { TSDB_PRECISION_MILLI, TSDB_PRECISION_MICRO, TSDB_PRECISION_NANO };
// the TSDB repository info // the TSDB repository info
typedef struct STsdbRepoInfo { typedef struct STsdbRepoInfo {
...@@ -82,6 +89,8 @@ typedef struct STsdbRepoInfo { ...@@ -82,6 +89,8 @@ typedef struct STsdbRepoInfo {
// TODO: Other informations to add // TODO: Other informations to add
} STsdbRepoInfo; } STsdbRepoInfo;
STsdbRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo);
// the meter configuration // the meter configuration
typedef struct { typedef struct {
STableId tableId; STableId tableId;
...@@ -106,60 +115,6 @@ typedef struct { ...@@ -106,60 +115,6 @@ typedef struct {
int64_t tableTotalDiskSize; // In bytes int64_t tableTotalDiskSize; // In bytes
} STableInfo; } STableInfo;
/**
* Create a new TSDB repository
* @param rootDir the TSDB repository root directory
* @param pCfg the TSDB repository configuration, upper layer to free the pointer
*
* @return a TSDB repository handle on success, NULL for failure and the error number is set
*/
tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter);
/**
* Close and free all resources taken by the repository
* @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.
*
* @return 0 for success, -1 for failure and the error number is set
*/
int32_t tsdbDropRepo(tsdb_repo_t *repo);
/**
* Open an existing TSDB storage repository
* @param tsdbDir the existing TSDB root directory
*
* @return a TSDB repository handle on success, NULL for failure and the error number is set
*/
tsdb_repo_t *tsdbOpenRepo(char *tsdbDir);
/**
* Close a TSDB repository. Only free memory resources, and keep the files.
* @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.
*
* @return 0 for success, -1 for failure and the error number is set
*/
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
*
* @return 0 for success, -1 for failure and the error number is set
*/
int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg);
/**
* Get the TSDB repository information, including some statistics
* @param pRepo the TSDB repository handle
* @param error the error number to set when failure occurs
*
* @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);
// -- For table manipulation // -- For table manipulation
/** /**
......
...@@ -53,7 +53,7 @@ typedef struct STSDBCache { ...@@ -53,7 +53,7 @@ typedef struct STSDBCache {
#define TSDB_NEXT_CACHE_BLOCK(pBlock) ((pBlock)->next) #define TSDB_NEXT_CACHE_BLOCK(pBlock) ((pBlock)->next)
#define TSDB_PREV_CACHE_BLOCK(pBlock) ((pBlock)->prev) #define TSDB_PREV_CACHE_BLOCK(pBlock) ((pBlock)->prev)
STsdbCache *tsdbCreateCache(int32_t numOfBlocks); STsdbCache *tsdbInitCache(int64_t maxSize);
int32_t tsdbFreeCache(STsdbCache *pCache); int32_t tsdbFreeCache(STsdbCache *pCache);
void * tsdbAllocFromCache(STsdbCache *pCache, int64_t bytes); void * tsdbAllocFromCache(STsdbCache *pCache, int64_t bytes);
......
...@@ -106,7 +106,7 @@ STSchema *tsdbGetTableSchema(STable *pTable); ...@@ -106,7 +106,7 @@ STSchema *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
STsdbMeta *tsdbCreateMeta(int32_t maxTables); STsdbMeta *tsdbInitMeta(int32_t maxTables);
int32_t tsdbFreeMeta(STsdbMeta *pMeta); int32_t tsdbFreeMeta(STsdbMeta *pMeta);
// Recover the meta handle from the file // Recover the meta handle from the file
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "tsdbCache.h" #include "tsdbCache.h"
STsdbCache *tsdbCreateCache(int32_t numOfBlocks) { STsdbCache *tsdbInitCache(int64_t maxSize) {
STsdbCache *pCacheHandle = (STsdbCache *)malloc(sizeof(STsdbCache)); STsdbCache *pCacheHandle = (STsdbCache *)malloc(sizeof(STsdbCache));
if (pCacheHandle == NULL) { if (pCacheHandle == NULL) {
// TODO : deal with the error // TODO : deal with the error
......
...@@ -42,6 +42,9 @@ ...@@ -42,6 +42,9 @@
#define TSDB_MIN_CACHE_SIZE (4 * 1024 * 1024) // 4M #define TSDB_MIN_CACHE_SIZE (4 * 1024 * 1024) // 4M
#define TSDB_MAX_CACHE_SIZE (1024 * 1024 * 1024) // 1G #define TSDB_MAX_CACHE_SIZE (1024 * 1024 * 1024) // 1G
#define TSDB_CFG_FILE_NAME "CONFIG"
#define TSDB_DATA_DIR_NAME "data"
enum { TSDB_REPO_STATE_ACTIVE, TSDB_REPO_STATE_CLOSED, TSDB_REPO_STATE_CONFIGURING }; enum { TSDB_REPO_STATE_ACTIVE, TSDB_REPO_STATE_CLOSED, TSDB_REPO_STATE_CONFIGURING };
typedef struct _tsdb_repo { typedef struct _tsdb_repo {
...@@ -116,7 +119,15 @@ void tsdbFreeCfg(STsdbCfg *pCfg) { ...@@ -116,7 +119,15 @@ void tsdbFreeCfg(STsdbCfg *pCfg) {
if (pCfg != NULL) free(pCfg); if (pCfg != NULL) free(pCfg);
} }
tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) { /**
* Create a new TSDB repository
* @param rootDir the TSDB repository root directory
* @param pCfg the TSDB repository configuration, upper layer need to free the pointer
* @param limiter the limitation tracker will implement in the future, make it void now
*
* @return a TSDB repository handle on success, NULL for failure
*/
tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO */) {
if (rootDir == NULL) return NULL; if (rootDir == NULL) return NULL;
if (access(rootDir, F_OK | R_OK | W_OK) == -1) return NULL; if (access(rootDir, F_OK | R_OK | W_OK) == -1) return NULL;
...@@ -134,22 +145,26 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) { ...@@ -134,22 +145,26 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) {
pRepo->config = *pCfg; pRepo->config = *pCfg;
pRepo->limiter = limiter; pRepo->limiter = limiter;
pRepo->tsdbMeta = tsdbCreateMeta(pCfg->maxTables); // Initialize meta
if (pRepo->tsdbMeta == NULL) { STsdbMeta *pMeta = tsdbInitMeta(pCfg->maxTables);
if (pMeta == NULL) {
free(pRepo->rootDir); free(pRepo->rootDir);
free(pRepo); free(pRepo);
return NULL; return NULL;
} }
pRepo->tsdbMeta = pMeta;
pRepo->tsdbCache = tsdbCreateCache(5); // Initialize cache
if (pRepo->tsdbCache == NULL) { STsdbCache *pCache = tsdbInitCache(pCfg->maxCacheSize);
if (pCache == NULL) {
free(pRepo->rootDir); free(pRepo->rootDir);
tsdbFreeMeta(pRepo->tsdbMeta); tsdbFreeMeta(pRepo->tsdbMeta);
free(pRepo); free(pRepo);
return NULL; return NULL;
} }
pRepo->tsdbCache = pCache;
// Create the Meta data file and data directory // Create the environment files and directories
if (tsdbSetRepoEnv(pRepo) < 0) { if (tsdbSetRepoEnv(pRepo) < 0) {
free(pRepo->rootDir); free(pRepo->rootDir);
tsdbFreeMeta(pRepo->tsdbMeta); tsdbFreeMeta(pRepo->tsdbMeta);
...@@ -163,6 +178,13 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) { ...@@ -163,6 +178,13 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) {
return (tsdb_repo_t *)pRepo; return (tsdb_repo_t *)pRepo;
} }
/**
* Close and free all resources taken by the repository
* @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.
*
* @return 0 for success, -1 for failure and the error number is set
*/
int32_t tsdbDropRepo(tsdb_repo_t *repo) { int32_t tsdbDropRepo(tsdb_repo_t *repo) {
STsdbRepo *pRepo = (STsdbRepo *)repo; STsdbRepo *pRepo = (STsdbRepo *)repo;
...@@ -183,6 +205,12 @@ int32_t tsdbDropRepo(tsdb_repo_t *repo) { ...@@ -183,6 +205,12 @@ int32_t tsdbDropRepo(tsdb_repo_t *repo) {
return 0; return 0;
} }
/**
* Open an existing TSDB storage repository
* @param tsdbDir the existing TSDB root directory
*
* @return a TSDB repository handle on success, NULL for failure and the error number is set
*/
tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) { tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) {
if (access(tsdbDir, F_OK | W_OK | R_OK) < 0) { if (access(tsdbDir, F_OK | W_OK | R_OK) < 0) {
return NULL; return NULL;
...@@ -205,7 +233,7 @@ tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) { ...@@ -205,7 +233,7 @@ tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) {
return NULL; return NULL;
} }
pRepo->tsdbCache = tsdbCreateCache(5); pRepo->tsdbCache = tsdbInitCache(5);
if (pRepo->tsdbCache == NULL) { if (pRepo->tsdbCache == NULL) {
// TODO: deal with error // TODO: deal with error
return NULL; return NULL;
...@@ -222,6 +250,13 @@ static int32_t tsdbFlushCache(STsdbRepo *pRepo) { ...@@ -222,6 +250,13 @@ static int32_t tsdbFlushCache(STsdbRepo *pRepo) {
return 0; return 0;
} }
/**
* Close a TSDB repository. Only free memory resources, and keep the files.
* @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.
*
* @return 0 for success, -1 for failure and the error number is set
*/
int32_t tsdbCloseRepo(tsdb_repo_t *repo) { int32_t tsdbCloseRepo(tsdb_repo_t *repo) {
STsdbRepo *pRepo = (STsdbRepo *)repo; STsdbRepo *pRepo = (STsdbRepo *)repo;
if (pRepo == NULL) return 0; if (pRepo == NULL) return 0;
...@@ -237,6 +272,12 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo) { ...@@ -237,6 +272,12 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo) {
return 0; return 0;
} }
/**
* Change the configuration of a repository
* @param pCfg the repository configuration, the upper layer should free the pointer
*
* @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) {
STsdbRepo *pRepo = (STsdbRepo *)repo; STsdbRepo *pRepo = (STsdbRepo *)repo;
...@@ -245,6 +286,14 @@ int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg) { ...@@ -245,6 +286,14 @@ int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg) {
return 0; return 0;
} }
/**
* Get the TSDB repository information, including some statistics
* @param pRepo the TSDB repository handle
* @param error the error number to set when failure occurs
*
* @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) { STsdbRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo) {
// TODO // TODO
return NULL; return NULL;
...@@ -299,7 +348,7 @@ static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) { ...@@ -299,7 +348,7 @@ static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) {
// Check tsdbId // Check tsdbId
if (pCfg->tsdbId < 0) return -1; if (pCfg->tsdbId < 0) return -1;
// Check MaxTables // Check maxTables
if (pCfg->maxTables == -1) { if (pCfg->maxTables == -1) {
pCfg->maxTables = TSDB_DEFAULT_TABLES; pCfg->maxTables = TSDB_DEFAULT_TABLES;
} else { } else {
...@@ -347,10 +396,18 @@ static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) { ...@@ -347,10 +396,18 @@ static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) {
return 0; return 0;
} }
static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo) { static int32_t tsdbGetCfgFname(STsdbRepo *pRepo, char *fname) {
char *metaFname = tsdbGetFileName(pRepo->rootDir, "tsdb", TSDB_FILE_TYPE_META); if (pRepo == NULL) return -1;
sprintf(fname, "%s/%s", pRepo->rootDir, TSDB_CFG_FILE_NAME);
return 0;
}
static int32_t tsdbSaveConfig(STsdbRepo *pRepo) {
char fname[128] = "\0"; // TODO: get rid of the literal 128
if (tsdbGetCfgFname(pRepo, fname) < 0) return -1;
int fd = open(metaFname, O_WRONLY | O_CREAT); int fd = open(fname, O_WRONLY | O_CREAT, 0755);
if (fd < 0) { if (fd < 0) {
return -1; return -1;
} }
...@@ -359,19 +416,45 @@ static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo) { ...@@ -359,19 +416,45 @@ static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo) {
return -1; return -1;
} }
// Create the data file close(fd);
char *dirName = calloc(1, strlen(pRepo->rootDir) + strlen("tsdb") + 2); return 0;
if (dirName == NULL) { }
static int32_t tsdbRestoreCfg(STsdbRepo *pRepo, STsdbCfg *pCfg) {
char fname[128] = "\0";
if (tsdbGetCfgFname(pRepo, fname) < 0) return -1;
int fd = open(fname, O_RDONLY);
if (fd < 0) {
return -1; return -1;
} }
sprintf(dirName, "%s/%s", pRepo->rootDir, "tsdb"); if (read(fd, (void *)pCfg, sizeof(STsdbCfg)) < sizeof(STsdbCfg)) {
if (mkdir(dirName, 0755) < 0) { close(fd);
free(dirName);
return -1; return -1;
} }
free(dirName); close(fd);
return 0;
}
static int32_t tsdbGetDataDirName(STsdbRepo *pRepo, char *fname) {
if (pRepo == NULL || pRepo->rootDir == NULL) return -1;
sprintf(fname, "%s/%s", pRepo->rootDir, TSDB_DATA_DIR_NAME);
return 0;
}
static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo) {
if (tsdbSaveConfig(pRepo) < 0) return -1;
char dirName[128] = "\0";
if (tsdbGetDataDirName(pRepo, dirName) < 0) return -1;
if (mkdir(dirName, 0755) < 0) {
return -1;
}
return 0; return 0;
} }
......
...@@ -17,7 +17,7 @@ static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable); ...@@ -17,7 +17,7 @@ static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable);
static int tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable); static int tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable);
static int tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable); static int tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable);
STsdbMeta *tsdbCreateMeta(int32_t maxTables) { STsdbMeta *tsdbInitMeta(int32_t maxTables) {
STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta)); STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta));
if (pMeta == NULL) { if (pMeta == NULL) {
return NULL; return NULL;
......
...@@ -72,7 +72,7 @@ TEST(TsdbTest, createRepo) { ...@@ -72,7 +72,7 @@ TEST(TsdbTest, createRepo) {
} }
TEST(TsdbTest, DISABLED_createTable) { TEST(TsdbTest, DISABLED_createTable) {
STsdbMeta *pMeta = tsdbCreateMeta(100); STsdbMeta *pMeta = tsdbInitMeta(100);
ASSERT_NE(pMeta, nullptr); ASSERT_NE(pMeta, nullptr);
STableCfg config; STableCfg config;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册