tdbEnv.c 4.2 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#include "tdbInt.h"

struct STDbEnv {
H
Hongze Cheng 已提交
19
  char *      rootDir;    // root directory of the environment
H
Hongze Cheng 已提交
20
  char *      jname;      // journal file name
21
  TdFilePtr   jpFile;        // journal file fd
H
Hongze Cheng 已提交
22 23
  pgsz_t      pgSize;     // page size
  cachesz_t   cacheSize;  // total cache size
H
Hongze Cheng 已提交
24 25 26
  STDbList    dbList;     // TDB List
  SPgFileList pgfList;    // SPgFile List
  SPgCache *  pPgCache;   // page cache
H
Hongze Cheng 已提交
27
  struct {
H
Hongze Cheng 已提交
28 29
#define TDB_ENV_PGF_HASH_BUCKETS 17
    SPgFileList buckets[TDB_ENV_PGF_HASH_BUCKETS];
H
Hongze Cheng 已提交
30
  } pgfht;  // page file hash table;
H
Hongze Cheng 已提交
31 32
};

33
#define TDB_ENV_PGF_HASH(fileid) (((uint8_t *)(fileid))[0] + ((uint8_t *)(fileid))[1] + ((uint8_t *)(fileid))[2])
H
Hongze Cheng 已提交
34

H
Hongze Cheng 已提交
35 36
static int tdbEnvDestroy(TENV *pEnv);

H
Hongze Cheng 已提交
37 38 39
int tdbEnvCreate(TENV **ppEnv, const char *rootDir) {
  TENV * pEnv;
  size_t slen;
H
Hongze Cheng 已提交
40
  size_t jlen;
H
Hongze Cheng 已提交
41

H
Hongze Cheng 已提交
42 43 44 45
  ASSERT(rootDir != NULL);

  *ppEnv = NULL;
  slen = strlen(rootDir);
H
Hongze Cheng 已提交
46 47
  jlen = slen + strlen(TDB_JOURNAL_NAME) + 1;
  pEnv = (TENV *)calloc(1, sizeof(*pEnv) + slen + 1 + jlen + 1);
H
Hongze Cheng 已提交
48 49 50 51
  if (pEnv == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
52 53
  pEnv->rootDir = (char *)(&pEnv[1]);
  pEnv->jname = pEnv->rootDir + slen + 1;
54
  pEnv->jpFile = NULL;
H
Hongze Cheng 已提交
55 56 57
  pEnv->pgSize = TDB_DEFAULT_PGSIZE;
  pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE;

H
Hongze Cheng 已提交
58
  memcpy(pEnv->rootDir, rootDir, slen);
H
Hongze Cheng 已提交
59
  pEnv->rootDir[slen] = '\0';
H
Hongze Cheng 已提交
60
  sprintf(pEnv->jname, "%s/%s", rootDir, TDB_JOURNAL_NAME);
H
Hongze Cheng 已提交
61

H
Hongze Cheng 已提交
62 63
  TD_DLIST_INIT(&(pEnv->dbList));
  TD_DLIST_INIT(&(pEnv->pgfList));
H
Hongze Cheng 已提交
64 65 66 67

  /* TODO */

  *ppEnv = pEnv;
H
Hongze Cheng 已提交
68 69 70
  return 0;
}

H
Hongze Cheng 已提交
71
int tdbEnvOpen(TENV *pEnv) {
H
Hongze Cheng 已提交
72 73 74
  SPgCache *pPgCache;
  int       ret;

H
Hongze Cheng 已提交
75
  ASSERT(pEnv != NULL);
H
Hongze Cheng 已提交
76

H
Hongze Cheng 已提交
77 78 79 80 81
  /* TODO: here we do not need to create the root directory, more
   * work should be done here
   */
  mkdir(pEnv->rootDir, 0755);

H
Hongze Cheng 已提交
82
  ret = pgCacheOpen(&pPgCache, pEnv);
H
Hongze Cheng 已提交
83 84 85
  if (ret != 0) {
    goto _err;
  }
H
Hongze Cheng 已提交
86

H
Hongze Cheng 已提交
87
  pEnv->pPgCache = pPgCache;
H
Hongze Cheng 已提交
88
  return 0;
H
Hongze Cheng 已提交
89 90 91

_err:
  return -1;
H
Hongze Cheng 已提交
92 93 94
}

int tdbEnvClose(TENV *pEnv) {
H
Hongze Cheng 已提交
95
  if (pEnv == NULL) return 0;
H
Hongze Cheng 已提交
96
  pgCacheClose(pEnv->pPgCache);
H
Hongze Cheng 已提交
97
  tdbEnvDestroy(pEnv);
H
Hongze Cheng 已提交
98 99 100
  return 0;
}

H
Hongze Cheng 已提交
101 102 103 104
int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize) {
  if (!TDB_IS_PGSIZE_VLD(pgSize) || cacheSize / pgSize < 10) {
    return -1;
  }
H
Hongze Cheng 已提交
105 106

  /* TODO */
H
Hongze Cheng 已提交
107 108 109 110

  pEnv->pgSize = pgSize;
  pEnv->cacheSize = cacheSize;

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

H
Hongze Cheng 已提交
114
pgsz_t tdbEnvGetPageSize(TENV *pEnv) { return pEnv->pgSize; }
H
Hongze Cheng 已提交
115 116 117

cachesz_t tdbEnvGetCacheSize(TENV *pEnv) { return pEnv->cacheSize; }

H
Hongze Cheng 已提交
118
SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) {
H
Hongze Cheng 已提交
119 120 121 122 123 124 125 126 127
  SPgFileList *pBucket;
  SPgFile *    pPgFile;

  pBucket = pEnv->pgfht.buckets + (TDB_ENV_PGF_HASH(fileid) % TDB_ENV_PGF_HASH_BUCKETS);  // TODO
  for (pPgFile = TD_DLIST_HEAD(pBucket); pPgFile != NULL; pPgFile = TD_DLIST_NODE_NEXT_WITH_FIELD(pPgFile, envHash)) {
    if (memcmp(fileid, pPgFile->fileid, TDB_FILE_ID_LEN) == 0) break;
  };

  return pPgFile;
H
Hongze Cheng 已提交
128 129
}

H
Hongze Cheng 已提交
130
SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return pEnv->pPgCache; }
H
Hongze Cheng 已提交
131 132 133 134

static int tdbEnvDestroy(TENV *pEnv) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
135 136 137
}

int tdbEnvBeginTxn(TENV *pEnv) {
138
  pEnv->jpFile = taosOpenFile(pEnv->jname, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_READ);
139
  if (pEnv->jpFile == NULL) {
H
Hongze Cheng 已提交
140 141 142
    return -1;
  }

H
Hongze Cheng 已提交
143 144 145 146
  return 0;
}

int tdbEnvCommit(TENV *pEnv) {
H
Hongze Cheng 已提交
147
  /* TODO */
148 149
  taosCloseFile(&pEnv->jpFile);
  pEnv->jpFile = NULL;
H
Hongze Cheng 已提交
150
  return 0;
H
Hongze Cheng 已提交
151 152 153
}

const char *tdbEnvGetRootDir(TENV *pEnv) { return pEnv->rootDir; }
H
Hongze Cheng 已提交
154 155 156 157

int tdbEnvRgstPageFile(TENV *pEnv, SPgFile *pPgFile) {
  SPgFileList *pBucket;

H
Hongze Cheng 已提交
158 159 160
  TD_DLIST_APPEND_WITH_FIELD(&(pEnv->pgfList), pPgFile, envPgfList);

  pBucket = pEnv->pgfht.buckets + (TDB_ENV_PGF_HASH(pPgFile->fileid) % TDB_ENV_PGF_HASH_BUCKETS);  // TODO
H
Hongze Cheng 已提交
161 162
  TD_DLIST_APPEND_WITH_FIELD(pBucket, pPgFile, envHash);

H
Hongze Cheng 已提交
163 164 165 166 167
  return 0;
}

int tdbEnvRgstDB(TENV *pEnv, TDB *pDb) {
  // TODO
H
Hongze Cheng 已提交
168 169
  return 0;
}