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

taosdata / TDengine / #4837

07 Nov 2025 09:40AM UTC coverage: 58.963% (+0.2%) from 58.728%
#4837

push

travis-ci

DuanKuanJun
coverity: cases_other.task add -R -Q2 -Q3 -Q4

150245 of 324452 branches covered (46.31%)

Branch coverage included in aggregate %.

200054 of 269646 relevant lines covered (74.19%)

317833830.25 hits per line

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

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

19
// SDataFileReader =============================================
20
struct SDataFileReader {
21
  SDataFileReaderConfig config[1];
22

23
  SBuffer  local[10];
24
  SBuffer *buffers;
25

26
  struct {
27
    bool headFooterLoaded;
28
    bool tombFooterLoaded;
29
    bool brinBlkLoaded;
30
    bool tombBlkLoaded;
31
  } ctx[1];
32

33
  STsdbFD *fd[TSDB_FTYPE_MAX];
34

35
  SHeadFooter   headFooter[1];
36
  STombFooter   tombFooter[1];
37
  TBrinBlkArray brinBlkArray[1];
38
  TTombBlkArray tombBlkArray[1];
39
};
40

41
static int32_t tsdbDataFileReadHeadFooter(SDataFileReader *reader) {
169,824,180✔
42
  if (reader->ctx->headFooterLoaded) {
169,824,180!
43
    return 0;
×
44
  }
45

46
  int32_t code = 0;
169,826,303✔
47
  int32_t lino = 0;
169,826,303✔
48

49
  int32_t ftype = TSDB_FTYPE_HEAD;
169,829,821✔
50
  if (reader->fd[ftype]) {
169,829,821✔
51
    int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
150,190,187✔
52
    char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
150,203,158✔
53
#if 1
54
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
150,214,975!
55
                                 (uint8_t *)reader->headFooter, sizeof(SHeadFooter), 0, encryptAlgorithm, encryptKey),
56
                    &lino, _exit);
57
#else
58
    int64_t size = reader->config->files[ftype].file.size;
59
    for (; size > TSDB_FHDR_SIZE; size--) {
60
      code = tsdbReadFile(reader->fd[ftype], size - sizeof(SHeadFooter), (uint8_t *)reader->headFooter,
61
                          sizeof(SHeadFooter), 0, encryptAlgorithm, encryptKey);
62
      if (code) continue;
63
      if (reader->headFooter->brinBlkPtr->offset + reader->headFooter->brinBlkPtr->size + sizeof(SHeadFooter) == size) {
64
        break;
65
      }
66
    }
67
    if (size <= TSDB_FHDR_SIZE) {
68
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
69
    }
70
#endif
71
  }
72

73
  reader->ctx->headFooterLoaded = true;
169,794,128✔
74

75
_exit:
169,817,842✔
76
  if (code) {
169,820,574!
77
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
78
              tstrerror(code));
79
  }
80
  return code;
169,801,682✔
81
}
82

83
static int32_t tsdbDataFileReadTombFooter(SDataFileReader *reader) {
21,706,451✔
84
  if (reader->ctx->tombFooterLoaded) {
21,706,451!
85
    return 0;
×
86
  }
87

88
  int32_t code = 0;
21,706,451✔
89
  int32_t lino = 0;
21,706,451✔
90

91
  int32_t ftype = TSDB_FTYPE_TOMB;
21,706,451✔
92
  if (reader->fd[ftype]) {
21,706,451✔
93
    int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
20,548,051✔
94
    char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
20,548,051✔
95
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(STombFooter),
20,548,051!
96
                                 (uint8_t *)reader->tombFooter, sizeof(STombFooter), 0, encryptAlgorithm, encryptKey),
97
                    &lino, _exit);
98
  }
99
  reader->ctx->tombFooterLoaded = true;
21,706,451✔
100

101
_exit:
21,706,451✔
102
  if (code) {
21,706,451!
103
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
104
              tstrerror(code));
105
  }
106
  return code;
21,704,787✔
107
}
108

109
int32_t tsdbDataFileReaderOpen(const char *fname[], const SDataFileReaderConfig *config, SDataFileReader **reader) {
171,885,036✔
110
  int32_t code = 0;
171,885,036✔
111
  int32_t lino = 0;
171,885,036✔
112

113
  if ((*reader = taosMemoryCalloc(1, sizeof(**reader))) == NULL) {
171,904,353!
114
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
115
  }
116

117
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); i++) {
1,890,722,743✔
118
    tBufferInit(reader[0]->local + i);
1,718,676,633✔
119
  }
120

121
  reader[0]->config[0] = config[0];
172,046,110✔
122
  reader[0]->buffers = config->buffers;
171,901,942✔
123
  if (reader[0]->buffers == NULL) {
171,888,807✔
124
    reader[0]->buffers = reader[0]->local;
168,965,568✔
125
  }
126

127
  if (fname) {
171,875,845✔
128
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
840,493,118✔
129
      if (fname[i]) {
672,380,197✔
130
        int32_t lcn = config->files[i].file.lcn;
466,772,864✔
131
        TAOS_CHECK_GOTO(tsdbOpenFile(fname[i], config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
466,777,974!
132
      }
133
    }
134
  } else {
135
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
18,974,054✔
136
      if (config->files[i].exist) {
15,181,192!
137
        char fname1[TSDB_FILENAME_LEN];
7,991,192✔
138
        tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
7,991,192✔
139
        int32_t lcn = config->files[i].file.lcn;
7,991,192✔
140
        TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
7,991,192!
141
      }
142
    }
143
  }
144

145
_exit:
171,908,219✔
146
  if (code) {
171,897,460!
147
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(config->tsdb->pVnode), __func__, __FILE__, lino,
×
148
              tstrerror(code));
149
  }
150
  return code;
171,897,460✔
151
}
152

153
void tsdbDataFileReaderClose(SDataFileReader **reader) {
303,584,813✔
154
  if (reader[0] == NULL) {
303,584,813✔
155
    return;
131,700,940✔
156
  }
157

158
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
171,902,704!
159
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);
171,900,531!
160

161
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
859,438,902✔
162
    if (reader[0]->fd[i]) {
687,537,703✔
163
      tsdbCloseFile(&reader[0]->fd[i]);
474,749,726✔
164
    }
165
  }
166

167
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
1,890,750,153✔
168
    tBufferDestroy(reader[0]->local + i);
1,718,848,569!
169
  }
170

171
  taosMemoryFree(reader[0]);
171,901,584!
172
  reader[0] = NULL;
171,894,605✔
173
}
174

175
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
169,833,281✔
176
  int32_t code = 0;
169,833,281✔
177
  int32_t lino = 0;
169,833,281✔
178
  void   *data = NULL;
169,839,819✔
179

180
  if (!reader->ctx->brinBlkLoaded) {
169,839,819!
181
    TAOS_CHECK_GOTO(tsdbDataFileReadHeadFooter(reader), &lino, _exit);
169,835,767!
182

183
    if (reader->headFooter->brinBlkPtr->size > 0) {
169,814,694✔
184
      data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
150,171,773!
185
      if (data == NULL) {
150,164,829!
186
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
187
      }
188

189
      int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
150,164,829✔
190
      char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
150,192,067✔
191

192
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_HEAD], reader->headFooter->brinBlkPtr->offset, data,
150,200,313!
193
                                   reader->headFooter->brinBlkPtr->size, 0, encryptAlgorithm, encryptKey),
194
                      &lino, _exit);
195

196
      int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
150,217,312✔
197
      TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
150,202,967✔
198
    } else {
199
      TARRAY2_INIT(reader->brinBlkArray);
19,635,398✔
200
    }
201

202
    reader->ctx->brinBlkLoaded = true;
169,808,595✔
203
  }
204
  brinBlkArray[0] = reader->brinBlkArray;
169,813,823✔
205

206
_exit:
169,825,202✔
207
  if (code) {
169,826,330!
208
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
209
              tstrerror(code));
210
    taosMemoryFree(data);
×
211
  }
212
  return code;
169,826,330✔
213
}
214

215
int32_t tsdbDataFileReadBrinBlock(SDataFileReader *reader, const SBrinBlk *brinBlk, SBrinBlock *brinBlock) {
152,656,757✔
216
  int32_t code = 0;
152,656,757✔
217
  int32_t lino = 0;
152,656,757✔
218

219
  SBuffer *buffer = reader->buffers + 0;
152,652,083✔
220
  SBuffer *assist = reader->buffers + 1;
152,655,969✔
221

222
  int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
152,692,342✔
223
  char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
152,676,015✔
224
  // load data
225
  tBufferClear(buffer);
226
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_HEAD], brinBlk->dp->offset, brinBlk->dp->size, buffer, 0,
152,655,087!
227
                                       encryptAlgorithm, encryptKey),
228
                  &lino, _exit);
229

230
  // decode brin block
231
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
152,689,107✔
232
  tBrinBlockClear(brinBlock);
152,696,098✔
233
  brinBlock->numOfPKs = brinBlk->numOfPKs;
152,708,577✔
234
  brinBlock->numOfRecords = brinBlk->numRec;
152,709,410✔
235
  for (int32_t i = 0; i < 10; i++) {  // int64_t
1,679,627,735✔
236

237
    SCompressInfo cinfo = {
1,526,915,996✔
238
        .cmprAlg = brinBlk->cmprAlg,
1,526,839,172✔
239
        .dataType = TSDB_DATA_TYPE_BIGINT,
240
        .compressedSize = brinBlk->size[i],
1,526,888,451✔
241
        .originalSize = brinBlk->numRec * sizeof(int64_t),
1,526,902,169✔
242
    };
243
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
1,526,880,420!
244
    br.offset += brinBlk->size[i];
1,526,948,861✔
245
  }
246

247
  for (int32_t i = 10; i < 15; i++) {  // int32_t
916,234,048✔
248
    SCompressInfo cinfo = {
763,506,579✔
249
        .cmprAlg = brinBlk->cmprAlg,
763,519,384✔
250
        .dataType = TSDB_DATA_TYPE_INT,
251
        .compressedSize = brinBlk->size[i],
763,512,489✔
252
        .originalSize = brinBlk->numRec * sizeof(int32_t),
763,517,277✔
253
    };
254
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
763,510,163!
255
    br.offset += brinBlk->size[i];
763,525,327✔
256
  }
257

258
  // primary keys
259
  if (brinBlk->numOfPKs > 0) {  // decode the primary keys
152,727,469✔
260
    SValueColumnCompressInfo firstInfos[TD_MAX_PK_COLS];
14,051✔
261
    SValueColumnCompressInfo lastInfos[TD_MAX_PK_COLS];
14,051✔
262

263
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
28,102✔
264
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, firstInfos + i), &lino, _exit);
14,051!
265
    }
266
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
28,102✔
267
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, lastInfos + i), &lino, _exit);
14,051!
268
    }
269

270
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
28,102✔
271
      SValueColumnCompressInfo *info = firstInfos + i;
14,051✔
272

273
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), info, brinBlock->firstKeyPKs + i, assist), &lino, _exit);
14,051!
274
      br.offset += (info->offsetCompressedSize + info->dataCompressedSize);
14,051✔
275
    }
276

277
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
28,102✔
278
      SValueColumnCompressInfo *info = lastInfos + i;
14,051✔
279

280
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), info, brinBlock->lastKeyPKs + i, assist), &lino, _exit);
14,051!
281
      br.offset += (info->offsetCompressedSize + info->dataCompressedSize);
14,051✔
282
    }
283
  }
284

285
  if (br.offset != br.buffer->size) {
152,708,625!
286
    tsdbError("vgId:%d %s failed at %s:%d since brin block size mismatch, expected: %u, actual: %u, fname:%s",
×
287
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino, br.buffer->size, br.offset,
288
              reader->fd[TSDB_FTYPE_HEAD]->path);
289
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
290
  }
291

292
_exit:
152,673,423✔
293
  if (code) {
152,705,354!
294
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
295
              tstrerror(code));
296
  }
297
  return code;
152,705,354✔
298
}
299

300
extern int32_t tBlockDataDecompress(SBufferReader *br, SBlockData *blockData, SBuffer *assist);
301

302
int32_t tsdbDataFileReadBlockData(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData) {
20,816,326✔
303
  int32_t code = 0;
20,816,326✔
304
  int32_t lino = 0;
20,816,326✔
305
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
20,816,326✔
306

307
  SBuffer *buffer = reader->buffers + 0;
20,816,326✔
308
  SBuffer *assist = reader->buffers + 1;
20,816,326✔
309

310
  int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
20,816,326✔
311
  char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
20,816,326✔
312
  // load data
313
  tBufferClear(buffer);
314
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockSize, buffer, 0,
20,816,326!
315
                                       encryptAlgorithm, encryptKey),
316
                  &lino, _exit);
317

318
  // decompress
319
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
20,816,326✔
320
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
20,816,326!
321

322
  if (br.offset != buffer->size) {
20,816,326!
323
    tsdbError("vgId:%d %s failed at %s:%d since block data size mismatch, expected: %u, actual: %u, fname:%s",
×
324
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer->size, br.offset,
325
              reader->fd[TSDB_FTYPE_DATA]->path);
326
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
327
  }
328

329
_exit:
20,816,326✔
330
  if (code) {
20,816,326!
331
    tsdbError("vgId:%d %s fid %d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
×
332
              __FILE__, lino, tstrerror(code));
333
  }
334
  return code;
20,816,326✔
335
}
336

337
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
439,406,935✔
338
                                          STSchema *pTSchema, int16_t cids[], int32_t ncid) {
339
  int32_t code = 0;
439,406,935✔
340
  int32_t lino = 0;
439,406,935✔
341
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
439,485,855✔
342

343
  SDiskDataHdr hdr;
439,499,775✔
344
  SBuffer     *buffer0 = reader->buffers + 0;
439,490,422✔
345
  SBuffer     *buffer1 = reader->buffers + 1;
439,528,164✔
346
  SBuffer     *assist = reader->buffers + 2;
439,508,947✔
347

348
  int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
439,509,513✔
349
  char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
439,521,478✔
350
  // load key part
351
  tBufferClear(buffer0);
352
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockKeySize, buffer0,
439,488,379!
353
                                       0, encryptAlgorithm, encryptKey),
354
                  &lino, _exit);
355

356
  // SDiskDataHdr
357
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
439,480,890✔
358
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
439,500,385!
359

360
  if (hdr.delimiter != TSDB_FILE_DLMT) {
439,373,069!
361
    tsdbError("vgId:%d %s failed at %s:%d since disk data header delimiter is invalid, fname:%s",
×
362
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd[TSDB_FTYPE_DATA]->path);
363
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
364
  }
365

366
  tBlockDataReset(bData);
439,373,069✔
367
  bData->suid = hdr.suid;
439,457,355✔
368
  bData->uid = hdr.uid;
439,504,977✔
369
  bData->nRow = hdr.nRow;
439,494,303✔
370

371
  // Key part
372
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
439,474,376!
373
  if (br.offset != buffer0->size) {
439,506,186✔
374
    tsdbError("vgId:%d %s failed at %s:%d since key part size mismatch, expected: %u, actual: %u, fname:%s",
58,969!
375
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer0->size, br.offset,
376
              reader->fd[TSDB_FTYPE_DATA]->path);
377
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
378
  }
379

380
  int extraColIdx = -1;
439,478,088✔
381
  for (int i = 0; i < ncid; i++) {
439,488,340✔
382
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
418,033,901✔
383
      extraColIdx = i;
418,025,377✔
384
      break;
418,025,377✔
385
    }
386
  }
387

388
  if (extraColIdx < 0) {
439,479,816✔
389
    goto _exit;
21,442,571✔
390
  }
391

392
  // load SBlockCol part
393
  tBufferClear(buffer0);
394
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset + record->blockKeySize,
418,041,384!
395
                                       hdr.szBlkCol, buffer0, 0, encryptAlgorithm, encryptKey),
396
                  &lino, _exit);
397

398
  // calc szHint
399
  int64_t szHint = 0;
418,082,415✔
400
  int     extraCols = 1;
418,082,415✔
401
  for (int i = extraColIdx + 1; i < ncid; ++i) {
418,082,415✔
402
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
145,207,281!
403
      ++extraCols;
145,200,255✔
404
      break;
145,200,255✔
405
    }
406
  }
407

408
  if (extraCols >= 2) {
418,075,389✔
409
    br = BUFFER_READER_INITIALIZER(0, buffer0);
145,203,982✔
410

411
    SBlockCol blockCol = {.cid = 0};
145,203,982✔
412
    for (int32_t i = extraColIdx; i < ncid; ++i) {
145,203,654✔
413
      int16_t extraColCid = cids[i];
145,195,124✔
414

415
      while (extraColCid > blockCol.cid) {
311,818,877✔
416
        if (br.offset >= buffer0->size) {
166,586,505!
417
          blockCol.cid = INT16_MAX;
×
418
          break;
×
419
        }
420

421
        TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
166,589,558!
422
      }
423

424
      if (extraColCid == blockCol.cid || blockCol.cid == INT16_MAX) {
145,232,372!
425
        extraColIdx = i;
145,232,372✔
426
        break;
145,232,372✔
427
      }
428
    }
429

430
    if (blockCol.cid > 0 && blockCol.cid < INT16_MAX /*&& blockCol->flag == HAS_VALUE*/) {
145,240,902✔
431
      int64_t   offset = blockCol.offset;
145,199,002✔
432
      SBlockCol lastNonNoneBlockCol = {.cid = 0};
145,199,002✔
433

434
      for (int32_t i = extraColIdx; i < ncid; ++i) {
994,619,919✔
435
        int16_t extraColCid = cids[i];
849,346,322✔
436

437
        while (extraColCid > blockCol.cid) {
1,575,631,902✔
438
          if (br.offset >= buffer0->size) {
726,210,985!
439
            blockCol.cid = INT16_MAX;
×
440
            break;
×
441
          }
442

443
          TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
726,238,367!
444
        }
445

446
        if (extraColCid == blockCol.cid) {
849,420,917!
447
          lastNonNoneBlockCol = blockCol;
849,420,917✔
448
          continue;
849,420,917✔
449
        }
450

451
        if (blockCol.cid == INT16_MAX) {
×
452
          break;
×
453
        }
454
      }
455

456
      if (lastNonNoneBlockCol.cid > 0) {
145,273,597✔
457
        szHint = lastNonNoneBlockCol.offset + lastNonNoneBlockCol.szBitmap + lastNonNoneBlockCol.szOffset +
145,243,642✔
458
                 lastNonNoneBlockCol.szValue - offset;
145,243,642✔
459
      }
460
    }
461
  }
462

463
  // load each column
464
  SBlockCol blockCol = {
418,111,461✔
465
      .cid = 0,
466
  };
467
  bool firstRead = true;
418,024,384✔
468
  br = BUFFER_READER_INITIALIZER(0, buffer0);
418,024,384✔
469
  for (int32_t i = 0; i < ncid; i++) {
1,540,316,451✔
470
    int16_t cid = cids[i];
1,122,171,260✔
471

472
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
1,122,117,016✔
473
      continue;
10,252✔
474
    }
475

476
    while (cid > blockCol.cid) {
2,147,483,647✔
477
      if (br.offset >= buffer0->size) {
2,092,749,905!
478
        blockCol.cid = INT16_MAX;
×
479
        break;
×
480
      }
481

482
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
2,092,684,945!
483
    }
484

485
    if (cid < blockCol.cid) {
1,122,331,071!
486
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
×
487
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
×
488
      SBlockCol none = {
×
489
          .cid = cid,
490
          .type = tcol->type,
×
491
          .cflag = tcol->flags,
×
492
          .flag = HAS_NONE,
493
          .szOrigin = 0,
494
          .szBitmap = 0,
495
          .szOffset = 0,
496
          .szValue = 0,
497
          .offset = 0,
498
      };
499
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &none, &br, bData, assist), &lino, _exit);
×
500
    } else if (cid == blockCol.cid) {
1,122,331,071✔
501
      int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
1,122,166,190✔
502
      char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
1,122,338,889✔
503
      // load from file
504
      tBufferClear(buffer1);
505
      TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA],
1,122,326,838!
506
                                           record->blockOffset + record->blockKeySize + hdr.szBlkCol + blockCol.offset,
507
                                           blockCol.szBitmap + blockCol.szOffset + blockCol.szValue, buffer1,
508
                                           firstRead ? szHint : 0, encryptAlgorithm, encryptKey),
509
                      &lino, _exit);
510

511
      firstRead = false;
1,122,296,634✔
512

513
      // decode the buffer
514
      SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
1,122,296,634✔
515
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
1,122,263,421!
516
    }
517
  }
518

519
_exit:
439,682,934✔
520
  if (code) {
439,521,798!
521
    tsdbError("vgId:%d %s fid:%d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
×
522
              __FILE__, lino, tstrerror(code));
523
  }
524
  return code;
439,521,798✔
525
}
526

527
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
188,740,594✔
528
                                 TColumnDataAggArray *columnDataAggArray) {
529
  int32_t  code = 0;
188,740,594✔
530
  int32_t  lino = 0;
188,740,594✔
531
  SBuffer *buffer = reader->buffers + 0;
188,741,218✔
532

533
  TARRAY2_CLEAR(columnDataAggArray, NULL);
188,742,472✔
534
  if (record->smaSize > 0) {
188,742,472!
535
    tBufferClear(buffer);
536
    int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
188,741,326✔
537
    char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
188,742,994✔
538
    TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_SMA], record->smaOffset, record->smaSize, buffer, 0,
188,741,362!
539
                                         encryptAlgorithm, encryptKey),
540
                    &lino, _exit);
541

542
    // decode sma data
543
    SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
188,741,488✔
544
    while (br.offset < record->smaSize) {
1,376,065,011✔
545
      SColumnDataAgg sma[1];
1,187,319,737✔
546

547
      TAOS_CHECK_GOTO(tGetColumnDataAgg(&br, sma), &lino, _exit);
1,187,316,017!
548
      TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(columnDataAggArray, sma), &lino, _exit);
2,147,483,647!
549
    }
550
    if (br.offset != record->smaSize) {
188,742,556!
551
      tsdbError("vgId:%d %s failed at %s:%d since sma data size mismatch, expected: %u, actual: %u, fname:%s",
×
552
                TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, record->smaSize, br.offset,
553
                reader->fd[TSDB_FTYPE_SMA]->path);
554
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
555
    }
556
  }
557

558
_exit:
188,742,016✔
559
  if (code) {
188,742,028!
560
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
561
              tstrerror(code));
562
  }
563
  return code;
188,742,028✔
564
}
565

566
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
35,556,526✔
567
  int32_t code = 0;
35,556,526✔
568
  int32_t lino = 0;
35,556,526✔
569
  void   *data = NULL;
35,572,922✔
570

571
  if (!reader->ctx->tombBlkLoaded) {
35,572,922!
572
    TAOS_CHECK_GOTO(tsdbDataFileReadTombFooter(reader), &lino, _exit);
21,706,451!
573

574
    if (reader->tombFooter->tombBlkPtr->size > 0) {
21,706,451✔
575
      if ((data = taosMemoryMalloc(reader->tombFooter->tombBlkPtr->size)) == NULL) {
20,548,051!
576
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
577
      }
578

579
      int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
20,548,051✔
580
      char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
20,548,051✔
581
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], reader->tombFooter->tombBlkPtr->offset, data,
20,548,051!
582
                                   reader->tombFooter->tombBlkPtr->size, 0, encryptAlgorithm, encryptKey),
583
                      &lino, _exit);
584

585
      int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
20,548,051✔
586
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
20,548,051✔
587
    } else {
588
      TARRAY2_INIT(reader->tombBlkArray);
1,156,736✔
589
    }
590

591
    reader->ctx->tombBlkLoaded = true;
21,702,351✔
592
  }
593
  tombBlkArray[0] = reader->tombBlkArray;
35,570,486✔
594

595
_exit:
35,569,594✔
596
  if (code) {
35,572,922!
597
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
598
              tstrerror(code));
599
    taosMemoryFree(data);
×
600
  }
601
  return code;
35,572,922✔
602
}
603

604
int32_t tsdbDataFileReadTombBlock(SDataFileReader *reader, const STombBlk *tombBlk, STombBlock *tData) {
13,250,414✔
605
  int32_t code = 0;
13,250,414✔
606
  int32_t lino = 0;
13,250,414✔
607

608
  SBuffer *buffer0 = reader->buffers + 0;
13,250,414✔
609
  SBuffer *assist = reader->buffers + 1;
13,250,414✔
610

611
  tBufferClear(buffer0);
612
  int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
13,250,414✔
613
  char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
13,250,414✔
614
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_TOMB], tombBlk->dp->offset, tombBlk->dp->size, buffer0, 0,
13,250,414!
615
                                       encryptAlgorithm, encryptKey),
616
                  &lino, _exit);
617

618
  int32_t       size = 0;
13,250,414✔
619
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
13,250,414✔
620
  tTombBlockClear(tData);
13,250,414✔
621
  tData->numOfRecords = tombBlk->numRec;
13,250,414✔
622
  for (int32_t i = 0; i < ARRAY_SIZE(tData->buffers); ++i) {
79,502,484✔
623
    SCompressInfo cinfo = {
66,252,070✔
624
        .cmprAlg = tombBlk->cmprAlg,
66,252,070✔
625
        .dataType = TSDB_DATA_TYPE_BIGINT,
626
        .originalSize = tombBlk->numRec * sizeof(int64_t),
66,252,070✔
627
        .compressedSize = tombBlk->size[i],
66,252,070✔
628
    };
629
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, tData->buffers + i, assist), &lino, _exit);
66,252,070!
630
    br.offset += tombBlk->size[i];
66,252,070✔
631
  }
632

633
_exit:
13,250,414✔
634
  if (code) {
13,250,414!
635
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
636
              tstrerror(code));
637
  }
638
  return code;
13,250,414✔
639
}
640

641
// SDataFileWriter =============================================
642
struct SDataFileWriter {
643
  SDataFileWriterConfig config[1];
644

645
  SSkmInfo skmTb[1];
646
  SSkmInfo skmRow[1];
647
  SBuffer  local[10];
648
  SBuffer *buffers;
649

650
  struct {
651
    bool             opened;
652
    SDataFileReader *reader;
653

654
    // for ts data
655
    TABLEID tbid[1];
656
    bool    tbHasOldData;
657

658
    const TBrinBlkArray *brinBlkArray;
659
    int32_t              brinBlkArrayIdx;
660
    SBrinBlock           brinBlock[1];
661
    int32_t              brinBlockIdx;
662
    SBlockData           blockData[1];
663
    int32_t              blockDataIdx;
664
    // for tomb data
665
    bool                 hasOldTomb;
666
    const TTombBlkArray *tombBlkArray;
667
    int32_t              tombBlkArrayIdx;
668
    STombBlock           tombBlock[1];
669
    int32_t              tombBlockIdx;
670
    // range
671
    SVersionRange range;
672
    SVersionRange tombRange;
673
  } ctx[1];
674

675
  STFile   files[TSDB_FTYPE_MAX];
676
  STsdbFD *fd[TSDB_FTYPE_MAX];
677

678
  SHeadFooter headFooter[1];
679
  STombFooter tombFooter[1];
680

681
  TBrinBlkArray brinBlkArray[1];
682
  SBrinBlock    brinBlock[1];
683
  SBlockData    blockData[1];
684

685
  TTombBlkArray tombBlkArray[1];
686
  STombBlock    tombBlock[1];
687
};
688

689
static int32_t tsdbDataFileWriterCloseAbort(SDataFileWriter *writer) {
×
690
  tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, __LINE__,
×
691
            "not implemented");
692
  return 0;
×
693
}
694

695
static void tsdbDataFileWriterDoClose(SDataFileWriter *writer) {
6,450,343✔
696
  if (writer->ctx->reader) {
6,450,343✔
697
    tsdbDataFileReaderClose(&writer->ctx->reader);
2,923,517✔
698
  }
699

700
  tTombBlockDestroy(writer->tombBlock);
6,450,343✔
701
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
6,450,533!
702
  tBlockDataDestroy(writer->blockData);
6,452,304✔
703
  tBrinBlockDestroy(writer->brinBlock);
6,450,343✔
704
  TARRAY2_DESTROY(writer->brinBlkArray, NULL);
6,451,108!
705

706
  tTombBlockDestroy(writer->ctx->tombBlock);
6,451,417✔
707
  tBlockDataDestroy(writer->ctx->blockData);
6,451,729✔
708
  tBrinBlockDestroy(writer->ctx->brinBlock);
6,452,304✔
709

710
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
70,963,568✔
711
    tBufferDestroy(writer->local + i);
64,511,264!
712
  }
713

714
  tDestroyTSchema(writer->skmRow->pTSchema);
6,452,304!
715
  tDestroyTSchema(writer->skmTb->pTSchema);
6,452,304!
716
}
6,452,304✔
717

718
static int32_t tsdbDataFileWriterDoOpenReader(SDataFileWriter *writer) {
6,452,304✔
719
  int32_t code = 0;
6,452,304✔
720
  int32_t lino = 0;
6,452,304✔
721

722
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
22,759,642✔
723
    if (writer->config->files[i].exist) {
19,230,855!
724
      SDataFileReaderConfig config[1] = {{
2,923,517✔
725
          .tsdb = writer->config->tsdb,
2,923,517✔
726
          .szPage = writer->config->szPage,
2,923,517✔
727
          .buffers = writer->buffers,
2,923,517✔
728
      }};
729

730
      for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
14,617,585✔
731
        config->files[i].exist = writer->config->files[i].exist;
11,694,068!
732
        if (config->files[i].exist) {
11,694,068!
733
          config->files[i].file = writer->config->files[i].file;
7,317,281✔
734
        }
735
      }
736

737
      TAOS_CHECK_GOTO(tsdbDataFileReaderOpen(NULL, config, &writer->ctx->reader), &lino, _exit);
2,923,517!
738
      break;
2,923,517✔
739
    }
740
  }
741

742
_exit:
6,452,304✔
743
  if (code) {
6,452,304!
744
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
745
              tstrerror(code));
746
  }
747
  return code;
6,452,304✔
748
}
749

750
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
6,452,304✔
751
  int32_t code = 0;
6,452,304✔
752
  int32_t lino = 0;
6,452,304✔
753
  int32_t ftype;
754
  SDiskID diskId = {0};
6,452,304✔
755

756
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
6,452,304!
757
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
6,452,304!
758
  writer->buffers = writer->config->buffers;
6,452,304✔
759
  if (writer->buffers == NULL) {
6,452,304!
760
    writer->buffers = writer->local;
×
761
  }
762

763
  // open reader
764
  TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpenReader(writer), &lino, _exit);
6,452,304!
765

766
  // .head
767
  ftype = TSDB_FTYPE_HEAD;
6,452,304✔
768
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
6,452,304✔
769
  TSDB_CHECK_CODE(code, lino, _exit);
6,452,304!
770
  writer->files[ftype] = (STFile){
12,904,296✔
771
      .type = ftype,
772
      .did = diskId,
773
      .fid = writer->config->fid,
6,452,304✔
774
      .cid = writer->config->cid,
6,452,304✔
775
      .size = 0,
776
      .minVer = VERSION_MAX,
777
      .maxVer = VERSION_MIN,
778
  };
779

780
  // .data
781
  ftype = TSDB_FTYPE_DATA;
6,450,343✔
782
  if (writer->config->files[ftype].exist) {
6,450,343!
783
    writer->files[ftype] = writer->config->files[ftype].file;
2,192,787✔
784
  } else {
785
    code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
4,257,244✔
786
    TSDB_CHECK_CODE(code, lino, _exit);
4,259,517!
787
    writer->files[ftype] = (STFile){
8,519,034✔
788
        .type = ftype,
789
        .did = diskId,
790
        .fid = writer->config->fid,
4,259,517✔
791
        .cid = writer->config->cid,
4,259,517✔
792
        .size = 0,
793
        .lcn = writer->config->lcn == 0 ? -1 : 0,
4,259,517✔
794
        .minVer = VERSION_MAX,
795
        .maxVer = VERSION_MIN,
796
    };
797
  }
798

799
  // .sma
800
  ftype = TSDB_FTYPE_SMA;
6,452,304✔
801
  if (writer->config->files[ftype].exist) {
6,452,304!
802
    writer->files[ftype] = writer->config->files[ftype].file;
2,192,787✔
803
  } else {
804
    code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
4,259,517✔
805
    TSDB_CHECK_CODE(code, lino, _exit);
4,259,517!
806
    writer->files[ftype] = (STFile){
8,519,034✔
807
        .type = ftype,
808
        .did = diskId,
809
        .fid = writer->config->fid,
4,259,517✔
810
        .cid = writer->config->cid,
4,259,517✔
811
        .size = 0,
812
        .minVer = VERSION_MAX,
813
        .maxVer = VERSION_MIN,
814
    };
815
  }
816

817
  // .tomb
818
  ftype = TSDB_FTYPE_TOMB;
6,452,304✔
819
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
6,452,304✔
820
  TSDB_CHECK_CODE(code, lino, _exit);
6,452,304!
821
  writer->files[ftype] = (STFile){
12,904,608✔
822
      .type = ftype,
823
      .did = diskId,
824
      .fid = writer->config->fid,
6,452,304✔
825
      .cid = writer->config->cid,
6,452,304✔
826
      .size = 0,
827
      .minVer = VERSION_MAX,
828
      .maxVer = VERSION_MIN,
829
  };
830

831
  // range
832
  writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
6,452,304✔
833
  writer->ctx->tombRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
6,452,304✔
834

835
  writer->ctx->opened = true;
6,452,304✔
836

837
_exit:
6,452,304✔
838
  if (code) {
6,452,304!
839
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
840
              tstrerror(code));
841
  }
842
  return code;
6,452,304✔
843
}
844

845
void tsdbWriterUpdVerRange(SVersionRange *range, int64_t minVer, int64_t maxVer) {
663,330,385✔
846
  range->minVer = TMIN(range->minVer, minVer);
663,330,385✔
847
  range->maxVer = TMAX(range->maxVer, maxVer);
663,448,523✔
848
}
663,491,958✔
849

850
int32_t tsdbFileWriteBrinBlock(STsdbFD *fd, SBrinBlock *brinBlock, uint32_t cmprAlg, int64_t *fileSize,
8,861,328✔
851
                               TBrinBlkArray *brinBlkArray, SBuffer *buffers, SVersionRange *range,
852
                               int32_t encryptAlgorithm, char *encryptKey) {
853
  if (brinBlock->numOfRecords == 0) {
8,861,328!
854
    return 0;
×
855
  }
856

857
  int32_t  code;
858
  SBuffer *buffer0 = buffers + 0;
8,861,328✔
859
  SBuffer *buffer1 = buffers + 1;
8,861,328✔
860
  SBuffer *assist = buffers + 2;
8,861,328✔
861

862
  SBrinBlk brinBlk = {
8,861,328✔
863
      .dp[0] =
864
          {
865
              .offset = *fileSize,
8,861,328✔
866
              .size = 0,
867
          },
868
      .numRec = brinBlock->numOfRecords,
8,861,328✔
869
      .numOfPKs = brinBlock->numOfPKs,
8,861,328✔
870
      .cmprAlg = cmprAlg,
871
  };
872
  for (int i = 0; i < brinBlock->numOfRecords; i++) {
1,058,289,559✔
873
    SBrinRecord record;
1,049,428,231✔
874

875
    TAOS_CHECK_RETURN(tBrinBlockGet(brinBlock, i, &record));
1,049,428,231!
876
    if (i == 0) {
1,049,428,231✔
877
      brinBlk.minTbid.suid = record.suid;
8,861,328✔
878
      brinBlk.minTbid.uid = record.uid;
8,861,328✔
879
      brinBlk.minVer = record.minVer;
8,861,328✔
880
      brinBlk.maxVer = record.maxVer;
8,861,328✔
881
    }
882
    if (i == brinBlock->numOfRecords - 1) {
1,049,428,231✔
883
      brinBlk.maxTbid.suid = record.suid;
8,860,753✔
884
      brinBlk.maxTbid.uid = record.uid;
8,860,753✔
885
    }
886
    if (record.minVer < brinBlk.minVer) {
1,049,428,231✔
887
      brinBlk.minVer = record.minVer;
7,966,724✔
888
    }
889
    if (record.maxVer > brinBlk.maxVer) {
1,049,428,231✔
890
      brinBlk.maxVer = record.maxVer;
139,222,204✔
891
    }
892
  }
893

894
  tsdbWriterUpdVerRange(range, brinBlk.minVer, brinBlk.maxVer);
8,861,328✔
895

896
  // write to file
897
  for (int32_t i = 0; i < 10; ++i) {
97,472,837✔
898
    SCompressInfo info = {
88,611,509✔
899
        .cmprAlg = cmprAlg,
900
        .dataType = TSDB_DATA_TYPE_BIGINT,
901
        .originalSize = brinBlock->buffers[i].size,
88,610,934✔
902
    };
903

904
    tBufferClear(buffer0);
905
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
88,611,509!
906
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptAlgorithm, encryptKey));
88,611,319!
907
    brinBlk.size[i] = info.compressedSize;
88,611,509✔
908
    brinBlk.dp->size += info.compressedSize;
88,611,509✔
909
    *fileSize += info.compressedSize;
88,611,509✔
910
  }
911
  for (int32_t i = 10; i < 15; ++i) {
53,167,968✔
912
    SCompressInfo info = {
44,306,640✔
913
        .cmprAlg = cmprAlg,
914
        .dataType = TSDB_DATA_TYPE_INT,
915
        .originalSize = brinBlock->buffers[i].size,
44,306,640✔
916
    };
917

918
    tBufferClear(buffer0);
919
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
44,306,640!
920
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptAlgorithm, encryptKey));
44,306,640!
921
    brinBlk.size[i] = info.compressedSize;
44,306,640✔
922
    brinBlk.dp->size += info.compressedSize;
44,306,640✔
923
    *fileSize += info.compressedSize;
44,306,640✔
924
  }
925

926
  // write primary keys to file
927
  if (brinBlock->numOfPKs > 0) {
8,861,328✔
928
    tBufferClear(buffer0);
929
    tBufferClear(buffer1);
930

931
    // encode
932
    for (int i = 0; i < brinBlock->numOfPKs; i++) {
28,102✔
933
      SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
14,051✔
934
      TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->firstKeyPKs[i], &info, buffer1, assist));
14,051!
935
      TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
14,051!
936
    }
937
    for (int i = 0; i < brinBlock->numOfPKs; i++) {
28,102✔
938
      SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
14,051✔
939
      TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->lastKeyPKs[i], &info, buffer1, assist));
14,051!
940
      TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
14,051!
941
    }
942

943
    // write to file
944
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptAlgorithm, encryptKey));
14,051!
945
    *fileSize += buffer0->size;
14,051✔
946
    brinBlk.dp->size += buffer0->size;
14,051✔
947
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer1->data, buffer1->size, encryptAlgorithm, encryptKey));
14,051!
948
    *fileSize += buffer1->size;
14,051✔
949
    brinBlk.dp->size += buffer1->size;
14,051✔
950
  }
951

952
  // append to brinBlkArray
953
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(brinBlkArray, &brinBlk));
17,722,656!
954

955
  tBrinBlockClear(brinBlock);
8,861,328✔
956

957
  return 0;
8,859,367✔
958
}
959

960
static int32_t tsdbDataFileWriteBrinBlock(SDataFileWriter *writer) {
8,861,328✔
961
  if (writer->brinBlock->numOfRecords == 0) {
8,861,328!
962
    return 0;
×
963
  }
964

965
  int32_t code = 0;
8,861,328✔
966
  int32_t lino = 0;
8,861,328✔
967

968
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
8,861,328✔
969
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
8,861,328✔
970

971
  TAOS_CHECK_GOTO(tsdbFileWriteBrinBlock(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlock, writer->config->cmprAlg,
8,861,328!
972
                                         &writer->files[TSDB_FTYPE_HEAD].size, writer->brinBlkArray, writer->buffers,
973
                                         &writer->ctx->range, encryptAlgorithm, encryptKey),
974
                  &lino, _exit);
975

976
_exit:
8,859,367✔
977
  if (code) {
8,859,367!
978
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
979
              tstrerror(code));
980
  }
981
  return code;
8,861,328✔
982
}
983

984
static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
1,049,421,834✔
985
  int32_t code = 0;
1,049,421,834✔
986
  int32_t lino = 0;
1,049,421,834✔
987

988
  for (;;) {
989
    code = tBrinBlockPut(writer->brinBlock, record);
1,049,423,375✔
990
    if (code == TSDB_CODE_INVALID_PARA) {
1,049,396,034!
991
      // different records with different primary keys
992
      TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
×
993
      continue;
×
994
    } else {
995
      TSDB_CHECK_CODE(code, lino, _exit);
1,049,396,034!
996
    }
997
    break;
1,049,396,034✔
998
  }
999

1000
  if ((writer->brinBlock->numOfRecords) >= 256) {
1,049,396,034✔
1001
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
3,434,808!
1002
  }
1003

1004
_exit:
1,049,366,517✔
1005
  if (code) {
1,049,403,438!
1006
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1007
              tstrerror(code));
1008
  }
1009
  return code;
1,049,403,438✔
1010
}
1011

1012
static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
326,519,758✔
1013
  if (bData->nRow == 0) {
326,519,758✔
1014
    return 0;
83,771,053✔
1015
  }
1016

1017
  if (!bData->uid) {
242,764,754!
1018
    return TSDB_CODE_INVALID_PARA;
×
1019
  }
1020

1021
  int32_t  code = 0;
242,769,362✔
1022
  int32_t  lino = 0;
242,769,362✔
1023
  SBuffer *buffers = writer->buffers;
242,768,475✔
1024
  SBuffer *assist = writer->buffers + 4;
242,765,348✔
1025

1026
  SColCompressInfo cmprInfo = {.pColCmpr = NULL, .defaultCmprAlg = writer->config->cmprAlg};
242,764,581✔
1027

1028
  SBrinRecord record[1] = {{
242,766,591✔
1029
      .suid = bData->suid,
242,765,273✔
1030
      .uid = bData->uid,
242,759,650✔
1031
      .minVer = bData->aVersion[0],
242,758,271✔
1032
      .maxVer = bData->aVersion[0],
242,764,506✔
1033
      .blockOffset = writer->files[TSDB_FTYPE_DATA].size,
242,768,491✔
1034
      .smaOffset = writer->files[TSDB_FTYPE_SMA].size,
242,770,751✔
1035
      .blockSize = 0,
1036
      .blockKeySize = 0,
1037
      .smaSize = 0,
1038
      .numRow = bData->nRow,
242,777,245✔
1039
      .count = 1,
1040
  }};
1041

1042
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &record->firstKey);
242,770,257✔
1043
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, bData->nRow - 1), &record->lastKey);
242,769,769✔
1044

1045
  for (int32_t i = 1; i < bData->nRow; ++i) {
2,147,483,647✔
1046
    if (tsdbRowCompareWithoutVersion(&tsdbRowFromBlockData(bData, i - 1), &tsdbRowFromBlockData(bData, i)) != 0) {
2,147,483,647!
1047
      record->count++;
2,147,483,647✔
1048
    }
1049
    if (bData->aVersion[i] < record->minVer) {
2,147,483,647✔
1050
      record->minVer = bData->aVersion[i];
50,739,918✔
1051
    }
1052
    if (bData->aVersion[i] > record->maxVer) {
2,147,483,647✔
1053
      record->maxVer = bData->aVersion[i];
820,137,675✔
1054
    }
1055
  }
1056

1057
  tsdbWriterUpdVerRange(&writer->ctx->range, record->minVer, record->maxVer);
242,777,245✔
1058

1059
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, bData->suid != 0 ? bData->suid : bData->uid,
242,777,245✔
1060
                        &cmprInfo.pColCmpr);
1061
  if (code) {
242,765,691!
1062
    tsdbWarn("vgId:%d failed to get column compress algrithm", TD_VID(writer->config->tsdb->pVnode));
×
1063
  }
1064

1065
  TAOS_CHECK_GOTO(tBlockDataCompress(bData, &cmprInfo, buffers, assist), &lino, _exit);
242,765,691!
1066

1067
  record->blockKeySize = buffers[0].size + buffers[1].size;
242,735,838✔
1068
  record->blockSize = record->blockKeySize + buffers[2].size + buffers[3].size;
242,763,563✔
1069

1070
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
242,750,638✔
1071
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
242,775,704✔
1072
  for (int i = 0; i < 4; i++) {
1,213,780,766✔
1073
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, buffers[i].data,
971,004,587!
1074
                                  buffers[i].size, encryptAlgorithm, encryptKey),
1075
                    &lino, _exit);
1076
    writer->files[TSDB_FTYPE_DATA].size += buffers[i].size;
970,993,297✔
1077
  }
1078

1079
  // to .sma file
1080
  tBufferClear(&buffers[0]);
1081
  for (int32_t i = 0; i < bData->nColData; ++i) {
1,557,802,145✔
1082
    SColData *colData = bData->aColData + i;
1,315,024,377✔
1083
    if ((colData->cflag & COL_SMA_ON) == 0 || ((colData->flag & HAS_VALUE) == 0)) continue;
1,315,023,810✔
1084

1085
    SColumnDataAgg sma[1] = {{.colId = colData->cid}};
1,274,828,867✔
1086
    tColDataCalcSMA[colData->type](colData, sma);
1,274,817,933✔
1087

1088
    TAOS_CHECK_GOTO(tPutColumnDataAgg(&buffers[0], sma), &lino, _exit);
1,274,849,068!
1089
  }
1090
  record->smaSize = buffers[0].size;
242,772,631✔
1091

1092
  if (record->smaSize > 0) {
242,772,631!
1093
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_SMA], record->smaOffset, buffers[0].data, record->smaSize,
242,775,704!
1094
                                  encryptAlgorithm, encryptKey),
1095
                    &lino, _exit);
1096
    writer->files[TSDB_FTYPE_SMA].size += record->smaSize;
242,763,785✔
1097
  }
1098

1099
  // append SBrinRecord
1100
  TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
242,767,536!
1101

1102
  tBlockDataClear(bData);
242,749,730✔
1103

1104
_exit:
242,735,317✔
1105
  if (code) {
242,745,896!
1106
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1107
              tstrerror(code));
1108
  }
1109
  taosHashCleanup(cmprInfo.pColCmpr);
242,745,896✔
1110
  return code;
242,738,843✔
1111
}
1112

1113
static int32_t tsdbDataFileDoWriteTSRow(SDataFileWriter *writer, TSDBROW *row) {
2,147,483,647✔
1114
  int32_t code = 0;
2,147,483,647✔
1115
  int32_t lino = 0;
2,147,483,647✔
1116

1117
  // update/append
1118
  if (row->type == TSDBROW_ROW_FMT) {
2,147,483,647!
1119
    TAOS_CHECK_GOTO(
×
1120
        tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid, TSDBROW_SVERSION(row), writer->config->skmRow), &lino,
1121
        _exit);
1122
  }
1123

1124
  if (TSDBROW_VERSION(row) <= writer->config->compactVersion  //
2,147,483,647!
1125
      && writer->blockData->nRow > 0                          //
2,147,483,647✔
1126
      &&
2,147,483,647✔
1127
      tsdbRowCompareWithoutVersion(row, &tsdbRowFromBlockData(writer->blockData, writer->blockData->nRow - 1)) == 0  //
2,147,483,647✔
1128
  ) {
1129
    TAOS_CHECK_GOTO(tBlockDataUpdateRow(writer->blockData, row, writer->config->skmRow->pTSchema), &lino, _exit);
2,147,483,647!
1130
  } else {
1131
    if (writer->blockData->nRow >= writer->config->maxRow) {
2,147,483,647✔
1132
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
19,916,566!
1133
    }
1134

1135
    TAOS_CHECK_GOTO(
2,147,483,647!
1136
        tBlockDataAppendRow(writer->blockData, row, writer->config->skmRow->pTSchema, writer->ctx->tbid->uid), &lino,
1137
        _exit);
1138
  }
1139

1140
_exit:
2,147,483,647✔
1141
  if (code) {
2,147,483,647!
1142
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1143
              tstrerror(code));
1144
  }
1145
  return code;
2,147,483,647✔
1146
}
1147

1148
static FORCE_INLINE int32_t tsdbRowKeyCmprNullAsLargest(const STsdbRowKey *key1, const STsdbRowKey *key2) {
1149
  if (key1 == NULL) {
2,147,483,647!
1150
    return 1;
3,763,838✔
1151
  } else if (key2 == NULL) {
2,147,483,647!
1152
    return -1;
41,560,655✔
1153
  } else {
1154
    return tsdbRowKeyCmpr(key1, key2);
2,147,483,647✔
1155
  }
1156
}
1157

1158
static int32_t tsdbDataFileDoWriteTableOldData(SDataFileWriter *writer, const STsdbRowKey *key) {
2,147,483,647✔
1159
  if (writer->ctx->tbHasOldData == false) {
2,147,483,647!
1160
    return 0;
×
1161
  }
1162

1163
  int32_t     code = 0;
2,147,483,647✔
1164
  int32_t     lino = 0;
2,147,483,647✔
1165
  STsdbRowKey rowKey;
2,147,483,647✔
1166

1167
  for (;;) {
59,327✔
1168
    for (;;) {
1169
      // SBlockData
1170
      for (; writer->ctx->blockDataIdx < writer->ctx->blockData->nRow; writer->ctx->blockDataIdx++) {
2,147,483,647✔
1171
        TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
2,147,483,647✔
1172

1173
        tsdbRowGetKey(&row, &rowKey);
2,147,483,647✔
1174
        if (tsdbRowKeyCmprNullAsLargest(&rowKey, key) < 0) {  // key <= rowKey
2,147,483,647✔
1175
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, &row), &lino, _exit);
2,147,483,647!
1176
        } else {
1177
          goto _exit;
2,147,483,647✔
1178
        }
1179
      }
1180

1181
      // SBrinBlock
1182
      if (writer->ctx->brinBlockIdx >= writer->ctx->brinBlock->numOfRecords) {
51,316,176✔
1183
        break;
1,280,218✔
1184
      }
1185

1186
      for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
67,838,749✔
1187
        SBrinRecord record;
67,227,989✔
1188
        code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record);
67,227,989✔
1189
        TSDB_CHECK_CODE(code, lino, _exit);
67,227,989!
1190
        if (record.uid != writer->ctx->tbid->uid) {
67,227,989✔
1191
          writer->ctx->tbHasOldData = false;
1,230,141✔
1192
          goto _exit;
1,230,141✔
1193
        }
1194

1195
        if (tsdbRowKeyCmprNullAsLargest(key, &record.firstKey) < 0) {  // key < record->firstKey
65,997,848✔
1196
          goto _exit;
31,035,766✔
1197
        } else {
1198
          SBrinRecord record[1];
34,962,082✔
1199
          code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
34,962,082✔
1200
          TSDB_CHECK_CODE(code, lino, _exit);
34,962,082!
1201
          if (tsdbRowKeyCmprNullAsLargest(key, &record->lastKey) > 0) {  // key > record->lastKey
34,962,082✔
1202
            if (writer->blockData->nRow > 0) {
17,802,791✔
1203
              TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
11,791!
1204
            }
1205

1206
            TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
17,802,791!
1207
          } else {
1208
            TAOS_CHECK_GOTO(tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData), &lino,
17,159,291!
1209
                            _exit);
1210

1211
            writer->ctx->blockDataIdx = 0;
17,159,291✔
1212
            writer->ctx->brinBlockIdx++;
17,159,291✔
1213
            break;
17,159,291✔
1214
          }
1215
        }
1216
      }
1217
    }
1218

1219
    // SBrinBlk
1220
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
1,280,218✔
1221
      writer->ctx->brinBlkArray = NULL;
1,220,891✔
1222
      writer->ctx->tbHasOldData = false;
1,220,891✔
1223
      goto _exit;
1,220,891✔
1224
    } else {
1225
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
59,327✔
1226

1227
      if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
59,327!
1228
        writer->ctx->tbHasOldData = false;
×
1229
        goto _exit;
×
1230
      }
1231

1232
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
59,327!
1233

1234
      writer->ctx->brinBlockIdx = 0;
59,327✔
1235
      writer->ctx->brinBlkArrayIdx++;
59,327✔
1236
    }
1237
  }
1238

1239
_exit:
2,147,483,647✔
1240
  if (code) {
2,147,483,647!
1241
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1242
              tstrerror(code));
1243
  }
1244
  return code;
2,147,483,647✔
1245
}
1246

1247
static int32_t tsdbDataFileDoWriteTSData(SDataFileWriter *writer, TSDBROW *row) {
2,147,483,647✔
1248
  int32_t code = 0;
2,147,483,647✔
1249
  int32_t lino = 0;
2,147,483,647✔
1250

1251
  if (writer->ctx->tbHasOldData) {
2,147,483,647!
1252
    STsdbRowKey key;
2,147,483,647✔
1253
    tsdbRowGetKey(row, &key);
2,147,483,647✔
1254
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
2,147,483,647!
1255
  }
1256

1257
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, row), &lino, _exit);
2,147,483,647!
1258

1259
_exit:
2,147,483,647✔
1260
  if (code) {
2,147,483,647!
1261
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1262
              tstrerror(code));
1263
  }
1264
  return code;
2,147,483,647✔
1265
}
1266

1267
static int32_t tsdbDataFileWriteTableDataEnd(SDataFileWriter *writer) {
126,184,400✔
1268
  if (writer->ctx->tbid->uid == 0) {
126,184,400✔
1269
    return 0;
5,426,520✔
1270
  }
1271

1272
  int32_t code = 0;
120,758,179✔
1273
  int32_t lino = 0;
120,758,179✔
1274

1275
  if (writer->ctx->tbHasOldData) {
120,757,880!
1276
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, NULL /* as the largest key */), &lino, _exit);
1,300,930!
1277
  }
1278

1279
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
120,762,911!
1280

1281
_exit:
120,762,541✔
1282
  if (code) {
120,760,584!
1283
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1284
              tstrerror(code));
1285
  }
1286
  return code;
120,757,809✔
1287
}
1288

1289
static int32_t tsdbDataFileWriteTableDataBegin(SDataFileWriter *writer, const TABLEID *tbid) {
126,180,435✔
1290
  int32_t code = 0;
126,180,435✔
1291
  int32_t lino = 0;
126,180,435✔
1292

1293
  SMetaInfo info;
126,180,435✔
1294
  bool      drop = false;
126,175,774✔
1295
  TABLEID   tbid1[1];
126,175,774✔
1296
  writer->ctx->tbHasOldData = false;
126,175,774✔
1297
  while (writer->ctx->brinBlkArray) {  // skip data of previous table
130,949,232✔
1298
    for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
833,845,727✔
1299
      SBrinRecord record;
828,385,320✔
1300
      TAOS_CHECK_GOTO(tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record), &lino, _exit);
828,385,320!
1301

1302
      if (record.uid == tbid->uid) {
828,388,655✔
1303
        writer->ctx->tbHasOldData = true;
2,451,032✔
1304
        goto _begin;
2,451,032✔
1305
      } else if (record.suid > tbid->suid || (record.suid == tbid->suid && record.uid > tbid->uid)) {
825,934,104✔
1306
        goto _begin;
37,061,997✔
1307
      } else {
1308
        if (record.uid != writer->ctx->tbid->uid) {
788,873,864✔
1309
          if (drop && tbid1->uid == record.uid) {
277,747,458!
1310
            continue;
×
1311
          } else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, record.uid, &info, NULL) != 0) {
277,747,458✔
1312
            drop = true;
29,388✔
1313
            tbid1->suid = record.suid;
29,388✔
1314
            tbid1->uid = record.uid;
29,388✔
1315
            continue;
29,388✔
1316
          } else {
1317
            drop = false;
277,718,070✔
1318
            writer->ctx->tbid->suid = record.suid;
277,718,070✔
1319
            writer->ctx->tbid->uid = record.uid;
277,718,070✔
1320
          }
1321
        }
1322

1323
        TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, &record), &lino, _exit);
788,844,281!
1324
      }
1325
    }
1326

1327
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
5,458,834✔
1328
      writer->ctx->brinBlkArray = NULL;
689,341✔
1329
      break;
689,341✔
1330
    } else {
1331
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
4,769,493✔
1332

1333
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
4,769,493!
1334

1335
      writer->ctx->brinBlockIdx = 0;
4,769,493✔
1336
      writer->ctx->brinBlkArrayIdx++;
4,769,493✔
1337
    }
1338
  }
1339

1340
_begin:
126,179,369✔
1341
  writer->ctx->tbid[0] = *tbid;
126,186,233✔
1342

1343
  if (tbid->uid == INT64_MAX) {
126,184,854✔
1344
    goto _exit;
5,426,520✔
1345
  }
1346

1347
  TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, tbid, writer->config->skmTb), &lino, _exit);
120,759,343!
1348
  TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, writer->ctx->tbid, writer->config->skmTb->pTSchema, NULL, 0), &lino,
120,756,865!
1349
                  _exit);
1350

1351
_exit:
126,186,915✔
1352
  if (code) {
126,187,981!
1353
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1354
              tstrerror(code));
1355
  }
1356
  return code;
126,187,981✔
1357
}
1358

1359
int32_t tsdbFileWriteHeadFooter(STsdbFD *fd, int64_t *fileSize, const SHeadFooter *footer, int32_t encryptAlgorithm,
5,426,520✔
1360
                                char *encryptKey) {
1361
  TAOS_CHECK_RETURN(
5,426,520!
1362
      tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptAlgorithm, encryptKey));
1363
  *fileSize += sizeof(*footer);
5,426,520✔
1364
  return 0;
5,426,520✔
1365
}
1366

1367
int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
12,030,534✔
1368
                               TTombBlkArray *tombBlkArray, SBuffer *buffers, SVersionRange *range,
1369
                               int32_t encryptAlgorithm, char *encryptKey) {
1370
  int32_t code;
1371

1372
  if (TOMB_BLOCK_SIZE(tombBlock) == 0) {
12,030,534!
1373
    return 0;
×
1374
  }
1375

1376
  SBuffer *buffer0 = buffers + 0;
12,030,534✔
1377
  SBuffer *assist = buffers + 1;
12,030,534✔
1378

1379
  STombBlk tombBlk = {
12,030,534✔
1380
      .dp[0] =
1381
          {
1382
              .offset = *fileSize,
12,030,534✔
1383
              .size = 0,
1384
          },
1385
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
12,030,534✔
1386
      .cmprAlg = cmprAlg,
1387
  };
1388
  for (int i = 0; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
294,601,135✔
1389
    STombRecord record;
282,570,601✔
1390
    TAOS_CHECK_RETURN(tTombBlockGet(tombBlock, i, &record));
282,570,601!
1391

1392
    if (i == 0) {
282,570,601✔
1393
      tombBlk.minTbid.suid = record.suid;
12,030,534✔
1394
      tombBlk.minTbid.uid = record.uid;
12,030,534✔
1395
      tombBlk.minVer = record.version;
12,030,534✔
1396
      tombBlk.maxVer = record.version;
12,030,534✔
1397
    }
1398
    if (i == TOMB_BLOCK_SIZE(tombBlock) - 1) {
282,570,601✔
1399
      tombBlk.maxTbid.suid = record.suid;
12,030,534✔
1400
      tombBlk.maxTbid.uid = record.uid;
12,030,534✔
1401
    }
1402
    if (record.version < tombBlk.minVer) {
282,570,601✔
1403
      tombBlk.minVer = record.version;
21,187✔
1404
    }
1405
    if (record.version > tombBlk.maxVer) {
282,570,601✔
1406
      tombBlk.maxVer = record.version;
254,722,724✔
1407
    }
1408
  }
1409

1410
  tsdbWriterUpdVerRange(range, tombBlk.minVer, tombBlk.maxVer);
12,030,534✔
1411

1412
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); i++) {
72,183,204✔
1413
    tBufferClear(buffer0);
1414

1415
    SCompressInfo cinfo = {
60,152,670✔
1416
        .cmprAlg = cmprAlg,
1417
        .dataType = TSDB_DATA_TYPE_BIGINT,
1418
        .originalSize = tombBlock->buffers[i].size,
60,152,670✔
1419
    };
1420
    TAOS_CHECK_RETURN(tCompressDataToBuffer(tombBlock->buffers[i].data, &cinfo, buffer0, assist));
60,152,670!
1421
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptAlgorithm, encryptKey));
60,152,670!
1422

1423
    tombBlk.size[i] = cinfo.compressedSize;
60,152,670✔
1424
    tombBlk.dp->size += tombBlk.size[i];
60,152,670✔
1425
    *fileSize += tombBlk.size[i];
60,152,670✔
1426
  }
1427

1428
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(tombBlkArray, &tombBlk));
24,061,068!
1429

1430
  tTombBlockClear(tombBlock);
12,030,534✔
1431
  return 0;
12,030,534✔
1432
}
1433

1434
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
5,426,520✔
1435
  int32_t code = 0;
5,426,520✔
1436
  int32_t lino = 0;
5,426,520✔
1437

1438
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
5,424,559✔
1439
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
5,426,520✔
1440

1441
  TAOS_CHECK_GOTO(tsdbFileWriteHeadFooter(writer->fd[TSDB_FTYPE_HEAD], &writer->files[TSDB_FTYPE_HEAD].size,
5,424,559!
1442
                                          writer->headFooter, encryptAlgorithm, encryptKey),
1443
                  &lino, _exit);
1444

1445
_exit:
5,426,520✔
1446
  if (code) {
5,426,520!
1447
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1448
              tstrerror(code));
1449
  }
1450
  return code;
5,426,520✔
1451
}
1452

1453
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
1,045,202✔
1454
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
1,045,202!
1455

1456
  int32_t code = 0;
1,045,202✔
1457
  int32_t lino = 0;
1,045,202✔
1458

1459
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
1,045,202✔
1460
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
1,045,202✔
1461

1462
  TAOS_CHECK_GOTO(tsdbFileWriteTombBlock(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlock, writer->config->cmprAlg,
1,045,202!
1463
                                         &writer->files[TSDB_FTYPE_TOMB].size, writer->tombBlkArray, writer->buffers,
1464
                                         &writer->ctx->tombRange, encryptAlgorithm, encryptKey),
1465
                  &lino, _exit);
1466

1467
_exit:
1,045,202✔
1468
  if (code) {
1,045,202!
1469
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1470
              tstrerror(code));
1471
  }
1472
  return code;
1,045,202✔
1473
}
1474

1475
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize,
218,220,842✔
1476
                             int32_t encryptAlgorithm, char *encryptKey) {
1477
  ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
218,220,842✔
1478
  if (ptr->size > 0) {
218,295,810✔
1479
    ptr->offset = *fileSize;
12,030,534✔
1480

1481
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(tombBlkArray), ptr->size,
12,030,534!
1482
                                    encryptAlgorithm, encryptKey));
1483

1484
    *fileSize += ptr->size;
12,030,534✔
1485
  }
1486
  return 0;
218,272,285✔
1487
}
1488

1489
static int32_t tsdbDataFileDoWriteTombBlk(SDataFileWriter *writer) {
1,045,202✔
1490
  if (TARRAY2_SIZE(writer->tombBlkArray) <= 0) {
1,045,202!
1491
    return TSDB_CODE_INVALID_PARA;
×
1492
  }
1493

1494
  int32_t code = 0;
1,045,202✔
1495
  int32_t lino = 0;
1,045,202✔
1496

1497
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
1,045,202✔
1498
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
1,045,202✔
1499

1500
  TAOS_CHECK_GOTO(
1,045,202!
1501
      tsdbFileWriteTombBlk(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlkArray, writer->tombFooter->tombBlkPtr,
1502
                           &writer->files[TSDB_FTYPE_TOMB].size, encryptAlgorithm, encryptKey),
1503
      &lino, _exit);
1504

1505
_exit:
1,045,202✔
1506
  if (code) {
1,045,202!
1507
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1508
              tstrerror(code));
1509
  }
1510
  return code;
1,045,202✔
1511
}
1512

1513
int32_t tsdbFileWriteTombFooter(STsdbFD *fd, const STombFooter *footer, int64_t *fileSize, int32_t encryptAlgorithm,
1,045,202✔
1514
                                char *encryptKey) {
1515
  TAOS_CHECK_RETURN(
1,045,202!
1516
      tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptAlgorithm, encryptKey));
1517
  *fileSize += sizeof(*footer);
1,045,202✔
1518
  return 0;
1,045,202✔
1519
}
1520

1521
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
1,045,202✔
1522
  int32_t code = 0;
1,045,202✔
1523
  int32_t lino = 0;
1,045,202✔
1524

1525
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
1,045,202✔
1526
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
1,045,202✔
1527

1528
  TAOS_CHECK_GOTO(tsdbFileWriteTombFooter(writer->fd[TSDB_FTYPE_TOMB], writer->tombFooter,
1,045,202!
1529
                                          &writer->files[TSDB_FTYPE_TOMB].size, encryptAlgorithm, encryptKey),
1530
                  &lino, _exit);
1531

1532
_exit:
1,045,202✔
1533
  if (code) {
1,045,202!
1534
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1535
              tstrerror(code));
1536
  }
1537
  return code;
1,045,202✔
1538
}
1539

1540
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
3,967,575✔
1541
  int32_t code = 0;
3,967,575✔
1542
  int32_t lino = 0;
3,967,575✔
1543

1544
  while (writer->ctx->hasOldTomb) {
4,698,305!
1545
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
236,613,272✔
1546
      STombRecord record1[1];
235,151,812✔
1547
      TAOS_CHECK_GOTO(tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1), &lino, _exit);
235,151,812!
1548

1549
      int32_t c = tTombRecordCompare(record, record1);
235,151,812✔
1550
      if (c < 0) {
235,151,812!
1551
        goto _write;
×
1552
      } else if (c > 0) {
235,151,812!
1553
        TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record1), &lino, _exit);
235,151,812!
1554

1555
        tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
235,151,812!
1556
                  ", version:%" PRId64,
1557
                  TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid,
1558
                  record1->suid, record1->uid, record1->version);
1559

1560
        if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
235,151,812!
1561
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1562
        }
1563
      } else {
1564
        tsdbError("vgId:%d duplicate tomb record, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64 ", version:%" PRId64,
×
1565
                  TD_VID(writer->config->tsdb->pVnode), writer->config->cid, record->suid, record->uid,
1566
                  record->version);
1567
      }
1568
    }
1569

1570
    if (writer->ctx->tombBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->tombBlkArray)) {
1,461,460✔
1571
      writer->ctx->hasOldTomb = false;
730,730✔
1572
      break;
730,730✔
1573
    } else {
1574
      const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);
730,730✔
1575

1576
      TAOS_CHECK_GOTO(tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock), &lino, _exit);
730,730!
1577

1578
      writer->ctx->tombBlockIdx = 0;
730,730✔
1579
      writer->ctx->tombBlkArrayIdx++;
730,730✔
1580
    }
1581
  }
1582

1583
_write:
3,967,575✔
1584
  if (record->suid == INT64_MAX) {
3,967,575✔
1585
    goto _exit;
1,045,202✔
1586
  }
1587

1588
  TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
2,922,373!
1589

1590
  tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
2,922,373!
1591
            ", version:%" PRId64,
1592
            TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid, record->suid,
1593
            record->uid, record->version);
1594

1595
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
2,922,373!
1596
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1597
  }
1598

1599
_exit:
3,967,575✔
1600
  if (code) {
3,967,575!
1601
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1602
              tstrerror(code));
1603
  }
1604
  return code;
3,967,575✔
1605
}
1606

1607
int32_t tsdbFileWriteBrinBlk(STsdbFD *fd, TBrinBlkArray *brinBlkArray, SFDataPtr *ptr, int64_t *fileSize,
5,426,520✔
1608
                             int32_t encryptAlgorithm, char *encryptKey) {
1609
  if (TARRAY2_SIZE(brinBlkArray) <= 0) {
5,426,520!
1610
    return TSDB_CODE_INVALID_PARA;
×
1611
  }
1612
  ptr->offset = *fileSize;
5,426,520✔
1613
  ptr->size = TARRAY2_DATA_LEN(brinBlkArray);
5,426,520✔
1614

1615
  TAOS_CHECK_RETURN(
5,426,520!
1616
      tsdbWriteFile(fd, ptr->offset, (uint8_t *)TARRAY2_DATA(brinBlkArray), ptr->size, encryptAlgorithm, encryptKey));
1617

1618
  *fileSize += ptr->size;
5,426,520✔
1619
  return 0;
5,426,520✔
1620
}
1621

1622
static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
5,426,520✔
1623
  int32_t code = 0;
5,426,520✔
1624
  int32_t lino = 0;
5,426,520✔
1625

1626
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
5,426,520✔
1627
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
5,426,520✔
1628

1629
  TAOS_CHECK_GOTO(
5,426,520!
1630
      tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
1631
                           &writer->files[TSDB_FTYPE_HEAD].size, encryptAlgorithm, encryptKey),
1632
      &lino, _exit);
1633

1634
_exit:
5,426,520✔
1635
  if (code) {
5,426,520!
1636
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1637
              tstrerror(code));
1638
  }
1639
  return code;
5,426,520✔
1640
}
1641

1642
void tsdbTFileUpdVerRange(STFile *f, SVersionRange range) {
240,809,636✔
1643
  f->minVer = TMIN(f->minVer, range.minVer);
240,809,636✔
1644
  f->maxVer = TMAX(f->maxVer, range.maxVer);
240,980,947✔
1645
}
241,016,472✔
1646

1647
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
6,452,304✔
1648
  int32_t code = 0;
6,452,304✔
1649
  int32_t lino = 0;
6,452,304✔
1650

1651
  int32_t  ftype;
1652
  STFileOp op;
6,452,304✔
1653

1654
  if (writer->fd[TSDB_FTYPE_HEAD]) {
6,452,304✔
1655
    TABLEID tbid[1] = {{
5,426,520✔
1656
        .suid = INT64_MAX,
1657
        .uid = INT64_MAX,
1658
    }};
1659

1660
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
5,426,520!
1661
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, tbid), &lino, _exit);
5,426,520!
1662
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
5,426,520!
1663
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlk(writer), &lino, _exit);
5,426,520!
1664
    TAOS_CHECK_GOTO(tsdbDataFileWriteHeadFooter(writer), &lino, _exit);
5,426,520!
1665

1666
    SVersionRange ofRange = {.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
5,426,520✔
1667

1668
    // .head
1669
    ftype = TSDB_FTYPE_HEAD;
5,426,520✔
1670
    if (writer->config->files[ftype].exist) {
5,426,520!
1671
      op = (STFileOp){
1,910,232✔
1672
          .optype = TSDB_FOP_REMOVE,
1673
          .fid = writer->config->fid,
1,910,232✔
1674
          .of = writer->config->files[ftype].file,
1,910,232✔
1675
      };
1676
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
1,910,232✔
1677
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
3,820,464!
1678
    }
1679
    op = (STFileOp){
5,424,559✔
1680
        .optype = TSDB_FOP_CREATE,
1681
        .fid = writer->config->fid,
5,426,520✔
1682
        .nf = writer->files[ftype],
5,426,520✔
1683
    };
1684
    tsdbTFileUpdVerRange(&op.nf, ofRange);
5,424,559✔
1685
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
5,426,520✔
1686
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
10,851,079!
1687

1688
    // .data
1689
    ftype = TSDB_FTYPE_DATA;
5,426,520✔
1690
    if (!writer->config->files[ftype].exist) {
5,426,520!
1691
      op = (STFileOp){
3,516,288✔
1692
          .optype = TSDB_FOP_CREATE,
1693
          .fid = writer->config->fid,
3,516,288✔
1694
          .nf = writer->files[ftype],
3,516,288✔
1695
      };
1696
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
3,516,288✔
1697
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
7,028,654!
1698
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
1,910,232!
1699
      op = (STFileOp){
1,910,232✔
1700
          .optype = TSDB_FOP_MODIFY,
1701
          .fid = writer->config->fid,
1,910,232✔
1702
          .of = writer->config->files[ftype].file,
1,910,232✔
1703
          .nf = writer->files[ftype],
1,910,232✔
1704
      };
1705
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
1,910,232✔
1706
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
3,820,464!
1707
    }
1708

1709
    // .sma
1710
    ftype = TSDB_FTYPE_SMA;
5,424,559✔
1711
    if (!writer->config->files[ftype].exist) {
5,424,559!
1712
      op = (STFileOp){
3,516,288✔
1713
          .optype = TSDB_FOP_CREATE,
1714
          .fid = writer->config->fid,
3,514,327✔
1715
          .nf = writer->files[ftype],
3,516,288✔
1716
      };
1717
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
3,516,288✔
1718
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
7,028,654!
1719
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
1,910,232✔
1720
      op = (STFileOp){
1,910,232✔
1721
          .optype = TSDB_FOP_MODIFY,
1722
          .fid = writer->config->fid,
1,910,232✔
1723
          .of = writer->config->files[ftype].file,
1,910,232✔
1724
          .nf = writer->files[ftype],
1,910,232✔
1725
      };
1726
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
1,910,232✔
1727
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
3,820,464!
1728
    }
1729
  }
1730

1731
  if (writer->fd[TSDB_FTYPE_TOMB]) {
6,452,304✔
1732
    STombRecord record[1] = {{
1,045,202✔
1733
        .suid = INT64_MAX,
1734
        .uid = INT64_MAX,
1735
        .version = INT64_MAX,
1736
    }};
1737

1738
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
1,045,202!
1739
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
1,045,202!
1740
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlk(writer), &lino, _exit);
1,045,202!
1741
    TAOS_CHECK_GOTO(tsdbDataFileWriteTombFooter(writer), &lino, _exit);
1,045,202!
1742

1743
    SVersionRange ofRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
1,045,202✔
1744

1745
    ftype = TSDB_FTYPE_TOMB;
1,045,202✔
1746
    if (writer->config->files[ftype].exist) {
1,045,202!
1747
      op = (STFileOp){
730,730✔
1748
          .optype = TSDB_FOP_REMOVE,
1749
          .fid = writer->config->fid,
730,730✔
1750
          .of = writer->config->files[ftype].file,
730,730✔
1751
      };
1752
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
730,730✔
1753
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
1,461,460!
1754
    }
1755
    op = (STFileOp){
1,045,202✔
1756
        .optype = TSDB_FOP_CREATE,
1757
        .fid = writer->config->fid,
1,045,202✔
1758
        .nf = writer->files[ftype],
1,045,202✔
1759
    };
1760
    tsdbTFileUpdVerRange(&op.nf, ofRange);
1,045,202✔
1761
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->tombRange);
1,045,202✔
1762
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2,090,404!
1763
  }
1764
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
6,452,304✔
1765
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
6,452,304✔
1766
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
32,258,363✔
1767
    if (writer->fd[i]) {
25,808,020✔
1768
      TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd[i], encryptAlgorithm, encryptKey), &lino, _exit);
17,321,605!
1769
      tsdbCloseFile(&writer->fd[i]);
17,324,762✔
1770
    }
1771
  }
1772

1773
_exit:
6,450,343✔
1774
  if (code) {
6,450,343!
1775
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1776
              tstrerror(code));
1777
  }
1778
  return code;
6,450,343✔
1779
}
1780

1781
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
5,426,520✔
1782
  int32_t code = 0;
5,426,520✔
1783
  int32_t lino = 0;
5,426,520✔
1784

1785
  int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};
5,426,520✔
1786

1787
  for (int32_t i = 0; i < ARRAY_SIZE(ftypes); ++i) {
21,701,846✔
1788
    int32_t ftype = ftypes[i];
16,275,326✔
1789

1790
    char    fname[TSDB_FILENAME_LEN];
16,276,712✔
1791
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;
16,277,287✔
1792

1793
    if (writer->files[ftype].size == 0) {
16,277,287✔
1794
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
12,458,784✔
1795
    }
1796

1797
    int32_t lcn = writer->files[ftype].lcn;
16,276,975✔
1798
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
16,279,248✔
1799
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
16,277,599!
1800

1801
    if (writer->files[ftype].size == 0) {
16,278,985✔
1802
      uint8_t hdr[TSDB_FHDR_SIZE] = {0};
12,456,560✔
1803

1804
      int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
12,454,599✔
1805
      char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
12,457,135✔
1806

1807
      TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, encryptAlgorithm, encryptKey), &lino,
12,457,135!
1808
                      _exit);
1809

1810
      writer->files[ftype].size += TSDB_FHDR_SIZE;
12,454,867✔
1811
    }
1812
  }
1813

1814
  if (writer->ctx->reader) {
5,426,520✔
1815
    TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray), &lino, _exit);
1,910,232!
1816
  }
1817

1818
_exit:
5,425,945✔
1819
  if (code) {
5,426,520!
1820
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1821
              tstrerror(code));
1822
  }
1823
  return code;
5,426,520✔
1824
}
1825

1826
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
9,560,872✔
1827
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
9,560,872!
1828
  if (!writer[0]) {
9,581,348!
1829
    return terrno;
×
1830
  }
1831

1832
  writer[0]->config[0] = config[0];
9,581,348✔
1833
  return 0;
9,582,544✔
1834
}
1835

1836
int32_t tsdbDataFileWriterClose(SDataFileWriter **writer, bool abort, TFileOpArray *opArr) {
9,582,544✔
1837
  if (writer[0] == NULL) return 0;
9,582,544!
1838

1839
  int32_t code = 0;
9,582,544✔
1840
  int32_t lino = 0;
9,582,544✔
1841

1842
  if (writer[0]->ctx->opened) {
9,582,544!
1843
    if (abort) {
6,452,304!
1844
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseAbort(writer[0]), &lino, _exit);
×
1845
    } else {
1846
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseCommit(writer[0], opArr), &lino, _exit);
6,452,304!
1847
    }
1848
    tsdbDataFileWriterDoClose(writer[0]);
6,450,343✔
1849
  }
1850
  taosMemoryFree(writer[0]);
9,580,583!
1851
  writer[0] = NULL;
9,581,969✔
1852

1853
_exit:
9,581,969✔
1854
  if (code) {
9,581,969!
1855
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer[0]->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1856
              tstrerror(code));
1857
  }
1858
  return code;
9,580,583✔
1859
}
1860

1861
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
2,147,483,647✔
1862
  int32_t code = 0;
2,147,483,647✔
1863
  int32_t lino = 0;
2,147,483,647✔
1864

1865
  if (!writer->ctx->opened) {
2,147,483,647!
1866
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
719,248!
1867
  }
1868

1869
  if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
2,147,483,647✔
1870
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
719,248!
1871
  }
1872

1873
  if (row->uid != writer->ctx->tbid->uid) {
2,147,483,647✔
1874
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
1,458,570!
1875
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row), &lino, _exit);
1,458,570!
1876
  }
1877

1878
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, &row->row), &lino, _exit);
2,147,483,647!
1879

1880
_exit:
2,147,483,647✔
1881
  if (code) {
2,147,483,647!
1882
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1883
              tstrerror(code));
1884
  }
1885
  return code;
2,147,483,647✔
1886
}
1887

1888
int32_t tsdbDataFileWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
164,923,348✔
1889
  if (bData->nRow == 0) {
164,923,348!
1890
    return 0;
×
1891
  }
1892

1893
  int32_t code = 0;
164,922,652✔
1894
  int32_t lino = 0;
164,922,652✔
1895

1896
  if (!bData->uid) {
164,923,718!
1897
    return TSDB_CODE_INVALID_PARA;
×
1898
  }
1899

1900
  if (!writer->ctx->opened) {
164,925,551!
1901
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
4,697,061!
1902
  }
1903

1904
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
164,927,313✔
1905
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
4,707,272!
1906
  }
1907

1908
  if (bData->uid != writer->ctx->tbid->uid) {
164,931,278✔
1909
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
119,305,037!
1910
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData), &lino, _exit);
119,299,239!
1911
  }
1912

1913
  if (writer->ctx->tbHasOldData) {
164,920,012!
1914
    STsdbRowKey key;
14,864,469✔
1915

1916
    tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &key);
14,864,469✔
1917
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
14,864,469!
1918
  }
1919

1920
  if (!writer->ctx->tbHasOldData       //
164,920,187!
1921
      && writer->blockData->nRow == 0  //
151,144,564✔
1922
  ) {
1923
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, bData), &lino, _exit);
150,206,718!
1924

1925
  } else {
1926
    for (int32_t i = 0; i < bData->nRow; ++i) {
2,147,483,647✔
1927
      TSDBROW row[1] = {tsdbRowFromBlockData(bData, i)};
2,147,483,647✔
1928
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, row), &lino, _exit);
2,147,483,647!
1929
    }
1930
  }
1931

1932
_exit:
164,818,126✔
1933
  if (code) {
164,895,774!
1934
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1935
              tstrerror(code));
1936
  }
1937
  return code;
164,895,703✔
1938
}
1939

1940
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
36,792,796✔
1941
  if (!writer->ctx->opened) {
36,792,796!
1942
    return TSDB_CODE_INVALID_PARA;
×
1943
  }
1944

1945
  if (writer->blockData->nRow == 0) return 0;
36,792,796!
1946
  if (writer->ctx->tbHasOldData) return 0;
36,792,796!
1947

1948
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
35,642,799✔
1949
}
1950

1951
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
1,045,202✔
1952
  int32_t code = 0;
1,045,202✔
1953
  int32_t lino = 0;
1,045,202✔
1954

1955
  char    fname[TSDB_FILENAME_LEN];
1,045,202✔
1956
  int32_t ftype = TSDB_FTYPE_TOMB;
1,045,202✔
1957

1958
  int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
1,045,202✔
1959

1960
  int32_t lcn = writer->files[ftype].lcn;
1,045,202✔
1961
  tsdbTFileName(writer->config->tsdb, writer->files + ftype, fname);
1,045,202✔
1962

1963
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
1,045,202!
1964

1965
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
1,045,202✔
1966
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
1,045,202✔
1967
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
1,045,202✔
1968

1969
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, encryptAlgorithm, encryptKey), &lino, _exit);
1,045,202!
1970
  writer->files[ftype].size += TSDB_FHDR_SIZE;
1,045,202✔
1971

1972
  if (writer->ctx->reader) {
1,045,202✔
1973
    TAOS_CHECK_GOTO(tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray), &lino, _exit);
1,017,349!
1974

1975
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
1,017,349✔
1976
      writer->ctx->hasOldTomb = true;
730,730✔
1977
    }
1978

1979
    writer->ctx->tombBlkArrayIdx = 0;
1,017,349✔
1980
    tTombBlockClear(writer->ctx->tombBlock);
1,017,349✔
1981
    writer->ctx->tombBlockIdx = 0;
1,017,349✔
1982
  }
1983

1984
_exit:
1,045,202✔
1985
  if (code) {
1,045,202!
1986
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1987
              tstrerror(code));
1988
  }
1989
  return code;
1,045,202✔
1990
}
1991

1992
int32_t tsdbDataFileWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
2,922,373✔
1993
  int32_t code = 0;
2,922,373✔
1994
  int32_t lino = 0;
2,922,373✔
1995

1996
  if (!writer->ctx->opened) {
2,922,373!
1997
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
1,035,995!
1998
  }
1999

2000
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
2,922,373✔
2001
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenTombFD(writer), &lino, _exit);
1,045,202!
2002
  }
2003

2004
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
2,922,373!
2005

2006
_exit:
2,922,373✔
2007
  if (code) {
2,922,373!
2008
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
2009
              tstrerror(code));
2010
  }
2011
  return code;
2,922,373✔
2012
}
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