From f77d33dcb170d7eb956a48caf8e5a8a8b760dcf5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 09:44:55 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdb.c | 41 ++++++++++++++++++++++++----- source/libs/tdb/src/db/tdbEnv.c | 4 ++- source/libs/tdb/src/db/tdbPgFile.c | 5 ++-- source/libs/tdb/src/inc/tdbEnv.h | 5 ++-- source/libs/tdb/src/inc/tdbInt.h | 3 +++ source/libs/tdb/src/inc/tdbPgFile.h | 2 +- 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 3b7156fbd4..df25fe20b8 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -16,7 +16,7 @@ #include "tdbInt.h" struct STDb { - char * dbname; // dbname; + char dbname[TDB_MAX_DBNAME_LEN]; SBTree * pBt; // current access method (may extend) SPgFile * pPgFile; // backend page file this DB is using TENV * pEnv; // TENV containing the DB @@ -63,16 +63,44 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { SPgFile * pPgFile; SPgCache *pPgCache; SBTree * pBt; + bool fileExist; + size_t dbNameLen; + char dbfname[128]; // TODO: make this as a macro or malloc on the heap ASSERT(pDb != NULL); + ASSERT(fname != NULL); + // TODO: Here we simply put an assert here. In the future, make `pEnv` + // can be set as NULL. + ASSERT(pEnv != NULL); - // Create a default ENV if pEnv is not set - if (pEnv == NULL) { - // if ((ret = tdbEnvOpen(&pEnv)) != 0) { - // return -1; - // } + // check the DB name + dbNameLen = 0; + if (dbname) { + dbNameLen = strlen(dbname); + if (dbNameLen >= TDB_MAX_DBNAME_LEN) { + return -1; + } + + memcpy(pDb->dbname, dbname, dbNameLen); } + pDb->dbname[dbNameLen] = '\0'; + + // open pPgFile or get from the env + snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname); + fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0); + if (fileExist) { + // TODO + } else { + ret = pgFileOpen(&pPgFile, dbfname, pEnv); + if (ret != 0) { + // TODO: handle error + return -1; + } + // Create and open the page file + } + +#if 0 pDb->pEnv = pEnv; // register DB to ENV @@ -104,6 +132,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { } pDb->pBt = pBt; +#endif return 0; } diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 58612b2165..d88b816a34 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -124,4 +124,6 @@ int tdbEnvBeginTxn(TENV *pEnv) { int tdbEnvCommit(TENV *pEnv) { // TODO return 0; -} \ No newline at end of file +} + +const char *tdbEnvGetRootDir(TENV *pEnv) { return pEnv->rootDir; } diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index 576d6f3da4..07c036d623 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -17,8 +17,9 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); -int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache) { - SPgFile *pPgFile; +int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { + SPgFile * pPgFile; + SPgCache *pPgCache; *ppPgFile = NULL; diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index a68ae0c7e9..ef2eab87ef 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -20,8 +20,9 @@ extern "C" { #endif -SPgFile* tdbEnvGetPageFile(TENV* pEnv, const uint8_t fileid[]); -SPgCache* tdbEnvGetPgCache(TENV* pEnv); +const char* tdbEnvGetRootDir(TENV* pEnv); +SPgFile* tdbEnvGetPageFile(TENV* pEnv, const uint8_t fileid[]); +SPgCache* tdbEnvGetPgCache(TENV* pEnv); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 996d07b956..782206a4aa 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -76,6 +76,9 @@ typedef pgsz_t pgoff_t; // cache #define TDB_DEFAULT_CACHE_SIZE (256 * 4096) // 1M +// dbname +#define TDB_MAX_DBNAME_LEN 24 + // tdb_log #define tdbError(var) diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index 84d8319c61..afe3511fb0 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -42,7 +42,7 @@ struct SPgFile { TDB * pDb; // For a SPgFile for multiple databases, this is the mapping DB. }; -int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache); +int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); int pgFileClose(SPgFile *pPgFile); SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno);