This commit is contained in:
Hongze Cheng 2022-02-24 07:11:15 +00:00
parent b0ff360aa5
commit 20845ce4bf
3 changed files with 24 additions and 14 deletions

View File

@ -31,8 +31,12 @@ struct SPCache {
SPgHdr * pDirtyTail; SPgHdr * pDirtyTail;
}; };
#define PCACHE_PAGE_HASH(pgid) 0 // TODO #define PCACHE_PAGE_HASH(pPgid) \
#define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL) ({ \
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 int tdbPCacheOpenImpl(SPCache *pCache);
static void tdbPCacheInitLock(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) { void tdbPCacheFetchFinish(SPCache *pCache, SPgHdr *pPage) {
// TODO /* TODO */
pPage->nRef++; // TODO: do we need atomic operation???
} }
void tdbPCacheRelease(SPgHdr *pHdr) { void tdbPCacheRelease(SPgHdr *pHdr) {
@ -110,7 +115,7 @@ static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcN
// 1. Search the hash table // 1. Search the hash table
pPage = pCache->pgHash[PCACHE_PAGE_HASH(pPgid) % pCache->nHash]; pPage = pCache->pgHash[PCACHE_PAGE_HASH(pPgid) % pCache->nHash];
while (pPage) { while (pPage) {
if (memcmp(pPgid, &(pPage->pgid), sizeof(*pPgid)) == 0) break; if (TDB_IS_SAME_PAGE(&(pPage->pgid), pPgid)) break;
pPage = pPage->pHashNext; pPage = pPage->pHashNext;
} }
@ -134,15 +139,15 @@ static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcN
tdbPCachePinPage(pPage); tdbPCachePinPage(pPage);
} }
// 4. Try a stress allocation // 4. Try a stress allocation (TODO)
// 5. Page here are just created from a free list // 5. Page here are just created from a free list
// or by recycling or allocated streesly, // or by recycling or allocated streesly,
// need to initialize it // need to initialize it
if (pPage) { if (pPage) {
memcpy(&pPage->pgid, pPgid, sizeof(*pPgid)); memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid));
pPage->pCache = pCache;
pPage->pLruNext = NULL; pPage->pLruNext = NULL;
// *(void **)pPage->page.pExtra = 0; (TODO)
tdbPCacheAddPageToHash(pPage); tdbPCacheAddPageToHash(pPage);
} }
@ -182,7 +187,7 @@ static void tdbPCacheAddPageToHash(SPgHdr *pPage) {
int h; int h;
pCache = pPage->pCache; pCache = pPage->pCache;
h = PCACHE_PAGE_HASH(&pPage->pgid) % pCache->nHash; h = PCACHE_PAGE_HASH(&(pPage->pgid)) % pCache->nHash;
pPage->pHashNext = pCache->pgHash[h]; pPage->pHashNext = pCache->pgHash[h];
pCache->pgHash[h] = pPage; pCache->pgHash[h] = pPage;

View File

@ -71,14 +71,18 @@ int tdbPFileClose(SPFile *pFile) {
} }
SPgHdr *tdbPFileGet(SPFile *pFile, SPgno pgno) { SPgHdr *tdbPFileGet(SPFile *pFile, SPgno pgno) {
SPCache *pCache; SPgid pgid;
SPgid pgid; SPgHdr *pPage;
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); if (true /* not load from the file, then load the content from the file*/) {
tdbPCacheFetchFinish(pCache, pPage); }
return pPage; return pPage;
} }

View File

@ -29,6 +29,7 @@ struct SPgHdr {
SPgid pgid; SPgid pgid;
u8 isAnchor; u8 isAnchor;
u8 isLocalPage; u8 isLocalPage;
i32 nRef;
SPCache *pCache; SPCache *pCache;
SPgHdr * pFreeNext; SPgHdr * pFreeNext;
SPgHdr * pHashNext; SPgHdr * pHashNext;