提交 6d446e61 编写于 作者: H Hongze Cheng

more TDB

上级 a195e51c
......@@ -14,9 +14,12 @@
*/
#include "tdbInt.h"
typedef TD_DLIST_NODE(SPage) SPgListNode;
struct SPage {
pgid_t pgid; // page id
// TODO
pgid_t pgid; // page id
frame_id_t frameid; // frame id
SPgListNode freeNode; // for SPgCache.freeList
uint8_t * pData; // real data
};
typedef TD_DLIST(SPage) SPgList;
......@@ -24,6 +27,7 @@ typedef TD_DLIST(SPage) SPgList;
struct SPgCache {
SRWLatch mutex;
pgsize_t pgsize;
int32_t npage;
SPage * pages;
SPgList freeList;
struct {
......@@ -32,31 +36,83 @@ struct SPgCache {
} pght; // page hash table
};
int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgsize) {
int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage) {
SPgCache *pPgCache;
SPage * pPage;
*ppPgCache = NULL;
if (!TDB_IS_PGSIZE_VLD(pgSize)) {
return -1;
}
pPgCache = (SPgCache *)calloc(1, sizeof(*pPgCache));
if (pPgCache == NULL) {
return -1;
}
pPgCache->pgsize = pgsize;
taosInitRWLatch(&(pPgCache->mutex));
pPgCache->pgsize = pgSize;
pPgCache->npage = npage;
pPgCache->pages = (SPage *)calloc(npage, sizeof(SPage));
if (pPgCache->pages == NULL) {
pgCacheDestroy(pPgCache);
return -1;
}
TD_DLIST_INIT(&(pPgCache->freeList));
for (int32_t i = 0; i < npage; i++) {
pPage = pPgCache->pages + i;
pPage->pgid = TDB_IVLD_PGID;
pPage->frameid = i;
pPage->pData = (uint8_t *)calloc(1, pgSize);
if (pPage->pData == NULL) {
pgCacheDestroy(pPgCache);
return -1;
}
pPgCache->pght.nbucket = npage;
pPgCache->pght.buckets = (SPgList *)calloc(pPgCache->pght.nbucket, sizeof(SPgList));
if (pPgCache->pght.buckets == NULL) {
pgCacheDestroy(pPgCache);
return -1;
}
TD_DLIST_APPEND_WITH_FIELD(&(pPgCache->freeList), pPage, freeNode);
}
*ppPgCache = pPgCache;
return 0;
}
int pgCacheDestroy(SPgCache *pPgCache) {
SPage *pPage;
if (pPgCache) {
tfree(pPgCache->pght.buckets);
if (pPgCache->pages) {
for (int32_t i = 0; i < pPgCache->npage; i++) {
pPage = pPgCache->pages + i;
tfree(pPage->pData);
}
free(pPgCache->pages);
}
free(pPgCache);
}
return 0;
}
int pgCacheOpen(SPgCache *pPgCache) {
int pgCacheOpen(SPgCache **ppPgCache) {
if (*ppPgCache == NULL) {
if (pgCacheCreate(ppPgCache, TDB_DEFAULT_PGSIZE, TDB_DEFAULT_CACHE_SIZE / TDB_DEFAULT_PGSIZE) < 0) {
return -1;
}
}
// TODO
return 0;
}
......
......@@ -24,9 +24,9 @@ typedef struct SPgCache SPgCache;
typedef struct SPage SPage;
// SPgCache
int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgsize);
int pgCacheDestroy(SPgCache *pPgCache);
int pgCacheOpen(SPgCache *pPgCache);
int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage);
int pgCacheOpen(SPgCache **ppPgCache);
int pgCacheClose(SPgCache *pPgCache);
int pgCacheClose(SPgCache *pPgCache);
SPage *pgCacheFetch(SPgCache *pPgCache);
......
......@@ -50,6 +50,9 @@ typedef int32_t pgsize_t;
#define TDB_DEFAULT_PGSIZE 4096
#define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE))
// cache
#define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K
// tdb_log
#define tdbError(var)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册