sdb.c 3.2 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "sdbInt.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20
SSdb *sdbInit(SSdbOpt *pOption) {
  mDebug("start to init sdb in %s", pOption->path);
S
Shengliang Guan 已提交
21

22 23 24
  SSdb *pSdb = calloc(1, sizeof(SSdb));
  if (pSdb == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
25
    mError("failed to init sdb since %s", terrstr());
26 27
    return NULL;
  }
S
Shengliang Guan 已提交
28

29
  char path[PATH_MAX + 100];
S
Shengliang Guan 已提交
30
  snprintf(path, PATH_MAX + 100, "%s%sdata", pOption->path, TD_DIRSEP);
31
  pSdb->currDir = strdup(path);
S
Shengliang Guan 已提交
32
  snprintf(path, PATH_MAX + 100, "%s%ssync", pOption->path, TD_DIRSEP);
33
  pSdb->syncDir = strdup(path);
S
Shengliang Guan 已提交
34
  snprintf(path, PATH_MAX + 100, "%s%stmp", pOption->path, TD_DIRSEP);
35 36
  pSdb->tmpDir = strdup(path);
  if (pSdb->currDir == NULL || pSdb->currDir == NULL || pSdb->currDir == NULL) {
S
Shengliang Guan 已提交
37
    sdbCleanup(pSdb);
38
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
39
    mError("failed to init sdb since %s", terrstr());
40
    return NULL;
S
Shengliang Guan 已提交
41 42 43
  }

  for (int32_t i = 0; i < SDB_MAX; ++i) {
44
    taosInitRWLatch(&pSdb->locks[i]);
S
Shengliang Guan 已提交
45 46
  }

S
Shengliang Guan 已提交
47
  pSdb->pMnode = pOption->pMnode;
S
Shengliang Guan 已提交
48
  mDebug("sdb init successfully");
S
Shengliang Guan 已提交
49
  return pSdb;
S
Shengliang Guan 已提交
50 51
}

S
Shengliang Guan 已提交
52 53
void sdbCleanup(SSdb *pSdb) {
  mDebug("start to cleanup sdb");
S
Shengliang Guan 已提交
54 55

  if (pSdb->curVer != pSdb->lastCommitVer) {
S
Shengliang Guan 已提交
56
    mDebug("write sdb file for curVer:% " PRId64 " and lastVer:%" PRId64, pSdb->curVer, pSdb->lastCommitVer);
S
Shengliang Guan 已提交
57 58 59
    sdbWriteFile(pSdb);
  }

60 61
  if (pSdb->currDir != NULL) {
    tfree(pSdb->currDir);
S
Shengliang Guan 已提交
62
  }
S
Shengliang Guan 已提交
63

64 65
  if (pSdb->syncDir != NULL) {
    tfree(pSdb->syncDir);
S
Shengliang Guan 已提交
66 67
  }

68 69
  if (pSdb->tmpDir != NULL) {
    tfree(pSdb->tmpDir);
S
Shengliang Guan 已提交
70 71
  }

S
Shengliang Guan 已提交
72
  for (int32_t i = 0; i < SDB_MAX; ++i) {
73
    SHashObj *hash = pSdb->hashObjs[i];
S
Shengliang Guan 已提交
74
    if (hash != NULL) {
S
Shengliang Guan 已提交
75
      taosHashClear(hash);
S
Shengliang Guan 已提交
76 77
      taosHashCleanup(hash);
    }
78
    pSdb->hashObjs[i] = NULL;
S
Shengliang Guan 已提交
79
  }
S
Shengliang Guan 已提交
80

S
Shengliang Guan 已提交
81
  mDebug("sdb is cleaned up");
S
Shengliang Guan 已提交
82 83
}

S
Shengliang Guan 已提交
84
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table) {
S
Shengliang Guan 已提交
85
  ESdbType sdb = table.sdbType;
86 87 88 89 90 91 92
  pSdb->keyTypes[sdb] = table.keyType;
  pSdb->insertFps[sdb] = table.insertFp;
  pSdb->updateFps[sdb] = table.updateFp;
  pSdb->deleteFps[sdb] = table.deleteFp;
  pSdb->deployFps[sdb] = table.deployFp;
  pSdb->encodeFps[sdb] = table.encodeFp;
  pSdb->decodeFps[sdb] = table.decodeFp;
S
Shengliang Guan 已提交
93

S
Shengliang Guan 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  for (int32_t i = 0; i < SDB_MAX; ++i) {
    int32_t type;
    if (pSdb->keyTypes[i] == SDB_KEY_INT32) {
      type = TSDB_DATA_TYPE_INT;
    } else if (pSdb->keyTypes[i] == SDB_KEY_INT64) {
      type = TSDB_DATA_TYPE_BIGINT;
    } else {
      type = TSDB_DATA_TYPE_BINARY;
    }

    SHashObj *hash = taosHashInit(64, taosGetDefaultHashFunction(type), true, HASH_NO_LOCK);
    if (hash == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    pSdb->hashObjs[i] = hash;
    taosInitRWLatch(&pSdb->locks[i]);
  }

  return 0;
S
Shengliang Guan 已提交
115
}