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

taosdata / TDengine / #5013

03 Apr 2026 03:59PM UTC coverage: 72.317% (+0.01%) from 72.305%
#5013

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%)

13131 existing lines in 160 files now uncovered.

257489 of 356056 relevant lines covered (72.32%)

129893134.08 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) {
257,125,257✔
39
  int32_t code = 0;
257,125,257✔
40
  int32_t lino = 0;
257,125,257✔
41

42
  reader[0] = taosMemoryCalloc(1, sizeof(*reader[0]));
257,161,994✔
43
  if (reader[0] == NULL) {
256,946,781✔
44
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
45
  }
46

47
  reader[0]->config[0] = config[0];
256,951,381✔
48
  reader[0]->buffers = config->buffers;
257,041,458✔
49
  if (reader[0]->buffers == NULL) {
257,156,460✔
50
    reader[0]->buffers = reader[0]->local;
257,136,705✔
51
  }
52

53
  // open file
54
  if (fname) {
257,179,886✔
55
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, config->tsdb, TD_FILE_READ, &reader[0]->fd, 0), &lino, _exit);
257,179,886✔
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);
257,137,272✔
64
  if (offset < TSDB_FHDR_SIZE) {
257,086,303✔
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);
257,086,303✔
71

72
#if 1
73
  TAOS_CHECK_GOTO(
257,141,048✔
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:
257,121,641✔
95
  if (code) {
257,140,491✔
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;
257,140,537✔
101
}
102

103
void tsdbSttFileReaderClose(SSttFileReader **reader) {
257,165,975✔
104
  if (reader[0]) {
257,165,975✔
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);
257,159,959✔
109
    TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
257,159,611✔
110
    TARRAY2_DESTROY(reader[0]->statisBlkArray, NULL);
257,164,862✔
111
    TARRAY2_DESTROY(reader[0]->sttBlkArray, NULL);
257,177,703✔
112
    taosMemoryFree(reader[0]);
257,182,508✔
113
    reader[0] = NULL;
257,156,234✔
114
  }
115
}
257,148,889✔
116

117
// SSttFSegReader
118
int32_t tsdbSttFileReadStatisBlk(SSttFileReader *reader, const TStatisBlkArray **statisBlkArray) {
253,878,956✔
119
  if (!reader->ctx->statisBlkLoaded) {
253,878,956✔
120
    if (reader->footer->statisBlkPtr->size > 0) {
253,903,451✔
121
      if (reader->footer->statisBlkPtr->size % sizeof(SStatisBlk) != 0) {
253,806,389✔
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);
253,658,316✔
128
      void   *data = taosMemoryMalloc(reader->footer->statisBlkPtr->size);
253,767,930✔
129
      if (!data) {
253,717,991✔
130
        return terrno;
×
131
      }
132

133
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
253,717,991✔
134

135
      int32_t code = tsdbReadFile(reader->fd, reader->footer->statisBlkPtr->offset, data,
253,782,922✔
136
                                  reader->footer->statisBlkPtr->size, 0, pEncryptData);
137
      if (code) {
253,773,521✔
138
        taosMemoryFree(data);
×
139
        return code;
×
140
      }
141

142
      TARRAY2_INIT_EX(reader->statisBlkArray, size, size, data);
253,773,521✔
143
    } else {
144
      TARRAY2_INIT(reader->statisBlkArray);
146,671✔
145
    }
146

147
    reader->ctx->statisBlkLoaded = true;
253,900,903✔
148
  }
149

150
  statisBlkArray[0] = reader->statisBlkArray;
253,946,444✔
151
  return 0;
253,914,166✔
152
}
153

154
int32_t tsdbSttFileReadTombBlk(SSttFileReader *reader, const TTombBlkArray **tombBlkArray) {
257,111,477✔
155
  if (!reader->ctx->tombBlkLoaded) {
257,111,477✔
156
    if (reader->footer->tombBlkPtr->size > 0) {
257,134,994✔
157
      if (reader->footer->tombBlkPtr->size % sizeof(STombBlk) != 0) {
11,839,897✔
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,839,897✔
164
      void   *data = taosMemoryMalloc(reader->footer->tombBlkPtr->size);
11,839,897✔
165
      if (!data) {
11,839,238✔
166
        return terrno;
×
167
      }
168

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

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

178
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
11,839,897✔
179
    } else {
180
      TARRAY2_INIT(reader->tombBlkArray);
245,202,492✔
181
    }
182

183
    reader->ctx->tombBlkLoaded = true;
257,084,194✔
184
  }
185

186
  tombBlkArray[0] = reader->tombBlkArray;
257,119,658✔
187
  return 0;
257,132,713✔
188
}
189

190
int32_t tsdbSttFileReadSttBlk(SSttFileReader *reader, const TSttBlkArray **sttBlkArray) {
257,075,080✔
191
  if (!reader->ctx->sttBlkLoaded) {
257,075,080✔
192
    if (reader->footer->sttBlkPtr->size > 0) {
257,077,646✔
193
      if (reader->footer->sttBlkPtr->size % sizeof(SSttBlk) != 0) {
256,957,931✔
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);
256,787,929✔
200
      void   *data = taosMemoryMalloc(reader->footer->sttBlkPtr->size);
256,906,637✔
201
      if (!data) {
256,862,284✔
202
        return terrno;
×
203
      }
204

205
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
256,862,284✔
206

207
      int32_t code = tsdbReadFile(reader->fd, reader->footer->sttBlkPtr->offset, data, reader->footer->sttBlkPtr->size,
256,953,729✔
208
                                  0, pEncryptData);
209
      if (code) {
256,942,299✔
210
        taosMemoryFree(data);
×
211
        return code;
×
212
      }
213

214
      TARRAY2_INIT_EX(reader->sttBlkArray, size, size, data);
256,942,322✔
215
    } else {
216
      TARRAY2_INIT(reader->sttBlkArray);
194,968✔
217
    }
218

219
    reader->ctx->sttBlkLoaded = true;
257,117,983✔
220
  }
221

222
  sttBlkArray[0] = reader->sttBlkArray;
257,221,122✔
223
  return 0;
257,141,495✔
224
}
225

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

230
  SBuffer *buffer0 = reader->buffers + 0;
16,209,715✔
231
  SBuffer *assist = reader->buffers + 1;
16,209,056✔
232

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

235
  // load data
236
  tBufferClear(buffer0);
237
  TAOS_CHECK_GOTO(
16,207,743✔
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,210,369✔
242
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
16,210,369✔
243

244
_exit:
16,210,369✔
245
  if (code) {
16,210,369✔
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,210,369✔
250
}
251

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

257
  SDiskDataHdr hdr;
188,604,526✔
258
  SBuffer     *buffer0 = reader->buffers + 0;
188,903,638✔
259
  SBuffer     *buffer1 = reader->buffers + 1;
188,886,218✔
260
  SBuffer     *assist = reader->buffers + 2;
188,909,307✔
261

262
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
188,870,559✔
263

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

269
  // decode header
270
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
188,943,674✔
271
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
188,951,619✔
272

273
  if (hdr.delimiter != TSDB_FILE_DLMT) {
188,875,503✔
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);
188,875,503✔
281
  bData->suid = hdr.suid;
188,888,948✔
282
  bData->uid = (sttBlk->suid == 0) ? sttBlk->minUid : 0;
188,934,216✔
283
  bData->nRow = hdr.nRow;
188,901,543✔
284

285
  // key part
286
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
188,852,489✔
287
  if (br.offset != buffer0->size) {
188,901,881✔
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;
188,895,824✔
295
  for (int i = 0; i < ncid; i++) {
191,073,457✔
296
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
161,346,197✔
297
      loadExtra = true;
159,169,970✔
298
      break;
159,169,970✔
299
    }
300
  }
301

302
  if (!loadExtra) {
188,897,230✔
303
    goto _exit;
29,733,401✔
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,
159,170,889✔
309
                                       pEncryptData),
310
                  &lino, _exit);
311

312
  // load each column
313
  SBlockCol blockCol = {
159,205,487✔
314
      .cid = 0,
315
  };
316
  br = BUFFER_READER_INITIALIZER(0, buffer0);
159,208,709✔
317
  for (int32_t i = 0; i < ncid; i++) {
599,240,304✔
318
    int16_t cid = cids[i];
439,983,824✔
319

320
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
439,982,879✔
321
      continue;
1,882,419✔
322
    }
323

324
    while (cid > blockCol.cid) {
1,544,342,061✔
325
      if (br.offset >= buffer0->size) {
1,106,964,936✔
326
        blockCol.cid = INT16_MAX;
816,497✔
327
        break;
816,497✔
328
      }
329

330
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
1,106,198,389✔
331
    }
332

333
    if (cid < blockCol.cid) {
438,193,622✔
334
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
3,969,387✔
335
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
3,969,387✔
336
      SBlockCol none = {
3,969,387✔
337
          .cid = cid,
338
          .type = tcol->type,
3,969,387✔
339
          .cflag = tcol->flags,
3,969,387✔
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);
3,969,387✔
348
    } else if (cid == blockCol.cid) {
434,224,235✔
349
      // load from file
350
      tBufferClear(buffer1);
351
      TAOS_CHECK_GOTO(
434,183,753✔
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);
434,202,519✔
358
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
434,204,129✔
359
    }
360
  }
361

362
_exit:
189,053,914✔
363
  if (code) {
188,935,943✔
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;
188,935,552✔
368
}
369

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

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

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

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

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

400
  if (br.offset != tombBlk->dp->size) {
6,009,813✔
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,009,813✔
407
  if (code) {
6,009,813✔
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,009,813✔
412
}
413

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

418
  SBuffer *buffer0 = reader->buffers + 0;
205,302,020✔
419
  SBuffer *assist = reader->buffers + 1;
205,274,503✔
420

421
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
205,294,082✔
422

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

429
  // decode data
430
  tStatisBlockClear(statisBlock);
205,346,284✔
431
  statisBlock->numOfPKs = statisBlk->numOfPKs;
205,336,434✔
432
  statisBlock->numOfRecords = statisBlk->numRec;
205,326,062✔
433
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
205,351,125✔
434
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlock->buffers); ++i) {
1,232,083,031✔
435
    SCompressInfo info = {
1,026,699,386✔
436
        .dataType = TSDB_DATA_TYPE_BIGINT,
437
        .cmprAlg = statisBlk->cmprAlg,
1,026,714,915✔
438
        .compressedSize = statisBlk->size[i],
1,026,712,788✔
439
        .originalSize = statisBlk->numRec * sizeof(int64_t),
1,026,731,922✔
440
    };
441

442
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &info, &statisBlock->buffers[i], assist), &lino, _exit);
1,026,701,420✔
443
    br.offset += statisBlk->size[i];
1,026,790,150✔
444
  }
445

446
  if (statisBlk->numOfPKs > 0) {
205,383,645✔
447
    SValueColumnCompressInfo firstKeyInfos[TD_MAX_PK_COLS];
11,567,314✔
448
    SValueColumnCompressInfo lastKeyInfos[TD_MAX_PK_COLS];
11,567,314✔
449

450
    // decode compress info
451
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
23,134,628✔
452
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, &firstKeyInfos[i]), &lino, _exit);
11,566,655✔
453
    }
454

455
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
23,134,628✔
456
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, &lastKeyInfos[i]), &lino, _exit);
11,566,655✔
457
    }
458

459
    // decode value columns
460
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
23,131,992✔
461
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), firstKeyInfos + i, &statisBlock->firstKeyPKs[i], assist),
11,565,337✔
462
                      &lino, _exit);
463
      br.offset += (firstKeyInfos[i].dataCompressedSize + firstKeyInfos[i].offsetCompressedSize);
11,565,996✔
464
    }
465

466
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
23,130,674✔
467
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), &lastKeyInfos[i], &statisBlock->lastKeyPKs[i], assist), &lino,
11,565,996✔
468
                      _exit);
469
      br.offset += (lastKeyInfos[i].dataCompressedSize + lastKeyInfos[i].offsetCompressedSize);
11,567,314✔
470
    }
471
  }
472

473
  if (br.offset != buffer0->size) {
205,381,984✔
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:
205,387,664✔
481
  if (code) {
205,365,763✔
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;
205,365,763✔
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,
42,847,519✔
517
                                           int64_t *fileSize, TSttBlkArray *sttBlkArray, SBuffer *buffers,
518
                                           SVersionRange *range, SEncryptData *encryptData) {
519
  if (blockData->nRow == 0) return 0;
42,847,519✔
520

521
  int32_t code = 0;
42,870,348✔
522

523
  SSttBlk sttBlk[1] = {{
42,873,182✔
524
      .suid = blockData->suid,
42,863,489✔
525
      .minUid = blockData->uid ? blockData->uid : blockData->aUid[0],
42,857,810✔
526
      .maxUid = blockData->uid ? blockData->uid : blockData->aUid[blockData->nRow - 1],
42,882,461✔
527
      .minKey = blockData->aTSKEY[0],
42,878,690✔
528
      .maxKey = blockData->aTSKEY[0],
42,871,590✔
529
      .minVer = blockData->aVersion[0],
42,880,292✔
530
      .maxVer = blockData->aVersion[0],
42,877,304✔
531
      .nRow = blockData->nRow,
42,856,072✔
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);
42,887,274✔
542
  TAOS_CHECK_RETURN(tBlockDataCompress(blockData, info, buffers, buffers + 4));
42,874,402✔
543

544
  sttBlk->bInfo.offset = *fileSize;
42,839,558✔
545
  sttBlk->bInfo.szKey = buffers[0].size + buffers[1].size;
42,840,840✔
546
  sttBlk->bInfo.szBlock = buffers[2].size + buffers[3].size + sttBlk->bInfo.szKey;
42,868,298✔
547
  for (int i = 0; i < 4; i++) {
214,371,459✔
548
    if (buffers[i].size) {
171,485,838✔
549
      TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffers[i].data, buffers[i].size, encryptData));
171,292,911✔
550
      *fileSize += buffers[i].size;
171,298,316✔
551
    }
552
  }
553

554
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(sttBlkArray, sttBlk));
85,761,515✔
555

556
  tBlockDataClear(blockData);
42,875,894✔
557
  return 0;
42,886,764✔
558
}
559

560
static int32_t tsdbSttFileDoWriteBlockData(SSttFileWriter *writer) {
62,546,528✔
561
  if (writer->blockData->nRow == 0) return 0;
62,546,528✔
562

563
  int32_t code = 0;
42,878,298✔
564
  int32_t lino = 0;
42,878,298✔
565

566
  tb_uid_t         uid = writer->blockData->suid == 0 ? writer->blockData->uid : writer->blockData->suid;
42,881,634✔
567
  SColCompressInfo info = {.defaultCmprAlg = writer->config->cmprAlg, .pColCmpr = NULL};
42,886,421✔
568
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, uid, &(info.pColCmpr));
42,883,915✔
569

570
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
42,871,315✔
571

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

576
_exit:
42,854,732✔
577
  if (code) {
42,859,771✔
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);
42,859,771✔
582
  return code;
42,847,290✔
583
}
584

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

590
  int32_t code = 0;
18,651,607✔
591
  int32_t lino = 0;
18,651,607✔
592

593
  SBuffer *buffer0 = writer->buffers + 0;
18,653,100✔
594
  SBuffer *buffer1 = writer->buffers + 1;
18,655,171✔
595
  SBuffer *assist = writer->buffers + 2;
18,652,213✔
596

597
  STbStatisRecord record;
18,653,586✔
598
  STbStatisBlock *statisBlock = writer->staticBlock;
18,655,156✔
599
  SStatisBlk      statisBlk = {0};
18,655,812✔
600

601
  statisBlk.dp->offset = writer->file->size;
18,653,835✔
602
  statisBlk.dp->size = 0;
18,652,665✔
603
  statisBlk.numRec = statisBlock->numOfRecords;
18,652,665✔
604
  statisBlk.cmprAlg = writer->config->cmprAlg;
18,651,708✔
605
  statisBlk.numOfPKs = statisBlock->numOfPKs;
18,652,278✔
606

607
  code = tStatisBlockGet(statisBlock, 0, &record);
18,653,764✔
608
  TSDB_CHECK_CODE(code, lino, _exit);
18,656,532✔
609
  statisBlk.minTbid.suid = record.suid;
18,656,532✔
610
  statisBlk.minTbid.uid = record.uid;
18,656,532✔
611

612
  code = tStatisBlockGet(statisBlock, statisBlock->numOfRecords - 1, &record);
18,656,532✔
613
  TSDB_CHECK_CODE(code, lino, _exit);
18,654,643✔
614
  statisBlk.maxTbid.suid = record.suid;
18,654,643✔
615
  statisBlk.maxTbid.uid = record.uid;
18,654,643✔
616

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

619
  // compress each column
620
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlk.size); i++) {
111,931,686✔
621
    SCompressInfo info = {
93,273,124✔
622
        .dataType = TSDB_DATA_TYPE_BIGINT,
623
        .cmprAlg = statisBlk.cmprAlg,
93,272,914✔
624
        .originalSize = statisBlock->buffers[i].size,
93,272,914✔
625
    };
626

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

632
    statisBlk.size[i] = info.compressedSize;
93,289,354✔
633
    statisBlk.dp->size += info.compressedSize;
93,285,335✔
634
    writer->file->size += info.compressedSize;
93,285,335✔
635
  }
636

637
  // compress primary keys
638
  if (statisBlk.numOfPKs > 0) {
18,658,562✔
639
    SValueColumnCompressInfo compressInfo = {.cmprAlg = statisBlk.cmprAlg};
465,428✔
640

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

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

649
    for (int32_t i = 0; i < statisBlk.numOfPKs; i++) {
930,856✔
650
      TAOS_CHECK_GOTO(tValueColumnCompress(&statisBlock->lastKeyPKs[i], &compressInfo, buffer1, assist), &lino, _exit);
464,776✔
651
      TAOS_CHECK_GOTO(tValueColumnCompressInfoEncode(&compressInfo, buffer0), &lino, _exit);
465,428✔
652
    }
653

654
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, buffer0->data, buffer0->size, pEncryptData), &lino,
466,080✔
655
                    _exit);
656
    writer->file->size += buffer0->size;
465,428✔
657
    statisBlk.dp->size += buffer0->size;
464,776✔
658

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

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

667
  tStatisBlockClear(writer->staticBlock);
18,654,609✔
668

669
_exit:
18,661,950✔
670
  if (code) {
18,652,221✔
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,652,554✔
675
}
676

677
static int32_t tsdbSttFileDoWriteTombBlock(SSttFileWriter *writer) {
18,386,884✔
678
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) {
18,386,884✔
679
    return 0;
16,841,107✔
680
  }
681

682
  int32_t code = 0;
1,547,139✔
683
  int32_t lino = 0;
1,547,139✔
684

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

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

691
_exit:
1,547,139✔
692
  if (code) {
1,547,139✔
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,547,139✔
697
}
698

699
int32_t tsdbFileWriteSttBlk(STsdbFD *fd, const TSttBlkArray *sttBlkArray, SFDataPtr *ptr, int64_t *fileSize,
18,387,333✔
700
                            SEncryptData *encryptData) {
701
  ptr->size = TARRAY2_DATA_LEN(sttBlkArray);
18,387,333✔
702
  if (ptr->size > 0) {
18,387,818✔
703
    ptr->offset = *fileSize;
18,337,910✔
704

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

707
    *fileSize += ptr->size;
18,338,164✔
708
  }
709
  return 0;
18,388,809✔
710
}
711

712
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
18,385,504✔
713
  int32_t code = 0;
18,385,504✔
714
  int32_t lino;
18,385,084✔
715

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

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

722
_exit:
18,387,763✔
723
  if (code) {
18,387,763✔
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,387,763✔
728
}
729

730
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
18,386,145✔
731
  int32_t code = 0;
18,386,145✔
732
  int32_t lino;
18,385,725✔
733
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,386,683✔
734

735
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
18,381,948✔
736
  if (writer->footer->statisBlkPtr->size) {
18,387,166✔
737
    writer->footer->statisBlkPtr->offset = writer->file->size;
18,335,040✔
738
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
18,340,345✔
739
                                  writer->footer->statisBlkPtr->size, pEncryptData),
740
                    &lino, _exit);
741
    writer->file->size += writer->footer->statisBlkPtr->size;
18,339,647✔
742
  }
743

744
_exit:
18,388,144✔
745
  if (code) {
18,388,949✔
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,388,949✔
750
}
751

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

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

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

762
_exit:
18,385,936✔
763
  if (code) {
18,385,148✔
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,385,148✔
768
}
769

770
int32_t tsdbFileWriteSttFooter(STsdbFD *fd, const SSttFooter *footer, int64_t *fileSize, SEncryptData *encryptData) {
18,385,851✔
771
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
18,385,851✔
772
  *fileSize += sizeof(*footer);
18,387,513✔
773
  return 0;
18,385,253✔
774
}
775

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

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

782
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
18,322,144✔
783
  int32_t code = 0;
18,322,144✔
784
  int32_t lino = 0;
18,322,144✔
785
  STsdb  *tsdb = writer->config->tsdb;
18,328,853✔
786

787
  // set
788
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
18,329,319✔
789
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
18,332,606✔
790
  writer->buffers = writer->config->buffers;
18,361,394✔
791
  if (writer->buffers == NULL) {
18,355,206✔
792
    writer->buffers = writer->local;
×
793
  }
794

795
  // alloc disk id
796
  SDiskID diskId = {0};
18,348,423✔
797
  code = tsdbAllocateDisk(tsdb, tsdbFTypeLabel(TSDB_FTYPE_STT), writer->config->expLevel, &diskId);
18,353,778✔
798
  TSDB_CHECK_CODE(code, lino, _exit);
18,363,151✔
799

800
  writer->file[0] = (STFile){
18,356,774✔
801
      .type = TSDB_FTYPE_STT,
802
      .did = diskId,
803
      .fid = writer->config->fid,
18,363,151✔
804
      .cid = writer->config->cid,
18,378,249✔
805
      .size = 0,
806
      .minVer = VERSION_MAX,
807
      .maxVer = VERSION_MIN,
808
      .stt[0] =
809
          {
810
              .level = writer->config->level,
18,365,029✔
811
          },
812
  };
813

814
  // open file
815
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
18,361,981✔
816
  char    fname[TSDB_FILENAME_LEN];
18,361,561✔
817

818
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
18,342,219✔
819
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd, 0), &lino, _exit);
18,353,123✔
820

821
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
18,362,207✔
822
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
18,359,097✔
823

824
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, 0, hdr, sizeof(hdr), pEncryptData), &lino, _exit);
18,364,482✔
825
  writer->file->size += sizeof(hdr);
18,365,863✔
826

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

830
  writer->ctx->opened = true;
18,374,876✔
831

832
_exit:
18,376,726✔
833
  if (code) {
18,380,352✔
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,380,352✔
838
}
839

840
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
18,369,383✔
841
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
201,510,922✔
842
    tBufferDestroy(writer->local + i);
183,178,483✔
843
  }
844
  tDestroyTSchema(writer->skmRow->pTSchema);
18,332,439✔
845
  tDestroyTSchema(writer->skmTb->pTSchema);
18,368,648✔
846
  tTombBlockDestroy(writer->tombBlock);
18,377,504✔
847
  tStatisBlockDestroy(writer->staticBlock);
18,343,911✔
848
  tBlockDataDestroy(writer->blockData);
18,368,769✔
849
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
18,386,149✔
850
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
18,387,800✔
851
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
18,381,053✔
852
}
18,377,508✔
853

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

859
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
18,388,337✔
860
  int32_t lino;
18,387,917✔
861
  int32_t code;
862

863
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
18,386,874✔
864
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlock(writer), &lino, _exit);
18,387,519✔
865
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlock(writer), &lino, _exit);
18,385,324✔
866
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteSttBlk(writer), &lino, _exit);
18,385,192✔
867
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlk(writer), &lino, _exit);
18,386,145✔
868
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlk(writer), &lino, _exit);
18,380,575✔
869
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteFooter(writer), &lino, _exit);
18,384,709✔
870
  TAOS_CHECK_GOTO(tsdbSttFileDoUpdateHeader(writer), &lino, _exit);
18,386,703✔
871

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

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

876
  tsdbCloseFile(&writer->fd);
18,378,850✔
877

878
  STFileOp op = (STFileOp){
36,728,572✔
879
      .optype = TSDB_FOP_CREATE,
880
      .fid = writer->config->fid,
18,360,304✔
881
      .nf = writer->file[0],
882
  };
883
  tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
18,375,426✔
884

885
  TAOS_CHECK_GOTO(TARRAY2_APPEND(opArray, op), &lino, _exit);
36,697,756✔
886

887
_exit:
18,341,842✔
888
  if (code) {
18,342,100✔
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,342,100✔
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,106,070✔
904
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
19,106,070✔
905
  if (writer[0] == NULL) {
19,043,800✔
906
    return terrno;
×
907
  }
908

909
  writer[0]->config[0] = config[0];
19,047,536✔
910
  writer[0]->ctx->opened = false;
19,050,026✔
911
  return 0;
19,115,569✔
912
}
913

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

917
  int32_t code = 0;
19,141,165✔
918
  int32_t lino = 0;
19,141,165✔
919

920
  if (writer[0]->ctx->opened) {
19,139,921✔
921
    if (abort) {
18,381,976✔
922
      TAOS_CHECK_GOTO(tsdbSttFWriterCloseAbort(writer[0]), &lino, _exit);
×
923
    } else {
924
      TAOS_CHECK_GOTO(tsdbSttFWriterCloseCommit(writer[0], opArray), &lino, _exit);
18,381,976✔
925
    }
926
    tsdbSttFWriterDoClose(writer[0]);
18,336,902✔
927
  }
928
  taosMemoryFree(writer[0]);
19,124,710✔
929
  writer[0] = NULL;
19,119,907✔
930

931
_exit:
19,120,273✔
932
  if (code) {
19,125,583✔
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,131,602✔
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,312,824✔
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,112,431✔
949

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

952
    TABLEID id = {
24,082,554✔
953
        .suid = row->suid,
24,089,693✔
954
        .uid = row->suid ? 0 : row->uid,
24,096,540✔
955
    };
956
    TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0), &lino, _exit);
24,075,803✔
957
  }
958

959
  if (writer->ctx->tbid->uid != row->uid) {
2,147,483,647✔
960
    writer->ctx->tbid->suid = row->suid;
95,208,714✔
961
    writer->ctx->tbid->uid = row->uid;
95,224,362✔
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);
120,434✔
971
      continue;
120,434✔
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);
183,292,656✔
994
  } else {
995
    if (writer->blockData->nRow >= writer->config->maxRow) {
2,147,483,647✔
996
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
18,566,045✔
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) {
18,942,753✔
1012
  int32_t code = 0;
18,942,753✔
1013
  int32_t lino = 0;
18,942,753✔
1014

1015
  SRowInfo row[1];
18,942,880✔
1016
  row->suid = bdata->suid;
18,943,225✔
1017
  for (int32_t i = 0; i < bdata->nRow; i++) {
151,426,862✔
1018
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
132,483,637✔
1019
    row->row = tsdbRowFromBlockData(bdata, i);
132,483,165✔
1020

1021
    TAOS_CHECK_GOTO(tsdbSttFileWriteRow(writer, row), &lino, _exit);
132,483,165✔
1022
  }
1023

1024
_exit:
18,943,697✔
1025
  if (code) {
18,943,697✔
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;
18,943,697✔
1030
}
1031

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

1036
  if (!writer->ctx->opened) {
6,187,783✔
1037
    TAOS_CHECK_GOTO(tsdbSttFWriterDoOpen(writer), &lino, _exit);
48,297✔
1038
  } else {
1039
    if (writer->blockData->nRow > 0) {
6,139,486✔
1040
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
1,498,842✔
1041
    }
1042

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

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

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

1054
_exit:
6,187,783✔
1055
  if (code) {
6,187,783✔
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,187,783✔
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,187,783✔
1065
}
1066

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