diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index fb9385af989aa8ef875fd6ef774905b961160b84..8fb54c83444370ce53761e42076a01cd9e5865f5 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -19,6 +19,7 @@ struct SPage { pgid_t pgid; // page id frame_id_t frameid; // frame id SPgListNode freeNode; // for SPgCache.freeList + SPgListNode pghtNode; // for pght uint8_t * pData; // real data }; @@ -36,6 +37,8 @@ struct SPgCache { } pght; // page hash table }; +static void pgCachePinPage(SPage *pPage); + int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage) { SPgCache *pPgCache; SPage * pPage; @@ -123,13 +126,37 @@ int pgCacheClose(SPgCache *pPgCache) { } SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) { - SPage *pPage; + SPage * pPage; + SPgFile *pPgFile; + SPgList *pBucket; + + // 1. Search the page hash table SPgCache.pght + pBucket = pPgCache->pght.buckets + ((0 /*TODO*/) % pPgCache->pght.nbucket); + pPage = TD_DLIST_HEAD(pBucket); + while (pPage && tdbCmprPgId(&(pPage->pgid), &pgid)) { + pPage = TD_DLIST_NODE_NEXT_WITH_FIELD(pPage, pghtNode); + } + + if (pPage) { + // Page is found, pin the page (TODO) and return the page + pgCachePinPage(pPage); + return pPage; + } + + // TODO - // 1. Check if the page is cached return NULL; } int pgCacheRelease(SPage *pPage) { // TODO return 0; +} + +static void pgCachePinPage(SPage *pPage) { + // TODO +} + +static void pgCacheUnpinPage(SPage *pPage) { + // TODO } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index e9cfde5e59ef4561bdea19ee063e9d93bb18e9c4..a2bbcbd562927b6b77ec754d562573c1a50c43b8 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -38,8 +38,28 @@ typedef struct { uint8_t fileid[TDB_FILE_ID_LEN]; pgno_t pgno; } pgid_t; + #define TDB_IVLD_PGID (pgid_t){0, TDB_IVLD_PGNO}; +static FORCE_INLINE int tdbCmprPgId(const void *p1, const void *p2) { + pgid_t *pgid1 = (pgid_t *)p1; + pgid_t *pgid2 = (pgid_t *)p2; + int rcode; + + rcode = memcmp(pgid1->fileid, pgid2->fileid, TDB_FILE_ID_LEN); + if (rcode) { + return rcode; + } else { + if (pgid1->pgno > pgid2->pgno) { + return 1; + } else if (pgid1->pgno < pgid2->pgno) { + return -1; + } else { + return 0; + } + } +} + // framd_id_t typedef int32_t frame_id_t; @@ -51,14 +71,14 @@ typedef int32_t pgsize_t; #define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) // cache -#define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K +#define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K // tdb_log #define tdbError(var) +#include "btree.h" #include "pgcache.h" #include "pgfile.h" -#include "btree.h" // tdb util int tdbGnrtFileID(const char *fname, uint8_t *fileid);