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

M
Minglei Jin 已提交
18 19
int32_t tsdbSetKeepCfg(STsdb *pTsdb, STsdbCfg *pCfg) {
  STsdbKeepCfg *pKeepCfg = &pTsdb->keepCfg;
C
Cary Xu 已提交
20
  pKeepCfg->precision = pCfg->precision;
21 22 23 24
  pKeepCfg->days = pCfg->days;
  pKeepCfg->keep0 = pCfg->keep0;
  pKeepCfg->keep1 = pCfg->keep1;
  pKeepCfg->keep2 = pCfg->keep2;
C
Cary Xu 已提交
25 26 27
  return 0;
}

C
Cary Xu 已提交
28
/**
C
Cary Xu 已提交
29 30 31 32 33 34
 * @brief
 *
 * @param pVnode
 * @param ppTsdb
 * @param dir
 * @return int
C
Cary Xu 已提交
35
 */
36
int tsdbOpen(SVnode *pVnode, STsdb **ppTsdb, const char *dir, STsdbKeepCfg *pKeepCfg) {
H
Hongze Cheng 已提交
37 38 39 40
  STsdb *pTsdb = NULL;
  int    slen = 0;

  *ppTsdb = NULL;
H
Hongze Cheng 已提交
41
  slen = strlen(pVnode->path) + strlen(dir) + 2;
H
Hongze Cheng 已提交
42 43 44 45 46 47 48 49 50

  // create handle
  pTsdb = (STsdb *)taosMemoryCalloc(1, sizeof(*pTsdb) + slen);
  if (pTsdb == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  pTsdb->path = (char *)&pTsdb[1];
H
Hongze Cheng 已提交
51
  sprintf(pTsdb->path, "%s%s%s", pVnode->path, TD_DIRSEP, dir);
wafwerar's avatar
wafwerar 已提交
52
  taosRealPath(pTsdb->path, NULL, slen);
H
Hongze Cheng 已提交
53
  pTsdb->pVnode = pVnode;
H
Hongze Cheng 已提交
54
  taosThreadRwlockInit(&pTsdb->rwLock, NULL);
55
  if (!pKeepCfg) {
M
Minglei Jin 已提交
56
    tsdbSetKeepCfg(pTsdb, &pVnode->config.tsdbCfg);
57 58 59
  } else {
    memcpy(&pTsdb->keepCfg, pKeepCfg, sizeof(STsdbKeepCfg));
  }
H
Hongze Cheng 已提交
60
  // pTsdb->fs = tsdbNewFS(REPO_KEEP_CFG(pTsdb));
H
Hongze Cheng 已提交
61

H
Hongze Cheng 已提交
62 63
  // create dir
  tfsMkdir(pVnode->pTfs, pTsdb->path);
H
Hongze Cheng 已提交
64 65

  // open tsdb
H
Hongze Cheng 已提交
66
  if (tsdbFSOpen(pTsdb) < 0) {
H
Hongze Cheng 已提交
67 68 69
    goto _err;
  }

70 71 72 73
  if (tsdbOpenCache(pTsdb) < 0) {
    goto _err;
  }

S
Shengliang Guan 已提交
74
  tsdbDebug("vgId:%d, tsdb is opened at %s, days:%d, keep:%d,%d,%d", TD_VID(pVnode), pTsdb->path, pTsdb->keepCfg.days,
C
Cary Xu 已提交
75
            pTsdb->keepCfg.keep0, pTsdb->keepCfg.keep1, pTsdb->keepCfg.keep2);
H
Hongze Cheng 已提交
76 77 78 79 80 81 82 83 84

  *ppTsdb = pTsdb;
  return 0;

_err:
  taosMemoryFree(pTsdb);
  return -1;
}

85 86
int tsdbClose(STsdb **pTsdb) {
  if (*pTsdb) {
H
Hongze Cheng 已提交
87
    taosThreadRwlockDestroy(&(*pTsdb)->rwLock);
H
Hongze Cheng 已提交
88
    tsdbFSClose(*pTsdb);
89
    tsdbCloseCache((*pTsdb)->lruCache);
H
Hongze Cheng 已提交
90
    taosMemoryFreeClear(*pTsdb);
H
Hongze Cheng 已提交
91 92 93
  }
  return 0;
}