pgcache.c 3.9 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
static void pgCachePinPage(SPage *pPage);
H
Hongze Cheng 已提交
18
static void pgCacheUnpinPage(SPage *pPage);
H
Hongze Cheng 已提交
19

H
Hongze Cheng 已提交
20
int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage) {
H
Hongze Cheng 已提交
21
  SPgCache *pPgCache;
H
Hongze Cheng 已提交
22 23 24 25 26 27 28
  SPage *   pPage;

  *ppPgCache = NULL;

  if (!TDB_IS_PGSIZE_VLD(pgSize)) {
    return -1;
  }
H
Hongze Cheng 已提交
29 30 31 32 33 34

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

H
Hongze Cheng 已提交
35
  taosInitRWLatch(&(pPgCache->mutex));
H
Hongze Cheng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  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 已提交
68

H
Hongze Cheng 已提交
69
  *ppPgCache = pPgCache;
H
Hongze Cheng 已提交
70 71 72 73
  return 0;
}

int pgCacheDestroy(SPgCache *pPgCache) {
H
Hongze Cheng 已提交
74
  SPage *pPage;
H
Hongze Cheng 已提交
75
  if (pPgCache) {
H
Hongze Cheng 已提交
76 77 78 79 80 81 82 83 84
    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 已提交
85 86 87
    free(pPgCache);
  }

H
Hongze Cheng 已提交
88 89 90
  return 0;
}

H
Hongze Cheng 已提交
91 92 93 94 95 96
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 已提交
97 98 99 100 101 102 103 104 105
  // TODO
  return 0;
}

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

H
Hongze Cheng 已提交
106 107 108 109 110 111
#define PG_CACHE_HASH(fileid, pgno)       \
  ({                                      \
    uint64_t *tmp = (uint64_t *)(fileid); \
    (tmp[0] + tmp[1] + tmp[2] + (pgno));  \
  })

H
Hongze Cheng 已提交
112
SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) {
H
Hongze Cheng 已提交
113 114 115 116 117
  SPage *  pPage;
  SPgFile *pPgFile;
  SPgList *pBucket;

  // 1. Search the page hash table SPgCache.pght
H
Hongze Cheng 已提交
118
  pBucket = pPgCache->pght.buckets + (PG_CACHE_HASH(pgid.fileid, pgid.pgno) % pPgCache->pght.nbucket);
H
Hongze Cheng 已提交
119 120 121 122 123 124
  pPage = TD_DLIST_HEAD(pBucket);
  while (pPage && tdbCmprPgId(&(pPage->pgid), &pgid)) {
    pPage = TD_DLIST_NODE_NEXT_WITH_FIELD(pPage, pghtNode);
  }

  if (pPage) {
H
Hongze Cheng 已提交
125
    // Page is found, pin the page and return the page
H
Hongze Cheng 已提交
126 127 128 129
    pgCachePinPage(pPage);
    return pPage;
  }

H
Hongze Cheng 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  // 2. Check the free list
  pPage = TD_DLIST_HEAD(&(pPgCache->freeList));
  if (pPage) {
    TD_DLIST_POP_WITH_FIELD(&(pPgCache->freeList), pPage, freeNode);
    pgCachePinPage(pPage);
    return pPage;
  }

  // 3. Try to recycle a page from the LRU list
  pPage = TD_DLIST_HEAD(&(pPgCache->lru));
  if (pPage) {
    TD_DLIST_POP_WITH_FIELD(&(pPgCache->lru), pPage, lruNode);
    // TODO: remove from the hash table
    pgCachePinPage(pPage);
    return pPage;
  }

  // 4. If a memory allocator is set, try to allocate from the allocator (TODO)
H
more  
Hongze Cheng 已提交
148

H
Hongze Cheng 已提交
149 150 151 152 153 154
  return NULL;
}

int pgCacheRelease(SPage *pPage) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
155 156 157 158 159 160 161 162
}

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

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