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

taosdata / TDengine / #4877

11 Dec 2025 02:43AM UTC coverage: 64.586% (-0.05%) from 64.632%
#4877

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

673 existing lines in 130 files now uncovered.

163673 of 253417 relevant lines covered (64.59%)

105540806.95 hits per line

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

89.69
/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) {
201,097,116✔
39
  int32_t code = 0;
201,097,116✔
40
  int32_t lino = 0;
201,097,116✔
41

42
  reader[0] = taosMemoryCalloc(1, sizeof(*reader[0]));
201,125,324✔
43
  if (reader[0] == NULL) {
200,931,364✔
44
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
45
  }
46

47
  reader[0]->config[0] = config[0];
200,936,465✔
48
  reader[0]->buffers = config->buffers;
200,961,284✔
49
  if (reader[0]->buffers == NULL) {
201,096,432✔
50
    reader[0]->buffers = reader[0]->local;
201,114,083✔
51
  }
52

53
  // open file
54
  if (fname) {
201,093,340✔
55
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, config->tsdb, TD_FILE_READ, &reader[0]->fd, 0), &lino, _exit);
201,093,340✔
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);
201,099,660✔
64
  if (offset < TSDB_FHDR_SIZE) {
201,042,414✔
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);
201,042,414✔
71

72
#if 1
73
  TAOS_CHECK_GOTO(
201,117,707✔
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:
200,986,191✔
95
  if (code) {
200,961,813✔
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;
200,961,813✔
101
}
102

103
void tsdbSttFileReaderClose(SSttFileReader **reader) {
201,073,215✔
104
  if (reader[0]) {
201,073,215✔
105
    for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
2,147,483,647✔
106
      tBufferDestroy(reader[0]->local + i);
2,010,999,906✔
107
    }
108
    tsdbCloseFile(&reader[0]->fd);
201,073,981✔
109
    TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
201,033,050✔
110
    TARRAY2_DESTROY(reader[0]->statisBlkArray, NULL);
201,071,133✔
111
    TARRAY2_DESTROY(reader[0]->sttBlkArray, NULL);
201,077,683✔
112
    taosMemoryFree(reader[0]);
201,064,068✔
113
    reader[0] = NULL;
201,093,017✔
114
  }
115
}
201,012,869✔
116

117
// SSttFSegReader
118
int32_t tsdbSttFileReadStatisBlk(SSttFileReader *reader, const TStatisBlkArray **statisBlkArray) {
198,102,761✔
119
  if (!reader->ctx->statisBlkLoaded) {
198,102,761✔
120
    if (reader->footer->statisBlkPtr->size > 0) {
198,166,321✔
121
      if (reader->footer->statisBlkPtr->size % sizeof(SStatisBlk) != 0) {
198,041,240✔
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);
197,925,116✔
128
      void   *data = taosMemoryMalloc(reader->footer->statisBlkPtr->size);
198,004,509✔
129
      if (!data) {
197,940,805✔
130
        return terrno;
×
131
      }
132

133
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
197,940,805✔
134

135
      int32_t code = tsdbReadFile(reader->fd, reader->footer->statisBlkPtr->offset, data,
198,054,162✔
136
                                  reader->footer->statisBlkPtr->size, 0, pEncryptData);
137
      if (code) {
198,061,395✔
138
        taosMemoryFree(data);
×
139
        return code;
×
140
      }
141

142
      TARRAY2_INIT_EX(reader->statisBlkArray, size, size, data);
198,061,395✔
143
    } else {
144
      TARRAY2_INIT(reader->statisBlkArray);
151,520✔
145
    }
146

147
    reader->ctx->statisBlkLoaded = true;
198,254,440✔
148
  }
149

150
  statisBlkArray[0] = reader->statisBlkArray;
198,234,590✔
151
  return 0;
198,213,276✔
152
}
153

154
int32_t tsdbSttFileReadTombBlk(SSttFileReader *reader, const TTombBlkArray **tombBlkArray) {
201,030,285✔
155
  if (!reader->ctx->tombBlkLoaded) {
201,030,285✔
156
    if (reader->footer->tombBlkPtr->size > 0) {
201,079,042✔
157
      if (reader->footer->tombBlkPtr->size % sizeof(STombBlk) != 0) {
11,950,707✔
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,950,707✔
164
      void   *data = taosMemoryMalloc(reader->footer->tombBlkPtr->size);
11,950,707✔
165
      if (!data) {
11,950,707✔
166
        return terrno;
×
167
      }
168

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

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

178
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
11,950,707✔
179
    } else {
180
      TARRAY2_INIT(reader->tombBlkArray);
189,033,774✔
181
    }
182

183
    reader->ctx->tombBlkLoaded = true;
201,048,350✔
184
  }
185

186
  tombBlkArray[0] = reader->tombBlkArray;
201,064,579✔
187
  return 0;
201,077,660✔
188
}
189

190
int32_t tsdbSttFileReadSttBlk(SSttFileReader *reader, const TSttBlkArray **sttBlkArray) {
201,000,729✔
191
  if (!reader->ctx->sttBlkLoaded) {
201,000,729✔
192
    if (reader->footer->sttBlkPtr->size > 0) {
201,059,937✔
193
      if (reader->footer->sttBlkPtr->size % sizeof(SSttBlk) != 0) {
200,861,946✔
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);
200,727,797✔
200
      void   *data = taosMemoryMalloc(reader->footer->sttBlkPtr->size);
200,840,786✔
201
      if (!data) {
200,773,236✔
202
        return terrno;
×
203
      }
204

205
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
200,773,236✔
206

207
      int32_t code = tsdbReadFile(reader->fd, reader->footer->sttBlkPtr->offset, data, reader->footer->sttBlkPtr->size,
200,879,198✔
208
                                  0, pEncryptData);
209
      if (code) {
200,872,127✔
UNCOV
210
        taosMemoryFree(data);
×
211
        return code;
×
212
      }
213

214
      TARRAY2_INIT_EX(reader->sttBlkArray, size, size, data);
200,872,127✔
215
    } else {
216
      TARRAY2_INIT(reader->sttBlkArray);
200,457✔
217
    }
218

219
    reader->ctx->sttBlkLoaded = true;
201,059,288✔
220
  }
221

222
  sttBlkArray[0] = reader->sttBlkArray;
201,109,895✔
223
  return 0;
201,090,261✔
224
}
225

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

230
  SBuffer *buffer0 = reader->buffers + 0;
19,601,414✔
231
  SBuffer *assist = reader->buffers + 1;
19,601,414✔
232

233
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
19,600,115✔
234

235
  // load data
236
  tBufferClear(buffer0);
237
  TAOS_CHECK_GOTO(
19,601,253✔
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);
19,601,253✔
242
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
19,601,253✔
243

244
_exit:
19,600,112✔
245
  if (code) {
19,600,112✔
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;
19,600,112✔
250
}
251

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

257
  SDiskDataHdr hdr;
127,284,816✔
258
  SBuffer     *buffer0 = reader->buffers + 0;
127,294,990✔
259
  SBuffer     *buffer1 = reader->buffers + 1;
127,319,310✔
260
  SBuffer     *assist = reader->buffers + 2;
127,361,609✔
261

262
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
127,398,572✔
263

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

269
  // decode header
270
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
127,378,504✔
271
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
127,334,504✔
272

273
  if (hdr.delimiter != TSDB_FILE_DLMT) {
127,200,010✔
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);
127,200,010✔
281
  bData->suid = hdr.suid;
127,255,757✔
282
  bData->uid = (sttBlk->suid == 0) ? sttBlk->minUid : 0;
127,252,589✔
283
  bData->nRow = hdr.nRow;
127,235,832✔
284

285
  // key part
286
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
127,245,701✔
287
  if (br.offset != buffer0->size) {
127,277,886✔
288
    tsdbError("vgId:%d %s failed at %s:%d since key part size mismatch, expected:%u, actual:%u, fname:%s",
20,566✔
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;
127,281,162✔
295
  for (int i = 0; i < ncid; i++) {
133,599,368✔
296
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
115,100,725✔
297
      loadExtra = true;
108,821,061✔
298
      break;
108,821,061✔
299
    }
300
  }
301

302
  if (!loadExtra) {
127,319,704✔
303
    goto _exit;
18,499,297✔
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,
108,833,005✔
309
                                       pEncryptData),
310
                  &lino, _exit);
311

312
  // load each column
313
  SBlockCol blockCol = {
108,852,107✔
314
      .cid = 0,
315
  };
316
  br = BUFFER_READER_INITIALIZER(0, buffer0);
108,875,263✔
317
  for (int32_t i = 0; i < ncid; i++) {
474,759,720✔
318
    int16_t cid = cids[i];
365,748,468✔
319

320
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
365,810,892✔
321
      continue;
4,407,749✔
322
    }
323

324
    while (cid > blockCol.cid) {
1,138,466,677✔
325
      if (br.offset >= buffer0->size) {
778,863,976✔
326
        blockCol.cid = INT16_MAX;
2,033,872✔
327
        break;
2,033,872✔
328
      }
329

330
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
776,863,578✔
331
    }
332

333
    if (cid < blockCol.cid) {
361,636,573✔
334
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
9,093,810✔
335
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
9,091,938✔
336
      SBlockCol none = {
9,091,938✔
337
          .cid = cid,
338
          .type = tcol->type,
9,092,226✔
339
          .cflag = tcol->flags,
9,091,794✔
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);
9,091,650✔
348
    } else if (cid == blockCol.cid) {
352,542,763✔
349
      // load from file
350
      tBufferClear(buffer1);
351
      TAOS_CHECK_GOTO(
352,404,929✔
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);
352,443,013✔
358
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
352,445,083✔
359
    }
360
  }
361

362
_exit:
127,704,179✔
363
  if (code) {
127,367,125✔
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;
127,367,125✔
368
}
369

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

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

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

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

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

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

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

418
  SBuffer *buffer0 = reader->buffers + 0;
152,673,844✔
419
  SBuffer *assist = reader->buffers + 1;
152,677,959✔
420

421
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
152,673,243✔
422

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

429
  // decode data
430
  tStatisBlockClear(statisBlock);
152,743,275✔
431
  statisBlock->numOfPKs = statisBlk->numOfPKs;
152,697,829✔
432
  statisBlock->numOfRecords = statisBlk->numRec;
152,741,764✔
433
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
152,735,843✔
434
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlock->buffers); ++i) {
916,602,309✔
435
    SCompressInfo info = {
763,804,640✔
436
        .dataType = TSDB_DATA_TYPE_BIGINT,
437
        .cmprAlg = statisBlk->cmprAlg,
763,780,864✔
438
        .compressedSize = statisBlk->size[i],
763,851,735✔
439
        .originalSize = statisBlk->numRec * sizeof(int64_t),
763,861,373✔
440
    };
441

442
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &info, &statisBlock->buffers[i], assist), &lino, _exit);
763,869,745✔
443
    br.offset += statisBlk->size[i];
763,859,458✔
444
  }
445

446
  if (statisBlk->numOfPKs > 0) {
152,797,669✔
447
    SValueColumnCompressInfo firstKeyInfos[TD_MAX_PK_COLS];
15,651,617✔
448
    SValueColumnCompressInfo lastKeyInfos[TD_MAX_PK_COLS];
15,651,617✔
449

450
    // decode compress info
451
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
31,301,496✔
452
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, &firstKeyInfos[i]), &lino, _exit);
15,650,311✔
453
    }
454

455
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
31,301,640✔
456
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, &lastKeyInfos[i]), &lino, _exit);
15,646,845✔
457
    }
458

459
    // decode value columns
460
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
31,297,886✔
461
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), firstKeyInfos + i, &statisBlock->firstKeyPKs[i], assist),
15,650,244✔
462
                      &lino, _exit);
463
      br.offset += (firstKeyInfos[i].dataCompressedSize + firstKeyInfos[i].offsetCompressedSize);
15,649,591✔
464
    }
465

466
    for (int32_t i = 0; i < statisBlk->numOfPKs; i++) {
31,298,097✔
467
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), &lastKeyInfos[i], &statisBlock->lastKeyPKs[i], assist), &lino,
15,648,362✔
468
                      _exit);
469
      br.offset += (lastKeyInfos[i].dataCompressedSize + lastKeyInfos[i].offsetCompressedSize);
15,651,617✔
470
    }
471
  }
472

473
  if (br.offset != buffer0->size) {
152,791,723✔
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:
152,769,149✔
481
  if (code) {
152,778,585✔
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;
152,778,674✔
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,
38,300,433✔
517
                                           int64_t *fileSize, TSttBlkArray *sttBlkArray, SBuffer *buffers,
518
                                           SVersionRange *range, SEncryptData *encryptData) {
519
  if (blockData->nRow == 0) return 0;
38,300,433✔
520

521
  int32_t code = 0;
38,302,890✔
522

523
  SSttBlk sttBlk[1] = {{
38,339,074✔
524
      .suid = blockData->suid,
38,304,173✔
525
      .minUid = blockData->uid ? blockData->uid : blockData->aUid[0],
38,299,584✔
526
      .maxUid = blockData->uid ? blockData->uid : blockData->aUid[blockData->nRow - 1],
38,306,579✔
527
      .minKey = blockData->aTSKEY[0],
38,302,307✔
528
      .maxKey = blockData->aTSKEY[0],
38,306,084✔
529
      .minVer = blockData->aVersion[0],
38,305,755✔
530
      .maxVer = blockData->aVersion[0],
38,305,896✔
531
      .nRow = blockData->nRow,
38,303,270✔
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);
38,307,332✔
542
  TAOS_CHECK_RETURN(tBlockDataCompress(blockData, info, buffers, buffers + 4));
38,303,765✔
543

544
  sttBlk->bInfo.offset = *fileSize;
38,300,938✔
545
  sttBlk->bInfo.szKey = buffers[0].size + buffers[1].size;
38,301,345✔
546
  sttBlk->bInfo.szBlock = buffers[2].size + buffers[3].size + sttBlk->bInfo.szKey;
38,297,877✔
547
  for (int i = 0; i < 4; i++) {
191,522,726✔
548
    if (buffers[i].size) {
153,212,649✔
549
      TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffers[i].data, buffers[i].size, encryptData));
152,972,454✔
550
      *fileSize += buffers[i].size;
152,971,530✔
551
    }
552
  }
553

554
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(sttBlkArray, sttBlk));
76,616,920✔
555

556
  tBlockDataClear(blockData);
38,306,843✔
557
  return 0;
38,302,528✔
558
}
559

560
static int32_t tsdbSttFileDoWriteBlockData(SSttFileWriter *writer) {
55,458,688✔
561
  if (writer->blockData->nRow == 0) return 0;
55,458,688✔
562

563
  int32_t code = 0;
38,303,641✔
564
  int32_t lino = 0;
38,303,641✔
565

566
  tb_uid_t         uid = writer->blockData->suid == 0 ? writer->blockData->uid : writer->blockData->suid;
38,299,331✔
567
  SColCompressInfo info = {.defaultCmprAlg = writer->config->cmprAlg, .pColCmpr = NULL};
38,306,422✔
568
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, uid, &(info.pColCmpr));
38,307,673✔
569

570
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
38,303,081✔
571

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

576
_exit:
38,300,838✔
577
  if (code) {
38,301,576✔
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);
38,301,576✔
582
  return code;
38,301,230✔
583
}
584

585
static int32_t tsdbSttFileDoWriteStatisBlock(SSttFileWriter *writer) {
17,434,269✔
586
  if (writer->staticBlock->numOfRecords == 0) {
17,434,269✔
587
    return 0;
1,362,019✔
588
  }
589

590
  int32_t code = 0;
16,074,149✔
591
  int32_t lino = 0;
16,074,149✔
592

593
  SBuffer *buffer0 = writer->buffers + 0;
16,075,256✔
594
  SBuffer *buffer1 = writer->buffers + 1;
16,075,148✔
595
  SBuffer *assist = writer->buffers + 2;
16,075,891✔
596

597
  STbStatisRecord record;
16,070,723✔
598
  STbStatisBlock *statisBlock = writer->staticBlock;
16,077,526✔
599
  SStatisBlk      statisBlk = {0};
16,077,113✔
600

601
  statisBlk.dp->offset = writer->file->size;
16,073,767✔
602
  statisBlk.dp->size = 0;
16,076,526✔
603
  statisBlk.numRec = statisBlock->numOfRecords;
16,076,526✔
604
  statisBlk.cmprAlg = writer->config->cmprAlg;
16,075,462✔
605
  statisBlk.numOfPKs = statisBlock->numOfPKs;
16,076,060✔
606

607
  code = tStatisBlockGet(statisBlock, 0, &record);
16,073,757✔
608
  TSDB_CHECK_CODE(code, lino, _exit);
16,070,949✔
609
  statisBlk.minTbid.suid = record.suid;
16,070,949✔
610
  statisBlk.minTbid.uid = record.uid;
16,070,949✔
611

612
  code = tStatisBlockGet(statisBlock, statisBlock->numOfRecords - 1, &record);
16,070,949✔
613
  TSDB_CHECK_CODE(code, lino, _exit);
16,076,143✔
614
  statisBlk.maxTbid.suid = record.suid;
16,076,143✔
615
  statisBlk.maxTbid.uid = record.uid;
16,076,143✔
616

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

619
  // compress each column
620
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlk.size); i++) {
96,462,729✔
621
    SCompressInfo info = {
80,383,709✔
622
        .dataType = TSDB_DATA_TYPE_BIGINT,
623
        .cmprAlg = statisBlk.cmprAlg,
80,378,555✔
624
        .originalSize = statisBlock->buffers[i].size,
80,378,555✔
625
    };
626

627
    tBufferClear(buffer0);
628
    TAOS_CHECK_GOTO(tCompressDataToBuffer(statisBlock->buffers[i].data, &info, buffer0, assist), &lino, _exit);
80,383,024✔
629
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, buffer0->data, info.compressedSize, pEncryptData),
80,382,824✔
630
                    &lino, _exit);
631

632
    statisBlk.size[i] = info.compressedSize;
80,389,443✔
633
    statisBlk.dp->size += info.compressedSize;
80,386,827✔
634
    writer->file->size += info.compressedSize;
80,386,827✔
635
  }
636

637
  // compress primary keys
638
  if (statisBlk.numOfPKs > 0) {
16,079,020✔
639
    SValueColumnCompressInfo compressInfo = {.cmprAlg = statisBlk.cmprAlg};
271,267✔
640

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

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

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

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

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

665
  TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(writer->statisBlkArray, &statisBlk), &lino, _exit);
32,156,737✔
666

667
  tStatisBlockClear(writer->staticBlock);
16,077,717✔
668

669
_exit:
16,077,602✔
670
  if (code) {
16,076,418✔
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,073,207✔
675
}
676

677
static int32_t tsdbSttFileDoWriteTombBlock(SSttFileWriter *writer) {
15,854,643✔
678
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) {
15,854,643✔
679
    return 0;
14,296,798✔
680
  }
681

682
  int32_t code = 0;
1,559,592✔
683
  int32_t lino = 0;
1,559,592✔
684

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

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

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

699
int32_t tsdbFileWriteSttBlk(STsdbFD *fd, const TSttBlkArray *sttBlkArray, SFDataPtr *ptr, int64_t *fileSize,
15,856,588✔
700
                            SEncryptData *encryptData) {
701
  ptr->size = TARRAY2_DATA_LEN(sttBlkArray);
15,856,588✔
702
  if (ptr->size > 0) {
15,857,108✔
703
    ptr->offset = *fileSize;
15,809,130✔
704

705
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(sttBlkArray), ptr->size, encryptData));
15,805,627✔
706

707
    *fileSize += ptr->size;
15,807,317✔
708
  }
709
  return 0;
15,859,527✔
710
}
711

712
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
15,854,740✔
713
  int32_t code = 0;
15,854,740✔
714
  int32_t lino;
15,849,953✔
715

716
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
15,856,352✔
717

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

722
_exit:
15,859,218✔
723
  if (code) {
15,856,799✔
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;
15,856,799✔
728
}
729

730
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
15,853,158✔
731
  int32_t code = 0;
15,853,158✔
732
  int32_t lino;
15,848,371✔
733
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
15,854,819✔
734

735
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
15,859,215✔
736
  if (writer->footer->statisBlkPtr->size) {
15,853,259✔
737
    writer->footer->statisBlkPtr->offset = writer->file->size;
15,800,444✔
738
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
15,804,537✔
739
                                  writer->footer->statisBlkPtr->size, pEncryptData),
740
                    &lino, _exit);
741
    writer->file->size += writer->footer->statisBlkPtr->size;
15,807,876✔
742
  }
743

744
_exit:
15,852,760✔
745
  if (code) {
15,857,667✔
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;
15,857,667✔
750
}
751

752
static int32_t tsdbSttFileDoWriteTombBlk(SSttFileWriter *writer) {
15,850,733✔
753
  int32_t code = 0;
15,850,733✔
754
  int32_t lino = 0;
15,850,733✔
755

756
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
15,854,193✔
757

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

762
_exit:
15,855,101✔
763
  if (code) {
15,855,002✔
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;
15,855,002✔
768
}
769

770
int32_t tsdbFileWriteSttFooter(STsdbFD *fd, const SSttFooter *footer, int64_t *fileSize, SEncryptData *encryptData) {
15,853,009✔
771
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
15,853,009✔
772
  *fileSize += sizeof(*footer);
15,857,126✔
773
  return 0;
15,857,644✔
774
}
775

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

779
  return tsdbFileWriteSttFooter(writer->fd, writer->footer, &writer->file->size, pEncryptData);
15,857,246✔
780
}
781

782
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
15,820,276✔
783
  int32_t code = 0;
15,820,276✔
784
  int32_t lino = 0;
15,820,276✔
785
  STsdb  *tsdb = writer->config->tsdb;
15,832,873✔
786

787
  // set
788
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
15,831,721✔
789
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
15,833,631✔
790
  writer->buffers = writer->config->buffers;
15,834,063✔
791
  if (writer->buffers == NULL) {
15,840,451✔
792
    writer->buffers = writer->local;
×
793
  }
794

795
  // alloc disk id
796
  SDiskID diskId = {0};
15,846,734✔
797
  code = tsdbAllocateDisk(tsdb, tsdbFTypeLabel(TSDB_FTYPE_STT), writer->config->expLevel, &diskId);
15,836,229✔
798
  TSDB_CHECK_CODE(code, lino, _exit);
15,845,702✔
799

800
  writer->file[0] = (STFile){
15,845,979✔
801
      .type = TSDB_FTYPE_STT,
802
      .did = diskId,
803
      .fid = writer->config->fid,
15,845,702✔
804
      .cid = writer->config->cid,
15,844,035✔
805
      .size = 0,
806
      .minVer = VERSION_MAX,
807
      .maxVer = VERSION_MIN,
808
      .stt[0] =
809
          {
810
              .level = writer->config->level,
15,847,506✔
811
          },
812
  };
813

814
  // open file
815
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
15,844,630✔
816
  char    fname[TSDB_FILENAME_LEN];
15,839,843✔
817

818
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
15,847,398✔
819
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd, 0), &lino, _exit);
15,835,047✔
820

821
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
15,845,944✔
822
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
15,852,383✔
823

824
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd, 0, hdr, sizeof(hdr), pEncryptData), &lino, _exit);
15,852,171✔
825
  writer->file->size += sizeof(hdr);
15,844,220✔
826

827
  // range
828
  writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
15,855,232✔
829

830
  writer->ctx->opened = true;
15,846,777✔
831

832
_exit:
15,844,720✔
833
  if (code) {
15,851,483✔
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;
15,851,483✔
838
}
839

840
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
15,843,686✔
841
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
173,838,130✔
842
    tBufferDestroy(writer->local + i);
158,031,480✔
843
  }
844
  tDestroyTSchema(writer->skmRow->pTSchema);
15,806,650✔
845
  tDestroyTSchema(writer->skmTb->pTSchema);
15,833,935✔
846
  tTombBlockDestroy(writer->tombBlock);
15,845,711✔
847
  tStatisBlockDestroy(writer->staticBlock);
15,819,180✔
848
  tBlockDataDestroy(writer->blockData);
15,848,215✔
849
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
15,858,105✔
850
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
15,853,262✔
851
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
15,851,467✔
852
}
15,850,883✔
853

854
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
15,851,730✔
855
  // TODO
856
  return 0;
15,851,730✔
857
}
858

859
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
15,849,903✔
860
  int32_t lino;
15,845,116✔
861
  int32_t code;
862

863
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
15,852,112✔
864
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlock(writer), &lino, _exit);
15,854,668✔
865
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlock(writer), &lino, _exit);
15,856,517✔
866
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteSttBlk(writer), &lino, _exit);
15,856,018✔
867
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlk(writer), &lino, _exit);
15,853,728✔
868
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteTombBlk(writer), &lino, _exit);
15,851,175✔
869
  TAOS_CHECK_GOTO(tsdbSttFileDoWriteFooter(writer), &lino, _exit);
15,853,820✔
870
  TAOS_CHECK_GOTO(tsdbSttFileDoUpdateHeader(writer), &lino, _exit);
15,857,577✔
871

872
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
15,853,987✔
873

874
  TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd, pEncryptData), &lino, _exit);
15,857,541✔
875

876
  tsdbCloseFile(&writer->fd);
15,853,599✔
877

878
  STFileOp op = (STFileOp){
31,689,459✔
879
      .optype = TSDB_FOP_CREATE,
880
      .fid = writer->config->fid,
15,844,735✔
881
      .nf = writer->file[0],
882
  };
883
  tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
15,853,912✔
884

885
  TAOS_CHECK_GOTO(TARRAY2_APPEND(opArray, op), &lino, _exit);
31,673,898✔
886

887
_exit:
15,836,146✔
888
  if (code) {
15,837,133✔
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;
15,837,133✔
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,468,494✔
904
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
16,468,494✔
905
  if (writer[0] == NULL) {
16,436,064✔
906
    return terrno;
×
907
  }
908

909
  writer[0]->config[0] = config[0];
16,436,433✔
910
  writer[0]->ctx->opened = false;
16,441,288✔
911
  return 0;
16,466,321✔
912
}
913

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

918
  if (writer[0]->ctx->opened) {
16,477,388✔
919
    if (abort) {
15,852,051✔
920
      TAOS_CHECK_GOTO(tsdbSttFWriterCloseAbort(writer[0]), &lino, _exit);
×
921
    } else {
922
      TAOS_CHECK_GOTO(tsdbSttFWriterCloseCommit(writer[0], opArray), &lino, _exit);
15,852,051✔
923
    }
924
    tsdbSttFWriterDoClose(writer[0]);
15,834,477✔
925
  }
926
  taosMemoryFree(writer[0]);
16,475,558✔
927
  writer[0] = NULL;
16,469,606✔
928

929
_exit:
16,471,809✔
930
  if (code) {
16,473,449✔
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,473,449✔
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);
15,792,823✔
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);
16,909,666✔
947

948
    TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, (TABLEID *)row, writer->config->skmTb), &lino, _exit);
16,900,892✔
949

950
    TABLEID id = {
16,907,396✔
951
        .suid = row->suid,
16,898,560✔
952
        .uid = row->suid ? 0 : row->uid,
16,899,767✔
953
    };
954
    TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0), &lino, _exit);
16,895,757✔
955
  }
956

957
  if (writer->ctx->tbid->uid != row->uid) {
2,147,483,647✔
958
    writer->ctx->tbid->suid = row->suid;
82,444,384✔
959
    writer->ctx->tbid->uid = row->uid;
82,448,619✔
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);
81,608✔
969
      continue;
68,863✔
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);
58,552,121✔
992
  } else {
993
    if (writer->blockData->nRow >= writer->config->maxRow) {
2,147,483,647✔
994
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
21,204,053✔
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) {
10,836,667✔
1010
  int32_t code = 0;
10,836,667✔
1011
  int32_t lino = 0;
10,836,667✔
1012

1013
  SRowInfo row[1];
10,832,363✔
1014
  row->suid = bdata->suid;
10,836,667✔
1015
  for (int32_t i = 0; i < bdata->nRow; i++) {
95,676,963✔
1016
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
84,841,024✔
1017
    row->row = tsdbRowFromBlockData(bdata, i);
84,841,024✔
1018

1019
    TAOS_CHECK_GOTO(tsdbSttFileWriteRow(writer, row), &lino, _exit);
84,841,024✔
1020
  }
1021

1022
_exit:
10,836,667✔
1023
  if (code) {
10,836,667✔
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;
10,836,667✔
1028
}
1029

1030
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
6,152,821✔
1031
  int32_t code;
1032
  int32_t lino;
6,152,821✔
1033

1034
  if (!writer->ctx->opened) {
6,152,821✔
1035
    TAOS_CHECK_GOTO(tsdbSttFWriterDoOpen(writer), &lino, _exit);
48,937✔
1036
  } else {
1037
    if (writer->blockData->nRow > 0) {
6,103,884✔
1038
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteBlockData(writer), &lino, _exit);
1,510,655✔
1039
    }
1040

1041
    if (STATIS_BLOCK_SIZE(writer->staticBlock) > 0) {
6,103,884✔
1042
      TAOS_CHECK_GOTO(tsdbSttFileDoWriteStatisBlock(writer), &lino, _exit);
1,510,068✔
1043
    }
1044
  }
1045

1046
  TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
6,152,821✔
1047

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

1052
_exit:
6,152,821✔
1053
  if (code) {
6,152,821✔
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
6,152,821✔
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;
6,152,821✔
1063
}
1064

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