From 20845ce4bfe0dc7c3eb5099e23fa4402c012d366 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 24 Feb 2022 07:11:15 +0000 Subject: [PATCH] more --- source/libs/tdb/src/db/tdbPCache.c | 21 +++++++++++++-------- source/libs/tdb/src/db/tdbPFile.c | 16 ++++++++++------ source/libs/tdb/src/inc/tdbPCache.h | 1 + 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index 9cb6889063..a62cfbdf59 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -31,8 +31,12 @@ struct SPCache { SPgHdr * pDirtyTail; }; -#define PCACHE_PAGE_HASH(pgid) 0 // TODO -#define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL) +#define PCACHE_PAGE_HASH(pPgid) \ + ({ \ + u32 *t = (u32 *)((pPgid)->fileid); \ + t[0] + t[1] + t[2] + t[3] + t[4] + t[5] + (pPgid)->pgno; \ + }) +#define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL) static int tdbPCacheOpenImpl(SPCache *pCache); static void tdbPCacheInitLock(SPCache *pCache); @@ -83,7 +87,8 @@ SPgHdr *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) { } void tdbPCacheFetchFinish(SPCache *pCache, SPgHdr *pPage) { - // TODO + /* TODO */ + pPage->nRef++; // TODO: do we need atomic operation??? } void tdbPCacheRelease(SPgHdr *pHdr) { @@ -110,7 +115,7 @@ static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcN // 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; + if (TDB_IS_SAME_PAGE(&(pPage->pgid), pPgid)) break; pPage = pPage->pHashNext; } @@ -134,15 +139,15 @@ static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcN tdbPCachePinPage(pPage); } - // 4. Try a stress allocation + // 4. Try a stress allocation (TODO) // 5. Page here are just created from a free list // or by recycling or allocated streesly, // need to initialize it if (pPage) { - memcpy(&pPage->pgid, pPgid, sizeof(*pPgid)); - pPage->pCache = pCache; + memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid)); pPage->pLruNext = NULL; + // *(void **)pPage->page.pExtra = 0; (TODO) tdbPCacheAddPageToHash(pPage); } @@ -182,7 +187,7 @@ static void tdbPCacheAddPageToHash(SPgHdr *pPage) { int h; pCache = pPage->pCache; - h = PCACHE_PAGE_HASH(&pPage->pgid) % pCache->nHash; + h = PCACHE_PAGE_HASH(&(pPage->pgid)) % pCache->nHash; pPage->pHashNext = pCache->pgHash[h]; pCache->pgHash[h] = pPage; diff --git a/source/libs/tdb/src/db/tdbPFile.c b/source/libs/tdb/src/db/tdbPFile.c index 5e70667b8f..144856c79a 100644 --- a/source/libs/tdb/src/db/tdbPFile.c +++ b/source/libs/tdb/src/db/tdbPFile.c @@ -71,14 +71,18 @@ int tdbPFileClose(SPFile *pFile) { } SPgHdr *tdbPFileGet(SPFile *pFile, SPgno pgno) { - SPCache *pCache; - SPgid pgid; - SPgHdr * pPage; + SPgid pgid; + SPgHdr *pPage; - pCache = pFile->pCache; + pPage = tdbPCacheFetch(pFile->pCache, &pgid, 1); + if (pPage == NULL) { + // TODO + ASSERT(0); + } + tdbPCacheFetchFinish(pFile->pCache, pPage); - pPage = tdbPCacheFetch(pCache, &pgid, true); - tdbPCacheFetchFinish(pCache, pPage); + if (true /* not load from the file, then load the content from the file*/) { + } return pPage; } diff --git a/source/libs/tdb/src/inc/tdbPCache.h b/source/libs/tdb/src/inc/tdbPCache.h index 41a7d0a26b..db936dad8c 100644 --- a/source/libs/tdb/src/inc/tdbPCache.h +++ b/source/libs/tdb/src/inc/tdbPCache.h @@ -29,6 +29,7 @@ struct SPgHdr { SPgid pgid; u8 isAnchor; u8 isLocalPage; + i32 nRef; SPCache *pCache; SPgHdr * pFreeNext; SPgHdr * pHashNext;