提交 c80686b9 编写于 作者: H hzcheng

add more and refactor

上级 a3f5bec0
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "tsdb.h" #include "tsdb.h"
#include "dataformat.h" #include "dataformat.h"
#include "tskiplist.h" #include "tskiplist.h"
#include "tsdbMetaFile.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
...@@ -78,14 +79,24 @@ typedef struct STable { ...@@ -78,14 +79,24 @@ typedef struct STable {
} STable; } STable;
// ---------- TSDB META HANDLE DEFINITION
typedef struct { typedef struct {
int32_t maxTables; int32_t maxTables; // Max number of tables
int32_t nTables;
STable **tables; // array of normal tables int32_t nTables; // Tables created
STable * stables; // linked list of super tables // TODO use container to implement this
void * tableMap; // hash map of uid ==> STable * STable **tables; // table array
STable *superList; // super table list TODO: change it to list container
void *map; // table map of (uid ===> table)
SMetaFile *mfh; // meta file handle
} STsdbMeta; } STsdbMeta;
STsdbMeta *tsdbInitMeta(const char *rootDir, int32_t maxTables);
int32_t tsdbFreeMeta(STsdbMeta *pMeta);
// ---- Operation on STable // ---- Operation on STable
#define TSDB_TABLE_ID(pTable) ((pTable)->tableId) #define TSDB_TABLE_ID(pTable) ((pTable)->tableId)
#define TSDB_TABLE_UID(pTable) ((pTable)->uid) #define TSDB_TABLE_UID(pTable) ((pTable)->uid)
...@@ -105,13 +116,6 @@ STSchema *tsdbGetTableSchema(STable *pTable); ...@@ -105,13 +116,6 @@ STSchema *tsdbGetTableSchema(STable *pTable);
#define TSDB_TABLE_OF_ID(pHandle, id) ((pHandle)->pTables)[id] #define TSDB_TABLE_OF_ID(pHandle, id) ((pHandle)->pTables)[id]
#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
STsdbMeta *tsdbInitMeta(int32_t maxTables);
int32_t tsdbFreeMeta(STsdbMeta *pMeta);
// Recover the meta handle from the file
STsdbMeta *tsdbOpenMeta(char *tsdbDir);
int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg); int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg);
int32_t tsdbDropTableImpl(STsdbMeta *pMeta, STableId tableId); int32_t tsdbDropTableImpl(STsdbMeta *pMeta, STableId tableId);
STable *tsdbIsValidTableToInsert(STsdbMeta *pMeta, STableId tableId); STable *tsdbIsValidTableToInsert(STsdbMeta *pMeta, STableId tableId);
......
...@@ -18,13 +18,12 @@ ...@@ -18,13 +18,12 @@
#include <stdint.h> #include <stdint.h>
#include "tsdbMeta.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define TSDB_META_FILE_NAME "META" #define TSDB_META_FILE_NAME "META"
#define TSDB_META_HASH_FRACTION 1.1
typedef struct { typedef struct {
int fd; // File descriptor int fd; // File descriptor
......
...@@ -145,8 +145,15 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO ...@@ -145,8 +145,15 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO
pRepo->config = *pCfg; pRepo->config = *pCfg;
pRepo->limiter = limiter; pRepo->limiter = limiter;
// Create the environment files and directories
if (tsdbSetRepoEnv(pRepo) < 0) {
free(pRepo->rootDir);
free(pRepo);
return NULL;
}
// Initialize meta // Initialize meta
STsdbMeta *pMeta = tsdbInitMeta(pCfg->maxTables); STsdbMeta *pMeta = tsdbInitMeta(rootDir, pCfg->maxTables);
if (pMeta == NULL) { if (pMeta == NULL) {
free(pRepo->rootDir); free(pRepo->rootDir);
free(pRepo); free(pRepo);
...@@ -164,15 +171,6 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO ...@@ -164,15 +171,6 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO
} }
pRepo->tsdbCache = pCache; pRepo->tsdbCache = pCache;
// Create the environment files and directories
if (tsdbSetRepoEnv(pRepo) < 0) {
free(pRepo->rootDir);
tsdbFreeMeta(pRepo->tsdbMeta);
tsdbFreeCache(pRepo->tsdbCache);
free(pRepo);
return NULL;
}
pRepo->state = TSDB_REPO_STATE_ACTIVE; pRepo->state = TSDB_REPO_STATE_ACTIVE;
return (tsdb_repo_t *)pRepo; return (tsdb_repo_t *)pRepo;
......
...@@ -18,23 +18,33 @@ static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable); ...@@ -18,23 +18,33 @@ 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 *tsdbInitMeta(int32_t maxTables) { /**
* Initialize the meta handle
* ASSUMPTIONS: VALID PARAMETER
*/
STsdbMeta *tsdbInitMeta(const char *rootDir, 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;
}
pMeta->maxTables = maxTables; pMeta->maxTables = maxTables;
pMeta->nTables = 0; pMeta->nTables = 0;
pMeta->stables = NULL; pMeta->superList = NULL;
pMeta->tables = (STable **)calloc(maxTables, sizeof(STable *)); pMeta->tables = (STable **)calloc(maxTables, sizeof(STable *));
if (pMeta->tables == NULL) { if (pMeta->tables == NULL) {
free(pMeta); free(pMeta);
return NULL; return NULL;
} }
pMeta->tableMap = taosInitHashTable(maxTables + maxTables / 10, taosGetDefaultHashFunction, false); pMeta->map = taosInitHashTable(maxTables * TSDB_META_HASH_FRACTION, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false);
if (pMeta->tableMap == NULL) { if (pMeta->map == NULL) {
free(pMeta->tables);
free(pMeta);
return NULL;
}
pMeta->mfh = tsdbInitMetaFile(rootDir, maxTables);
if (pMeta->mfh == NULL) {
taosCleanUpHashTable(pMeta->map);
free(pMeta->tables); free(pMeta->tables);
free(pMeta); free(pMeta);
return NULL; return NULL;
...@@ -46,6 +56,8 @@ STsdbMeta *tsdbInitMeta(int32_t maxTables) { ...@@ -46,6 +56,8 @@ STsdbMeta *tsdbInitMeta(int32_t maxTables) {
int32_t tsdbFreeMeta(STsdbMeta *pMeta) { int32_t tsdbFreeMeta(STsdbMeta *pMeta) {
if (pMeta == NULL) return 0; if (pMeta == NULL) return 0;
tsdbCloseMetaFile(pMeta->mfh);
for (int i = 0; i < pMeta->maxTables; i++) { for (int i = 0; i < pMeta->maxTables; i++) {
if (pMeta->tables[i] != NULL) { if (pMeta->tables[i] != NULL) {
tsdbFreeTable(pMeta->tables[i]); tsdbFreeTable(pMeta->tables[i]);
...@@ -54,14 +66,14 @@ int32_t tsdbFreeMeta(STsdbMeta *pMeta) { ...@@ -54,14 +66,14 @@ int32_t tsdbFreeMeta(STsdbMeta *pMeta) {
free(pMeta->tables); free(pMeta->tables);
STable *pTable = pMeta->stables; STable *pTable = pMeta->superList;
while (pTable != NULL) { while (pTable != NULL) {
STable *pTemp = pTable; STable *pTemp = pTable;
pTable = pTemp->next; pTable = pTemp->next;
tsdbFreeTable(pTemp); tsdbFreeTable(pTemp);
} }
taosCleanUpHashTable(pMeta->tableMap); taosCleanUpHashTable(pMeta->map);
free(pMeta); free(pMeta);
...@@ -126,17 +138,6 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) { ...@@ -126,17 +138,6 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) {
return 0; return 0;
} }
STsdbMeta *tsdbOpenMeta(char *tsdbDir) {
// TODO : Open meta file for reading
STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta));
if (pMeta == NULL) {
return NULL;
}
return pMeta;
}
/** /**
* Check if a table is valid to insert. * Check if a table is valid to insert.
* @return NULL for invalid and the pointer to the table if valid * @return NULL for invalid and the pointer to the table if valid
...@@ -206,7 +207,7 @@ static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { ...@@ -206,7 +207,7 @@ static int32_t tsdbCheckTableCfg(STableCfg *pCfg) {
} }
STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t uid) { STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t uid) {
void *ptr = taosGetDataFromHashTable(pMeta->tableMap, (char *)(&uid), sizeof(uid)); void *ptr = taosGetDataFromHashTable(pMeta->map, (char *)(&uid), sizeof(uid));
if (ptr == NULL) return NULL; if (ptr == NULL) return NULL;
...@@ -216,12 +217,12 @@ STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t uid) { ...@@ -216,12 +217,12 @@ STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t uid) {
static int tsdbAddTableToMeta(STsdbMeta *pMeta, STable *pTable) { static int tsdbAddTableToMeta(STsdbMeta *pMeta, STable *pTable) {
if (pTable->type == TSDB_SUPER_TABLE) { if (pTable->type == TSDB_SUPER_TABLE) {
// add super table to the linked list // add super table to the linked list
if (pMeta->stables == NULL) { if (pMeta->superList == NULL) {
pMeta->stables = pTable; pMeta->superList = pTable;
pTable->next = NULL; pTable->next = NULL;
} else { } else {
STable *pTemp = pMeta->stables; STable *pTemp = pMeta->superList;
pMeta->stables = pTable; pMeta->superList = pTable;
pTable->next = pTemp; pTable->next = pTemp;
} }
} else { } else {
...@@ -245,7 +246,7 @@ static int tsdbRemoveTableFromMeta(STsdbMeta *pMeta, STable *pTable) { ...@@ -245,7 +246,7 @@ static int tsdbRemoveTableFromMeta(STsdbMeta *pMeta, STable *pTable) {
static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable) { static int tsdbAddTableIntoMap(STsdbMeta *pMeta, STable *pTable) {
// TODO: add the table to the map // TODO: add the table to the map
int64_t uid = pTable->tableId.uid; int64_t uid = pTable->tableId.uid;
if (taosAddToHashTable(pMeta->tableMap, (char *)(&uid), sizeof(uid), (void *)(&pTable), sizeof(pTable)) < 0) { if (taosAddToHashTable(pMeta->map, (char *)(&uid), sizeof(uid), (void *)(&pTable), sizeof(pTable)) < 0) {
return -1; return -1;
} }
return 0; return 0;
......
...@@ -15,11 +15,11 @@ ...@@ -15,11 +15,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "taosdef.h"
#include "hash.h" #include "hash.h"
#include "tsdbMetaFile.h" #include "tsdbMetaFile.h"
#define TSDB_META_FILE_HEADER_SIZE 512 #define TSDB_META_FILE_HEADER_SIZE 512
#define TSDB_META_HASH_FRACTION 1.1
typedef struct { typedef struct {
int32_t offset; int32_t offset;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册