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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

89.52
/source/dnode/vnode/src/tsdb/tsdbSttFileRW.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 "tsdbSttFileRW.h"
17
#include "meta.h"
18
#include "tsdbDataFileRW.h"
19

20
// SSttFReader ============================================================
21
struct SSttFileReader {
22
  SSttFileReaderConfig config[1];
23
  STsdbFD             *fd;
24
  SSttFooter           footer[1];
25
  struct {
26
    bool sttBlkLoaded;
27
    bool statisBlkLoaded;
28
    bool tombBlkLoaded;
29
  } ctx[1];
30
  TSttBlkArray    sttBlkArray[1];
31
  TStatisBlkArray statisBlkArray[1];
32
  TTombBlkArray   tombBlkArray[1];
33
  SBuffer         local[10];
34
  SBuffer        *buffers;
35
};
36

37
// SSttFileReader
38
int32_t tsdbSttFileReaderOpen(const char *fname, const SSttFileReaderConfig *config, SSttFileReader **reader) {
429,675,930✔
39
  int32_t code = 0;
429,675,930✔
40
  int32_t lino = 0;
429,675,930✔
41

42
  reader[0] = taosMemoryCalloc(1, sizeof(*reader[0]));
429,737,287✔
43
  if (reader[0] == NULL) {
429,228,422✔
44
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
45
  }
46

47
  reader[0]->config[0] = config[0];
429,238,426✔
48
  reader[0]->buffers = config->buffers;
429,385,322✔
49
  if (reader[0]->buffers == NULL) {
429,735,256✔
50
    reader[0]->buffers = reader[0]->local;
429,585,100✔
51
  }
52

53
  // open file
54
  if (fname) {
429,660,065✔
55
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, config->tsdb, TD_FILE_READ, &reader[0]->fd, 0), &lino, _exit);
429,660,065✔
56
  } else {
57
    char fname1[TSDB_FILENAME_LEN];
×
58
    tsdbTFileName(config->tsdb, config->file, fname1);
×
59
    TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd, 0), &lino, _exit);
×
60
  }
61

62
  // // open each segment reader
63
  int64_t offset = config->file->size - sizeof(SSttFooter);
429,690,521✔
64
  if (offset < TSDB_FHDR_SIZE) {
429,615,221✔
65
    tsdbError("vgId:%d %s failed at %s:%d since file size is too small: %" PRId64 " fname:%s",
×
66
              TD_VID(config->tsdb->pVnode), __func__, __FILE__, __LINE__, config->file->size, reader[0]->fd->path);
67
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
68
  }
69

70
  SEncryptData *pEncryptData = &(config->tsdb->pVnode->config.tsdbCfg.encryptData);
429,615,221✔
71

72
#if 1
73
  TAOS_CHECK_GOTO(
429,708,515✔
74
      tsdbReadFile(reader[0]->fd, offset, (uint8_t *)(reader[0]->footer), sizeof(SSttFooter), 0, pEncryptData), &lino,
75
      _exit);
76
#else
77
  int64_t size = config->file->size;
78

79
  for (; size > TSDB_FHDR_SIZE; size--) {
80
    code = tsdbReadFile(reader[0]->fd, size - sizeof(SSttFooter), (uint8_t *)(reader[0]->footer), sizeof(SSttFooter), 0,
81
                        encryptAlgoirthm, encryptKey);
82
    if (code) continue;
83
    if ((*reader)->footer->sttBlkPtr->offset + (*reader)->footer->sttBlkPtr->size + sizeof(SSttFooter) == size ||
84
        (*reader)->footer->statisBlkPtr->offset + (*reader)->footer->statisBlkPtr->size + sizeof(SSttFooter) == size ||
85
        (*reader)->footer->tombBlkPtr->offset + (*reader)->footer->tombBlkPtr->size + sizeof(SSttFooter) == size) {
86
      break;
87
    }
88
  }
89
  if (size <= TSDB_FHDR_SIZE) {
90
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
91
  }
92
#endif
93

94
_exit:
429,461,654✔
95
  if (code) {
429,532,625✔
96
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(config->tsdb->pVnode), __func__, __FILE__, lino,
×
97
              tstrerror(code));
98
    tsdbSttFileReaderClose(reader);
×
99
  }
100
  return code;
429,532,652✔
101
}
102

103
void tsdbSttFileReaderClose(SSttFileReader **reader) {
429,726,510✔
104
  if (reader[0]) {
429,726,510✔
105
    for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
2,147,483,647✔
106
      tBufferDestroy(reader[0]->local + i);
2,147,483,647✔
107
    }
108
    tsdbCloseFile(&reader[0]->fd);
429,718,540✔
109
    TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
429,666,630✔
110
    TARRAY2_DESTROY(reader[0]->statisBlkArray, NULL);
429,725,903✔
111
    TARRAY2_DESTROY(reader[0]->sttBlkArray, NULL);
429,742,542✔
112
    taosMemoryFree(reader[0]);
429,758,346✔
113
    reader[0] = NULL;
429,676,970✔
114
  }
115
}
429,656,796✔
116

117
// SSttFSegReader
118
int32_t tsdbSttFileReadStatisBlk(SSttFileReader *reader, const TStatisBlkArray **statisBlkArray) {
426,274,279✔
119
  if (!reader->ctx->statisBlkLoaded) {
426,274,279✔
120
    if (reader->footer->statisBlkPtr->size > 0) {
426,419,428✔
121
      if (reader->footer->statisBlkPtr->size % sizeof(SStatisBlk) != 0) {
426,357,266✔
122
        tsdbError("vgId:%d %s failed at %s:%d since stt file statis block size is not valid, fname:%s",
×
123
                  TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd->path);
124
        return TSDB_CODE_FILE_CORRUPTED;
×
125
      }
126

127
      int32_t size = reader->footer->statisBlkPtr->size / sizeof(SStatisBlk);
426,077,743✔
128
      void   *data = taosMemoryMalloc(reader->footer->statisBlkPtr->size);
426,206,348✔
129
      if (!data) {
426,116,561✔
130
        return terrno;
×
131
      }
132

133
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
426,116,561✔
134

135
      int32_t code = tsdbReadFile(reader->fd, reader->footer->statisBlkPtr->offset, data,
426,304,508✔
136
                                  reader->footer->statisBlkPtr->size, 0, pEncryptData);
137
      if (code) {
426,339,770✔
138
        taosMemoryFree(data);
×
139
        return code;
×
140
      }
141

142
      TARRAY2_INIT_EX(reader->statisBlkArray, size, size, data);
426,339,770✔
143
    } else {
144
      TARRAY2_INIT(reader->statisBlkArray);
149,002✔
145
    }
146

147
    reader->ctx->statisBlkLoaded = true;
426,470,945✔
148
  }
149

150
  statisBlkArray[0] = reader->statisBlkArray;
426,404,756✔
151
  return 0;
426,430,321✔
152
}
153

154
int32_t tsdbSttFileReadTombBlk(SSttFileReader *reader, const TTombBlkArray **tombBlkArray) {
429,590,737✔
155
  if (!reader->ctx->tombBlkLoaded) {
429,590,737✔
156
    if (reader->footer->tombBlkPtr->size > 0) {
429,696,476✔
157
      if (reader->footer->tombBlkPtr->size % sizeof(STombBlk) != 0) {
11,434,525✔
158
        tsdbError("vgId:%d %s failed at %s:%d since stt file tomb block size is not valid, fname:%s",
×
159
                  TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd->path);
160
        return TSDB_CODE_FILE_CORRUPTED;
×
161
      }
162

163
      int32_t size = reader->footer->tombBlkPtr->size / sizeof(STombBlk);
11,434,525✔
164
      void   *data = taosMemoryMalloc(reader->footer->tombBlkPtr->size);
11,434,525✔
165
      if (!data) {
11,434,525✔
166
        return terrno;
×
167
      }
168

169
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
11,434,525✔
170

171
      int32_t code = tsdbReadFile(reader->fd, reader->footer->tombBlkPtr->offset, data,
11,434,525✔
172
                                  reader->footer->tombBlkPtr->size, 0, pEncryptData);
173
      if (code) {
11,434,525✔
174
        taosMemoryFree(data);
×
175
        return code;
×
176
      }
177

178
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
11,434,525✔
179
    } else {
180
      TARRAY2_INIT(reader->tombBlkArray);
418,098,337✔
181
    }
182

183
    reader->ctx->tombBlkLoaded = true;
429,642,476✔
184
  }
185

186
  tombBlkArray[0] = reader->tombBlkArray;
429,692,872✔
187
  return 0;
429,713,654✔
188
}
189

190
int32_t tsdbSttFileReadSttBlk(SSttFileReader *reader, const TSttBlkArray **sttBlkArray) {
429,420,276✔
191
  if (!reader->ctx->sttBlkLoaded) {
429,420,276✔
192
    if (reader->footer->sttBlkPtr->size > 0) {
429,554,370✔
193
      if (reader->footer->sttBlkPtr->size % sizeof(SSttBlk) != 0) {
429,503,914✔
194
        tsdbError("vgId:%d %s failed at %s:%d since stt file stt block size is not valid, fname:%s",
×
195
                  TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd->path);
196
        return TSDB_CODE_FILE_CORRUPTED;
×
197
      }
198

199
      int32_t size = reader->footer->sttBlkPtr->size / sizeof(SSttBlk);
429,104,631✔
200
      void   *data = taosMemoryMalloc(reader->footer->sttBlkPtr->size);
429,302,653✔
201
      if (!data) {
429,224,402✔
202
        return terrno;
×
203
      }
204

205
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
429,224,402✔
206

207
      int32_t code = tsdbReadFile(reader->fd, reader->footer->sttBlkPtr->offset, data, reader->footer->sttBlkPtr->size,
429,514,879✔
208
                                  0, pEncryptData);
209
      if (code) {
429,517,673✔
210
        taosMemoryFree(data);
×
211
        return code;
×
212
      }
213

214
      TARRAY2_INIT_EX(reader->sttBlkArray, size, size, data);
429,517,673✔
215
    } else {
216
      TARRAY2_INIT(reader->sttBlkArray);
197,313✔
217
    }
218

219
    reader->ctx->sttBlkLoaded = true;
429,703,087✔
220
  }
221

222
  sttBlkArray[0] = reader->sttBlkArray;
429,817,304✔
223
  return 0;
429,533,865✔
224
}
225

226
int32_t tsdbSttFileReadBlockData(SSttFileReader *reader, const SSttBlk *sttBlk, SBlockData *bData) {
16,640,610✔
227
  int32_t code = 0;
16,640,610✔
228
  int32_t lino = 0;
16,640,610✔
229

230
  SBuffer *buffer0 = reader->buffers + 0;
16,641,924✔
231
  SBuffer *assist = reader->buffers + 1;
16,641,918✔
232

233
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
16,641,267✔
234

235
  // load data
236
  tBufferClear(buffer0);
237
  TAOS_CHECK_GOTO(
16,642,575✔
238
      tsdbReadFileToBuffer(reader->fd, sttBlk->bInfo.offset, sttBlk->bInfo.szBlock, buffer0, 0, pEncryptData), &lino,
239
      _exit);
240

241
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
16,643,232✔
242
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
16,643,232✔
243

244
_exit:
16,643,226✔
245
  if (code) {
16,642,575✔
246
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
247
              tstrerror(code));
248
  }
249
  return code;
16,642,575✔
250
}
251

252
int32_t tsdbSttFileReadBlockDataByColumn(SSttFileReader *reader, const SSttBlk *sttBlk, SBlockData *bData,
240,785,559✔
253
                                         STSchema *pTSchema, int16_t cids[], int32_t ncid) {
254
  int32_t code = 0;
240,785,559✔
255
  int32_t lino = 0;
240,785,559✔
256

257
  SDiskDataHdr hdr;
240,649,994✔
258
  SBuffer     *buffer0 = reader->buffers + 0;
240,815,638✔
259
  SBuffer     *buffer1 = reader->buffers + 1;
240,788,629✔
260
  SBuffer     *assist = reader->buffers + 2;
240,809,558✔
261

262
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
240,785,552✔
263

264
  // load key part
265
  tBufferClear(buffer0);
266
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd, sttBlk->bInfo.offset, sttBlk->bInfo.szKey, buffer0, 0, pEncryptData),
240,796,616✔
267
                  &lino, _exit);
268

269
  // decode header
270
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
240,863,910✔
271
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
240,877,174✔
272

273
  if (hdr.delimiter != TSDB_FILE_DLMT) {
240,790,639✔
274
    tsdbError("vgId:%d %s failed at %s:%d since disk data header delimiter is invalid, fname:%s",
×
275
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd->path);
276
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
277
  }
278

279
  // set data container
280
  tBlockDataReset(bData);
240,790,639✔
281
  bData->suid = hdr.suid;
240,814,738✔
282
  bData->uid = (sttBlk->suid == 0) ? sttBlk->minUid : 0;
240,871,065✔
283
  bData->nRow = hdr.nRow;
240,826,969✔
284

285
  // key part
286
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
240,763,313✔
287
  if (br.offset != buffer0->size) {
240,828,087✔
UNCOV
288
    tsdbError("vgId:%d %s failed at %s:%d since key part size mismatch, expected:%u, actual:%u, fname:%s",
×
289
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer0->size, br.offset,
290
              reader->fd->path);
291
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
292
  }
293

294
  bool loadExtra = false;
240,826,872✔
295
  for (int i = 0; i < ncid; i++) {
288,414,774✔
296
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
232,852,802✔
297
      loadExtra = true;
185,257,068✔
298
      break;
185,257,068✔
299
    }
300
  }
301

302
  if (!loadExtra) {
240,819,040✔
303
    goto _exit;
55,559,020✔
304
  }
305

306
  // load SBlockCol part
307
  tBufferClear(buffer0);
308
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd, sttBlk->bInfo.offset + sttBlk->bInfo.szKey, hdr.szBlkCol, buffer0, 0,
185,257,159✔
309
                                       pEncryptData),
310
                  &lino, _exit);
311

312
  // load each column
313
  SBlockCol blockCol = {
185,308,279✔
314
      .cid = 0,
315
  };
316
  br = BUFFER_READER_INITIALIZER(0, buffer0);
185,310,829✔
317
  for (int32_t i = 0; i < ncid; i++) {
749,730,732✔
318
    int16_t cid = cids[i];
564,371,323✔
319

320
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
564,369,737✔
321
      continue;
22,400,027✔
322
    }
323

324
    while (cid > blockCol.cid) {
1,904,439,742✔
325
      if (br.offset >= buffer0->size) {
1,373,808,770✔
326
        blockCol.cid = INT16_MAX;
11,440,456✔
327
        break;
11,440,456✔
328
      }
329

330
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
1,362,425,787✔
331
    }
332

333
    if (cid < blockCol.cid) {
542,071,428✔
334
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
44,415,723✔
335
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
44,415,723✔
336
      SBlockCol none = {
44,415,723✔
337
          .cid = cid,
338
          .type = tcol->type,
44,411,264✔
339
          .cflag = tcol->flags,
44,413,175✔
340
          .flag = HAS_NONE,
341
          .szOrigin = 0,
342
          .szBitmap = 0,
343
          .szOffset = 0,
344
          .szValue = 0,
345
          .offset = 0,
346
      };
347
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &none, &br, bData, assist), &lino, _exit);
44,415,086✔
348
    } else if (cid == blockCol.cid) {
497,655,705✔
349
      // load from file
350
      tBufferClear(buffer1);
351
      TAOS_CHECK_GOTO(
497,627,687✔
352
          tsdbReadFileToBuffer(reader->fd, sttBlk->bInfo.offset + sttBlk->bInfo.szKey + hdr.szBlkCol + blockCol.offset,
353
                               blockCol.szBitmap + blockCol.szOffset + blockCol.szValue, buffer1, 0, pEncryptData),
354
          &lino, _exit);
355

356
      // decode the buffer
357
      SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
497,639,640✔
358
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
497,642,573✔
359
    }
360
  }
361

362
_exit:
240,993,305✔
363
  if (code) {
240,858,858✔
364
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
365
              tstrerror(code));
366
  }
367
  return code;
240,858,669✔
368
}
369

370
int32_t tsdbSttFileReadTombBlock(SSttFileReader *reader, const STombBlk *tombBlk, STombBlock *tombBlock) {
6,005,390✔
371
  int32_t code = 0;
6,005,390✔
372
  int32_t lino = 0;
6,005,390✔
373

374
  SBuffer *buffer0 = reader->buffers + 0;
6,005,390✔
375
  SBuffer *assist = reader->buffers + 1;
6,005,390✔
376

377
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
6,005,390✔
378

379
  // load
380
  tBufferClear(buffer0);
381
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd, tombBlk->dp->offset, tombBlk->dp->size, buffer0, 0, pEncryptData),
6,005,390✔
382
                  &lino, _exit);
383

384
  // decode
385
  int32_t       size = 0;
6,005,390✔
386
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
6,005,390✔
387
  tTombBlockClear(tombBlock);
6,005,390✔
388
  tombBlock->numOfRecords = tombBlk->numRec;
6,005,073✔
389
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); ++i) {
36,032,023✔
390
    SCompressInfo cinfo = {
30,026,633✔
391
        .cmprAlg = tombBlk->cmprAlg,
30,026,633✔
392
        .dataType = TSDB_DATA_TYPE_BIGINT,
393
        .originalSize = tombBlk->numRec * sizeof(int64_t),
30,026,633✔
394
        .compressedSize = tombBlk->size[i],
30,026,633✔
395
    };
396
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, tombBlock->buffers + i, assist), &lino, _exit);
30,026,633✔
397
    br.offset += tombBlk->size[i];
30,026,950✔
398
  }
399

400
  if (br.offset != tombBlk->dp->size) {
6,005,390✔
401
    tsdbError("vgId:%d %s failed at %s:%d since tomb block size mismatch, fname:%s",
×
402
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd->path);
403
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
404
  }
405

406
_exit:
6,005,390✔
407
  if (code) {
6,005,390✔
408
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
409
              tstrerror(code));
410
  }
411
  return code;
6,005,390✔
412
}
413

414
int32_t tsdbSttFileReadStatisBlock(SSttFileReader *reader, const SStatisBlk *statisBlk, STbStatisBlock *statisBlock) {
263,099,428✔
415
  int32_t code = 0;
263,099,428✔
416
  int32_t lino = 0;
263,099,428✔
417

418
  SBuffer *buffer0 = reader->buffers + 0;
263,116,621✔
419
  SBuffer *assist = reader->buffers + 1;
263,075,812✔
420

421
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
263,105,183✔
422

423
  // load data
424
  tBufferClear(buffer0);
425
  TAOS_CHECK_GOTO(
263,108,280✔
426
      tsdbReadFileToBuffer(reader->fd, statisBlk->dp->offset, statisBlk->dp->size, buffer0, 0, pEncryptData), &lino,
427
      _exit);
428

429
  // decode data
430
  tStatisBlockClear(statisBlock);
263,185,263✔
431
  statisBlock->numOfPKs = statisBlk->numOfPKs;
263,165,282✔
432
  statisBlock->numOfRecords = statisBlk->numRec;
263,159,348✔
433
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
263,165,534✔
434
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlock->buffers); ++i) {
1,578,990,947✔
435
    SCompressInfo info = {
1,315,779,862✔
436
        .dataType = TSDB_DATA_TYPE_BIGINT,
437
        .cmprAlg = statisBlk->cmprAlg,
1,315,773,987✔
438
        .compressedSize = statisBlk->size[i],
1,315,788,558✔
439
        .originalSize = statisBlk->numRec * sizeof(int64_t),
1,315,772,768✔
440
    };
441

442
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &info, &statisBlock->buffers[i], assist), &lino, _exit);
1,315,772,552✔
443
    br.offset += statisBlk->size[i];
1,315,884,223✔
444
  }
445

446
  if (statisBlk->numOfPKs > 0) {
263,211,085✔
447
    SValueColumnCompressInfo firstKeyInfos[TD_MAX_PK_COLS];
63,864,884✔
448
    SValueColumnCompressInfo lastKeyInfos[TD_MAX_PK_COLS];
63,865,521✔
449

450
    // decode compress info
451
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
127,727,837✔
452
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, &firstKeyInfos[i]), &lino, _exit);
63,864,247✔
453
    }
454

455
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
127,729,131✔
456
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, &lastKeyInfos[i]), &lino, _exit);
63,848,939✔
457
    }
458

459
    // decode value columns
460
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
127,723,378✔
461
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), firstKeyInfos + i, &statisBlock->firstKeyPKs[i], assist),
63,853,398✔
462
                      &lino, _exit);
463
      br.offset += (firstKeyInfos[i].dataCompressedSize + firstKeyInfos[i].offsetCompressedSize);
63,864,247✔
464
    }
465

466
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
127,722,721✔
467
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), &lastKeyInfos[i], &statisBlock->lastKeyPKs[i], assist), &lino,
63,844,460✔
468
                      _exit);
469
      br.offset += (lastKeyInfos[i].dataCompressedSize + lastKeyInfos[i].offsetCompressedSize);
63,858,514✔
470
    }
471
  }
472

473
  if (br.offset != buffer0->size) {
263,209,463✔
474
    tsdbError("vgId:%d %s failed at %s:%d since statis block size mismatch, expected: %u, actual: %u, fname:%s",
×
475
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer0->size, br.offset,
476
              reader->fd->path);
477
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
478
  }
479

480
_exit:
263,236,922✔
481
  if (code) {
263,188,769✔
482
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
483
              tstrerror(code));
484
  }
485
  return code;
263,188,769✔
486
}
487

488
// SSttFWriter ============================================================
489
struct SSttFileWriter {
490
  SSttFileWriterConfig config[1];
491
  struct {
492
    bool    opened;
493
    TABLEID tbid[1];
494
    // range
495
    SVersionRange range;
496
  } ctx[1];
497
  // file
498
  STsdbFD *fd;
499
  STFile   file[1];
500
  // data
501
  SSttFooter      footer[1];
502
  TTombBlkArray   tombBlkArray[1];
503
  TSttBlkArray    sttBlkArray[1];
504
  TStatisBlkArray statisBlkArray[1];
505
  STombBlock      tombBlock[1];
506
  STbStatisBlock  staticBlock[1];
507
  SBlockData      blockData[1];
508
  // helper data
509
  SSkmInfo skmTb[1];
510
  SSkmInfo skmRow[1];
511
  SBuffer  local[10];
512
  SBuffer *buffers;
513
  // SColCompressInfo2 pInfo;
514
};
515

516
static int32_t tsdbFileDoWriteSttBlockData(STsdbFD *fd, SBlockData *blockData, SColCompressInfo *info,
43,357,142✔
517
                                           int64_t *fileSize, TSttBlkArray *sttBlkArray, SBuffer *buffers,
518
                                           SVersionRange *range, SEncryptData *encryptData) {
519
  if (blockData->nRow == 0) return 0;
43,357,142✔
520

521
  int32_t code = 0;
43,361,294✔
522

523
  SSttBlk sttBlk[1] = {{
43,364,344✔
524
      .suid = blockData->suid,
43,360,156✔
525
      .minUid = blockData->uid ? blockData->uid : blockData->aUid[0],
43,359,641✔
526
      .maxUid = blockData->uid ? blockData->uid : blockData->aUid[blockData->nRow - 1],
43,366,967✔
527
      .minKey = blockData->aTSKEY[0],
43,364,412✔
528
      .maxKey = blockData->aTSKEY[0],
43,361,994✔
529
      .minVer = blockData->aVersion[0],
43,366,077✔
530
      .maxVer = blockData->aVersion[0],
43,362,937✔
531
      .nRow = blockData->nRow,
43,359,935✔
532
  }};
533

534
  for (int32_t iRow = 1; iRow < blockData->nRow; iRow++) {
2,147,483,647✔
535
    if (sttBlk->minKey > blockData->aTSKEY[iRow]) sttBlk->minKey = blockData->aTSKEY[iRow];
2,147,483,647✔
536
    if (sttBlk->maxKey < blockData->aTSKEY[iRow]) sttBlk->maxKey = blockData->aTSKEY[iRow];
2,147,483,647✔
537
    if (sttBlk->minVer > blockData->aVersion[iRow]) sttBlk->minVer = blockData->aVersion[iRow];
2,147,483,647✔
538
    if (sttBlk->maxVer < blockData->aVersion[iRow]) sttBlk->maxVer = blockData->aVersion[iRow];
2,147,483,647✔
539
  }
540

541
  tsdbWriterUpdVerRange(range, sttBlk->minVer, sttBlk->maxVer);
43,366,148✔
542
  TAOS_CHECK_RETURN(tBlockDataCompress(blockData, info, buffers, buffers + 4));
43,364,475✔
543

544
  sttBlk->bInfo.offset = *fileSize;
43,354,138✔
545
  sttBlk->bInfo.szKey = buffers[0].size + buffers[1].size;
43,352,979✔
546
  sttBlk->bInfo.szBlock = buffers[2].size + buffers[3].size + sttBlk->bInfo.szKey;
43,360,845✔
547
  for (int i = 0; i < 4; i++) {
216,808,523✔
548
    if (buffers[i].size) {
173,440,759✔
549
      TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffers[i].data, buffers[i].size, encryptData));
173,226,776✔
550
      *fileSize += buffers[i].size;
173,226,909✔
551
    }
552
  }
553

554
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(sttBlkArray, sttBlk));
86,733,681✔
555

556
  tBlockDataClear(blockData);
43,365,917✔
557
  return 0;
43,366,836✔
558
}
559

560
static int32_t tsdbSttFileDoWriteBlockData(SSttFileWriter *writer) {
63,148,141✔
561
  if (writer->blockData->nRow == 0) return 0;
63,148,141✔
562

563
  int32_t code = 0;
43,367,158✔
564
  int32_t lino = 0;
43,367,158✔
565

566
  tb_uid_t         uid = writer->blockData->suid == 0 ? writer->blockData->uid : writer->blockData->suid;
43,367,475✔
567
  SColCompressInfo info = {.defaultCmprAlg = writer->config->cmprAlg, .pColCmpr = NULL};
43,365,946✔
568
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, uid, &(info.pColCmpr));
43,367,452✔
569

570
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
43,362,729✔
571

572
  TAOS_CHECK_GOTO(tsdbFileDoWriteSttBlockData(writer->fd, writer->blockData, &info, &writer->file->size,
43,365,778✔
573
                                              writer->sttBlkArray, writer->buffers, &writer->ctx->range, pEncryptData),
574
                  &lino, _exit);
575

576
_exit:
43,360,997✔
577
  if (code) {
43,361,057✔
578
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
579
              tstrerror(code));
580
  }
581
  taosHashCleanup(info.pColCmpr);
43,361,057✔
582
  return code;
43,355,895✔
583
}
584

585
static int32_t tsdbSttFileDoWriteStatisBlock(SSttFileWriter *writer) {
20,095,865✔
586
  if (writer->staticBlock->numOfRecords == 0) {
20,095,865✔
587
    return 0;
1,352,633✔
588
  }
589

590
  int32_t code = 0;
18,745,593✔
591
  int32_t lino = 0;
18,745,593✔
592

593
  SBuffer *buffer0 = writer->buffers + 0;
18,744,038✔
594
  SBuffer *buffer1 = writer->buffers + 1;
18,748,353✔
595
  SBuffer *assist = writer->buffers + 2;
18,747,924✔
596

597
  STbStatisRecord record;
18,745,273✔
598
  STbStatisBlock *statisBlock = writer->staticBlock;
18,747,075✔
599
  SStatisBlk      statisBlk = {0};
18,748,059✔
600

601
  statisBlk.dp->offset = writer->file->size;
18,747,959✔
602
  statisBlk.dp->size = 0;
18,747,771✔
603
  statisBlk.numRec = statisBlock->numOfRecords;
18,747,771✔
604
  statisBlk.cmprAlg = writer->config->cmprAlg;
18,742,573✔
605
  statisBlk.numOfPKs = statisBlock->numOfPKs;
18,745,412✔
606

607
  code = tStatisBlockGet(statisBlock, 0, &record);
18,746,614✔
608
  TSDB_CHECK_CODE(code, lino, _exit);
18,744,757✔
609
  statisBlk.minTbid.suid = record.suid;
18,744,757✔
610
  statisBlk.minTbid.uid = record.uid;
18,744,757✔
611

612
  code = tStatisBlockGet(statisBlock, statisBlock->numOfRecords - 1, &record);
18,744,757✔
613
  TSDB_CHECK_CODE(code, lino, _exit);
18,745,278✔
614
  statisBlk.maxTbid.suid = record.suid;
18,745,278✔
615
  statisBlk.maxTbid.uid = record.uid;
18,745,278✔
616

617
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,745,278✔
618

619
  // compress each column
620
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlk.size); i++) {
112,481,627✔
621
    SCompressInfo info = {
93,732,410✔
622
        .dataType = TSDB_DATA_TYPE_BIGINT,
623
        .cmprAlg = statisBlk.cmprAlg,
93,732,169✔
624
        .originalSize = statisBlock->buffers[i].size,
93,732,169✔
625
    };
626

627
    tBufferClear(buffer0);
628
    TAOS_CHECK_GOTO(tCompressDataToBuffer(statisBlock->buffers[i].data, &info, buffer0, assist), &lino, _exit);
93,734,399✔
629
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, buffer0->data, info.compressedSize, pEncryptData),
93,736,180✔
630
                    &lino, _exit);
631

632
    statisBlk.size[i] = info.compressedSize;
93,740,928✔
633
    statisBlk.dp->size += info.compressedSize;
93,739,329✔
634
    writer->file->size += info.compressedSize;
93,739,329✔
635
  }
636

637
  // compress primary keys
638
  if (statisBlk.numOfPKs > 0) {
18,749,217✔
639
    SValueColumnCompressInfo compressInfo = {.cmprAlg = statisBlk.cmprAlg};
424,217✔
640

641
    tBufferClear(buffer0);
642
    tBufferClear(buffer1);
643

644
    for (int32_t i = 0; i < statisBlk.numOfPKs; i++) {
848,434✔
645
      TAOS_CHECK_GOTO(tValueColumnCompress(&statisBlock->firstKeyPKs[i], &compressInfo, buffer1, assist), &lino, _exit);
424,217✔
646
      TAOS_CHECK_GOTO(tValueColumnCompressInfoEncode(&compressInfo, buffer0), &lino, _exit);
424,217✔
647
    }
648

649
    for (int32_t i = 0; i < statisBlk.numOfPKs; i++) {
848,434✔
650
      TAOS_CHECK_GOTO(tValueColumnCompress(&statisBlock->lastKeyPKs[i], &compressInfo, buffer1, assist), &lino, _exit);
424,217✔
651
      TAOS_CHECK_GOTO(tValueColumnCompressInfoEncode(&compressInfo, buffer0), &lino, _exit);
424,217✔
652
    }
653

654
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, buffer0->data, buffer0->size, pEncryptData), &lino,
424,217✔
655
                    _exit);
656
    writer->file->size += buffer0->size;
424,217✔
657
    statisBlk.dp->size += buffer0->size;
424,217✔
658

659
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, buffer1->data, buffer1->size, pEncryptData), &lino,
424,217✔
660
                    _exit);
661
    writer->file->size += buffer1->size;
424,217✔
662
    statisBlk.dp->size += buffer1->size;
424,217✔
663
  }
664

665
  TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(writer->statisBlkArray, &statisBlk), &lino, _exit);
37,498,022✔
666

667
  tStatisBlockClear(writer->staticBlock);
18,748,805✔
668

669
_exit:
18,749,736✔
670
  if (code) {
18,748,332✔
671
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
672
              tstrerror(code));
673
  }
674
  return code;
18,746,962✔
675
}
676

677
static int32_t tsdbSttFileDoWriteTombBlock(SSttFileWriter *writer) {
18,495,292✔
678
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) {
18,495,292✔
679
    return 0;
16,949,890✔
680
  }
681

682
  int32_t code = 0;
1,546,790✔
683
  int32_t lino = 0;
1,546,790✔
684

685
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,546,790✔
686

687
  TAOS_CHECK_GOTO(tsdbFileWriteTombBlock(writer->fd, writer->tombBlock, writer->config->cmprAlg, &writer->file->size,
1,546,790✔
688
                                         writer->tombBlkArray, writer->buffers, &writer->ctx->range, pEncryptData),
689
                  &lino, _exit);
690

691
_exit:
1,546,790✔
692
  if (code) {
1,546,790✔
693
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
694
              tstrerror(code));
695
  }
696
  return code;
1,546,790✔
697
}
698

699
int32_t tsdbFileWriteSttBlk(STsdbFD *fd, const TSttBlkArray *sttBlkArray, SFDataPtr *ptr, int64_t *fileSize,
18,492,520✔
700
                            SEncryptData *encryptData) {
701
  ptr->size = TARRAY2_DATA_LEN(sttBlkArray);
18,492,520✔
702
  if (ptr->size > 0) {
18,495,585✔
703
    ptr->offset = *fileSize;
18,446,999✔
704

705
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(sttBlkArray), ptr->size, encryptData));
18,446,689✔
706

707
    *fileSize += ptr->size;
18,447,521✔
708
  }
709
  return 0;
18,496,997✔
710
}
711

712
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
18,495,153✔
713
  int32_t code = 0;
18,495,153✔
714
  int32_t lino;
18,494,706✔
715

716
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,495,153✔
717

718
  TAOS_CHECK_GOTO(tsdbFileWriteSttBlk(writer->fd, writer->sttBlkArray, writer->footer->sttBlkPtr, &writer->file->size,
18,496,222✔
719
                                      pEncryptData),
720
                  &lino, _exit);
721

722
_exit:
18,497,468✔
723
  if (code) {
18,497,091✔
724
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
725
              tstrerror(code));
726
  }
727
  return code;
18,497,091✔
728
}
729

730
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
18,492,373✔
731
  int32_t code = 0;
18,492,373✔
732
  int32_t lino;
18,491,926✔
733
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,493,642✔
734

735
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
18,494,611✔
736
  if (writer->footer->statisBlkPtr->size) {
18,494,890✔
737
    writer->footer->statisBlkPtr->offset = writer->file->size;
18,447,721✔
738
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
18,444,991✔
739
                                  writer->footer->statisBlkPtr->size, pEncryptData),
740
                    &lino, _exit);
741
    writer->file->size += writer->footer->statisBlkPtr->size;
18,446,733✔
742
  }
743

744
_exit:
18,494,118✔
745
  if (code) {
18,494,907✔
746
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
747
              tstrerror(code));
748
  }
749
  return code;
18,494,907✔
750
}
751

752
static int32_t tsdbSttFileDoWriteTombBlk(SSttFileWriter *writer) {
18,492,121✔
753
  int32_t code = 0;
18,492,121✔
754
  int32_t lino = 0;
18,492,121✔
755

756
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,494,291✔
757

758
  TAOS_CHECK_GOTO(tsdbFileWriteTombBlk(writer->fd, writer->tombBlkArray, writer->footer->tombBlkPtr,
18,494,588✔
759
                                       &writer->file->size, pEncryptData),
760
                  &lino, _exit);
761

762
_exit:
18,492,416✔
763
  if (code) {
18,492,039✔
764
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
765
              tstrerror(code));
766
  }
767
  return code;
18,492,039✔
768
}
769

770
int32_t tsdbFileWriteSttFooter(STsdbFD *fd, const SSttFooter *footer, int64_t *fileSize, SEncryptData *encryptData) {
18,492,968✔
771
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
18,492,968✔
772
  *fileSize += sizeof(*footer);
18,493,924✔
773
  return 0;
18,490,764✔
774
}
775

776
static int32_t tsdbSttFileDoWriteFooter(SSttFileWriter *writer) {
18,488,137✔
777
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,488,137✔
778

779
  return tsdbFileWriteSttFooter(writer->fd, writer->footer, &writer->file->size, pEncryptData);
18,493,204✔
780
}
781

782
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
18,470,202✔
783
  int32_t code = 0;
18,470,202✔
784
  int32_t lino = 0;
18,470,202✔
785
  STsdb  *tsdb = writer->config->tsdb;
18,478,980✔
786

787
  // set
788
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
18,480,540✔
789
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
18,480,002✔
790
  writer->buffers = writer->config->buffers;
18,484,716✔
791
  if (writer->buffers == NULL) {
18,486,783✔
792
    writer->buffers = writer->local;
×
793
  }
794

795
  // alloc disk id
796
  SDiskID diskId = {0};
18,483,779✔
797
  code = tsdbAllocateDisk(tsdb, tsdbFTypeLabel(TSDB_FTYPE_STT), writer->config->expLevel, &diskId);
18,477,190✔
798
  TSDB_CHECK_CODE(code, lino, _exit);
18,480,230✔
799

800
  writer->file[0] = (STFile){
18,477,524✔
801
      .type = TSDB_FTYPE_STT,
802
      .did = diskId,
803
      .fid = writer->config->fid,
18,480,230✔
804
      .cid = writer->config->cid,
18,491,998✔
805
      .size = 0,
806
      .minVer = VERSION_MAX,
807
      .maxVer = VERSION_MIN,
808
      .stt[0] =
809
          {
810
              .level = writer->config->level,
18,488,975✔
811
          },
812
  };
813

814
  // open file
815
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
18,484,302✔
816
  char    fname[TSDB_FILENAME_LEN];
18,483,855✔
817

818
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
18,483,394✔
819
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd, 0), &lino, _exit);
18,474,468✔
820

821
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
18,480,011✔
822
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,476,542✔
823

824
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, 0, hdr, sizeof(hdr), pEncryptData), &lino, _exit);
18,487,017✔
825
  writer->file->size += sizeof(hdr);
18,474,097✔
826

827
  // range
828
  writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
18,492,313✔
829

830
  writer->ctx->opened = true;
18,489,124✔
831

832
_exit:
18,489,834✔
833
  if (code) {
18,493,651✔
834
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
835
              tstrerror(code));
836
  }
837
  return code;
18,493,651✔
838
}
839

840
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
18,481,858✔
841
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
202,952,398✔
842
    tBufferDestroy(writer->local + i);
184,482,542✔
843
  }
844
  tDestroyTSchema(writer->skmRow->pTSchema);
18,469,856✔
845
  tDestroyTSchema(writer->skmTb->pTSchema);
18,481,857✔
846
  tTombBlockDestroy(writer->tombBlock);
18,483,997✔
847
  tStatisBlockDestroy(writer->staticBlock);
18,480,719✔
848
  tBlockDataDestroy(writer->blockData);
18,481,711✔
849
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
18,492,814✔
850
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
18,493,398✔
851
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
18,491,982✔
852
}
18,490,815✔
853

854
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
18,489,711✔
855
  // TODO
856
  return 0;
18,489,711✔
857
}
858

859
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
18,494,203✔
860
  int32_t lino;
18,493,756✔
861
  int32_t code;
862

863
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
18,494,203✔
864
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlock(writer), &lino, _exit);
18,491,857✔
865
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlock(writer), &lino, _exit);
18,495,434✔
866
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteSttBlk(writer), &lino, _exit);
18,496,206✔
867
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlk(writer), &lino, _exit);
18,492,696✔
868
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlk(writer), &lino, _exit);
18,493,833✔
869
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteFooter(writer), &lino, _exit);
18,491,791✔
870
  TAOS_CHECK_GOTO(tsdbSttFileDoUpdateHeader(writer), &lino, _exit);
18,491,207✔
871

872
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,490,154✔
873

874
  TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd, pEncryptData), &lino, _exit);
18,493,628✔
875

876
  tsdbCloseFile(&writer->fd);
18,490,039✔
877

878
  STFileOp op = (STFileOp){
36,966,870✔
879
      .optype = TSDB_FOP_CREATE,
880
      .fid = writer->config->fid,
18,480,368✔
881
      .nf = writer->file[0],
882
  };
883
  tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
18,490,021✔
884

885
  TAOS_CHECK_GOTO(TARRAY2_APPEND(opArray, op), &lino, _exit);
36,951,654✔
886

887
_exit:
18,466,976✔
888
  if (code) {
18,466,568✔
889
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
890
              tstrerror(code));
891
  }
892
  return code;
18,466,568✔
893
}
894

895
static int32_t tsdbSttFWriterCloseAbort(SSttFileWriter *writer) {
×
896
  char fname[TSDB_FILENAME_LEN];
×
897
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
×
898
  tsdbCloseFile(&writer->fd);
×
899
  tsdbRemoveFile(fname);
×
900
  return 0;
×
901
}
902

903
int32_t tsdbSttFileWriterOpen(const SSttFileWriterConfig *config, SSttFileWriter **writer) {
19,236,448✔
904
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
19,236,448✔
905
  if (writer[0] == NULL) {
19,209,553✔
906
    return terrno;
×
907
  }
908

909
  writer[0]->config[0] = config[0];
19,212,844✔
910
  writer[0]->ctx->opened = false;
19,212,670✔
911
  return 0;
19,243,967✔
912
}
913

914
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
19,249,992✔
915
  if (writer == NULL || writer[0] == NULL) return 0;
19,249,992✔
916

917
  int32_t code = 0;
19,251,662✔
918
  int32_t lino = 0;
19,251,662✔
919

920
  if (writer[0]->ctx->opened) {
19,250,263✔
921
    if (abort) {
18,493,631✔
922
      TAOS_CHECK_GOTO(tsdbSttFWriterCloseAbort(writer[0]), &lino, _exit);
×
923
    } else {
924
      TAOS_CHECK_GOTO(tsdbSttFWriterCloseCommit(writer[0], opArray), &lino, _exit);
18,493,631✔
925
    }
926
    tsdbSttFWriterDoClose(writer[0]);
18,469,844✔
927
  }
928
  taosMemoryFree(writer[0]);
19,247,465✔
929
  writer[0] = NULL;
19,243,836✔
930

931
_exit:
19,245,472✔
932
  if (code) {
19,246,580✔
933
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer[0]->config->tsdb->pVnode), __func__, __FILE__, lino,
×
934
              tstrerror(code));
935
  }
936
  return code;
19,246,305✔
937
}
938

939
int32_t tsdbSttFileWriteRow(SSttFileWriter *writer, SRowInfo *row) {
2,147,483,647✔
940
  int32_t code = 0;
2,147,483,647✔
941
  int32_t lino = 0;
2,147,483,647✔
942

943
  if (!writer->ctx->opened) {
2,147,483,647✔
944
    TAOS_CHECK_GOTO(tsdbSttFWriterDoOpen(writer), &lino, _exit);
18,432,610✔
945
  }
946

947
  if (!TABLE_SAME_SCHEMA(row->suid, row->uid, writer->ctx->tbid->suid, writer->ctx->tbid->uid)) {
2,147,483,647✔
948
    TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
24,296,360✔
949

950
    TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, (TABLEID *)row, writer->config->skmTb), &lino, _exit);
24,296,225✔
951

952
    TABLEID id = {
24,296,067✔
953
        .suid = row->suid,
24,296,585✔
954
        .uid = row->suid ? 0 : row->uid,
24,299,702✔
955
    };
956
    TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0), &lino, _exit);
24,294,971✔
957
  }
958

959
  if (writer->ctx->tbid->uid != row->uid) {
2,147,483,647✔
960
    writer->ctx->tbid->suid = row->suid;
88,199,349✔
961
    writer->ctx->tbid->uid = row->uid;
88,207,750✔
962
  }
963

964
  STsdbRowKey key;
2,147,483,647✔
965
  tsdbRowGetKey(&row->row, &key);
2,147,483,647✔
966

967
  for (;;) {
968
    code = tStatisBlockPut(writer->staticBlock, row, writer->config->maxRow);
2,147,483,647✔
969
    if (code == TSDB_CODE_INVALID_PARA) {
2,147,483,647✔
970
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlock(writer), &lino, _exit);
105,903✔
971
      continue;
105,903✔
972
    } else {
973
      TSDB_CHECK_CODE(code, lino, _exit);
2,147,483,647✔
974
    }
975
    break;
2,147,483,647✔
976
  }
977

978
  if (row->row.type == TSDBROW_ROW_FMT) {
2,147,483,647✔
979
    TAOS_CHECK_GOTO(tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
2,147,483,647✔
980
                                     TSDBROW_SVERSION(&row->row), writer->config->skmRow),
981
                    &lino, _exit);
982
  }
983

984
  // row to col conversion
985
  if (key.version <= writer->config->compactVersion                                //
2,147,483,647✔
986
      && writer->blockData->nRow > 0                                               //
2,147,483,647✔
987
      && (writer->blockData->uid                                                   //
2,147,483,647✔
988
              ? writer->blockData->uid                                             //
989
              : writer->blockData->aUid[writer->blockData->nRow - 1]) == row->uid  //
2,147,483,647✔
990
      && tsdbRowCompareWithoutVersion(&row->row,
2,147,483,647✔
991
                                      &tsdbRowFromBlockData(writer->blockData, writer->blockData->nRow - 1)) == 0  //
2,147,483,647✔
992
  ) {
993
    TAOS_CHECK_GOTO(tBlockDataUpdateRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema), &lino, _exit);
180,021,496✔
994
  } else {
995
    if (writer->blockData->nRow >= writer->config->maxRow) {
2,147,483,647✔
996
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
18,868,081✔
997
    }
998

999
    TAOS_CHECK_GOTO(tBlockDataAppendRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema, row->uid),
2,147,483,647✔
1000
                    &lino, _exit);
1001
  }
1002

1003
_exit:
2,147,483,647✔
1004
  if (code) {
2,147,483,647✔
1005
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1006
              tstrerror(code));
1007
  }
1008
  return code;
2,147,483,647✔
1009
}
1010

1011
int32_t tsdbSttFileWriteBlockData(SSttFileWriter *writer, SBlockData *bdata) {
11,554,724✔
1012
  int32_t code = 0;
11,554,724✔
1013
  int32_t lino = 0;
11,554,724✔
1014

1015
  SRowInfo row[1];
11,555,261✔
1016
  row->suid = bdata->suid;
11,556,608✔
1017
  for (int32_t i = 0; i < bdata->nRow; i++) {
129,870,888✔
1018
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
118,310,041✔
1019
    row->row = tsdbRowFromBlockData(bdata, i);
118,308,628✔
1020

1021
    TAOS_CHECK_GOTO(tsdbSttFileWriteRow(writer, row), &lino, _exit);
118,308,628✔
1022
  }
1023

1024
_exit:
11,557,079✔
1025
  if (code) {
11,557,079✔
1026
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1027
              tstrerror(code));
1028
  }
1029
  return code;
11,557,079✔
1030
}
1031

1032
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
6,179,672✔
1033
  int32_t code;
1034
  int32_t lino;
6,179,672✔
1035

1036
  if (!writer->ctx->opened) {
6,179,672✔
1037
    TAOS_CHECK_GOTO(tsdbSttFWriterDoOpen(writer), &lino, _exit);
48,311✔
1038
  } else {
1039
    if (writer->blockData->nRow > 0) {
6,131,361✔
1040
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
1,498,479✔
1041
    }
1042

1043
    if (STATIS_BLOCK_SIZE(writer->staticBlock) > 0) {
6,131,361✔
1044
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlock(writer), &lino, _exit);
1,498,479✔
1045
    }
1046
  }
1047

1048
  TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
6,179,672✔
1049

1050
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
6,179,672✔
1051
    TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlock(writer), &lino, _exit);
×
1052
  }
1053

1054
_exit:
6,179,672✔
1055
  if (code) {
6,179,672✔
1056
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1057
              tstrerror(code));
1058
  } else {
1059
    tsdbTrace("vgId:%d write tomb record to stt file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
6,179,672✔
1060
              ", version:%" PRId64,
1061
              TD_VID(writer->config->tsdb->pVnode), writer->fd->path, writer->config->cid, record->suid, record->uid,
1062
              record->version);
1063
  }
1064
  return code;
6,179,672✔
1065
}
1066

1067
bool tsdbSttFileWriterIsOpened(SSttFileWriter *writer) { return writer->ctx->opened; }
3,941,782✔
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