From bfd427ae842ef06af29c12821f4c85bdf37bf427 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 20 Jan 2022 07:03:06 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdb_mpool.c | 94 ++++++++++++++++++++++++----- source/libs/tdb/src/inc/tdb_inc.h | 7 ++- source/libs/tdb/src/inc/tdb_mpool.h | 45 +++++++------- 3 files changed, 106 insertions(+), 40 deletions(-) diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c index 4e005b9e03..60829ede94 100644 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ b/source/libs/tdb/src/db/tdb_mpool.c @@ -15,8 +15,10 @@ #include "tdb_mpool.h" +static int tdbGnrtFileID(const char *fname, uint8_t *fileid); + int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { - TDB_MPOOL *mp; + TDB_MPOOL *mp = NULL; size_t tsize; pg_t * pagep; @@ -30,42 +32,102 @@ int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { mp = (TDB_MPOOL *)calloc(1, sizeof(*mp)); if (mp == NULL) { tdbError("failed to malloc memory pool handle"); - return -1; + goto _err; } // initialize the handle mp->cachesize = cachesize; mp->pgsize = 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) { tdbError("failed to malloc memory pool pages"); - free(mp); - return -1; + goto _err; } - 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; - mp->hashtab = (pg_list_t *)calloc(mp->nbucket, sizeof(pg_list_t)); - if (mp->hashtab == NULL) { - tdbError("failed to malloc memory pool hash table"); - free(mp->pages); - free(mp); - return -1; + taosInitRWLatch(&mp->pages[i]->rwLatch); + mp->pages[i]->frameid = i; + mp->pages[i]->pgid = TDB_IVLD_PGID; + + // TODO: add the new page to the free list + // TD_DLIST_APPEND(&mp->freeList, mp->pages[i]); } - for (int i = 0; i < mp->npages; i++) { - pagep = (pg_t *)MP_PAGE_AT(mp, i); - TD_DLIST_APPEND(&mp->freeList, pagep); +#define PGTAB_FACTOR 1.0 + mp->pgtab.nbucket = mp->npages / PGTAB_FACTOR; + 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 *mpp = mp; return 0; + +_err: + tdbMPoolClose(mp); + *mpp = NULL; + return -1; } 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 return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdb_inc.h b/source/libs/tdb/src/inc/tdb_inc.h index ba9d6a560c..d4c9796e54 100644 --- a/source/libs/tdb/src/inc/tdb_inc.h +++ b/source/libs/tdb/src/inc/tdb_inc.h @@ -26,16 +26,17 @@ extern "C" { // pgno_t typedef int32_t pgno_t; -#define TDB_IVLD_PGID ((pgno_t)-1) +#define TDB_IVLD_PGNO ((pgno_t)-1) // fileid -#define TDB_FILE_UID_LEN 20 +#define TDB_FILE_ID_LEN 24 // pgid_t typedef struct { - uint8_t fileid[TDB_FILE_UID_LEN]; + uint8_t fileid[TDB_FILE_ID_LEN]; pgno_t pgid; } pgid_t; +#define TDB_IVLD_PGID (pgid_t){0, TDB_IVLD_PGNO}; // framd_id_t typedef int32_t frame_id_t; diff --git a/source/libs/tdb/src/inc/tdb_mpool.h b/source/libs/tdb/src/inc/tdb_mpool.h index 6b8cb8f844..4bd4d12108 100644 --- a/source/libs/tdb/src/inc/tdb_mpool.h +++ b/source/libs/tdb/src/inc/tdb_mpool.h @@ -26,13 +26,16 @@ extern "C" { typedef struct TDB_MPOOL TDB_MPOOL; 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 { - SRWLatch rwLatch; - pgid_t mpgid; - uint8_t dirty; - int32_t pinRef; - TD_DLIST_NODE(pg_t); - char *page[]; + SRWLatch rwLatch; + frame_id_t frameid; + pgid_t pgid; + uint8_t dirty; + int32_t pinRef; + pg_free_list_node_t free; + pg_hash_list_node_t hash; + uint8_t data[]; } pg_t; #define MP_PAGE_SIZE(pgsize) (sizeof(pg_t) + (pgsize)) @@ -42,33 +45,33 @@ struct TDB_MPOOL { int64_t cachesize; pgsize_t pgsize; int32_t npages; - pg_t * pages; + pg_t ** pages; pg_list_t freeList; - // Hash - int32_t nbucket; - pg_list_t *hashtab; - // TODO: TD_DLIST(TD_MPFILE) mpfList; // MPFILE registered on this memory pool + struct { + int32_t nbucket; + pg_list_t *hashtab; + } pgtab; // page table, hash }; +#define MP_PAGE_AT(mp, idx) (mp)->pages[idx] + struct TDB_MPFILE { - uint8_t fuid[20]; // file unique ID - TDB_MPOOL *mp; // underlying memory pool - char * fname; // file name - int fd; // fd + uint8_t fileid[TDB_FILE_ID_LEN]; // file ID + TDB_MPOOL *mp; // underlying memory pool + char * fname; // file name + int fd; // fd }; -#define MP_PAGE_AT(mp, idx) ((char *)((mp)->pages) + MP_PAGE_SIZE((mp)->pgsize) * (idx)) - /*=================================================== Exposed apis ==================================================*/ // TDB_MPOOL int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize); int tdbMPoolClose(TDB_MPOOL *mp); // TDB_MPFILE -int tdbMPFOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp); -int tdbMPFClose(TDB_MPFILE *mpf); -int tdbMPFGet(TDB_MPFILE *mpf, pgno_t pgid, void *addr); -int tdbMPFPut(TDB_MPOOL *mpf, pgno_t pgid, void *addr); +int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp); +int tdbMPoolFileClose(TDB_MPFILE *mpf); +int tdbMPoolFileGet(TDB_MPFILE *mpf, pgno_t pgid, void *addr); +int tdbMPoolFilePut(TDB_MPOOL *mpf, pgno_t pgid, void *addr); #ifdef __cplusplus } -- GitLab