From 2849a62ced26b6c2479452709c253541837340da Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 10 Feb 2022 06:36:24 +0000 Subject: [PATCH] more TDB --- source/dnode/vnode/src/meta/metaTDBImpl.c | 5 +++++ source/libs/tdb/inc/tdb.h | 8 ++++++++ source/libs/tdb/src/db/pgfile.c | 2 +- source/libs/tdb/src/db/tdb.c | 7 ++++++- source/libs/tdb/src/db/tdbEnv.c | 18 ++++++++++++++++++ source/libs/tdb/src/db/tdbUtil.c | 23 +++++++++++++++++++++-- source/libs/tdb/src/db/tdb_mpool.c | 2 +- source/libs/tdb/src/inc/tdbInt.h | 3 --- source/libs/tdb/src/inc/tdbUtil.h | 7 ++++++- 9 files changed, 66 insertions(+), 9 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 3a5b55925b..4a65cf277b 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -54,6 +54,11 @@ int metaOpenDB(SMeta *pMeta) { // Create and open the ENV A((tdbEnvCreate(&pEnv)), _err); +#if 0 + // Set options of the environment + A(tdbEnvSetPageSize(pEnv, 8192), _err); + A(tdbEnvSetCacheSize(pEnv, 16 * 1024 * 1024), _err); +#endif A((tdbEnvOpen(&pEnv)), _err); // Create and open each DB diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 5bbaf2ac88..9a168c5311 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -26,11 +26,19 @@ typedef struct STDb TDB; typedef struct STDbEnv TENV; typedef struct STDbCurosr TDBC; +typedef int32_t pgsize_t; +typedef int32_t cachesz_t; + // TEVN int tdbEnvCreate(TENV **ppEnv); int tdbEnvOpen(TENV **ppEnv); int tdbEnvClose(TENV *pEnv); +int tdbEnvSetPageSize(TENV *pEnv, pgsize_t szPage); +int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache); +pgsize_t tdbEnvGetPageSize(TENV *pEnv); +cachesz_t tdbEnvGetCacheSize(TENV *pEnv); + // TDB int tdbCreate(TDB **ppDb); int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv); diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 1c3e2700b4..7f0ab55b00 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -44,7 +44,7 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache) { return -1; } - if (tdbGnrtFileID(fname, pPgFile->fileid) < 0) { + if (tdbGnrtFileID(fname, pPgFile->fileid, false) < 0) { pgFileClose(pPgFile); return -1; } diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index ffaef0addd..bd8d7ec1f7 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -70,7 +70,12 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { ASSERT(fname != NULL); - // Check if file exists (TODO) + // Check if file exists + if (tdbCheckFileAccess(fname, TDB_F_OK) != 0) { + if (1) { + // create the file + } + } // Check if the SPgFile already opened pPgFile = tdbEnvGetPageFile(pEnv, fileid); diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 3d7e98f477..fe9238e1eb 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -57,6 +57,8 @@ int tdbEnvOpen(TENV **ppEnv) { pEnv = *ppEnv; TERR_A(ret, pgCacheCreate(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize), _err); + TERR_A(ret, pgCacheOpen(&pPgCache), _err); + pEnv->pPgCache = pPgCache; return 0; @@ -72,6 +74,22 @@ int tdbEnvClose(TENV *pEnv) { return 0; } +int tdbEnvSetPageSize(TENV *pEnv, pgsize_t szPage) { + /* TODO */ + pEnv->pgSize = szPage; + return 0; +} + +int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache) { + /* TODO */ + pEnv->cacheSize = szCache; + return 0; +} + +pgsize_t tdbEnvGetPageSize(TENV *pEnv) { return pEnv->pgSize; } + +cachesz_t tdbEnvGetCacheSize(TENV *pEnv) { return pEnv->cacheSize; } + SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) { // TODO return NULL; diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index 6391397efd..856a54a2da 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -15,7 +15,7 @@ #include "tdbInt.h" -int tdbGnrtFileID(const char *fname, uint8_t *fileid) { +int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) { struct stat statbuf; if (stat(fname, &statbuf) < 0) { @@ -26,8 +26,27 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid) { ((uint64_t *)fileid)[0] = (uint64_t)statbuf.st_ino; ((uint64_t *)fileid)[1] = (uint64_t)statbuf.st_dev; - ((uint64_t *)fileid)[2] = rand(); + if (unique) { + ((uint64_t *)fileid)[2] = rand(); + } return 0; } +int tdbCheckFileAccess(const char *pathname, int mode) { + int flags = 0; + + if (mode & TDB_F_OK) { + flags |= F_OK; + } + + if (mode & TDB_R_OK) { + flags |= R_OK; + } + + if (mode & TDB_W_OK) { + flags |= W_OK; + } + + return access(pathname, flags); +} \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c index 1b591a5c3f..cc7927d51b 100644 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ b/source/libs/tdb/src/db/tdb_mpool.c @@ -119,7 +119,7 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { goto _err; } - if (tdbGnrtFileID(fname, mpf->fileid) < 0) { + if (tdbGnrtFileID(fname, mpf->fileid, false) < 0) { goto _err; } diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 1500fee6dc..74fed019e7 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -16,7 +16,6 @@ #ifndef _TD_TDB_INTERNAL_H_ #define _TD_TDB_INTERNAL_H_ -#include "os.h" #include "tlist.h" #include "tlockfree.h" @@ -64,14 +63,12 @@ static FORCE_INLINE int tdbCmprPgId(const void *p1, const void *p2) { typedef int32_t frame_id_t; // pgsize_t -typedef int32_t pgsize_t; #define TDB_MIN_PGSIZE 512 #define TDB_MAX_PGSIZE 16384 #define TDB_DEFAULT_PGSIZE 4096 #define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) // cache -typedef int32_t cachesz_t; #define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K // tdb_log diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 6ffb9dd3d4..8e1fe013e8 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -22,7 +22,12 @@ extern "C" { #define TDB_ROUND8(x) (((x) + 7) & ~7) -int tdbGnrtFileID(const char *fname, uint8_t *fileid); +int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); + +#define TDB_F_OK 0x1 +#define TDB_R_OK 0x2 +#define TDB_W_OK 0x4 +int tdbCheckFileAccess(const char *pathname, int mode); #ifdef __cplusplus } -- GitLab