sdb.c 3.8 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 (ESdbType 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

S
Shengliang Guan 已提交
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
    sdbWriteFile(pSdb);
S
Shengliang Guan 已提交
58
  // }
S
Shengliang Guan 已提交
59

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
  }

72
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
73
    SHashObj *hash = pSdb->hashObjs[i];
74 75 76
    if (hash == NULL) continue;

    SdbDeleteFp deleteFp = pSdb->deleteFps[i];
S
Shengliang Guan 已提交
77
    SSdbRow   **ppRow = taosHashIterate(hash, NULL);
78 79 80 81 82 83 84 85 86
    while (ppRow != NULL) {
      SSdbRow *pRow = *ppRow;
      if (pRow == NULL) continue;

      if (deleteFp != NULL) {
        (*deleteFp)(pSdb, pRow->pObj);
      }
      sdbFreeRow(pRow);
      ppRow = taosHashIterate(hash, ppRow);
S
Shengliang Guan 已提交
87
    }
88 89 90 91 92 93 94 95
  }

  for (ESdbType i = 0; i < SDB_MAX; ++i) {
    SHashObj *hash = pSdb->hashObjs[i];
    if (hash == NULL) continue;

    taosHashClear(hash);
    taosHashCleanup(hash);
96
    pSdb->hashObjs[i] = NULL;
S
Shengliang Guan 已提交
97
    mDebug("sdb table:%d is cleaned up", i);
S
Shengliang Guan 已提交
98
  }
S
Shengliang Guan 已提交
99

S
Shengliang Guan 已提交
100
  free(pSdb);
S
Shengliang Guan 已提交
101
  mDebug("sdb is cleaned up");
S
Shengliang Guan 已提交
102 103
}

S
Shengliang Guan 已提交
104
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table) {
S
Shengliang Guan 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  ESdbType sdbType = table.sdbType;
  EKeyType keyType = table.keyType;
  pSdb->keyTypes[sdbType] = table.keyType;
  pSdb->insertFps[sdbType] = table.insertFp;
  pSdb->updateFps[sdbType] = table.updateFp;
  pSdb->deleteFps[sdbType] = table.deleteFp;
  pSdb->deployFps[sdbType] = table.deployFp;
  pSdb->encodeFps[sdbType] = table.encodeFp;
  pSdb->decodeFps[sdbType] = table.decodeFp;

  int32_t hashType = 0;
  if (keyType == SDB_KEY_INT32) {
    hashType = TSDB_DATA_TYPE_INT;
  } else if (keyType == SDB_KEY_INT64) {
    hashType = TSDB_DATA_TYPE_BIGINT;
  } else {
    hashType = TSDB_DATA_TYPE_BINARY;
  }
S
Shengliang Guan 已提交
123

S
Shengliang Guan 已提交
124 125 126 127
  SHashObj *hash = taosHashInit(64, taosGetDefaultHashFunction(hashType), true, HASH_NO_LOCK);
  if (hash == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
Shengliang Guan 已提交
128 129
  }

S
Shengliang Guan 已提交
130 131
  pSdb->hashObjs[sdbType] = hash;
  taosInitRWLatch(&pSdb->locks[sdbType]);
S
Shengliang Guan 已提交
132
  mDebug("sdb table:%d is initialized", sdbType);
S
Shengliang Guan 已提交
133

S
Shengliang Guan 已提交
134
  return 0;
S
Shengliang Guan 已提交
135
}