• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.12
/source/dnode/mnode/sdb/src/sdb.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "sdb.h"
18

19
static int32_t sdbCreateDir(SSdb *pSdb);
20

21
SSdb *sdbInit(SSdbOpt *pOption) {
32✔
22
  mInfo("start to init sdb in %s", pOption->path);
32✔
23

24
  SSdb *pSdb = taosMemoryCalloc(1, sizeof(SSdb));
32✔
25
  if (pSdb == NULL) {
32✔
26
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
27
    mError("failed to init sdb since %s", terrstr());
×
28
    return NULL;
×
29
  }
30

31
  char path[PATH_MAX + 100] = {0};
32✔
32
  snprintf(path, sizeof(path), "%s%sdata", pOption->path, TD_DIRSEP);
32✔
33
  pSdb->currDir = taosStrdup(path);
32✔
34
  snprintf(path, sizeof(path), "%s%stmp", pOption->path, TD_DIRSEP);
32✔
35
  pSdb->tmpDir = taosStrdup(path);
32✔
36

37
  // Store mnode directory path for persisting encrypted flag
38
  tstrncpy(pSdb->mnodePath, pOption->path, PATH_MAX);
32✔
39

40
  if (pSdb->currDir == NULL || pSdb->tmpDir == NULL) {
32✔
41
    sdbCleanup(pSdb);
×
42
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
43
    mError("failed to init sdb since %s", terrstr());
×
44
    return NULL;
×
45
  }
46

47
  if (sdbCreateDir(pSdb) != 0) {
32✔
48
    sdbCleanup(pSdb);
×
49
    return NULL;
×
50
  }
51

52
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
1,536✔
53
    (void)taosThreadRwlockInit(&pSdb->locks[i], NULL);
1,504✔
54
    pSdb->maxId[i] = 0;
1,504✔
55
    pSdb->tableVer[i] = 0;
1,504✔
56
    pSdb->keyTypes[i] = SDB_KEY_INT32;
1,504✔
57
  }
58

59
  pSdb->pWal = pOption->pWal;
32✔
60
  pSdb->applyIndex = -1;
32✔
61
  pSdb->applyTerm = -1;
32✔
62
  pSdb->applyConfig = -1;
32✔
63
  pSdb->commitIndex = -1;
32✔
64
  pSdb->commitTerm = -1;
32✔
65
  pSdb->commitConfig = -1;
32✔
66
  pSdb->pMnode = pOption->pMnode;
32✔
67
  pSdb->encrypted = false;
32✔
68
  (void)taosThreadMutexInit(&pSdb->filelock, NULL);
32✔
69
  mInfo("sdb init success");
32✔
70
  return pSdb;
32✔
71
}
72

73
void sdbCleanup(SSdb *pSdb) {
30✔
74
  mInfo("start to cleanup sdb");
30✔
75

76
  int32_t code = 0;
30✔
77

78
  if ((code = sdbWriteFile(pSdb, 0)) != 0) {
30✔
79
    mError("failed to write sdb file since %s", tstrerror(code));
×
80
  }
81

82
  if (pSdb->currDir != NULL) {
30✔
83
    taosMemoryFreeClear(pSdb->currDir);
30✔
84
  }
85

86
  if (pSdb->tmpDir != NULL) {
30✔
87
    taosRemoveDir(pSdb->tmpDir);
30✔
88
    taosMemoryFreeClear(pSdb->tmpDir);
30✔
89
  }
90

91
  // mnodePath is now a fixed-size array (char mnodePath[PATH_MAX]), no need to free
92

93
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
1,440✔
94
    SHashObj *hash = pSdb->hashObjs[i];
1,410✔
95
    if (hash == NULL) continue;
1,410✔
96

97
    SSdbRow **ppRow = taosHashIterate(hash, NULL);
710✔
98
    while (ppRow != NULL) {
1,070✔
99
      SSdbRow *pRow = *ppRow;
360✔
100
      if (pRow == NULL) continue;
360✔
101

102
      sdbFreeRow(pSdb, pRow, true);
360✔
103
      ppRow = taosHashIterate(hash, ppRow);
360✔
104
    }
105
  }
106

107
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
1,440✔
108
    SHashObj *hash = pSdb->hashObjs[i];
1,410✔
109
    if (hash == NULL) continue;
1,410✔
110

111
    taosHashClear(hash);
710✔
112
    taosHashCleanup(hash);
710✔
113
    (void)taosThreadRwlockDestroy(&pSdb->locks[i]);
710✔
114
    pSdb->hashObjs[i] = NULL;
710✔
115
    memset(&pSdb->locks[i], 0, sizeof(pSdb->locks[i]));
710✔
116

117
    mInfo("sdb table:%s is cleaned up", sdbTableName(i));
710✔
118
  }
119

120
  (void)taosThreadMutexDestroy(&pSdb->filelock);
30✔
121
  taosMemoryFree(pSdb);
30✔
122
  mInfo("sdb is cleaned up");
30✔
123
}
30✔
124

125
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table) {
712✔
126
  int32_t code = 0;
712✔
127

128
  ESdbType sdbType = table.sdbType;
712✔
129
  EKeyType keyType = table.keyType;
712✔
130
  pSdb->keyTypes[sdbType] = table.keyType;
712✔
131
  pSdb->insertFps[sdbType] = table.insertFp;
712✔
132
  pSdb->updateFps[sdbType] = table.updateFp;
712✔
133
  pSdb->deleteFps[sdbType] = table.deleteFp;
712✔
134
  pSdb->deployFps[sdbType] = table.deployFp;
712✔
135
  pSdb->encodeFps[sdbType] = table.encodeFp;
712✔
136
  pSdb->decodeFps[sdbType] = table.decodeFp;
712✔
137
  pSdb->upgradeFps[sdbType] = table.upgradeFp;
712✔
138
  pSdb->afterRestoredFps[sdbType] = table.afterRestoredFp;
712✔
139
  pSdb->validateFps[sdbType] = table.validateFp;
712✔
140

141
  int32_t hashType = 0;
712✔
142
  if (keyType == SDB_KEY_INT32) {
712✔
143
    hashType = TSDB_DATA_TYPE_INT;
324✔
144
  } else if (keyType == SDB_KEY_INT64) {
388✔
145
    hashType = TSDB_DATA_TYPE_BIGINT;
84✔
146
  } else {
147
    hashType = TSDB_DATA_TYPE_BINARY;
304✔
148
  }
149

150
  SHashObj *hash = taosHashInit(64, taosGetDefaultHashFunction(hashType), true, HASH_ENTRY_LOCK);
712✔
151
  if (hash == NULL) {
712✔
152
    TAOS_RETURN(terrno);
×
153
  }
154

155
  pSdb->maxId[sdbType] = 0;
712✔
156
  pSdb->hashObjs[sdbType] = hash;
712✔
157
  mInfo("sdb table:%s is initialized", sdbTableName(sdbType));
712✔
158

159
  TAOS_RETURN(0);
712✔
160
}
161

162
static int32_t sdbCreateDir(SSdb *pSdb) {
32✔
163
  int32_t code = 0;
32✔
164
  if (taosMulMkDir(pSdb->currDir) != 0) {
32✔
165
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
166
    mError("failed to create dir:%s since %s", pSdb->currDir, tstrerror(code));
×
167
    TAOS_RETURN(code);
×
168
  }
169

170
  if (taosMkDir(pSdb->tmpDir) != 0) {
32✔
171
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
172
    mError("failed to create dir:%s since %s", pSdb->tmpDir, tstrerror(code));
×
173
    TAOS_RETURN(code);
×
174
  }
175

176
  return 0;
32✔
177
}
178

179
void sdbSetApplyInfo(SSdb *pSdb, int64_t index, int64_t term, int64_t config) {
672✔
180
  mInfo("vgId:1, mnode apply info changed from index:%" PRId64 " term:%" PRId64 " config:%" PRId64 " to index:%" PRId64
672✔
181
        " term:%" PRId64 " config:%" PRId64,
182
        pSdb->applyIndex, pSdb->applyTerm, pSdb->applyConfig, index, term, config);
183

184
  pSdb->applyIndex = index;
672✔
185
  pSdb->applyTerm = term;
672✔
186
  pSdb->applyConfig = config;
672✔
187
}
672✔
188

189
void sdbGetCommitInfo(SSdb *pSdb, int64_t *index, int64_t *term, int64_t *config) {
322✔
190
  *index = pSdb->commitIndex;
322✔
191
  *term = pSdb->commitTerm;
322✔
192
  *config = pSdb->commitConfig;
322✔
193
#if 1
194
  mTrace("mnode current info, apply index:%" PRId64 " term:%" PRId64 " config:%" PRId64 ", commit index:%" PRId64
322✔
195
         " term:%" PRId64 " config:%" PRId64,
196
         pSdb->applyIndex, pSdb->applyTerm, pSdb->applyConfig, *index, *term, *config);
197
#endif
198
}
322✔
199

200
void sdbWriteLock(SSdb *pSdb, int32_t type) {
7,802✔
201
  TdThreadRwlock *pLock = &pSdb->locks[type];
7,802✔
202
  // mTrace("sdb table:%d start write lock:%p", type, pLock);
203
  (void)taosThreadRwlockWrlock(pLock);
7,802✔
204
  // mTrace("sdb table:%d stop write lock:%p", type, pLock);
205
}
7,802✔
206

207
void sdbReadLock(SSdb *pSdb, int32_t type) {
8,619✔
208
  TdThreadRwlock *pLock = &pSdb->locks[type];
8,619✔
209
  // mTrace("sdb table:%d start read lock:%p", type, pLock);
210
  (void)taosThreadRwlockRdlock(pLock);
8,619✔
211
  // mTrace("sdb table:%d stop read lock:%p", type, pLock);
212
}
8,619✔
213

214
void sdbUnLock(SSdb *pSdb, int32_t type) {
16,421✔
215
  TdThreadRwlock *pLock = &pSdb->locks[type];
16,421✔
216
  // mTrace("sdb table:%d unlock:%p", type, pLock);
217
  (void)taosThreadRwlockUnlock(pLock);
16,421✔
218
}
16,421✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc