tdb_mpool.c 2.9 KB
Newer Older
H
refact  
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
#include "tdb_mpool.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19
static int tdbGnrtFileID(const char *fname, uint8_t *fileid);

H
Hongze Cheng 已提交
20
int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) {
H
Hongze Cheng 已提交
21
  TDB_MPOOL *mp = NULL;
H
Hongze Cheng 已提交
22
  size_t     tsize;
H
refact  
Hongze Cheng 已提交
23
  pg_t *     pagep;
H
Hongze Cheng 已提交
24 25 26 27 28 29 30 31 32 33 34

  // check parameters
  if (!TDB_IS_PGSIZE_VLD(pgsize)) {
    tdbError("invalid page size");
    return -1;
  }

  // allocate handle
  mp = (TDB_MPOOL *)calloc(1, sizeof(*mp));
  if (mp == NULL) {
    tdbError("failed to malloc memory pool handle");
H
Hongze Cheng 已提交
35
    goto _err;
H
Hongze Cheng 已提交
36 37 38 39 40 41
  }

  // initialize the handle
  mp->cachesize = cachesize;
  mp->pgsize = pgsize;
  mp->npages = cachesize / pgsize;
H
Hongze Cheng 已提交
42 43 44 45

  TD_DLIST_INIT(&mp->freeList);

  mp->pages = (pg_t **)calloc(mp->npages, sizeof(pg_t *));
H
Hongze Cheng 已提交
46 47
  if (mp->pages == NULL) {
    tdbError("failed to malloc memory pool pages");
H
Hongze Cheng 已提交
48
    goto _err;
H
Hongze Cheng 已提交
49 50
  }

H
Hongze Cheng 已提交
51 52 53 54 55
  for (frame_id_t i = 0; i < mp->npages; i++) {
    mp->pages[i] = (pg_t *)calloc(1, MP_PAGE_SIZE(pgsize));
    if (mp->pages[i] == NULL) {
      goto _err;
    }
H
Hongze Cheng 已提交
56

H
Hongze Cheng 已提交
57 58 59 60 61 62
    taosInitRWLatch(&mp->pages[i]->rwLatch);
    mp->pages[i]->frameid = i;
    mp->pages[i]->pgid = TDB_IVLD_PGID;

    // TODO: add the new page to the free list
    // TD_DLIST_APPEND(&mp->freeList, mp->pages[i]);
H
Hongze Cheng 已提交
63 64
  }

H
Hongze Cheng 已提交
65 66 67 68 69 70
#define PGTAB_FACTOR 1.0
  mp->pgtab.nbucket = mp->npages / PGTAB_FACTOR;
  mp->pgtab.hashtab = (pg_list_t *)calloc(mp->pgtab.nbucket, sizeof(pg_list_t));
  if (mp->pgtab.hashtab == NULL) {
    tdbError("failed to malloc memory pool hash table");
    goto _err;
H
Hongze Cheng 已提交
71 72 73 74 75
  }

  // return
  *mpp = mp;
  return 0;
H
Hongze Cheng 已提交
76 77 78 79 80

_err:
  tdbMPoolClose(mp);
  *mpp = NULL;
  return -1;
H
Hongze Cheng 已提交
81 82
}

H
Hongze Cheng 已提交
83
int tdbMPoolClose(TDB_MPOOL *mp) {
H
Hongze Cheng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  if (mp) {
    tfree(mp->pgtab.hashtab);
    if (mp->pages) {
      for (int i = 0; i < mp->npages; i++) {
        tfree(mp->pages[i]);
      }

      free(mp->pages);
    }

    free(mp);
  }
  return 0;
}

int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) {
  TDB_MPFILE *mpf;

  if ((mpf = (TDB_MPFILE *)calloc(1, sizeof(*mpf))) == NULL) {
    return -1;
  }

  mpf->fd = -1;

  if ((mpf->fname = strdup(fname)) == NULL) {
    goto _err;
  }

  if ((mpf->fd = open(fname, O_CREAT | O_RDWR, 0755)) < 0) {
    goto _err;
  }

  *mpfp = mpf;
  return 0;

_err:
  tdbMPoolFileClose(mpf);
  *mpfp = NULL;
  return -1;
}

int tdbMPoolFileClose(TDB_MPFILE *mpf) {
  // TODO
  return 0;
}

static int tdbGnrtFileID(const char *fname, uint8_t *fileid) {
H
Hongze Cheng 已提交
131 132 133
  // TODO
  return 0;
}