提交 0dde6985 编写于 作者: H hzcheng

TD-34

上级 89c97225
...@@ -24,9 +24,10 @@ extern "C" { ...@@ -24,9 +24,10 @@ extern "C" {
#endif #endif
typedef enum { typedef enum {
TSDB_FILE_TYPE_HEAD, // .head file type TSDB_FILE_TYPE_HEAD = 0, // .head file type
TSDB_FILE_TYPE_DATA, // .data file type TSDB_FILE_TYPE_DATA, // .data file type
TSDB_FILE_TYPE_LAST // .last file type TSDB_FILE_TYPE_LAST, // .last file type
TSDB_FILE_TYPE_MAX
} TSDB_FILE_TYPE; } TSDB_FILE_TYPE;
extern const char *tsdbFileSuffix[]; extern const char *tsdbFileSuffix[];
...@@ -37,15 +38,15 @@ typedef struct { ...@@ -37,15 +38,15 @@ typedef struct {
} SFileInfo; } SFileInfo;
typedef struct { typedef struct {
int64_t size; // total size of the file int8_t type;
int64_t tombSize; // unused file size char fname[128];
int64_t size; // total size of the file
int64_t tombSize; // unused file size
} SFile; } SFile;
typedef struct { typedef struct {
int32_t fileId; int32_t fileId;
SFile fhead; SFile files[TSDB_FILE_TYPE_MAX];
SFile fdata;
SFile flast;
} SFileGroup; } SFileGroup;
// TSDB file handle // TSDB file handle
...@@ -57,14 +58,12 @@ typedef struct { ...@@ -57,14 +58,12 @@ typedef struct {
SFileGroup fGroup[]; SFileGroup fGroup[];
} STsdbFileH; } STsdbFileH;
#define IS_VALID_TSDB_FILE_TYPE(type) ((type) >= TSDB_FILE_TYPE_HEAD && (type) <= TSDB_FILE_TYPE_LAST) #define IS_VALID_TSDB_FILE_TYPE(type) ((type) >= TSDB_FILE_TYPE_HEAD && (type) < TSDB_FILE_TYPE_MAX)
STsdbFileH *tsdbInitFile(char *dataDir, int32_t daysPerFile, int32_t keep, int32_t minRowsPerFBlock, STsdbFileH *tsdbInitFile(char *dataDir, int32_t daysPerFile, int32_t keep, int32_t minRowsPerFBlock,
int32_t maxRowsPerFBlock); int32_t maxRowsPerFBlock);
void tsdbCloseFile(STsdbFileH *pFileH); void tsdbCloseFile(STsdbFileH *pFileH);
int tsdbCreateFileGroup(char *dataDir, int fileId, SFileGroup *pFGroup, int maxTables);
char *tsdbGetFileName(char *dirName, char *fname, TSDB_FILE_TYPE type);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -88,23 +88,26 @@ const char *tsdbFileSuffix[] = { ...@@ -88,23 +88,26 @@ const char *tsdbFileSuffix[] = {
".last" // TSDB_FILE_TYPE_LAST ".last" // TSDB_FILE_TYPE_LAST
}; };
static int tsdbWriteFileHead(int fd) { static int tsdbWriteFileHead(int fd, SFile *pFile) {
char head[TSDB_FILE_HEAD_SIZE] = "\0"; char head[TSDB_FILE_HEAD_SIZE] = "\0";
pFile->size += TSDB_FILE_HEAD_SIZE;
// TODO: write version and File statistic to the head
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
if (write(fd, head, TSDB_FILE_HEAD_SIZE) < 0) return -1; if (write(fd, head, TSDB_FILE_HEAD_SIZE) < 0) return -1;
return 0; return 0;
} }
static int tsdbWriteHeadFileIdx(int fd, int maxTables) { static int tsdbWriteHeadFileIdx(int fd, int maxTables, SFile *pFile) {
int size = sizeof(SCompIdx) * maxTables; int size = sizeof(SCompIdx) * maxTables;
void *buf = calloc(1, size); void *buf = calloc(1, size);
if (buf == NULL) return -1; if (buf == NULL) return -1;
if (lseek(fd, TSDB_FILE_HEAD_SIZE, SEEK_SET) < 0) { if (lseek(fd, TSDB_FILE_HEAD_SIZE, SEEK_SET) < 0) {
free(buf); free(buf);
return NULL; return -1;
} }
if (write(fd, buf, size) < 0) { if (write(fd, buf, size) < 0) {
...@@ -112,40 +115,70 @@ static int tsdbWriteHeadFileIdx(int fd, int maxTables) { ...@@ -112,40 +115,70 @@ static int tsdbWriteHeadFileIdx(int fd, int maxTables) {
return -1; return -1;
} }
pFile->size += size;
return 0; return 0;
} }
static int tsdbCreateFile(char *dataDir, int fileId, int8_t type, int maxTables) { static int tsdbGetFileName(char *dataDir, int fileId, int8_t type, char *fname) {
char fname[128] = "\0"; if (dataDir == NULL || fname == NULL || !IS_VALID_TSDB_FILE_TYPE(type)) return -1;
sprintf(fname, "%s/f%d%s", dataDir, fileId, tsdbFileSuffix[type]); sprintf(fname, "%s/f%d%s", dataDir, fileId, tsdbFileSuffix[type]);
if (access(fname, F_OK) == 0) {
return 0;
}
/**
* Create a file and set the SFile object
*/
static int tsdbCreateFile(char *dataDir, int fileId, int8_t type, int maxTables, SFile *pFile) {
memset((void *)pFile, 0, sizeof(SFile));
pFile->type = type;
tsdbGetFileName(dataDir, fileId, type, pFile->fname);
if (access(pFile->fname, F_OK) == 0) {
// File already exists // File already exists
return -1; return -1;
} }
int fd = open(fname, O_RDWR | O_CREAT, 0755); int fd = open(pFile->fname, O_WRONLY | O_CREAT, 0755);
if (fd < 0) return -1; if (fd < 0) return -1;
if (tsdbWriteFileHead(fd) < 0) { if (type == TSDB_FILE_TYPE_HEAD) {
close(fd); if (tsdbWriteHeadFileIdx(fd, maxTables, pFile) < 0) {
return -1;
}
if (type == TSDB_FILE_TYPE_LAST) {
if (tsdbWriteHeadFileIdx(fd, maxTables) < 0) {
close(fd); close(fd);
return -1; return -1;
} }
} }
if (tsdbWriteFileHead(fd, pFile) < 0) {
close(fd);
return -1;
}
close(fd); close(fd);
return 0; return 0;
} }
/**
*
*/
// Create a file group with fileId and return a SFileGroup object // Create a file group with fileId and return a SFileGroup object
static int tsdbCreateFileGroup(char *dataDir, int fileId, SFileGroup *pFGroup) { int tsdbCreateFileGroup(char *dataDir, int fileId, SFileGroup *pFGroup, int maxTables) {
// tsdbCreateFile() if (dataDir == NULL || pFGroup == NULL) return -1;
memset((void *)pFGroup, 0, sizeof(SFileGroup));
for (int type = TSDB_FILE_TYPE_HEAD; type < TSDB_FILE_TYPE_MAX; type++) {
if (tsdbCreateFile(dataDir, fileId, type, maxTables, &(pFGroup->files[type])) < 0) {
// TODO: deal with the error here, remove the created files
return -1;
}
}
pFGroup->fileId = fileId;
return 0; return 0;
} }
...@@ -199,16 +232,6 @@ void tsdbCloseFile(STsdbFileH *pFileH) { ...@@ -199,16 +232,6 @@ void tsdbCloseFile(STsdbFileH *pFileH) {
// TODO // TODO
} }
char *tsdbGetFileName(char *dirName, char *fname, TSDB_FILE_TYPE type) {
if (!IS_VALID_TSDB_FILE_TYPE(type)) return NULL;
char *fileName = (char *)malloc(strlen(dirName) + strlen(fname) + strlen(tsdbFileSuffix[type]) + 5);
if (fileName == NULL) return NULL;
sprintf(fileName, "%s/%s%s", dirName, fname, tsdbFileSuffix[type]);
return fileName;
}
static void tsdbGetKeyRangeOfFileId(int32_t daysPerFile, int8_t precision, int32_t fileId, TSKEY *minKey, static void tsdbGetKeyRangeOfFileId(int32_t daysPerFile, int8_t precision, int32_t fileId, TSKEY *minKey,
TSKEY *maxKey) { TSKEY *maxKey) {
*minKey = fileId * daysPerFile * tsMsPerDay[precision]; *minKey = fileId * daysPerFile * tsMsPerDay[precision];
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "tsdb.h" #include "tsdb.h"
#include "dataformat.h" #include "dataformat.h"
#include "tsdbFile.h"
#include "tsdbMeta.h" #include "tsdbMeta.h"
TEST(TsdbTest, tableEncodeDecode) { TEST(TsdbTest, tableEncodeDecode) {
...@@ -106,4 +107,12 @@ TEST(TsdbTest, createRepo) { ...@@ -106,4 +107,12 @@ TEST(TsdbTest, createRepo) {
TEST(TsdbTest, openRepo) { TEST(TsdbTest, openRepo) {
tsdb_repo_t *pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0"); tsdb_repo_t *pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0");
ASSERT_NE(pRepo, nullptr); ASSERT_NE(pRepo, nullptr);
}
TEST(TsdbTest, createFileGroup) {
SFileGroup fGroup;
ASSERT_EQ(tsdbCreateFileGroup("/home/ubuntu/work/ttest/vnode0/data", 1820, &fGroup, 1000), 0);
int k = 0;
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册