sdb.c 3.1 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", pOption->path);
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
  mDebug("sdb init successfully");
S
Shengliang Guan 已提交
48
  return pSdb;
S
Shengliang Guan 已提交
49 50
}

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
83
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table) {
S
Shengliang Guan 已提交
84
  ESdbType sdb = table.sdbType;
85 86 87 88 89 90 91
  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 已提交
92

S
Shengliang Guan 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  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 已提交
114
}