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

more TDB

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