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

taosdata / TDengine / #5001

24 Mar 2026 01:11AM UTC coverage: 72.254% (-0.05%) from 72.307%
#5001

push

travis-ci

web-flow
fix: possible memory leak in tdb; (#34872)

1 of 20 new or added lines in 2 files covered. (5.0%)

770 existing lines in 143 files now uncovered.

253287 of 350550 relevant lines covered (72.25%)

130300301.72 hits per line

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

80.29
/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) {
358,425,924✔
20
  if (!(lvl[0] = taosMemoryMalloc(sizeof(SSttLvl)))) {
358,425,924✔
21
    return terrno;
×
22
  }
23
  lvl[0]->level = level;
358,343,509✔
24
  TARRAY2_INIT(lvl[0]->fobjArr);
358,389,111✔
25
  return 0;
358,367,546✔
26
}
27

28
static void tsdbSttLvlClearFObj(void *data) { TAOS_UNUSED(tsdbTFileObjUnref(*(STFileObj **)data)); }
361,179,601✔
29

30
void tsdbSttLvlClear(SSttLvl **lvl) {
357,026,600✔
31
  if (lvl[0] != NULL) {
357,026,600✔
32
    TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlClearFObj);
715,270,302✔
33
    taosMemoryFree(lvl[0]);
357,034,677✔
34
    lvl[0] = NULL;
357,028,169✔
35
  }
36
}
357,027,740✔
37

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

42
  const STFileObj *fobj1;
43
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
63,539,127✔
44
    STFileObj *fobj;
32,995,241✔
45
    code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj);
32,996,431✔
46
    if (code) {
32,996,431✔
47
      tsdbSttLvlClear(lvl);
×
48
      return code;
×
49
    }
50

51
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
32,996,431✔
52
    if (code) {
32,996,431✔
53
      tsdbSttLvlClear(lvl);
×
54
      (void)taosThreadMutexDestroy(&fobj->mutex);
×
55
      taosMemoryFree(fobj);
×
56
      return code;
×
57
    }
58
  }
59
  return 0;
30,543,294✔
60
}
61

62
static int32_t tsdbSttLvlInitRef(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
310,186,967✔
63
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
310,186,967✔
64
  if (code) return code;
310,075,924✔
65

66
  STFileObj *fobj1;
310,074,340✔
67
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
620,495,451✔
68
    code = tsdbTFileObjRef(fobj1);
310,371,415✔
69
    if (code) {
310,329,470✔
70
      tsdbSttLvlClear(lvl);
×
71
      return code;
×
72
    }
73
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj1);
310,329,470✔
74
    if (code) {
310,322,136✔
75
      if (tsdbTFileObjUnref(fobj1) != 0) {
×
76
        tsdbError("failed to unref file obj, fobj:%p", fobj1);
×
77
      }
78
      tsdbSttLvlClear(lvl);
×
79
      return code;
×
80
    }
81
  }
82
  return 0;
310,145,217✔
83
}
84

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

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

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

113
static void tsdbSttLvlRemoveFObj(void *data) {
2,926,015✔
114
  int32_t code = tsdbTFileObjRemove(*(STFileObj **)data);
2,926,015✔
115
  if (code) {
2,926,355✔
116
    tsdbError("failed to remove file obj, code:%d, error:%s", code, tstrerror(code));
×
117
  }
118
}
2,926,355✔
119
static void tsdbSttLvlRemove(SSttLvl **lvl) {
1,432,787✔
120
  TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlRemoveFObj);
3,836,861✔
121
  taosMemoryFree(lvl[0]);
1,433,127✔
122
  lvl[0] = NULL;
1,433,127✔
123
}
1,433,127✔
124

125
static int32_t tsdbSttLvlApplyEdit(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl *lvl2) {
5,659,092✔
126
  int32_t code = 0;
5,659,092✔
127

128
  if (lvl1->level != lvl2->level) {
5,659,092✔
129
    return TSDB_CODE_INVALID_PARA;
×
130
  }
131

132
  int32_t i1 = 0, i2 = 0;
5,659,092✔
133
  while (i1 < TARRAY2_SIZE(lvl1->fobjArr) || i2 < TARRAY2_SIZE(lvl2->fobjArr)) {
13,152,738✔
134
    STFileObj *fobj1 = i1 < TARRAY2_SIZE(lvl1->fobjArr) ? TARRAY2_GET(lvl1->fobjArr, i1) : NULL;
7,493,646✔
135
    STFileObj *fobj2 = i2 < TARRAY2_SIZE(lvl2->fobjArr) ? TARRAY2_GET(lvl2->fobjArr, i2) : NULL;
7,493,646✔
136

137
    if (fobj1 && fobj2) {
7,493,646✔
138
      if (fobj1->f->cid < fobj2->f->cid) {
6,010,705✔
139
        // create a file obj
140
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
×
141
        if (code) return code;
×
142
        code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
×
143
        if (code) return code;
×
144
        i1++;
×
145
        i2++;
×
146
      } else if (fobj1->f->cid > fobj2->f->cid) {
6,010,705✔
147
        // remove a file obj
148
        TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
506,533✔
149
      } else {
150
        if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
5,504,172✔
151
          if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
5,488,424✔
152
            fobj2->f[0] = fobj1->f[0];
×
153
          }
154
        } else {
155
          TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
15,748✔
156
          code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
15,748✔
157
          if (code) return code;
15,748✔
158
          code = TARRAY2_SORT_INSERT(lvl2->fobjArr, fobj2, tsdbTFileObjCmpr);
15,748✔
159
          if (code) return code;
15,748✔
160
        }
161
        i1++;
5,504,172✔
162
        i2++;
5,504,172✔
163
      }
164
    } else if (fobj1) {
1,482,941✔
165
      // create a file obj
166
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
1,482,941✔
167
      if (code) return code;
1,482,941✔
168
      code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
1,482,941✔
169
      if (code) return code;
1,482,941✔
170
      i1++;
1,482,941✔
171
      i2++;
1,482,941✔
172
    } else {
173
      // remove a file obj
174
      TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
175
    }
176
  }
177
  return 0;
5,659,092✔
178
}
179

180
static int32_t tsdbSttLvlCmprFn(const SSttLvl **lvl1, const SSttLvl **lvl2) {
11,113,580✔
181
  if (lvl1[0]->level < lvl2[0]->level) return -1;
11,113,580✔
182
  if (lvl1[0]->level > lvl2[0]->level) return 1;
6,268,010✔
183
  return 0;
4,427,532✔
184
}
185

186
static int32_t tsdbSttLvlToJson(const SSttLvl *lvl, cJSON *json) {
22,585,497✔
187
  if (cJSON_AddNumberToObject(json, "level", lvl->level) == NULL) {
22,585,497✔
188
    return TSDB_CODE_OUT_OF_MEMORY;
×
189
  }
190

191
  cJSON *ajson = cJSON_AddArrayToObject(json, "files");
22,585,284✔
192
  if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
22,585,497✔
193
  const STFileObj *fobj;
194
  TARRAY2_FOREACH(lvl->fobjArr, fobj) {
46,501,993✔
195
    cJSON *item = cJSON_CreateObject();
23,914,727✔
196
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
23,915,468✔
197
    (void)cJSON_AddItemToArray(ajson, item);
23,915,468✔
198

199
    int32_t code = tsdbTFileToJson(fobj->f, item);
23,915,468✔
200
    if (code) return code;
23,916,496✔
201
  }
202

203
  return 0;
22,586,095✔
204
}
205

206
static int32_t tsdbJsonToSttLvl(STsdb *pTsdb, const cJSON *json, SSttLvl **lvl) {
757,698✔
207
  const cJSON *item1, *item2;
208
  int32_t      level;
209

210
  item1 = cJSON_GetObjectItem(json, "level");
757,698✔
211
  if (cJSON_IsNumber(item1)) {
757,698✔
212
    level = item1->valuedouble;
757,698✔
213
  } else {
214
    return TSDB_CODE_FILE_CORRUPTED;
×
215
  }
216

217
  int32_t code = tsdbSttLvlInit(level, lvl);
757,698✔
218
  if (code) return code;
757,698✔
219

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

226
  cJSON_ArrayForEach(item2, item1) {
1,547,771✔
227
    STFile tf;
789,974✔
228
    code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &tf);
790,073✔
229
    if (code) {
790,073✔
230
      tsdbSttLvlClear(lvl);
×
231
      return code;
×
232
    }
233

234
    STFileObj *fobj;
789,974✔
235
    code = tsdbTFileObjInit(pTsdb, &tf, &fobj);
790,073✔
236
    if (code) {
790,073✔
237
      tsdbSttLvlClear(lvl);
×
238
      return code;
×
239
    }
240

241
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
790,073✔
242
    if (code) return code;
790,073✔
243
  }
244
  TARRAY2_SORT(lvl[0]->fobjArr, tsdbTFileObjCmpr);
757,698✔
245
  return 0;
757,698✔
246
}
247

248
int32_t tsdbTFileSetToJson(const STFileSet *fset, cJSON *json) {
20,561,820✔
249
  int32_t code = 0;
20,561,820✔
250
  cJSON  *item1, *item2;
251

252
  // fid
253
  if (cJSON_AddNumberToObject(json, "fid", fset->fid) == NULL) {
20,561,820✔
254
    return TSDB_CODE_OUT_OF_MEMORY;
×
255
  }
256

257
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
102,800,435✔
258
    if (fset->farr[ftype] == NULL) continue;
82,241,336✔
259

260
    code = tsdbTFileToJson(fset->farr[ftype]->f, json);
5,881,855✔
261
    if (code) return code;
5,881,096✔
262
  }
263

264
  // each level
265
  item1 = cJSON_AddArrayToObject(json, "stt lvl");
20,559,099✔
266
  if (item1 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
20,561,009✔
267
  const SSttLvl *lvl;
268
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
43,147,104✔
269
    item2 = cJSON_CreateObject();
22,584,686✔
270
    if (!item2) return TSDB_CODE_OUT_OF_MEMORY;
22,585,497✔
271
    (void)cJSON_AddItemToArray(item1, item2);
22,585,497✔
272

273
    code = tsdbSttLvlToJson(lvl, item2);
22,585,497✔
274
    if (code) return code;
22,586,095✔
275
  }
276

277
  // about compact and commit
278
  if (cJSON_AddNumberToObject(json, "last compact", fset->lastCompact) == NULL) {
20,561,820✔
279
    return TSDB_CODE_OUT_OF_MEMORY;
×
280
  }
281

282
  if (cJSON_AddNumberToObject(json, "last commit", fset->lastCommit) == NULL) {
20,561,247✔
283
    return TSDB_CODE_OUT_OF_MEMORY;
×
284
  }
285

286
  if (cJSON_AddNumberToObject(json, "last migrate", fset->lastMigrate) == NULL) {
20,561,820✔
287
    return TSDB_CODE_OUT_OF_MEMORY;
×
288
  }
289

290
  if (cJSON_AddNumberToObject(json, "last rollup", fset->lastRollup) == NULL) {
20,561,820✔
291
    return TSDB_CODE_OUT_OF_MEMORY;
×
292
  }
293

294
  if (cJSON_AddNumberToObject(json, "rlevel", fset->lastRollupLevel) == NULL) {
20,561,820✔
295
    return TSDB_CODE_OUT_OF_MEMORY;
×
296
  }
297

298
  return 0;
20,561,820✔
299
}
300

301
int32_t tsdbJsonToTFileSet(STsdb *pTsdb, const cJSON *json, STFileSet **fset) {
745,452✔
302
  int32_t      code;
303
  const cJSON *item1, *item2;
304
  int32_t      fid;
305
  STFile       tf;
745,353✔
306

307
  // fid
308
  item1 = cJSON_GetObjectItem(json, "fid");
745,452✔
309
  if (cJSON_IsNumber(item1)) {
745,452✔
310
    fid = item1->valuedouble;
745,452✔
311
  } else {
312
    return TSDB_CODE_FILE_CORRUPTED;
×
313
  }
314

315
  code = tsdbTFileSetInit(fid, fset);
745,452✔
316
  if (code) return code;
745,452✔
317

318
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
3,727,260✔
319
    code = tsdbJsonToTFile(json, ftype, &tf);
2,981,090✔
320
    if (code == TSDB_CODE_NOT_FOUND) {
2,981,808✔
321
      continue;
2,449,015✔
322
    } else if (code) {
532,793✔
323
      tsdbTFileSetClear(fset);
×
324
      return code;
×
325
    } else {
326
      code = tsdbTFileObjInit(pTsdb, &tf, &(*fset)->farr[ftype]);
532,793✔
327
      if (code) return code;
532,793✔
328
    }
329
  }
330

331
  // each level
332
  item1 = cJSON_GetObjectItem(json, "stt lvl");
746,170✔
333
  if (cJSON_IsArray(item1)) {
745,452✔
334
    cJSON_ArrayForEach(item2, item1) {
1,503,150✔
335
      SSttLvl *lvl;
757,599✔
336
      code = tsdbJsonToSttLvl(pTsdb, item2, &lvl);
757,698✔
337
      if (code) {
757,698✔
338
        tsdbTFileSetClear(fset);
×
339
        return code;
×
340
      }
341

342
      code = TARRAY2_APPEND((*fset)->lvlArr, lvl);
757,698✔
343
      if (code) return code;
757,698✔
344
    }
345
    TARRAY2_SORT((*fset)->lvlArr, tsdbSttLvlCmprFn);
745,452✔
346
  } else {
347
    return TSDB_CODE_FILE_CORRUPTED;
×
348
  }
349
  // about compact and commit
350
  item1 = cJSON_GetObjectItem(json, "last compact");
745,452✔
351
  if (cJSON_IsNumber(item1)) {
745,452✔
352
    (*fset)->lastCompact = item1->valuedouble;
745,452✔
353
  } else {
354
    (*fset)->lastCompact = 0;
×
355
  }
356

357
  item1 = cJSON_GetObjectItem(json, "last commit");
745,452✔
358
  if (cJSON_IsNumber(item1)) {
745,452✔
359
    (*fset)->lastCommit = item1->valuedouble;
745,452✔
360
  } else {
361
    (*fset)->lastCommit = 0;
×
362
  }
363

364
  item1 = cJSON_GetObjectItem(json, "last migrate");
745,452✔
365
  if (cJSON_IsNumber(item1)) {
745,452✔
366
    (*fset)->lastMigrate = item1->valuedouble;
745,452✔
367
  } else {
368
    (*fset)->lastMigrate = 0;
×
369
  }
370

371
  item1 = cJSON_GetObjectItem(json, "last rollup");
745,452✔
372
  if (cJSON_IsNumber(item1)) {
745,452✔
373
    (*fset)->lastRollup = item1->valuedouble;
745,452✔
374
  } else {
375
    (*fset)->lastRollup = 0;
×
376
  }
377

378
  item1 = cJSON_GetObjectItem(json, "rlevel");
745,452✔
379
  if (cJSON_IsNumber(item1)) {
745,452✔
380
    (*fset)->lastRollupLevel = item1->valuedouble;
745,452✔
381
  } else {
382
    (*fset)->lastRollupLevel = 0;
×
383
  }
384

385
  return 0;
745,452✔
386
}
387

388
// NOTE: the api does not remove file (seems this is not true?), only do memory operation
389
int32_t tsdbTFileSetEdit(STsdb *pTsdb, STFileSet *fset, const STFileOp *op) {
24,039,778✔
390
  int32_t code = 0;
24,039,778✔
391

392
  if (op->optype == TSDB_FOP_CREATE) {
24,039,778✔
393
    // create a new file
394
    STFileObj *fobj;
20,166,770✔
395
    code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
20,167,267✔
396
    if (code) return code;
20,167,865✔
397

398
    if (fobj->f->type == TSDB_FTYPE_STT) {
20,167,865✔
399
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, fobj->f->stt->level);
18,427,494✔
400
      if (!lvl) {
18,427,261✔
401
        code = tsdbSttLvlInit(fobj->f->stt->level, &lvl);
16,926,192✔
402
        if (code) return code;
16,926,348✔
403

404
        code = TARRAY2_SORT_INSERT(fset->lvlArr, lvl, tsdbSttLvlCmprFn);
16,926,348✔
405
        if (code) return code;
16,925,962✔
406
      }
407

408
      code = TARRAY2_SORT_INSERT(lvl->fobjArr, fobj, tsdbTFileObjCmpr);
18,427,031✔
409
      if (code) return code;
18,427,860✔
410
    } else {
411
      fset->farr[fobj->f->type] = fobj;
1,740,371✔
412
    }
413
  } else if (op->optype == TSDB_FOP_REMOVE) {
3,872,299✔
414
    // delete a file
415
    if (op->of.type == TSDB_FTYPE_STT) {
3,396,321✔
416
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
2,926,463✔
417

418
      STFileObj  tfobj = {.f[0] = {.cid = op->of.cid}};
2,926,463✔
419
      STFileObj *tfobjp = &tfobj;
2,926,463✔
420
      int32_t    idx = TARRAY2_SEARCH_IDX(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
2,926,463✔
421
      TARRAY2_REMOVE(lvl->fobjArr, idx, tsdbSttLvlClearFObj);
2,926,463✔
422
    } else {
423
      code = tsdbTFileObjUnref(fset->farr[op->of.type]);
469,858✔
424
      if (code) return code;
469,858✔
425
      fset->farr[op->of.type] = NULL;
469,858✔
426
    }
427
  } else {
428
    if (op->nf.type == TSDB_FTYPE_STT) {
475,592✔
429
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
×
430

431
      STFileObj   tfobj = {.f[0] = {.cid = op->of.cid}}, *tfobjp = &tfobj;
×
432
      STFileObj **fobjPtr = TARRAY2_SEARCH(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
×
433
      if (fobjPtr) {
×
434
        tfobjp = *fobjPtr;
×
435
        tfobjp->f[0] = op->nf;
×
436
      } else {
437
        tsdbError("file not found, cid:%" PRId64, op->of.cid);
×
438
      }
439
    } else {
440
      fset->farr[op->nf.type]->f[0] = op->nf;
475,592✔
441
    }
442
  }
443

444
  return 0;
24,039,175✔
445
}
446

447
int32_t tsdbTFileSetApplyEdit(STsdb *pTsdb, const STFileSet *fset1, STFileSet *fset2) {
4,746,047✔
448
  int32_t code = 0;
4,746,047✔
449

450
  if (fset1->fid != fset2->fid) {
4,746,047✔
451
    return TSDB_CODE_INVALID_PARA;
×
452
  }
453

454
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
23,729,895✔
455
    if (!fset1->farr[ftype] && !fset2->farr[ftype]) continue;
18,983,848✔
456

457
    STFileObj *fobj1 = fset1->farr[ftype];
4,901,737✔
458
    STFileObj *fobj2 = fset2->farr[ftype];
4,902,077✔
459

460
    if (fobj1 && fobj2) {
4,902,077✔
461
      if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
4,600,254✔
462
        if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
4,135,969✔
463
          fobj2->f[0] = fobj1->f[0];
474,070✔
464
        }
465
      } else {
466
        if (fobj1->f->cid != fobj2->f->cid) {
464,285✔
467
          code = tsdbTFileObjRemove(fobj2);
406,243✔
468
          if (code) return code;
406,243✔
469
        } else {
470
          code = tsdbTFileObjRemoveUpdateLC(fobj2);
58,042✔
471
          if (code) return code;
58,042✔
472
        }
473
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
464,285✔
474
        if (code) return code;
464,285✔
475
      }
476
    } else if (fobj1) {
301,823✔
477
      // create a new file
478
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
301,823✔
479
      if (code) return code;
301,483✔
480
    } else {
481
      // remove the file
UNCOV
482
      code = tsdbTFileObjRemove(fobj2);
×
UNCOV
483
      if (code) return code;
×
UNCOV
484
      fset2->farr[ftype] = NULL;
×
485
    }
486
  }
487

488
  // stt part
489
  int32_t i1 = 0, i2 = 0;
4,746,047✔
490
  while (i1 < TARRAY2_SIZE(fset1->lvlArr) || i2 < TARRAY2_SIZE(fset2->lvlArr)) {
13,214,364✔
491
    SSttLvl *lvl1 = i1 < TARRAY2_SIZE(fset1->lvlArr) ? TARRAY2_GET(fset1->lvlArr, i1) : NULL;
8,467,977✔
492
    SSttLvl *lvl2 = i2 < TARRAY2_SIZE(fset2->lvlArr) ? TARRAY2_GET(fset2->lvlArr, i2) : NULL;
8,467,977✔
493

494
    if (lvl1 && lvl2) {
8,467,977✔
495
      if (lvl1->level < lvl2->level) {
8,046,287✔
496
        // add a new stt level
497
        code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
1,174,454✔
498
        if (code) return code;
1,174,454✔
499
        code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
1,174,454✔
500
        if (code) return code;
1,174,454✔
501
        i1++;
1,174,454✔
502
        i2++;
1,174,454✔
503
      } else if (lvl1->level > lvl2->level) {
6,871,833✔
504
        // remove the stt level
505
        TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
1,212,741✔
506
      } else {
507
        // apply edit on stt level
508
        code = tsdbSttLvlApplyEdit(pTsdb, lvl1, lvl2);
5,659,092✔
509
        if (code) return code;
5,659,092✔
510
        i1++;
5,659,092✔
511
        i2++;
5,659,092✔
512
      }
513
    } else if (lvl1) {
421,690✔
514
      // add a new stt level
515
      code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
228,462✔
516
      if (code) return code;
228,462✔
517
      code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
228,462✔
518
      if (code) return code;
228,462✔
519
      i1++;
228,462✔
520
      i2++;
228,462✔
521
    } else {
522
      // remove the stt level
523
      TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
193,228✔
524
    }
525
  }
526

527
  fset2->lastCompact = fset1->lastCompact;
4,746,047✔
528
  fset2->lastCommit = fset1->lastCommit;
4,746,047✔
529
  fset2->lastMigrate = fset1->lastMigrate;
4,746,047✔
530
  fset2->lastRollup = fset1->lastRollup;
4,746,047✔
531
  fset2->lastRollupLevel = fset1->lastRollupLevel;
4,746,047✔
532

533
  return 0;
4,746,047✔
534
}
535

536
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) {
356,788,572✔
537
  fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
356,788,572✔
538
  if (fset[0] == NULL) {
356,609,349✔
539
    return terrno;
×
540
  }
541

542
  fset[0]->fid = fid;
356,637,921✔
543
  fset[0]->maxVerValid = VERSION_MAX;
356,690,125✔
544
  TARRAY2_INIT(fset[0]->lvlArr);
356,694,171✔
545

546
  // block commit variables
547
  (void)taosThreadCondInit(&fset[0]->canCommit, NULL);
356,696,560✔
548
  (*fset)->numWaitCommit = 0;
356,747,427✔
549
  (*fset)->blockCommit = false;
356,769,565✔
550

551
  for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
1,070,001,732✔
552
    struct STFileSetCond *cond = &(*fset)->conds[i];
713,224,144✔
553
    cond->running = false;
713,240,825✔
554
    cond->numWait = 0;
713,273,097✔
555
    (void)taosThreadCondInit(&cond->cond, NULL);
713,320,947✔
556
  }
557

558
  return 0;
356,777,588✔
559
}
560

561
int32_t tsdbTFileSetInitCopy(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
24,837,391✔
562
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
24,837,391✔
563
  if (code) return code;
24,837,391✔
564

565
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
124,183,367✔
566
    if (fset1->farr[ftype] == NULL) continue;
99,346,574✔
567

568
    code = tsdbTFileObjInit(pTsdb, fset1->farr[ftype]->f, &fset[0]->farr[ftype]);
9,632,219✔
569
    if (code) {
9,632,219✔
570
      tsdbTFileSetClear(fset);
×
571
      return code;
×
572
    }
573
  }
574

575
  const SSttLvl *lvl1;
576
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
53,976,360✔
577
    SSttLvl *lvl;
29,138,322✔
578
    code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl);
29,139,182✔
579
    if (code) {
29,139,182✔
580
      tsdbTFileSetClear(fset);
×
581
      return code;
×
582
    }
583

584
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
29,139,182✔
585
    if (code) {
29,139,567✔
586
      tsdbTFileSetClear(fset);
×
587
      return code;
×
588
    }
589
  }
590

591
  (*fset)->lastCompact = fset1->lastCompact;
24,837,178✔
592
  (*fset)->lastCommit = fset1->lastCommit;
24,837,391✔
593
  (*fset)->lastMigrate = fset1->lastMigrate;
24,837,391✔
594
  (*fset)->lastRollup = fset1->lastRollup;
24,837,391✔
595
  (*fset)->lastRollupLevel = fset1->lastRollupLevel;
24,837,391✔
596

597
  return 0;
24,837,391✔
598
}
599

600
int32_t tsdbTFileSetFilteredInitDup(STsdb *pTsdb, const STFileSet *fset1, int64_t ever, STFileSet **fset,
354✔
601
                                    TFileOpArray *fopArr) {
602
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
354✔
603
  if (code) return code;
354✔
604

605
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
1,770✔
606
    if (fset1->farr[ftype] == NULL) continue;
1,416✔
607
    STFileObj *fobj = fset1->farr[ftype];
×
608
    if (fobj->f->maxVer <= ever) {
×
609
      code = tsdbTFileObjInit(pTsdb, fobj->f, &fset[0]->farr[ftype]);
×
610
      if (code) {
×
611
        tsdbTFileSetClear(fset);
×
612
        return code;
×
613
      }
614
    } else {
615
      STFileOp op = {
×
616
          .optype = TSDB_FOP_REMOVE,
617
          .fid = fobj->f->fid,
×
618
          .of = fobj->f[0],
619
      };
620
      code = TARRAY2_APPEND(fopArr, op);
×
621
      if (code) return code;
×
622
    }
623
  }
624

625
  const SSttLvl *lvl1;
626
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
708✔
627
    SSttLvl *lvl;
354✔
628
    code = tsdbSttLvlFilteredInitEx(pTsdb, lvl1, ever, &lvl, fopArr);
354✔
629
    if (code) {
354✔
630
      tsdbTFileSetClear(fset);
×
631
      return code;
×
632
    }
633

634
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
354✔
635
    if (code) return code;
354✔
636
  }
637

638
  return 0;
354✔
639
}
640

641
int32_t tsdbTFileSetRangeInitRef(STsdb *pTsdb, const STFileSet *fset1, int64_t sver, int64_t ever,
354✔
642
                                 STFileSetRange **fsr) {
643
  fsr[0] = taosMemoryCalloc(1, sizeof(*fsr[0]));
354✔
644
  if (fsr[0] == NULL) {
354✔
645
    return terrno;
×
646
  }
647
  fsr[0]->fid = fset1->fid;
354✔
648
  fsr[0]->sver = sver;
354✔
649
  fsr[0]->ever = ever;
354✔
650

651
  int32_t code = tsdbTFileSetInitRef(pTsdb, fset1, &fsr[0]->fset);
354✔
652
  if (code) {
354✔
653
    taosMemoryFree(fsr[0]);
×
654
    fsr[0] = NULL;
×
655
  }
656
  return code;
354✔
657
}
658

659
int32_t tsdbTFileSetInitRef(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
315,314,175✔
660
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
315,314,175✔
661
  if (code) return code;
315,382,542✔
662

663
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
1,576,419,409✔
664
    if (fset1->farr[ftype] == NULL) continue;
1,261,063,264✔
665

666
    code = tsdbTFileObjRef(fset1->farr[ftype]);
67,954,184✔
667
    if (code) {
68,017,670✔
668
      tsdbTFileSetClear(fset);
×
669
      return code;
×
670
    }
671
    fset[0]->farr[ftype] = fset1->farr[ftype];
68,017,670✔
672
  }
673

674
  const SSttLvl *lvl1;
675
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
625,483,555✔
676
    SSttLvl *lvl;
310,199,328✔
677
    code = tsdbSttLvlInitRef(pTsdb, lvl1, &lvl);
310,191,648✔
678
    if (code) {
310,138,168✔
679
      tsdbSttLvlClear(&lvl);
×
680
      tsdbTFileSetClear(fset);
×
681
      return code;
×
682
    }
683

684
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
310,138,168✔
685
    if (code) {
310,161,084✔
686
      tsdbSttLvlClear(&lvl);
×
687
      tsdbTFileSetClear(fset);
×
688
      return code;
×
689
    }
690
  }
691

692
  (*fset)->lastCompact = fset1->lastCompact;
315,355,246✔
693
  (*fset)->lastCommit = fset1->lastCommit;
315,378,004✔
694
  (*fset)->lastMigrate = fset1->lastMigrate;
315,325,231✔
695
  (*fset)->lastRollup = fset1->lastRollup;
315,333,518✔
696
  (*fset)->lastRollupLevel = fset1->lastRollupLevel;
315,354,111✔
697

698
  return 0;
315,374,169✔
699
}
700

701
void tsdbTFileSetRangeClear(STFileSetRange **fsr) {
1,062✔
702
  if (!fsr[0]) return;
1,062✔
703

704
  tsdbTFileSetClear(&fsr[0]->fset);
1,062✔
705
  taosMemoryFree(fsr[0]);
1,062✔
706
  fsr[0] = NULL;
1,062✔
707
  return;
1,062✔
708
}
709

710
void tsdbTFileSetRangeArrayDestroy(TFileSetRangeArray **ppArr) {
180,321✔
711
  if (ppArr && ppArr[0]) {
180,321✔
712
    TARRAY2_DESTROY(ppArr[0], tsdbTFileSetRangeClear);
61,405✔
713
    taosMemoryFree(ppArr[0]);
60,343✔
714
    ppArr[0] = NULL;
60,343✔
715
  }
716
}
180,321✔
717

718
void tsdbTFileSetClear(STFileSet **fset) {
372,637,332✔
719
  if (fset && *fset) {
372,637,332✔
720
    for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
1,784,120,725✔
721
      if ((*fset)->farr[ftype] == NULL) continue;
1,427,270,703✔
722
      int32_t code = tsdbTFileObjUnref((*fset)->farr[ftype]);
79,744,704✔
723
      if (code) {
79,761,019✔
724
        tsdbError("failed to unref file, fid:%d, ftype:%d", (*fset)->fid, ftype);
×
725
      }
726
      (*fset)->farr[ftype] = NULL;
79,761,019✔
727
    }
728

729
    TARRAY2_DESTROY((*fset)->lvlArr, tsdbSttLvlClear);
712,415,940✔
730

731
    (void)taosThreadCondDestroy(&(*fset)->canCommit);
356,838,606✔
732
    for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
1,070,517,138✔
733
      (void)taosThreadCondDestroy(&(*fset)->conds[i].cond);
713,653,402✔
734
    }
735
    taosMemoryFreeClear(*fset);
356,863,736✔
736
  }
737
}
372,630,101✔
738

739
void tsdbTFileSetRemove(STFileSet *fset) {
85,340✔
740
  if (fset == NULL) return;
85,340✔
741

742
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
426,700✔
743
    if (fset->farr[ftype] != NULL) {
341,360✔
744
      int32_t code = tsdbTFileObjRemove(fset->farr[ftype]);
7,095✔
745
      if (code) {
7,095✔
746
        tsdbError("failed to remove file, fid:%d, ftype:%d", fset->fid, ftype);
×
747
      }
748
      fset->farr[ftype] = NULL;
7,095✔
749
    }
750
  }
751

752
  TARRAY2_DESTROY(fset->lvlArr, tsdbSttLvlRemove);
112,158✔
753
}
754

755
int64_t tsdbTFileSetGetDataSize(const STFileSet *fset) {
158,178✔
756
  int64_t size = 0;
158,178✔
757
  if (fset->farr[TSDB_FTYPE_DATA]) {
158,178✔
758
    size += fset->farr[TSDB_FTYPE_DATA]->f->size;
75,176✔
759
  }
760

761
  SSttLvl *lvl;
762
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
241,180✔
763
    STFileObj *fobj;
764
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { size += fobj->f->size; }
169,644✔
765
  }
766
  return size;
158,178✔
767
}
768

769
SSttLvl *tsdbTFileSetGetSttLvl(STFileSet *fset, int32_t level) {
21,353,354✔
770
  SSttLvl   sttLvl = {.level = level};
21,353,354✔
771
  SSttLvl  *lvl = &sttLvl;
21,353,937✔
772
  SSttLvl **lvlPtr = TARRAY2_SEARCH(fset->lvlArr, &lvl, tsdbSttLvlCmprFn, TD_EQ);
21,353,339✔
773
  return lvlPtr ? lvlPtr[0] : NULL;
21,353,126✔
774
}
775

776
int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2) {
421,996,656✔
777
  if (fset1[0]->fid < fset2[0]->fid) return -1;
421,996,656✔
778
  if (fset1[0]->fid > fset2[0]->fid) return 1;
419,729,225✔
779
  return 0;
12,372,040✔
780
}
781

782
int64_t tsdbTFileSetMaxCid(const STFileSet *fset) {
743,712✔
783
  int64_t maxCid = 0;
743,712✔
784
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
3,719,996✔
785
    if (fset->farr[ftype] == NULL) continue;
2,975,566✔
786
    maxCid = TMAX(maxCid, fset->farr[ftype]->f->cid);
529,973✔
787
  }
788
  const SSttLvl   *lvl;
789
  const STFileObj *fobj;
790
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
1,501,884✔
791
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { maxCid = TMAX(maxCid, fobj->f->cid); }
1,547,283✔
792
  }
793
  return maxCid;
744,430✔
794
}
795

796
bool tsdbTFileSetIsEmpty(const STFileSet *fset) {
21,765,887✔
797
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
100,671,419✔
798
    if (fset->farr[ftype] != NULL) return false;
82,440,505✔
799
  }
800
  return TARRAY2_SIZE(fset->lvlArr) == 0;
18,230,914✔
801
}
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