From 3989a061c69c5bd3eb49c60c0d7120f29e4281d3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 11:40:44 +0000 Subject: [PATCH] more TDB --- source/dnode/vnode/CMakeLists.txt | 2 +- source/libs/tdb/inc/tdb.h | 9 ++++++-- source/libs/tdb/src/db/pgfile.c | 2 +- source/libs/tdb/src/db/tdb.c | 34 +++++++++++++++++++++++++------ source/libs/tdb/src/db/tdbEnv.c | 19 ++++++++++++++++- source/libs/tdb/src/inc/pgfile.h | 2 +- source/libs/tdb/src/inc/tdbEnv.h | 3 +++ 7 files changed, 59 insertions(+), 12 deletions(-) diff --git a/source/dnode/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt index bd633fa70a..429bd2143f 100644 --- a/source/dnode/vnode/CMakeLists.txt +++ b/source/dnode/vnode/CMakeLists.txt @@ -1,5 +1,5 @@ set(META_DB_IMPL_LIST "BDB" "TDB") -set(META_DB_IMPL "BDB" CACHE STRING "Use BDB as the default META implementation") +set(META_DB_IMPL "TDB" CACHE STRING "Use BDB as the default META implementation") set_property(CACHE META_DB_IMPL PROPERTY STRINGS ${META_DB_IMPL_LIST}) if(META_DB_IMPL IN_LIST META_DB_IMPL_LIST) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 7784282a24..63da7999c9 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -22,16 +22,21 @@ extern "C" { #endif -typedef struct STDb TDB; -typedef struct STDbEnv TENV; +typedef struct STDb TDB; +typedef struct STDbEnv TENV; +typedef struct STDbCurosr TDBC; // TEVN +int tdbEnvOpen(TENV **ppEnv); +int tdbEnvClose(TENV *pEnv); // TDB int tdbCreate(TDB **ppDb); int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv); int tdbClose(TDB *pDb); +// TDBC + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index f7d4eef799..1c3e2700b4 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -17,7 +17,7 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); -int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile) { +int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache) { SPgFile *pPgFile; *ppPgFile = NULL; diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index b5b248eac3..ffaef0addd 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -42,8 +42,11 @@ static int tdbDestroy(TDB *pDb) { } int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { - TDB *pDb; - int ret; + TDB * pDb; + int ret; + uint8_t fileid[TDB_FILE_ID_LEN]; + SPgFile * pPgFile; + SPgCache *pPgCache; // Create DB if DB handle is not created yet if (ppDb == NULL) { @@ -56,12 +59,31 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { // Create a default ENV if pEnv is not set if (pEnv == NULL) { - // if ((ret = tenvOpen(&pEnv)) != 0) { - // return -1; - // } + if ((ret = tdbEnvOpen(&pEnv)) != 0) { + return -1; + } } - /* TODO */ + pDb->pEnv = pEnv; + + // register DB to ENV + + ASSERT(fname != NULL); + + // Check if file exists (TODO) + + // Check if the SPgFile already opened + pPgFile = tdbEnvGetPageFile(pEnv, fileid); + if (pPgFile == NULL) { + pPgCache = tdbEnvGetPgCache(pEnv); + if ((ret = pgFileOpen(&pPgFile, fname, pPgCache)) != 0) { + return -1; + } + } + + pDb->pPgFile = pPgFile; + + // open the access method (TODO) return 0; } diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index c738b691c8..302c238927 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -21,4 +21,21 @@ struct STDbEnv { struct { } pgfht; // page file hash table; SPgCache pgc; // page cache -}; \ No newline at end of file +}; + +int tdbEnvOpen(TENV **ppEnv) { + // TODO + return 0; +} + +int tdbEnvClose(TENV *pEnv) { + // TODO + return 0; +} + +SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) { + // TODO + return NULL; +} + +SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return &(pEnv->pgc); } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index ad59fc711c..cb954ba946 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -30,7 +30,7 @@ struct SPgFile { pgno_t pgFileSize; }; -int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile); +int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache); int pgFileClose(SPgFile *pPgFile); SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno); diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index 2bd93fc530..a68ae0c7e9 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -20,6 +20,9 @@ extern "C" { #endif +SPgFile* tdbEnvGetPageFile(TENV* pEnv, const uint8_t fileid[]); +SPgCache* tdbEnvGetPgCache(TENV* pEnv); + #ifdef __cplusplus } #endif