refact
This commit is contained in:
parent
770b1bb1c0
commit
2c60c32ec4
|
@ -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);
|
||||
|
|
|
@ -13,11 +13,21 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue