pgcache.c 3.8 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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 已提交
15 16
#include "tdbInt.h"

H
Hongze Cheng 已提交
17
typedef TD_DLIST_NODE(SPage) SPgListNode;
H
Hongze Cheng 已提交
18
struct SPage {
H
Hongze Cheng 已提交
19 20 21
  pgid_t      pgid;      // page id
  frame_id_t  frameid;   // frame id
  SPgListNode freeNode;  // for SPgCache.freeList
H
Hongze Cheng 已提交
22
  SPgListNode pghtNode;  // for pght
H
Hongze Cheng 已提交
23
  uint8_t *   pData;     // real data
H
Hongze Cheng 已提交
24 25 26 27 28
};

typedef TD_DLIST(SPage) SPgList;

struct SPgCache {
H
Hongze Cheng 已提交
29 30
  SRWLatch mutex;
  pgsize_t pgsize;
H
Hongze Cheng 已提交
31
  int32_t  npage;
H
Hongze Cheng 已提交
32 33
  SPage *  pages;
  SPgList  freeList;
H
Hongze Cheng 已提交
34
  struct {
H
Hongze Cheng 已提交
35 36
    int32_t  nbucket;
    SPgList *buckets;
H
Hongze Cheng 已提交
37 38 39
  } pght;  // page hash table
};

H
Hongze Cheng 已提交
40 41
static void pgCachePinPage(SPage *pPage);

H
Hongze Cheng 已提交
42
int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage) {
H
Hongze Cheng 已提交
43
  SPgCache *pPgCache;
H
Hongze Cheng 已提交
44 45 46 47 48 49 50
  SPage *   pPage;

  *ppPgCache = NULL;

  if (!TDB_IS_PGSIZE_VLD(pgSize)) {
    return -1;
  }
H
Hongze Cheng 已提交
51 52 53 54 55 56

  pPgCache = (SPgCache *)calloc(1, sizeof(*pPgCache));
  if (pPgCache == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
57
  taosInitRWLatch(&(pPgCache->mutex));
H
Hongze Cheng 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  pPgCache->pgsize = pgSize;
  pPgCache->npage = npage;

  pPgCache->pages = (SPage *)calloc(npage, sizeof(SPage));
  if (pPgCache->pages == NULL) {
    pgCacheDestroy(pPgCache);
    return -1;
  }

  TD_DLIST_INIT(&(pPgCache->freeList));

  for (int32_t i = 0; i < npage; i++) {
    pPage = pPgCache->pages + i;

    pPage->pgid = TDB_IVLD_PGID;
    pPage->frameid = i;

    pPage->pData = (uint8_t *)calloc(1, pgSize);
    if (pPage->pData == NULL) {
      pgCacheDestroy(pPgCache);
      return -1;
    }

    pPgCache->pght.nbucket = npage;
    pPgCache->pght.buckets = (SPgList *)calloc(pPgCache->pght.nbucket, sizeof(SPgList));
    if (pPgCache->pght.buckets == NULL) {
      pgCacheDestroy(pPgCache);
      return -1;
    }

    TD_DLIST_APPEND_WITH_FIELD(&(pPgCache->freeList), pPage, freeNode);
  }
H
Hongze Cheng 已提交
90

H
Hongze Cheng 已提交
91
  *ppPgCache = pPgCache;
H
Hongze Cheng 已提交
92 93 94 95
  return 0;
}

int pgCacheDestroy(SPgCache *pPgCache) {
H
Hongze Cheng 已提交
96
  SPage *pPage;
H
Hongze Cheng 已提交
97
  if (pPgCache) {
H
Hongze Cheng 已提交
98 99 100 101 102 103 104 105 106
    tfree(pPgCache->pght.buckets);
    if (pPgCache->pages) {
      for (int32_t i = 0; i < pPgCache->npage; i++) {
        pPage = pPgCache->pages + i;
        tfree(pPage->pData);
      }

      free(pPgCache->pages);
    }
H
Hongze Cheng 已提交
107 108 109
    free(pPgCache);
  }

H
Hongze Cheng 已提交
110 111 112
  return 0;
}

H
Hongze Cheng 已提交
113 114 115 116 117 118
int pgCacheOpen(SPgCache **ppPgCache) {
  if (*ppPgCache == NULL) {
    if (pgCacheCreate(ppPgCache, TDB_DEFAULT_PGSIZE, TDB_DEFAULT_CACHE_SIZE / TDB_DEFAULT_PGSIZE) < 0) {
      return -1;
    }
  }
H
Hongze Cheng 已提交
119 120 121 122 123 124 125 126 127
  // TODO
  return 0;
}

int pgCacheClose(SPgCache *pPgCache) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
128 129 130 131 132 133
#define PG_CACHE_HASH(fileid, pgno)       \
  ({                                      \
    uint64_t *tmp = (uint64_t *)(fileid); \
    (tmp[0] + tmp[1] + tmp[2] + (pgno));  \
  })

H
Hongze Cheng 已提交
134
SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) {
H
Hongze Cheng 已提交
135 136 137 138 139
  SPage *  pPage;
  SPgFile *pPgFile;
  SPgList *pBucket;

  // 1. Search the page hash table SPgCache.pght
H
Hongze Cheng 已提交
140
  pBucket = pPgCache->pght.buckets + (PG_CACHE_HASH(pgid.fileid, pgid.pgno) % pPgCache->pght.nbucket);
H
Hongze Cheng 已提交
141 142 143 144 145 146 147 148 149 150 151 152
  pPage = TD_DLIST_HEAD(pBucket);
  while (pPage && tdbCmprPgId(&(pPage->pgid), &pgid)) {
    pPage = TD_DLIST_NODE_NEXT_WITH_FIELD(pPage, pghtNode);
  }

  if (pPage) {
    // Page is found, pin the page (TODO) and return the page
    pgCachePinPage(pPage);
    return pPage;
  }

  // TODO
H
more  
Hongze Cheng 已提交
153

H
Hongze Cheng 已提交
154 155 156 157 158 159
  return NULL;
}

int pgCacheRelease(SPage *pPage) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
160 161 162 163 164 165 166 167
}

static void pgCachePinPage(SPage *pPage) {
  // TODO
}

static void pgCacheUnpinPage(SPage *pPage) {
  // TODO
H
Hongze Cheng 已提交
168
}