提交 35e5120c 编写于 作者: H Hongze Cheng

more

上级 b80da12d
......@@ -22,7 +22,7 @@ option(
option(
BUILD_WITH_SQLITE
"If build with sqlite"
ON
OFF
)
option(
......
......@@ -20,126 +20,201 @@
#include "tcoding.h"
#include "thash.h"
typedef struct {
tb_uid_t uid;
int32_t sver;
} SSchemaKey;
struct SMetaDB {
// DB
DB * pStbDB;
DB * pNtbDB;
SHashObj *pCtbMap;
DB * pSchemaDB;
DB *pTbDB;
DB *pSchemaDB;
// IDX
SHashObj *pIdxMap;
DB * pNameIdx;
DB * pUidIdx;
DB *pNameIdx;
DB *pStbIdx;
DB *pNtbIdx;
DB *pCtbIdx;
// 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);
static int metaCreateDBEnv(SMetaDB *pDB, const char *path);
static void metaDestroyDBEnv(SMetaDB *pDB);
static int metaEncodeSchemaKey(void **buf, SSchemaKey *pSchemaKey);
static void * metaDecodeSchemaKey(void *buf, SSchemaKey *pSchemaKey);
static int metaNameIdxCb(DB *sdbp, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int metaUidIdxCb(DB *sdbp, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static void metaPutSchema(SMeta *pMeta, tb_uid_t uid, STSchema *pSchema);
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)
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 META_CLOSE_DB(pDB)
#define META_ASSOCIATE_IDX(pDB, pIdx, cbf) \
do { \
int ret = (pDB)->associate((pDB), NULL, (pIdx), (cbf), 0); \
if (ret != 0) { \
P_ERROR("Failed to associate META DB", ret); \
metaCloseDB(pMeta); \
} \
} while (0)
#define BDB_PERR(info, code) fprintf(stderr, info " reason: %s", db_strerror(code))
#define metaOpenBDBIdx metaOpenBDBDb
int metaOpenDB(SMeta *pMeta) {
int ret;
SMetaDB *pDB;
pMeta->pDB = metaNewDB();
if (pMeta->pDB == NULL) {
// Create DB object
pDB = metaNewDB();
if (pDB == NULL) {
return -1;
}
pDB = pMeta->pDB;
pMeta->pDB = pDB;
if (metaCreateDBEnv(pDB, pMeta->path) < 0) {
// Open DB Env
if (metaOpenBDBEnv(&(pDB->pEvn), 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
// Open DBs
if (metaOpenBDBDb(&(pDB->pTbDB), pDB->pEvn, "meta.db") < 0) {
metaCloseDB(pMeta);
return -1;
}
META_OPEN_DB(pDB->pNameIdx, pDB->pEvn, "index.db");
if (metaOpenBDBDb(&(pDB->pSchemaDB), pDB->pEvn, "meta.db") < 0) {
metaCloseDB(pMeta);
return -1;
}
// META_OPEN_DB(pDB->pUidIdx, pDB->pEvn, "index.db");
// Open Indices
if (metaOpenBDBIdx(&(pDB->pNameIdx), pDB->pEvn, "index.db") < 0) {
metaCloseDB(pMeta);
return -1;
}
// 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);
if (metaOpenBDBIdx(&(pDB->pStbIdx), pDB->pEvn, "index.db") < 0) {
metaCloseDB(pMeta);
return -1;
}
for (;;) {
// Loop to associate each super table db
break;
if (metaOpenBDBIdx(&(pDB->pNtbIdx), pDB->pEvn, "index.db") < 0) {
metaCloseDB(pMeta);
return -1;
}
{
// TODO: Loop to open index DB for each super table
// and create the association between main DB and index
if (metaOpenBDBIdx(&(pDB->pCtbIdx), pDB->pEvn, "index.db") < 0) {
metaCloseDB(pMeta);
return -1;
}
// Associate Indices
return 0;
}
void metaCloseDB(SMeta *pMeta) {
if (pMeta->pDB) {
metaDestroyDBEnv(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;
static SMetaDB *metaNewDB();
static void metaFreeDB(SMetaDB *pDB);
static int metaCreateDBEnv(SMetaDB *pDB, const char *path);
static void metaDestroyDBEnv(SMetaDB *pDB);
static int metaEncodeSchemaKey(void **buf, SSchemaKey *pSchemaKey);
static void * metaDecodeSchemaKey(void *buf, SSchemaKey *pSchemaKey);
static int metaNameIdxCb(DB *sdbp, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int metaUidIdxCb(DB *sdbp, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static void metaPutSchema(SMeta *pMeta, tb_uid_t uid, STSchema *pSchema);
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_ASSOCIATE_IDX(pDB, pIdx, cbf) \
do { \
int ret = (pDB)->associate((pDB), NULL, (pIdx), (cbf), 0); \
if (ret != 0) { \
P_ERROR("Failed to associate META DB", ret); \
metaCloseDB(pMeta); \
} \
} while (0)
int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) {
char buf[512];
void * pBuf;
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册