more
This commit is contained in:
parent
a4ee9d7c9c
commit
b2e189178d
|
@ -121,7 +121,7 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo);
|
||||||
*
|
*
|
||||||
* @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(tsdb_repo_t repo, STsdbCfg *pCfg);
|
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
|
||||||
|
|
|
@ -84,10 +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 * tsdbCreateMeta (int32_t numOfTables);
|
STsdbMeta * tsdbCreateMeta (int32_t maxTables);
|
||||||
int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle);
|
int32_t tsdbFreeMeta(STsdbMeta *pMeta);
|
||||||
|
|
||||||
// Recover the meta handle from the file
|
// 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);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <stdio.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
@ -5,6 +6,8 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
// #include "taosdef.h"
|
// #include "taosdef.h"
|
||||||
// #include "disk.h"
|
// #include "disk.h"
|
||||||
|
@ -16,11 +19,12 @@
|
||||||
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 {
|
||||||
|
char *rootDir;
|
||||||
// TSDB configuration
|
// TSDB configuration
|
||||||
STsdbCfg config;
|
STsdbCfg config;
|
||||||
|
|
||||||
// The meter meta handle of this TSDB repository
|
// The meter meta handle of this TSDB repository
|
||||||
SMetaHandle *tsdbMeta;
|
STsdbMeta *tsdbMeta;
|
||||||
|
|
||||||
// The cache Handle
|
// The cache Handle
|
||||||
SCacheHandle *tsdbCache;
|
SCacheHandle *tsdbCache;
|
||||||
|
@ -41,7 +45,8 @@ typedef struct _tsdb_repo {
|
||||||
} STsdbRepo;
|
} STsdbRepo;
|
||||||
|
|
||||||
static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg);
|
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_ID(pRepo, sid) (((STSDBRepo *)pRepo)->pTableList)[sid]
|
||||||
#define TSDB_GET_TABLE_BY_NAME(pRepo, name)
|
#define TSDB_GET_TABLE_BY_NAME(pRepo, name)
|
||||||
|
@ -63,29 +68,30 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pRepo->rootDir = strdup(rootDir);
|
||||||
pRepo->config = *pCfg;
|
pRepo->config = *pCfg;
|
||||||
pRepo->limiter = limiter;
|
pRepo->limiter = limiter;
|
||||||
|
|
||||||
pRepo->tsdbMeta = tsdbCreateMeta(pCfg->maxTables);
|
pRepo->tsdbMeta = tsdbCreateMeta(pCfg->maxTables);
|
||||||
if (pRepo->tsdbMeta == NULL) {
|
if (pRepo->tsdbMeta == NULL) {
|
||||||
// TODO: deal with error
|
free(pRepo->rootDir);
|
||||||
free(pRepo);
|
free(pRepo);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Initialize cache handle
|
|
||||||
pRepo->tsdbCache = tsdbCreateCache(5);
|
pRepo->tsdbCache = tsdbCreateCache(5);
|
||||||
if (pRepo->tsdbCache == NULL) {
|
if (pRepo->tsdbCache == NULL) {
|
||||||
// TODO: free the object and return error
|
free(pRepo->rootDir);
|
||||||
tsdbFreeMetaHandle(pRepo->tsdbCache);
|
tsdbFreeMeta(pRepo->tsdbMeta);
|
||||||
free(pRepo);
|
free(pRepo);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the Meta data file and data directory
|
// Create the Meta data file and data directory
|
||||||
if (tsdbCreateRepoFiles(pRepo) < 0) {
|
if (tsdbSetRepoEnv(pRepo) < 0) {
|
||||||
// Failed to create and save files
|
free(pRepo->rootDir);
|
||||||
tsdbFreeMetaHandle(pRepo->tsdbCache);
|
tsdbFreeMeta(pRepo->tsdbMeta);
|
||||||
|
tsdbFreeCache(pRepo->tsdbCache);
|
||||||
free(pRepo);
|
free(pRepo);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -101,12 +107,16 @@ int32_t tsdbDropRepo(tsdb_repo_t *repo) {
|
||||||
pRepo->state = TSDB_REPO_STATE_CLOSED;
|
pRepo->state = TSDB_REPO_STATE_CLOSED;
|
||||||
|
|
||||||
// Free the metaHandle
|
// Free the metaHandle
|
||||||
tsdbFreeMetaHandle(pRepo->tsdbMeta);
|
tsdbFreeMeta(pRepo->tsdbMeta);
|
||||||
|
|
||||||
// Free the cache
|
// Free the cache
|
||||||
tsdbFreeCache(pRepo->tsdbCache);
|
tsdbFreeCache(pRepo->tsdbCache);
|
||||||
|
|
||||||
tsdbClearFiles(pRepo);
|
// Destroy the repository info
|
||||||
|
tsdbDestroyRepoEnv(pRepo);
|
||||||
|
|
||||||
|
free(pRepo->rootDir);
|
||||||
|
free(pRepo);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -122,7 +132,7 @@ tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Initialize configuration from the file
|
// TODO: Initialize configuration from the file
|
||||||
pRepo->tsdbMeta = tsdbOpenMetaHandle();
|
pRepo->tsdbMeta = tsdbCreateMeta(pRepo->config.maxTables);
|
||||||
if (pRepo->tsdbMeta == NULL) {
|
if (pRepo->tsdbMeta == NULL) {
|
||||||
free(pRepo);
|
free(pRepo);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -150,9 +160,9 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo) {
|
||||||
|
|
||||||
pRepo->state = TSDB_REPO_STATE_CLOSED;
|
pRepo->state = TSDB_REPO_STATE_CLOSED;
|
||||||
|
|
||||||
tsdbFreeMetaHandle(pRepo->tsdbMeta);
|
tsdbFreeMeta(pRepo->tsdbMeta);
|
||||||
|
|
||||||
tsdbFreeCache(pRepo->tsdbMeta);
|
tsdbFreeCache(pRepo->tsdbCache);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -160,7 +170,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->config = pCfg;
|
pRepo->config = *pCfg;
|
||||||
// TODO
|
// TODO
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -192,10 +202,61 @@ static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t tsdbCreateRepoFiles(STsdbRepo *pRepo) {
|
static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo) {
|
||||||
// TODO
|
char *metaFname = tsdbGetFileName(pRepo->rootDir, "tsdb", TSDB_FILE_TYPE_META);
|
||||||
|
|
||||||
|
int fd = open(metaFname, O_WRONLY|O_CREAT);
|
||||||
|
if (fd < 0) {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t tsdbClearFiles(STsdbRepo *pRepo) {
|
if (write(fd, (void *)(&(pRepo->config)), sizeof(STsdbCfg)) < 0) {
|
||||||
// TODO
|
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 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;
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
#include "tsdb.h"
|
#include "tsdb.h"
|
||||||
#include "tsdbMeta.h"
|
#include "tsdbMeta.h"
|
||||||
|
|
||||||
STsdbMeta *tsdbCreateMeta(int32_t maxNumOfTables) {
|
STsdbMeta *tsdbCreateMeta(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;
|
||||||
|
@ -12,7 +12,7 @@ STsdbMeta *tsdbCreateMeta(int32_t maxNumOfTables) {
|
||||||
|
|
||||||
pMeta->numOfTables = 0;
|
pMeta->numOfTables = 0;
|
||||||
pMeta->numOfSuperTables = 0;
|
pMeta->numOfSuperTables = 0;
|
||||||
pMeta->pTables = calloc(sizeof(STable *), numOfTables);
|
pMeta->pTables = calloc(sizeof(STable *), maxTables);
|
||||||
if (pMeta->pTables == NULL) {
|
if (pMeta->pTables == NULL) {
|
||||||
free(pMeta);
|
free(pMeta);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -29,9 +29,7 @@ STsdbMeta *tsdbCreateMeta(int32_t maxNumOfTables) {
|
||||||
return pMeta;
|
return pMeta;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t tsdbFreeMetaHandle(STsdbMeta *pMetaHandle) {
|
int32_t tsdbFreeMeta(STsdbMeta *pMeta) {
|
||||||
// TODO
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; }
|
static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; }
|
||||||
|
@ -47,7 +45,7 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pHandle, STableCfg *pCfg) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pHandle->pTables[pCfg->tableId] = pTable;
|
pHandle->pTables[pCfg->tableId.tid] = pTable;
|
||||||
|
|
||||||
// TODO: add name to it
|
// TODO: add name to it
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue