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

taosdata / TDengine / #5010

29 Mar 2026 04:32AM UTC coverage: 72.292% (+0.03%) from 72.26%
#5010

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253774 of 351039 relevant lines covered (72.29%)

133420324.04 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) {
26,260,270✔
42
  if (reader->ctx->headFooterLoaded) {
26,260,270✔
43
    return 0;
×
44
  }
45

46
  int32_t code = 0;
26,260,588✔
47
  int32_t lino = 0;
26,260,588✔
48

49
  int32_t ftype = TSDB_FTYPE_HEAD;
26,258,485✔
50
  if (reader->fd[ftype]) {
26,258,485✔
51
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
23,395,917✔
52

53
#if 1
54
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
23,397,654✔
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;
26,271,073✔
74

75
_exit:
26,274,564✔
76
  if (code) {
26,281,600✔
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;
26,257,017✔
81
}
82

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

88
  int32_t code = 0;
3,123,418✔
89
  int32_t lino = 0;
3,123,418✔
90

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

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

101
_exit:
3,124,069✔
102
  if (code) {
3,124,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,123,418✔
107
}
108

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

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

117
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); i++) {
291,402,620✔
118
    tBufferInit(reader[0]->local + i);
264,817,000✔
119
  }
120

121
  reader[0]->config[0] = config[0];
26,585,620✔
122
  reader[0]->buffers = config->buffers;
26,533,269✔
123
  if (reader[0]->buffers == NULL) {
26,523,387✔
124
    reader[0]->buffers = reader[0]->local;
26,131,256✔
125
  }
126

127
  if (fname) {
26,506,546✔
128
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
130,103,029✔
129
      if (fname[i]) {
104,077,878✔
130
        int32_t lcn = config->files[i].file.lcn;
72,527,712✔
131
        TAOS_CHECK_GOTO(tsdbOpenFile(fname[i], config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
72,537,544✔
132
      }
133
    }
134
  } else {
135
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2,532,090✔
136
      if (config->files[i].exist) {
2,025,672✔
137
        char fname1[TSDB_FILENAME_LEN];
1,045,326✔
138
        tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
1,045,326✔
139
        int32_t lcn = config->files[i].file.lcn;
1,045,326✔
140
        TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
1,045,326✔
141
      }
142
    }
143
  }
144

145
_exit:
26,531,569✔
146
  if (code) {
26,531,255✔
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;
26,531,255✔
151
}
152

153
void tsdbDataFileReaderClose(SDataFileReader **reader) {
52,223,058✔
154
  if (reader[0] == NULL) {
52,223,058✔
155
    return;
25,755,443✔
156
  }
157

158
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
26,506,081✔
159
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);
26,533,125✔
160

161
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
132,640,360✔
162
    if (reader[0]->fd[i]) {
106,101,990✔
163
      tsdbCloseFile(&reader[0]->fd[i]);
73,579,305✔
164
    }
165
  }
166

167
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
291,683,402✔
168
    tBufferDestroy(reader[0]->local + i);
265,157,382✔
169
  }
170

171
  taosMemoryFree(reader[0]);
26,526,020✔
172
  reader[0] = NULL;
26,518,673✔
173
}
174

175
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
26,281,457✔
176
  int32_t code = 0;
26,281,457✔
177
  int32_t lino = 0;
26,281,457✔
178
  void   *data = NULL;
26,288,226✔
179

180
  if (!reader->ctx->brinBlkLoaded) {
26,288,226✔
181
    TAOS_CHECK_GOTO(tsdbDataFileReadHeadFooter(reader), &lino, _exit);
26,289,950✔
182

183
    if (reader->headFooter->brinBlkPtr->size > 0) {
26,263,014✔
184
      data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
23,397,722✔
185
      if (data == NULL) {
23,389,409✔
186
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
187
      }
188

189
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
23,389,409✔
190

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

195
      int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
23,420,189✔
196
      TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
23,417,412✔
197
    } else {
198
      TARRAY2_INIT(reader->brinBlkArray);
2,870,047✔
199
    }
200

201
    reader->ctx->brinBlkLoaded = true;
26,273,021✔
202
  }
203
  brinBlkArray[0] = reader->brinBlkArray;
26,278,135✔
204

205
_exit:
26,279,234✔
206
  if (code) {
26,281,267✔
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;
26,281,267✔
212
}
213

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

218
  SBuffer *buffer = reader->buffers + 0;
21,735,307✔
219
  SBuffer *assist = reader->buffers + 1;
21,750,988✔
220

221
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
21,750,389✔
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,728,890✔
226
                                       pEncryptData),
227
                  &lino, _exit);
228

229
  // decode brin block
230
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
21,774,240✔
231
  tBrinBlockClear(brinBlock);
21,772,673✔
232
  brinBlock->numOfPKs = brinBlk->numOfPKs;
21,795,909✔
233
  brinBlock->numOfRecords = brinBlk->numRec;
21,792,048✔
234
  for (int32_t i = 0; i < 10; i++) {  // int64_t
239,641,568✔
235

236
    SCompressInfo cinfo = {
217,841,902✔
237
        .cmprAlg = brinBlk->cmprAlg,
217,836,601✔
238
        .dataType = TSDB_DATA_TYPE_BIGINT,
239
        .compressedSize = brinBlk->size[i],
217,855,205✔
240
        .originalSize = brinBlk->numRec * sizeof(int64_t),
217,859,245✔
241
    };
242
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
217,890,387✔
243
    br.offset += brinBlk->size[i];
217,833,668✔
244
  }
245

246
  for (int32_t i = 10; i < 15; i++) {  // int32_t
130,779,741✔
247
    SCompressInfo cinfo = {
108,976,319✔
248
        .cmprAlg = brinBlk->cmprAlg,
108,973,953✔
249
        .dataType = TSDB_DATA_TYPE_INT,
250
        .compressedSize = brinBlk->size[i],
108,974,327✔
251
        .originalSize = brinBlk->numRec * sizeof(int32_t),
108,975,865✔
252
    };
253
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
108,972,922✔
254
    br.offset += brinBlk->size[i];
108,979,264✔
255
  }
256

257
  // primary keys
258
  if (brinBlk->numOfPKs > 0) {  // decode the primary keys
21,803,422✔
259
    SValueColumnCompressInfo firstInfos[TD_MAX_PK_COLS];
3,839✔
260
    SValueColumnCompressInfo lastInfos[TD_MAX_PK_COLS];
3,839✔
261

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

269
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
7,678✔
270
      SValueColumnCompressInfo *info = firstInfos + i;
3,839✔
271

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

276
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
7,678✔
277
      SValueColumnCompressInfo *info = lastInfos + i;
3,839✔
278

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

284
  if (br.offset != br.buffer->size) {
21,799,799✔
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,796,211✔
292
  if (code) {
21,797,754✔
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,797,754✔
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,573,399✔
302
  int32_t code = 0;
2,573,399✔
303
  int32_t lino = 0;
2,573,399✔
304
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
2,573,399✔
305

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

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

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

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

321
  if (br.offset != buffer->size) {
2,573,399✔
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,573,399✔
329
  if (code) {
2,573,399✔
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,573,399✔
334
}
335

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

342
  SDiskDataHdr hdr;
86,196,162✔
343
  SBuffer     *buffer0 = reader->buffers + 0;
86,216,892✔
344
  SBuffer     *buffer1 = reader->buffers + 1;
86,217,344✔
345
  SBuffer     *assist = reader->buffers + 2;
86,220,613✔
346

347
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
86,211,814✔
348

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

355
  // SDiskDataHdr
356
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
86,171,452✔
357
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
86,183,709✔
358

359
  if (hdr.delimiter != TSDB_FILE_DLMT) {
86,113,901✔
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);
86,113,901✔
366
  bData->suid = hdr.suid;
86,163,564✔
367
  bData->uid = hdr.uid;
86,205,743✔
368
  bData->nRow = hdr.nRow;
86,182,580✔
369

370
  // Key part
371
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
86,199,911✔
372
  if (br.offset != buffer0->size) {
86,202,500✔
373
    tsdbError("vgId:%d %s failed at %s:%d since key part size mismatch, expected: %u, actual: %u, fname:%s",
40,842✔
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;
86,165,058✔
380
  for (int i = 0; i < ncid; i++) {
86,176,554✔
381
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
84,116,177✔
382
      extraColIdx = i;
84,098,771✔
383
      break;
84,098,771✔
384
    }
385
  }
386

387
  if (extraColIdx < 0) {
86,159,148✔
388
    goto _exit;
2,065,997✔
389
  }
390

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

397
  // calc szHint
398
  int64_t szHint = 0;
84,116,773✔
399
  int     extraCols = 1;
84,116,773✔
400
  for (int i = extraColIdx + 1; i < ncid; ++i) {
84,116,773✔
401
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
45,125,470✔
402
      ++extraCols;
45,138,640✔
403
      break;
45,138,640✔
404
    }
405
  }
406

407
  if (extraCols >= 2) {
84,129,943✔
408
    br = BUFFER_READER_INITIALIZER(0, buffer0);
45,136,293✔
409

410
    SBlockCol blockCol = {.cid = 0};
45,136,293✔
411
    for (int32_t i = extraColIdx; i < ncid; ++i) {
45,137,496✔
412
      int16_t extraColCid = cids[i];
45,112,319✔
413

414
      while (extraColCid > blockCol.cid) {
150,136,242✔
415
        if (br.offset >= buffer0->size) {
104,986,465✔
416
          blockCol.cid = INT16_MAX;
×
417
          break;
×
418
        }
419

420
        TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
104,998,625✔
421
      }
422

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

429
    if (blockCol.cid > 0 && blockCol.cid < INT16_MAX /*&& blockCol->flag == HAS_VALUE*/) {
45,174,954✔
430
      int64_t   offset = blockCol.offset;
45,148,821✔
431
      SBlockCol lastNonNoneBlockCol = {.cid = 0};
45,148,821✔
432

433
      for (int32_t i = extraColIdx; i < ncid; ++i) {
245,290,596✔
434
        int16_t extraColCid = cids[i];
200,124,749✔
435

436
        while (extraColCid > blockCol.cid) {
550,933,511✔
437
          if (br.offset >= buffer0->size) {
350,791,736✔
438
            blockCol.cid = INT16_MAX;
×
439
            break;
×
440
          }
441

442
          TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
350,784,755✔
443
        }
444

445
        if (extraColCid == blockCol.cid) {
200,141,775✔
446
          lastNonNoneBlockCol = blockCol;
200,141,775✔
447
          continue;
200,141,775✔
448
        }
449

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

455
      if (lastNonNoneBlockCol.cid > 0) {
45,165,847✔
456
        szHint = lastNonNoneBlockCol.offset + lastNonNoneBlockCol.szBitmap + lastNonNoneBlockCol.szOffset +
45,148,876✔
457
                 lastNonNoneBlockCol.szValue - offset;
45,148,876✔
458
      }
459
    }
460
  }
461

462
  // load each column
463
  SBlockCol blockCol = {
84,139,047✔
464
      .cid = 0,
465
  };
466
  bool firstRead = true;
84,103,515✔
467
  br = BUFFER_READER_INITIALIZER(0, buffer0);
84,103,515✔
468
  for (int32_t i = 0; i < ncid; i++) {
323,245,248✔
469
    int16_t cid = cids[i];
239,082,218✔
470

471
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
239,099,069✔
472
      continue;
1,426✔
473
    }
474

475
    while (cid > blockCol.cid) {
848,368,030✔
476
      if (br.offset >= buffer0->size) {
609,102,750✔
477
        blockCol.cid = INT16_MAX;
×
478
        break;
×
479
      }
480

481
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
609,153,150✔
482
    }
483

484
    if (cid < blockCol.cid) {
239,265,280✔
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) {
239,265,280✔
500
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
239,099,438✔
501

502
      // load from file
503
      tBufferClear(buffer1);
504
      TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA],
239,159,951✔
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;
239,137,163✔
511

512
      // decode the buffer
513
      SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
239,137,163✔
514
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
239,142,009✔
515
    }
516
  }
517

518
_exit:
86,381,510✔
519
  if (code) {
86,212,602✔
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;
86,212,602✔
524
}
525

526
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
32,750,737✔
527
                                 TColumnDataAggArray *columnDataAggArray) {
528
  int32_t  code = 0;
32,750,737✔
529
  int32_t  lino = 0;
32,750,737✔
530
  SBuffer *buffer = reader->buffers + 0;
32,763,568✔
531

532
  TARRAY2_CLEAR(columnDataAggArray, NULL);
32,794,753✔
533
  if (record->smaSize > 0) {
32,793,519✔
534
    tBufferClear(buffer);
535
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
32,795,340✔
536

537
    TAOS_CHECK_GOTO(
32,796,574✔
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);
32,778,855✔
543
    while (br.offset < record->smaSize) {
416,370,656✔
544
      SColumnDataAgg sma[1];
383,550,365✔
545

546
      TAOS_CHECK_GOTO(tGetColumnDataAgg(&br, sma), &lino, _exit);
383,431,810✔
547
      TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(columnDataAggArray, sma), &lino, _exit);
766,763,975✔
548
    }
549
    if (br.offset != record->smaSize) {
32,798,322✔
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:
32,791,516✔
558
  if (code) {
32,795,267✔
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;
32,795,267✔
563
}
564

565
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
5,071,410✔
566
  int32_t code = 0;
5,071,410✔
567
  int32_t lino = 0;
5,071,410✔
568
  void   *data = NULL;
5,072,719✔
569

570
  if (!reader->ctx->tombBlkLoaded) {
5,072,719✔
571
    TAOS_CHECK_GOTO(tsdbDataFileReadTombFooter(reader), &lino, _exit);
3,124,069✔
572

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

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

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

584
      int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
2,977,042✔
585
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
2,977,042✔
586
    } else {
587
      TARRAY2_INIT(reader->tombBlkArray);
145,724✔
588
    }
589

590
    reader->ctx->tombBlkLoaded = true;
3,122,766✔
591
  }
592
  tombBlkArray[0] = reader->tombBlkArray;
5,072,067✔
593

594
_exit:
5,072,719✔
595
  if (code) {
5,072,719✔
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;
5,072,719✔
601
}
602

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

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

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

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

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

632
_exit:
1,846,500✔
633
  if (code) {
1,846,500✔
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,846,500✔
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) {
799,313✔
695
  if (writer->ctx->reader) {
799,313✔
696
    tsdbDataFileReaderClose(&writer->ctx->reader);
392,893✔
697
  }
698

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

705
  tTombBlockDestroy(writer->ctx->tombBlock);
799,313✔
706
  tBlockDataDestroy(writer->ctx->blockData);
798,835✔
707
  tBrinBlockDestroy(writer->ctx->brinBlock);
799,313✔
708

709
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
8,786,707✔
710
    tBufferDestroy(writer->local + i);
7,987,394✔
711
  }
712

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

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

721
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2,734,029✔
722
    if (writer->config->files[i].exist) {
2,327,609✔
723
      SDataFileReaderConfig config[1] = {{
392,893✔
724
          .tsdb = writer->config->tsdb,
392,893✔
725
          .szPage = writer->config->szPage,
392,893✔
726
          .buffers = writer->buffers,
392,893✔
727
      }};
728

729
      for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
1,964,465✔
730
        config->files[i].exist = writer->config->files[i].exist;
1,571,572✔
731
        if (config->files[i].exist) {
1,571,572✔
732
          config->files[i].file = writer->config->files[i].file;
973,611✔
733
        }
734
      }
735

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

741
_exit:
799,313✔
742
  if (code) {
799,313✔
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;
799,313✔
747
}
748

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

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

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

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

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

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

816
  // .tomb
817
  ftype = TSDB_FTYPE_TOMB;
799,313✔
818
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
799,313✔
819
  TSDB_CHECK_CODE(code, lino, _exit);
799,313✔
820
  writer->files[ftype] = (STFile){
1,598,626✔
821
      .type = ftype,
822
      .did = diskId,
823
      .fid = writer->config->fid,
799,313✔
824
      .cid = writer->config->cid,
799,313✔
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};
799,313✔
832
  writer->ctx->tombRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
798,690✔
833

834
  writer->ctx->opened = true;
799,313✔
835

836
_exit:
798,690✔
837
  if (code) {
798,690✔
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;
798,690✔
842
}
843

844
void tsdbWriterUpdVerRange(SVersionRange *range, int64_t minVer, int64_t maxVer) {
78,129,932✔
845
  range->minVer = TMIN(range->minVer, minVer);
78,129,932✔
846
  range->maxVer = TMAX(range->maxVer, maxVer);
78,162,294✔
847
}
78,160,958✔
848

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

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

861
  SBrinBlk brinBlk = {
759,104✔
862
      .dp[0] =
863
          {
864
              .offset = *fileSize,
759,104✔
865
              .size = 0,
866
          },
867
      .numRec = brinBlock->numOfRecords,
759,104✔
868
      .numOfPKs = brinBlock->numOfPKs,
759,104✔
869
      .cmprAlg = cmprAlg,
870
  };
871
  for (int i = 0; i < brinBlock->numOfRecords; i++) {
48,344,197✔
872
    SBrinRecord record;
47,585,093✔
873

874
    TAOS_CHECK_RETURN(tBrinBlockGet(brinBlock, i, &record));
47,585,093✔
875
    if (i == 0) {
47,585,093✔
876
      brinBlk.minTbid.suid = record.suid;
759,104✔
877
      brinBlk.minTbid.uid = record.uid;
759,104✔
878
      brinBlk.minVer = record.minVer;
759,104✔
879
      brinBlk.maxVer = record.maxVer;
759,104✔
880
    }
881
    if (i == brinBlock->numOfRecords - 1) {
47,585,093✔
882
      brinBlk.maxTbid.suid = record.suid;
759,104✔
883
      brinBlk.maxTbid.uid = record.uid;
759,104✔
884
    }
885
    if (record.minVer < brinBlk.minVer) {
47,585,093✔
886
      brinBlk.minVer = record.minVer;
497,283✔
887
    }
888
    if (record.maxVer > brinBlk.maxVer) {
47,585,093✔
889
      brinBlk.maxVer = record.maxVer;
13,507,532✔
890
    }
891
  }
892

893
  tsdbWriterUpdVerRange(range, brinBlk.minVer, brinBlk.maxVer);
759,104✔
894

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

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

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

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

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

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

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

954
  tBrinBlockClear(brinBlock);
759,104✔
955

956
  return 0;
759,104✔
957
}
958

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

964
  int32_t code = 0;
759,104✔
965
  int32_t lino = 0;
759,104✔
966

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

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

974
_exit:
759,104✔
975
  if (code) {
759,104✔
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;
759,104✔
980
}
981

982
static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
47,575,424✔
983
  int32_t code = 0;
47,575,424✔
984
  int32_t lino = 0;
47,575,424✔
985

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

998
  if ((writer->brinBlock->numOfRecords) >= 256) {
47,547,659✔
999
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
97,579✔
1000
  }
1001

1002
_exit:
47,557,953✔
1003
  if (code) {
47,549,655✔
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;
47,549,655✔
1008
}
1009

1010
static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
46,907,660✔
1011
  if (bData->nRow == 0) {
46,907,660✔
1012
    return 0;
13,080,349✔
1013
  }
1014

1015
  if (!bData->uid) {
33,838,714✔
1016
    return TSDB_CODE_INVALID_PARA;
×
1017
  }
1018

1019
  int32_t  code = 0;
33,840,801✔
1020
  int32_t  lino = 0;
33,840,801✔
1021
  SBuffer *buffers = writer->buffers;
33,842,572✔
1022
  SBuffer *assist = writer->buffers + 4;
33,839,321✔
1023

1024
  SColCompressInfo cmprInfo = {.pColCmpr = NULL, .defaultCmprAlg = writer->config->cmprAlg};
33,838,670✔
1025

1026
  SBrinRecord record[1] = {{
33,836,494✔
1027
      .suid = bData->suid,
33,835,430✔
1028
      .uid = bData->uid,
33,834,889✔
1029
      .minVer = bData->aVersion[0],
33,837,182✔
1030
      .maxVer = bData->aVersion[0],
33,836,211✔
1031
      .blockOffset = writer->files[TSDB_FTYPE_DATA].size,
33,839,547✔
1032
      .smaOffset = writer->files[TSDB_FTYPE_SMA].size,
33,840,960✔
1033
      .blockSize = 0,
1034
      .blockKeySize = 0,
1035
      .smaSize = 0,
1036
      .numRow = bData->nRow,
33,838,832✔
1037
      .count = 1,
1038
  }};
1039

1040
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &record->firstKey);
33,840,855✔
1041
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, bData->nRow - 1), &record->lastKey);
33,844,575✔
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];
4,987,441✔
1049
    }
1050
    if (bData->aVersion[i] > record->maxVer) {
2,147,483,647✔
1051
      record->maxVer = bData->aVersion[i];
102,490,226✔
1052
    }
1053
  }
1054

1055
  tsdbWriterUpdVerRange(&writer->ctx->range, record->minVer, record->maxVer);
33,844,802✔
1056

1057
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, bData->suid != 0 ? bData->suid : bData->uid,
33,844,997✔
1058
                        &cmprInfo.pColCmpr);
1059
  if (code) {
33,836,822✔
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);
33,836,822✔
1064

1065
  record->blockKeySize = buffers[0].size + buffers[1].size;
33,814,838✔
1066
  record->blockSize = record->blockKeySize + buffers[2].size + buffers[3].size;
33,831,963✔
1067

1068
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
33,830,045✔
1069

1070
  for (int i = 0; i < 4; i++) {
169,166,528✔
1071
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, buffers[i].data,
135,322,623✔
1072
                                  buffers[i].size, pEncryptData),
1073
                    &lino, _exit);
1074
    writer->files[TSDB_FTYPE_DATA].size += buffers[i].size;
135,322,477✔
1075
  }
1076

1077
  // to .sma file
1078
  tBufferClear(&buffers[0]);
1079
  for (int32_t i = 0; i < bData->nColData; ++i) {
262,004,805✔
1080
    SColData *colData = bData->aColData + i;
228,168,400✔
1081
    if ((colData->cflag & COL_SMA_ON) == 0 || ((colData->flag & HAS_VALUE) == 0)) continue;
228,176,335✔
1082

1083
    SColumnDataAgg sma[1] = {{.colId = colData->cid}};
222,590,572✔
1084
    tColDataCalcSMA[colData->type](colData, sma);
222,582,327✔
1085

1086
    TAOS_CHECK_GOTO(tPutColumnDataAgg(&buffers[0], sma), &lino, _exit);
222,570,785✔
1087
  }
1088
  record->smaSize = buffers[0].size;
33,843,037✔
1089

1090
  if (record->smaSize > 0) {
33,843,037✔
1091
    TAOS_CHECK_GOTO(
33,843,648✔
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;
33,830,525✔
1095
  }
1096

1097
  // append SBrinRecord
1098
  TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
33,834,261✔
1099

1100
  tBlockDataClear(bData);
33,802,379✔
1101

1102
_exit:
33,833,391✔
1103
  if (code) {
33,825,404✔
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);
33,825,404✔
1108
  return code;
33,813,404✔
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,738,550✔
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;
533,852✔
1149
  } else if (key2 == NULL) {
2,147,483,647✔
1150
    return -1;
7,103,540✔
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 (;;) {
6,924✔
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,699,543✔
1181
        break;
189,055✔
1182
      }
1183

1184
      for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
8,668,104✔
1185
        SBrinRecord record;
8,548,594✔
1186
        code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record);
8,548,594✔
1187
        TSDB_CHECK_CODE(code, lino, _exit);
8,548,594✔
1188
        if (record.uid != writer->ctx->tbid->uid) {
8,548,594✔
1189
          writer->ctx->tbHasOldData = false;
187,906✔
1190
          goto _exit;
187,906✔
1191
        }
1192

1193
        if (tsdbRowKeyCmprNullAsLargest(key, &record.firstKey) < 0) {  // key < record->firstKey
8,360,688✔
1194
          goto _exit;
3,906,246✔
1195
        } else {
1196
          SBrinRecord record[1];
4,454,442✔
1197
          code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
4,454,442✔
1198
          TSDB_CHECK_CODE(code, lino, _exit);
4,454,442✔
1199
          if (tsdbRowKeyCmprNullAsLargest(key, &record->lastKey) > 0) {  // key > record->lastKey
4,454,442✔
1200
            if (writer->blockData->nRow > 0) {
2,157,616✔
1201
              TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
1,570✔
1202
            }
1203

1204
            TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
2,157,616✔
1205
          } else {
1206
            TAOS_CHECK_GOTO(tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData), &lino,
2,296,826✔
1207
                            _exit);
1208

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

1217
    // SBrinBlk
1218
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
189,055✔
1219
      writer->ctx->brinBlkArray = NULL;
182,131✔
1220
      writer->ctx->tbHasOldData = false;
182,131✔
1221
      goto _exit;
182,131✔
1222
    } else {
1223
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
6,924✔
1224

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

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

1232
      writer->ctx->brinBlockIdx = 0;
6,924✔
1233
      writer->ctx->brinBlkArrayIdx++;
6,924✔
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,701,749✔
1266
  if (writer->ctx->tbid->uid == 0) {
14,701,749✔
1267
    return 0;
661,525✔
1268
  }
1269

1270
  int32_t code = 0;
14,044,204✔
1271
  int32_t lino = 0;
14,044,204✔
1272

1273
  if (writer->ctx->tbHasOldData) {
14,043,199✔
1274
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, NULL /* as the largest key */), &lino, _exit);
135,288✔
1275
  }
1276

1277
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
14,045,551✔
1278

1279
_exit:
14,045,956✔
1280
  if (code) {
14,045,956✔
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;
14,042,864✔
1285
}
1286

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

1291
  SMetaInfo info;
14,703,984✔
1292
  bool      drop = false;
14,704,647✔
1293
  TABLEID   tbid1[1];
14,704,647✔
1294
  writer->ctx->tbHasOldData = false;
14,706,811✔
1295
  while (writer->ctx->brinBlkArray) {  // skip data of previous table
14,984,719✔
1296
    for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
14,732,432✔
1297
      SBrinRecord record;
14,381,340✔
1298
      TAOS_CHECK_GOTO(tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record), &lino, _exit);
14,381,340✔
1299

1300
      if (record.uid == tbid->uid) {
14,380,593✔
1301
        writer->ctx->tbHasOldData = true;
370,037✔
1302
        goto _begin;
370,037✔
1303
      } else if (record.suid > tbid->suid || (record.suid == tbid->suid && record.uid > tbid->uid)) {
14,010,556✔
1304
        goto _begin;
2,390,948✔
1305
      } else {
1306
        if (record.uid != writer->ctx->tbid->uid) {
11,619,608✔
1307
          if (drop && tbid1->uid == record.uid) {
3,969,258✔
1308
            continue;
×
1309
          } else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, record.uid, &info, NULL) != 0) {
3,969,258✔
1310
            drop = true;
38,716✔
1311
            tbid1->suid = record.suid;
38,716✔
1312
            tbid1->uid = record.uid;
38,716✔
1313
            continue;
38,716✔
1314
          } else {
1315
            drop = false;
3,930,542✔
1316
            writer->ctx->tbid->suid = record.suid;
3,930,542✔
1317
            writer->ctx->tbid->uid = record.uid;
3,930,542✔
1318
          }
1319
        }
1320

1321
        TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, &record), &lino, _exit);
11,580,892✔
1322
      }
1323
    }
1324

1325
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
352,006✔
1326
      writer->ctx->brinBlkArray = NULL;
74,768✔
1327
      break;
74,768✔
1328
    } else {
1329
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
277,238✔
1330

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

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

1338
_begin:
14,701,960✔
1339
  writer->ctx->tbid[0] = *tbid;
14,706,148✔
1340

1341
  if (tbid->uid == INT64_MAX) {
14,707,076✔
1342
    goto _exit;
661,525✔
1343
  }
1344

1345
  TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, tbid, writer->config->skmTb), &lino, _exit);
14,044,462✔
1346
  TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, writer->ctx->tbid, writer->config->skmTb->pTSchema, NULL, 0), &lino,
14,042,318✔
1347
                  _exit);
1348

1349
_exit:
14,708,661✔
1350
  if (code) {
14,708,661✔
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,708,661✔
1355
}
1356

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

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

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

1372
  SBuffer *buffer0 = buffers + 0;
1,688,764✔
1373
  SBuffer *assist = buffers + 1;
1,688,764✔
1374

1375
  STombBlk tombBlk = {
1,688,764✔
1376
      .dp[0] =
1377
          {
1378
              .offset = *fileSize,
1,688,764✔
1379
              .size = 0,
1380
          },
1381
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
1,688,764✔
1382
      .cmprAlg = cmprAlg,
1383
  };
1384
  for (int i = 0; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
41,362,635✔
1385
    STombRecord record;
39,674,534✔
1386
    TAOS_CHECK_RETURN(tTombBlockGet(tombBlock, i, &record));
39,674,534✔
1387

1388
    if (i == 0) {
39,673,871✔
1389
      tombBlk.minTbid.suid = record.suid;
1,688,101✔
1390
      tombBlk.minTbid.uid = record.uid;
1,688,101✔
1391
      tombBlk.minVer = record.version;
1,688,101✔
1392
      tombBlk.maxVer = record.version;
1,688,101✔
1393
    }
1394
    if (i == TOMB_BLOCK_SIZE(tombBlock) - 1) {
39,673,871✔
1395
      tombBlk.maxTbid.suid = record.suid;
1,688,764✔
1396
      tombBlk.maxTbid.uid = record.uid;
1,688,764✔
1397
    }
1398
    if (record.version < tombBlk.minVer) {
39,673,871✔
1399
      tombBlk.minVer = record.version;
3,021✔
1400
    }
1401
    if (record.version > tombBlk.maxVer) {
39,673,871✔
1402
      tombBlk.maxVer = record.version;
35,748,043✔
1403
    }
1404
  }
1405

1406
  tsdbWriterUpdVerRange(range, tombBlk.minVer, tombBlk.maxVer);
1,688,101✔
1407

1408
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); i++) {
10,132,584✔
1409
    tBufferClear(buffer0);
1410

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

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

1424
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(tombBlkArray, &tombBlk));
3,377,528✔
1425

1426
  tTombBlockClear(tombBlock);
1,688,764✔
1427
  return 0;
1,688,764✔
1428
}
1429

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

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

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

1440
_exit:
661,525✔
1441
  if (code) {
661,525✔
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;
661,525✔
1446
}
1447

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

1451
  int32_t code = 0;
140,345✔
1452
  int32_t lino = 0;
140,345✔
1453

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

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

1461
_exit:
140,345✔
1462
  if (code) {
140,345✔
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;
140,345✔
1467
}
1468

1469
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize,
18,154,393✔
1470
                             SEncryptData *encryptData) {
1471
  ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
18,154,393✔
1472
  if (ptr->size > 0) {
18,177,222✔
1473
    ptr->offset = *fileSize;
1,688,764✔
1474

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

1478
    *fileSize += ptr->size;
1,688,764✔
1479
  }
1480
  return 0;
18,170,516✔
1481
}
1482

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

1488
  int32_t code = 0;
140,345✔
1489
  int32_t lino = 0;
140,345✔
1490

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

1493
  TAOS_CHECK_GOTO(
140,345✔
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:
140,345✔
1499
  if (code) {
140,345✔
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;
140,345✔
1504
}
1505

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

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

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

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

1522
_exit:
140,345✔
1523
  if (code) {
140,345✔
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;
140,345✔
1528
}
1529

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

1534
  while (writer->ctx->hasOldTomb) {
648,791✔
1535
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
33,265,804✔
1536
      STombRecord record1[1];
33,059,780✔
1537
      TAOS_CHECK_GOTO(tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1), &lino, _exit);
33,059,780✔
1538

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

1545
        tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
33,059,780✔
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) {
33,059,780✔
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)) {
206,024✔
1561
      writer->ctx->hasOldTomb = false;
103,012✔
1562
      break;
103,012✔
1563
    } else {
1564
      const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);
103,012✔
1565

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

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

1573
_write:
545,779✔
1574
  if (record->suid == INT64_MAX) {
545,779✔
1575
    goto _exit;
140,345✔
1576
  }
1577

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

1580
  tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
405,434✔
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) {
405,434✔
1586
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1587
  }
1588

1589
_exit:
545,779✔
1590
  if (code) {
545,779✔
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;
545,779✔
1595
}
1596

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

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

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

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

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

1617
  TAOS_CHECK_GOTO(
661,525✔
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:
661,525✔
1623
  if (code) {
661,525✔
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;
661,525✔
1628
}
1629

1630
void tsdbTFileUpdVerRange(STFile *f, SVersionRange range) {
20,903,446✔
1631
  f->minVer = TMIN(f->minVer, range.minVer);
20,903,446✔
1632
  f->maxVer = TMAX(f->maxVer, range.maxVer);
20,950,575✔
1633
}
20,958,056✔
1634

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

1639
  int32_t  ftype;
1640
  STFileOp op;
799,313✔
1641

1642
  if (writer->fd[TSDB_FTYPE_HEAD]) {
799,313✔
1643
    TABLEID tbid[1] = {{
661,525✔
1644
        .suid = INT64_MAX,
1645
        .uid = INT64_MAX,
1646
    }};
1647

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

1654
    SVersionRange ofRange = {.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
661,525✔
1655

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

1676
    // .data
1677
    ftype = TSDB_FTYPE_DATA;
661,525✔
1678
    if (!writer->config->files[ftype].exist) {
661,525✔
1679
      op = (STFileOp){
404,626✔
1680
          .optype = TSDB_FOP_CREATE,
1681
          .fid = writer->config->fid,
404,626✔
1682
          .nf = writer->files[ftype],
404,626✔
1683
      };
1684
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
404,626✔
1685
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
809,252✔
1686
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
256,899✔
1687
      op = (STFileOp){
256,899✔
1688
          .optype = TSDB_FOP_MODIFY,
1689
          .fid = writer->config->fid,
256,899✔
1690
          .of = writer->config->files[ftype].file,
256,899✔
1691
          .nf = writer->files[ftype],
256,899✔
1692
      };
1693
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
256,899✔
1694
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
513,798✔
1695
    }
1696

1697
    // .sma
1698
    ftype = TSDB_FTYPE_SMA;
661,525✔
1699
    if (!writer->config->files[ftype].exist) {
661,525✔
1700
      op = (STFileOp){
404,626✔
1701
          .optype = TSDB_FOP_CREATE,
1702
          .fid = writer->config->fid,
404,626✔
1703
          .nf = writer->files[ftype],
404,626✔
1704
      };
1705
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
404,626✔
1706
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
809,252✔
1707
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
256,899✔
1708
      op = (STFileOp){
256,899✔
1709
          .optype = TSDB_FOP_MODIFY,
1710
          .fid = writer->config->fid,
256,899✔
1711
          .of = writer->config->files[ftype].file,
256,899✔
1712
          .nf = writer->files[ftype],
256,899✔
1713
      };
1714
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
256,899✔
1715
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
513,798✔
1716
    }
1717
  }
1718

1719
  if (writer->fd[TSDB_FTYPE_TOMB]) {
799,313✔
1720
    STombRecord record[1] = {{
140,345✔
1721
        .suid = INT64_MAX,
1722
        .uid = INT64_MAX,
1723
        .version = INT64_MAX,
1724
    }};
1725

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

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

1733
    ftype = TSDB_FTYPE_TOMB;
140,345✔
1734
    if (writer->config->files[ftype].exist) {
140,345✔
1735
      op = (STFileOp){
103,012✔
1736
          .optype = TSDB_FOP_REMOVE,
1737
          .fid = writer->config->fid,
103,012✔
1738
          .of = writer->config->files[ftype].file,
103,012✔
1739
      };
1740
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
103,012✔
1741
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
206,024✔
1742
    }
1743
    op = (STFileOp){
140,345✔
1744
        .optype = TSDB_FOP_CREATE,
1745
        .fid = writer->config->fid,
140,345✔
1746
        .nf = writer->files[ftype],
140,345✔
1747
    };
1748
    tsdbTFileUpdVerRange(&op.nf, ofRange);
140,345✔
1749
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->tombRange);
140,345✔
1750
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
280,690✔
1751
  }
1752
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
799,313✔
1753
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
3,996,087✔
1754
    if (writer->fd[i]) {
3,196,774✔
1755
      TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd[i], pEncryptData), &lino, _exit);
2,124,920✔
1756
      tsdbCloseFile(&writer->fd[i]);
2,124,173✔
1757
    }
1758
  }
1759

1760
_exit:
799,313✔
1761
  if (code) {
799,313✔
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;
799,313✔
1766
}
1767

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

1772
  int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};
661,525✔
1773

1774
  for (int32_t i = 0; i < ARRAY_SIZE(ftypes); ++i) {
2,646,100✔
1775
    int32_t ftype = ftypes[i];
1,984,575✔
1776

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

1780
    if (writer->files[ftype].size == 0) {
1,984,575✔
1781
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
1,470,777✔
1782
    }
1783

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

1788
    if (writer->files[ftype].size == 0) {
1,983,952✔
1789
      uint8_t hdr[TSDB_FHDR_SIZE] = {0};
1,470,777✔
1790

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

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

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

1799
  if (writer->ctx->reader) {
661,525✔
1800
    TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray), &lino, _exit);
256,899✔
1801
  }
1802

1803
_exit:
661,525✔
1804
  if (code) {
661,525✔
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;
661,525✔
1809
}
1810

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

1817
  writer[0]->config[0] = config[0];
4,433,870✔
1818
  return 0;
4,440,562✔
1819
}
1820

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

1824
  int32_t code = 0;
4,441,214✔
1825
  int32_t lino = 0;
4,441,214✔
1826

1827
  if (writer[0]->ctx->opened) {
4,441,035✔
1828
    if (abort) {
799,313✔
1829
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseAbort(writer[0]), &lino, _exit);
×
1830
    } else {
1831
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseCommit(writer[0], opArr), &lino, _exit);
799,313✔
1832
    }
1833
    tsdbDataFileWriterDoClose(writer[0]);
799,313✔
1834
  }
1835
  taosMemoryFree(writer[0]);
4,442,170✔
1836
  writer[0] = NULL;
4,438,950✔
1837

1838
_exit:
4,438,950✔
1839
  if (code) {
4,440,384✔
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,439,123✔
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);
84,317✔
1852
  }
1853

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

1858
  if (row->uid != writer->ctx->tbid->uid) {
2,147,483,647✔
1859
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
168,426✔
1860
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row), &lino, _exit);
168,426✔
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) {
31,394,256✔
1874
  if (bData->nRow == 0) {
31,394,256✔
1875
    return 0;
×
1876
  }
1877

1878
  int32_t code = 0;
31,397,266✔
1879
  int32_t lino = 0;
31,397,266✔
1880

1881
  if (!bData->uid) {
31,396,308✔
1882
    return TSDB_CODE_INVALID_PARA;
×
1883
  }
1884

1885
  if (!writer->ctx->opened) {
31,397,641✔
1886
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
576,340✔
1887
  }
1888

1889
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
31,397,634✔
1890
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
576,585✔
1891
  }
1892

1893
  if (bData->uid != writer->ctx->tbid->uid) {
31,398,759✔
1894
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
13,877,788✔
1895
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData), &lino, _exit);
13,875,108✔
1896
  }
1897

1898
  if (writer->ctx->tbHasOldData) {
31,396,697✔
1899
    STsdbRowKey key;
2,176,345✔
1900

1901
    tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &key);
2,176,345✔
1902
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
2,176,345✔
1903
  }
1904

1905
  if (!writer->ctx->tbHasOldData       //
31,396,182✔
1906
      && writer->blockData->nRow == 0  //
29,447,382✔
1907
  ) {
1908
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, bData), &lino, _exit);
29,316,577✔
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:
31,365,133✔
1918
  if (code) {
31,367,768✔
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;
31,368,756✔
1923
}
1924

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

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

1933
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
822,359✔
1934
}
1935

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

1940
  char    fname[TSDB_FILENAME_LEN];
140,345✔
1941
  int32_t ftype = TSDB_FTYPE_TOMB;
140,345✔
1942

1943
  int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
140,345✔
1944

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

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

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

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

1956
  if (writer->ctx->reader) {
140,345✔
1957
    TAOS_CHECK_GOTO(tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray), &lino, _exit);
136,592✔
1958

1959
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
136,592✔
1960
      writer->ctx->hasOldTomb = true;
103,012✔
1961
    }
1962

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

1968
_exit:
140,345✔
1969
  if (code) {
140,345✔
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;
140,345✔
1974
}
1975

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

1980
  if (!writer->ctx->opened) {
405,434✔
1981
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
138,656✔
1982
  }
1983

1984
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
405,434✔
1985
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenTombFD(writer), &lino, _exit);
140,345✔
1986
  }
1987

1988
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
405,434✔
1989

1990
_exit:
405,434✔
1991
  if (code) {
405,434✔
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;
405,434✔
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