diff --git a/src/vnode/tsdb/inc/tsdb.h b/src/vnode/tsdb/inc/tsdb.h index 655448ed16f0685f7209f8c69a4498ad227930d4..91e159a1a46a1523a567dd67422b8635352bd02a 100644 --- a/src/vnode/tsdb/inc/tsdb.h +++ b/src/vnode/tsdb/inc/tsdb.h @@ -98,6 +98,17 @@ typedef struct { int64_t tableTotalDiskSize; // In bytes } STableInfo; +/** + * Create a configuration for TSDB default + * @return a pointer to a configuration. the configuration must call tsdbFreeCfg to free memory after usage + */ +STsdbCfg *tsdbCreateDefaultCfg(); + +/** + * Free + */ +void tsdbFreeCfg(STsdbCfg *pCfg); + /** * Create a new TSDB repository * @param rootDir the TSDB repository root directory diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index cf217f07711e577c5c2af46efdeddee07d58c361..e73fa8672d621c341aabf331bb4cee3853ebe6dd 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -1,13 +1,14 @@ -#include +#include #include +#include #include #include +#include #include +#include #include #include #include -#include -#include // #include "taosdef.h" // #include "disk.h" @@ -16,6 +17,29 @@ #include "tsdbFile.h" #include "tsdbMeta.h" +#define TSDB_DEFAULT_PRECISION TSDB_PRECISION_MILLI // default precision +#define IS_VALID_PRECISION(precision) (((precision) >= TSDB_PRECISION_MILLI) && ((precision) <= TSDB_PRECISION_NANO)) +#define TSDB_MIN_ID 0 +#define TSDB_MAX_ID INT_MAX +#define TSDB_MIN_TABLES 10 +#define TSDB_MAX_TABLES 100000 +#define TSDB_DEFAULT_TABLES 1000 +#define TSDB_DEFAULT_DAYS_PER_FILE 10 +#define TSDB_MIN_DAYS_PER_FILE 1 +#define TSDB_MAX_DAYS_PER_FILE 60 +#define TSDB_DEFAULT_MIN_ROW_FBLOCK 100 +#define TSDB_MIN_MIN_ROW_FBLOCK 10 +#define TSDB_MAX_MIN_ROW_FBLOCK 1000 +#define TSDB_DEFAULT_MAX_ROW_FBLOCK 4096 +#define TSDB_MIN_MAX_ROW_FBLOCK 200 +#define TSDB_MAX_MAX_ROW_FBLOCK 10000 +#define TSDB_DEFAULT_KEEP 3650 +#define TSDB_MIN_KEEP 1 +#define TSDB_MAX_KEEP INT_MAX +#define TSDB_DEFAULT_CACHE_SIZE (16 * 1024 * 1024) // 16M +#define TSDB_MIN_CACHE_SIZE (4 * 1024 * 1024) // 4M +#define TSDB_MAX_CACHE_SIZE (1024 * 1024 * 1024) // 1G + enum { TSDB_REPO_STATE_ACTIVE, TSDB_REPO_STATE_CLOSED, TSDB_REPO_STATE_CONFIGURING }; typedef struct _tsdb_repo { @@ -55,11 +79,31 @@ static int tsdbRecoverRepo(int fd, STsdbCfg *pCfg); #define TSDB_IS_REPO_ACTIVE(pRepo) ((pRepo)->state == TSDB_REPO_STATE_ACTIVE) #define TSDB_IS_REPO_CLOSED(pRepo) ((pRepo)->state == TSDB_REPO_STATE_CLOSED) -tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) { +STsdbCfg *tsdbCreateDefaultCfg() { + STsdbCfg *pCfg = (STsdbCfg *)malloc(sizeof(STsdbCfg)); + if (pCfg == NULL) return NULL; + + pCfg->precision = -1; + pCfg->tsdbId = 0; + pCfg->maxTables = -1; + pCfg->daysPerFile = -1; + pCfg->minRowsPerFileBlock = -1; + pCfg->maxRowsPerFileBlock = -1; + pCfg->keep = -1; + pCfg->maxCacheSize = -1; + return pCfg; +} + +void tsdbFreeCfg(STsdbCfg *pCfg) { + if (pCfg != NULL) free(pCfg); +} + + +tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) { if (rootDir == NULL) return NULL; - if (access(rootDir, F_OK|R_OK|W_OK) == -1) return NULL; + if (access(rootDir, F_OK | R_OK | W_OK) == -1) return NULL; if (tsdbCheckAndSetDefaultCfg(pCfg) < 0) { return NULL; @@ -200,9 +244,7 @@ int32_t tsdbAlterTable(tsdb_repo_t *pRepo, STableCfg *pCfg) { return 0; } -int32_t tsdbDropTable(tsdb_repo_t *pRepo, STableId tid) { - return 0; -} +int32_t tsdbDropTable(tsdb_repo_t *pRepo, STableId tid) { return 0; } STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid) { // TODO @@ -219,14 +261,68 @@ int32_t tsdbInsertData(tsdb_repo_t *repo, STableId tableId, char *pData) { // Check the configuration and set default options static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) { - // TODO + // Check precision + if (pCfg->precision == -1) { + pCfg->precision = TSDB_DEFAULT_PRECISION; + } else { + if (!IS_VALID_PRECISION(pCfg->precision)) return -1; + } + + // Check tsdbId + if (pCfg->tsdbId < 0) return -1; + + // Check MaxTables + if (pCfg->maxTables == -1) { + pCfg->maxTables = TSDB_DEFAULT_TABLES; + } else { + if (pCfg->maxTables < TSDB_MIN_TABLES || pCfg->maxTables > TSDB_MAX_TABLES) return -1; + } + + // Check daysPerFile + if (pCfg->daysPerFile == -1) { + pCfg->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE; + } else { + if (pCfg->daysPerFile < TSDB_MIN_DAYS_PER_FILE || pCfg->daysPerFile > TSDB_MAX_DAYS_PER_FILE) return -1; + } + + // Check minRowsPerFileBlock and maxRowsPerFileBlock + if (pCfg->minRowsPerFileBlock == -1) { + pCfg->minRowsPerFileBlock = TSDB_DEFAULT_MIN_ROW_FBLOCK; + } else { + if (pCfg->minRowsPerFileBlock < TSDB_MIN_MIN_ROW_FBLOCK || pCfg->minRowsPerFileBlock > TSDB_MAX_MIN_ROW_FBLOCK) + return -1; + } + + if (pCfg->maxRowsPerFileBlock == -1) { + pCfg->maxRowsPerFileBlock = TSDB_DEFAULT_MAX_ROW_FBLOCK; + } else { + if (pCfg->maxRowsPerFileBlock < TSDB_MIN_MAX_ROW_FBLOCK || pCfg->maxRowsPerFileBlock > TSDB_MAX_MAX_ROW_FBLOCK) + return -1; + } + + if (pCfg->minRowsPerFileBlock > pCfg->maxRowsPerFileBlock) return -1; + + // Check keep + if (pCfg->keep == -1) { + pCfg->keep = TSDB_DEFAULT_KEEP; + } else { + if (pCfg->keep < TSDB_MIN_KEEP || pCfg->keep > TSDB_MAX_KEEP) return -1; + } + + // Check maxCacheSize + if (pCfg->maxCacheSize == -1) { + pCfg->maxCacheSize = TSDB_DEFAULT_CACHE_SIZE; + } else { + if (pCfg->maxCacheSize < TSDB_MIN_CACHE_SIZE || pCfg->maxCacheSize > TSDB_MAX_CACHE_SIZE) return -1; + } + return 0; } static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo) { char *metaFname = tsdbGetFileName(pRepo->rootDir, "tsdb", TSDB_FILE_TYPE_META); - int fd = open(metaFname, O_WRONLY|O_CREAT); + int fd = open(metaFname, O_WRONLY | O_CREAT); if (fd < 0) { return -1; } diff --git a/src/vnode/tsdb/src/tsdbMeta.c b/src/vnode/tsdb/src/tsdbMeta.c index 76801b1ddce9cfb9b1eb16d1710765711fd124d2..e56254456cb1debd87c2af04aad3d9bf4ead8f9d 100644 --- a/src/vnode/tsdb/src/tsdbMeta.c +++ b/src/vnode/tsdb/src/tsdbMeta.c @@ -10,6 +10,8 @@ #define TSDB_MAX_TABLES 100000 #define TSDB_DEFAULT_NSTABLES 10 +#define TSDB_SUPER_TABLE_SL_LEVEL 5 // TODO: may change here + #define IS_VALID_MAX_TABLES(maxTables) (((maxTables) >= TSDB_MIN_TABLES) && ((maxTables) <= TSDB_MAX_TABLES)) static int tsdbFreeTable(STable *pTable); @@ -90,7 +92,8 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) { pSTable->stableUid = -1; pSTable->numOfCols = pCfg->numOfCols; pSTable->pSchema = tdDupSchema(pCfg->schema); - // pSTable->content.pIndex = tSkipListCreate(5, 0, 10); // TODO: change here + pSTable->content.pIndex = + tSkipListCreate(TSDB_SUPER_TABLE_SL_LEVEL, 0, 8, 0, 0, NULL); // TODO: change here tsdbAddTable(pMeta, pSTable); } else { if (pSTable->type != TSDB_SUPER_TABLE) return NULL; @@ -113,7 +116,7 @@ int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg) { pTable->stableUid = -1; pTable->pSchema = tdDupSchema(pCfg->schema); } - // pTable->content.pData = tSkipListCreate(5, 0, 10); // TODO: change here + pTable->content.pData = tSkipListCreate(TSDB_SUPER_TABLE_SL_LEVEL, 0, 8, 0, 0, NULL); tsdbAddTable(pMeta, pTable);