提交 e9bf4fce 编写于 作者: H Hongze Cheng

refact

上级 40d58ce1
...@@ -22,7 +22,7 @@ extern "C" { ...@@ -22,7 +22,7 @@ extern "C" {
void taosRemoveDir(const char *dirname); void taosRemoveDir(const char *dirname);
bool taosDirExist(char *dirname); bool taosDirExist(char *dirname);
bool taosMkDir(char *dirname); bool taosMkDir(const char *dirname);
void taosRemoveOldFiles(char *dirname, int32_t keepDays); void taosRemoveOldFiles(char *dirname, int32_t keepDays);
bool taosExpandDir(char *dirname, char *outname, int32_t maxlen); bool taosExpandDir(char *dirname, char *outname, int32_t maxlen);
bool taosRealPath(char *dirname, int32_t maxlen); bool taosRealPath(char *dirname, int32_t maxlen);
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_META_DB_H_
#define _TD_META_DB_H_
#include "meta.h"
#include "tkv.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct SMetaDB {
STkvDb * pDB;
STkvDb * pIdx;
STkvCache *pCache;
} SMetaDB;
int metaOpenDB(SMeta *pMeta);
void metaCloseDB(SMeta *pMeta);
#ifdef __cplusplus
}
#endif
#endif /*_TD_META_DB_H_*/
\ No newline at end of file
...@@ -16,22 +16,18 @@ ...@@ -16,22 +16,18 @@
#ifndef _TD_META_DEF_H_ #ifndef _TD_META_DEF_H_
#define _TD_META_DEF_H_ #define _TD_META_DEF_H_
#include "metaDB.h"
#include "metaUid.h" #include "metaUid.h"
#include "tkv.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct SMeta { struct SMeta {
STableUidGenerator uidGenerator; char* path; // path of current meta
STbUidGenerator uidGenerator; // meta table UID generator
STkvDb* tableDb; // uid->table obj SMetaDB* pMetaDB; // meta DB for real storage engine
STkvDb* tbnameDb; // tbname --> uid SMetaOptions options; // meta option
STkvDb* schemaDb; // uid+version --> schema
STkvDb* tagDb; // uid --> tag
STkvDb* tagIdx; // TODO: need to integrate lucene or our own
// STkvCache* metaCache; // TODO: add a global cache here
}; };
#ifdef __cplusplus #ifdef __cplusplus
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_META_OPTIONS_H_
#define _TD_META_OPTIONS_H_
#include "meta.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const SMetaOptions defaultMetaOptions;
int metaValidateOptions(const SMetaOptions *);
void metaOptionsCopy(SMetaOptions *pDest, const SMetaOptions *pSrc);
#ifdef __cplusplus
}
#endif
#endif /*_TD_META_OPTIONS_H_*/
\ No newline at end of file
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_META_TABLE_OPTIONS_H_
#define _TD_META_TABLE_OPTIONS_H_
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /*_TD_META_TABLE_OPTIONS_H_*/
\ No newline at end of file
...@@ -23,21 +23,18 @@ extern "C" { ...@@ -23,21 +23,18 @@ extern "C" {
#endif #endif
/* ------------------------ APIS EXPOSED ------------------------ */ /* ------------------------ APIS EXPOSED ------------------------ */
typedef struct STableUidGenerator STableUidGenerator; typedef struct STbUidGenerator {
tb_uid_t nextUid;
} STbUidGenerator;
// tb_uid_t // tb_uid_t
#define IVLD_TB_UID 0 #define IVLD_TB_UID 0
tb_uid_t generateUid(STableUidGenerator *); tb_uid_t generateUid(STbUidGenerator *);
// STableUidGenerator // STableUidGenerator
void tableUidGeneratorInit(STableUidGenerator *, tb_uid_t suid); void tableUidGeneratorInit(STbUidGenerator *, tb_uid_t suid);
#define tableUidGeneratorClear(ug) #define tableUidGeneratorClear(ug)
/* ------------------------ FOR TEST AND COMPILE ONLY ------------------------ */
struct STableUidGenerator {
tb_uid_t nextUid;
};
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
\ No newline at end of file
...@@ -13,74 +13,62 @@ ...@@ -13,74 +13,62 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "tcoding.h"
#include "meta.h" #include "meta.h"
#include "metaDef.h" #include "metaDef.h"
#include "tcoding.h" #include "metaOptions.h"
#include "metaDB.h"
static int metaCreateSuperTable(SMeta *pMeta, const char *tbname, const SSuperTableOpts *pSuperTableOpts); static SMeta *metaNew(const char *path, const SMetaOptions *pMetaOptions);
static int metaCreateChildTable(SMeta *pMeta, const char *tbname, const SChildTableOpts *pChildTableOpts); static void metaFree(SMeta *pMeta);
static int metaCreateNormalTable(SMeta *pMeta, const char *tbname, const SNormalTableOpts *pNormalTableOpts); static int metaCreateSuperTable(SMeta *pMeta, const char *tbname, const SSuperTableOpts *pSuperTableOpts);
static int metaCreateChildTable(SMeta *pMeta, const char *tbname, const SChildTableOpts *pChildTableOpts);
static int metaCreateNormalTable(SMeta *pMeta, const char *tbname, const SNormalTableOpts *pNormalTableOpts);
SMeta *metaOpen(const char *path, const SMetaOptions *pMetaOpts) { SMeta *metaOpen(const char *path, const SMetaOptions *pMetaOptions) {
SMeta *pMeta = NULL; SMeta *pMeta = NULL;
pMeta = (SMeta *)calloc(1, sizeof(*pMeta)); // Set default options
if (pMetaOptions == NULL) {
pMetaOptions = &defaultMetaOptions;
}
// Validate the options
if (metaValidateOptions(pMetaOptions) < 0) {
// TODO: deal with error
return NULL;
}
// Allocate handle
pMeta = metaNew(path, pMetaOptions);
if (pMeta == NULL) { if (pMeta == NULL) {
// TODO: handle error
return NULL;
}
// Create META path
taosMkDir(path);
// Open the DBs needed
if (metaOpenDB(pMeta) < 0) {
// TODO: handle error
metaFree(pMeta);
return NULL; return NULL;
} }
// TODO: check if file exists and handle the error
taosMkDir("meta");
// Open tableDb
STkvOpts *tableDbOpts = tkvOptsCreate();
tkvOptsSetCreateIfMissing(tableDbOpts, 1);
pMeta->tableDb = tkvOpen(tableDbOpts, "meta/table_db");
tkvOptsDestroy(tableDbOpts);
// Open tbnameDb
STkvOpts *tbnameDbOpts = tkvOptsCreate();
tkvOptsSetCreateIfMissing(tbnameDbOpts, 1);
pMeta->tbnameDb = tkvOpen(tbnameDbOpts, "meta/tbname_db");
tkvOptsDestroy(tbnameDbOpts);
// Open schemaDb
STkvOpts *schemaDbOpts = tkvOptsCreate();
tkvOptsSetCreateIfMissing(schemaDbOpts, 1);
pMeta->schemaDb = tkvOpen(schemaDbOpts, "meta/schema_db");
tkvOptsDestroy(schemaDbOpts);
// Open tagDb
STkvOpts *tagDbOpts = tkvOptsCreate();
tkvOptsSetCreateIfMissing(tagDbOpts, 1);
pMeta->tagDb = tkvOpen(tagDbOpts, "meta/tag_db");
tkvOptsDestroy(tagDbOpts);
// Open tagIdx
STkvOpts *tagIdxDbOpts = tkvOptsCreate();
tkvOptsSetCreateIfMissing(tagIdxDbOpts, 1);
pMeta->tagIdx = tkvOpen(tagIdxDbOpts, "meta/tag_idx_db");
tkvOptsDestroy(tagIdxDbOpts);
// TODO: need to figure out how to persist the START UID
tableUidGeneratorInit(&(pMeta->uidGenerator), IVLD_TB_UID);
return pMeta; return pMeta;
} }
void metaClose(SMeta *pMeta) { void metaClose(SMeta *pMeta) {
if (pMeta) { if (pMeta) {
tableUidGeneratorClear(&pMeta->uidGenerator); tableUidGeneratorClear(&pMeta->uidGenerator);
metaCloseDB(pMeta);
tkvClose(pMeta->tagIdx);
tkvClose(pMeta->tagDb);
tkvClose(pMeta->schemaDb);
tkvClose(pMeta->tbnameDb);
tkvClose(pMeta->tableDb);
free(pMeta); free(pMeta);
} }
} }
#if 0
int metaCreateTable(SMeta *pMeta, const STableOptions *pTableOpts) { int metaCreateTable(SMeta *pMeta, const STableOptions *pTableOpts) {
size_t vallen; size_t vallen;
char * pUid; char * pUid;
...@@ -106,8 +94,37 @@ int metaCreateTable(SMeta *pMeta, const STableOptions *pTableOpts) { ...@@ -106,8 +94,37 @@ int metaCreateTable(SMeta *pMeta, const STableOptions *pTableOpts) {
return 0; return 0;
} }
#endif
/* ------------------------ STATIC METHODS ------------------------ */ /* ------------------------ STATIC METHODS ------------------------ */
static SMeta *metaNew(const char *path, const SMetaOptions *pMetaOptions) {
SMeta *pMeta;
size_t psize = strlen(path);
pMeta = (SMeta *)calloc(1, sizeof(*pMeta));
if (pMeta == NULL) {
return NULL;
}
pMeta->path = strdup(path);
if (pMeta->path == NULL) {
return NULL;
}
metaOptionsCopy(&(pMeta->options), pMetaOptions);
return pMeta;
};
static void metaFree(SMeta *pMeta) {
if (pMeta) {
tfree(pMeta->path);
free(pMeta);
}
}
// OLD -------------------------------------------------------------------
#if 0
static int metaCreateSuperTable(SMeta *pMeta, const char *tbname, const SSuperTableOpts *pSuperTableOpts) { static int metaCreateSuperTable(SMeta *pMeta, const char *tbname, const SSuperTableOpts *pSuperTableOpts) {
size_t vallen; size_t vallen;
size_t keylen; size_t keylen;
...@@ -258,3 +275,5 @@ void metaTableOptsClear(STableOptions *pTableOpts) { ...@@ -258,3 +275,5 @@ void metaTableOptsClear(STableOptions *pTableOpts) {
} }
void metaDestroy(const char *path) { taosRemoveDir(path); } void metaDestroy(const char *path) { taosRemoveDir(path); }
#endif
\ No newline at end of file
...@@ -26,5 +26,11 @@ void metaOptionsClear(SMetaOptions *pMetaOptions) { ...@@ -26,5 +26,11 @@ void metaOptionsClear(SMetaOptions *pMetaOptions) {
// TODO // TODO
} }
/* ------------------------ STATIC METHODS ------------------------ */ int metaValidateOptions(const SMetaOptions *pMetaOptions) {
static void metaOptionsCopy(SMetaOptions *pDest, const SMetaOptions *pSrc) { memcpy(pDest, pSrc, sizeof(*pSrc)); } // TODO
\ No newline at end of file return 0;
}
void metaOptionsCopy(SMetaOptions *pDest, const SMetaOptions *pSrc) { memcpy(pDest, pSrc, sizeof(*pSrc)); }
/* ------------------------ STATIC METHODS ------------------------ */
\ No newline at end of file
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
\ No newline at end of file
...@@ -15,12 +15,12 @@ ...@@ -15,12 +15,12 @@
#include "metaUid.h" #include "metaUid.h"
tb_uid_t generateUid(STableUidGenerator *pGen) { tb_uid_t generateUid(STbUidGenerator *pGen) {
// Generate a new table UID // Generate a new table UID
return ++(pGen->nextUid); return ++(pGen->nextUid);
} }
void tableUidGeneratorInit(STableUidGenerator *pGen, tb_uid_t suid) { void tableUidGeneratorInit(STbUidGenerator *pGen, tb_uid_t suid) {
// Init a generator // Init a generator
pGen->nextUid = suid; pGen->nextUid = suid;
} }
\ No newline at end of file
add_executable(metaTest "") # add_executable(metaTest "")
target_sources(metaTest # target_sources(metaTest
PRIVATE # PRIVATE
"../src/metaMain.c" # "../src/metaMain.c"
"../src/metaUid.c" # "../src/metaUid.c"
"metaTests.cpp" # "metaTests.cpp"
) # )
target_include_directories(metaTest # target_include_directories(metaTest
PUBLIC # PUBLIC
"${CMAKE_SOURCE_DIR}/include/server/vnode/meta" # "${CMAKE_SOURCE_DIR}/include/server/vnode/meta"
"${CMAKE_CURRENT_SOURCE_DIR}/../inc" # "${CMAKE_CURRENT_SOURCE_DIR}/../inc"
) # )
target_link_libraries(metaTest # target_link_libraries(metaTest
os # os
util # util
common # common
gtest_main # gtest_main
tkv # tkv
) # )
enable_testing() # enable_testing()
add_test( # add_test(
NAME meta_test # NAME meta_test
COMMAND metaTest # COMMAND metaTest
) # )
\ No newline at end of file \ No newline at end of file
...@@ -60,7 +60,7 @@ void taosRemoveDir(const char *dirname) { ...@@ -60,7 +60,7 @@ void taosRemoveDir(const char *dirname) {
bool taosDirExist(char *dirname) { return access(dirname, F_OK) == 0; } bool taosDirExist(char *dirname) { return access(dirname, F_OK) == 0; }
bool taosMkDir(char *dirname) { bool taosMkDir(const char *dirname) {
int32_t code = mkdir(dirname, 0755); int32_t code = mkdir(dirname, 0755);
if (code < 0 && errno == EEXIST) { if (code < 0 && errno == EEXIST) {
return true; return true;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册