diff --git a/cmake/cmake.options b/cmake/cmake.options index b3f8ea7bbd..edaab3bd45 100644 --- a/cmake/cmake.options +++ b/cmake/cmake.options @@ -22,7 +22,7 @@ option( option( BUILD_WITH_SQLITE "If build with sqlite" - ON + OFF ) option( diff --git a/source/dnode/vnode/meta/src/metaBDBImpl.c b/source/dnode/vnode/meta/src/metaBDBImpl.c index 026bcfdafb..7bbe7ddc71 100644 --- a/source/dnode/vnode/meta/src/metaBDBImpl.c +++ b/source/dnode/vnode/meta/src/metaBDBImpl.c @@ -20,26 +20,177 @@ #include "tcoding.h" #include "thash.h" +struct SMetaDB { + // DB + DB *pTbDB; + DB *pSchemaDB; + // IDX + DB *pNameIdx; + DB *pStbIdx; + DB *pNtbIdx; + DB *pCtbIdx; + // ENV + DB_ENV *pEvn; +}; + +static SMetaDB *metaNewDB(); +static void metaFreeDB(SMetaDB *pDB); +static int metaOpenBDBEnv(DB_ENV **ppEnv, const char *path); +static void metaCloseBDBEnv(DB_ENV *pEnv); +static int metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName); +static void metaCloseBDBDb(DB *pDB); + +#define BDB_PERR(info, code) fprintf(stderr, info " reason: %s", db_strerror(code)) +#define metaOpenBDBIdx metaOpenBDBDb + +int metaOpenDB(SMeta *pMeta) { + SMetaDB *pDB; + + // Create DB object + pDB = metaNewDB(); + if (pDB == NULL) { + return -1; + } + + pMeta->pDB = pDB; + + // Open DB Env + if (metaOpenBDBEnv(&(pDB->pEvn), pMeta->path) < 0) { + metaCloseDB(pMeta); + return -1; + } + + // Open DBs + if (metaOpenBDBDb(&(pDB->pTbDB), pDB->pEvn, "meta.db") < 0) { + metaCloseDB(pMeta); + return -1; + } + + if (metaOpenBDBDb(&(pDB->pSchemaDB), pDB->pEvn, "meta.db") < 0) { + metaCloseDB(pMeta); + return -1; + } + + // Open Indices + if (metaOpenBDBIdx(&(pDB->pNameIdx), pDB->pEvn, "index.db") < 0) { + metaCloseDB(pMeta); + return -1; + } + + if (metaOpenBDBIdx(&(pDB->pStbIdx), pDB->pEvn, "index.db") < 0) { + metaCloseDB(pMeta); + return -1; + } + + if (metaOpenBDBIdx(&(pDB->pNtbIdx), pDB->pEvn, "index.db") < 0) { + metaCloseDB(pMeta); + return -1; + } + + if (metaOpenBDBIdx(&(pDB->pCtbIdx), pDB->pEvn, "index.db") < 0) { + metaCloseDB(pMeta); + return -1; + } + // Associate Indices + + return 0; +} + +void metaCloseDB(SMeta *pMeta) { + if (pMeta->pDB) { + metaCloseBDBEnv(pMeta->pDB->pEvn); + metaFreeDB(pMeta->pDB); + pMeta->pDB = NULL; + } +} + +int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { + // TODO + return 0; +} + +int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) { + // TODO + return 0; +} + +/* ------------------------ STATIC METHODS ------------------------ */ +static SMetaDB *metaNewDB() { + SMetaDB *pDB = NULL; + pDB = (SMetaDB *)calloc(1, sizeof(*pDB)); + if (pDB == NULL) { + return NULL; + } + + return pDB; +} + +static void metaFreeDB(SMetaDB *pDB) { + if (pDB) { + free(pDB); + } +} + +static int metaOpenBDBEnv(DB_ENV **ppEnv, const char *path) { + int ret; + DB_ENV *pEnv; + + if (path == NULL) return 0; + + ret = db_env_create(&pEnv, 0); + if (ret != 0) { + BDB_PERR("Failed to create META env", ret); + return -1; + } + + ret = pEnv->open(pEnv, path, DB_CREATE | DB_INIT_MPOOL, 0); + if (ret != 0) { + BDB_PERR("Failed to open META env", ret); + return -1; + } + + *ppEnv = pEnv; + + return 0; +} + +static void metaCloseBDBEnv(DB_ENV *pEnv) { + if (pEnv) { + pEnv->close(pEnv, 0); + } +} + +static int metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName) { + int ret; + DB *pDB; + + ret = db_create(&((pDB)), (pEnv), 0); + if (ret != 0) { + BDB_PERR("Failed to create META DB", ret); + return -1; + } + + ret = pDB->open(pDB, NULL, pFName, NULL, DB_BTREE, DB_CREATE, 0); + if (ret) { + BDB_PERR("Failed to open META DB", ret); + return -1; + } + + return 0; +} + +static void metaCloseBDBDb(DB *pDB) { + if (pDB) { + pDB->close(pDB, 0); + } +} + +#if 0 typedef struct { tb_uid_t uid; int32_t sver; } SSchemaKey; -struct SMetaDB { - // DB - DB * pStbDB; - DB * pNtbDB; - SHashObj *pCtbMap; - DB * pSchemaDB; - // IDX - SHashObj *pIdxMap; - DB * pNameIdx; - DB * pUidIdx; - // ENV - DB_ENV *pEvn; -}; - -#define P_ERROR(info, code) fprintf(stderr, info "reason: %s", db_strerror(code)) static SMetaDB *metaNewDB(); static void metaFreeDB(SMetaDB *pDB); @@ -54,26 +205,6 @@ static int metaEncodeTbInfo(void **buf, STbCfg *pTbCfg); static void * metaDecodeTbInfo(void *buf, STbCfg *pTbCfg); static int metaSaveTbInfo(DB *pDB, tb_uid_t uid, STbCfg *pTbCfg); -#define META_OPEN_DB(pDB, pEnv, fName) \ - do { \ - int ret; \ - ret = db_create(&((pDB)), (pEnv), 0); \ - if (ret != 0) { \ - P_ERROR("Failed to create META DB", ret); \ - metaCloseDB(pMeta); \ - return -1; \ - } \ - \ - ret = (pDB)->open((pDB), NULL, (fName), NULL, DB_BTREE, DB_CREATE, 0); \ - if (ret != 0) { \ - P_ERROR("Failed to open META DB", ret); \ - metaCloseDB(pMeta); \ - return -1; \ - } \ - } while (0) - -#define META_CLOSE_DB(pDB) - #define META_ASSOCIATE_IDX(pDB, pIdx, cbf) \ do { \ int ret = (pDB)->associate((pDB), NULL, (pIdx), (cbf), 0); \ @@ -83,62 +214,6 @@ static int metaSaveTbInfo(DB *pDB, tb_uid_t uid, STbCfg *pTbCfg); } \ } while (0) -int metaOpenDB(SMeta *pMeta) { - int ret; - SMetaDB *pDB; - - pMeta->pDB = metaNewDB(); - if (pMeta->pDB == NULL) { - return -1; - } - - pDB = pMeta->pDB; - - if (metaCreateDBEnv(pDB, pMeta->path) < 0) { - metaCloseDB(pMeta); - return -1; - } - - META_OPEN_DB(pDB->pStbDB, pDB->pEvn, "meta.db"); - - META_OPEN_DB(pDB->pNtbDB, pDB->pEvn, "meta.db"); - - META_OPEN_DB(pDB->pSchemaDB, pDB->pEvn, "meta.db"); - - { - // TODO: Loop to open each super table db - } - - META_OPEN_DB(pDB->pNameIdx, pDB->pEvn, "index.db"); - - // META_OPEN_DB(pDB->pUidIdx, pDB->pEvn, "index.db"); - - // Associate name index - META_ASSOCIATE_IDX(pDB->pStbDB, pDB->pNameIdx, metaNameIdxCb); - // META_ASSOCIATE_IDX(pDB->pStbDB, pDB->pUidIdx, metaUidIdxCb); - // META_ASSOCIATE_IDX(pDB->pNtbDB, pDB->pNameIdx, metaNameIdxCb); - // META_ASSOCIATE_IDX(pDB->pNtbDB, pDB->pUidIdx, metaUidIdxCb); - - for (;;) { - // Loop to associate each super table db - break; - } - - { - // TODO: Loop to open index DB for each super table - // and create the association between main DB and index - } - - return 0; -} - -void metaCloseDB(SMeta *pMeta) { - if (pMeta->pDB) { - metaDestroyDBEnv(pMeta->pDB); - metaFreeDB(pMeta->pDB); - pMeta->pDB = NULL; - } -} int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { char buf[512]; @@ -199,68 +274,6 @@ int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) { } /* ------------------------ STATIC METHODS ------------------------ */ -static SMetaDB *metaNewDB() { - SMetaDB *pDB; - pDB = (SMetaDB *)calloc(1, sizeof(*pDB)); - if (pDB == NULL) { - return NULL; - } - - pDB->pCtbMap = taosHashInit(0, MurmurHash3_32, false, HASH_NO_LOCK); - if (pDB->pCtbMap == NULL) { - metaFreeDB(pDB); - return NULL; - } - - pDB->pIdxMap = taosHashInit(0, MurmurHash3_32, false, HASH_NO_LOCK); - if (pDB->pIdxMap == NULL) { - metaFreeDB(pDB); - return NULL; - } - - return pDB; -} - -static void metaFreeDB(SMetaDB *pDB) { - if (pDB == NULL) { - if (pDB->pIdxMap) { - taosHashCleanup(pDB->pIdxMap); - } - - if (pDB->pCtbMap) { - taosHashCleanup(pDB->pCtbMap); - } - - free(pDB); - } -} - -static int metaCreateDBEnv(SMetaDB *pDB, const char *path) { - int ret; - - if (path == NULL) return 0; - - ret = db_env_create(&(pDB->pEvn), 0); - if (ret != 0) { - P_ERROR("Failed to create META DB ENV", ret); - return -1; - } - - ret = pDB->pEvn->open(pDB->pEvn, path, DB_CREATE | DB_INIT_MPOOL, 0); - if (ret != 0) { - P_ERROR("failed to open META DB ENV", ret); - return -1; - } - - return 0; -} - -static void metaDestroyDBEnv(SMetaDB *pDB) { - if (pDB->pEvn) { - pDB->pEvn->close(pDB->pEvn, 0); - } -} - static int metaEncodeSchemaKey(void **buf, SSchemaKey *pSchemaKey) { int tsize = 0; @@ -362,4 +375,5 @@ static int metaSaveTbInfo(DB *pDB, tb_uid_t uid, STbCfg *pTbCfg) { pDB->put(pDB, NULL, &key, &value, 0); return 0; -} \ No newline at end of file +} +#endif \ No newline at end of file