sdb.c 2.5 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
#include "tglobal.h"
S
Shengliang Guan 已提交
19

S
Shengliang Guan 已提交
20
SSdbMgr tsSdb = {0};
S
Shengliang Guan 已提交
21 22

int32_t sdbInit() {
S
Shengliang Guan 已提交
23 24
  char path[PATH_MAX + 100];

S
Shengliang Guan 已提交
25
  snprintf(path, PATH_MAX + 100, "%s%scur%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP);
S
Shengliang Guan 已提交
26
  tsSdb.currDir = strdup(path);
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28 29 30 31 32 33 34
  snprintf(path, PATH_MAX + 100, "%s%ssync%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP);
  tsSdb.syncDir = strdup(path);

  snprintf(path, PATH_MAX + 100, "%s%stmp%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP);
  tsSdb.tmpDir = strdup(path);

  if (tsSdb.currDir == NULL || tsSdb.currDir == NULL || tsSdb.currDir == NULL) {
S
Shengliang Guan 已提交
35
    return TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
36 37 38
  }

  for (int32_t i = 0; i < SDB_MAX; ++i) {
S
Shengliang Guan 已提交
39
    int32_t type;
S
Shengliang Guan 已提交
40
    if (tsSdb.keyTypes[i] == SDB_KEY_INT32) {
S
Shengliang Guan 已提交
41
      type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
42
    } else if (tsSdb.keyTypes[i] == SDB_KEY_INT64) {
S
Shengliang Guan 已提交
43 44 45 46 47 48 49
      type = TSDB_DATA_TYPE_BIGINT;
    } else {
      type = TSDB_DATA_TYPE_BINARY;
    }

    SHashObj *hash = taosHashInit(128, taosGetDefaultHashFunction(type), true, HASH_NO_LOCK);
    if (hash == NULL) {
S
Shengliang Guan 已提交
50
      return TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
51 52
    }

S
Shengliang Guan 已提交
53 54
    tsSdb.hashObjs[i] = hash;
    taosInitRWLatch(&tsSdb.locks[i]);
S
Shengliang Guan 已提交
55 56 57 58 59 60
  }

  return 0;
}

void sdbCleanup() {
S
Shengliang Guan 已提交
61 62
  if (tsSdb.curVer != tsSdb.lastCommitVer) {
    sdbCommit();
S
Shengliang Guan 已提交
63 64
  }

S
Shengliang Guan 已提交
65 66 67
  if (tsSdb.currDir != NULL) {
    tfree(tsSdb.currDir);
  }
S
Shengliang Guan 已提交
68

S
Shengliang Guan 已提交
69 70
  if (tsSdb.syncDir != NULL) {
    tfree(tsSdb.syncDir);
S
Shengliang Guan 已提交
71 72
  }

S
Shengliang Guan 已提交
73 74
  if (tsSdb.tmpDir != NULL) {
    tfree(tsSdb.tmpDir);
S
Shengliang Guan 已提交
75 76
  }

S
Shengliang Guan 已提交
77 78 79 80 81 82 83
  for (int32_t i = 0; i < SDB_MAX; ++i) {
    SHashObj *hash = tsSdb.hashObjs[i];
    if (hash != NULL) {
      taosHashCleanup(hash);
    }
    tsSdb.hashObjs[i] = NULL;
  }
S
Shengliang Guan 已提交
84 85
}

S
Shengliang Guan 已提交
86 87 88 89 90 91 92 93 94
void sdbSetTable(SSdbTable table) {
  ESdbType sdb = table.sdbType;
  tsSdb.keyTypes[sdb] = table.keyType;
  tsSdb.insertFps[sdb] = table.insertFp;
  tsSdb.updateFps[sdb] = table.updateFp;
  tsSdb.deleteFps[sdb] = table.deleteFp;
  tsSdb.deployFps[sdb] = table.deployFp;
  tsSdb.encodeFps[sdb] = table.encodeFp;
  tsSdb.decodeFps[sdb] = table.decodeFp;
S
Shengliang Guan 已提交
95
}