tdb_mpool.h 2.7 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
  SRWLatch             rwLatch;
  frame_id_t           frameid;
  pgid_t               pgid;
  uint8_t              dirty;
H
refact  
Hongze Cheng 已提交
35
  uint8_t              rbit;
H
Hongze Cheng 已提交
36 37 38
  int32_t              pinRef;
  pg_free_dlist_node_t free;
  pg_hash_dlist_node_t hash;
H
refact  
Hongze Cheng 已提交
39
  void *               p;
H
refact  
Hongze Cheng 已提交
40
} pg_t;
H
Hongze Cheng 已提交
41

H
refact  
Hongze Cheng 已提交
42
typedef TD_DLIST(pg_t) pg_list_t;
H
Hongze Cheng 已提交
43 44 45 46
typedef struct {
  SRWLatch latch;
  TD_DLIST(TDB_MPFILE);
} mpf_bucket_t;
H
Hongze Cheng 已提交
47
struct TDB_MPOOL {
H
refact  
Hongze Cheng 已提交
48 49 50
  int64_t   cachesize;
  pgsize_t  pgsize;
  int32_t   npages;
H
refact  
Hongze Cheng 已提交
51
  pg_t *    pages;
H
refact  
Hongze Cheng 已提交
52
  pg_list_t freeList;
H
Hongze Cheng 已提交
53 54 55 56
  struct {
    int32_t    nbucket;
    pg_list_t *hashtab;
  } pgtab;  // page table, hash<pgid_t, pg_t>
H
Hongze Cheng 已提交
57
  struct {
H
Hongze Cheng 已提交
58 59 60
#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 已提交
61
};
H
more  
Hongze Cheng 已提交
62

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

H
Hongze Cheng 已提交
65
typedef TD_DLIST_NODE(TDB_MPFILE) td_mpf_dlist_node_t;
H
Hongze Cheng 已提交
66
struct TDB_MPFILE {
H
Hongze Cheng 已提交
67 68 69 70 71
  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 已提交
72
};
H
Hongze Cheng 已提交
73

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

H
Hongze Cheng 已提交
79
// TDB_MPFILE
H
Hongze Cheng 已提交
80 81
int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp);
int tdbMPoolFileClose(TDB_MPFILE *mpf);
H
Hongze Cheng 已提交
82 83 84 85
int tdbMPoolFileNewPage(TDB_MPFILE *mpf, pgno_t *pgno, void *addr);
int tdbMPoolFileFreePage(TDB_MPOOL *mpf, pgno_t *pgno, void *addr);
int tdbMPoolFileGetPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr);
int tdbMPoolFilePutPage(TDB_MPOOL *mpf, pgno_t pgno, void *addr);
H
Hongze Cheng 已提交
86

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

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