提交 a4ee9d7c 编写于 作者: H hzcheng

refactor

上级 9f0c0151
...@@ -37,7 +37,6 @@ typedef struct { ...@@ -37,7 +37,6 @@ typedef struct {
// the TSDB repository configuration // the TSDB repository configuration
typedef struct { typedef struct {
char * rootDir; // TSDB repository root directory, TODO: need to adjust here
int32_t tsdbId; int32_t tsdbId;
int32_t maxTables; // maximum number of tables this repository can have int32_t maxTables; // maximum number of tables this repository can have
int32_t daysPerFile; // day per file sharding policy int32_t daysPerFile; // day per file sharding policy
...@@ -45,7 +44,6 @@ typedef struct { ...@@ -45,7 +44,6 @@ typedef struct {
int32_t maxRowsPerFileBlock; // maximum rows per file block int32_t maxRowsPerFileBlock; // maximum rows per file block
int32_t keep; // day of data to keep int32_t keep; // day of data to keep
int64_t maxCacheSize; // maximum cache size this TSDB can use int64_t maxCacheSize; // maximum cache size this TSDB can use
void * cachePool; // the cache pool the repository to use
} STsdbCfg; } STsdbCfg;
// the TSDB repository info // the TSDB repository info
...@@ -89,7 +87,7 @@ typedef struct { ...@@ -89,7 +87,7 @@ typedef struct {
* *
* @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); tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter);
/** /**
* Close and free all resources taken by the repository * Close and free all resources taken by the repository
......
...@@ -62,7 +62,7 @@ typedef struct { ...@@ -62,7 +62,7 @@ typedef struct {
// A map of tableName->tableId // A map of tableName->tableId
// TODO: May use hash table // TODO: May use hash table
void *pNameTableMap; void *pNameTableMap;
} SMetaHandle; } STsdbMeta;
// ---- Operation on STable // ---- Operation on STable
#define TSDB_TABLE_ID(pTable) ((pTable)->tableId) #define TSDB_TABLE_ID(pTable) ((pTable)->tableId)
...@@ -84,7 +84,7 @@ SSchema *tsdbGetTableSchema(STable *pTable); ...@@ -84,7 +84,7 @@ 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 * tsdbCreateMeta (int32_t numOfTables);
int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle); int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle);
// Recover the meta handle from the file // Recover the meta handle from the file
......
#include <fcntl.h>
#include <pthread.h> #include <pthread.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <sys/types.h>
#include <unistd.h>
// #include "taosdef.h" // #include "taosdef.h"
// #include "disk.h" // #include "disk.h"
#include "tsdbFile.h"
#include "tsdb.h" #include "tsdb.h"
#include "tsdbCache.h" #include "tsdbCache.h"
#include "tsdbFile.h"
#include "tsdbMeta.h" #include "tsdbMeta.h"
enum { enum { TSDB_REPO_STATE_ACTIVE, TSDB_REPO_STATE_CLOSED, TSDB_REPO_STATE_CONFIGURING };
TSDB_REPO_STATE_ACTIVE,
TSDB_REPO_STATE_CLOSED,
TSDB_REPO_STATE_CONFIGURING
};
typedef struct _tsdb_repo { typedef struct _tsdb_repo {
// TSDB configuration // TSDB configuration
STsdbCfg *pCfg; STsdbCfg config;
// The meter meta handle of this TSDB repository // The meter meta handle of this TSDB repository
SMetaHandle *tsdbMeta; SMetaHandle *tsdbMeta;
...@@ -37,31 +33,40 @@ typedef struct _tsdb_repo { ...@@ -37,31 +33,40 @@ typedef struct _tsdb_repo {
pthread_mutex_t tsdbMutex; pthread_mutex_t tsdbMutex;
// A limiter to monitor the resources used by tsdb
void *limiter;
int8_t state; int8_t state;
} STsdbRepo; } STsdbRepo;
static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg);
static int32_t tsdbCreateRepoFiles(STsdbRepo *pRepo);
#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_ACTIVE(pRepo) ((pRepo)->state == TSDB_REPO_STATE_ACTIVE)
#define TSDB_IS_REPO_CLOSED(pRepo) ((pRepo)->state == TSDB_REPO_STATE_CLOSED) #define TSDB_IS_REPO_CLOSED(pRepo) ((pRepo)->state == TSDB_REPO_STATE_CLOSED)
tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) {
if (rootDir == NULL) return NULL;
tsdb_repo_t *tsdbCreateRepo(STsdbCfg *pCfg) { if (access(rootDir, F_OK|R_OK|W_OK) == -1) return NULL;
// Check the configuration if (tsdbCheckAndSetDefaultCfg(pCfg) < 0) {
if (tsdbCheckCfg(pCfg) < 0) {
return NULL; return NULL;
} }
STsdbRepo *pRepo = (STsdbRepo *)malloc(sizeof(STsdbRepo)); STsdbRepo *pRepo = (STsdbRepo *)malloc(sizeof(STsdbRepo));
if (pRepo == NULL) { if (pRepo == NULL) {
// TODO: deal with error
return NULL; return NULL;
} }
// TODO: Initailize pMetahandle pRepo->config = *pCfg;
pRepo->tsdbMeta = tsdbCreateMetaHandle(pCfg->maxTables); pRepo->limiter = limiter;
pRepo->tsdbMeta = tsdbCreateMeta(pCfg->maxTables);
if (pRepo->tsdbMeta == NULL) { if (pRepo->tsdbMeta == NULL) {
// TODO: deal with error // TODO: deal with error
free(pRepo); free(pRepo);
...@@ -77,11 +82,8 @@ tsdb_repo_t *tsdbCreateRepo(STsdbCfg *pCfg) { ...@@ -77,11 +82,8 @@ tsdb_repo_t *tsdbCreateRepo(STsdbCfg *pCfg) {
return NULL; return NULL;
} }
// Set configuration
pRepo->pCfg = pCfg;
// Create the Meta data file and data directory // Create the Meta data file and data directory
if (tsdbCreateFiles(pRepo) < 0) { if (tsdbCreateRepoFiles(pRepo) < 0) {
// Failed to create and save files // Failed to create and save files
tsdbFreeMetaHandle(pRepo->tsdbCache); tsdbFreeMetaHandle(pRepo->tsdbCache);
free(pRepo); free(pRepo);
...@@ -110,8 +112,7 @@ int32_t tsdbDropRepo(tsdb_repo_t *repo) { ...@@ -110,8 +112,7 @@ int32_t tsdbDropRepo(tsdb_repo_t *repo) {
} }
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;
} }
...@@ -159,7 +160,7 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo) { ...@@ -159,7 +160,7 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo) {
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;
pRepo->pCfg = pCfg; pRepo->config = pCfg;
// TODO // TODO
return 0; return 0;
} }
...@@ -185,18 +186,13 @@ int32_t tsdbInsertData(tsdb_repo_t *pRepo, STableId tid, char *pData, int32_t *e ...@@ -185,18 +186,13 @@ int32_t tsdbInsertData(tsdb_repo_t *pRepo, STableId tid, char *pData, int32_t *e
// TODO // TODO
} }
// Check the correctness of the TSDB configuration // Check the configuration and set default options
static int32_t tsdbCheckCfg(STsdbCfg *pCfg) { static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) {
if (pCfg->rootDir == NULL) return -1;
if (access(pCfg->rootDir, F_OK|R_OK|W_OK) == -1) {
return -1;
}
// TODO // TODO
return 0; return 0;
} }
static int32_t tsdbCreateFiles(STsdbRepo *pRepo) { static int32_t tsdbCreateRepoFiles(STsdbRepo *pRepo) {
// TODO // TODO
} }
......
...@@ -4,39 +4,39 @@ ...@@ -4,39 +4,39 @@
#include "tsdb.h" #include "tsdb.h"
#include "tsdbMeta.h" #include "tsdbMeta.h"
SMetaHandle *tsdbCreateMetaHandle(int32_t numOfTables) { STsdbMeta *tsdbCreateMeta(int32_t maxNumOfTables) {
SMetaHandle *pMetahandle = (SMetaHandle *)malloc(sizeof(SMetaHandle)); STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta));
if (pMetahandle == NULL) { if (pMeta == NULL) {
return NULL; return NULL;
} }
pMetahandle->numOfTables = 0; pMeta->numOfTables = 0;
pMetahandle->numOfSuperTables = 0; pMeta->numOfSuperTables = 0;
pMetahandle->pTables = calloc(sizeof(STable *), numOfTables); pMeta->pTables = calloc(sizeof(STable *), numOfTables);
if (pMetahandle->pTables == NULL) { if (pMeta->pTables == NULL) {
free(pMetahandle); free(pMeta);
return NULL; return NULL;
} }
// TODO : initialize the map // TODO : initialize the map
// pMetahandle->pNameTableMap = ; // pMetahandle->pNameTableMap = ;
if (pMetahandle->pNameTableMap == NULL) { if (pMeta->pNameTableMap == NULL) {
free(pMetahandle->pTables); free(pMeta->pTables);
free(pMetahandle); free(pMeta);
return NULL; return NULL;
} }
return pMetahandle; return pMeta;
} }
int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle) { int32_t tsdbFreeMetaHandle(STsdbMeta *pMetaHandle) {
// TODO // TODO
} }
static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; } static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; }
int32_t tsdbCreateTableImpl(SMetaHandle *pHandle, STableCfg *pCfg) { int32_t tsdbCreateTableImpl(STsdbMeta *pHandle, STableCfg *pCfg) {
if (tsdbCheckTableCfg(pCfg) < 0) { if (tsdbCheckTableCfg(pCfg) < 0) {
return -1; return -1;
} }
...@@ -53,10 +53,10 @@ int32_t tsdbCreateTableImpl(SMetaHandle *pHandle, STableCfg *pCfg) { ...@@ -53,10 +53,10 @@ int32_t tsdbCreateTableImpl(SMetaHandle *pHandle, STableCfg *pCfg) {
return 0; return 0;
} }
SMetaHandle * tsdbOpenMetaHandle(char *tsdbDir) { STsdbMeta * tsdbOpenMetaHandle(char *tsdbDir) {
// Open meta file for reading // Open meta file for reading
SMetaHandle *pHandle = (SMetaHandle *)malloc(sizeof(SMetaHandle)); STsdbMeta *pHandle = (STsdbMeta *)malloc(sizeof(STsdbMeta));
if (pHandle == NULL) { if (pHandle == NULL) {
return NULL; return NULL;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册