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

taosdata / TDengine / #4688

26 Aug 2025 02:05PM UTC coverage: 56.997% (-0.9%) from 57.894%
#4688

push

travis-ci

web-flow
fix: modify the prompt language of the taos-shell (#32758)

* fix: modify prompt language

* fix: add shell test case

* fix: modify comments

* fix: modify test case for TDengine TSDB

130660 of 292423 branches covered (44.68%)

Branch coverage included in aggregate %.

16 of 17 new or added lines in 2 files covered. (94.12%)

9459 existing lines in 157 files now uncovered.

198294 of 284715 relevant lines covered (69.65%)

4532552.29 hits per line

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

73.34
/source/dnode/vnode/src/tsdb/tsdbFSet2.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
#include "tsdbFSet2.h"
17
#include "vnd.h"
18

19
int32_t tsdbSttLvlInit(int32_t level, SSttLvl **lvl) {
2,584,200✔
20
  if (!(lvl[0] = taosMemoryMalloc(sizeof(SSttLvl)))) {
2,584,200!
21
    return terrno;
×
22
  }
23
  lvl[0]->level = level;
2,589,670✔
24
  TARRAY2_INIT(lvl[0]->fobjArr);
2,589,670✔
25
  return 0;
2,589,670✔
26
}
27

28
static void tsdbSttLvlClearFObj(void *data) { TAOS_UNUSED(tsdbTFileObjUnref(*(STFileObj **)data)); }
2,608,232✔
29

30
void tsdbSttLvlClear(SSttLvl **lvl) {
2,586,324✔
31
  if (lvl[0] != NULL) {
2,586,324!
32
    TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlClearFObj);
5,185,818!
33
    taosMemoryFree(lvl[0]);
2,586,775✔
34
    lvl[0] = NULL;
2,586,749✔
35
  }
36
}
2,586,708✔
37

38
static int32_t tsdbSttLvlInitEx(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
56,150✔
39
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
56,150✔
40
  if (code) return code;
56,150!
41

42
  const STFileObj *fobj1;
43
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
123,441✔
44
    STFileObj *fobj;
45
    code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj);
67,291✔
46
    if (code) {
67,294✔
47
      tsdbSttLvlClear(lvl);
1✔
48
      return code;
×
49
    }
50

51
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
67,293✔
52
    if (code) {
67,291!
53
      tsdbSttLvlClear(lvl);
×
54
      taosMemoryFree(fobj);
×
55
      return code;
×
56
    }
57
  }
58
  return 0;
56,150✔
59
}
60

61
static int32_t tsdbSttLvlInitRef(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
2,509,006✔
62
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
2,509,006✔
63
  if (code) return code;
2,514,267!
64

65
  STFileObj *fobj1;
66
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
5,033,499✔
67
    code = tsdbTFileObjRef(fobj1);
2,519,859✔
68
    if (code) {
2,520,334✔
69
      tsdbSttLvlClear(lvl);
422✔
70
      return code;
×
71
    }
72
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj1);
2,519,912✔
73
    if (code) {
2,519,232!
74
      if (tsdbTFileObjUnref(fobj1) != 0) {
×
75
        tsdbError("failed to unref file obj, fobj:%p", fobj1);
×
76
      }
77
      tsdbSttLvlClear(lvl);
×
78
      return code;
×
79
    }
80
  }
81
  return 0;
2,513,640✔
82
}
83

84
static int32_t tsdbSttLvlFilteredInitEx(STsdb *pTsdb, const SSttLvl *lvl1, int64_t ever, SSttLvl **lvl,
3✔
85
                                        TFileOpArray *fopArr) {
86
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
3✔
87
  if (code) return code;
3!
88

89
  const STFileObj *fobj1;
90
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
6✔
91
    if (fobj1->f->maxVer <= ever) {
3!
92
      STFileObj *fobj;
93
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj);
3✔
94
      if (code) {
3!
95
        tsdbSttLvlClear(lvl);
×
96
        return code;
×
97
      }
98

99
      TAOS_CHECK_RETURN(TARRAY2_APPEND(lvl[0]->fobjArr, fobj));
6!
100
    } else {
101
      STFileOp op = {
×
102
          .optype = TSDB_FOP_REMOVE,
103
          .fid = fobj1->f->fid,
×
104
          .of = fobj1->f[0],
105
      };
106
      TAOS_CHECK_RETURN(TARRAY2_APPEND(fopArr, op));
×
107
    }
108
  }
109
  return 0;
3✔
110
}
111

112
static void tsdbSttLvlRemoveFObj(void *data) {
8,833✔
113
  int32_t code = tsdbTFileObjRemove(*(STFileObj **)data);
8,833✔
114
  if (code) {
8,833!
115
    tsdbError("failed to remove file obj, code:%d, error:%s", code, tstrerror(code));
×
116
  }
117
}
8,833✔
118
static void tsdbSttLvlRemove(SSttLvl **lvl) {
3,997✔
119
  TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlRemoveFObj);
10,731!
120
  taosMemoryFree(lvl[0]);
3,997!
121
  lvl[0] = NULL;
3,997✔
122
}
3,997✔
123

124
static int32_t tsdbSttLvlApplyEdit(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl *lvl2) {
15,854✔
125
  int32_t code = 0;
15,854✔
126

127
  if (lvl1->level != lvl2->level) {
15,854!
128
    return TSDB_CODE_INVALID_PARA;
×
129
  }
130

131
  int32_t i1 = 0, i2 = 0;
15,854✔
132
  while (i1 < TARRAY2_SIZE(lvl1->fobjArr) || i2 < TARRAY2_SIZE(lvl2->fobjArr)) {
39,682!
133
    STFileObj *fobj1 = i1 < TARRAY2_SIZE(lvl1->fobjArr) ? TARRAY2_GET(lvl1->fobjArr, i1) : NULL;
23,828!
134
    STFileObj *fobj2 = i2 < TARRAY2_SIZE(lvl2->fobjArr) ? TARRAY2_GET(lvl2->fobjArr, i2) : NULL;
23,828✔
135

136
    if (fobj1 && fobj2) {
23,828!
137
      if (fobj1->f->cid < fobj2->f->cid) {
19,000!
138
        // create a file obj
139
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
×
140
        if (code) return code;
×
141
        code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
×
142
        if (code) return code;
×
143
        i1++;
×
144
        i2++;
×
145
      } else if (fobj1->f->cid > fobj2->f->cid) {
19,000✔
146
        // remove a file obj
147
        TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
2,062!
148
      } else {
149
        if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
16,938✔
150
          if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
16,901!
151
            fobj2->f[0] = fobj1->f[0];
×
152
          }
153
        } else {
154
          TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
37!
155
          code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
37✔
156
          if (code) return code;
37!
157
          code = TARRAY2_SORT_INSERT(lvl2->fobjArr, fobj2, tsdbTFileObjCmpr);
37✔
158
          if (code) return code;
37!
159
        }
160
        i1++;
16,938✔
161
        i2++;
16,938✔
162
      }
163
    } else if (fobj1) {
4,828!
164
      // create a file obj
165
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
4,828✔
166
      if (code) return code;
4,828!
167
      code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
4,828!
168
      if (code) return code;
4,828!
169
      i1++;
4,828✔
170
      i2++;
4,828✔
171
    } else {
172
      // remove a file obj
173
      TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
174
    }
175
  }
176
  return 0;
15,854✔
177
}
178

179
static int32_t tsdbSttLvlCmprFn(const SSttLvl **lvl1, const SSttLvl **lvl2) {
32,795✔
180
  if (lvl1[0]->level < lvl2[0]->level) return -1;
32,795✔
181
  if (lvl1[0]->level > lvl2[0]->level) return 1;
18,917✔
182
  return 0;
13,706✔
183
}
184

185
static int32_t tsdbSttLvlToJson(const SSttLvl *lvl, cJSON *json) {
32,706✔
186
  if (cJSON_AddNumberToObject(json, "level", lvl->level) == NULL) {
32,706!
187
    return TSDB_CODE_OUT_OF_MEMORY;
×
188
  }
189

190
  cJSON *ajson = cJSON_AddArrayToObject(json, "files");
32,705✔
191
  if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
32,705!
192
  const STFileObj *fobj;
193
  TARRAY2_FOREACH(lvl->fobjArr, fobj) {
71,331✔
194
    cJSON *item = cJSON_CreateObject();
38,625✔
195
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
38,625!
196
    (void)cJSON_AddItemToArray(ajson, item);
38,625✔
197

198
    int32_t code = tsdbTFileToJson(fobj->f, item);
38,624✔
199
    if (code) return code;
38,626!
200
  }
201

202
  return 0;
32,706✔
203
}
204

205
static int32_t tsdbJsonToSttLvl(STsdb *pTsdb, const cJSON *json, SSttLvl **lvl) {
2,129✔
206
  const cJSON *item1, *item2;
207
  int32_t      level;
208

209
  item1 = cJSON_GetObjectItem(json, "level");
2,129✔
210
  if (cJSON_IsNumber(item1)) {
2,129!
211
    level = item1->valuedouble;
2,131✔
212
  } else {
213
    return TSDB_CODE_FILE_CORRUPTED;
×
214
  }
215

216
  int32_t code = tsdbSttLvlInit(level, lvl);
2,131✔
217
  if (code) return code;
2,131!
218

219
  item1 = cJSON_GetObjectItem(json, "files");
2,131✔
220
  if (!cJSON_IsArray(item1)) {
2,131!
221
    tsdbSttLvlClear(lvl);
×
222
    return TSDB_CODE_FILE_CORRUPTED;
×
223
  }
224

225
  cJSON_ArrayForEach(item2, item1) {
4,337!
226
    STFile tf;
227
    code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &tf);
2,205✔
228
    if (code) {
2,205!
229
      tsdbSttLvlClear(lvl);
×
230
      return code;
×
231
    }
232

233
    STFileObj *fobj;
234
    code = tsdbTFileObjInit(pTsdb, &tf, &fobj);
2,205✔
235
    if (code) {
2,206!
UNCOV
236
      tsdbSttLvlClear(lvl);
×
237
      return code;
×
238
    }
239

240
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
2,206✔
241
    if (code) return code;
2,206!
242
  }
243
  TARRAY2_SORT(lvl[0]->fobjArr, tsdbTFileObjCmpr);
2,132✔
244
  return 0;
2,131✔
245
}
246

247
int32_t tsdbTFileSetToJson(const STFileSet *fset, cJSON *json) {
27,957✔
248
  int32_t code = 0;
27,957✔
249
  cJSON  *item1, *item2;
250

251
  // fid
252
  if (cJSON_AddNumberToObject(json, "fid", fset->fid) == NULL) {
27,957!
253
    return TSDB_CODE_OUT_OF_MEMORY;
×
254
  }
255

256
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
139,784✔
257
    if (fset->farr[ftype] == NULL) continue;
111,825✔
258

259
    code = tsdbTFileToJson(fset->farr[ftype]->f, json);
23,942✔
260
    if (code) return code;
23,944!
261
  }
262

263
  // each level
264
  item1 = cJSON_AddArrayToObject(json, "stt lvl");
27,959✔
265
  if (item1 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
27,958!
266
  const SSttLvl *lvl;
267
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
60,664✔
268
    item2 = cJSON_CreateObject();
32,706✔
269
    if (!item2) return TSDB_CODE_OUT_OF_MEMORY;
32,706!
270
    (void)cJSON_AddItemToArray(item1, item2);
32,706✔
271

272
    code = tsdbSttLvlToJson(lvl, item2);
32,706✔
273
    if (code) return code;
32,706!
274
  }
275

276
  // about compact and commit
277
  if (cJSON_AddNumberToObject(json, "last compact", fset->lastCompact) == NULL) {
27,958!
278
    return TSDB_CODE_OUT_OF_MEMORY;
×
279
  }
280

281
  if (cJSON_AddNumberToObject(json, "last commit", fset->lastCommit) == NULL) {
27,955!
282
    return TSDB_CODE_OUT_OF_MEMORY;
×
283
  }
284

285
  if (cJSON_AddNumberToObject(json, "last migrate", fset->lastMigrate) == NULL) {
27,957!
286
    return TSDB_CODE_OUT_OF_MEMORY;
×
287
  }
288

289
  return 0;
27,957✔
290
}
291

292
int32_t tsdbJsonToTFileSet(STsdb *pTsdb, const cJSON *json, STFileSet **fset) {
2,327✔
293
  int32_t      code;
294
  const cJSON *item1, *item2;
295
  int32_t      fid;
296
  STFile       tf;
297

298
  // fid
299
  item1 = cJSON_GetObjectItem(json, "fid");
2,327✔
300
  if (cJSON_IsNumber(item1)) {
2,336!
301
    fid = item1->valuedouble;
2,337✔
302
  } else {
303
    return TSDB_CODE_FILE_CORRUPTED;
×
304
  }
305

306
  code = tsdbTFileSetInit(fid, fset);
2,337✔
307
  if (code) return code;
2,333!
308

309
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
11,682✔
310
    code = tsdbJsonToTFile(json, ftype, &tf);
9,342✔
311
    if (code == TSDB_CODE_NOT_FOUND) {
9,349✔
312
      continue;
7,545✔
313
    } else if (code) {
1,804!
314
      tsdbTFileSetClear(fset);
×
315
      return code;
×
316
    } else {
317
      code = tsdbTFileObjInit(pTsdb, &tf, &(*fset)->farr[ftype]);
1,804✔
318
      if (code) return code;
1,804!
319
    }
320
  }
321

322
  // each level
323
  item1 = cJSON_GetObjectItem(json, "stt lvl");
2,340✔
324
  if (cJSON_IsArray(item1)) {
2,337!
325
    cJSON_ArrayForEach(item2, item1) {
4,468!
326
      SSttLvl *lvl;
327
      code = tsdbJsonToSttLvl(pTsdb, item2, &lvl);
2,130✔
328
      if (code) {
2,130!
UNCOV
329
        tsdbTFileSetClear(fset);
×
330
        return code;
×
331
      }
332

333
      code = TARRAY2_APPEND((*fset)->lvlArr, lvl);
2,131✔
334
      if (code) return code;
2,131!
335
    }
336
    TARRAY2_SORT((*fset)->lvlArr, tsdbSttLvlCmprFn);
2,338✔
337
  } else {
338
    return TSDB_CODE_FILE_CORRUPTED;
×
339
  }
340
  // about compact and commit
341
  item1 = cJSON_GetObjectItem(json, "last compact");
2,338✔
342
  if (cJSON_IsNumber(item1)) {
2,338!
343
    (*fset)->lastCompact = item1->valuedouble;
2,337✔
344
  } else {
345
    (*fset)->lastCompact = 0;
×
346
  }
347

348
  item1 = cJSON_GetObjectItem(json, "last commit");
2,337✔
349
  if (cJSON_IsNumber(item1)) {
2,338!
350
    (*fset)->lastCommit = item1->valuedouble;
2,338✔
351
  } else {
352
    (*fset)->lastCommit = 0;
×
353
  }
354

355
  item1 = cJSON_GetObjectItem(json, "last migrate");
2,338✔
356
  if (cJSON_IsNumber(item1)) {
2,337!
357
    (*fset)->lastMigrate = item1->valuedouble;
2,338✔
358
  } else {
359
    (*fset)->lastMigrate = 0;
×
360
  }
361

362
  return 0;
2,338✔
363
}
364

365
// NOTE: the api does not remove file (seems this is not true?), only do memory operation
366
int32_t tsdbTFileSetEdit(STsdb *pTsdb, STFileSet *fset, const STFileOp *op) {
41,546✔
367
  int32_t code = 0;
41,546✔
368

369
  if (op->optype == TSDB_FOP_CREATE) {
41,546✔
370
    // create a new file
371
    STFileObj *fobj;
372
    code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
29,157✔
373
    if (code) return code;
29,157!
374

375
    if (fobj->f->type == TSDB_FTYPE_STT) {
29,157✔
376
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, fobj->f->stt->level);
21,725✔
377
      if (!lvl) {
21,725✔
378
        code = tsdbSttLvlInit(fobj->f->stt->level, &lvl);
16,852✔
379
        if (code) return code;
16,852!
380

381
        code = TARRAY2_SORT_INSERT(fset->lvlArr, lvl, tsdbSttLvlCmprFn);
16,852✔
382
        if (code) return code;
16,852!
383
      }
384

385
      code = TARRAY2_SORT_INSERT(lvl->fobjArr, fobj, tsdbTFileObjCmpr);
21,725✔
386
      if (code) return code;
21,725!
387
    } else {
388
      fset->farr[fobj->f->type] = fobj;
7,432✔
389
    }
390
  } else if (op->optype == TSDB_FOP_REMOVE) {
12,389✔
391
    // delete a file
392
    if (op->of.type == TSDB_FTYPE_STT) {
10,821✔
393
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
8,833✔
394

395
      STFileObj  tfobj = {.f[0] = {.cid = op->of.cid}};
8,833✔
396
      STFileObj *tfobjp = &tfobj;
8,833✔
397
      int32_t    idx = TARRAY2_SEARCH_IDX(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
8,833✔
398
      TARRAY2_REMOVE(lvl->fobjArr, idx, tsdbSttLvlClearFObj);
8,833!
399
    } else {
400
      code = tsdbTFileObjUnref(fset->farr[op->of.type]);
1,988✔
401
      if (code) return code;
1,988!
402
      fset->farr[op->of.type] = NULL;
1,988✔
403
    }
404
  } else {
405
    if (op->nf.type == TSDB_FTYPE_STT) {
1,568!
406
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
×
407

408
      STFileObj   tfobj = {.f[0] = {.cid = op->of.cid}}, *tfobjp = &tfobj;
×
409
      STFileObj **fobjPtr = TARRAY2_SEARCH(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
×
410
      if (fobjPtr) {
×
411
        tfobjp = *fobjPtr;
×
412
        tfobjp->f[0] = op->nf;
×
413
      } else {
414
        tsdbError("file not found, cid:%" PRId64, op->of.cid);
×
415
      }
416
    } else {
417
      fset->farr[op->nf.type]->f[0] = op->nf;
1,568✔
418
    }
419
  }
420

421
  return 0;
41,546✔
422
}
423

424
int32_t tsdbTFileSetApplyEdit(STsdb *pTsdb, const STFileSet *fset1, STFileSet *fset2) {
13,574✔
425
  int32_t code = 0;
13,574✔
426

427
  if (fset1->fid != fset2->fid) {
13,574!
428
    return TSDB_CODE_INVALID_PARA;
×
429
  }
430

431
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
67,870✔
432
    if (!fset1->farr[ftype] && !fset2->farr[ftype]) continue;
54,296✔
433

434
    STFileObj *fobj1 = fset1->farr[ftype];
19,484✔
435
    STFileObj *fobj2 = fset2->farr[ftype];
19,484✔
436

437
    if (fobj1 && fobj2) {
19,484✔
438
      if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
18,432✔
439
        if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
16,512✔
440
          fobj2->f[0] = fobj1->f[0];
1,568✔
441
        }
442
      } else {
443
        if (fobj1->f->cid != fobj2->f->cid) {
1,920✔
444
          code = tsdbTFileObjRemove(fobj2);
1,800✔
445
          if (code) return code;
1,800!
446
        } else {
447
          code = tsdbTFileObjRemoveUpdateLC(fobj2);
120✔
448
          if (code) return code;
120!
449
        }
450
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
1,920✔
451
        if (code) return code;
1,920!
452
      }
453
    } else if (fobj1) {
1,052✔
454
      // create a new file
455
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
1,003✔
456
      if (code) return code;
1,003!
457
    } else {
458
      // remove the file
459
      code = tsdbTFileObjRemove(fobj2);
49✔
460
      if (code) return code;
49!
461
      fset2->farr[ftype] = NULL;
49✔
462
    }
463
  }
464

465
  // stt part
466
  int32_t i1 = 0, i2 = 0;
13,574✔
467
  while (i1 < TARRAY2_SIZE(fset1->lvlArr) || i2 < TARRAY2_SIZE(fset2->lvlArr)) {
37,267✔
468
    SSttLvl *lvl1 = i1 < TARRAY2_SIZE(fset1->lvlArr) ? TARRAY2_GET(fset1->lvlArr, i1) : NULL;
23,693✔
469
    SSttLvl *lvl2 = i2 < TARRAY2_SIZE(fset2->lvlArr) ? TARRAY2_GET(fset2->lvlArr, i2) : NULL;
23,693✔
470

471
    if (lvl1 && lvl2) {
23,693✔
472
      if (lvl1->level < lvl2->level) {
22,849✔
473
        // add a new stt level
474
        code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
3,433✔
475
        if (code) return code;
3,433!
476
        code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
3,433✔
477
        if (code) return code;
3,433!
478
        i1++;
3,433✔
479
        i2++;
3,433✔
480
      } else if (lvl1->level > lvl2->level) {
19,416✔
481
        // remove the stt level
482
        TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
3,562!
483
      } else {
484
        // apply edit on stt level
485
        code = tsdbSttLvlApplyEdit(pTsdb, lvl1, lvl2);
15,854✔
486
        if (code) return code;
15,854!
487
        i1++;
15,854✔
488
        i2++;
15,854✔
489
      }
490
    } else if (lvl1) {
844✔
491
      // add a new stt level
492
      code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
459✔
493
      if (code) return code;
459!
494
      code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
459✔
495
      if (code) return code;
459!
496
      i1++;
459✔
497
      i2++;
459✔
498
    } else {
499
      // remove the stt level
500
      TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
385!
501
    }
502
  }
503

504
  fset2->lastCompact = fset1->lastCompact;
13,574✔
505
  fset2->lastCommit = fset1->lastCommit;
13,574✔
506
  fset2->lastMigrate = fset1->lastMigrate;
13,574✔
507

508
  return 0;
13,574✔
509
}
510

511
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) {
2,647,921✔
512
  fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
2,647,921!
513
  if (fset[0] == NULL) {
2,646,263!
514
    return terrno;
×
515
  }
516

517
  fset[0]->fid = fid;
2,646,263✔
518
  fset[0]->maxVerValid = VERSION_MAX;
2,646,263✔
519
  TARRAY2_INIT(fset[0]->lvlArr);
2,646,263✔
520

521
  // block commit variables
522
  (void)taosThreadCondInit(&fset[0]->canCommit, NULL);
2,646,263✔
523
  (*fset)->numWaitCommit = 0;
2,645,668✔
524
  (*fset)->blockCommit = false;
2,645,668✔
525

526
  for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
7,926,539✔
527
    struct STFileSetCond *cond = &(*fset)->conds[i];
5,283,340✔
528
    cond->running = false;
5,283,340✔
529
    cond->numWait = 0;
5,283,340✔
530
    (void)taosThreadCondInit(&cond->cond, NULL);
5,283,340✔
531
  }
532

533
  return 0;
2,643,199✔
534
}
535

536
int32_t tsdbTFileSetInitCopy(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
40,789✔
537
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
40,789✔
538
  if (code) return code;
40,792!
539

540
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
203,953✔
541
    if (fset1->farr[ftype] == NULL) continue;
163,154✔
542

543
    code = tsdbTFileObjInit(pTsdb, fset1->farr[ftype]->f, &fset[0]->farr[ftype]);
39,159✔
544
    if (code) {
39,165!
545
      tsdbTFileSetClear(fset);
×
546
      return code;
×
547
    }
548
  }
549

550
  const SSttLvl *lvl1;
551
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
93,061✔
552
    SSttLvl *lvl;
553
    code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl);
52,258✔
554
    if (code) {
52,259!
UNCOV
555
      tsdbTFileSetClear(fset);
×
556
      return code;
×
557
    }
558

559
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
52,260✔
560
    if (code) return code;
52,262!
561
  }
562

563
  (*fset)->lastCompact = fset1->lastCompact;
40,803✔
564
  (*fset)->lastCommit = fset1->lastCommit;
40,803✔
565
  (*fset)->lastMigrate = fset1->lastMigrate;
40,803✔
566

567
  return 0;
40,803✔
568
}
569

570
int32_t tsdbTFileSetFilteredInitDup(STsdb *pTsdb, const STFileSet *fset1, int64_t ever, STFileSet **fset,
3✔
571
                                    TFileOpArray *fopArr) {
572
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
3✔
573
  if (code) return code;
3!
574

575
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
15✔
576
    if (fset1->farr[ftype] == NULL) continue;
12!
577
    STFileObj *fobj = fset1->farr[ftype];
×
578
    if (fobj->f->maxVer <= ever) {
×
579
      code = tsdbTFileObjInit(pTsdb, fobj->f, &fset[0]->farr[ftype]);
×
580
      if (code) {
×
581
        tsdbTFileSetClear(fset);
×
582
        return code;
×
583
      }
584
    } else {
585
      STFileOp op = {
×
586
          .optype = TSDB_FOP_REMOVE,
587
          .fid = fobj->f->fid,
×
588
          .of = fobj->f[0],
589
      };
590
      code = TARRAY2_APPEND(fopArr, op);
×
591
      if (code) return code;
×
592
    }
593
  }
594

595
  const SSttLvl *lvl1;
596
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
6✔
597
    SSttLvl *lvl;
598
    code = tsdbSttLvlFilteredInitEx(pTsdb, lvl1, ever, &lvl, fopArr);
3✔
599
    if (code) {
3!
600
      tsdbTFileSetClear(fset);
×
601
      return code;
×
602
    }
603

604
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
3!
605
    if (code) return code;
3!
606
  }
607

608
  return 0;
3✔
609
}
610

611
int32_t tsdbTFileSetRangeInitRef(STsdb *pTsdb, const STFileSet *fset1, int64_t sver, int64_t ever,
3✔
612
                                 STFileSetRange **fsr) {
613
  fsr[0] = taosMemoryCalloc(1, sizeof(*fsr[0]));
3!
614
  if (fsr[0] == NULL) {
3!
615
    return terrno;
×
616
  }
617
  fsr[0]->fid = fset1->fid;
3✔
618
  fsr[0]->sver = sver;
3✔
619
  fsr[0]->ever = ever;
3✔
620

621
  int32_t code = tsdbTFileSetInitRef(pTsdb, fset1, &fsr[0]->fset);
3✔
622
  if (code) {
3!
623
    taosMemoryFree(fsr[0]);
×
624
    fsr[0] = NULL;
×
625
  }
626
  return code;
3✔
627
}
628

629
int32_t tsdbTFileSetInitRef(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
2,590,463✔
630
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
2,590,463✔
631
  if (code) return code;
2,583,797!
632

633
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
12,930,253✔
634
    if (fset1->farr[ftype] == NULL) continue;
10,342,828✔
635

636
    code = tsdbTFileObjRef(fset1->farr[ftype]);
380,313✔
637
    if (code) {
383,892!
638
      tsdbTFileSetClear(fset);
×
639
      return code;
×
640
    }
641
    fset[0]->farr[ftype] = fset1->farr[ftype];
383,941✔
642
  }
643

644
  const SSttLvl *lvl1;
645
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
5,101,259✔
646
    SSttLvl *lvl;
647
    code = tsdbSttLvlInitRef(pTsdb, lvl1, &lvl);
2,508,814✔
648
    if (code) {
2,513,243!
649
      tsdbSttLvlClear(&lvl);
×
650
      tsdbTFileSetClear(fset);
×
651
      return code;
×
652
    }
653

654
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
2,513,575✔
655
    if (code) {
2,513,834!
656
      tsdbSttLvlClear(&lvl);
×
657
      tsdbTFileSetClear(fset);
×
658
      return code;
×
659
    }
660
  }
661

662
  (*fset)->lastCompact = fset1->lastCompact;
2,592,445✔
663
  (*fset)->lastCommit = fset1->lastCommit;
2,592,445✔
664
  (*fset)->lastMigrate = fset1->lastMigrate;
2,592,445✔
665

666
  return 0;
2,592,445✔
667
}
668

669
void tsdbTFileSetRangeClear(STFileSetRange **fsr) {
9✔
670
  if (!fsr[0]) return;
9!
671

672
  tsdbTFileSetClear(&fsr[0]->fset);
9✔
673
  taosMemoryFree(fsr[0]);
9!
674
  fsr[0] = NULL;
9✔
675
  return;
9✔
676
}
677

678
void tsdbTFileSetRangeArrayDestroy(TFileSetRangeArray **ppArr) {
579✔
679
  if (ppArr && ppArr[0]) {
579!
680
    TARRAY2_DESTROY(ppArr[0], tsdbTFileSetRangeClear);
204!
681
    taosMemoryFree(ppArr[0]);
195!
682
    ppArr[0] = NULL;
195✔
683
  }
684
}
579✔
685

686
void tsdbTFileSetClear(STFileSet **fset) {
2,665,422✔
687
  if (fset && *fset) {
2,665,422!
688
    for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
13,255,978✔
689
      if ((*fset)->farr[ftype] == NULL) continue;
10,604,504✔
690
      int32_t code = tsdbTFileObjUnref((*fset)->farr[ftype]);
433,045✔
691
      if (code) {
433,290!
692
        tsdbError("failed to unref file, fid:%d, ftype:%d", (*fset)->fid, ftype);
×
693
      }
694
      (*fset)->farr[ftype] = NULL;
433,294✔
695
    }
696

697
    TARRAY2_DESTROY((*fset)->lvlArr, tsdbSttLvlClear);
5,233,931!
698

699
    (void)taosThreadCondDestroy(&(*fset)->canCommit);
2,651,737✔
700
    for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
7,953,204✔
701
      (void)taosThreadCondDestroy(&(*fset)->conds[i].cond);
5,301,896✔
702
    }
703
    taosMemoryFreeClear(*fset);
2,651,308!
704
  }
705
}
2,665,526✔
706

707
void tsdbTFileSetRemove(STFileSet *fset) {
128✔
708
  if (fset == NULL) return;
128!
709

710
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
640✔
711
    if (fset->farr[ftype] != NULL) {
512✔
712
      int32_t code = tsdbTFileObjRemove(fset->farr[ftype]);
19✔
713
      if (code) {
19!
714
        tsdbError("failed to remove file, fid:%d, ftype:%d", fset->fid, ftype);
×
715
      }
716
      fset->farr[ftype] = NULL;
19✔
717
    }
718
  }
719

720
  TARRAY2_DESTROY(fset->lvlArr, tsdbSttLvlRemove);
178!
721
}
722

723
SSttLvl *tsdbTFileSetGetSttLvl(STFileSet *fset, int32_t level) {
30,558✔
724
  SSttLvl   sttLvl = {.level = level};
30,558✔
725
  SSttLvl  *lvl = &sttLvl;
30,558✔
726
  SSttLvl **lvlPtr = TARRAY2_SEARCH(fset->lvlArr, &lvl, tsdbSttLvlCmprFn, TD_EQ);
30,558✔
727
  return lvlPtr ? lvlPtr[0] : NULL;
30,558✔
728
}
729

730
int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2) {
177,285✔
731
  if (fset1[0]->fid < fset2[0]->fid) return -1;
177,285✔
732
  if (fset1[0]->fid > fset2[0]->fid) return 1;
169,081✔
733
  return 0;
37,663✔
734
}
735

736
int64_t tsdbTFileSetMaxCid(const STFileSet *fset) {
2,336✔
737
  int64_t maxCid = 0;
2,336✔
738
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
11,684✔
739
    if (fset->farr[ftype] == NULL) continue;
9,348✔
740
    maxCid = TMAX(maxCid, fset->farr[ftype]->f->cid);
1,805✔
741
  }
742
  const SSttLvl   *lvl;
743
  const STFileObj *fobj;
744
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
4,467✔
745
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { maxCid = TMAX(maxCid, fobj->f->cid); }
4,337✔
746
  }
747
  return maxCid;
2,336✔
748
}
749

750
bool tsdbTFileSetIsEmpty(const STFileSet *fset) {
32,356✔
751
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
127,521✔
752
    if (fset->farr[ftype] != NULL) return false;
108,889✔
753
  }
754
  return TARRAY2_SIZE(fset->lvlArr) == 0;
18,632✔
755
}
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