tdb_mpool.h 2.6 KB
Newer Older
H
more  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Hongze Cheng 已提交
16 17
#ifndef _TD_TDB_MPOOL_H_
#define _TD_TDB_MPOOL_H_
H
Hongze Cheng 已提交
18

H
refact  
Hongze Cheng 已提交
19
#include "tdb_inc.h"
H
more  
Hongze Cheng 已提交
20 21 22 23 24

#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
25
// Exposed handle
H
Hongze Cheng 已提交
26 27
typedef struct TDB_MPOOL  TDB_MPOOL;
typedef struct TDB_MPFILE TDB_MPFILE;
H
Hongze Cheng 已提交
28

H
Hongze Cheng 已提交
29
typedef TD_DLIST_NODE(pg_t) pg_free_dlist_node_t, pg_hash_dlist_node_t;
H
refact  
Hongze Cheng 已提交
30
typedef struct pg_t {
H
Hongze Cheng 已提交
31 32 33 34 35 36 37 38
  SRWLatch             rwLatch;
  frame_id_t           frameid;
  pgid_t               pgid;
  uint8_t              dirty;
  int32_t              pinRef;
  pg_free_dlist_node_t free;
  pg_hash_dlist_node_t hash;
  uint8_t              data[];
H
refact  
Hongze Cheng 已提交
39
} pg_t;
H
Hongze Cheng 已提交
40

H
refact  
Hongze Cheng 已提交
41
#define MP_PAGE_SIZE(pgsize) (sizeof(pg_t) + (pgsize))
H
Hongze Cheng 已提交
42

H
refact  
Hongze Cheng 已提交
43
typedef TD_DLIST(pg_t) pg_list_t;
H
Hongze Cheng 已提交
44 45 46 47
typedef struct {
  SRWLatch latch;
  TD_DLIST(TDB_MPFILE);
} mpf_bucket_t;
H
Hongze Cheng 已提交
48
struct TDB_MPOOL {
H
refact  
Hongze Cheng 已提交
49 50 51
  int64_t   cachesize;
  pgsize_t  pgsize;
  int32_t   npages;
H
Hongze Cheng 已提交
52
  pg_t **   pages;
H
refact  
Hongze Cheng 已提交
53
  pg_list_t freeList;
H
Hongze Cheng 已提交
54 55 56 57
  struct {
    int32_t    nbucket;
    pg_list_t *hashtab;
  } pgtab;  // page table, hash<pgid_t, pg_t>
H
Hongze Cheng 已提交
58
  struct {
H
Hongze Cheng 已提交
59 60 61
#define MPF_HASH_BUCKETS 16
    mpf_bucket_t buckets[MPF_HASH_BUCKETS];
  } mpfht;  // MPF hash table. MPFs using this MP will be put in this hash table
H
Hongze Cheng 已提交
62
};
H
more  
Hongze Cheng 已提交
63

H
Hongze Cheng 已提交
64 65
#define MP_PAGE_AT(mp, idx) (mp)->pages[idx]

H
Hongze Cheng 已提交
66
typedef TD_DLIST_NODE(TDB_MPFILE) td_mpf_dlist_node_t;
H
Hongze Cheng 已提交
67
struct TDB_MPFILE {
H
Hongze Cheng 已提交
68 69 70 71 72
  char *              fname;                    // file name
  int                 fd;                       // fd
  uint8_t             fileid[TDB_FILE_ID_LEN];  // file ID
  TDB_MPOOL *         mp;                       // underlying memory pool
  td_mpf_dlist_node_t node;
H
Hongze Cheng 已提交
73
};
H
Hongze Cheng 已提交
74

H
Hongze Cheng 已提交
75 76
/*=================================================== Exposed apis ==================================================*/
// TDB_MPOOL
H
Hongze Cheng 已提交
77 78
int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize);
int tdbMPoolClose(TDB_MPOOL *mp);
H
Hongze Cheng 已提交
79

H
Hongze Cheng 已提交
80
// TDB_MPFILE
H
Hongze Cheng 已提交
81 82 83 84
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);
H
Hongze Cheng 已提交
85

H
more  
Hongze Cheng 已提交
86 87 88 89
#ifdef __cplusplus
}
#endif

H
Hongze Cheng 已提交
90
#endif /*_TD_TDB_MPOOL_H_*/