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

S
Shengliang Guan 已提交
19 20
static int32_t sdbCreateDir(SSdb *pSdb);

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

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

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

S
Shengliang Guan 已提交
45 46 47 48 49
  if (sdbCreateDir(pSdb) != 0) {
    sdbCleanup(pSdb);
    return NULL;
  }

50
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
51
    taosInitRWLatch(&pSdb->locks[i]);
S
Shengliang Guan 已提交
52 53
  }

S
Shengliang Guan 已提交
54
  pSdb->pMnode = pOption->pMnode;
S
Shengliang Guan 已提交
55
  mDebug("sdb init successfully");
S
Shengliang Guan 已提交
56
  return pSdb;
S
Shengliang Guan 已提交
57 58
}

S
Shengliang Guan 已提交
59 60
void sdbCleanup(SSdb *pSdb) {
  mDebug("start to cleanup sdb");
S
Shengliang Guan 已提交
61

S
Shengliang Guan 已提交
62
  // if (pSdb->curVer != pSdb->lastCommitVer) {
S
Shengliang Guan 已提交
63 64
  mDebug("write sdb file for curVer:% " PRId64 " and lastVer:%" PRId64, pSdb->curVer, pSdb->lastCommitVer);
  sdbWriteFile(pSdb);
S
Shengliang Guan 已提交
65
  // }
S
Shengliang Guan 已提交
66

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

71 72
  if (pSdb->syncDir != NULL) {
    tfree(pSdb->syncDir);
S
Shengliang Guan 已提交
73 74
  }

75 76
  if (pSdb->tmpDir != NULL) {
    tfree(pSdb->tmpDir);
S
Shengliang Guan 已提交
77 78
  }

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

    SdbDeleteFp deleteFp = pSdb->deleteFps[i];
S
Shengliang Guan 已提交
84
    SSdbRow   **ppRow = taosHashIterate(hash, NULL);
85 86 87 88 89 90 91 92 93
    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 已提交
94
    }
95 96 97 98 99 100 101 102
  }

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

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

S
Shengliang Guan 已提交
107
  free(pSdb);
S
Shengliang Guan 已提交
108
  mDebug("sdb is cleaned up");
S
Shengliang Guan 已提交
109 110
}

S
Shengliang Guan 已提交
111
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table) {
S
Shengliang Guan 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  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 已提交
130

S
Shengliang Guan 已提交
131 132 133 134
  SHashObj *hash = taosHashInit(64, taosGetDefaultHashFunction(hashType), true, HASH_NO_LOCK);
  if (hash == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
Shengliang Guan 已提交
135 136
  }

S
Shengliang Guan 已提交
137
  pSdb->maxId[sdbType] = 0;
S
Shengliang Guan 已提交
138 139
  pSdb->hashObjs[sdbType] = hash;
  taosInitRWLatch(&pSdb->locks[sdbType]);
S
Shengliang Guan 已提交
140
  mDebug("sdb table:%d is initialized", sdbType);
S
Shengliang Guan 已提交
141

S
Shengliang Guan 已提交
142
  return 0;
S
Shengliang Guan 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
}

static int32_t sdbCreateDir(SSdb *pSdb) {
  if (taosMkDir(pSdb->currDir) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    mError("failed to create dir:%s since %s", pSdb->currDir, terrstr());
    return -1;
  }

  if (taosMkDir(pSdb->syncDir) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    mError("failed to create dir:%s since %s", pSdb->syncDir, terrstr());
    return -1;
  }

  if (taosMkDir(pSdb->tmpDir) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    mError("failed to create dir:%s since %s", pSdb->tmpDir, terrstr());
    return -1;
  }

  return 0;
}