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

taosdata / TDengine / #3569

28 Dec 2024 03:56AM UTC coverage: 62.96% (-0.3%) from 63.286%
#3569

push

travis-ci

web-flow
Merge pull request #29369 from taosdata/enh/TS-4994-more-test-case

enh: add more cases

139174 of 284303 branches covered (48.95%)

Branch coverage included in aggregate %.

26 of 32 new or added lines in 3 files covered. (81.25%)

1505 existing lines in 138 files now uncovered.

217071 of 281522 relevant lines covered (77.11%)

18800589.91 hits per line

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

62.54
/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) {
7,942,221✔
20
  if (!(lvl[0] = taosMemoryMalloc(sizeof(SSttLvl)))) {
7,942,221!
21
    return terrno;
×
22
  }
23
  lvl[0]->level = level;
7,947,479✔
24
  TARRAY2_INIT(lvl[0]->fobjArr);
7,947,479✔
25
  return 0;
7,947,479✔
26
}
27

28
static void tsdbSttLvlClearFObj(void *data) { TAOS_UNUSED(tsdbTFileObjUnref(*(STFileObj **)data)); }
7,976,599✔
29

30
void tsdbSttLvlClear(SSttLvl **lvl) {
7,946,283✔
31
  if (lvl[0] != NULL) {
7,946,283!
32
    TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlClearFObj);
15,915,790!
33
    taosMemoryFree(lvl[0]);
7,946,566!
34
    lvl[0] = NULL;
7,946,909✔
35
  }
36
}
7,946,638✔
37

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

42
  const STFileObj *fobj1;
43
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
351,989✔
44
    STFileObj *fobj;
45
    code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj);
183,240✔
46
    if (code) {
183,313✔
47
      tsdbSttLvlClear(lvl);
2✔
48
      return code;
×
49
    }
50

51
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
183,311✔
52
    if (code) {
183,322!
53
      tsdbSttLvlClear(lvl);
×
54
      taosMemoryFree(fobj);
×
55
      return code;
×
56
    }
57
  }
58
  return 0;
168,749✔
59
}
60

61
static int32_t tsdbSttLvlInitRef(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
7,640,663✔
62
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
7,640,663✔
63
  if (code) return code;
7,646,133!
64

65
  STFileObj *fobj1;
66
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
15,301,987✔
67
    code = tsdbTFileObjRef(fobj1);
7,655,392✔
68
    if (code) {
7,658,916✔
69
      tsdbSttLvlClear(lvl);
1,576✔
70
      return code;
×
71
    }
72
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj1);
7,657,340✔
73
    if (code) {
7,655,854!
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;
7,646,595✔
82
}
83

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

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

UNCOV
99
      TAOS_CHECK_RETURN(TARRAY2_APPEND(lvl[0]->fobjArr, fobj));
×
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
  }
UNCOV
109
  return 0;
×
110
}
111

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

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

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

131
  int32_t i1 = 0, i2 = 0;
20,516✔
132
  while (i1 < TARRAY2_SIZE(lvl1->fobjArr) || i2 < TARRAY2_SIZE(lvl2->fobjArr)) {
52,570!
133
    STFileObj *fobj1 = i1 < TARRAY2_SIZE(lvl1->fobjArr) ? TARRAY2_GET(lvl1->fobjArr, i1) : NULL;
32,054!
134
    STFileObj *fobj2 = i2 < TARRAY2_SIZE(lvl2->fobjArr) ? TARRAY2_GET(lvl2->fobjArr, i2) : NULL;
32,054✔
135

136
    if (fobj1 && fobj2) {
32,054!
137
      if (fobj1->f->cid < fobj2->f->cid) {
27,981!
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) {
27,981✔
146
        // remove a file obj
147
        TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
1,286!
148
      } else {
149
        if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
26,695!
150
          if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
26,695!
151
            fobj2->f[0] = fobj1->f[0];
×
152
          }
153
        } else {
154
          TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
155
          code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
×
156
          if (code) return code;
×
157
          code = TARRAY2_SORT_INSERT(lvl2->fobjArr, fobj2, tsdbTFileObjCmpr);
×
158
          if (code) return code;
×
159
        }
160
        i1++;
26,695✔
161
        i2++;
26,695✔
162
      }
163
    } else if (fobj1) {
4,073!
164
      // create a file obj
165
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
4,073✔
166
      if (code) return code;
4,073!
167
      code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
4,073!
168
      if (code) return code;
4,073!
169
      i1++;
4,073✔
170
      i2++;
4,073✔
171
    } else {
172
      // remove a file obj
173
      TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
174
    }
175
  }
176
  return 0;
20,516✔
177
}
178

179
static int32_t tsdbSttLvlCmprFn(const SSttLvl **lvl1, const SSttLvl **lvl2) {
21,260✔
180
  if (lvl1[0]->level < lvl2[0]->level) return -1;
21,260✔
181
  if (lvl1[0]->level > lvl2[0]->level) return 1;
16,004✔
182
  return 0;
11,549✔
183
}
184

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

190
  cJSON *ajson = cJSON_AddArrayToObject(json, "files");
151,305✔
191
  if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
151,313!
192
  const STFileObj *fobj;
193
  TARRAY2_FOREACH(lvl->fobjArr, fobj) {
312,882✔
194
    cJSON *item = cJSON_CreateObject();
161,565✔
195
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
161,547!
196
    (void)cJSON_AddItemToArray(ajson, item);
161,547✔
197

198
    int32_t code = tsdbTFileToJson(fobj->f, item);
161,541✔
199
    if (code) return code;
161,569!
200
  }
201

202
  return 0;
151,317✔
203
}
204

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

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

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

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

225
  cJSON_ArrayForEach(item2, item1) {
3,091!
226
    STFile tf;
227
    code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &tf);
1,548✔
228
    if (code) {
1,547!
229
      tsdbSttLvlClear(lvl);
×
230
      return code;
×
231
    }
232

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

240
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
1,548✔
241
    if (code) return code;
1,547!
242
  }
243
  TARRAY2_SORT(lvl[0]->fobjArr, tsdbTFileObjCmpr);
1,543✔
244
  return 0;
1,543✔
245
}
246

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

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

256
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
753,231✔
257
    if (fset->farr[ftype] == NULL) continue;
602,583✔
258

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

263
  // each level
264
  item1 = cJSON_AddArrayToObject(json, "stt lvl");
150,648✔
265
  if (item1 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
150,636!
266
  const SSttLvl *lvl;
267
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
301,953✔
268
    item2 = cJSON_CreateObject();
151,305✔
269
    if (!item2) return TSDB_CODE_OUT_OF_MEMORY;
151,307!
270
    (void)cJSON_AddItemToArray(item1, item2);
151,307✔
271

272
    code = tsdbSttLvlToJson(lvl, item2);
151,304✔
273
    if (code) return code;
151,317!
274
  }
275

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

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

285
  return 0;
150,645✔
286
}
287

288
int32_t tsdbJsonToTFileSet(STsdb *pTsdb, const cJSON *json, STFileSet **fset) {
1,516✔
289
  int32_t      code;
290
  const cJSON *item1, *item2;
291
  int32_t      fid;
292
  STFile       tf;
293

294
  // fid
295
  item1 = cJSON_GetObjectItem(json, "fid");
1,516✔
296
  if (cJSON_IsNumber(item1)) {
1,523!
297
    fid = item1->valuedouble;
1,522✔
298
  } else {
299
    return TSDB_CODE_FILE_CORRUPTED;
×
300
  }
301

302
  code = tsdbTFileSetInit(fid, fset);
1,522✔
303
  if (code) return code;
1,523!
304

305
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
7,600✔
306
    code = tsdbJsonToTFile(json, ftype, &tf);
6,078✔
307
    if (code == TSDB_CODE_NOT_FOUND) {
6,076✔
308
      continue;
5,103✔
309
    } else if (code) {
973!
310
      tsdbTFileSetClear(fset);
×
311
      return code;
×
312
    } else {
313
      code = tsdbTFileObjInit(pTsdb, &tf, &(*fset)->farr[ftype]);
973✔
314
      if (code) return code;
974!
315
    }
316
  }
317

318
  // each level
319
  item1 = cJSON_GetObjectItem(json, "stt lvl");
1,522✔
320
  if (cJSON_IsArray(item1)) {
1,523!
321
    cJSON_ArrayForEach(item2, item1) {
3,066!
322
      SSttLvl *lvl;
323
      code = tsdbJsonToSttLvl(pTsdb, item2, &lvl);
1,544✔
324
      if (code) {
1,543✔
325
        tsdbTFileSetClear(fset);
1✔
326
        return code;
×
327
      }
328

329
      code = TARRAY2_APPEND((*fset)->lvlArr, lvl);
1,542✔
330
      if (code) return code;
1,543!
331
    }
332
    TARRAY2_SORT((*fset)->lvlArr, tsdbSttLvlCmprFn);
1,522✔
333
  } else {
334
    return TSDB_CODE_FILE_CORRUPTED;
×
335
  }
336
  // about compact and commit
337
  item1 = cJSON_GetObjectItem(json, "last compact");
1,522✔
338
  if (cJSON_IsNumber(item1)) {
1,522✔
339
    (*fset)->lastCompact = item1->valuedouble;
1,370✔
340
  } else {
341
    (*fset)->lastCompact = 0;
152✔
342
  }
343

344
  item1 = cJSON_GetObjectItem(json, "last commit");
1,522✔
345
  if (cJSON_IsNumber(item1)) {
1,522✔
346
    (*fset)->lastCommit = item1->valuedouble;
1,370✔
347
  } else {
348
    (*fset)->lastCommit = 0;
152✔
349
  }
350

351
  return 0;
1,522✔
352
}
353

354
// NOTE: the api does not remove file, only do memory operation
355
int32_t tsdbTFileSetEdit(STsdb *pTsdb, STFileSet *fset, const STFileOp *op) {
154,356✔
356
  int32_t code = 0;
154,356✔
357

358
  if (op->optype == TSDB_FOP_CREATE) {
154,356✔
359
    // create a new file
360
    STFileObj *fobj;
361
    code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
143,818✔
362
    if (code) return code;
143,935!
363

364
    if (fobj->f->type == TSDB_FTYPE_STT) {
143,935✔
365
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, fobj->f->stt->level);
134,839✔
366
      if (!lvl) {
134,831✔
367
        code = tsdbSttLvlInit(fobj->f->stt->level, &lvl);
130,756✔
368
        if (code) return code;
130,782!
369

370
        code = TARRAY2_SORT_INSERT(fset->lvlArr, lvl, tsdbSttLvlCmprFn);
130,782✔
371
        if (code) return code;
130,799!
372
      }
373

374
      code = TARRAY2_SORT_INSERT(lvl->fobjArr, fobj, tsdbTFileObjCmpr);
134,874✔
375
      if (code) return code;
134,869!
376
    } else {
377
      fset->farr[fobj->f->type] = fobj;
9,096✔
378
    }
379
  } else if (op->optype == TSDB_FOP_REMOVE) {
10,538✔
380
    // delete a file
381
    if (op->of.type == TSDB_FTYPE_STT) {
8,917✔
382
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
7,475✔
383

384
      STFileObj  tfobj = {.f[0] = {.cid = op->of.cid}};
7,475✔
385
      STFileObj *tfobjp = &tfobj;
7,475✔
386
      int32_t    idx = TARRAY2_SEARCH_IDX(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
7,475✔
387
      TARRAY2_REMOVE(lvl->fobjArr, idx, tsdbSttLvlClearFObj);
7,475!
388
    } else {
389
      code = tsdbTFileObjUnref(fset->farr[op->of.type]);
1,442✔
390
      if (code) return code;
1,442!
391
      fset->farr[op->of.type] = NULL;
1,442✔
392
    }
393
  } else {
394
    if (op->nf.type == TSDB_FTYPE_STT) {
1,621!
395
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
×
396

397
      STFileObj   tfobj = {.f[0] = {.cid = op->of.cid}}, *tfobjp = &tfobj;
×
398
      STFileObj **fobjPtr = TARRAY2_SEARCH(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
×
399
      if (fobjPtr) {
×
400
        tfobjp = *fobjPtr;
×
401
        tfobjp->f[0] = op->nf;
×
402
      } else {
403
        tsdbError("file not found, cid:%" PRId64, op->of.cid);
×
404
      }
405
    } else {
406
      fset->farr[op->nf.type]->f[0] = op->nf;
1,621✔
407
    }
408
  }
409

410
  return 0;
154,503✔
411
}
412

413
int32_t tsdbTFileSetApplyEdit(STsdb *pTsdb, const STFileSet *fset1, STFileSet *fset2) {
22,113✔
414
  int32_t code = 0;
22,113✔
415

416
  if (fset1->fid != fset2->fid) {
22,113!
417
    return TSDB_CODE_INVALID_PARA;
×
418
  }
419

420
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
110,565✔
421
    if (!fset1->farr[ftype] && !fset2->farr[ftype]) continue;
88,452!
422

423
    STFileObj *fobj1 = fset1->farr[ftype];
38,302✔
424
    STFileObj *fobj2 = fset2->farr[ftype];
38,302✔
425

426
    if (fobj1 && fobj2) {
38,302!
427
      if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
34,221✔
428
        if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
32,779✔
429
          fobj2->f[0] = fobj1->f[0];
1,621✔
430
        }
431
      } else {
432
        if (fobj1->f->cid != fobj2->f->cid) {
1,442!
433
          code = tsdbTFileObjRemove(fobj2);
1,442✔
434
          if (code) return code;
1,442!
435
        } else {
436
          code = tsdbTFileObjRemoveUpdateLC(fobj2);
×
437
          if (code) return code;
×
438
        }
439
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
1,442✔
440
        if (code) return code;
1,442!
441
      }
442
    } else if (fobj1) {
4,081!
443
      // create a new file
444
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
4,081✔
445
      if (code) return code;
4,081!
446
    } else {
447
      // remove the file
448
      code = tsdbTFileObjRemove(fobj2);
×
449
      if (code) return code;
×
450
      fset2->farr[ftype] = NULL;
×
451
    }
452
  }
453

454
  // stt part
455
  int32_t i1 = 0, i2 = 0;
22,113✔
456
  while (i1 < TARRAY2_SIZE(fset1->lvlArr) || i2 < TARRAY2_SIZE(fset2->lvlArr)) {
49,450✔
457
    SSttLvl *lvl1 = i1 < TARRAY2_SIZE(fset1->lvlArr) ? TARRAY2_GET(fset1->lvlArr, i1) : NULL;
27,337✔
458
    SSttLvl *lvl2 = i2 < TARRAY2_SIZE(fset2->lvlArr) ? TARRAY2_GET(fset2->lvlArr, i2) : NULL;
27,337✔
459

460
    if (lvl1 && lvl2) {
27,337✔
461
      if (lvl1->level < lvl2->level) {
24,071✔
462
        // add a new stt level
463
        code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
1,426✔
464
        if (code) return code;
1,426!
465
        code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
1,426✔
466
        if (code) return code;
1,426!
467
        i1++;
1,426✔
468
        i2++;
1,426✔
469
      } else if (lvl1->level > lvl2->level) {
22,645✔
470
        // remove the stt level
471
        TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
2,129!
472
      } else {
473
        // apply edit on stt level
474
        code = tsdbSttLvlApplyEdit(pTsdb, lvl1, lvl2);
20,516✔
475
        if (code) return code;
20,516!
476
        i1++;
20,516✔
477
        i2++;
20,516✔
478
      }
479
    } else if (lvl1) {
3,266✔
480
      // add a new stt level
481
      code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
1,973✔
482
      if (code) return code;
1,973!
483
      code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
1,973✔
484
      if (code) return code;
1,973!
485
      i1++;
1,973✔
486
      i2++;
1,973✔
487
    } else {
488
      // remove the stt level
489
      TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
1,293!
490
    }
491
  }
492

493
  fset2->lastCompact = fset1->lastCompact;
22,113✔
494
  fset2->lastCommit = fset1->lastCommit;
22,113✔
495

496
  return 0;
22,113✔
497
}
498

499
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) {
8,020,124✔
500
  fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
8,020,124!
501
  if (fset[0] == NULL) {
8,013,902!
502
    return terrno;
×
503
  }
504

505
  fset[0]->fid = fid;
8,013,902✔
506
  fset[0]->maxVerValid = VERSION_MAX;
8,013,902✔
507
  TARRAY2_INIT(fset[0]->lvlArr);
8,013,902✔
508

509
  // block commit variables
510
  (void)taosThreadCondInit(&fset[0]->canCommit, NULL);
8,013,902✔
511
  (*fset)->numWaitCommit = 0;
8,017,070✔
512
  (*fset)->blockCommit = false;
8,017,070✔
513

514
  for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
24,038,544✔
515
    struct STFileSetCond *cond = &(*fset)->conds[i];
16,023,668✔
516
    cond->running = false;
16,023,668✔
517
    cond->numWait = 0;
16,023,668✔
518
    (void)taosThreadCondInit(&cond->cond, NULL);
16,023,668✔
519
  }
520

521
  return 0;
8,014,876✔
522
}
523

524
int32_t tsdbTFileSetInitCopy(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
163,193✔
525
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
163,193✔
526
  if (code) return code;
163,042!
527

528
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
815,274✔
529
    if (fset1->farr[ftype] == NULL) continue;
652,205✔
530

531
    code = tsdbTFileObjInit(pTsdb, fset1->farr[ftype]->f, &fset[0]->farr[ftype]);
56,774✔
532
    if (code) {
56,779!
UNCOV
533
      tsdbTFileSetClear(fset);
×
534
      return code;
×
535
    }
536
  }
537

538
  const SSttLvl *lvl1;
539
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
328,415✔
540
    SSttLvl *lvl;
541
    code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl);
165,164✔
542
    if (code) {
165,340!
UNCOV
543
      tsdbTFileSetClear(fset);
×
544
      return code;
×
545
    }
546

547
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
165,342✔
548
    if (code) return code;
165,346!
549
  }
550

551
  (*fset)->lastCompact = fset1->lastCompact;
163,251✔
552
  (*fset)->lastCommit = fset1->lastCommit;
163,251✔
553

554
  return 0;
163,251✔
555
}
556

UNCOV
557
int32_t tsdbTFileSetFilteredInitDup(STsdb *pTsdb, const STFileSet *fset1, int64_t ever, STFileSet **fset,
×
558
                                    TFileOpArray *fopArr) {
UNCOV
559
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
×
UNCOV
560
  if (code) return code;
×
561

UNCOV
562
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
563
    if (fset1->farr[ftype] == NULL) continue;
×
UNCOV
564
    STFileObj *fobj = fset1->farr[ftype];
×
UNCOV
565
    if (fobj->f->maxVer <= ever) {
×
UNCOV
566
      code = tsdbTFileObjInit(pTsdb, fobj->f, &fset[0]->farr[ftype]);
×
UNCOV
567
      if (code) {
×
568
        tsdbTFileSetClear(fset);
×
569
        return code;
×
570
      }
571
    } else {
572
      STFileOp op = {
×
573
          .optype = TSDB_FOP_REMOVE,
574
          .fid = fobj->f->fid,
×
575
          .of = fobj->f[0],
576
      };
577
      code = TARRAY2_APPEND(fopArr, op);
×
578
      if (code) return code;
×
579
    }
580
  }
581

582
  const SSttLvl *lvl1;
UNCOV
583
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
×
584
    SSttLvl *lvl;
UNCOV
585
    code = tsdbSttLvlFilteredInitEx(pTsdb, lvl1, ever, &lvl, fopArr);
×
UNCOV
586
    if (code) {
×
587
      tsdbTFileSetClear(fset);
×
588
      return code;
×
589
    }
590

UNCOV
591
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
×
UNCOV
592
    if (code) return code;
×
593
  }
594

UNCOV
595
  return 0;
×
596
}
597

UNCOV
598
int32_t tsdbTFileSetRangeInitRef(STsdb *pTsdb, const STFileSet *fset1, int64_t sver, int64_t ever,
×
599
                                 STFileSetRange **fsr) {
UNCOV
600
  fsr[0] = taosMemoryCalloc(1, sizeof(*fsr[0]));
×
UNCOV
601
  if (fsr[0] == NULL) {
×
602
    return terrno;
×
603
  }
UNCOV
604
  fsr[0]->fid = fset1->fid;
×
UNCOV
605
  fsr[0]->sver = sver;
×
UNCOV
606
  fsr[0]->ever = ever;
×
607

UNCOV
608
  int32_t code = tsdbTFileSetInitRef(pTsdb, fset1, &fsr[0]->fset);
×
UNCOV
609
  if (code) {
×
610
    taosMemoryFree(fsr[0]);
×
611
    fsr[0] = NULL;
×
612
  }
UNCOV
613
  return code;
×
614
}
615

616
int32_t tsdbTFileSetInitRef(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
7,726,944✔
617
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
7,726,944✔
618
  if (code) return code;
7,720,468!
619

620
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
38,611,822✔
621
    if (fset1->farr[ftype] == NULL) continue;
30,887,498✔
622

623
    code = tsdbTFileObjRef(fset1->farr[ftype]);
458,376✔
624
    if (code) {
462,235✔
625
      tsdbTFileSetClear(fset);
3✔
626
      return code;
×
627
    }
628
    fset[0]->farr[ftype] = fset1->farr[ftype];
462,232✔
629
  }
630

631
  const SSttLvl *lvl1;
632
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
15,368,219✔
633
    SSttLvl *lvl;
634
    code = tsdbSttLvlInitRef(pTsdb, lvl1, &lvl);
7,639,731✔
635
    if (code) {
7,642,476!
636
      tsdbSttLvlClear(&lvl);
×
637
      tsdbTFileSetClear(fset);
×
638
      return code;
×
639
    }
640

641
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
7,644,935✔
642
    if (code) {
7,643,895!
643
      tsdbSttLvlClear(&lvl);
×
644
      tsdbTFileSetClear(fset);
×
645
      return code;
×
646
    }
647
  }
648

649
  (*fset)->lastCompact = fset1->lastCompact;
7,728,488✔
650
  (*fset)->lastCommit = fset1->lastCommit;
7,728,488✔
651

652
  return 0;
7,728,488✔
653
}
654

UNCOV
655
void tsdbTFileSetRangeClear(STFileSetRange **fsr) {
×
UNCOV
656
  if (!fsr[0]) return;
×
657

UNCOV
658
  tsdbTFileSetClear(&fsr[0]->fset);
×
UNCOV
659
  taosMemoryFree(fsr[0]);
×
UNCOV
660
  fsr[0] = NULL;
×
UNCOV
661
  return;
×
662
}
663

664
void tsdbTFileSetRangeArrayDestroy(TFileSetRangeArray **ppArr) {
438✔
665
  if (ppArr && ppArr[0]) {
438!
666
    TARRAY2_DESTROY(ppArr[0], tsdbTFileSetRangeClear);
146!
667
    taosMemoryFree(ppArr[0]);
146!
668
    ppArr[0] = NULL;
146✔
669
  }
670
}
438✔
671

672
void tsdbTFileSetClear(STFileSet **fset) {
8,154,176✔
673
  if (fset && *fset) {
8,154,176!
674
    for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
40,129,626✔
675
      if ((*fset)->farr[ftype] == NULL) continue;
32,103,014✔
676
      int32_t code = tsdbTFileObjUnref((*fset)->farr[ftype]);
532,168✔
677
      if (code) {
532,838!
678
        tsdbError("failed to unref file, fid:%d, ftype:%d", (*fset)->fid, ftype);
×
679
      }
680
      (*fset)->farr[ftype] = NULL;
532,812✔
681
    }
682

683
    TARRAY2_DESTROY((*fset)->lvlArr, tsdbSttLvlClear);
15,969,984!
684

685
    (void)taosThreadCondDestroy(&(*fset)->canCommit);
8,026,227✔
686
    for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
24,077,849✔
687
      (void)taosThreadCondDestroy(&(*fset)->conds[i].cond);
16,051,520✔
688
    }
689
    taosMemoryFreeClear(*fset);
8,026,329!
690
  }
691
}
8,154,147✔
692

693
void tsdbTFileSetRemove(STFileSet *fset) {
×
694
  if (fset == NULL) return;
×
695

696
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
697
    if (fset->farr[ftype] != NULL) {
×
698
      int32_t code = tsdbTFileObjRemove(fset->farr[ftype]);
×
699
      if (code) {
×
700
        tsdbError("failed to remove file, fid:%d, ftype:%d", fset->fid, ftype);
×
701
      }
702
      fset->farr[ftype] = NULL;
×
703
    }
704
  }
705

706
  TARRAY2_DESTROY(fset->lvlArr, tsdbSttLvlRemove);
×
707
}
708

709
SSttLvl *tsdbTFileSetGetSttLvl(STFileSet *fset, int32_t level) {
142,314✔
710
  SSttLvl   sttLvl = {.level = level};
142,314✔
711
  SSttLvl  *lvl = &sttLvl;
142,314✔
712
  SSttLvl **lvlPtr = TARRAY2_SEARCH(fset->lvlArr, &lvl, tsdbSttLvlCmprFn, TD_EQ);
142,314✔
713
  return lvlPtr ? lvlPtr[0] : NULL;
142,308✔
714
}
715

716
int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2) {
3,629,965✔
717
  if (fset1[0]->fid < fset2[0]->fid) return -1;
3,629,965✔
718
  if (fset1[0]->fid > fset2[0]->fid) return 1;
3,616,963✔
719
  return 0;
37,739✔
720
}
721

722
int64_t tsdbTFileSetMaxCid(const STFileSet *fset) {
1,520✔
723
  int64_t maxCid = 0;
1,520✔
724
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
7,603✔
725
    if (fset->farr[ftype] == NULL) continue;
6,083✔
726
    maxCid = TMAX(maxCid, fset->farr[ftype]->f->cid);
974✔
727
  }
728
  const SSttLvl   *lvl;
729
  const STFileObj *fobj;
730
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
3,063✔
731
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { maxCid = TMAX(maxCid, fobj->f->cid); }
3,090✔
732
  }
733
  return maxCid;
1,520✔
734
}
735

736
bool tsdbTFileSetIsEmpty(const STFileSet *fset) {
154,975✔
737
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
718,346✔
738
    if (fset->farr[ftype] != NULL) return false;
577,660✔
739
  }
740
  return TARRAY2_SIZE(fset->lvlArr) == 0;
140,686✔
741
}
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