From 708fef9af0cfdd5c55fb7e34824634713d754a4c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 23 Feb 2022 03:32:56 +0000 Subject: [PATCH] more --- source/libs/tdb/src/db/tdb.c | 2 +- source/libs/tdb/src/db/tdbBtree.c | 14 +++---- source/libs/tdb/src/db/tdbPgCache.c | 56 +++++++++++++++++++++------- source/libs/tdb/src/db/tdbPgFile.c | 18 ++++----- source/libs/tdb/src/db/tdbUtil.c | 2 +- source/libs/tdb/src/inc/tdbBtree.h | 2 +- source/libs/tdb/src/inc/tdbInt.h | 6 +-- source/libs/tdb/src/inc/tdbPgCache.h | 16 ++++++-- source/libs/tdb/src/inc/tdbPgFile.h | 10 ++--- source/libs/tdb/src/inc/tdbUtil.h | 2 +- 10 files changed, 82 insertions(+), 46 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index cc3b7fa6b9..6021fba6ee 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -65,7 +65,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { SBTree * pBt; bool fileExist; size_t dbNameLen; - pgno_t dbRootPgno; + SPgno dbRootPgno; char dbfname[128]; // TODO: make this as a macro or malloc on the heap ASSERT(pDb != NULL); diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 86e7980733..d76155bbdf 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -17,12 +17,12 @@ struct SBtCursor { SBTree *pBtree; - pgno_t pgno; + SPgno pgno; SPage * pPage; // current page traversing }; typedef struct { - pgno_t pgno; + SPgno pgno; pgsz_t offset; } SBtIdx; @@ -34,7 +34,7 @@ typedef struct __attribute__((__packed__)) { pgoff_t freeOff; // free payload offset pgsz_t fragSize; // total fragment size pgoff_t offPayload; // payload offset - pgno_t rChildPgno; // right most child page number + SPgno rChildPgno; // right most child page number } SBtPgHdr; typedef int (*BtreeCmprFn)(const void *, const void *); @@ -45,7 +45,7 @@ typedef int (*BtreeCmprFn)(const void *, const void *); static int btreeCreate(SBTree **ppBt); static int btreeDestroy(SBTree *pBt); -static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno); +static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno); int btreeOpen(SBTree **ppBt, SPgFile *pPgFile) { SBTree *pBt; @@ -98,8 +98,8 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) { SPage * pPage; SBtPgHdr * pBtPgHdr; SPgFile * pPgFile; - pgno_t childPgno; - pgno_t rootPgno; + SPgno childPgno; + SPgno rootPgno; int nPayloads; void * pPayload; BtreeCmprFn cmpFn; @@ -157,7 +157,7 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) { return 0; } -static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno) { +static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno) { SPgFile *pPgFile; // TODO return 0; diff --git a/source/libs/tdb/src/db/tdbPgCache.c b/source/libs/tdb/src/db/tdbPgCache.c index 17a2fbfe0b..c7ec7b0cc5 100644 --- a/source/libs/tdb/src/db/tdbPgCache.c +++ b/source/libs/tdb/src/db/tdbPgCache.c @@ -24,20 +24,19 @@ struct SPCache { SPgHdr * lru; int nRecyclable; int nHash; - SPgHdr * pgHash; + SPgHdr ** pgHash; int nFree; SPgHdr * pFree; }; -struct SPgHdr { - void * pData; - SPgid pgid; - SPgHdr *pFreeNext; -}; +#define PCACHE_PAGE_HASH(pgid) 0 // TODO -static void tdbPCacheLock(SPCache *pCache); -static void tdbPCacheUnlock(SPCache *pCache); -static bool tdbPCacheLocked(SPCache *pCache); +static void tdbPCacheInitLock(SPCache *pCache); +static void tdbPCacheClearLock(SPCache *pCache); +static void tdbPCacheLock(SPCache *pCache); +static void tdbPCacheUnlock(SPCache *pCache); +static bool tdbPCacheLocked(SPCache *pCache); +static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage); int tdbOpenPCache(int pageSize, int cacheSize, int extraSize, SPCache **ppCache) { SPCache *pCache; @@ -53,7 +52,7 @@ int tdbOpenPCache(int pageSize, int cacheSize, int extraSize, SPCache **ppCache) pCache->cacheSize = cacheSize; pCache->extraSize = extraSize; - pthread_mutex_init(&pCache->mutex, NULL); + tdbPCacheInitLock(pCache); for (int i = 0; i < cacheSize; i++) { pPtr = calloc(1, pageSize + extraSize + sizeof(SPgHdr)); @@ -76,17 +75,24 @@ int tdbClosePCache(SPCache *pCache) { return 0; } -void *tdbPCacheFetch(SPCache *pCache, SPgid *pPgid) { +SPgHdr *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) { + SPgHdr *pPage; + tdbPCacheLock(pCache); - // 1. search the hash table + pPage = tdbPCacheFetchImpl(pCache, pPgid, alcNewPage); tdbPCacheUnlock(pCache); - return NULL; + + return pPage; } -void tdbPCacheRelease(void *pHdr) { +void tdbPCacheRelease(SPgHdr *pHdr) { // TODO } +static void tdbPCacheInitLock(SPCache *pCache) { pthread_mutex_init(&(pCache->mutex), NULL); } + +static void tdbPCacheClearLock(SPCache *pCache) { pthread_mutex_destroy(&(pCache->mutex)); } + static void tdbPCacheLock(SPCache *pCache) { pthread_mutex_lock(&(pCache->mutex)); } static void tdbPCacheUnlock(SPCache *pCache) { pthread_mutex_unlock(&(pCache->mutex)); } @@ -95,4 +101,26 @@ static bool tdbPCacheLocked(SPCache *pCache) { assert(0); // TODO return true; +} + +static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) { + SPgHdr *pPage; + + // 1. Search the hash table + pPage = pCache->pgHash[PCACHE_PAGE_HASH(pPgid) % pCache->nHash]; + while (pPage) { + if (memcmp(pPgid, &(pPage->pgid), sizeof(*pPgid)) == 0) break; + pPage = pPage->pHashNext; + } + + if (pPage) { + // TODO: pin the page and return the page + return pPage; + } else if (!alcNewPage) { + return pPage; + } + + // Try other methods + + return pPage; } \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index ee5b486f7b..7c1865e84b 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -17,8 +17,8 @@ typedef struct SPage1 { char magic[64]; - pgno_t mdbRootPgno; // master DB root page number - pgno_t freePgno; // free list page number + SPgno mdbRootPgno; // master DB root page number + SPgno freePgno; // free list page number uint32_t nFree; // number of free pages } SPage1; @@ -28,13 +28,13 @@ typedef struct SFreePage { TDB_STATIC_ASSERT(sizeof(SPage1) <= TDB_MIN_PGSIZE, "TDB Page1 definition too large"); -static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); +static int pgFileRead(SPgFile *pPgFile, SPgno pgno, uint8_t *pData); int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { SPgFile * pPgFile; SPgCache *pPgCache; size_t fnameLen; - pgno_t fsize; + SPgno fsize; *ppPgFile = NULL; @@ -67,7 +67,7 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { if (pPgFile->fsize == 0) { // A created file - pgno_t pgno; + SPgno pgno; pgid_t pgid; pgFileAllocatePage(pPgFile, &pgno); @@ -106,7 +106,7 @@ int pgFileClose(SPgFile *pPgFile) { return 0; } -SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { +SPage *pgFileFetch(SPgFile *pPgFile, SPgno pgno) { SPgCache *pPgCache; SPage * pPage; pgid_t pgid; @@ -161,8 +161,8 @@ int pgFileWrite(SPage *pPage) { return 0; } -int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) { - pgno_t pgno; +int pgFileAllocatePage(SPgFile *pPgFile, SPgno *pPgno) { + SPgno pgno; SPage1 * pPage1; SPgCache *pPgCache; pgid_t pgid; @@ -189,7 +189,7 @@ int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) { return 0; } -static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) { +static int pgFileRead(SPgFile *pPgFile, SPgno pgno, uint8_t *pData) { pgsz_t pgSize; ssize_t rsize; uint8_t *pTData; diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index fa9a3297da..8dbba4d328 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -51,7 +51,7 @@ int tdbCheckFileAccess(const char *pathname, int mode) { return access(pathname, flags); } -int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize) { +int tdbGetFileSize(const char *fname, pgsz_t pgSize, SPgno *pSize) { struct stat st; int ret; diff --git a/source/libs/tdb/src/inc/tdbBtree.h b/source/libs/tdb/src/inc/tdbBtree.h index 94af3331ba..faa71cf2f8 100644 --- a/source/libs/tdb/src/inc/tdbBtree.h +++ b/source/libs/tdb/src/inc/tdbBtree.h @@ -34,7 +34,7 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey); int btreeCursorNext(SBtCursor *pBtCur); struct SBTree { - pgno_t root; + SPgno root; }; #ifdef __cplusplus diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 7b5600581d..ded6067ec1 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -27,8 +27,8 @@ extern "C" { typedef struct SPgFile SPgFile; -// pgno_t -typedef int32_t pgno_t; +// SPgno +typedef int32_t SPgno; #define TDB_IVLD_PGNO ((pgno_t)0) // fileid @@ -37,7 +37,7 @@ typedef int32_t pgno_t; // pgid_t typedef struct { uint8_t fileid[TDB_FILE_ID_LEN]; - pgno_t pgno; + SPgno pgno; } pgid_t, SPgid; #define TDB_IVLD_PGID (pgid_t){0, TDB_IVLD_PGNO}; diff --git a/source/libs/tdb/src/inc/tdbPgCache.h b/source/libs/tdb/src/inc/tdbPgCache.h index 72f690bf0a..d362f1f0ee 100644 --- a/source/libs/tdb/src/inc/tdbPgCache.h +++ b/source/libs/tdb/src/inc/tdbPgCache.h @@ -23,10 +23,18 @@ extern "C" { typedef struct SPCache SPCache; typedef struct SPgHdr SPgHdr; -int tdbOpenPCache(int pageSize, int cacheSize, int extraSize, SPCache **ppCache); -int tdbPCacheClose(SPCache *pCache); -void *tdbPCacheFetch(SPCache *pCache, SPgid *pPgid); -void tdbPCacheRelease(void *pHdr); +struct SPgHdr { + void * pData; + void * pExtra; + SPgid pgid; + SPgHdr *pFreeNext; + SPgHdr *pHashNext; +}; + +int tdbOpenPCache(int pageSize, int cacheSize, int extraSize, SPCache **ppCache); +int tdbPCacheClose(SPCache *pCache); +SPgHdr *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage); +void tdbPCacheRelease(SPgHdr *pHdr); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index 2a7116a0dd..46e257cf81 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -24,7 +24,7 @@ typedef struct __attribute__((__packed__)) { char hdrInfo[16]; // info string pgsz_t szPage; // page size of current file int32_t cno; // commit number counter - pgno_t freePgno; // freelist page number + SPgno freePgno; // freelist page number uint8_t resv[100]; // reserved space } SPgFileHdr; @@ -36,8 +36,8 @@ struct SPgFile { TENV * pEnv; // env containing this page file char * fname; // backend file name uint8_t fileid[TDB_FILE_ID_LEN]; // file id - pgno_t lsize; // page file logical size (for count) - pgno_t fsize; // real file size on disk (for rollback) + SPgno lsize; // page file logical size (for count) + SPgno fsize; // real file size on disk (for rollback) int fd; SPgFileListNode envHash; SPgFileListNode envPgfList; @@ -46,11 +46,11 @@ struct SPgFile { int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); int pgFileClose(SPgFile *pPgFile); -SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno); +SPage *pgFileFetch(SPgFile *pPgFile, SPgno pgno); int pgFileRelease(SPage *pPage); int pgFileWrite(SPage *pPage); -int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno); +int pgFileAllocatePage(SPgFile *pPgFile, SPgno *pPgno); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 8108e5aba6..629a71a09f 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -35,7 +35,7 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); #define TDB_W_OK 0x4 int tdbCheckFileAccess(const char *pathname, int mode); -int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize); +int tdbGetFileSize(const char *fname, pgsz_t pgSize, SPgno *pSize); #ifdef __cplusplus } -- GitLab