提交 c80686b9 编写于 作者: H hzcheng

add more and refactor

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