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

more tdb

上级 c50b787f
...@@ -34,7 +34,7 @@ extern "C" { ...@@ -34,7 +34,7 @@ extern "C" {
#define TD_SLIST_HEAD(sl) ((sl)->sl_head_) #define TD_SLIST_HEAD(sl) ((sl)->sl_head_)
#define TD_SLIST_NELES(sl) ((sl)->sl_neles_) #define TD_SLIST_NELES(sl) ((sl)->sl_neles_)
#define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_) #define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_)
#define TD_SLIST_NODE_NEXT_WITH_FIELD(sln, feild) ((sln)->feild.sl_next_) #define TD_SLIST_NODE_NEXT_WITH_FIELD(sln, field) ((sln)->field.sl_next_)
#define TD_SLIST_INIT(sl) \ #define TD_SLIST_INIT(sl) \
do { \ do { \
...@@ -49,9 +49,9 @@ extern "C" { ...@@ -49,9 +49,9 @@ extern "C" {
TD_SLIST_NELES(sl) += 1; \ TD_SLIST_NELES(sl) += 1; \
} while (0) } while (0)
#define TD_SLIST_PUSH_WITH_FIELD(sl, sln, feild) \ #define TD_SLIST_PUSH_WITH_FIELD(sl, sln, field) \
do { \ do { \
TD_SLIST_NODE_NEXT_WITH_FIELD(sln, feild) = TD_SLIST_HEAD(sl); \ TD_SLIST_NODE_NEXT_WITH_FIELD(sln, field) = TD_SLIST_HEAD(sl); \
TD_SLIST_HEAD(sl) = (sln); \ TD_SLIST_HEAD(sl) = (sln); \
TD_SLIST_NELES(sl) += 1; \ TD_SLIST_NELES(sl) += 1; \
} while (0) } while (0)
...@@ -62,9 +62,9 @@ extern "C" { ...@@ -62,9 +62,9 @@ extern "C" {
TD_SLIST_NELES(sl) -= 1; \ TD_SLIST_NELES(sl) -= 1; \
} while (0) } while (0)
#define TD_SLIST_POP_WITH_FIELD(sl, feild) \ #define TD_SLIST_POP_WITH_FIELD(sl, field) \
do { \ do { \
TD_SLIST_HEAD(sl) = TD_SLIST_NODE_NEXT_WITH_FIELD(TD_SLIST_HEAD(sl), feild); \ TD_SLIST_HEAD(sl) = TD_SLIST_NODE_NEXT_WITH_FIELD(TD_SLIST_HEAD(sl), field); \
TD_SLIST_NELES(sl) -= 1; \ TD_SLIST_NELES(sl) -= 1; \
} while (0) } while (0)
...@@ -84,8 +84,8 @@ extern "C" { ...@@ -84,8 +84,8 @@ extern "C" {
#define TD_DLIST_NODE_PREV(dln) ((dln)->dl_prev_) #define TD_DLIST_NODE_PREV(dln) ((dln)->dl_prev_)
#define TD_DLIST_NODE_NEXT(dln) ((dln)->dl_next_) #define TD_DLIST_NODE_NEXT(dln) ((dln)->dl_next_)
#define TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) ((dln)->feild.dl_prev_) #define TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) ((dln)->field.dl_prev_)
#define TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) ((dln)->feild.dl_next_) #define TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) ((dln)->field.dl_next_)
#define TD_DLIST_HEAD(dl) ((dl)->dl_head_) #define TD_DLIST_HEAD(dl) ((dl)->dl_head_)
#define TD_DLIST_TAIL(dl) ((dl)->dl_tail_) #define TD_DLIST_TAIL(dl) ((dl)->dl_tail_)
#define TD_DLIST_NELES(dl) ((dl)->dl_neles_) #define TD_DLIST_NELES(dl) ((dl)->dl_neles_)
...@@ -110,15 +110,15 @@ extern "C" { ...@@ -110,15 +110,15 @@ extern "C" {
TD_DLIST_NELES(dl) += 1; \ TD_DLIST_NELES(dl) += 1; \
} while (0) } while (0)
#define TD_DLIST_APPEND_WITH_FEILD(dl, dln, feild) \ #define TD_DLIST_APPEND_WITH_FIELD(dl, dln, field) \
do { \ do { \
if (TD_DLIST_HEAD(dl) == NULL) { \ if (TD_DLIST_HEAD(dl) == NULL) { \
TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = NULL; \ TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \
TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln); \ TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln); \
} else { \ } else { \
TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = TD_DLIST_TAIL(dl); \ TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_TAIL(dl); \
TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = NULL; \ TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \
TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_TAIL(dl), feild) = (dln); \ TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_TAIL(dl), field) = (dln); \
TD_DLIST_TAIL(dl) = (dln); \ TD_DLIST_TAIL(dl) = (dln); \
} \ } \
TD_DLIST_NELES(dl) += 1; \ TD_DLIST_NELES(dl) += 1; \
...@@ -138,15 +138,15 @@ extern "C" { ...@@ -138,15 +138,15 @@ extern "C" {
TD_DLIST_NELES(dl) += 1; \ TD_DLIST_NELES(dl) += 1; \
} while (0) } while (0)
#define TD_DLIST_PREPEND_WITH_FIELD(dl, dln, feild) \ #define TD_DLIST_PREPEND_WITH_FIELD(dl, dln, field) \
do { \ do { \
if (TD_DLIST_HEAD(dl) == NULL) { \ if (TD_DLIST_HEAD(dl) == NULL) { \
TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = NULL; \ TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \
TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln); \ TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln); \
} else { \ } else { \
TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = NULL; \ TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = NULL; \
TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = TD_DLIST_HEAD(dl); \ TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = TD_DLIST_HEAD(dl); \
TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_HEAD(dl), feild) = (dln); \ TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_HEAD(dl), field) = (dln); \
TD_DLIST_HEAD(dl) = (dln); \ TD_DLIST_HEAD(dl) = (dln); \
} \ } \
TD_DLIST_NELES(dl) += 1; \ TD_DLIST_NELES(dl) += 1; \
...@@ -173,21 +173,21 @@ extern "C" { ...@@ -173,21 +173,21 @@ extern "C" {
#define TD_DLIST_POP_WITH_FIELD(dl, dln, field) \ #define TD_DLIST_POP_WITH_FIELD(dl, dln, field) \
do { \ do { \
if (TD_DLIST_HEAD(dl) == (dln)) { \ if (TD_DLIST_HEAD(dl) == (dln)) { \
TD_DLIST_HEAD(dl) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild); \ TD_DLIST_HEAD(dl) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field); \
} \ } \
if (TD_DLIST_TAIL(dl) == (dln)) { \ if (TD_DLIST_TAIL(dl) == (dln)) { \
TD_DLIST_TAIL(dl) = TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild); \ TD_DLIST_TAIL(dl) = TD_DLIST_NODE_PREV_WITH_FIELD(dln, field); \
} \ } \
if (TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) != NULL) { \ if (TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) != NULL) { \
TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild), feild) = \ TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_NODE_PREV_WITH_FIELD(dln, field), field) = \
TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild); \ TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field); \
} \ } \
if (TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) != NULL) { \ if (TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) != NULL) { \
TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild), feild) = \ TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field), field) = \
TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild); \ TD_DLIST_NODE_PREV_WITH_FIELD(dln, field); \
} \ } \
TD_DLIST_NELES(dl) -= 1; \ TD_DLIST_NELES(dl) -= 1; \
TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = NULL; \ TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \
} while (0) } while (0)
// General double linked list // General double linked list
......
...@@ -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); static int tdbGnrtFileID(const char *fname, uint8_t *fileid);
static void tdbMpoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf);
static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf);
static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, 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 = NULL; TDB_MPOOL *mp = NULL;
...@@ -60,7 +62,7 @@ int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { ...@@ -60,7 +62,7 @@ int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) {
mp->pages[i]->pgid = TDB_IVLD_PGID; mp->pages[i]->pgid = TDB_IVLD_PGID;
// add new page to the free list // add new page to the free list
TD_DLIST_APPEND_WITH_FEILD(&(mp->freeList), mp->pages[i], free); TD_DLIST_APPEND_WITH_FIELD(&(mp->freeList), mp->pages[i], free);
} }
#define PGTAB_FACTOR 1.0 #define PGTAB_FACTOR 1.0
...@@ -105,7 +107,6 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { ...@@ -105,7 +107,6 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) {
} }
mpf->fd = -1; mpf->fd = -1;
mpf->mp = mp;
if ((mpf->fname = strdup(fname)) == NULL) { if ((mpf->fname = strdup(fname)) == NULL) {
goto _err; goto _err;
...@@ -120,7 +121,7 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { ...@@ -120,7 +121,7 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) {
} }
// Register current MPF to MP // Register current MPF to MP
tdbMpoolRegFile(mp, mpf); tdbMPoolRegFile(mp, mpf);
*mpfp = mpf; *mpfp = mpf;
return 0; return 0;
...@@ -193,6 +194,76 @@ static int tdbGnrtFileID(const char *fname, uint8_t *fileid) { ...@@ -193,6 +194,76 @@ static int tdbGnrtFileID(const char *fname, uint8_t *fileid) {
return 0; return 0;
} }
static void tdbMpoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { #define MPF_GET_BUCKETID(fileid) \
// TODO ({ \
uint64_t *tmp = fileid; \
(tmp[0] + tmp[1] + tmp[2]) % MPF_HASH_BUCKETS; \
})
static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) {
int bucketid;
mpf_bucket_t *bktp;
bucketid = MPF_GET_BUCKETID(mpf->fileid);
bktp = mp->mpfht.buckets + bucketid;
taosWLockLatch(&(bktp->latch));
TD_DLIST_APPEND_WITH_FIELD(bktp, mpf, node);
taosWUnLockLatch(&(bktp->latch));
mpf->mp = mp;
}
static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid) {
int bucketid;
TDB_MPFILE * mpf = NULL;
mpf_bucket_t *bktp;
bucketid = MPF_GET_BUCKETID(fileid);
bktp = mp->mpfht.buckets + bucketid;
taosRLockLatch(&(bktp->latch));
mpf = TD_DLIST_HEAD(bktp);
while (mpf) {
if (memcmp(fileid, mpf->fileid, TDB_FILE_ID_LEN) == 0) {
break;
}
mpf = TD_DLIST_NODE_NEXT_WITH_FIELD(mpf, node);
}
taosRUnLockLatch(&(bktp->latch));
return mpf;
}
static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) {
mpf_bucket_t *bktp;
TDB_MPFILE * tmpf;
if (mpf->mp == NULL) return;
ASSERT(mpf->mp == mp);
bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(mpf->fileid);
taosWLockLatch(&(bktp->latch));
tmpf = TD_DLIST_HEAD(bktp);
while (tmpf) {
if (memcmp(mpf->fileid, tmpf->fileid, TDB_FILE_ID_LEN) == 0) {
TD_DLIST_POP_WITH_FIELD(bktp, tmpf, node);
break;
}
tmpf = TD_DLIST_NODE_NEXT_WITH_FIELD(tmpf, node);
}
taosWUnLockLatch(&(bktp->latch));
ASSERT(tmpf == mpf);
} }
\ No newline at end of file
...@@ -26,21 +26,25 @@ extern "C" { ...@@ -26,21 +26,25 @@ 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 TD_DLIST_NODE(pg_t) pg_free_dlist_node_t, pg_hash_dlist_node_t;
typedef struct pg_t { typedef struct pg_t {
SRWLatch rwLatch; SRWLatch rwLatch;
frame_id_t frameid; frame_id_t frameid;
pgid_t pgid; pgid_t pgid;
uint8_t dirty; uint8_t dirty;
int32_t pinRef; int32_t pinRef;
pg_free_list_node_t free; pg_free_dlist_node_t free;
pg_hash_list_node_t hash; pg_hash_dlist_node_t hash;
uint8_t data[]; 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))
typedef TD_DLIST(pg_t) pg_list_t; typedef TD_DLIST(pg_t) pg_list_t;
typedef struct {
SRWLatch latch;
TD_DLIST(TDB_MPFILE);
} mpf_bucket_t;
struct TDB_MPOOL { struct TDB_MPOOL {
int64_t cachesize; int64_t cachesize;
pgsize_t pgsize; pgsize_t pgsize;
...@@ -52,19 +56,20 @@ struct TDB_MPOOL { ...@@ -52,19 +56,20 @@ struct TDB_MPOOL {
pg_list_t *hashtab; pg_list_t *hashtab;
} pgtab; // page table, hash<pgid_t, pg_t> } pgtab; // page table, hash<pgid_t, pg_t>
struct { struct {
int32_t nbucket; #define MPF_HASH_BUCKETS 16
TD_DLIST(TDB_MPFILE) hashtab[8]; mpf_bucket_t buckets[MPF_HASH_BUCKETS];
} mpftab; } mpfht; // MPF hash table. MPFs using this MP will be put in this hash table
}; };
#define MP_PAGE_AT(mp, idx) (mp)->pages[idx] #define MP_PAGE_AT(mp, idx) (mp)->pages[idx]
typedef TD_DLIST_NODE(TDB_MPFILE) td_mpf_dlist_node_t;
struct TDB_MPFILE { struct TDB_MPFILE {
char * fname; // file name char * fname; // file name
int fd; // fd int fd; // fd
uint8_t fileid[TDB_FILE_ID_LEN]; // file ID uint8_t fileid[TDB_FILE_ID_LEN]; // file ID
TDB_MPOOL *mp; // underlying memory pool TDB_MPOOL * mp; // underlying memory pool
TD_DLIST_NODE(TDB_MPFILE); td_mpf_dlist_node_t node;
}; };
/*=================================================== Exposed apis ==================================================*/ /*=================================================== Exposed apis ==================================================*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册