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

taosdata / TDengine / #3565

25 Dec 2024 05:34AM UTC coverage: 51.098% (-11.1%) from 62.21%
#3565

push

travis-ci

web-flow
Merge pull request #29316 from taosdata/enh/3.0/TD-33266

enh(ut):Add wal & config UT.

111558 of 284773 branches covered (39.17%)

Branch coverage included in aggregate %.

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

39015 existing lines in 102 files now uncovered.

177882 of 281666 relevant lines covered (63.15%)

15090998.35 hits per line

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

0.0
/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

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

UNCOV
28
static void tsdbSttLvlClearFObj(void *data) { TAOS_UNUSED(tsdbTFileObjUnref(*(STFileObj **)data)); }
×
29

UNCOV
30
void tsdbSttLvlClear(SSttLvl **lvl) {
×
UNCOV
31
  if (lvl[0] != NULL) {
×
UNCOV
32
    TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlClearFObj);
×
UNCOV
33
    taosMemoryFree(lvl[0]);
×
UNCOV
34
    lvl[0] = NULL;
×
35
  }
UNCOV
36
}
×
37

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

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

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

UNCOV
61
static int32_t tsdbSttLvlInitRef(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
×
UNCOV
62
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
×
UNCOV
63
  if (code) return code;
×
64

65
  STFileObj *fobj1;
UNCOV
66
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
×
UNCOV
67
    code = tsdbTFileObjRef(fobj1);
×
UNCOV
68
    if (code) {
×
UNCOV
69
      tsdbSttLvlClear(lvl);
×
70
      return code;
×
71
    }
UNCOV
72
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj1);
×
UNCOV
73
    if (code) {
×
74
      if (tsdbTFileObjUnref(fobj1) != 0) {
×
75
        tsdbError("failed to unref file obj, fobj:%p", fobj1);
×
76
      }
77
      tsdbSttLvlClear(lvl);
×
78
      return code;
×
79
    }
80
  }
UNCOV
81
  return 0;
×
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

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

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

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

UNCOV
131
  int32_t i1 = 0, i2 = 0;
×
UNCOV
132
  while (i1 < TARRAY2_SIZE(lvl1->fobjArr) || i2 < TARRAY2_SIZE(lvl2->fobjArr)) {
×
UNCOV
133
    STFileObj *fobj1 = i1 < TARRAY2_SIZE(lvl1->fobjArr) ? TARRAY2_GET(lvl1->fobjArr, i1) : NULL;
×
UNCOV
134
    STFileObj *fobj2 = i2 < TARRAY2_SIZE(lvl2->fobjArr) ? TARRAY2_GET(lvl2->fobjArr, i2) : NULL;
×
135

UNCOV
136
    if (fobj1 && fobj2) {
×
UNCOV
137
      if (fobj1->f->cid < fobj2->f->cid) {
×
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++;
×
UNCOV
145
      } else if (fobj1->f->cid > fobj2->f->cid) {
×
146
        // remove a file obj
UNCOV
147
        TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
148
      } else {
UNCOV
149
        if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
×
UNCOV
150
          if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
×
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
        }
UNCOV
160
        i1++;
×
UNCOV
161
        i2++;
×
162
      }
UNCOV
163
    } else if (fobj1) {
×
164
      // create a file obj
UNCOV
165
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
×
UNCOV
166
      if (code) return code;
×
UNCOV
167
      code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
×
UNCOV
168
      if (code) return code;
×
UNCOV
169
      i1++;
×
UNCOV
170
      i2++;
×
171
    } else {
172
      // remove a file obj
173
      TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
174
    }
175
  }
UNCOV
176
  return 0;
×
177
}
178

UNCOV
179
static int32_t tsdbSttLvlCmprFn(const SSttLvl **lvl1, const SSttLvl **lvl2) {
×
UNCOV
180
  if (lvl1[0]->level < lvl2[0]->level) return -1;
×
UNCOV
181
  if (lvl1[0]->level > lvl2[0]->level) return 1;
×
UNCOV
182
  return 0;
×
183
}
184

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

UNCOV
190
  cJSON *ajson = cJSON_AddArrayToObject(json, "files");
×
UNCOV
191
  if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
×
192
  const STFileObj *fobj;
UNCOV
193
  TARRAY2_FOREACH(lvl->fobjArr, fobj) {
×
UNCOV
194
    cJSON *item = cJSON_CreateObject();
×
UNCOV
195
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
196
    (void)cJSON_AddItemToArray(ajson, item);
×
197

UNCOV
198
    int32_t code = tsdbTFileToJson(fobj->f, item);
×
UNCOV
199
    if (code) return code;
×
200
  }
201

UNCOV
202
  return 0;
×
203
}
204

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

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

UNCOV
216
  int32_t code = tsdbSttLvlInit(level, lvl);
×
UNCOV
217
  if (code) return code;
×
218

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

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

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

UNCOV
240
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
×
UNCOV
241
    if (code) return code;
×
242
  }
UNCOV
243
  TARRAY2_SORT(lvl[0]->fobjArr, tsdbTFileObjCmpr);
×
UNCOV
244
  return 0;
×
245
}
246

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

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

UNCOV
256
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
257
    if (fset->farr[ftype] == NULL) continue;
×
258

UNCOV
259
    code = tsdbTFileToJson(fset->farr[ftype]->f, json);
×
UNCOV
260
    if (code) return code;
×
261
  }
262

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

UNCOV
272
    code = tsdbSttLvlToJson(lvl, item2);
×
UNCOV
273
    if (code) return code;
×
274
  }
275

UNCOV
276
  return 0;
×
277
}
278

UNCOV
279
int32_t tsdbJsonToTFileSet(STsdb *pTsdb, const cJSON *json, STFileSet **fset) {
×
280
  int32_t      code;
281
  const cJSON *item1, *item2;
282
  int32_t      fid;
283
  STFile       tf;
284

285
  // fid
UNCOV
286
  item1 = cJSON_GetObjectItem(json, "fid");
×
UNCOV
287
  if (cJSON_IsNumber(item1)) {
×
UNCOV
288
    fid = item1->valuedouble;
×
289
  } else {
290
    return TSDB_CODE_FILE_CORRUPTED;
×
291
  }
292

UNCOV
293
  code = tsdbTFileSetInit(fid, fset);
×
UNCOV
294
  if (code) return code;
×
295

UNCOV
296
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
297
    code = tsdbJsonToTFile(json, ftype, &tf);
×
UNCOV
298
    if (code == TSDB_CODE_NOT_FOUND) {
×
UNCOV
299
      continue;
×
UNCOV
300
    } else if (code) {
×
301
      tsdbTFileSetClear(fset);
×
302
      return code;
×
303
    } else {
UNCOV
304
      code = tsdbTFileObjInit(pTsdb, &tf, &(*fset)->farr[ftype]);
×
UNCOV
305
      if (code) return code;
×
306
    }
307
  }
308

309
  // each level
UNCOV
310
  item1 = cJSON_GetObjectItem(json, "stt lvl");
×
UNCOV
311
  if (cJSON_IsArray(item1)) {
×
UNCOV
312
    cJSON_ArrayForEach(item2, item1) {
×
313
      SSttLvl *lvl;
UNCOV
314
      code = tsdbJsonToSttLvl(pTsdb, item2, &lvl);
×
UNCOV
315
      if (code) {
×
UNCOV
316
        tsdbTFileSetClear(fset);
×
317
        return code;
×
318
      }
319

UNCOV
320
      code = TARRAY2_APPEND((*fset)->lvlArr, lvl);
×
UNCOV
321
      if (code) return code;
×
322
    }
UNCOV
323
    TARRAY2_SORT((*fset)->lvlArr, tsdbSttLvlCmprFn);
×
324
  } else {
325
    return TSDB_CODE_FILE_CORRUPTED;
×
326
  }
327

UNCOV
328
  return 0;
×
329
}
330

331
// NOTE: the api does not remove file, only do memory operation
UNCOV
332
int32_t tsdbTFileSetEdit(STsdb *pTsdb, STFileSet *fset, const STFileOp *op) {
×
UNCOV
333
  int32_t code = 0;
×
334

UNCOV
335
  if (op->optype == TSDB_FOP_CREATE) {
×
336
    // create a new file
337
    STFileObj *fobj;
UNCOV
338
    code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
×
UNCOV
339
    if (code) return code;
×
340

UNCOV
341
    if (fobj->f->type == TSDB_FTYPE_STT) {
×
UNCOV
342
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, fobj->f->stt->level);
×
UNCOV
343
      if (!lvl) {
×
UNCOV
344
        code = tsdbSttLvlInit(fobj->f->stt->level, &lvl);
×
UNCOV
345
        if (code) return code;
×
346

UNCOV
347
        code = TARRAY2_SORT_INSERT(fset->lvlArr, lvl, tsdbSttLvlCmprFn);
×
UNCOV
348
        if (code) return code;
×
349
      }
350

UNCOV
351
      code = TARRAY2_SORT_INSERT(lvl->fobjArr, fobj, tsdbTFileObjCmpr);
×
UNCOV
352
      if (code) return code;
×
353
    } else {
UNCOV
354
      fset->farr[fobj->f->type] = fobj;
×
355
    }
UNCOV
356
  } else if (op->optype == TSDB_FOP_REMOVE) {
×
357
    // delete a file
UNCOV
358
    if (op->of.type == TSDB_FTYPE_STT) {
×
UNCOV
359
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
×
360

UNCOV
361
      STFileObj  tfobj = {.f[0] = {.cid = op->of.cid}};
×
UNCOV
362
      STFileObj *tfobjp = &tfobj;
×
UNCOV
363
      int32_t    idx = TARRAY2_SEARCH_IDX(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
×
UNCOV
364
      TARRAY2_REMOVE(lvl->fobjArr, idx, tsdbSttLvlClearFObj);
×
365
    } else {
UNCOV
366
      code = tsdbTFileObjUnref(fset->farr[op->of.type]);
×
UNCOV
367
      if (code) return code;
×
UNCOV
368
      fset->farr[op->of.type] = NULL;
×
369
    }
370
  } else {
UNCOV
371
    if (op->nf.type == TSDB_FTYPE_STT) {
×
372
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
×
373

374
      STFileObj   tfobj = {.f[0] = {.cid = op->of.cid}}, *tfobjp = &tfobj;
×
375
      STFileObj **fobjPtr = TARRAY2_SEARCH(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
×
376
      if (fobjPtr) {
×
377
        tfobjp = *fobjPtr;
×
378
        tfobjp->f[0] = op->nf;
×
379
      } else {
380
        tsdbError("file not found, cid:%" PRId64, op->of.cid);
×
381
      }
382
    } else {
UNCOV
383
      fset->farr[op->nf.type]->f[0] = op->nf;
×
384
    }
385
  }
386

UNCOV
387
  return 0;
×
388
}
389

UNCOV
390
int32_t tsdbTFileSetApplyEdit(STsdb *pTsdb, const STFileSet *fset1, STFileSet *fset2) {
×
UNCOV
391
  int32_t code = 0;
×
392

UNCOV
393
  if (fset1->fid != fset2->fid) {
×
394
    return TSDB_CODE_INVALID_PARA;
×
395
  }
396

UNCOV
397
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
398
    if (!fset1->farr[ftype] && !fset2->farr[ftype]) continue;
×
399

UNCOV
400
    STFileObj *fobj1 = fset1->farr[ftype];
×
UNCOV
401
    STFileObj *fobj2 = fset2->farr[ftype];
×
402

UNCOV
403
    if (fobj1 && fobj2) {
×
UNCOV
404
      if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
×
UNCOV
405
        if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
×
UNCOV
406
          fobj2->f[0] = fobj1->f[0];
×
407
        }
408
      } else {
UNCOV
409
        if (fobj1->f->cid != fobj2->f->cid) {
×
UNCOV
410
          code = tsdbTFileObjRemove(fobj2);
×
UNCOV
411
          if (code) return code;
×
412
        } else {
413
          code = tsdbTFileObjRemoveUpdateLC(fobj2);
×
414
          if (code) return code;
×
415
        }
UNCOV
416
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
×
UNCOV
417
        if (code) return code;
×
418
      }
UNCOV
419
    } else if (fobj1) {
×
420
      // create a new file
UNCOV
421
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
×
UNCOV
422
      if (code) return code;
×
423
    } else {
424
      // remove the file
425
      code = tsdbTFileObjRemove(fobj2);
×
426
      if (code) return code;
×
427
      fset2->farr[ftype] = NULL;
×
428
    }
429
  }
430

431
  // stt part
UNCOV
432
  int32_t i1 = 0, i2 = 0;
×
UNCOV
433
  while (i1 < TARRAY2_SIZE(fset1->lvlArr) || i2 < TARRAY2_SIZE(fset2->lvlArr)) {
×
UNCOV
434
    SSttLvl *lvl1 = i1 < TARRAY2_SIZE(fset1->lvlArr) ? TARRAY2_GET(fset1->lvlArr, i1) : NULL;
×
UNCOV
435
    SSttLvl *lvl2 = i2 < TARRAY2_SIZE(fset2->lvlArr) ? TARRAY2_GET(fset2->lvlArr, i2) : NULL;
×
436

UNCOV
437
    if (lvl1 && lvl2) {
×
UNCOV
438
      if (lvl1->level < lvl2->level) {
×
439
        // add a new stt level
UNCOV
440
        code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
×
UNCOV
441
        if (code) return code;
×
UNCOV
442
        code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
×
UNCOV
443
        if (code) return code;
×
UNCOV
444
        i1++;
×
UNCOV
445
        i2++;
×
UNCOV
446
      } else if (lvl1->level > lvl2->level) {
×
447
        // remove the stt level
UNCOV
448
        TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
×
449
      } else {
450
        // apply edit on stt level
UNCOV
451
        code = tsdbSttLvlApplyEdit(pTsdb, lvl1, lvl2);
×
UNCOV
452
        if (code) return code;
×
UNCOV
453
        i1++;
×
UNCOV
454
        i2++;
×
455
      }
UNCOV
456
    } else if (lvl1) {
×
457
      // add a new stt level
UNCOV
458
      code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
×
UNCOV
459
      if (code) return code;
×
UNCOV
460
      code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
×
UNCOV
461
      if (code) return code;
×
UNCOV
462
      i1++;
×
UNCOV
463
      i2++;
×
464
    } else {
465
      // remove the stt level
UNCOV
466
      TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
×
467
    }
468
  }
469

UNCOV
470
  return 0;
×
471
}
472

UNCOV
473
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) {
×
UNCOV
474
  fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
×
UNCOV
475
  if (fset[0] == NULL) {
×
476
    return terrno;
×
477
  }
478

UNCOV
479
  fset[0]->fid = fid;
×
UNCOV
480
  fset[0]->maxVerValid = VERSION_MAX;
×
UNCOV
481
  TARRAY2_INIT(fset[0]->lvlArr);
×
482

483
  // block commit variables
UNCOV
484
  (void)taosThreadCondInit(&fset[0]->canCommit, NULL);
×
UNCOV
485
  (*fset)->numWaitCommit = 0;
×
UNCOV
486
  (*fset)->blockCommit = false;
×
487

UNCOV
488
  for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
×
UNCOV
489
    struct STFileSetCond *cond = &(*fset)->conds[i];
×
UNCOV
490
    cond->running = false;
×
UNCOV
491
    cond->numWait = 0;
×
UNCOV
492
    (void)taosThreadCondInit(&cond->cond, NULL);
×
493
  }
494

UNCOV
495
  return 0;
×
496
}
497

UNCOV
498
int32_t tsdbTFileSetInitCopy(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
×
UNCOV
499
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
×
UNCOV
500
  if (code) return code;
×
501

UNCOV
502
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
503
    if (fset1->farr[ftype] == NULL) continue;
×
504

UNCOV
505
    code = tsdbTFileObjInit(pTsdb, fset1->farr[ftype]->f, &fset[0]->farr[ftype]);
×
UNCOV
506
    if (code) {
×
507
      tsdbTFileSetClear(fset);
×
508
      return code;
×
509
    }
510
  }
511

512
  const SSttLvl *lvl1;
UNCOV
513
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
×
514
    SSttLvl *lvl;
UNCOV
515
    code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl);
×
UNCOV
516
    if (code) {
×
517
      tsdbTFileSetClear(fset);
×
518
      return code;
×
519
    }
520

UNCOV
521
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
×
UNCOV
522
    if (code) return code;
×
523
  }
524

UNCOV
525
  return 0;
×
526
}
527

528
int32_t tsdbTFileSetFilteredInitDup(STsdb *pTsdb, const STFileSet *fset1, int64_t ever, STFileSet **fset,
×
529
                                    TFileOpArray *fopArr) {
530
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
×
531
  if (code) return code;
×
532

533
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
534
    if (fset1->farr[ftype] == NULL) continue;
×
535
    STFileObj *fobj = fset1->farr[ftype];
×
536
    if (fobj->f->maxVer <= ever) {
×
537
      code = tsdbTFileObjInit(pTsdb, fobj->f, &fset[0]->farr[ftype]);
×
538
      if (code) {
×
539
        tsdbTFileSetClear(fset);
×
540
        return code;
×
541
      }
542
    } else {
543
      STFileOp op = {
×
544
          .optype = TSDB_FOP_REMOVE,
545
          .fid = fobj->f->fid,
×
546
          .of = fobj->f[0],
547
      };
548
      code = TARRAY2_APPEND(fopArr, op);
×
549
      if (code) return code;
×
550
    }
551
  }
552

553
  const SSttLvl *lvl1;
554
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
×
555
    SSttLvl *lvl;
556
    code = tsdbSttLvlFilteredInitEx(pTsdb, lvl1, ever, &lvl, fopArr);
×
557
    if (code) {
×
558
      tsdbTFileSetClear(fset);
×
559
      return code;
×
560
    }
561

562
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
×
563
    if (code) return code;
×
564
  }
565

566
  return 0;
×
567
}
568

569
int32_t tsdbTFileSetRangeInitRef(STsdb *pTsdb, const STFileSet *fset1, int64_t sver, int64_t ever,
×
570
                                 STFileSetRange **fsr) {
571
  fsr[0] = taosMemoryCalloc(1, sizeof(*fsr[0]));
×
572
  if (fsr[0] == NULL) {
×
573
    return terrno;
×
574
  }
575
  fsr[0]->fid = fset1->fid;
×
576
  fsr[0]->sver = sver;
×
577
  fsr[0]->ever = ever;
×
578

579
  int32_t code = tsdbTFileSetInitRef(pTsdb, fset1, &fsr[0]->fset);
×
580
  if (code) {
×
581
    taosMemoryFree(fsr[0]);
×
582
    fsr[0] = NULL;
×
583
  }
584
  return code;
×
585
}
586

UNCOV
587
int32_t tsdbTFileSetInitRef(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
×
UNCOV
588
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
×
UNCOV
589
  if (code) return code;
×
590

UNCOV
591
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
592
    if (fset1->farr[ftype] == NULL) continue;
×
593

UNCOV
594
    code = tsdbTFileObjRef(fset1->farr[ftype]);
×
UNCOV
595
    if (code) {
×
596
      tsdbTFileSetClear(fset);
×
597
      return code;
×
598
    }
UNCOV
599
    fset[0]->farr[ftype] = fset1->farr[ftype];
×
600
  }
601

602
  const SSttLvl *lvl1;
UNCOV
603
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
×
604
    SSttLvl *lvl;
UNCOV
605
    code = tsdbSttLvlInitRef(pTsdb, lvl1, &lvl);
×
UNCOV
606
    if (code) {
×
607
      tsdbSttLvlClear(&lvl);
×
608
      tsdbTFileSetClear(fset);
×
609
      return code;
×
610
    }
611

UNCOV
612
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
×
UNCOV
613
    if (code) {
×
614
      tsdbSttLvlClear(&lvl);
×
615
      tsdbTFileSetClear(fset);
×
616
      return code;
×
617
    }
618
  }
619

UNCOV
620
  return 0;
×
621
}
622

623
void tsdbTFileSetRangeClear(STFileSetRange **fsr) {
×
624
  if (!fsr[0]) return;
×
625

626
  tsdbTFileSetClear(&fsr[0]->fset);
×
627
  taosMemoryFree(fsr[0]);
×
628
  fsr[0] = NULL;
×
629
  return;
×
630
}
631

UNCOV
632
void tsdbTFileSetRangeArrayDestroy(TFileSetRangeArray **ppArr) {
×
UNCOV
633
  if (ppArr && ppArr[0]) {
×
UNCOV
634
    TARRAY2_DESTROY(ppArr[0], tsdbTFileSetRangeClear);
×
UNCOV
635
    taosMemoryFree(ppArr[0]);
×
UNCOV
636
    ppArr[0] = NULL;
×
637
  }
UNCOV
638
}
×
639

UNCOV
640
void tsdbTFileSetClear(STFileSet **fset) {
×
UNCOV
641
  if (fset && *fset) {
×
UNCOV
642
    for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
643
      if ((*fset)->farr[ftype] == NULL) continue;
×
UNCOV
644
      int32_t code = tsdbTFileObjUnref((*fset)->farr[ftype]);
×
UNCOV
645
      if (code) {
×
646
        tsdbError("failed to unref file, fid:%d, ftype:%d", (*fset)->fid, ftype);
×
647
      }
UNCOV
648
      (*fset)->farr[ftype] = NULL;
×
649
    }
650

UNCOV
651
    TARRAY2_DESTROY((*fset)->lvlArr, tsdbSttLvlClear);
×
652

UNCOV
653
    (void)taosThreadCondDestroy(&(*fset)->canCommit);
×
UNCOV
654
    for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
×
UNCOV
655
      (void)taosThreadCondDestroy(&(*fset)->conds[i].cond);
×
656
    }
UNCOV
657
    taosMemoryFreeClear(*fset);
×
658
  }
UNCOV
659
}
×
660

661
void tsdbTFileSetRemove(STFileSet *fset) {
×
662
  if (fset == NULL) return;
×
663

664
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
665
    if (fset->farr[ftype] != NULL) {
×
666
      int32_t code = tsdbTFileObjRemove(fset->farr[ftype]);
×
667
      if (code) {
×
668
        tsdbError("failed to remove file, fid:%d, ftype:%d", fset->fid, ftype);
×
669
      }
670
      fset->farr[ftype] = NULL;
×
671
    }
672
  }
673

674
  TARRAY2_DESTROY(fset->lvlArr, tsdbSttLvlRemove);
×
675
}
676

UNCOV
677
SSttLvl *tsdbTFileSetGetSttLvl(STFileSet *fset, int32_t level) {
×
UNCOV
678
  SSttLvl   sttLvl = {.level = level};
×
UNCOV
679
  SSttLvl  *lvl = &sttLvl;
×
UNCOV
680
  SSttLvl **lvlPtr = TARRAY2_SEARCH(fset->lvlArr, &lvl, tsdbSttLvlCmprFn, TD_EQ);
×
UNCOV
681
  return lvlPtr ? lvlPtr[0] : NULL;
×
682
}
683

UNCOV
684
int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2) {
×
UNCOV
685
  if (fset1[0]->fid < fset2[0]->fid) return -1;
×
UNCOV
686
  if (fset1[0]->fid > fset2[0]->fid) return 1;
×
UNCOV
687
  return 0;
×
688
}
689

UNCOV
690
int64_t tsdbTFileSetMaxCid(const STFileSet *fset) {
×
UNCOV
691
  int64_t maxCid = 0;
×
UNCOV
692
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
693
    if (fset->farr[ftype] == NULL) continue;
×
UNCOV
694
    maxCid = TMAX(maxCid, fset->farr[ftype]->f->cid);
×
695
  }
696
  const SSttLvl   *lvl;
697
  const STFileObj *fobj;
UNCOV
698
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
×
UNCOV
699
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { maxCid = TMAX(maxCid, fobj->f->cid); }
×
700
  }
UNCOV
701
  return maxCid;
×
702
}
703

UNCOV
704
bool tsdbTFileSetIsEmpty(const STFileSet *fset) {
×
UNCOV
705
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
UNCOV
706
    if (fset->farr[ftype] != NULL) return false;
×
707
  }
UNCOV
708
  return TARRAY2_SIZE(fset->lvlArr) == 0;
×
709
}
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