diff --git a/src/common/src/dataformat.c b/src/common/src/dataformat.c index 8f5389f9aa7d60c665e75f24bfd9e3230a12b03c..419e37639257b2172ffe4eaa0c68436bb8ff6f6c 100644 --- a/src/common/src/dataformat.c +++ b/src/common/src/dataformat.c @@ -91,7 +91,7 @@ void tdSetCol(STColumn *pCol, int8_t type, int16_t colId, int32_t bytes) { STSchema *tdNewSchema(int32_t nCols) { int32_t size = sizeof(STSchema) + sizeof(STColumn) * nCols; - STSchema *pSchema = (STSchema *)malloc(size); + STSchema *pSchema = (STSchema *)calloc(1, size); if (pSchema == NULL) return NULL; pSchema->numOfCols = 0; diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index 57ab85a67b13802d2ae5c71dc8029d0cfbbcafb4..073321816db29939f5ba28400ad5ad5f598bfa29 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -78,6 +78,7 @@ static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo); static int32_t tsdbDestroyRepoEnv(STsdbRepo *pRepo); static int tsdbOpenMetaFile(char *tsdbDir); static int32_t tsdbInsertDataToTable(tsdb_repo_t *repo, SSubmitBlk *pBlock); +static int32_t tsdbRestoreCfg(STsdbRepo *pRepo, STsdbCfg *pCfg); #define TSDB_GET_TABLE_BY_ID(pRepo, sid) (((STSDBRepo *)pRepo)->pTableList)[sid] #define TSDB_GET_TABLE_BY_NAME(pRepo, name) @@ -220,8 +221,10 @@ tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) { pRepo->rootDir = strdup(tsdbDir); + tsdbRestoreCfg(pRepo, &(pRepo->config)); + pRepo->tsdbMeta = tsdbInitMeta(tsdbDir, pRepo->config.maxTables); - if (pRepo == NULL) { + if (pRepo->tsdbMeta == NULL) { free(pRepo->rootDir); free(pRepo); return NULL; diff --git a/src/vnode/tsdb/src/tsdbMeta.c b/src/vnode/tsdb/src/tsdbMeta.c index 3e4d3e8f5808c4411f3d2e07485927b120245976..7f1cd0fd1aad2a306f31942e6360a7d42bade49f 100644 --- a/src/vnode/tsdb/src/tsdbMeta.c +++ b/src/vnode/tsdb/src/tsdbMeta.c @@ -79,7 +79,7 @@ STable *tsdbDecodeTable(void *cont, int contLen) { T_READ_MEMBER(ptr, int32_t, pTable->superUid); T_READ_MEMBER(ptr, int32_t, pTable->sversion); - if (pTable->type = TSDB_SUPER_TABLE) { + if (pTable->type == TSDB_SUPER_TABLE) { pTable->schema = tdDecodeSchema(&ptr); pTable->tagSchema = tdDecodeSchema(&ptr); } else if (pTable->type == TSDB_CHILD_TABLE) { diff --git a/src/vnode/tsdb/tests/tsdbTests.cpp b/src/vnode/tsdb/tests/tsdbTests.cpp index 5ce25d2fd7dd88fb6d8dbfed3eda0333a3e172e5..46ae3940d2f260bbf3afa5cdf5c970ae6fe544c1 100644 --- a/src/vnode/tsdb/tests/tsdbTests.cpp +++ b/src/vnode/tsdb/tests/tsdbTests.cpp @@ -3,6 +3,44 @@ #include "tsdb.h" #include "dataformat.h" +#include "tsdbMeta.h" + +TEST(TsdbTest, tableEncodeDecode) { + STable *pTable = (STable *)malloc(sizeof(STable)); + + pTable->type = TSDB_NORMAL_TABLE; + pTable->tableId.uid = 987607499877672L; + pTable->tableId.tid = 0; + pTable->superUid = -1; + pTable->sversion = 0; + pTable->tagSchema = NULL; + pTable->tagVal = NULL; + int nCols = 5; + STSchema *schema = tdNewSchema(nCols); + + for (int i = 0; i < nCols; i++) { + if (i == 0) { + tdSchemaAppendCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1); + } else { + tdSchemaAppendCol(schema, TSDB_DATA_TYPE_INT, i, -1); + } + } + + pTable->schema = schema; + + int bufLen = 0; + void *buf = tsdbEncodeTable(pTable, &bufLen); + + STable *tTable = tsdbDecodeTable(buf, bufLen); + + ASSERT_EQ(pTable->type, tTable->type); + ASSERT_EQ(pTable->tableId.uid, tTable->tableId.uid); + ASSERT_EQ(pTable->tableId.tid, tTable->tableId.tid); + ASSERT_EQ(pTable->superUid, tTable->superUid); + ASSERT_EQ(pTable->sversion, tTable->sversion); + ASSERT_EQ(memcmp(pTable->schema, tTable->schema, sizeof(STSchema) + sizeof(STColumn) * nCols), 0); + ASSERT_EQ(tTable->content.pData, nullptr); +} TEST(TsdbTest, createRepo) { STsdbCfg config; @@ -65,3 +103,7 @@ TEST(TsdbTest, createRepo) { int k = 0; } +TEST(TsdbTest, openRepo) { + tsdb_repo_t *pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0"); + ASSERT_NE(pRepo, nullptr); +} \ No newline at end of file