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

taosdata / TDengine / #4997

20 Mar 2026 06:10AM UTC coverage: 71.739% (-0.3%) from 72.069%
#4997

push

travis-ci

web-flow
feat: add query phase tracking for SHOW QUERIES (#34706)

148 of 183 new or added lines in 10 files covered. (80.87%)

9273 existing lines in 172 files now uncovered.

244572 of 340921 relevant lines covered (71.74%)

133392941.95 hits per line

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

91.33
/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) {
25,746,036✔
42
  if (reader->ctx->headFooterLoaded) {
25,746,036✔
43
    return 0;
×
44
  }
45

46
  int32_t code = 0;
25,751,058✔
47
  int32_t lino = 0;
25,751,058✔
48

49
  int32_t ftype = TSDB_FTYPE_HEAD;
25,742,879✔
50
  if (reader->fd[ftype]) {
25,742,879✔
51
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
22,920,209✔
52

53
#if 1
54
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
22,920,230✔
55
                                 (uint8_t *)reader->headFooter, sizeof(SHeadFooter), 0, pEncryptData),
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;
25,735,829✔
74

75
_exit:
25,747,065✔
76
  if (code) {
25,748,583✔
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;
25,743,100✔
81
}
82

83
static int32_t tsdbDataFileReadTombFooter(SDataFileReader *reader) {
3,074,782✔
84
  if (reader->ctx->tombFooterLoaded) {
3,074,782✔
85
    return 0;
×
86
  }
87

88
  int32_t code = 0;
3,075,427✔
89
  int32_t lino = 0;
3,075,427✔
90

91
  int32_t ftype = TSDB_FTYPE_TOMB;
3,075,427✔
92
  if (reader->fd[ftype]) {
3,075,427✔
93
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2,920,258✔
94

95
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(STombFooter),
2,920,258✔
96
                                 (uint8_t *)reader->tombFooter, sizeof(STombFooter), 0, pEncryptData),
97
                    &lino, _exit);
98
  }
99
  reader->ctx->tombFooterLoaded = true;
3,075,424✔
100

101
_exit:
3,076,069✔
102
  if (code) {
3,076,069✔
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;
3,075,427✔
107
}
108

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

113
  if ((*reader = taosMemoryCalloc(1, sizeof(**reader))) == NULL) {
25,994,528✔
114
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
115
  }
116

117
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); i++) {
285,782,765✔
118
    tBufferInit(reader[0]->local + i);
259,766,978✔
119
  }
120

121
  reader[0]->config[0] = config[0];
26,015,787✔
122
  reader[0]->buffers = config->buffers;
25,996,833✔
123
  if (reader[0]->buffers == NULL) {
25,996,684✔
124
    reader[0]->buffers = reader[0]->local;
25,612,379✔
125
  }
126

127
  if (fname) {
25,991,520✔
128
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
127,465,206✔
129
      if (fname[i]) {
101,970,195✔
130
        int32_t lcn = config->files[i].file.lcn;
71,046,480✔
131
        TAOS_CHECK_GOTO(tsdbOpenFile(fname[i], config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
71,049,658✔
132
      }
133
    }
134
  } else {
135
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2,523,685✔
136
      if (config->files[i].exist) {
2,018,948✔
137
        char fname1[TSDB_FILENAME_LEN];
1,018,260✔
138
        tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
1,018,260✔
139
        int32_t lcn = config->files[i].file.lcn;
1,018,260✔
140
        TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
1,018,260✔
141
      }
142
    }
143
  }
144

145
_exit:
25,999,748✔
146
  if (code) {
25,998,025✔
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;
25,998,025✔
151
}
152

153
void tsdbDataFileReaderClose(SDataFileReader **reader) {
40,556,836✔
154
  if (reader[0] == NULL) {
40,556,836✔
155
    return;
14,562,130✔
156
  }
157

158
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
25,998,000✔
159
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);
25,998,791✔
160

161
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
129,993,431✔
162
    if (reader[0]->fd[i]) {
103,991,164✔
163
      tsdbCloseFile(&reader[0]->fd[i]);
72,071,849✔
164
    }
165
  }
166

167
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
285,884,525✔
168
    tBufferDestroy(reader[0]->local + i);
259,905,212✔
169
  }
170

171
  taosMemoryFree(reader[0]);
25,979,313✔
172
  reader[0] = NULL;
25,991,818✔
173
}
174

175
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
25,748,171✔
176
  int32_t code = 0;
25,748,171✔
177
  int32_t lino = 0;
25,748,171✔
178
  void   *data = NULL;
25,751,661✔
179

180
  if (!reader->ctx->brinBlkLoaded) {
25,751,661✔
181
    TAOS_CHECK_GOTO(tsdbDataFileReadHeadFooter(reader), &lino, _exit);
25,750,320✔
182

183
    if (reader->headFooter->brinBlkPtr->size > 0) {
25,740,027✔
184
      data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
22,917,294✔
185
      if (data == NULL) {
22,907,453✔
186
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
187
      }
188

189
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
22,907,453✔
190

191
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_HEAD], reader->headFooter->brinBlkPtr->offset, data,
22,918,812✔
192
                                   reader->headFooter->brinBlkPtr->size, 0, pEncryptData),
193
                      &lino, _exit);
194

195
      int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
22,928,891✔
196
      TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
22,929,494✔
197
    } else {
198
      TARRAY2_INIT(reader->brinBlkArray);
2,824,912✔
199
    }
200

201
    reader->ctx->brinBlkLoaded = true;
25,741,440✔
202
  }
203
  brinBlkArray[0] = reader->brinBlkArray;
25,751,030✔
204

205
_exit:
25,744,355✔
206
  if (code) {
25,749,086✔
207
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
208
              tstrerror(code));
209
    taosMemoryFree(data);
×
210
  }
211
  return code;
25,749,086✔
212
}
213

214
int32_t tsdbDataFileReadBrinBlock(SDataFileReader *reader, const SBrinBlk *brinBlk, SBrinBlock *brinBlock) {
21,311,277✔
215
  int32_t code = 0;
21,311,277✔
216
  int32_t lino = 0;
21,311,277✔
217

218
  SBuffer *buffer = reader->buffers + 0;
21,301,822✔
219
  SBuffer *assist = reader->buffers + 1;
21,304,248✔
220

221
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
21,308,546✔
222

223
  // load data
224
  tBufferClear(buffer);
225
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_HEAD], brinBlk->dp->offset, brinBlk->dp->size, buffer, 0,
21,301,878✔
226
                                       pEncryptData),
227
                  &lino, _exit);
228

229
  // decode brin block
230
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
21,322,236✔
231
  tBrinBlockClear(brinBlock);
21,322,236✔
232
  brinBlock->numOfPKs = brinBlk->numOfPKs;
21,322,865✔
233
  brinBlock->numOfRecords = brinBlk->numRec;
21,316,611✔
234
  for (int32_t i = 0; i < 10; i++) {  // int64_t
234,515,478✔
235

236
    SCompressInfo cinfo = {
213,191,426✔
237
        .cmprAlg = brinBlk->cmprAlg,
213,185,496✔
238
        .dataType = TSDB_DATA_TYPE_BIGINT,
239
        .compressedSize = brinBlk->size[i],
213,192,043✔
240
        .originalSize = brinBlk->numRec * sizeof(int64_t),
213,190,525✔
241
    };
242
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
213,193,661✔
243
    br.offset += brinBlk->size[i];
213,199,435✔
244
  }
245

246
  for (int32_t i = 10; i < 15; i++) {  // int32_t
127,935,269✔
247
    SCompressInfo cinfo = {
106,608,621✔
248
        .cmprAlg = brinBlk->cmprAlg,
106,611,848✔
249
        .dataType = TSDB_DATA_TYPE_INT,
250
        .compressedSize = brinBlk->size[i],
106,613,231✔
251
        .originalSize = brinBlk->numRec * sizeof(int32_t),
106,612,458✔
252
    };
253
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
106,614,444✔
254
    br.offset += brinBlk->size[i];
106,611,820✔
255
  }
256

257
  // primary keys
258
  if (brinBlk->numOfPKs > 0) {  // decode the primary keys
21,326,648✔
259
    SValueColumnCompressInfo firstInfos[TD_MAX_PK_COLS];
4,167✔
260
    SValueColumnCompressInfo lastInfos[TD_MAX_PK_COLS];
4,167✔
261

262
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
8,334✔
263
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, firstInfos + i), &lino, _exit);
4,167✔
264
    }
265
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
8,334✔
266
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, lastInfos + i), &lino, _exit);
4,167✔
267
    }
268

269
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
8,334✔
270
      SValueColumnCompressInfo *info = firstInfos + i;
4,167✔
271

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

276
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
8,334✔
277
      SValueColumnCompressInfo *info = lastInfos + i;
4,167✔
278

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

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

291
_exit:
21,332,550✔
292
  if (code) {
21,322,981✔
293
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
294
              tstrerror(code));
295
  }
296
  return code;
21,322,981✔
297
}
298

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

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

306
  SBuffer *buffer = reader->buffers + 0;
2,570,951✔
307
  SBuffer *assist = reader->buffers + 1;
2,570,951✔
308

309
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2,570,951✔
310

311
  // load data
312
  tBufferClear(buffer);
313
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockSize, buffer, 0,
2,570,951✔
314
                                       pEncryptData),
315
                  &lino, _exit);
316

317
  // decompress
318
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
2,570,951✔
319
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
2,570,951✔
320

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

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

336
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
84,078,177✔
337
                                          STSchema *pTSchema, int16_t cids[], int32_t ncid) {
338
  int32_t code = 0;
84,078,177✔
339
  int32_t lino = 0;
84,078,177✔
340
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
84,093,842✔
341

342
  SDiskDataHdr hdr;
84,095,615✔
343
  SBuffer     *buffer0 = reader->buffers + 0;
84,112,339✔
344
  SBuffer     *buffer1 = reader->buffers + 1;
84,115,990✔
345
  SBuffer     *assist = reader->buffers + 2;
84,119,495✔
346

347
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
84,113,688✔
348

349
  // load key part
350
  tBufferClear(buffer0);
351
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockKeySize, buffer0,
84,103,864✔
352
                                       0, pEncryptData),
353
                  &lino, _exit);
354

355
  // SDiskDataHdr
356
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
84,094,540✔
357
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
84,100,612✔
358

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

365
  tBlockDataReset(bData);
84,060,721✔
366
  bData->suid = hdr.suid;
84,090,488✔
367
  bData->uid = hdr.uid;
84,112,634✔
368
  bData->nRow = hdr.nRow;
84,110,841✔
369

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

379
  int extraColIdx = -1;
84,094,333✔
380
  for (int i = 0; i < ncid; i++) {
84,107,471✔
381
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
82,056,768✔
382
      extraColIdx = i;
82,055,437✔
383
      break;
82,055,437✔
384
    }
385
  }
386

387
  if (extraColIdx < 0) {
84,106,140✔
388
    goto _exit;
2,045,700✔
389
  }
390

391
  // load SBlockCol part
392
  tBufferClear(buffer0);
393
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset + record->blockKeySize,
82,060,268✔
394
                                       hdr.szBlkCol, buffer0, 0, pEncryptData),
395
                  &lino, _exit);
396

397
  // calc szHint
398
  int64_t szHint = 0;
82,066,495✔
399
  int     extraCols = 1;
82,066,495✔
400
  for (int i = extraColIdx + 1; i < ncid; ++i) {
82,066,495✔
401
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
44,205,771✔
402
      ++extraCols;
44,212,276✔
403
      break;
44,212,276✔
404
    }
405
  }
406

407
  if (extraCols >= 2) {
82,073,000✔
408
    br = BUFFER_READER_INITIALIZER(0, buffer0);
44,212,269✔
409

410
    SBlockCol blockCol = {.cid = 0};
44,212,269✔
411
    for (int32_t i = extraColIdx; i < ncid; ++i) {
44,213,775✔
412
      int16_t extraColCid = cids[i];
44,205,487✔
413

414
      while (extraColCid > blockCol.cid) {
147,739,311✔
415
        if (br.offset >= buffer0->size) {
103,521,725✔
416
          blockCol.cid = INT16_MAX;
×
417
          break;
×
418
        }
419

420
        TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
103,526,721✔
421
      }
422

423
      if (extraColCid == blockCol.cid || blockCol.cid == INT16_MAX) {
44,217,586✔
424
        extraColIdx = i;
44,217,586✔
425
        break;
44,217,586✔
426
      }
427
    }
428

429
    if (blockCol.cid > 0 && blockCol.cid < INT16_MAX /*&& blockCol->flag == HAS_VALUE*/) {
44,225,874✔
430
      int64_t   offset = blockCol.offset;
44,218,508✔
431
      SBlockCol lastNonNoneBlockCol = {.cid = 0};
44,218,508✔
432

433
      for (int32_t i = extraColIdx; i < ncid; ++i) {
239,869,010✔
434
        int16_t extraColCid = cids[i];
195,648,957✔
435

436
        while (extraColCid > blockCol.cid) {
539,490,990✔
437
          if (br.offset >= buffer0->size) {
343,840,488✔
438
            blockCol.cid = INT16_MAX;
×
439
            break;
×
440
          }
441

442
          TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
343,844,623✔
443
        }
444

445
        if (extraColCid == blockCol.cid) {
195,650,502✔
446
          lastNonNoneBlockCol = blockCol;
195,650,502✔
447
          continue;
195,650,502✔
448
        }
449

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

455
      if (lastNonNoneBlockCol.cid > 0) {
44,220,053✔
456
        szHint = lastNonNoneBlockCol.offset + lastNonNoneBlockCol.szBitmap + lastNonNoneBlockCol.szOffset +
44,218,792✔
457
                 lastNonNoneBlockCol.szValue - offset;
44,218,792✔
458
      }
459
    }
460
  }
461

462
  // load each column
463
  SBlockCol blockCol = {
82,079,523✔
464
      .cid = 0,
465
  };
466
  bool firstRead = true;
82,036,281✔
467
  br = BUFFER_READER_INITIALIZER(0, buffer0);
82,036,281✔
468
  for (int32_t i = 0; i < ncid; i++) {
315,543,658✔
469
    int16_t cid = cids[i];
233,467,846✔
470

471
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
233,485,454✔
472
      continue;
1,436✔
473
    }
474

475
    while (cid > blockCol.cid) {
829,116,547✔
476
      if (br.offset >= buffer0->size) {
595,594,341✔
477
        blockCol.cid = INT16_MAX;
×
478
        break;
×
479
      }
480

481
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
595,587,503✔
482
    }
483

484
    if (cid < blockCol.cid) {
233,522,206✔
485
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
×
486
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
×
487
      SBlockCol none = {
×
488
          .cid = cid,
489
          .type = tcol->type,
×
490
          .cflag = tcol->flags,
×
491
          .flag = HAS_NONE,
492
          .szOrigin = 0,
493
          .szBitmap = 0,
494
          .szOffset = 0,
495
          .szValue = 0,
496
          .offset = 0,
497
      };
498
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &none, &br, bData, assist), &lino, _exit);
×
499
    } else if (cid == blockCol.cid) {
233,522,206✔
500
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
233,477,877✔
501

502
      // load from file
503
      tBufferClear(buffer1);
504
      TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA],
233,512,722✔
505
                                           record->blockOffset + record->blockKeySize + hdr.szBlkCol + blockCol.offset,
506
                                           blockCol.szBitmap + blockCol.szOffset + blockCol.szValue, buffer1,
507
                                           firstRead ? szHint : 0, pEncryptData),
508
                      &lino, _exit);
509

510
      firstRead = false;
233,503,839✔
511

512
      // decode the buffer
513
      SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
233,503,839✔
514
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
233,506,858✔
515
    }
516
  }
517

518
_exit:
84,156,826✔
519
  if (code) {
84,119,332✔
520
    tsdbError("vgId:%d %s fid:%d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
×
521
              __FILE__, lino, tstrerror(code));
522
  }
523
  return code;
84,119,332✔
524
}
525

526
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
31,722,656✔
527
                                 TColumnDataAggArray *columnDataAggArray) {
528
  int32_t  code = 0;
31,722,656✔
529
  int32_t  lino = 0;
31,722,656✔
530
  SBuffer *buffer = reader->buffers + 0;
31,728,995✔
531

532
  TARRAY2_CLEAR(columnDataAggArray, NULL);
31,738,557✔
533
  if (record->smaSize > 0) {
31,739,311✔
534
    tBufferClear(buffer);
535
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
31,739,010✔
536

537
    TAOS_CHECK_GOTO(
31,739,611✔
538
        tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_SMA], record->smaOffset, record->smaSize, buffer, 0, pEncryptData),
539
        &lino, _exit);
540

541
    // decode sma data
542
    SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
31,729,875✔
543
    while (br.offset < record->smaSize) {
402,169,950✔
544
      SColumnDataAgg sma[1];
370,431,644✔
545

546
      TAOS_CHECK_GOTO(tGetColumnDataAgg(&br, sma), &lino, _exit);
370,443,426✔
547
      TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(columnDataAggArray, sma), &lino, _exit);
740,790,633✔
548
    }
549
    if (br.offset != record->smaSize) {
31,739,310✔
550
      tsdbError("vgId:%d %s failed at %s:%d since sma data size mismatch, expected: %u, actual: %u, fname:%s",
×
551
                TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, record->smaSize, br.offset,
552
                reader->fd[TSDB_FTYPE_SMA]->path);
553
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
554
    }
555
  }
556

557
_exit:
31,734,616✔
558
  if (code) {
31,739,160✔
559
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
560
              tstrerror(code));
561
  }
562
  return code;
31,739,160✔
563
}
564

565
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
4,993,265✔
566
  int32_t code = 0;
4,993,265✔
567
  int32_t lino = 0;
4,993,265✔
568
  void   *data = NULL;
4,993,265✔
569

570
  if (!reader->ctx->tombBlkLoaded) {
4,993,265✔
571
    TAOS_CHECK_GOTO(tsdbDataFileReadTombFooter(reader), &lino, _exit);
3,075,427✔
572

573
    if (reader->tombFooter->tombBlkPtr->size > 0) {
3,075,427✔
574
      if ((data = taosMemoryMalloc(reader->tombFooter->tombBlkPtr->size)) == NULL) {
2,920,258✔
575
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
576
      }
577

578
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2,920,258✔
579

580
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], reader->tombFooter->tombBlkPtr->offset, data,
2,920,258✔
581
                                   reader->tombFooter->tombBlkPtr->size, 0, pEncryptData),
582
                      &lino, _exit);
583

584
      int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
2,920,258✔
585
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
2,920,258✔
586
    } else {
587
      TARRAY2_INIT(reader->tombBlkArray);
154,524✔
588
    }
589

590
    reader->ctx->tombBlkLoaded = true;
3,074,782✔
591
  }
592
  tombBlkArray[0] = reader->tombBlkArray;
4,991,975✔
593

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

603
int32_t tsdbDataFileReadTombBlock(SDataFileReader *reader, const STombBlk *tombBlk, STombBlock *tData) {
1,819,274✔
604
  int32_t code = 0;
1,819,274✔
605
  int32_t lino = 0;
1,819,274✔
606

607
  SBuffer *buffer0 = reader->buffers + 0;
1,819,274✔
608
  SBuffer *assist = reader->buffers + 1;
1,819,274✔
609

610
  tBufferClear(buffer0);
611
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,819,274✔
612

613
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_TOMB], tombBlk->dp->offset, tombBlk->dp->size, buffer0, 0,
1,819,274✔
614
                                       pEncryptData),
615
                  &lino, _exit);
616

617
  int32_t       size = 0;
1,819,274✔
618
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
1,819,274✔
619
  tTombBlockClear(tData);
1,819,274✔
620
  tData->numOfRecords = tombBlk->numRec;
1,819,274✔
621
  for (int32_t i = 0; i < ARRAY_SIZE(tData->buffers); ++i) {
10,915,644✔
622
    SCompressInfo cinfo = {
9,096,370✔
623
        .cmprAlg = tombBlk->cmprAlg,
9,096,370✔
624
        .dataType = TSDB_DATA_TYPE_BIGINT,
625
        .originalSize = tombBlk->numRec * sizeof(int64_t),
9,096,370✔
626
        .compressedSize = tombBlk->size[i],
9,096,370✔
627
    };
628
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, tData->buffers + i, assist), &lino, _exit);
9,096,370✔
629
    br.offset += tombBlk->size[i];
9,096,370✔
630
  }
631

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

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

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

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

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

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

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

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

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

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

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

694
static void tsdbDataFileWriterDoClose(SDataFileWriter *writer) {
781,462✔
695
  if (writer->ctx->reader) {
781,462✔
696
    tsdbDataFileReaderClose(&writer->ctx->reader);
381,246✔
697
  }
698

699
  tTombBlockDestroy(writer->tombBlock);
781,462✔
700
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
781,462✔
701
  tBlockDataDestroy(writer->blockData);
781,462✔
702
  tBrinBlockDestroy(writer->brinBlock);
781,462✔
703
  TARRAY2_DESTROY(writer->brinBlkArray, NULL);
781,462✔
704

705
  tTombBlockDestroy(writer->ctx->tombBlock);
781,462✔
706
  tBlockDataDestroy(writer->ctx->blockData);
781,462✔
707
  tBrinBlockDestroy(writer->ctx->brinBlock);
781,462✔
708

709
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
8,596,082✔
710
    tBufferDestroy(writer->local + i);
7,814,620✔
711
  }
712

713
  tDestroyTSchema(writer->skmRow->pTSchema);
781,462✔
714
  tDestroyTSchema(writer->skmTb->pTSchema);
781,462✔
715
}
781,462✔
716

717
static int32_t tsdbDataFileWriterDoOpenReader(SDataFileWriter *writer) {
781,462✔
718
  int32_t code = 0;
781,462✔
719
  int32_t lino = 0;
781,462✔
720

721
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2,688,422✔
722
    if (writer->config->files[i].exist) {
2,288,206✔
723
      SDataFileReaderConfig config[1] = {{
381,246✔
724
          .tsdb = writer->config->tsdb,
381,246✔
725
          .szPage = writer->config->szPage,
381,246✔
726
          .buffers = writer->buffers,
381,246✔
727
      }};
728

729
      for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
1,906,230✔
730
        config->files[i].exist = writer->config->files[i].exist;
1,524,984✔
731
        if (config->files[i].exist) {
1,524,984✔
732
          config->files[i].file = writer->config->files[i].file;
940,596✔
733
        }
734
      }
735

736
      TAOS_CHECK_GOTO(tsdbDataFileReaderOpen(NULL, config, &writer->ctx->reader), &lino, _exit);
381,246✔
737
      break;
381,246✔
738
    }
739
  }
740

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

749
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
781,462✔
750
  int32_t code = 0;
781,462✔
751
  int32_t lino = 0;
781,462✔
752
  int32_t ftype;
753
  SDiskID diskId = {0};
781,462✔
754

755
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
781,462✔
756
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
781,462✔
757
  writer->buffers = writer->config->buffers;
781,462✔
758
  if (writer->buffers == NULL) {
781,462✔
759
    writer->buffers = writer->local;
×
760
  }
761

762
  // open reader
763
  TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpenReader(writer), &lino, _exit);
781,462✔
764

765
  // .head
766
  ftype = TSDB_FTYPE_HEAD;
781,462✔
767
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
781,462✔
768
  TSDB_CHECK_CODE(code, lino, _exit);
781,462✔
769
  writer->files[ftype] = (STFile){
1,562,924✔
770
      .type = ftype,
771
      .did = diskId,
772
      .fid = writer->config->fid,
781,462✔
773
      .cid = writer->config->cid,
781,462✔
774
      .size = 0,
775
      .minVer = VERSION_MAX,
776
      .maxVer = VERSION_MIN,
777
  };
778

779
  // .data
780
  ftype = TSDB_FTYPE_DATA;
781,462✔
781
  if (writer->config->files[ftype].exist) {
781,462✔
782
    writer->files[ftype] = writer->config->files[ftype].file;
279,214✔
783
  } else {
784
    code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
502,248✔
785
    TSDB_CHECK_CODE(code, lino, _exit);
502,248✔
786
    writer->files[ftype] = (STFile){
1,004,496✔
787
        .type = ftype,
788
        .did = diskId,
789
        .fid = writer->config->fid,
502,248✔
790
        .cid = writer->config->cid,
502,248✔
791
        .size = 0,
792
        .lcn = writer->config->lcn == 0 ? -1 : 0,
502,248✔
793
        .minVer = VERSION_MAX,
794
        .maxVer = VERSION_MIN,
795
    };
796
  }
797

798
  // .sma
799
  ftype = TSDB_FTYPE_SMA;
781,462✔
800
  if (writer->config->files[ftype].exist) {
781,462✔
801
    writer->files[ftype] = writer->config->files[ftype].file;
279,214✔
802
  } else {
803
    code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
502,248✔
804
    TSDB_CHECK_CODE(code, lino, _exit);
502,248✔
805
    writer->files[ftype] = (STFile){
1,004,496✔
806
        .type = ftype,
807
        .did = diskId,
808
        .fid = writer->config->fid,
502,248✔
809
        .cid = writer->config->cid,
502,248✔
810
        .size = 0,
811
        .minVer = VERSION_MAX,
812
        .maxVer = VERSION_MIN,
813
    };
814
  }
815

816
  // .tomb
817
  ftype = TSDB_FTYPE_TOMB;
781,462✔
818
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
781,462✔
819
  TSDB_CHECK_CODE(code, lino, _exit);
781,462✔
820
  writer->files[ftype] = (STFile){
1,562,924✔
821
      .type = ftype,
822
      .did = diskId,
823
      .fid = writer->config->fid,
781,462✔
824
      .cid = writer->config->cid,
781,462✔
825
      .size = 0,
826
      .minVer = VERSION_MAX,
827
      .maxVer = VERSION_MIN,
828
  };
829

830
  // range
831
  writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
781,462✔
832
  writer->ctx->tombRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
781,462✔
833

834
  writer->ctx->opened = true;
781,462✔
835

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

844
void tsdbWriterUpdVerRange(SVersionRange *range, int64_t minVer, int64_t maxVer) {
75,420,017✔
845
  range->minVer = TMIN(range->minVer, minVer);
75,420,017✔
846
  range->maxVer = TMAX(range->maxVer, maxVer);
75,429,202✔
847
}
75,434,435✔
848

849
int32_t tsdbFileWriteBrinBlock(STsdbFD *fd, SBrinBlock *brinBlock, uint32_t cmprAlg, int64_t *fileSize,
732,482✔
850
                               TBrinBlkArray *brinBlkArray, SBuffer *buffers, SVersionRange *range,
851
                               SEncryptData *encryptData) {
852
  if (brinBlock->numOfRecords == 0) {
732,482✔
853
    return 0;
×
854
  }
855

856
  int32_t  code;
857
  SBuffer *buffer0 = buffers + 0;
732,482✔
858
  SBuffer *buffer1 = buffers + 1;
732,482✔
859
  SBuffer *assist = buffers + 2;
732,482✔
860

861
  SBrinBlk brinBlk = {
732,482✔
862
      .dp[0] =
863
          {
864
              .offset = *fileSize,
732,482✔
865
              .size = 0,
866
          },
867
      .numRec = brinBlock->numOfRecords,
732,482✔
868
      .numOfPKs = brinBlock->numOfPKs,
732,482✔
869
      .cmprAlg = cmprAlg,
870
  };
871
  for (int i = 0; i < brinBlock->numOfRecords; i++) {
45,055,863✔
872
    SBrinRecord record;
44,323,381✔
873

874
    TAOS_CHECK_RETURN(tBrinBlockGet(brinBlock, i, &record));
44,322,920✔
875
    if (i == 0) {
44,323,381✔
876
      brinBlk.minTbid.suid = record.suid;
732,482✔
877
      brinBlk.minTbid.uid = record.uid;
732,482✔
878
      brinBlk.minVer = record.minVer;
732,482✔
879
      brinBlk.maxVer = record.maxVer;
732,482✔
880
    }
881
    if (i == brinBlock->numOfRecords - 1) {
44,323,381✔
882
      brinBlk.maxTbid.suid = record.suid;
732,482✔
883
      brinBlk.maxTbid.uid = record.uid;
732,482✔
884
    }
885
    if (record.minVer < brinBlk.minVer) {
44,323,381✔
886
      brinBlk.minVer = record.minVer;
478,077✔
887
    }
888
    if (record.maxVer > brinBlk.maxVer) {
44,323,381✔
889
      brinBlk.maxVer = record.maxVer;
12,917,818✔
890
    }
891
  }
892

893
  tsdbWriterUpdVerRange(range, brinBlk.minVer, brinBlk.maxVer);
732,482✔
894

895
  // write to file
896
  for (int32_t i = 0; i < 10; ++i) {
8,057,302✔
897
    SCompressInfo info = {
7,324,820✔
898
        .cmprAlg = cmprAlg,
899
        .dataType = TSDB_DATA_TYPE_BIGINT,
900
        .originalSize = brinBlock->buffers[i].size,
7,324,820✔
901
    };
902

903
    tBufferClear(buffer0);
904
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
7,324,820✔
905
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
7,324,820✔
906
    brinBlk.size[i] = info.compressedSize;
7,324,820✔
907
    brinBlk.dp->size += info.compressedSize;
7,324,820✔
908
    *fileSize += info.compressedSize;
7,324,820✔
909
  }
910
  for (int32_t i = 10; i < 15; ++i) {
4,394,892✔
911
    SCompressInfo info = {
3,662,410✔
912
        .cmprAlg = cmprAlg,
913
        .dataType = TSDB_DATA_TYPE_INT,
914
        .originalSize = brinBlock->buffers[i].size,
3,662,410✔
915
    };
916

917
    tBufferClear(buffer0);
918
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
3,662,410✔
919
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
3,662,410✔
920
    brinBlk.size[i] = info.compressedSize;
3,662,410✔
921
    brinBlk.dp->size += info.compressedSize;
3,662,410✔
922
    *fileSize += info.compressedSize;
3,662,410✔
923
  }
924

925
  // write primary keys to file
926
  if (brinBlock->numOfPKs > 0) {
732,482✔
927
    tBufferClear(buffer0);
928
    tBufferClear(buffer1);
929

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

942
    // write to file
943
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
3,335✔
944
    *fileSize += buffer0->size;
3,335✔
945
    brinBlk.dp->size += buffer0->size;
3,335✔
946
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer1->data, buffer1->size, encryptData));
3,335✔
947
    *fileSize += buffer1->size;
3,335✔
948
    brinBlk.dp->size += buffer1->size;
3,335✔
949
  }
950

951
  // append to brinBlkArray
952
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(brinBlkArray, &brinBlk));
1,464,964✔
953

954
  tBrinBlockClear(brinBlock);
732,482✔
955

956
  return 0;
732,482✔
957
}
958

959
static int32_t tsdbDataFileWriteBrinBlock(SDataFileWriter *writer) {
732,482✔
960
  if (writer->brinBlock->numOfRecords == 0) {
732,482✔
961
    return 0;
×
962
  }
963

964
  int32_t code = 0;
732,482✔
965
  int32_t lino = 0;
732,482✔
966

967
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
732,482✔
968

969
  TAOS_CHECK_GOTO(tsdbFileWriteBrinBlock(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlock, writer->config->cmprAlg,
732,482✔
970
                                         &writer->files[TSDB_FTYPE_HEAD].size, writer->brinBlkArray, writer->buffers,
971
                                         &writer->ctx->range, pEncryptData),
972
                  &lino, _exit);
973

974
_exit:
732,482✔
975
  if (code) {
732,482✔
976
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
977
              tstrerror(code));
978
  }
979
  return code;
732,482✔
980
}
981

982
static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
44,321,174✔
983
  int32_t code = 0;
44,321,174✔
984
  int32_t lino = 0;
44,321,174✔
985

986
  for (;;) {
987
    code = tBrinBlockPut(writer->brinBlock, record);
44,322,108✔
988
    if (code == TSDB_CODE_INVALID_PARA) {
44,314,903✔
989
      // different records with different primary keys
UNCOV
990
      TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
×
UNCOV
991
      continue;
×
992
    } else {
993
      TSDB_CHECK_CODE(code, lino, _exit);
44,314,903✔
994
    }
995
    break;
44,314,903✔
996
  }
997

998
  if ((writer->brinBlock->numOfRecords) >= 256) {
44,314,903✔
999
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
86,622✔
1000
  }
1001

1002
_exit:
44,310,183✔
1003
  if (code) {
44,316,428✔
1004
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1005
              tstrerror(code));
1006
  }
1007
  return code;
44,316,428✔
1008
}
1009

1010
static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
45,290,762✔
1011
  if (bData->nRow == 0) {
45,290,762✔
1012
    return 0;
12,841,597✔
1013
  }
1014

1015
  if (!bData->uid) {
32,452,763✔
1016
    return TSDB_CODE_INVALID_PARA;
×
1017
  }
1018

1019
  int32_t  code = 0;
32,453,500✔
1020
  int32_t  lino = 0;
32,453,500✔
1021
  SBuffer *buffers = writer->buffers;
32,454,698✔
1022
  SBuffer *assist = writer->buffers + 4;
32,453,823✔
1023

1024
  SColCompressInfo cmprInfo = {.pColCmpr = NULL, .defaultCmprAlg = writer->config->cmprAlg};
32,453,500✔
1025

1026
  SBrinRecord record[1] = {{
32,454,434✔
1027
      .suid = bData->suid,
32,455,029✔
1028
      .uid = bData->uid,
32,454,028✔
1029
      .minVer = bData->aVersion[0],
32,452,822✔
1030
      .maxVer = bData->aVersion[0],
32,455,096✔
1031
      .blockOffset = writer->files[TSDB_FTYPE_DATA].size,
32,455,368✔
1032
      .smaOffset = writer->files[TSDB_FTYPE_SMA].size,
32,455,029✔
1033
      .blockSize = 0,
1034
      .blockKeySize = 0,
1035
      .smaSize = 0,
1036
      .numRow = bData->nRow,
32,454,095✔
1037
      .count = 1,
1038
  }};
1039

1040
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &record->firstKey);
32,453,626✔
1041
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, bData->nRow - 1), &record->lastKey);
32,454,367✔
1042

1043
  for (int32_t i = 1; i < bData->nRow; ++i) {
2,147,483,647✔
1044
    if (tsdbRowCompareWithoutVersion(&tsdbRowFromBlockData(bData, i - 1), &tsdbRowFromBlockData(bData, i)) != 0) {
2,147,483,647✔
1045
      record->count++;
2,147,483,647✔
1046
    }
1047
    if (bData->aVersion[i] < record->minVer) {
2,147,483,647✔
1048
      record->minVer = bData->aVersion[i];
5,504,009✔
1049
    }
1050
    if (bData->aVersion[i] > record->maxVer) {
2,147,483,647✔
1051
      record->maxVer = bData->aVersion[i];
98,040,166✔
1052
    }
1053
  }
1054

1055
  tsdbWriterUpdVerRange(&writer->ctx->range, record->minVer, record->maxVer);
32,454,765✔
1056

1057
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, bData->suid != 0 ? bData->suid : bData->uid,
32,455,368✔
1058
                        &cmprInfo.pColCmpr);
1059
  if (code) {
32,451,455✔
1060
    tsdbWarn("vgId:%d failed to get column compress algrithm", TD_VID(writer->config->tsdb->pVnode));
×
1061
  }
1062

1063
  TAOS_CHECK_GOTO(tBlockDataCompress(bData, &cmprInfo, buffers, assist), &lino, _exit);
32,451,455✔
1064

1065
  record->blockKeySize = buffers[0].size + buffers[1].size;
32,448,825✔
1066
  record->blockSize = record->blockKeySize + buffers[2].size + buffers[3].size;
32,451,194✔
1067

1068
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
32,448,286✔
1069

1070
  for (int i = 0; i < 4; i++) {
162,252,763✔
1071
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, buffers[i].data,
129,798,388✔
1072
                                  buffers[i].size, pEncryptData),
1073
                    &lino, _exit);
1074
    writer->files[TSDB_FTYPE_DATA].size += buffers[i].size;
129,798,546✔
1075
  }
1076

1077
  // to .sma file
1078
  tBufferClear(&buffers[0]);
1079
  for (int32_t i = 0; i < bData->nColData; ++i) {
255,094,030✔
1080
    SColData *colData = bData->aColData + i;
222,640,241✔
1081
    if ((colData->cflag & COL_SMA_ON) == 0 || ((colData->flag & HAS_VALUE) == 0)) continue;
222,642,493✔
1082

1083
    SColumnDataAgg sma[1] = {{.colId = colData->cid}};
217,187,111✔
1084
    tColDataCalcSMA[colData->type](colData, sma);
217,184,020✔
1085

1086
    TAOS_CHECK_GOTO(tPutColumnDataAgg(&buffers[0], sma), &lino, _exit);
217,182,233✔
1087
  }
1088
  record->smaSize = buffers[0].size;
32,453,559✔
1089

1090
  if (record->smaSize > 0) {
32,453,890✔
1091
    TAOS_CHECK_GOTO(
32,453,707✔
1092
        tsdbWriteFile(writer->fd[TSDB_FTYPE_SMA], record->smaOffset, buffers[0].data, record->smaSize, pEncryptData),
1093
        &lino, _exit);
1094
    writer->files[TSDB_FTYPE_SMA].size += record->smaSize;
32,451,500✔
1095
  }
1096

1097
  // append SBrinRecord
1098
  TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
32,453,287✔
1099

1100
  tBlockDataClear(bData);
32,447,785✔
1101

1102
_exit:
32,445,136✔
1103
  if (code) {
32,450,409✔
1104
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1105
              tstrerror(code));
1106
  }
1107
  taosHashCleanup(cmprInfo.pColCmpr);
32,450,409✔
1108
  return code;
32,446,469✔
1109
}
1110

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

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

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

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

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

1146
static FORCE_INLINE int32_t tsdbRowKeyCmprNullAsLargest(const STsdbRowKey *key1, const STsdbRowKey *key2) {
1147
  if (key1 == NULL) {
2,147,483,647✔
1148
    return 1;
520,154✔
1149
  } else if (key2 == NULL) {
2,147,483,647✔
1150
    return -1;
5,857,807✔
1151
  } else {
1152
    return tsdbRowKeyCmpr(key1, key2);
2,147,483,647✔
1153
  }
1154
}
1155

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

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

1165
  for (;;) {
5,083✔
1166
    for (;;) {
1167
      // SBlockData
1168
      for (; writer->ctx->blockDataIdx < writer->ctx->blockData->nRow; writer->ctx->blockDataIdx++) {
2,147,483,647✔
1169
        TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
2,147,483,647✔
1170

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

1179
      // SBrinBlock
1180
      if (writer->ctx->brinBlockIdx >= writer->ctx->brinBlock->numOfRecords) {
6,529,726✔
1181
        break;
182,163✔
1182
      }
1183

1184
      for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
8,391,263✔
1185
        SBrinRecord record;
8,277,527✔
1186
        code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record);
8,277,527✔
1187
        TSDB_CHECK_CODE(code, lino, _exit);
8,277,376✔
1188
        if (record.uid != writer->ctx->tbid->uid) {
8,277,376✔
1189
          writer->ctx->tbHasOldData = false;
186,833✔
1190
          goto _exit;
186,833✔
1191
        }
1192

1193
        if (tsdbRowKeyCmprNullAsLargest(key, &record.firstKey) < 0) {  // key < record->firstKey
8,090,694✔
1194
          goto _exit;
3,805,329✔
1195
        } else {
1196
          SBrinRecord record[1];
4,285,365✔
1197
          code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
4,285,365✔
1198
          TSDB_CHECK_CODE(code, lino, _exit);
4,285,365✔
1199
          if (tsdbRowKeyCmprNullAsLargest(key, &record->lastKey) > 0) {  // key > record->lastKey
4,285,365✔
1200
            if (writer->blockData->nRow > 0) {
2,042,974✔
1201
              TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
1,525✔
1202
            }
1203

1204
            TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
2,042,974✔
1205
          } else {
1206
            TAOS_CHECK_GOTO(tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData), &lino,
2,242,391✔
1207
                            _exit);
1208

1209
            writer->ctx->blockDataIdx = 0;
2,242,391✔
1210
            writer->ctx->brinBlockIdx++;
2,242,391✔
1211
            break;
2,242,391✔
1212
          }
1213
        }
1214
      }
1215
    }
1216

1217
    // SBrinBlk
1218
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
182,163✔
1219
      writer->ctx->brinBlkArray = NULL;
177,080✔
1220
      writer->ctx->tbHasOldData = false;
177,080✔
1221
      goto _exit;
177,080✔
1222
    } else {
1223
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
5,083✔
1224

1225
      if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
5,083✔
1226
        writer->ctx->tbHasOldData = false;
×
1227
        goto _exit;
×
1228
      }
1229

1230
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
5,083✔
1231

1232
      writer->ctx->brinBlockIdx = 0;
5,083✔
1233
      writer->ctx->brinBlkArrayIdx++;
5,083✔
1234
    }
1235
  }
1236

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

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

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

1255
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, row), &lino, _exit);
2,147,483,647✔
1256

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

1265
static int32_t tsdbDataFileWriteTableDataEnd(SDataFileWriter *writer) {
14,393,395✔
1266
  if (writer->ctx->tbid->uid == 0) {
14,393,395✔
1267
    return 0;
645,860✔
1268
  }
1269

1270
  int32_t code = 0;
13,751,176✔
1271
  int32_t lino = 0;
13,751,176✔
1272

1273
  if (writer->ctx->tbHasOldData) {
13,751,168✔
1274
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, NULL /* as the largest key */), &lino, _exit);
131,392✔
1275
  }
1276

1277
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
13,751,499✔
1278

1279
_exit:
13,751,168✔
1280
  if (code) {
13,750,829✔
1281
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1282
              tstrerror(code));
1283
  }
1284
  return code;
13,751,838✔
1285
}
1286

1287
static int32_t tsdbDataFileWriteTableDataBegin(SDataFileWriter *writer, const TABLEID *tbid) {
14,389,757✔
1288
  int32_t code = 0;
14,389,757✔
1289
  int32_t lino = 0;
14,389,757✔
1290

1291
  SMetaInfo info;
14,397,359✔
1292
  bool      drop = false;
14,397,690✔
1293
  TABLEID   tbid1[1];
14,397,690✔
1294
  writer->ctx->tbHasOldData = false;
14,397,359✔
1295
  while (writer->ctx->brinBlkArray) {  // skip data of previous table
14,661,437✔
1296
    for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
12,786,924✔
1297
      SBrinRecord record;
12,452,692✔
1298
      TAOS_CHECK_GOTO(tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record), &lino, _exit);
12,452,692✔
1299

1300
      if (record.uid == tbid->uid) {
12,452,129✔
1301
        writer->ctx->tbHasOldData = true;
363,913✔
1302
        goto _begin;
363,913✔
1303
      } else if (record.suid > tbid->suid || (record.suid == tbid->suid && record.uid > tbid->uid)) {
12,088,948✔
1304
        goto _begin;
2,256,172✔
1305
      } else {
1306
        if (record.uid != writer->ctx->tbid->uid) {
9,832,776✔
1307
          if (drop && tbid1->uid == record.uid) {
3,877,862✔
1308
            continue;
×
1309
          } else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, record.uid, &info, NULL) != 0) {
3,877,862✔
1310
            drop = true;
8,219✔
1311
            tbid1->suid = record.suid;
8,219✔
1312
            tbid1->uid = record.uid;
8,219✔
1313
            continue;
8,219✔
1314
          } else {
1315
            drop = false;
3,869,643✔
1316
            writer->ctx->tbid->suid = record.suid;
3,869,643✔
1317
            writer->ctx->tbid->uid = record.uid;
3,869,643✔
1318
          }
1319
        }
1320

1321
        TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, &record), &lino, _exit);
9,824,708✔
1322
      }
1323
    }
1324

1325
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
334,072✔
1326
      writer->ctx->brinBlkArray = NULL;
70,325✔
1327
      break;
70,325✔
1328
    } else {
1329
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
263,747✔
1330

1331
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
263,747✔
1332

1333
      writer->ctx->brinBlockIdx = 0;
263,747✔
1334
      writer->ctx->brinBlkArrayIdx++;
263,747✔
1335
    }
1336
  }
1337

1338
_begin:
14,398,360✔
1339
  writer->ctx->tbid[0] = *tbid;
14,398,360✔
1340

1341
  if (tbid->uid == INT64_MAX) {
14,398,360✔
1342
    goto _exit;
645,860✔
1343
  }
1344

1345
  TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, tbid, writer->config->skmTb), &lino, _exit);
13,752,500✔
1346
  TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, writer->ctx->tbid, writer->config->skmTb->pTSchema, NULL, 0), &lino,
13,750,111✔
1347
                  _exit);
1348

1349
_exit:
14,397,628✔
1350
  if (code) {
14,397,628✔
1351
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1352
              tstrerror(code));
1353
  }
1354
  return code;
14,397,628✔
1355
}
1356

1357
int32_t tsdbFileWriteHeadFooter(STsdbFD *fd, int64_t *fileSize, const SHeadFooter *footer, SEncryptData *encryptData) {
645,860✔
1358
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
645,860✔
1359
  *fileSize += sizeof(*footer);
645,860✔
1360
  return 0;
645,860✔
1361
}
1362

1363
int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
1,663,711✔
1364
                               TTombBlkArray *tombBlkArray, SBuffer *buffers, SVersionRange *range,
1365
                               SEncryptData *encryptData) {
1366
  int32_t code;
1367

1368
  if (TOMB_BLOCK_SIZE(tombBlock) == 0) {
1,663,711✔
1369
    return 0;
×
1370
  }
1371

1372
  SBuffer *buffer0 = buffers + 0;
1,663,711✔
1373
  SBuffer *assist = buffers + 1;
1,663,711✔
1374

1375
  STombBlk tombBlk = {
1,663,711✔
1376
      .dp[0] =
1377
          {
1378
              .offset = *fileSize,
1,663,711✔
1379
              .size = 0,
1380
          },
1381
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
1,663,711✔
1382
      .cmprAlg = cmprAlg,
1383
  };
1384
  for (int i = 0; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
40,705,293✔
1385
    STombRecord record;
39,041,582✔
1386
    TAOS_CHECK_RETURN(tTombBlockGet(tombBlock, i, &record));
39,041,582✔
1387

1388
    if (i == 0) {
39,041,582✔
1389
      tombBlk.minTbid.suid = record.suid;
1,663,147✔
1390
      tombBlk.minTbid.uid = record.uid;
1,663,147✔
1391
      tombBlk.minVer = record.version;
1,663,147✔
1392
      tombBlk.maxVer = record.version;
1,663,147✔
1393
    }
1394
    if (i == TOMB_BLOCK_SIZE(tombBlock) - 1) {
39,041,582✔
1395
      tombBlk.maxTbid.suid = record.suid;
1,663,711✔
1396
      tombBlk.maxTbid.uid = record.uid;
1,663,711✔
1397
    }
1398
    if (record.version < tombBlk.minVer) {
39,041,582✔
1399
      tombBlk.minVer = record.version;
3,026✔
1400
    }
1401
    if (record.version > tombBlk.maxVer) {
39,041,582✔
1402
      tombBlk.maxVer = record.version;
35,165,897✔
1403
    }
1404
  }
1405

1406
  tsdbWriterUpdVerRange(range, tombBlk.minVer, tombBlk.maxVer);
1,663,711✔
1407

1408
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); i++) {
9,982,266✔
1409
    tBufferClear(buffer0);
1410

1411
    SCompressInfo cinfo = {
8,318,555✔
1412
        .cmprAlg = cmprAlg,
1413
        .dataType = TSDB_DATA_TYPE_BIGINT,
1414
        .originalSize = tombBlock->buffers[i].size,
8,318,555✔
1415
    };
1416
    TAOS_CHECK_RETURN(tCompressDataToBuffer(tombBlock->buffers[i].data, &cinfo, buffer0, assist));
8,317,427✔
1417
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
8,318,555✔
1418

1419
    tombBlk.size[i] = cinfo.compressedSize;
8,318,555✔
1420
    tombBlk.dp->size += tombBlk.size[i];
8,318,555✔
1421
    *fileSize += tombBlk.size[i];
8,317,991✔
1422
  }
1423

1424
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(tombBlkArray, &tombBlk));
3,327,422✔
1425

1426
  tTombBlockClear(tombBlock);
1,663,711✔
1427
  return 0;
1,663,147✔
1428
}
1429

1430
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
645,860✔
1431
  int32_t code = 0;
645,860✔
1432
  int32_t lino = 0;
645,860✔
1433

1434
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
645,860✔
1435

1436
  TAOS_CHECK_GOTO(tsdbFileWriteHeadFooter(writer->fd[TSDB_FTYPE_HEAD], &writer->files[TSDB_FTYPE_HEAD].size,
645,860✔
1437
                                          writer->headFooter, pEncryptData),
1438
                  &lino, _exit);
1439

1440
_exit:
645,860✔
1441
  if (code) {
645,860✔
1442
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1443
              tstrerror(code));
1444
  }
1445
  return code;
645,860✔
1446
}
1447

1448
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
138,063✔
1449
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
138,063✔
1450

1451
  int32_t code = 0;
138,063✔
1452
  int32_t lino = 0;
138,063✔
1453

1454
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
138,063✔
1455

1456
  TAOS_CHECK_GOTO(tsdbFileWriteTombBlock(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlock, writer->config->cmprAlg,
138,063✔
1457
                                         &writer->files[TSDB_FTYPE_TOMB].size, writer->tombBlkArray, writer->buffers,
1458
                                         &writer->ctx->tombRange, pEncryptData),
1459
                  &lino, _exit);
1460

1461
_exit:
138,063✔
1462
  if (code) {
138,063✔
1463
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1464
              tstrerror(code));
1465
  }
1466
  return code;
138,063✔
1467
}
1468

1469
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize,
18,004,278✔
1470
                             SEncryptData *encryptData) {
1471
  ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
18,004,278✔
1472
  if (ptr->size > 0) {
18,012,056✔
1473
    ptr->offset = *fileSize;
1,663,711✔
1474

1475
    TAOS_CHECK_RETURN(
1,663,147✔
1476
        tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(tombBlkArray), ptr->size, encryptData));
1477

1478
    *fileSize += ptr->size;
1,663,147✔
1479
  }
1480
  return 0;
18,008,821✔
1481
}
1482

1483
static int32_t tsdbDataFileDoWriteTombBlk(SDataFileWriter *writer) {
138,063✔
1484
  if (TARRAY2_SIZE(writer->tombBlkArray) <= 0) {
138,063✔
1485
    return TSDB_CODE_INVALID_PARA;
×
1486
  }
1487

1488
  int32_t code = 0;
138,063✔
1489
  int32_t lino = 0;
138,063✔
1490

1491
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
138,063✔
1492

1493
  TAOS_CHECK_GOTO(
138,063✔
1494
      tsdbFileWriteTombBlk(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlkArray, writer->tombFooter->tombBlkPtr,
1495
                           &writer->files[TSDB_FTYPE_TOMB].size, pEncryptData),
1496
      &lino, _exit);
1497

1498
_exit:
138,063✔
1499
  if (code) {
138,063✔
1500
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1501
              tstrerror(code));
1502
  }
1503
  return code;
138,063✔
1504
}
1505

1506
int32_t tsdbFileWriteTombFooter(STsdbFD *fd, const STombFooter *footer, int64_t *fileSize, SEncryptData *encryptData) {
138,063✔
1507
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
138,063✔
1508
  *fileSize += sizeof(*footer);
138,063✔
1509
  return 0;
138,063✔
1510
}
1511

1512
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
138,063✔
1513
  int32_t code = 0;
138,063✔
1514
  int32_t lino = 0;
138,063✔
1515

1516
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
138,063✔
1517

1518
  TAOS_CHECK_GOTO(tsdbFileWriteTombFooter(writer->fd[TSDB_FTYPE_TOMB], writer->tombFooter,
138,063✔
1519
                                          &writer->files[TSDB_FTYPE_TOMB].size, pEncryptData),
1520
                  &lino, _exit);
1521

1522
_exit:
138,063✔
1523
  if (code) {
138,063✔
1524
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1525
              tstrerror(code));
1526
  }
1527
  return code;
138,063✔
1528
}
1529

1530
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
536,566✔
1531
  int32_t code = 0;
536,566✔
1532
  int32_t lino = 0;
536,566✔
1533

1534
  while (writer->ctx->hasOldTomb) {
638,598✔
1535
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
32,733,202✔
1536
      STombRecord record1[1];
32,529,138✔
1537
      TAOS_CHECK_GOTO(tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1), &lino, _exit);
32,529,138✔
1538

1539
      int32_t c = tTombRecordCompare(record, record1);
32,529,138✔
1540
      if (c < 0) {
32,529,138✔
1541
        goto _write;
×
1542
      } else if (c > 0) {
32,529,138✔
1543
        TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record1), &lino, _exit);
32,529,138✔
1544

1545
        tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
32,529,138✔
1546
                  ", version:%" PRId64,
1547
                  TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid,
1548
                  record1->suid, record1->uid, record1->version);
1549

1550
        if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
32,529,138✔
1551
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1552
        }
1553
      } else {
1554
        tsdbError("vgId:%d duplicate tomb record, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64 ", version:%" PRId64,
×
1555
                  TD_VID(writer->config->tsdb->pVnode), writer->config->cid, record->suid, record->uid,
1556
                  record->version);
1557
      }
1558
    }
1559

1560
    if (writer->ctx->tombBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->tombBlkArray)) {
204,064✔
1561
      writer->ctx->hasOldTomb = false;
102,032✔
1562
      break;
102,032✔
1563
    } else {
1564
      const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);
102,032✔
1565

1566
      TAOS_CHECK_GOTO(tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock), &lino, _exit);
102,032✔
1567

1568
      writer->ctx->tombBlockIdx = 0;
102,032✔
1569
      writer->ctx->tombBlkArrayIdx++;
102,032✔
1570
    }
1571
  }
1572

1573
_write:
536,566✔
1574
  if (record->suid == INT64_MAX) {
536,566✔
1575
    goto _exit;
138,063✔
1576
  }
1577

1578
  TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
398,503✔
1579

1580
  tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
398,503✔
1581
            ", version:%" PRId64,
1582
            TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid, record->suid,
1583
            record->uid, record->version);
1584

1585
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
398,503✔
1586
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1587
  }
1588

1589
_exit:
536,566✔
1590
  if (code) {
536,566✔
1591
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1592
              tstrerror(code));
1593
  }
1594
  return code;
536,566✔
1595
}
1596

1597
int32_t tsdbFileWriteBrinBlk(STsdbFD *fd, TBrinBlkArray *brinBlkArray, SFDataPtr *ptr, int64_t *fileSize,
645,860✔
1598
                             SEncryptData *encryptData) {
1599
  if (TARRAY2_SIZE(brinBlkArray) <= 0) {
645,860✔
1600
    return TSDB_CODE_INVALID_PARA;
×
1601
  }
1602
  ptr->offset = *fileSize;
645,860✔
1603
  ptr->size = TARRAY2_DATA_LEN(brinBlkArray);
645,860✔
1604

1605
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, ptr->offset, (uint8_t *)TARRAY2_DATA(brinBlkArray), ptr->size, encryptData));
645,860✔
1606

1607
  *fileSize += ptr->size;
645,860✔
1608
  return 0;
645,860✔
1609
}
1610

1611
static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
645,860✔
1612
  int32_t code = 0;
645,860✔
1613
  int32_t lino = 0;
645,860✔
1614

1615
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
645,860✔
1616

1617
  TAOS_CHECK_GOTO(
645,860✔
1618
      tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
1619
                           &writer->files[TSDB_FTYPE_HEAD].size, pEncryptData),
1620
      &lino, _exit);
1621

1622
_exit:
645,860✔
1623
  if (code) {
645,860✔
1624
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1625
              tstrerror(code));
1626
  }
1627
  return code;
645,860✔
1628
}
1629

1630
void tsdbTFileUpdVerRange(STFile *f, SVersionRange range) {
20,708,521✔
1631
  f->minVer = TMIN(f->minVer, range.minVer);
20,708,521✔
1632
  f->maxVer = TMAX(f->maxVer, range.maxVer);
20,727,647✔
1633
}
20,717,035✔
1634

1635
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
781,462✔
1636
  int32_t code = 0;
781,462✔
1637
  int32_t lino = 0;
781,462✔
1638

1639
  int32_t  ftype;
1640
  STFileOp op;
781,462✔
1641

1642
  if (writer->fd[TSDB_FTYPE_HEAD]) {
781,462✔
1643
    TABLEID tbid[1] = {{
645,860✔
1644
        .suid = INT64_MAX,
1645
        .uid = INT64_MAX,
1646
    }};
1647

1648
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
645,860✔
1649
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, tbid), &lino, _exit);
645,860✔
1650
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
645,860✔
1651
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlk(writer), &lino, _exit);
645,860✔
1652
    TAOS_CHECK_GOTO(tsdbDataFileWriteHeadFooter(writer), &lino, _exit);
645,860✔
1653

1654
    SVersionRange ofRange = {.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
645,860✔
1655

1656
    // .head
1657
    ftype = TSDB_FTYPE_HEAD;
645,860✔
1658
    if (writer->config->files[ftype].exist) {
645,860✔
1659
      op = (STFileOp){
247,405✔
1660
          .optype = TSDB_FOP_REMOVE,
1661
          .fid = writer->config->fid,
247,405✔
1662
          .of = writer->config->files[ftype].file,
247,405✔
1663
      };
1664
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
247,405✔
1665
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
494,810✔
1666
    }
1667
    op = (STFileOp){
645,860✔
1668
        .optype = TSDB_FOP_CREATE,
1669
        .fid = writer->config->fid,
645,860✔
1670
        .nf = writer->files[ftype],
645,860✔
1671
    };
1672
    tsdbTFileUpdVerRange(&op.nf, ofRange);
645,860✔
1673
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
645,860✔
1674
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
1,291,720✔
1675

1676
    // .data
1677
    ftype = TSDB_FTYPE_DATA;
645,860✔
1678
    if (!writer->config->files[ftype].exist) {
645,860✔
1679
      op = (STFileOp){
398,455✔
1680
          .optype = TSDB_FOP_CREATE,
1681
          .fid = writer->config->fid,
398,455✔
1682
          .nf = writer->files[ftype],
398,455✔
1683
      };
1684
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
398,455✔
1685
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
796,910✔
1686
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
247,405✔
1687
      op = (STFileOp){
247,405✔
1688
          .optype = TSDB_FOP_MODIFY,
1689
          .fid = writer->config->fid,
247,405✔
1690
          .of = writer->config->files[ftype].file,
247,405✔
1691
          .nf = writer->files[ftype],
247,405✔
1692
      };
1693
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
247,405✔
1694
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
494,810✔
1695
    }
1696

1697
    // .sma
1698
    ftype = TSDB_FTYPE_SMA;
645,860✔
1699
    if (!writer->config->files[ftype].exist) {
645,860✔
1700
      op = (STFileOp){
398,455✔
1701
          .optype = TSDB_FOP_CREATE,
1702
          .fid = writer->config->fid,
398,455✔
1703
          .nf = writer->files[ftype],
398,455✔
1704
      };
1705
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
398,455✔
1706
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
796,910✔
1707
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
247,405✔
1708
      op = (STFileOp){
247,405✔
1709
          .optype = TSDB_FOP_MODIFY,
1710
          .fid = writer->config->fid,
247,405✔
1711
          .of = writer->config->files[ftype].file,
247,405✔
1712
          .nf = writer->files[ftype],
247,405✔
1713
      };
1714
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
247,405✔
1715
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
494,810✔
1716
    }
1717
  }
1718

1719
  if (writer->fd[TSDB_FTYPE_TOMB]) {
781,462✔
1720
    STombRecord record[1] = {{
138,063✔
1721
        .suid = INT64_MAX,
1722
        .uid = INT64_MAX,
1723
        .version = INT64_MAX,
1724
    }};
1725

1726
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
138,063✔
1727
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
138,063✔
1728
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlk(writer), &lino, _exit);
138,063✔
1729
    TAOS_CHECK_GOTO(tsdbDataFileWriteTombFooter(writer), &lino, _exit);
138,063✔
1730

1731
    SVersionRange ofRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
138,063✔
1732

1733
    ftype = TSDB_FTYPE_TOMB;
138,063✔
1734
    if (writer->config->files[ftype].exist) {
138,063✔
1735
      op = (STFileOp){
102,032✔
1736
          .optype = TSDB_FOP_REMOVE,
1737
          .fid = writer->config->fid,
102,032✔
1738
          .of = writer->config->files[ftype].file,
102,032✔
1739
      };
1740
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
102,032✔
1741
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
204,064✔
1742
    }
1743
    op = (STFileOp){
138,063✔
1744
        .optype = TSDB_FOP_CREATE,
1745
        .fid = writer->config->fid,
138,063✔
1746
        .nf = writer->files[ftype],
138,063✔
1747
    };
1748
    tsdbTFileUpdVerRange(&op.nf, ofRange);
138,063✔
1749
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->tombRange);
138,063✔
1750
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
276,126✔
1751
  }
1752
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
781,462✔
1753
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
3,907,310✔
1754
    if (writer->fd[i]) {
3,125,848✔
1755
      TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd[i], pEncryptData), &lino, _exit);
2,075,182✔
1756
      tsdbCloseFile(&writer->fd[i]);
2,075,643✔
1757
    }
1758
  }
1759

1760
_exit:
781,462✔
1761
  if (code) {
781,462✔
1762
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1763
              tstrerror(code));
1764
  }
1765
  return code;
781,462✔
1766
}
1767

1768
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
645,860✔
1769
  int32_t code = 0;
645,860✔
1770
  int32_t lino = 0;
645,860✔
1771

1772
  int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};
645,860✔
1773

1774
  for (int32_t i = 0; i < ARRAY_SIZE(ftypes); ++i) {
2,583,440✔
1775
    int32_t ftype = ftypes[i];
1,937,580✔
1776

1777
    char    fname[TSDB_FILENAME_LEN];
1,937,580✔
1778
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;
1,937,580✔
1779

1780
    if (writer->files[ftype].size == 0) {
1,937,580✔
1781
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
1,442,770✔
1782
    }
1783

1784
    int32_t lcn = writer->files[ftype].lcn;
1,937,580✔
1785
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
1,937,580✔
1786
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
1,937,580✔
1787

1788
    if (writer->files[ftype].size == 0) {
1,937,580✔
1789
      uint8_t hdr[TSDB_FHDR_SIZE] = {0};
1,442,770✔
1790

1791
      SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,442,770✔
1792

1793
      TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, pEncryptData), &lino, _exit);
1,442,770✔
1794

1795
      writer->files[ftype].size += TSDB_FHDR_SIZE;
1,442,770✔
1796
    }
1797
  }
1798

1799
  if (writer->ctx->reader) {
645,860✔
1800
    TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray), &lino, _exit);
247,405✔
1801
  }
1802

1803
_exit:
645,860✔
1804
  if (code) {
645,860✔
1805
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1806
              tstrerror(code));
1807
  }
1808
  return code;
645,860✔
1809
}
1810

1811
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
4,297,989✔
1812
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
4,297,989✔
1813
  if (!writer[0]) {
4,297,429✔
1814
    return terrno;
×
1815
  }
1816

1817
  writer[0]->config[0] = config[0];
4,297,890✔
1818
  return 0;
4,303,883✔
1819
}
1820

1821
int32_t tsdbDataFileWriterClose(SDataFileWriter **writer, bool abort, TFileOpArray *opArr) {
4,302,961✔
1822
  if (writer == NULL || writer[0] == NULL) return 0;
4,302,961✔
1823

1824
  int32_t code = 0;
4,302,961✔
1825
  int32_t lino = 0;
4,302,961✔
1826

1827
  if (writer[0]->ctx->opened) {
4,303,422✔
1828
    if (abort) {
781,462✔
1829
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseAbort(writer[0]), &lino, _exit);
×
1830
    } else {
1831
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseCommit(writer[0], opArr), &lino, _exit);
781,462✔
1832
    }
1833
    tsdbDataFileWriterDoClose(writer[0]);
781,462✔
1834
  }
1835
  taosMemoryFree(writer[0]);
4,303,883✔
1836
  writer[0] = NULL;
4,302,039✔
1837

1838
_exit:
4,302,039✔
1839
  if (code) {
4,302,961✔
1840
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer[0]->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1841
              tstrerror(code));
1842
  }
1843
  return code;
4,302,500✔
1844
}
1845

1846
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
2,147,483,647✔
1847
  int32_t code = 0;
2,147,483,647✔
1848
  int32_t lino = 0;
2,147,483,647✔
1849

1850
  if (!writer->ctx->opened) {
2,147,483,647✔
1851
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
82,604✔
1852
  }
1853

1854
  if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
2,147,483,647✔
1855
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
82,604✔
1856
  }
1857

1858
  if (row->uid != writer->ctx->tbid->uid) {
2,147,483,647✔
1859
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
165,003✔
1860
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row), &lino, _exit);
165,003✔
1861
  }
1862

1863
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, &row->row), &lino, _exit);
2,147,483,647✔
1864

1865
_exit:
2,147,483,647✔
1866
  if (code) {
2,147,483,647✔
1867
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1868
              tstrerror(code));
1869
  }
1870
  return code;
2,147,483,647✔
1871
}
1872

1873
int32_t tsdbDataFileWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
30,120,865✔
1874
  if (bData->nRow == 0) {
30,120,865✔
1875
    return 0;
×
1876
  }
1877

1878
  int32_t code = 0;
30,122,402✔
1879
  int32_t lino = 0;
30,122,402✔
1880

1881
  if (!bData->uid) {
30,122,733✔
1882
    return TSDB_CODE_INVALID_PARA;
×
1883
  }
1884

1885
  if (!writer->ctx->opened) {
30,120,187✔
1886
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
562,388✔
1887
  }
1888

1889
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
30,120,534✔
1890
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
563,256✔
1891
  }
1892

1893
  if (bData->uid != writer->ctx->tbid->uid) {
30,121,799✔
1894
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
13,587,497✔
1895
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData), &lino, _exit);
13,586,827✔
1896
  }
1897

1898
  if (writer->ctx->tbHasOldData) {
30,121,398✔
1899
    STsdbRowKey key;
2,120,478✔
1900

1901
    tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &key);
2,120,478✔
1902
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
2,121,204✔
1903
  }
1904

1905
  if (!writer->ctx->tbHasOldData       //
30,121,521✔
1906
      && writer->blockData->nRow == 0  //
28,222,938✔
1907
  ) {
1908
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, bData), &lino, _exit);
28,094,005✔
1909

1910
  } else {
1911
    for (int32_t i = 0; i < bData->nRow; ++i) {
2,147,483,647✔
1912
      TSDBROW row[1] = {tsdbRowFromBlockData(bData, i)};
2,147,483,647✔
1913
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, row), &lino, _exit);
2,147,483,647✔
1914
    }
1915
  }
1916

1917
_exit:
30,109,440✔
1918
  if (code) {
30,113,049✔
1919
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1920
              tstrerror(code));
1921
  }
1922
  return code;
30,114,039✔
1923
}
1924

1925
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
890,734✔
1926
  if (!writer->ctx->opened) {
890,734✔
1927
    return TSDB_CODE_INVALID_PARA;
×
1928
  }
1929

1930
  if (writer->blockData->nRow == 0) return 0;
890,734✔
1931
  if (writer->ctx->tbHasOldData) return 0;
890,734✔
1932

1933
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
773,789✔
1934
}
1935

1936
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
138,063✔
1937
  int32_t code = 0;
138,063✔
1938
  int32_t lino = 0;
138,063✔
1939

1940
  char    fname[TSDB_FILENAME_LEN];
138,063✔
1941
  int32_t ftype = TSDB_FTYPE_TOMB;
138,063✔
1942

1943
  int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
138,063✔
1944

1945
  int32_t lcn = writer->files[ftype].lcn;
138,063✔
1946
  tsdbTFileName(writer->config->tsdb, writer->files + ftype, fname);
138,063✔
1947

1948
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
138,063✔
1949

1950
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
138,063✔
1951
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
138,063✔
1952

1953
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, pEncryptData), &lino, _exit);
138,063✔
1954
  writer->files[ftype].size += TSDB_FHDR_SIZE;
138,063✔
1955

1956
  if (writer->ctx->reader) {
138,063✔
1957
    TAOS_CHECK_GOTO(tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray), &lino, _exit);
134,442✔
1958

1959
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
134,442✔
1960
      writer->ctx->hasOldTomb = true;
102,032✔
1961
    }
1962

1963
    writer->ctx->tombBlkArrayIdx = 0;
134,442✔
1964
    tTombBlockClear(writer->ctx->tombBlock);
134,442✔
1965
    writer->ctx->tombBlockIdx = 0;
134,442✔
1966
  }
1967

1968
_exit:
138,063✔
1969
  if (code) {
138,063✔
1970
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1971
              tstrerror(code));
1972
  }
1973
  return code;
138,063✔
1974
}
1975

1976
int32_t tsdbDataFileWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
398,503✔
1977
  int32_t code = 0;
398,503✔
1978
  int32_t lino = 0;
398,503✔
1979

1980
  if (!writer->ctx->opened) {
398,503✔
1981
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
136,470✔
1982
  }
1983

1984
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
398,503✔
1985
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenTombFD(writer), &lino, _exit);
138,063✔
1986
  }
1987

1988
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
398,503✔
1989

1990
_exit:
398,503✔
1991
  if (code) {
398,503✔
1992
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1993
              tstrerror(code));
1994
  }
1995
  return code;
398,503✔
1996
}
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