From 2c60c32ec48f35d65038a3963a48864e517ed2a3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 3 Nov 2021 16:13:18 +0800 Subject: [PATCH] refact --- source/dnode/vnode/meta/inc/metaDB.h | 8 ++++- source/dnode/vnode/meta/src/metaDB.c | 51 +++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/source/dnode/vnode/meta/inc/metaDB.h b/source/dnode/vnode/meta/inc/metaDB.h index 8d7482acbb..b1531d2fd7 100644 --- a/source/dnode/vnode/meta/inc/metaDB.h +++ b/source/dnode/vnode/meta/inc/metaDB.h @@ -24,7 +24,13 @@ extern "C" { #endif -typedef rocksdb_t meta_db_t; +typedef struct { + rocksdb_t *tbDb; // uid -> tb obj + rocksdb_t *nameDb; // name -> uid + rocksdb_t *tagDb; // uid -> tag + rocksdb_t *schemaDb; // uid+version -> schema + rocksdb_t *mapDb; // suid -> uid_list +} meta_db_t; int metaOpenDB(SMeta *pMeta); void metaCloseDB(SMeta *pMeta); diff --git a/source/dnode/vnode/meta/src/metaDB.c b/source/dnode/vnode/meta/src/metaDB.c index a8e63b6156..d7e1d39c61 100644 --- a/source/dnode/vnode/meta/src/metaDB.c +++ b/source/dnode/vnode/meta/src/metaDB.c @@ -13,11 +13,21 @@ * along with this program. If not, see . */ -#include "meta.h" #include "metaDef.h" +#define META_OPEN_DB_IMPL(pDB, options, dir, err) \ + do { \ + pDB = rocksdb_open(options, dir, &err); \ + if (pDB == NULL) { \ + metaCloseDB(pMeta); \ + rocksdb_options_destroy(options); \ + return -1; \ + } \ + } while (0) + int metaOpenDB(SMeta *pMeta) { char dbDir[128]; + char dir[128]; char * err = NULL; rocksdb_options_t *options = rocksdb_options_create(); @@ -29,21 +39,52 @@ int metaOpenDB(SMeta *pMeta) { } rocksdb_options_set_create_if_missing(options, 1); - pMeta->pDB = rocksdb_open(options, dbDir, &err); + pMeta->pDB = (meta_db_t *)calloc(1, sizeof(*(pMeta->pDB))); if (pMeta->pDB == NULL) { // TODO: handle error - rocksdb_options_destroy(options); return -1; } - rocksdb_options_destroy(options); + // tbDb + sprintf(dir, "%s/tb_db", dbDir); + META_OPEN_DB_IMPL(pMeta->pDB->tbDb, options, dir, err); + // nameDb + sprintf(dir, "%s/name_db", dbDir); + META_OPEN_DB_IMPL(pMeta->pDB->nameDb, options, dir, err); + + // tagDb + sprintf(dir, "%s/tag_db", dbDir); + META_OPEN_DB_IMPL(pMeta->pDB->tagDb, options, dir, err); + + // schemaDb + sprintf(dir, "%s/schema_db", dbDir); + META_OPEN_DB_IMPL(pMeta->pDB->schemaDb, options, dir, err); + + // mapDb + sprintf(dir, "%s/map_db", dbDir); + META_OPEN_DB_IMPL(pMeta->pDB->mapDb, options, dir, err); + + rocksdb_options_destroy(options); return 0; } +#define META_CLOSE_DB_IMPL(pDB) \ + do { \ + if (pDB) { \ + rocksdb_close(pDB); \ + pDB = NULL; \ + } \ + } while (0) + void metaCloseDB(SMeta *pMeta) { if (pMeta->pDB) { - rocksdb_close(pMeta->pDB); + META_CLOSE_DB_IMPL(pMeta->pDB->mapDb); + META_CLOSE_DB_IMPL(pMeta->pDB->schemaDb); + META_CLOSE_DB_IMPL(pMeta->pDB->tagDb); + META_CLOSE_DB_IMPL(pMeta->pDB->nameDb); + META_CLOSE_DB_IMPL(pMeta->pDB->tbDb); + free(pMeta->pDB); pMeta->pDB = NULL; } }