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

taosdata / TDengine / #3942

25 Apr 2025 11:21AM UTC coverage: 62.853% (+0.3%) from 62.507%
#3942

push

travis-ci

web-flow
docs: jdbc tmq supports database subscription. [TS-6222] (#30819)

* docs: jdbc tmq supports database subscription. [TS-6222]

* Update docs/zh/07-develop/07-tmq.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update 07-tmq.md

---------

Co-authored-by: haoranchen <haoran920c@163.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

156603 of 317531 branches covered (49.32%)

Branch coverage included in aggregate %.

241895 of 316485 relevant lines covered (76.43%)

6664240.48 hits per line

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

65.8
/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) {
3,899,083✔
20
  if (!(lvl[0] = taosMemoryMalloc(sizeof(SSttLvl)))) {
3,899,083!
21
    return terrno;
×
22
  }
23
  lvl[0]->level = level;
3,910,561✔
24
  TARRAY2_INIT(lvl[0]->fobjArr);
3,910,561✔
25
  return 0;
3,910,561✔
26
}
27

28
static void tsdbSttLvlClearFObj(void *data) { TAOS_UNUSED(tsdbTFileObjUnref(*(STFileObj **)data)); }
3,922,148✔
29

30
void tsdbSttLvlClear(SSttLvl **lvl) {
3,910,784✔
31
  if (lvl[0] != NULL) {
3,910,784!
32
    TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlClearFObj);
7,824,788!
33
    taosMemoryFree(lvl[0]);
3,911,885!
34
    lvl[0] = NULL;
3,912,421✔
35
  }
36
}
3,912,307✔
37

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

42
  const STFileObj *fobj1;
43
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
769,575✔
44
    STFileObj *fobj;
45
    code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj);
388,048✔
46
    if (code) {
388,155!
47
      tsdbSttLvlClear(lvl);
×
48
      return code;
×
49
    }
50

51
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
388,159✔
52
    if (code) {
388,002!
53
      tsdbSttLvlClear(lvl);
×
54
      taosMemoryFree(fobj);
×
55
      return code;
×
56
    }
57
  }
58
  return 0;
381,527✔
59
}
60

61
static int32_t tsdbSttLvlInitRef(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
3,171,922✔
62
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
3,171,922✔
63
  if (code) return code;
3,182,979!
64

65
  STFileObj *fobj1;
66
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
6,366,379✔
67
    code = tsdbTFileObjRef(fobj1);
3,182,897✔
68
    if (code) {
3,185,234✔
69
      tsdbSttLvlClear(lvl);
450✔
70
      return code;
×
71
    }
72
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj1);
3,184,784✔
73
    if (code) {
3,183,400!
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;
3,183,482✔
82
}
83

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

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

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
  }
109
  return 0;
×
110
}
111

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

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

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

131
  int32_t i1 = 0, i2 = 0;
14,758✔
132
  while (i1 < TARRAY2_SIZE(lvl1->fobjArr) || i2 < TARRAY2_SIZE(lvl2->fobjArr)) {
34,179!
133
    STFileObj *fobj1 = i1 < TARRAY2_SIZE(lvl1->fobjArr) ? TARRAY2_GET(lvl1->fobjArr, i1) : NULL;
19,421!
134
    STFileObj *fobj2 = i2 < TARRAY2_SIZE(lvl2->fobjArr) ? TARRAY2_GET(lvl2->fobjArr, i2) : NULL;
19,421✔
135

136
    if (fobj1 && fobj2) {
19,421!
137
      if (fobj1->f->cid < fobj2->f->cid) {
15,221!
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) {
15,221✔
146
        // remove a file obj
147
        TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
1,214!
148
      } else {
149
        if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
14,007✔
150
          if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
13,970!
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++;
14,007✔
161
        i2++;
14,007✔
162
      }
163
    } else if (fobj1) {
4,200!
164
      // create a file obj
165
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
4,201✔
166
      if (code) return code;
4,201!
167
      code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
4,201!
168
      if (code) return code;
4,201!
169
      i1++;
4,201✔
170
      i2++;
4,201✔
171
    } else {
172
      // remove a file obj
173
      TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
174
    }
175
  }
176
  return 0;
14,758✔
177
}
178

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

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

190
  cJSON *ajson = cJSON_AddArrayToObject(json, "files");
358,594✔
191
  if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
358,596!
192
  const STFileObj *fobj;
193
  TARRAY2_FOREACH(lvl->fobjArr, fobj) {
720,648✔
194
    cJSON *item = cJSON_CreateObject();
362,041✔
195
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
362,059!
196
    (void)cJSON_AddItemToArray(ajson, item);
362,059✔
197

198
    int32_t code = tsdbTFileToJson(fobj->f, item);
362,056✔
199
    if (code) return code;
362,052!
200
  }
201

202
  return 0;
358,607✔
203
}
204

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

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

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

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

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

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

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

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

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

256
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
1,765,875✔
257
    if (fset->farr[ftype] == NULL) continue;
1,412,696✔
258

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

263
  // each level
264
  item1 = cJSON_AddArrayToObject(json, "stt lvl");
353,179✔
265
  if (item1 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
353,187!
266
  const SSttLvl *lvl;
267
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
711,789✔
268
    item2 = cJSON_CreateObject();
358,611✔
269
    if (!item2) return TSDB_CODE_OUT_OF_MEMORY;
358,620!
270
    (void)cJSON_AddItemToArray(item1, item2);
358,620✔
271

272
    code = tsdbSttLvlToJson(lvl, item2);
358,610✔
273
    if (code) return code;
358,602!
274
  }
275

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

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

285
  return 0;
353,182✔
286
}
287

288
int32_t tsdbJsonToTFileSet(STsdb *pTsdb, const cJSON *json, STFileSet **fset) {
1,954✔
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,954✔
296
  if (cJSON_IsNumber(item1)) {
1,971!
297
    fid = item1->valuedouble;
1,972✔
298
  } else {
299
    return TSDB_CODE_FILE_CORRUPTED;
×
300
  }
301

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

305
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
9,840✔
306
    code = tsdbJsonToTFile(json, ftype, &tf);
7,864✔
307
    if (code == TSDB_CODE_NOT_FOUND) {
7,878✔
308
      continue;
6,670✔
309
    } else if (code) {
1,208!
310
      tsdbTFileSetClear(fset);
×
311
      return code;
×
312
    } else {
313
      code = tsdbTFileObjInit(pTsdb, &tf, &(*fset)->farr[ftype]);
1,208✔
314
      if (code) return code;
1,204!
315
    }
316
  }
317

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

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

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

351
  return 0;
1,972✔
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) {
364,905✔
356
  int32_t code = 0;
364,905✔
357

358
  if (op->optype == TSDB_FOP_CREATE) {
364,905✔
359
    // create a new file
360
    STFileObj *fobj;
361
    code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
354,245✔
362
    if (code) return code;
354,369!
363

364
    if (fobj->f->type == TSDB_FTYPE_STT) {
354,369✔
365
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, fobj->f->stt->level);
348,094✔
366
      if (!lvl) {
348,069✔
367
        code = tsdbSttLvlInit(fobj->f->stt->level, &lvl);
343,832✔
368
        if (code) return code;
343,813!
369

370
        code = TARRAY2_SORT_INSERT(fset->lvlArr, lvl, tsdbSttLvlCmprFn);
343,813✔
371
        if (code) return code;
343,833!
372
      }
373

374
      code = TARRAY2_SORT_INSERT(lvl->fobjArr, fobj, tsdbTFileObjCmpr);
348,070✔
375
      if (code) return code;
348,077!
376
    } else {
377
      fset->farr[fobj->f->type] = fobj;
6,275✔
378
    }
379
  } else if (op->optype == TSDB_FOP_REMOVE) {
10,660✔
380
    // delete a file
381
    if (op->of.type == TSDB_FTYPE_STT) {
9,752✔
382
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
8,497✔
383

384
      STFileObj  tfobj = {.f[0] = {.cid = op->of.cid}};
8,497✔
385
      STFileObj *tfobjp = &tfobj;
8,497✔
386
      int32_t    idx = TARRAY2_SEARCH_IDX(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
8,497✔
387
      TARRAY2_REMOVE(lvl->fobjArr, idx, tsdbSttLvlClearFObj);
8,497!
388
    } else {
389
      code = tsdbTFileObjUnref(fset->farr[op->of.type]);
1,255✔
390
      if (code) return code;
1,255!
391
      fset->farr[op->of.type] = NULL;
1,255✔
392
    }
393
  } else {
394
    if (op->nf.type == TSDB_FTYPE_STT) {
908!
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;
908✔
407
    }
408
  }
409

410
  return 0;
365,012✔
411
}
412

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

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

420
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
61,106✔
421
    if (!fset1->farr[ftype] && !fset2->farr[ftype]) continue;
48,884✔
422

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

426
    if (fobj1 && fobj2) {
14,756✔
427
      if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
13,779✔
428
        if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
12,595✔
429
          fobj2->f[0] = fobj1->f[0];
908✔
430
        }
431
      } else {
432
        if (fobj1->f->cid != fobj2->f->cid) {
1,184✔
433
          code = tsdbTFileObjRemove(fobj2);
1,064✔
434
          if (code) return code;
1,064!
435
        } else {
436
          code = tsdbTFileObjRemoveUpdateLC(fobj2);
120✔
437
          if (code) return code;
120!
438
        }
439
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
1,184✔
440
        if (code) return code;
1,184!
441
      }
442
    } else if (fobj1) {
977✔
443
      // create a new file
444
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
921✔
445
      if (code) return code;
921!
446
    } else {
447
      // remove the file
448
      code = tsdbTFileObjRemove(fobj2);
56✔
449
      if (code) return code;
56!
450
      fset2->farr[ftype] = NULL;
56✔
451
    }
452
  }
453

454
  // stt part
455
  int32_t i1 = 0, i2 = 0;
12,222✔
456
  while (i1 < TARRAY2_SIZE(fset1->lvlArr) || i2 < TARRAY2_SIZE(fset2->lvlArr)) {
35,422✔
457
    SSttLvl *lvl1 = i1 < TARRAY2_SIZE(fset1->lvlArr) ? TARRAY2_GET(fset1->lvlArr, i1) : NULL;
23,200✔
458
    SSttLvl *lvl2 = i2 < TARRAY2_SIZE(fset2->lvlArr) ? TARRAY2_GET(fset2->lvlArr, i2) : NULL;
23,200✔
459

460
    if (lvl1 && lvl2) {
23,200✔
461
      if (lvl1->level < lvl2->level) {
22,088✔
462
        // add a new stt level
463
        code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
3,577✔
464
        if (code) return code;
3,577!
465
        code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
3,577✔
466
        if (code) return code;
3,577!
467
        i1++;
3,577✔
468
        i2++;
3,577✔
469
      } else if (lvl1->level > lvl2->level) {
18,511✔
470
        // remove the stt level
471
        TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
3,753!
472
      } else {
473
        // apply edit on stt level
474
        code = tsdbSttLvlApplyEdit(pTsdb, lvl1, lvl2);
14,758✔
475
        if (code) return code;
14,758!
476
        i1++;
14,758✔
477
        i2++;
14,758✔
478
      }
479
    } else if (lvl1) {
1,112✔
480
      // add a new stt level
481
      code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
613✔
482
      if (code) return code;
613!
483
      code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
613✔
484
      if (code) return code;
613!
485
      i1++;
613✔
486
      i2++;
613✔
487
    } else {
488
      // remove the stt level
489
      TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
499!
490
    }
491
  }
492

493
  fset2->lastCompact = fset1->lastCompact;
12,222✔
494
  fset2->lastCommit = fset1->lastCommit;
12,222✔
495

496
  return 0;
12,222✔
497
}
498

499
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) {
3,956,730✔
500
  fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
3,956,730!
501
  if (fset[0] == NULL) {
3,954,524!
502
    return terrno;
×
503
  }
504

505
  fset[0]->fid = fid;
3,954,524✔
506
  fset[0]->maxVerValid = VERSION_MAX;
3,954,524✔
507
  TARRAY2_INIT(fset[0]->lvlArr);
3,954,524✔
508

509
  // block commit variables
510
  (void)taosThreadCondInit(&fset[0]->canCommit, NULL);
3,954,524✔
511
  (*fset)->numWaitCommit = 0;
3,952,949✔
512
  (*fset)->blockCommit = false;
3,952,949✔
513

514
  for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
11,833,567✔
515
    struct STFileSetCond *cond = &(*fset)->conds[i];
7,886,661✔
516
    cond->running = false;
7,886,661✔
517
    cond->numWait = 0;
7,886,661✔
518
    (void)taosThreadCondInit(&cond->cond, NULL);
7,886,661✔
519
  }
520

521
  return 0;
3,946,906✔
522
}
523

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

528
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
1,821,407✔
529
    if (fset1->farr[ftype] == NULL) continue;
1,457,009✔
530

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

538
  const SSttLvl *lvl1;
539
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
741,740✔
540
    SSttLvl *lvl;
541
    code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl);
376,995✔
542
    if (code) {
377,323✔
543
      tsdbTFileSetClear(fset);
26✔
544
      return code;
×
545
    }
546

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

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

554
  return 0;
364,745✔
555
}
556

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

562
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
563
    if (fset1->farr[ftype] == NULL) continue;
×
564
    STFileObj *fobj = fset1->farr[ftype];
×
565
    if (fobj->f->maxVer <= ever) {
×
566
      code = tsdbTFileObjInit(pTsdb, fobj->f, &fset[0]->farr[ftype]);
×
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;
583
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
×
584
    SSttLvl *lvl;
585
    code = tsdbSttLvlFilteredInitEx(pTsdb, lvl1, ever, &lvl, fopArr);
×
586
    if (code) {
×
587
      tsdbTFileSetClear(fset);
×
588
      return code;
×
589
    }
590

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

595
  return 0;
×
596
}
597

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

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

616
int32_t tsdbTFileSetInitRef(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
3,249,573✔
617
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
3,249,573✔
618
  if (code) return code;
3,236,152!
619

620
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
16,193,180✔
621
    if (fset1->farr[ftype] == NULL) continue;
12,952,873✔
622

623
    code = tsdbTFileObjRef(fset1->farr[ftype]);
395,001✔
624
    if (code) {
399,143!
625
      tsdbTFileSetClear(fset);
×
626
      return code;
×
627
    }
628
    fset[0]->farr[ftype] = fset1->farr[ftype];
399,156✔
629
  }
630

631
  const SSttLvl *lvl1;
632
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
6,424,173✔
633
    SSttLvl *lvl;
634
    code = tsdbSttLvlInitRef(pTsdb, lvl1, &lvl);
3,171,451✔
635
    if (code) {
3,182,310!
636
      tsdbSttLvlClear(&lvl);
×
637
      tsdbTFileSetClear(fset);
×
638
      return code;
×
639
    }
640

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

649
  (*fset)->lastCompact = fset1->lastCompact;
3,252,722✔
650
  (*fset)->lastCommit = fset1->lastCommit;
3,252,722✔
651

652
  return 0;
3,252,722✔
653
}
654

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

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

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

672
void tsdbTFileSetClear(STFileSet **fset) {
4,424,373✔
673
  if (fset && *fset) {
4,424,373!
674
    for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
19,823,055✔
675
      if ((*fset)->farr[ftype] == NULL) continue;
15,857,527✔
676
      int32_t code = tsdbTFileObjUnref((*fset)->farr[ftype]);
438,476✔
677
      if (code) {
439,249!
678
        tsdbError("failed to unref file, fid:%d, ftype:%d", (*fset)->fid, ftype);
×
679
      }
680
      (*fset)->farr[ftype] = NULL;
439,261✔
681
    }
682

683
    TARRAY2_DESTROY((*fset)->lvlArr, tsdbSttLvlClear);
7,872,416!
684

685
    (void)taosThreadCondDestroy(&(*fset)->canCommit);
3,966,692✔
686
    for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
11,892,182✔
687
      (void)taosThreadCondDestroy(&(*fset)->conds[i].cond);
7,927,238✔
688
    }
689
    taosMemoryFreeClear(*fset);
3,964,944!
690
  }
691
}
4,424,559✔
692

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

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

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

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

716
int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2) {
11,290,285✔
717
  if (fset1[0]->fid < fset2[0]->fid) return -1;
11,290,285✔
718
  if (fset1[0]->fid > fset2[0]->fid) return 1;
11,284,537✔
719
  return 0;
34,889✔
720
}
721

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

736
bool tsdbTFileSetIsEmpty(const STFileSet *fset) {
356,720✔
737
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
1,757,386✔
738
    if (fset->farr[ftype] != NULL) return false;
1,412,394✔
739
  }
740
  return TARRAY2_SIZE(fset->lvlArr) == 0;
344,992✔
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