提交 b2e18917 编写于 作者: H hzcheng

more

上级 a4ee9d7c
......@@ -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
......
......@@ -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);
#include <stdio.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdint.h>
......@@ -5,6 +6,8 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
// #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
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册