sdb.c 3.4 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

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

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 open 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%scur", 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 37 38
  pSdb->tmpDir = strdup(path);
  if (pSdb->currDir == NULL || pSdb->currDir == NULL || pSdb->currDir == NULL) {
    sdbClose(pSdb);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
39
    mError("failed to open sdb since %s", terrstr());
40
    return NULL;
S
Shengliang Guan 已提交
41 42 43
  }

  for (int32_t i = 0; i < SDB_MAX; ++i) {
S
Shengliang Guan 已提交
44
    int32_t type;
45
    if (pSdb->keyTypes[i] == SDB_KEY_INT32) {
S
Shengliang Guan 已提交
46
      type = TSDB_DATA_TYPE_INT;
47
    } else if (pSdb->keyTypes[i] == SDB_KEY_INT64) {
S
Shengliang Guan 已提交
48 49 50 51 52
      type = TSDB_DATA_TYPE_BIGINT;
    } else {
      type = TSDB_DATA_TYPE_BINARY;
    }

S
Shengliang Guan 已提交
53
    SHashObj *hash = taosHashInit(64, taosGetDefaultHashFunction(type), true, HASH_NO_LOCK);
S
Shengliang Guan 已提交
54
    if (hash == NULL) {
55 56
      sdbClose(pSdb);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
57
      mError("failed to open sdb since %s", terrstr());
58
      return NULL;
S
Shengliang Guan 已提交
59 60
    }

61 62
    pSdb->hashObjs[i] = hash;
    taosInitRWLatch(&pSdb->locks[i]);
S
Shengliang Guan 已提交
63 64
  }

S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74
  int32_t code = sdbReadFile(pSdb);
  if (code != 0) {
    sdbClose(pSdb);
    terrno = code;
    mError("failed to open sdb since %s", terrstr());
    return NULL;
  }

  mDebug("sdb open successfully");
  return pSdb;
S
Shengliang Guan 已提交
75 76
}

77
void sdbClose(SSdb *pSdb) {
S
Shengliang Guan 已提交
78 79 80 81 82 83 84 85
  mDebug("start to close sdb");

  if (pSdb->curVer != pSdb->lastCommitVer) {
    mDebug("start to write sdb file since curVer:% " PRId64 " and lastCommitVer:%" PRId64 " inequal", pSdb->curVer,
           pSdb->lastCommitVer);
    sdbWriteFile(pSdb);
  }

86 87
  if (pSdb->currDir != NULL) {
    tfree(pSdb->currDir);
S
Shengliang Guan 已提交
88
  }
S
Shengliang Guan 已提交
89

90 91
  if (pSdb->syncDir != NULL) {
    tfree(pSdb->syncDir);
S
Shengliang Guan 已提交
92 93
  }

94 95
  if (pSdb->tmpDir != NULL) {
    tfree(pSdb->tmpDir);
S
Shengliang Guan 已提交
96 97
  }

S
Shengliang Guan 已提交
98
  for (int32_t i = 0; i < SDB_MAX; ++i) {
99
    SHashObj *hash = pSdb->hashObjs[i];
S
Shengliang Guan 已提交
100
    if (hash != NULL) {
S
Shengliang Guan 已提交
101
      taosHashClear(hash);
S
Shengliang Guan 已提交
102 103
      taosHashCleanup(hash);
    }
104
    pSdb->hashObjs[i] = NULL;
S
Shengliang Guan 已提交
105
  }
S
Shengliang Guan 已提交
106 107

  mDebug("sdb is closed");
S
Shengliang Guan 已提交
108 109
}

110
void sdbSetTable(SSdb *pSdb, SSdbTable table) {
S
Shengliang Guan 已提交
111
  ESdbType sdb = table.sdbType;
112 113 114 115 116 117 118
  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 已提交
119 120

  mDebug("set sdb handle of table %d", pSdb, table);
S
Shengliang Guan 已提交
121
}