tdb_mpool.h 2.4 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_list_node_t, pg_hash_list_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_list_node_t free;
  pg_hash_list_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
struct TDB_MPOOL {
H
refact  
Hongze Cheng 已提交
45 46 47
  int64_t   cachesize;
  pgsize_t  pgsize;
  int32_t   npages;
H
Hongze Cheng 已提交
48
  pg_t **   pages;
H
refact  
Hongze Cheng 已提交
49
  pg_list_t freeList;
H
Hongze Cheng 已提交
50 51 52 53
  struct {
    int32_t    nbucket;
    pg_list_t *hashtab;
  } pgtab;  // page table, hash<pgid_t, pg_t>
H
Hongze Cheng 已提交
54 55 56 57
  struct {
    int32_t nbucket;
    TD_DLIST(TDB_MPFILE) hashtab[8];
  } mpftab;
H
Hongze Cheng 已提交
58
};
H
more  
Hongze Cheng 已提交
59

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

H
Hongze Cheng 已提交
62
struct TDB_MPFILE {
H
Hongze Cheng 已提交
63 64
  char *     fname;                    // file name
  int        fd;                       // fd
H
more  
Hongze Cheng 已提交
65 66
  uint8_t    fileid[TDB_FILE_ID_LEN];  // file ID
  TDB_MPOOL *mp;                       // underlying memory pool
H
Hongze Cheng 已提交
67
  TD_DLIST_NODE(TDB_MPFILE);
H
Hongze Cheng 已提交
68
};
H
Hongze Cheng 已提交
69

H
Hongze Cheng 已提交
70 71
/*=================================================== Exposed apis ==================================================*/
// TDB_MPOOL
H
Hongze Cheng 已提交
72 73
int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize);
int tdbMPoolClose(TDB_MPOOL *mp);
H
Hongze Cheng 已提交
74

H
Hongze Cheng 已提交
75
// TDB_MPFILE
H
Hongze Cheng 已提交
76 77 78 79
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 已提交
80

H
more  
Hongze Cheng 已提交
81 82 83 84
#ifdef __cplusplus
}
#endif

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