提交 bfd427ae 编写于 作者: H Hongze Cheng

more TDB

上级 ed06d822
...@@ -15,8 +15,10 @@ ...@@ -15,8 +15,10 @@
#include "tdb_mpool.h" #include "tdb_mpool.h"
static int tdbGnrtFileID(const char *fname, uint8_t *fileid);
int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) {
TDB_MPOOL *mp; TDB_MPOOL *mp = NULL;
size_t tsize; size_t tsize;
pg_t * pagep; pg_t * pagep;
...@@ -30,42 +32,102 @@ int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { ...@@ -30,42 +32,102 @@ int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) {
mp = (TDB_MPOOL *)calloc(1, sizeof(*mp)); mp = (TDB_MPOOL *)calloc(1, sizeof(*mp));
if (mp == NULL) { if (mp == NULL) {
tdbError("failed to malloc memory pool handle"); tdbError("failed to malloc memory pool handle");
return -1; goto _err;
} }
// initialize the handle // initialize the handle
mp->cachesize = cachesize; mp->cachesize = cachesize;
mp->pgsize = pgsize; mp->pgsize = pgsize;
mp->npages = cachesize / pgsize; mp->npages = cachesize / pgsize;
mp->pages = (pg_t *)calloc(mp->npages, MP_PAGE_SIZE(pgsize));
TD_DLIST_INIT(&mp->freeList);
mp->pages = (pg_t **)calloc(mp->npages, sizeof(pg_t *));
if (mp->pages == NULL) { if (mp->pages == NULL) {
tdbError("failed to malloc memory pool pages"); tdbError("failed to malloc memory pool pages");
free(mp); goto _err;
return -1;
} }
TD_DLIST_INIT(&(mp->freeList)); for (frame_id_t i = 0; i < mp->npages; i++) {
mp->pages[i] = (pg_t *)calloc(1, MP_PAGE_SIZE(pgsize));
if (mp->pages[i] == NULL) {
goto _err;
}
mp->nbucket = mp->npages; taosInitRWLatch(&mp->pages[i]->rwLatch);
mp->hashtab = (pg_list_t *)calloc(mp->nbucket, sizeof(pg_list_t)); mp->pages[i]->frameid = i;
if (mp->hashtab == NULL) { mp->pages[i]->pgid = TDB_IVLD_PGID;
tdbError("failed to malloc memory pool hash table");
free(mp->pages); // TODO: add the new page to the free list
free(mp); // TD_DLIST_APPEND(&mp->freeList, mp->pages[i]);
return -1;
} }
for (int i = 0; i < mp->npages; i++) { #define PGTAB_FACTOR 1.0
pagep = (pg_t *)MP_PAGE_AT(mp, i); mp->pgtab.nbucket = mp->npages / PGTAB_FACTOR;
TD_DLIST_APPEND(&mp->freeList, pagep); mp->pgtab.hashtab = (pg_list_t *)calloc(mp->pgtab.nbucket, sizeof(pg_list_t));
if (mp->pgtab.hashtab == NULL) {
tdbError("failed to malloc memory pool hash table");
goto _err;
} }
// return // return
*mpp = mp; *mpp = mp;
return 0; return 0;
_err:
tdbMPoolClose(mp);
*mpp = NULL;
return -1;
} }
int tdbMPoolClose(TDB_MPOOL *mp) { int tdbMPoolClose(TDB_MPOOL *mp) {
if (mp) {
tfree(mp->pgtab.hashtab);
if (mp->pages) {
for (int i = 0; i < mp->npages; i++) {
tfree(mp->pages[i]);
}
free(mp->pages);
}
free(mp);
}
return 0;
}
int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) {
TDB_MPFILE *mpf;
if ((mpf = (TDB_MPFILE *)calloc(1, sizeof(*mpf))) == NULL) {
return -1;
}
mpf->fd = -1;
if ((mpf->fname = strdup(fname)) == NULL) {
goto _err;
}
if ((mpf->fd = open(fname, O_CREAT | O_RDWR, 0755)) < 0) {
goto _err;
}
*mpfp = mpf;
return 0;
_err:
tdbMPoolFileClose(mpf);
*mpfp = NULL;
return -1;
}
int tdbMPoolFileClose(TDB_MPFILE *mpf) {
// TODO
return 0;
}
static int tdbGnrtFileID(const char *fname, uint8_t *fileid) {
// TODO // TODO
return 0; return 0;
} }
\ No newline at end of file
...@@ -26,16 +26,17 @@ extern "C" { ...@@ -26,16 +26,17 @@ extern "C" {
// pgno_t // pgno_t
typedef int32_t pgno_t; typedef int32_t pgno_t;
#define TDB_IVLD_PGID ((pgno_t)-1) #define TDB_IVLD_PGNO ((pgno_t)-1)
// fileid // fileid
#define TDB_FILE_UID_LEN 20 #define TDB_FILE_ID_LEN 24
// pgid_t // pgid_t
typedef struct { typedef struct {
uint8_t fileid[TDB_FILE_UID_LEN]; uint8_t fileid[TDB_FILE_ID_LEN];
pgno_t pgid; pgno_t pgid;
} pgid_t; } pgid_t;
#define TDB_IVLD_PGID (pgid_t){0, TDB_IVLD_PGNO};
// framd_id_t // framd_id_t
typedef int32_t frame_id_t; typedef int32_t frame_id_t;
......
...@@ -26,13 +26,16 @@ extern "C" { ...@@ -26,13 +26,16 @@ extern "C" {
typedef struct TDB_MPOOL TDB_MPOOL; typedef struct TDB_MPOOL TDB_MPOOL;
typedef struct TDB_MPFILE TDB_MPFILE; typedef struct TDB_MPFILE TDB_MPFILE;
typedef TD_DLIST_NODE(pg_t) pg_free_list_node_t, pg_hash_list_node_t;
typedef struct pg_t { typedef struct pg_t {
SRWLatch rwLatch; SRWLatch rwLatch;
pgid_t mpgid; frame_id_t frameid;
uint8_t dirty; pgid_t pgid;
int32_t pinRef; uint8_t dirty;
TD_DLIST_NODE(pg_t); int32_t pinRef;
char *page[]; pg_free_list_node_t free;
pg_hash_list_node_t hash;
uint8_t data[];
} pg_t; } pg_t;
#define MP_PAGE_SIZE(pgsize) (sizeof(pg_t) + (pgsize)) #define MP_PAGE_SIZE(pgsize) (sizeof(pg_t) + (pgsize))
...@@ -42,33 +45,33 @@ struct TDB_MPOOL { ...@@ -42,33 +45,33 @@ struct TDB_MPOOL {
int64_t cachesize; int64_t cachesize;
pgsize_t pgsize; pgsize_t pgsize;
int32_t npages; int32_t npages;
pg_t * pages; pg_t ** pages;
pg_list_t freeList; pg_list_t freeList;
// Hash<pgid_t, frame_id_t> struct {
int32_t nbucket; int32_t nbucket;
pg_list_t *hashtab; pg_list_t *hashtab;
// TODO: TD_DLIST(TD_MPFILE) mpfList; // MPFILE registered on this memory pool } pgtab; // page table, hash<pgid_t, pg_t>
}; };
#define MP_PAGE_AT(mp, idx) (mp)->pages[idx]
struct TDB_MPFILE { struct TDB_MPFILE {
uint8_t fuid[20]; // file unique ID uint8_t fileid[TDB_FILE_ID_LEN]; // file ID
TDB_MPOOL *mp; // underlying memory pool TDB_MPOOL *mp; // underlying memory pool
char * fname; // file name char * fname; // file name
int fd; // fd int fd; // fd
}; };
#define MP_PAGE_AT(mp, idx) ((char *)((mp)->pages) + MP_PAGE_SIZE((mp)->pgsize) * (idx))
/*=================================================== Exposed apis ==================================================*/ /*=================================================== Exposed apis ==================================================*/
// TDB_MPOOL // TDB_MPOOL
int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize); int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize);
int tdbMPoolClose(TDB_MPOOL *mp); int tdbMPoolClose(TDB_MPOOL *mp);
// TDB_MPFILE // TDB_MPFILE
int tdbMPFOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp); int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp);
int tdbMPFClose(TDB_MPFILE *mpf); int tdbMPoolFileClose(TDB_MPFILE *mpf);
int tdbMPFGet(TDB_MPFILE *mpf, pgno_t pgid, void *addr); int tdbMPoolFileGet(TDB_MPFILE *mpf, pgno_t pgid, void *addr);
int tdbMPFPut(TDB_MPOOL *mpf, pgno_t pgid, void *addr); int tdbMPoolFilePut(TDB_MPOOL *mpf, pgno_t pgid, void *addr);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册