From a466372179f897abb4e3bbe3546a4c06e2795582 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 31 Mar 2022 02:10:26 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 71 ++++++++++++++++++++++++++---- source/libs/tdb/src/inc/tdbEnv.h | 13 +++--- source/libs/tdb/src/inc/tdbPager.h | 2 + source/libs/tdb/src/inc/tdbUtil.h | 5 +++ 4 files changed, 75 insertions(+), 16 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index a981e8c99e..6e74aefc62 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -54,13 +54,13 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv) { return -1; } - pEnv->nHash = 8; - tsize = sizeof(SPager *) * pEnv->nHash; - pEnv->pagerHash = TDB_REALLOC(pEnv->pagerHash, tsize); - if (pEnv->pagerHash == NULL) { + pEnv->nPgrHash = 8; + tsize = sizeof(SPager *) * pEnv->nPgrHash; + pEnv->pgrHash = TDB_REALLOC(pEnv->pgrHash, tsize); + if (pEnv->pgrHash == NULL) { return -1; } - memset(pEnv->pagerHash, 0, tsize); + memset(pEnv->pgrHash, 0, tsize); mkdir(rootDir, 0755); @@ -81,7 +81,7 @@ int tdbBegin(TENV *pEnv) { int tdbCommit(TENV *pEnv) { SPager *pPager; - pPager = pEnv->pagerList; + pPager = pEnv->pgrList; while (pPager) { tdbPagerCommit(pPager); } @@ -95,10 +95,63 @@ int tdbRollback(TENV *pEnv) { } SPager *tdbEnvGetPager(TENV *pEnv, const char *fname) { - // TODO - return NULL; + int hash; + SPager **ppPager; + + hash = tdbCstringHash(fname); + ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash]; + for (; *ppPager && (strcmp(fname, (*ppPager)->dbFileName) != 0); ppPager = &((*ppPager)->pHashNext)) { + } + + return *ppPager; } -static void tdbEnvAddPager(TENV *pEnv, SPager *pPager) { +void tdbEnvAddPager(TENV *pEnv, SPager *pPager) { + int hash; + SPager **ppPager; + // rehash if neccessary + if (pEnv->nPager + 1 > pEnv->nPgrHash) { + // TODO + } + + // add to list + pPager->pNext = pEnv->pgrList; + pEnv->pgrList = pPager; + + // add to hash + hash = tdbCstringHash(pPager->dbFileName); + ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash]; + pPager->pHashNext = *ppPager; + *ppPager = pPager; + + // increase the counter + pEnv->nPager++; +} + +void tdbEnvRemovePager(TENV *pEnv, SPager *pPager) { + int hash; + SPager **ppPager; + + // remove from the list + for (ppPager = &pEnv->pgrList; *ppPager && (*ppPager != pPager); ppPager = &((*ppPager)->pNext)) { + } + ASSERT(*ppPager == pPager); + *ppPager = pPager->pNext; + + // remove from hash + hash = tdbCstringHash(pPager->dbFileName); + ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash]; + for (; *ppPager && *ppPager != pPager; ppPager = &((*ppPager)->pHashNext)) { + } + ASSERT(*ppPager == pPager); + *ppPager = pPager->pNext; + + // decrease the counter + pEnv->nPager--; + + // rehash if necessary + if (pEnv->nPgrHash > 8 && pEnv->nPager < pEnv->nPgrHash / 2) { + // TODO + } } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index 3eb3ebb328..e10c5d54e0 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -20,18 +20,15 @@ extern "C" { #endif -#define TDB_PAGER_ENV_FIELDS \ - SPager *pNext; \ - SPager *pHashNext; - typedef struct STEnv { char *rootDir; char *jfname; int jfd; SPCache *pCache; - SPager *pagerList; - int nHash; - SPager **pagerHash; + SPager *pgrList; + int nPager; + int nPgrHash; + SPager **pgrHash; } TENV; int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv); @@ -40,6 +37,8 @@ int tdbBegin(TENV *pEnv); int tdbCommit(TENV *pEnv); int tdbRollback(TENV *pEnv); +void tdbEnvAddPager(TENV *pEnv, SPager *pPager); +void tdbEnvRemovePager(TENV *pEnv, SPager *pPager); SPager *tdbEnvGetPager(TENV *pEnv, const char *fname); #ifdef __cplusplus diff --git a/source/libs/tdb/src/inc/tdbPager.h b/source/libs/tdb/src/inc/tdbPager.h index 7ae86eb3ac..81b6074431 100644 --- a/source/libs/tdb/src/inc/tdbPager.h +++ b/source/libs/tdb/src/inc/tdbPager.h @@ -32,6 +32,8 @@ struct SPager { SPgno dbOrigSize; SPage *pDirty; u8 inTran; + SPager *pNext; // used by TENV + SPager *pHashNext; // used by TENV }; int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager); diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index c06d9d18c9..a4cb09ea94 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -101,6 +101,11 @@ static inline int tdbGetVarInt(const u8 *p, int *v) { return n; } +static inline int tdbCstringHash(const char *s) { + // TODO + return 0; +} + #ifdef __cplusplus } #endif -- GitLab