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

taosdata / TDengine / #4924

09 Jan 2026 08:13AM UTC coverage: 65.354% (-0.02%) from 65.373%
#4924

push

travis-ci

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

33 of 56 new or added lines in 8 files covered. (58.93%)

3192 existing lines in 116 files now uncovered.

198218 of 303297 relevant lines covered (65.35%)

118995160.34 hits per line

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

89.5
/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) {
372,204,659✔
39
  int32_t code = 0;
372,204,659✔
40
  int32_t lino = 0;
372,204,659✔
41

42
  reader[0] = taosMemoryCalloc(1, sizeof(*reader[0]));
372,237,748✔
43
  if (reader[0] == NULL) {
371,897,684✔
44
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
45
  }
46

47
  reader[0]->config[0] = config[0];
371,904,902✔
48
  reader[0]->buffers = config->buffers;
371,938,955✔
49
  if (reader[0]->buffers == NULL) {
372,227,852✔
50
    reader[0]->buffers = reader[0]->local;
372,221,145✔
51
  }
52

53
  // open file
54
  if (fname) {
372,193,275✔
55
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, config->tsdb, TD_FILE_READ, &reader[0]->fd, 0), &lino, _exit);
372,193,275✔
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);
372,164,825✔
64
  if (offset < TSDB_FHDR_SIZE) {
372,078,797✔
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);
372,078,797✔
71

72
#if 1
73
  TAOS_CHECK_GOTO(
372,206,988✔
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:
371,986,771✔
95
  if (code) {
371,930,444✔
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;
371,930,444✔
101
}
102

103
void tsdbSttFileReaderClose(SSttFileReader **reader) {
372,158,059✔
104
  if (reader[0]) {
372,158,059✔
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);
372,150,427✔
109
    TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
372,031,827✔
110
    TARRAY2_DESTROY(reader[0]->statisBlkArray, NULL);
372,144,327✔
111
    TARRAY2_DESTROY(reader[0]->sttBlkArray, NULL);
372,116,601✔
112
    taosMemoryFree(reader[0]);
372,131,631✔
113
    reader[0] = NULL;
372,168,933✔
114
  }
115
}
372,048,447✔
116

117
// SSttFSegReader
118
int32_t tsdbSttFileReadStatisBlk(SSttFileReader *reader, const TStatisBlkArray **statisBlkArray) {
369,253,332✔
119
  if (!reader->ctx->statisBlkLoaded) {
369,253,332✔
120
    if (reader->footer->statisBlkPtr->size > 0) {
369,410,058✔
121
      if (reader->footer->statisBlkPtr->size % sizeof(SStatisBlk) != 0) {
369,274,132✔
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);
369,099,825✔
128
      void   *data = taosMemoryMalloc(reader->footer->statisBlkPtr->size);
369,225,539✔
129
      if (!data) {
369,207,293✔
130
        return terrno;
×
131
      }
132

133
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
369,207,293✔
134

135
      int32_t code = tsdbReadFile(reader->fd, reader->footer->statisBlkPtr->offset, data,
369,321,997✔
136
                                  reader->footer->statisBlkPtr->size, 0, pEncryptData);
137
      if (code) {
369,346,822✔
138
        taosMemoryFree(data);
×
139
        return code;
×
140
      }
141

142
      TARRAY2_INIT_EX(reader->statisBlkArray, size, size, data);
369,346,822✔
143
    } else {
144
      TARRAY2_INIT(reader->statisBlkArray);
135,112✔
145
    }
146

147
    reader->ctx->statisBlkLoaded = true;
369,526,863✔
148
  }
149

150
  statisBlkArray[0] = reader->statisBlkArray;
369,452,965✔
151
  return 0;
369,470,721✔
152
}
153

154
int32_t tsdbSttFileReadTombBlk(SSttFileReader *reader, const TTombBlkArray **tombBlkArray) {
372,037,255✔
155
  if (!reader->ctx->tombBlkLoaded) {
372,037,255✔
156
    if (reader->footer->tombBlkPtr->size > 0) {
372,130,412✔
157
      if (reader->footer->tombBlkPtr->size % sizeof(STombBlk) != 0) {
10,888,914✔
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);
10,888,914✔
164
      void   *data = taosMemoryMalloc(reader->footer->tombBlkPtr->size);
10,888,914✔
165
      if (!data) {
10,888,914✔
166
        return terrno;
×
167
      }
168

169
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
10,888,914✔
170

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

178
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
10,888,914✔
179
    } else {
180
      TARRAY2_INIT(reader->tombBlkArray);
361,125,107✔
181
    }
182

183
    reader->ctx->tombBlkLoaded = true;
372,160,821✔
184
  }
185

186
  tombBlkArray[0] = reader->tombBlkArray;
372,127,111✔
187
  return 0;
372,113,122✔
188
}
189

190
int32_t tsdbSttFileReadSttBlk(SSttFileReader *reader, const TSttBlkArray **sttBlkArray) {
371,936,019✔
191
  if (!reader->ctx->sttBlkLoaded) {
371,936,019✔
192
    if (reader->footer->sttBlkPtr->size > 0) {
372,103,032✔
193
      if (reader->footer->sttBlkPtr->size % sizeof(SSttBlk) != 0) {
371,946,923✔
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);
371,723,934✔
200
      void   *data = taosMemoryMalloc(reader->footer->sttBlkPtr->size);
371,872,843✔
201
      if (!data) {
371,788,290✔
202
        return terrno;
×
203
      }
204

205
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
371,788,290✔
206

207
      int32_t code = tsdbReadFile(reader->fd, reader->footer->sttBlkPtr->offset, data, reader->footer->sttBlkPtr->size,
371,971,991✔
208
                                  0, pEncryptData);
209
      if (code) {
372,017,245✔
210
        taosMemoryFree(data);
×
211
        return code;
×
212
      }
213

214
      TARRAY2_INIT_EX(reader->sttBlkArray, size, size, data);
372,017,245✔
215
    } else {
216
      TARRAY2_INIT(reader->sttBlkArray);
179,932✔
217
    }
218

219
    reader->ctx->sttBlkLoaded = true;
372,188,365✔
220
  }
221

222
  sttBlkArray[0] = reader->sttBlkArray;
372,200,628✔
223
  return 0;
372,213,732✔
224
}
225

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

230
  SBuffer *buffer0 = reader->buffers + 0;
14,950,803✔
231
  SBuffer *assist = reader->buffers + 1;
14,950,803✔
232

233
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
14,950,803✔
234

235
  // load data
236
  tBufferClear(buffer0);
237
  TAOS_CHECK_GOTO(
14,950,215✔
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);
14,950,198✔
242
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
14,950,803✔
243

244
_exit:
14,950,198✔
245
  if (code) {
14,949,610✔
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;
14,949,610✔
250
}
251

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

257
  SDiskDataHdr hdr;
172,741,599✔
258
  SBuffer     *buffer0 = reader->buffers + 0;
172,792,918✔
259
  SBuffer     *buffer1 = reader->buffers + 1;
172,784,984✔
260
  SBuffer     *assist = reader->buffers + 2;
172,831,635✔
261

262
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
172,886,716✔
263

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

269
  // decode header
270
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
172,878,776✔
271
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
172,825,119✔
272

273
  if (hdr.delimiter != TSDB_FILE_DLMT) {
172,660,517✔
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);
172,660,517✔
281
  bData->suid = hdr.suid;
172,721,482✔
282
  bData->uid = (sttBlk->suid == 0) ? sttBlk->minUid : 0;
172,726,830✔
283
  bData->nRow = hdr.nRow;
172,685,793✔
284

285
  // key part
286
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
172,699,936✔
287
  if (br.offset != buffer0->size) {
172,766,271✔
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;
172,758,943✔
295
  for (int i = 0; i < ncid; i++) {
208,660,933✔
296
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
162,906,504✔
297
      loadExtra = true;
127,038,584✔
298
      break;
127,038,584✔
299
    }
300
  }
301

302
  if (!loadExtra) {
172,793,013✔
303
    goto _exit;
45,761,510✔
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,
127,094,661✔
309
                                       pEncryptData),
310
                  &lino, _exit);
311

312
  // load each column
313
  SBlockCol blockCol = {
127,108,218✔
314
      .cid = 0,
315
  };
316
  br = BUFFER_READER_INITIALIZER(0, buffer0);
127,113,225✔
317
  for (int32_t i = 0; i < ncid; i++) {
589,728,766✔
318
    int16_t cid = cids[i];
462,496,100✔
319

320
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
462,519,494✔
321
      continue;
19,435,542✔
322
    }
323

324
    while (cid > blockCol.cid) {
1,358,749,646✔
325
      if (br.offset >= buffer0->size) {
926,026,510✔
326
        blockCol.cid = INT16_MAX;
10,600,035✔
327
        break;
10,600,035✔
328
      }
329

330
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
915,461,213✔
331
    }
332

333
    if (cid < blockCol.cid) {
443,323,171✔
334
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
49,992,233✔
335
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
49,990,544✔
336
      SBlockCol none = {
49,990,544✔
337
          .cid = cid,
338
          .type = tcol->type,
49,990,544✔
339
          .cflag = tcol->flags,
49,989,966✔
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);
49,991,655✔
348
    } else if (cid == blockCol.cid) {
393,330,938✔
349
      // load from file
350
      tBufferClear(buffer1);
351
      TAOS_CHECK_GOTO(
393,190,883✔
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);
393,242,536✔
358
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
393,245,361✔
359
    }
360
  }
361

362
_exit:
173,190,443✔
363
  if (code) {
172,839,253✔
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;
172,839,253✔
368
}
369

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

374
  SBuffer *buffer0 = reader->buffers + 0;
5,569,619✔
375
  SBuffer *assist = reader->buffers + 1;
5,569,619✔
376

377
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
5,569,619✔
378

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

384
  // decode
385
  int32_t       size = 0;
5,569,619✔
386
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
5,569,619✔
387
  tTombBlockClear(tombBlock);
5,569,619✔
388
  tombBlock->numOfRecords = tombBlk->numRec;
5,569,619✔
389
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); ++i) {
33,417,714✔
390
    SCompressInfo cinfo = {
27,848,095✔
391
        .cmprAlg = tombBlk->cmprAlg,
27,848,095✔
392
        .dataType = TSDB_DATA_TYPE_BIGINT,
393
        .originalSize = tombBlk->numRec * sizeof(int64_t),
27,848,095✔
394
        .compressedSize = tombBlk->size[i],
27,848,095✔
395
    };
396
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, tombBlock->buffers + i, assist), &lino, _exit);
27,848,095✔
397
    br.offset += tombBlk->size[i];
27,848,095✔
398
  }
399

400
  if (br.offset != tombBlk->dp->size) {
5,569,619✔
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:
5,569,619✔
407
  if (code) {
5,569,619✔
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;
5,569,619✔
412
}
413

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

418
  SBuffer *buffer0 = reader->buffers + 0;
250,994,780✔
419
  SBuffer *assist = reader->buffers + 1;
251,067,002✔
420

421
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
251,107,597✔
422

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

429
  // decode data
430
  tStatisBlockClear(statisBlock);
251,145,460✔
431
  statisBlock->numOfPKs = statisBlk->numOfPKs;
251,071,646✔
432
  statisBlock->numOfRecords = statisBlk->numRec;
251,120,892✔
433
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
251,120,046✔
434
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlock->buffers); ++i) {
1,506,930,242✔
435
    SCompressInfo info = {
1,255,733,491✔
436
        .dataType = TSDB_DATA_TYPE_BIGINT,
437
        .cmprAlg = statisBlk->cmprAlg,
1,255,716,303✔
438
        .compressedSize = statisBlk->size[i],
1,255,783,925✔
439
        .originalSize = statisBlk->numRec * sizeof(int64_t),
1,255,802,738✔
440
    };
441

442
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &info, &statisBlock->buffers[i], assist), &lino, _exit);
1,255,798,202✔
443
    br.offset += statisBlk->size[i];
1,255,779,103✔
444
  }
445

446
  if (statisBlk->numOfPKs > 0) {
251,196,751✔
447
    SValueColumnCompressInfo firstKeyInfos[TD_MAX_PK_COLS];
90,960,021✔
448
    SValueColumnCompressInfo lastKeyInfos[TD_MAX_PK_COLS];
90,958,865✔
449

450
    // decode compress info
451
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
181,915,968✔
452
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, &firstKeyInfos[i]), &lino, _exit);
90,958,287✔
453
    }
454

455
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
181,915,968✔
456
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, &lastKeyInfos[i]), &lino, _exit);
90,949,011✔
457
    }
458

459
    // decode value columns
460
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
181,909,032✔
461
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), firstKeyInfos + i, &statisBlock->firstKeyPKs[i], assist),
90,952,479✔
462
                      &lino, _exit);
463
      br.offset += (firstKeyInfos[i].dataCompressedSize + firstKeyInfos[i].offsetCompressedSize);
90,955,947✔
464
    }
465

466
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
181,906,086✔
467
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), &lastKeyInfos[i], &statisBlock->lastKeyPKs[i], assist), &lino,
90,955,397✔
468
                      _exit);
469
      br.offset += (lastKeyInfos[i].dataCompressedSize + lastKeyInfos[i].offsetCompressedSize);
90,960,021✔
470
    }
471
  }
472

473
  if (br.offset != buffer0->size) {
251,187,019✔
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:
251,173,333✔
481
  if (code) {
251,173,831✔
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;
251,173,831✔
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,920,495✔
517
                                           int64_t *fileSize, TSttBlkArray *sttBlkArray, SBuffer *buffers,
518
                                           SVersionRange *range, SEncryptData *encryptData) {
519
  if (blockData->nRow == 0) return 0;
43,920,495✔
520

521
  int32_t code = 0;
43,924,573✔
522

523
  SSttBlk sttBlk[1] = {{
43,927,331✔
524
      .suid = blockData->suid,
43,924,791✔
525
      .minUid = blockData->uid ? blockData->uid : blockData->aUid[0],
43,925,592✔
526
      .maxUid = blockData->uid ? blockData->uid : blockData->aUid[blockData->nRow - 1],
43,928,418✔
527
      .minKey = blockData->aTSKEY[0],
43,924,226✔
528
      .maxKey = blockData->aTSKEY[0],
43,927,968✔
529
      .minVer = blockData->aVersion[0],
43,928,278✔
530
      .maxVer = blockData->aVersion[0],
43,926,834✔
531
      .nRow = blockData->nRow,
43,927,491✔
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,926,249✔
542
  TAOS_CHECK_RETURN(tBlockDataCompress(blockData, info, buffers, buffers + 4));
43,927,109✔
543

544
  sttBlk->bInfo.offset = *fileSize;
43,924,471✔
545
  sttBlk->bInfo.szKey = buffers[0].size + buffers[1].size;
43,923,383✔
546
  sttBlk->bInfo.szBlock = buffers[2].size + buffers[3].size + sttBlk->bInfo.szKey;
43,925,210✔
547
  for (int i = 0; i < 4; i++) {
219,614,133✔
548
    if (buffers[i].size) {
175,685,203✔
549
      TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffers[i].data, buffers[i].size, encryptData));
175,474,093✔
550
      *fileSize += buffers[i].size;
175,480,858✔
551
    }
552
  }
553

554
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(sttBlkArray, sttBlk));
87,852,197✔
555

556
  tBlockDataClear(blockData);
43,923,267✔
557
  return 0;
43,926,608✔
558
}
559

560
static int32_t tsdbSttFileDoWriteBlockData(SSttFileWriter *writer) {
61,440,483✔
561
  if (writer->blockData->nRow == 0) return 0;
61,440,483✔
562

563
  int32_t code = 0;
43,927,784✔
564
  int32_t lino = 0;
43,927,784✔
565

566
  tb_uid_t         uid = writer->blockData->suid == 0 ? writer->blockData->uid : writer->blockData->suid;
43,925,858✔
567
  SColCompressInfo info = {.defaultCmprAlg = writer->config->cmprAlg, .pColCmpr = NULL};
43,927,171✔
568
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, uid, &(info.pColCmpr));
43,929,331✔
569

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

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

576
_exit:
43,924,256✔
577
  if (code) {
43,925,072✔
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,925,072✔
582
  return code;
43,925,781✔
583
}
584

585
static int32_t tsdbSttFileDoWriteStatisBlock(SSttFileWriter *writer) {
17,785,462✔
586
  if (writer->staticBlock->numOfRecords == 0) {
17,785,462✔
587
    return 0;
1,254,475✔
588
  }
589

590
  int32_t code = 0;
16,534,079✔
591
  int32_t lino = 0;
16,534,079✔
592

593
  SBuffer *buffer0 = writer->buffers + 0;
16,534,644✔
594
  SBuffer *buffer1 = writer->buffers + 1;
16,532,784✔
595
  SBuffer *assist = writer->buffers + 2;
16,534,312✔
596

597
  STbStatisRecord record;
16,534,656✔
598
  STbStatisBlock *statisBlock = writer->staticBlock;
16,534,682✔
599
  SStatisBlk      statisBlk = {0};
16,535,498✔
600

601
  statisBlk.dp->offset = writer->file->size;
16,532,982✔
602
  statisBlk.dp->size = 0;
16,533,378✔
603
  statisBlk.numRec = statisBlock->numOfRecords;
16,533,378✔
604
  statisBlk.cmprAlg = writer->config->cmprAlg;
16,534,033✔
605
  statisBlk.numOfPKs = statisBlock->numOfPKs;
16,535,761✔
606

607
  code = tStatisBlockGet(statisBlock, 0, &record);
16,533,428✔
608
  TSDB_CHECK_CODE(code, lino, _exit);
16,532,173✔
609
  statisBlk.minTbid.suid = record.suid;
16,532,173✔
610
  statisBlk.minTbid.uid = record.uid;
16,532,173✔
611

612
  code = tStatisBlockGet(statisBlock, statisBlock->numOfRecords - 1, &record);
16,532,173✔
613
  TSDB_CHECK_CODE(code, lino, _exit);
16,535,349✔
614
  statisBlk.maxTbid.suid = record.suid;
16,535,349✔
615
  statisBlk.maxTbid.uid = record.uid;
16,535,349✔
616

617
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
16,535,349✔
618

619
  // compress each column
620
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlk.size); i++) {
99,204,748✔
621
    SCompressInfo info = {
82,668,171✔
622
        .dataType = TSDB_DATA_TYPE_BIGINT,
623
        .cmprAlg = statisBlk.cmprAlg,
82,663,767✔
624
        .originalSize = statisBlock->buffers[i].size,
82,663,767✔
625
    };
626

627
    tBufferClear(buffer0);
628
    TAOS_CHECK_GOTO(tCompressDataToBuffer(statisBlock->buffers[i].data, &info, buffer0, assist), &lino, _exit);
82,666,985✔
629
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, buffer0->data, info.compressedSize, pEncryptData),
82,667,810✔
630
                    &lino, _exit);
631

632
    statisBlk.size[i] = info.compressedSize;
82,677,196✔
633
    statisBlk.dp->size += info.compressedSize;
82,672,712✔
634
    writer->file->size += info.compressedSize;
82,672,712✔
635
  }
636

637
  // compress primary keys
638
  if (statisBlk.numOfPKs > 0) {
16,536,577✔
639
    SValueColumnCompressInfo compressInfo = {.cmprAlg = statisBlk.cmprAlg};
284,041✔
640

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

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

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

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

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

665
  TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(writer->statisBlkArray, &statisBlk), &lino, _exit);
33,072,336✔
666

667
  tStatisBlockClear(writer->staticBlock);
16,535,759✔
668

669
_exit:
16,535,008✔
670
  if (code) {
16,535,278✔
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;
16,531,649✔
675
}
676

677
static int32_t tsdbSttFileDoWriteTombBlock(SSttFileWriter *writer) {
16,312,576✔
678
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) {
16,312,576✔
679
    return 0;
14,880,411✔
680
  }
681

682
  int32_t code = 0;
1,436,027✔
683
  int32_t lino = 0;
1,436,027✔
684

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

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

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

699
int32_t tsdbFileWriteSttBlk(STsdbFD *fd, const TSttBlkArray *sttBlkArray, SFDataPtr *ptr, int64_t *fileSize,
16,313,078✔
700
                            SEncryptData *encryptData) {
701
  ptr->size = TARRAY2_DATA_LEN(sttBlkArray);
16,313,078✔
702
  if (ptr->size > 0) {
16,314,357✔
703
    ptr->offset = *fileSize;
16,268,793✔
704

705
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(sttBlkArray), ptr->size, encryptData));
16,265,809✔
706

707
    *fileSize += ptr->size;
16,267,493✔
708
  }
709
  return 0;
16,315,304✔
710
}
711

712
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
16,311,314✔
713
  int32_t code = 0;
16,311,314✔
714
  int32_t lino;
16,310,918✔
715

716
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
16,313,503✔
717

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

722
_exit:
16,314,694✔
723
  if (code) {
16,311,463✔
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;
16,311,463✔
728
}
729

730
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
16,310,391✔
731
  int32_t code = 0;
16,310,391✔
732
  int32_t lino;
16,309,995✔
733
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
16,311,111✔
734

735
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
16,314,117✔
736
  if (writer->footer->statisBlkPtr->size) {
16,313,769✔
737
    writer->footer->statisBlkPtr->offset = writer->file->size;
16,265,232✔
738
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
16,266,747✔
739
                                  writer->footer->statisBlkPtr->size, pEncryptData),
740
                    &lino, _exit);
741
    writer->file->size += writer->footer->statisBlkPtr->size;
16,269,663✔
742
  }
743

744
_exit:
16,312,685✔
745
  if (code) {
16,313,081✔
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;
16,313,081✔
750
}
751

752
static int32_t tsdbSttFileDoWriteTombBlk(SSttFileWriter *writer) {
16,309,717✔
753
  int32_t code = 0;
16,309,717✔
754
  int32_t lino = 0;
16,309,717✔
755

756
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
16,312,692✔
757

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

762
_exit:
16,315,086✔
763
  if (code) {
16,315,456✔
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;
16,315,456✔
768
}
769

770
int32_t tsdbFileWriteSttFooter(STsdbFD *fd, const SSttFooter *footer, int64_t *fileSize, SEncryptData *encryptData) {
16,313,026✔
771
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
16,313,026✔
772
  *fileSize += sizeof(*footer);
16,313,181✔
773
  return 0;
16,314,496✔
774
}
775

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

779
  return tsdbFileWriteSttFooter(writer->fd, writer->footer, &writer->file->size, pEncryptData);
16,309,108✔
780
}
781

782
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
16,294,261✔
783
  int32_t code = 0;
16,294,261✔
784
  int32_t lino = 0;
16,294,261✔
785
  STsdb  *tsdb = writer->config->tsdb;
16,299,092✔
786

787
  // set
788
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
16,299,477✔
789
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
16,296,976✔
790
  writer->buffers = writer->config->buffers;
16,298,645✔
791
  if (writer->buffers == NULL) {
16,300,145✔
792
    writer->buffers = writer->local;
×
793
  }
794

795
  // alloc disk id
796
  SDiskID diskId = {0};
16,310,586✔
797
  code = tsdbAllocateDisk(tsdb, tsdbFTypeLabel(TSDB_FTYPE_STT), writer->config->expLevel, &diskId);
16,295,980✔
798
  TSDB_CHECK_CODE(code, lino, _exit);
16,302,564✔
799

800
  writer->file[0] = (STFile){
16,298,181✔
801
      .type = TSDB_FTYPE_STT,
802
      .did = diskId,
803
      .fid = writer->config->fid,
16,302,564✔
804
      .cid = writer->config->cid,
16,308,710✔
805
      .size = 0,
806
      .minVer = VERSION_MAX,
807
      .maxVer = VERSION_MIN,
808
      .stt[0] =
809
          {
810
              .level = writer->config->level,
16,297,064✔
811
          },
812
  };
813

814
  // open file
815
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
16,297,423✔
816
  char    fname[TSDB_FILENAME_LEN];
16,297,027✔
817

818
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
16,301,234✔
819
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd, 0), &lino, _exit);
16,297,359✔
820

821
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
16,301,466✔
822
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
16,305,493✔
823

824
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, 0, hdr, sizeof(hdr), pEncryptData), &lino, _exit);
16,304,504✔
825
  writer->file->size += sizeof(hdr);
16,293,958✔
826

827
  // range
828
  writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
16,307,998✔
829

830
  writer->ctx->opened = true;
16,309,559✔
831

832
_exit:
16,308,625✔
833
  if (code) {
16,303,913✔
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;
16,303,913✔
838
}
839

840
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
16,302,671✔
841
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
179,021,267✔
842
    tBufferDestroy(writer->local + i);
162,731,997✔
843
  }
844
  tDestroyTSchema(writer->skmRow->pTSchema);
16,289,270✔
845
  tDestroyTSchema(writer->skmTb->pTSchema);
16,299,072✔
846
  tTombBlockDestroy(writer->tombBlock);
16,302,174✔
847
  tStatisBlockDestroy(writer->staticBlock);
16,290,063✔
848
  tBlockDataDestroy(writer->blockData);
16,309,326✔
849
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
16,314,271✔
850
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
16,308,338✔
851
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
16,311,759✔
852
}
16,314,721✔
853

854
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
16,310,123✔
855
  // TODO
856
  return 0;
16,310,123✔
857
}
858

859
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
16,313,285✔
860
  int32_t lino;
16,312,889✔
861
  int32_t code;
862

863
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
16,314,025✔
864
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlock(writer), &lino, _exit);
16,311,765✔
865
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlock(writer), &lino, _exit);
16,313,196✔
866
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteSttBlk(writer), &lino, _exit);
16,314,128✔
867
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlk(writer), &lino, _exit);
16,311,413✔
868
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlk(writer), &lino, _exit);
16,310,904✔
869
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteFooter(writer), &lino, _exit);
16,312,547✔
870
  TAOS_CHECK_GOTO(tsdbSttFileDoUpdateHeader(writer), &lino, _exit);
16,314,015✔
871

872
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
16,311,397✔
873

874
  TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd, pEncryptData), &lino, _exit);
16,311,828✔
875

876
  tsdbCloseFile(&writer->fd);
16,309,756✔
877

878
  STFileOp op = (STFileOp){
32,600,901✔
879
      .optype = TSDB_FOP_CREATE,
880
      .fid = writer->config->fid,
16,298,350✔
881
      .nf = writer->file[0],
882
  };
883
  tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
16,309,361✔
884

885
  TAOS_CHECK_GOTO(TARRAY2_APPEND(opArray, op), &lino, _exit);
32,588,404✔
886

887
_exit:
16,287,142✔
888
  if (code) {
16,288,620✔
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;
16,288,620✔
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) {
16,970,075✔
904
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
16,970,075✔
905
  if (writer[0] == NULL) {
16,957,286✔
906
    return terrno;
×
907
  }
908

909
  writer[0]->config[0] = config[0];
16,957,105✔
910
  writer[0]->ctx->opened = false;
16,960,449✔
911
  return 0;
16,985,893✔
912
}
913

914
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
16,988,404✔
915
  int32_t code = 0;
16,988,404✔
916
  int32_t lino = 0;
16,988,404✔
917

918
  if (writer[0]->ctx->opened) {
16,989,220✔
919
    if (abort) {
16,313,285✔
920
      TAOS_CHECK_GOTO(tsdbSttFWriterCloseAbort(writer[0]), &lino, _exit);
×
921
    } else {
922
      TAOS_CHECK_GOTO(tsdbSttFWriterCloseCommit(writer[0], opArray), &lino, _exit);
16,313,285✔
923
    }
924
    tsdbSttFWriterDoClose(writer[0]);
16,289,471✔
925
  }
926
  taosMemoryFree(writer[0]);
16,987,597✔
927
  writer[0] = NULL;
16,983,546✔
928

929
_exit:
16,985,201✔
930
  if (code) {
16,985,546✔
931
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer[0]->config->tsdb->pVnode), __func__, __FILE__, lino,
×
932
              tstrerror(code));
933
  }
934
  return code;
16,985,546✔
935
}
936

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

941
  if (!writer->ctx->opened) {
2,147,483,647✔
942
    TAOS_CHECK_GOTO(tsdbSttFWriterDoOpen(writer), &lino, _exit);
16,253,461✔
943
  }
944

945
  if (!TABLE_SAME_SCHEMA(row->suid, row->uid, writer->ctx->tbid->suid, writer->ctx->tbid->uid)) {
2,147,483,647✔
946
    TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
28,271,597✔
947

948
    TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, (TABLEID *)row, writer->config->skmTb), &lino, _exit);
28,268,545✔
949

950
    TABLEID id = {
28,264,692✔
951
        .suid = row->suid,
28,267,577✔
952
        .uid = row->suid ? 0 : row->uid,
28,272,166✔
953
    };
954
    TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0), &lino, _exit);
28,257,181✔
955
  }
956

957
  if (writer->ctx->tbid->uid != row->uid) {
2,147,483,647✔
958
    writer->ctx->tbid->suid = row->suid;
92,723,576✔
959
    writer->ctx->tbid->uid = row->uid;
92,734,352✔
960
  }
961

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

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

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

982
  // row to col conversion
983
  if (key.version <= writer->config->compactVersion                                //
2,147,483,647✔
984
      && writer->blockData->nRow > 0                                               //
2,147,483,647✔
985
      && (writer->blockData->uid                                                   //
2,147,483,647✔
986
              ? writer->blockData->uid                                             //
987
              : writer->blockData->aUid[writer->blockData->nRow - 1]) == row->uid  //
2,147,483,647✔
988
      && tsdbRowCompareWithoutVersion(&row->row,
2,147,483,647✔
989
                                      &tsdbRowFromBlockData(writer->blockData, writer->blockData->nRow - 1)) == 0  //
2,147,483,647✔
990
  ) {
991
    TAOS_CHECK_GOTO(tBlockDataUpdateRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema), &lino, _exit);
68,387,978✔
992
  } else {
993
    if (writer->blockData->nRow >= writer->config->maxRow) {
2,147,483,647✔
994
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
15,473,723✔
995
    }
996

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

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

1009
int32_t tsdbSttFileWriteBlockData(SSttFileWriter *writer, SBlockData *bdata) {
17,591,220✔
1010
  int32_t code = 0;
17,591,220✔
1011
  int32_t lino = 0;
17,591,220✔
1012

1013
  SRowInfo row[1];
17,590,860✔
1014
  row->suid = bdata->suid;
17,591,666✔
1015
  for (int32_t i = 0; i < bdata->nRow; i++) {
140,083,168✔
1016
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
122,492,631✔
1017
    row->row = tsdbRowFromBlockData(bdata, i);
122,494,861✔
1018

1019
    TAOS_CHECK_GOTO(tsdbSttFileWriteRow(writer, row), &lino, _exit);
122,494,861✔
1020
  }
1021

1022
_exit:
17,591,666✔
1023
  if (code) {
17,591,666✔
1024
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1025
              tstrerror(code));
1026
  }
1027
  return code;
17,591,666✔
1028
}
1029

1030
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
5,773,309✔
1031
  int32_t code;
1032
  int32_t lino;
5,773,309✔
1033

1034
  if (!writer->ctx->opened) {
5,773,309✔
1035
    TAOS_CHECK_GOTO(tsdbSttFWriterDoOpen(writer), &lino, _exit);
44,820✔
1036
  } else {
1037
    if (writer->blockData->nRow > 0) {
5,728,489✔
1038
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
1,391,207✔
1039
    }
1040

1041
    if (STATIS_BLOCK_SIZE(writer->staticBlock) > 0) {
5,728,489✔
1042
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlock(writer), &lino, _exit);
1,391,207✔
1043
    }
1044
  }
1045

1046
  TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
5,773,309✔
1047

1048
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
5,773,309✔
1049
    TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlock(writer), &lino, _exit);
×
1050
  }
1051

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

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