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

taosdata / TDengine / #4995

18 Mar 2026 06:26AM UTC coverage: 71.996% (-0.2%) from 72.244%
#4995

push

travis-ci

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

2 of 4 new or added lines in 2 files covered. (50.0%)

5312 existing lines in 167 files now uncovered.

244665 of 339830 relevant lines covered (72.0%)

135013613.72 hits per line

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

90.94
/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) {
27,468,887✔
42
  if (reader->ctx->headFooterLoaded) {
27,468,887✔
43
    return 0;
×
44
  }
45

46
  int32_t code = 0;
27,471,484✔
47
  int32_t lino = 0;
27,471,484✔
48

49
  int32_t ftype = TSDB_FTYPE_HEAD;
27,465,496✔
50
  if (reader->fd[ftype]) {
27,465,496✔
51
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
24,650,721✔
52

53
#if 1
54
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
24,658,552✔
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;
27,470,835✔
74

75
_exit:
27,465,876✔
76
  if (code) {
27,465,876✔
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;
27,463,693✔
81
}
82

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

88
  int32_t code = 0;
3,061,168✔
89
  int32_t lino = 0;
3,061,168✔
90

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

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

101
_exit:
3,060,531✔
102
  if (code) {
3,060,531✔
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,060,531✔
107
}
108

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

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

117
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); i++) {
304,645,785✔
118
    tBufferInit(reader[0]->local + i);
276,919,945✔
119
  }
120

121
  reader[0]->config[0] = config[0];
27,725,840✔
122
  reader[0]->buffers = config->buffers;
27,713,884✔
123
  if (reader[0]->buffers == NULL) {
27,709,070✔
124
    reader[0]->buffers = reader[0]->local;
27,330,291✔
125
  }
126

127
  if (fname) {
27,711,752✔
128
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
136,062,890✔
129
      if (fname[i]) {
108,846,986✔
130
        int32_t lcn = config->files[i].file.lcn;
76,226,350✔
131
        TAOS_CHECK_GOTO(tsdbOpenFile(fname[i], config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
76,227,216✔
132
      }
133
    }
134
  } else {
135
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2,503,245✔
136
      if (config->files[i].exist) {
2,002,854✔
137
        char fname1[TSDB_FILENAME_LEN];
1,018,655✔
138
        tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
1,018,655✔
139
        int32_t lcn = config->files[i].file.lcn;
1,018,655✔
140
        TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
1,018,655✔
141
      }
142
    }
143
  }
144

145
_exit:
27,716,940✔
146
  if (code) {
27,714,950✔
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;
27,714,950✔
151
}
152

153
void tsdbDataFileReaderClose(SDataFileReader **reader) {
46,780,962✔
154
  if (reader[0] == NULL) {
46,780,962✔
155
    return;
19,084,056✔
156
  }
157

158
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
27,706,867✔
159
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);
27,714,947✔
160

161
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
138,569,469✔
162
    if (reader[0]->fd[i]) {
110,852,852✔
163
      tsdbCloseFile(&reader[0]->fd[i]);
77,250,613✔
164
    }
165
  }
166

167
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
304,810,922✔
168
    tBufferDestroy(reader[0]->local + i);
277,087,500✔
169
  }
170

171
  taosMemoryFree(reader[0]);
27,723,422✔
172
  reader[0] = NULL;
27,712,078✔
173
}
174

175
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
27,468,014✔
176
  int32_t code = 0;
27,468,014✔
177
  int32_t lino = 0;
27,468,014✔
178
  void   *data = NULL;
27,468,014✔
179

180
  if (!reader->ctx->brinBlkLoaded) {
27,468,014✔
181
    TAOS_CHECK_GOTO(tsdbDataFileReadHeadFooter(reader), &lino, _exit);
27,468,150✔
182

183
    if (reader->headFooter->brinBlkPtr->size > 0) {
27,462,488✔
184
      data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
24,649,012✔
185
      if (data == NULL) {
24,649,794✔
186
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
187
      }
188

189
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
24,649,794✔
190

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

195
      int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
24,657,755✔
196
      TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
24,657,018✔
197
    } else {
198
      TARRAY2_INIT(reader->brinBlkArray);
2,812,796✔
199
    }
200

201
    reader->ctx->brinBlkLoaded = true;
27,465,399✔
202
  }
203
  brinBlkArray[0] = reader->brinBlkArray;
27,471,538✔
204

205
_exit:
27,467,057✔
206
  if (code) {
27,468,863✔
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;
27,468,863✔
212
}
213

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

218
  SBuffer *buffer = reader->buffers + 0;
23,074,146✔
219
  SBuffer *assist = reader->buffers + 1;
23,072,331✔
220

221
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
23,079,669✔
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,
23,072,802✔
226
                                       pEncryptData),
227
                  &lino, _exit);
228

229
  // decode brin block
230
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
23,081,200✔
231
  tBrinBlockClear(brinBlock);
23,078,603✔
232
  brinBlock->numOfPKs = brinBlk->numOfPKs;
23,083,190✔
233
  brinBlock->numOfRecords = brinBlk->numRec;
23,083,054✔
234
  for (int32_t i = 0; i < 10; i++) {  // int64_t
253,907,432✔
235

236
    SCompressInfo cinfo = {
230,822,569✔
237
        .cmprAlg = brinBlk->cmprAlg,
230,817,888✔
238
        .dataType = TSDB_DATA_TYPE_BIGINT,
239
        .compressedSize = brinBlk->size[i],
230,823,550✔
240
        .originalSize = brinBlk->numRec * sizeof(int64_t),
230,820,403✔
241
    };
242
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
230,824,420✔
243
    br.offset += brinBlk->size[i];
230,826,175✔
244
  }
245

246
  for (int32_t i = 10; i < 15; i++) {  // int32_t
138,501,780✔
247
    SCompressInfo cinfo = {
115,417,983✔
248
        .cmprAlg = brinBlk->cmprAlg,
115,418,119✔
249
        .dataType = TSDB_DATA_TYPE_INT,
250
        .compressedSize = brinBlk->size[i],
115,417,983✔
251
        .originalSize = brinBlk->numRec * sizeof(int32_t),
115,418,720✔
252
    };
253
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
115,419,185✔
254
    br.offset += brinBlk->size[i];
115,417,980✔
255
  }
256

257
  // primary keys
258
  if (brinBlk->numOfPKs > 0) {  // decode the primary keys
23,083,797✔
259
    SValueColumnCompressInfo firstInfos[TD_MAX_PK_COLS];
4,093✔
260
    SValueColumnCompressInfo lastInfos[TD_MAX_PK_COLS];
4,093✔
261

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

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

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

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

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

284
  if (br.offset != br.buffer->size) {
23,084,863✔
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:
23,077,202✔
292
  if (code) {
23,083,196✔
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;
23,083,196✔
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,563,944✔
302
  int32_t code = 0;
2,563,944✔
303
  int32_t lino = 0;
2,563,944✔
304
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
2,563,944✔
305

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

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

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

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

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

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

342
  SDiskDataHdr hdr;
86,018,523✔
343
  SBuffer     *buffer0 = reader->buffers + 0;
86,023,815✔
344
  SBuffer     *buffer1 = reader->buffers + 1;
86,028,051✔
345
  SBuffer     *assist = reader->buffers + 2;
86,026,555✔
346

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

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

355
  // SDiskDataHdr
356
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
86,019,107✔
357
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
86,022,441✔
358

359
  if (hdr.delimiter != TSDB_FILE_DLMT) {
85,972,784✔
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);
85,972,784✔
366
  bData->suid = hdr.suid;
85,999,912✔
367
  bData->uid = hdr.uid;
86,021,545✔
368
  bData->nRow = hdr.nRow;
85,993,229✔
369

370
  // Key part
371
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
86,021,624✔
372
  if (br.offset != buffer0->size) {
86,027,432✔
373
    tsdbError("vgId:%d %s failed at %s:%d since key part size mismatch, expected: %u, actual: %u, fname:%s",
24,011✔
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,005,458✔
380
  for (int i = 0; i < ncid; i++) {
86,018,326✔
381
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
83,956,239✔
382
      extraColIdx = i;
83,934,535✔
383
      break;
83,934,535✔
384
    }
385
  }
386

387
  if (extraColIdx < 0) {
85,996,622✔
388
    goto _exit;
2,069,968✔
389
  }
390

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

397
  // calc szHint
398
  int64_t szHint = 0;
83,937,781✔
399
  int     extraCols = 1;
83,937,781✔
400
  for (int i = extraColIdx + 1; i < ncid; ++i) {
83,937,781✔
401
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
44,842,554✔
402
      ++extraCols;
44,846,972✔
403
      break;
44,846,972✔
404
    }
405
  }
406

407
  if (extraCols >= 2) {
83,942,199✔
408
    br = BUFFER_READER_INITIALIZER(0, buffer0);
44,846,163✔
409

410
    SBlockCol blockCol = {.cid = 0};
44,846,163✔
411
    for (int32_t i = extraColIdx; i < ncid; ++i) {
44,847,226✔
412
      int16_t extraColCid = cids[i];
44,834,488✔
413

414
      while (extraColCid > blockCol.cid) {
150,263,086✔
415
        if (br.offset >= buffer0->size) {
105,424,060✔
416
          blockCol.cid = INT16_MAX;
×
417
          break;
×
418
        }
419

420
        TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
105,431,613✔
421
      }
422

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

429
    if (blockCol.cid > 0 && blockCol.cid < INT16_MAX /*&& blockCol->flag == HAS_VALUE*/) {
44,851,764✔
430
      int64_t   offset = blockCol.offset;
44,850,186✔
431
      SBlockCol lastNonNoneBlockCol = {.cid = 0};
44,850,186✔
432

433
      for (int32_t i = extraColIdx; i < ncid; ++i) {
241,997,016✔
434
        int16_t extraColCid = cids[i];
197,144,759✔
435

436
        while (extraColCid > blockCol.cid) {
542,341,697✔
437
          if (br.offset >= buffer0->size) {
345,194,867✔
438
            blockCol.cid = INT16_MAX;
×
439
            break;
×
440
          }
441

442
          TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
345,194,060✔
443
        }
444

445
        if (extraColCid == blockCol.cid) {
197,146,830✔
446
          lastNonNoneBlockCol = blockCol;
197,146,830✔
447
          continue;
197,146,830✔
448
        }
449

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

455
      if (lastNonNoneBlockCol.cid > 0) {
44,852,257✔
456
        szHint = lastNonNoneBlockCol.offset + lastNonNoneBlockCol.szBitmap + lastNonNoneBlockCol.szOffset +
44,848,655✔
457
                 lastNonNoneBlockCol.szValue - offset;
44,848,655✔
458
      }
459
    }
460
  }
461

462
  // load each column
463
  SBlockCol blockCol = {
83,940,684✔
464
      .cid = 0,
465
  };
466
  bool firstRead = true;
83,931,596✔
467
  br = BUFFER_READER_INITIALIZER(0, buffer0);
83,931,596✔
468
  for (int32_t i = 0; i < ncid; i++) {
320,188,906✔
469
    int16_t cid = cids[i];
236,214,832✔
470

471
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
236,242,084✔
472
      continue;
1,422✔
473
    }
474

475
    while (cid > blockCol.cid) {
838,976,117✔
476
      if (br.offset >= buffer0->size) {
602,672,105✔
477
        blockCol.cid = INT16_MAX;
×
478
        break;
×
479
      }
480

481
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
602,687,093✔
482
    }
483

484
    if (cid < blockCol.cid) {
236,304,012✔
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) {
236,304,012✔
500
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
236,242,053✔
501

502
      // load from file
503
      tBufferClear(buffer1);
504
      TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA],
236,217,553✔
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;
236,261,474✔
511

512
      // decode the buffer
513
      SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
236,261,474✔
514
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
236,263,606✔
515
    }
516
  }
517

518
_exit:
86,105,014✔
519
  if (code) {
86,023,556✔
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,023,556✔
524
}
525

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

532
  TARRAY2_CLEAR(columnDataAggArray, NULL);
32,840,158✔
533
  if (record->smaSize > 0) {
32,840,908✔
534
    tBufferClear(buffer);
535
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
32,843,009✔
536

537
    TAOS_CHECK_GOTO(
32,843,764✔
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,834,673✔
543
    while (br.offset < record->smaSize) {
413,525,923✔
544
      SColumnDataAgg sma[1];
380,627,647✔
545

546
      TAOS_CHECK_GOTO(tGetColumnDataAgg(&br, sma), &lino, _exit);
380,617,578✔
547
      TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(columnDataAggArray, sma), &lino, _exit);
761,221,137✔
548
    }
549
    if (br.offset != record->smaSize) {
32,844,956✔
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,833,606✔
558
  if (code) {
32,843,153✔
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,843,153✔
563
}
564

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

570
  if (!reader->ctx->tombBlkLoaded) {
4,965,058✔
571
    TAOS_CHECK_GOTO(tsdbDataFileReadTombFooter(reader), &lino, _exit);
3,061,168✔
572

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

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

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

584
      int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
2,909,934✔
585
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
2,909,934✔
586
    } else {
587
      TARRAY2_INIT(reader->tombBlkArray);
151,234✔
588
    }
589

590
    reader->ctx->tombBlkLoaded = true;
3,060,531✔
591
  }
592
  tombBlkArray[0] = reader->tombBlkArray;
4,965,058✔
593

594
_exit:
4,965,058✔
595
  if (code) {
4,965,058✔
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,965,058✔
601
}
602

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

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

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

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

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

632
_exit:
1,799,376✔
633
  if (code) {
1,799,376✔
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,799,376✔
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) {
782,799✔
695
  if (writer->ctx->reader) {
782,799✔
696
    tsdbDataFileReaderClose(&writer->ctx->reader);
381,794✔
697
  }
698

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

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

709
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
8,610,789✔
710
    tBufferDestroy(writer->local + i);
7,827,990✔
711
  }
712

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

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

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

729
      for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
1,908,970✔
730
        config->files[i].exist = writer->config->files[i].exist;
1,527,176✔
731
        if (config->files[i].exist) {
1,527,176✔
732
          config->files[i].file = writer->config->files[i].file;
945,020✔
733
        }
734
      }
735

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

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

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

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

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

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

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

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

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

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

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

844
void tsdbWriterUpdVerRange(SVersionRange *range, int64_t minVer, int64_t maxVer) {
76,110,069✔
845
  range->minVer = TMIN(range->minVer, minVer);
76,110,069✔
846
  range->maxVer = TMAX(range->maxVer, maxVer);
76,118,396✔
847
}
76,122,411✔
848

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

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

861
  SBrinBlk brinBlk = {
743,244✔
862
      .dp[0] =
863
          {
864
              .offset = *fileSize,
743,244✔
865
              .size = 0,
866
          },
867
      .numRec = brinBlock->numOfRecords,
743,244✔
868
      .numOfPKs = brinBlock->numOfPKs,
743,244✔
869
      .cmprAlg = cmprAlg,
870
  };
871
  for (int i = 0; i < brinBlock->numOfRecords; i++) {
47,276,564✔
872
    SBrinRecord record;
46,533,320✔
873

874
    TAOS_CHECK_RETURN(tBrinBlockGet(brinBlock, i, &record));
46,533,320✔
875
    if (i == 0) {
46,533,320✔
876
      brinBlk.minTbid.suid = record.suid;
743,244✔
877
      brinBlk.minTbid.uid = record.uid;
743,244✔
878
      brinBlk.minVer = record.minVer;
743,244✔
879
      brinBlk.maxVer = record.maxVer;
743,244✔
880
    }
881
    if (i == brinBlock->numOfRecords - 1) {
46,533,320✔
882
      brinBlk.maxTbid.suid = record.suid;
743,244✔
883
      brinBlk.maxTbid.uid = record.uid;
743,244✔
884
    }
885
    if (record.minVer < brinBlk.minVer) {
46,533,320✔
886
      brinBlk.minVer = record.minVer;
479,831✔
887
    }
888
    if (record.maxVer > brinBlk.maxVer) {
46,533,320✔
889
      brinBlk.maxVer = record.maxVer;
13,261,251✔
890
    }
891
  }
892

893
  tsdbWriterUpdVerRange(range, brinBlk.minVer, brinBlk.maxVer);
743,244✔
894

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

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

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

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

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

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

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

954
  tBrinBlockClear(brinBlock);
743,244✔
955

956
  return 0;
742,779✔
957
}
958

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

964
  int32_t code = 0;
743,244✔
965
  int32_t lino = 0;
743,244✔
966

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

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

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

982
static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
46,532,118✔
983
  int32_t code = 0;
46,532,118✔
984
  int32_t lino = 0;
46,532,118✔
985

986
  for (;;) {
987
    code = tBrinBlockPut(writer->brinBlock, record);
46,531,790✔
988
    if (code == TSDB_CODE_INVALID_PARA) {
46,525,206✔
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);
46,525,206✔
994
    }
995
    break;
46,525,206✔
996
  }
997

998
  if ((writer->brinBlock->numOfRecords) >= 256) {
46,525,206✔
999
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
94,932✔
1000
  }
1001

1002
_exit:
46,529,543✔
1003
  if (code) {
46,524,386✔
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;
46,524,386✔
1008
}
1009

1010
static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
45,819,173✔
1011
  if (bData->nRow == 0) {
45,819,173✔
1012
    return 0;
12,747,293✔
1013
  }
1014

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

1019
  int32_t  code = 0;
33,075,206✔
1020
  int32_t  lino = 0;
33,075,206✔
1021
  SBuffer *buffers = writer->buffers;
33,075,543✔
1022
  SBuffer *assist = writer->buffers + 4;
33,074,886✔
1023

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

1026
  SBrinRecord record[1] = {{
33,075,753✔
1027
      .suid = bData->suid,
33,073,556✔
1028
      .uid = bData->uid,
33,072,882✔
1029
      .minVer = bData->aVersion[0],
33,072,874✔
1030
      .maxVer = bData->aVersion[0],
33,072,883✔
1031
      .blockOffset = writer->files[TSDB_FTYPE_DATA].size,
33,074,599✔
1032
      .smaOffset = writer->files[TSDB_FTYPE_SMA].size,
33,074,271✔
1033
      .blockSize = 0,
1034
      .blockKeySize = 0,
1035
      .smaSize = 0,
1036
      .numRow = bData->nRow,
33,075,232✔
1037
      .count = 1,
1038
  }};
1039

1040
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &record->firstKey);
33,076,153✔
1041
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, bData->nRow - 1), &record->lastKey);
33,074,813✔
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,338,681✔
1049
    }
1050
    if (bData->aVersion[i] > record->maxVer) {
2,147,483,647✔
1051
      record->maxVer = bData->aVersion[i];
99,282,442✔
1052
    }
1053
  }
1054

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

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

1065
  record->blockKeySize = buffers[0].size + buffers[1].size;
33,068,211✔
1066
  record->blockSize = record->blockKeySize + buffers[2].size + buffers[3].size;
33,073,821✔
1067

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

1070
  for (int i = 0; i < 4; i++) {
165,373,168✔
1071
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, buffers[i].data,
132,296,687✔
1072
                                  buffers[i].size, pEncryptData),
1073
                    &lino, _exit);
1074
    writer->files[TSDB_FTYPE_DATA].size += buffers[i].size;
132,293,891✔
1075
  }
1076

1077
  // to .sma file
1078
  tBufferClear(&buffers[0]);
1079
  for (int32_t i = 0; i < bData->nColData; ++i) {
257,094,113✔
1080
    SColData *colData = bData->aColData + i;
224,021,005✔
1081
    if ((colData->cflag & COL_SMA_ON) == 0 || ((colData->flag & HAS_VALUE) == 0)) continue;
224,020,017✔
1082

1083
    SColumnDataAgg sma[1] = {{.colId = colData->cid}};
218,544,149✔
1084
    tColDataCalcSMA[colData->type](colData, sma);
218,540,397✔
1085

1086
    TAOS_CHECK_GOTO(tPutColumnDataAgg(&buffers[0], sma), &lino, _exit);
218,520,279✔
1087
  }
1088
  record->smaSize = buffers[0].size;
33,075,279✔
1089

1090
  if (record->smaSize > 0) {
33,075,880✔
1091
    TAOS_CHECK_GOTO(
33,070,849✔
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,071,122✔
1095
  }
1096

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

1100
  tBlockDataClear(bData);
33,069,273✔
1101

1102
_exit:
33,074,145✔
1103
  if (code) {
33,070,841✔
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,070,841✔
1108
  return code;
33,068,440✔
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,669,938✔
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;
519,186✔
1149
  } else if (key2 == NULL) {
2,147,483,647✔
1150
    return -1;
5,794,577✔
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,064✔
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,559,291✔
1181
        break;
181,149✔
1182
      }
1183

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

1193
        if (tsdbRowKeyCmprNullAsLargest(key, &record.firstKey) < 0) {  // key < record->firstKey
8,230,063✔
1194
          goto _exit;
3,845,151✔
1195
        } else {
1196
          SBrinRecord record[1];
4,384,912✔
1197
          code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
4,384,912✔
1198
          TSDB_CHECK_CODE(code, lino, _exit);
4,384,763✔
1199
          if (tsdbRowKeyCmprNullAsLargest(key, &record->lastKey) > 0) {  // key > record->lastKey
4,384,763✔
1200
            if (writer->blockData->nRow > 0) {
2,149,198✔
1201
              TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
1,527✔
1202
            }
1203

1204
            TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
2,149,198✔
1205
          } else {
1206
            TAOS_CHECK_GOTO(tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData), &lino,
2,235,565✔
1207
                            _exit);
1208

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

1217
    // SBrinBlk
1218
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
181,149✔
1219
      writer->ctx->brinBlkArray = NULL;
176,085✔
1220
      writer->ctx->tbHasOldData = false;
176,085✔
1221
      goto _exit;
176,085✔
1222
    } else {
1223
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
5,064✔
1224

1225
      if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
5,064✔
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,064✔
1231

1232
      writer->ctx->brinBlockIdx = 0;
5,064✔
1233
      writer->ctx->brinBlkArrayIdx++;
5,064✔
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,336,646✔
1266
  if (writer->ctx->tbid->uid == 0) {
14,336,646✔
1267
    return 0;
648,312✔
1268
  }
1269

1270
  int32_t code = 0;
13,689,000✔
1271
  int32_t lino = 0;
13,689,000✔
1272

1273
  if (writer->ctx->tbHasOldData) {
13,688,663✔
1274
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, NULL /* as the largest key */), &lino, _exit);
129,976✔
1275
  }
1276

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

1279
_exit:
13,688,326✔
1280
  if (code) {
13,688,326✔
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,688,334✔
1285
}
1286

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

1291
  SMetaInfo info;
14,336,646✔
1292
  bool      drop = false;
14,336,318✔
1293
  TABLEID   tbid1[1];
14,336,318✔
1294
  writer->ctx->tbHasOldData = false;
14,336,646✔
1295
  while (writer->ctx->brinBlkArray) {  // skip data of previous table
14,607,058✔
1296
    for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
14,219,513✔
1297
      SBrinRecord record;
13,876,123✔
1298
      TAOS_CHECK_GOTO(tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record), &lino, _exit);
13,876,123✔
1299

1300
      if (record.uid == tbid->uid) {
13,875,395✔
1301
        writer->ctx->tbHasOldData = true;
360,071✔
1302
        goto _begin;
360,071✔
1303
      } else if (record.suid > tbid->suid || (record.suid == tbid->suid && record.uid > tbid->uid)) {
13,516,052✔
1304
        goto _begin;
2,207,832✔
1305
      } else {
1306
        if (record.uid != writer->ctx->tbid->uid) {
11,306,764✔
1307
          if (drop && tbid1->uid == record.uid) {
3,935,690✔
1308
            continue;
×
1309
          } else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, record.uid, &info, NULL) != 0) {
3,935,690✔
UNCOV
1310
            drop = true;
×
UNCOV
1311
            tbid1->suid = record.suid;
×
UNCOV
1312
            tbid1->uid = record.uid;
×
UNCOV
1313
            continue;
×
1314
          } else {
1315
            drop = false;
3,935,690✔
1316
            writer->ctx->tbid->suid = record.suid;
3,935,690✔
1317
            writer->ctx->tbid->uid = record.uid;
3,935,690✔
1318
          }
1319
        }
1320

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

1325
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
343,390✔
1326
      writer->ctx->brinBlkArray = NULL;
72,978✔
1327
      break;
72,978✔
1328
    } else {
1329
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
270,412✔
1330

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

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

1338
_begin:
14,335,590✔
1339
  writer->ctx->tbid[0] = *tbid;
14,336,984✔
1340

1341
  if (tbid->uid == INT64_MAX) {
14,336,983✔
1342
    goto _exit;
648,312✔
1343
  }
1344

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

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

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

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

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

1372
  SBuffer *buffer0 = buffers + 0;
1,652,947✔
1373
  SBuffer *assist = buffers + 1;
1,652,947✔
1374

1375
  STombBlk tombBlk = {
1,652,947✔
1376
      .dp[0] =
1377
          {
1378
              .offset = *fileSize,
1,652,947✔
1379
              .size = 0,
1380
          },
1381
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
1,652,947✔
1382
      .cmprAlg = cmprAlg,
1383
  };
1384
  for (int i = 0; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
40,305,845✔
1385
    STombRecord record;
38,652,898✔
1386
    TAOS_CHECK_RETURN(tTombBlockGet(tombBlock, i, &record));
38,652,898✔
1387

1388
    if (i == 0) {
38,652,898✔
1389
      tombBlk.minTbid.suid = record.suid;
1,652,947✔
1390
      tombBlk.minTbid.uid = record.uid;
1,652,947✔
1391
      tombBlk.minVer = record.version;
1,652,947✔
1392
      tombBlk.maxVer = record.version;
1,652,947✔
1393
    }
1394
    if (i == TOMB_BLOCK_SIZE(tombBlock) - 1) {
38,652,898✔
1395
      tombBlk.maxTbid.suid = record.suid;
1,652,947✔
1396
      tombBlk.maxTbid.uid = record.uid;
1,652,947✔
1397
    }
1398
    if (record.version < tombBlk.minVer) {
38,652,898✔
1399
      tombBlk.minVer = record.version;
2,967✔
1400
    }
1401
    if (record.version > tombBlk.maxVer) {
38,652,898✔
1402
      tombBlk.maxVer = record.version;
34,797,428✔
1403
    }
1404
  }
1405

1406
  tsdbWriterUpdVerRange(range, tombBlk.minVer, tombBlk.maxVer);
1,652,947✔
1407

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

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

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

1424
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(tombBlkArray, &tombBlk));
3,305,894✔
1425

1426
  tTombBlockClear(tombBlock);
1,652,947✔
1427
  return 0;
1,652,947✔
1428
}
1429

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

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

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

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

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

1451
  int32_t code = 0;
136,988✔
1452
  int32_t lino = 0;
136,988✔
1453

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

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

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

1469
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize,
17,773,935✔
1470
                             SEncryptData *encryptData) {
1471
  ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
17,773,935✔
1472
  if (ptr->size > 0) {
17,778,859✔
1473
    ptr->offset = *fileSize;
1,652,947✔
1474

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

1478
    *fileSize += ptr->size;
1,652,947✔
1479
  }
1480
  return 0;
17,777,606✔
1481
}
1482

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

1488
  int32_t code = 0;
136,988✔
1489
  int32_t lino = 0;
136,988✔
1490

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

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

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

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

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

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

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

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

1534
  while (writer->ctx->hasOldTomb) {
633,613✔
1535
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
32,379,381✔
1536
      STombRecord record1[1];
32,178,089✔
1537
      TAOS_CHECK_GOTO(tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1), &lino, _exit);
32,178,089✔
1538

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

1545
        tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
32,178,089✔
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,178,089✔
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)) {
201,292✔
1561
      writer->ctx->hasOldTomb = false;
100,646✔
1562
      break;
100,646✔
1563
    } else {
1564
      const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);
100,646✔
1565

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

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

1573
_write:
532,967✔
1574
  if (record->suid == INT64_MAX) {
532,967✔
1575
    goto _exit;
136,988✔
1576
  }
1577

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

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

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

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

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

1607
  *fileSize += ptr->size;
647,847✔
1608
  return 0;
648,312✔
1609
}
1610

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

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

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

1630
void tsdbTFileUpdVerRange(STFile *f, SVersionRange range) {
20,494,923✔
1631
  f->minVer = TMIN(f->minVer, range.minVer);
20,494,923✔
1632
  f->maxVer = TMAX(f->maxVer, range.maxVer);
20,500,202✔
1633
}
20,505,599✔
1634

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

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

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

1648
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
648,312✔
1649
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, tbid), &lino, _exit);
648,312✔
1650
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
648,312✔
1651
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlk(writer), &lino, _exit);
648,312✔
1652
    TAOS_CHECK_GOTO(tsdbDataFileWriteHeadFooter(writer), &lino, _exit);
647,847✔
1653

1654
    SVersionRange ofRange = {.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
647,847✔
1655

1656
    // .head
1657
    ftype = TSDB_FTYPE_HEAD;
647,847✔
1658
    if (writer->config->files[ftype].exist) {
647,847✔
1659
      op = (STFileOp){
249,063✔
1660
          .optype = TSDB_FOP_REMOVE,
1661
          .fid = writer->config->fid,
249,063✔
1662
          .of = writer->config->files[ftype].file,
249,063✔
1663
      };
1664
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
249,063✔
1665
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
498,126✔
1666
    }
1667
    op = (STFileOp){
648,312✔
1668
        .optype = TSDB_FOP_CREATE,
1669
        .fid = writer->config->fid,
647,847✔
1670
        .nf = writer->files[ftype],
647,847✔
1671
    };
1672
    tsdbTFileUpdVerRange(&op.nf, ofRange);
648,312✔
1673
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
648,312✔
1674
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
1,296,159✔
1675

1676
    // .data
1677
    ftype = TSDB_FTYPE_DATA;
647,847✔
1678
    if (!writer->config->files[ftype].exist) {
647,847✔
1679
      op = (STFileOp){
399,249✔
1680
          .optype = TSDB_FOP_CREATE,
1681
          .fid = writer->config->fid,
398,784✔
1682
          .nf = writer->files[ftype],
398,784✔
1683
      };
1684
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
399,249✔
1685
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
798,033✔
1686
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
249,063✔
1687
      op = (STFileOp){
249,063✔
1688
          .optype = TSDB_FOP_MODIFY,
1689
          .fid = writer->config->fid,
249,063✔
1690
          .of = writer->config->files[ftype].file,
249,063✔
1691
          .nf = writer->files[ftype],
249,063✔
1692
      };
1693
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
249,063✔
1694
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
498,126✔
1695
    }
1696

1697
    // .sma
1698
    ftype = TSDB_FTYPE_SMA;
648,312✔
1699
    if (!writer->config->files[ftype].exist) {
648,312✔
1700
      op = (STFileOp){
399,249✔
1701
          .optype = TSDB_FOP_CREATE,
1702
          .fid = writer->config->fid,
398,784✔
1703
          .nf = writer->files[ftype],
398,784✔
1704
      };
1705
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
399,249✔
1706
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
797,568✔
1707
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
249,063✔
1708
      op = (STFileOp){
249,063✔
1709
          .optype = TSDB_FOP_MODIFY,
1710
          .fid = writer->config->fid,
249,063✔
1711
          .of = writer->config->files[ftype].file,
249,063✔
1712
          .nf = writer->files[ftype],
249,063✔
1713
      };
1714
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
249,063✔
1715
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
498,126✔
1716
    }
1717
  }
1718

1719
  if (writer->fd[TSDB_FTYPE_TOMB]) {
782,334✔
1720
    STombRecord record[1] = {{
136,988✔
1721
        .suid = INT64_MAX,
1722
        .uid = INT64_MAX,
1723
        .version = INT64_MAX,
1724
    }};
1725

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

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

1733
    ftype = TSDB_FTYPE_TOMB;
136,988✔
1734
    if (writer->config->files[ftype].exist) {
136,988✔
1735
      op = (STFileOp){
100,646✔
1736
          .optype = TSDB_FOP_REMOVE,
1737
          .fid = writer->config->fid,
100,646✔
1738
          .of = writer->config->files[ftype].file,
100,646✔
1739
      };
1740
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
100,646✔
1741
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
201,292✔
1742
    }
1743
    op = (STFileOp){
136,988✔
1744
        .optype = TSDB_FOP_CREATE,
1745
        .fid = writer->config->fid,
136,988✔
1746
        .nf = writer->files[ftype],
136,988✔
1747
    };
1748
    tsdbTFileUpdVerRange(&op.nf, ofRange);
136,988✔
1749
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->tombRange);
136,988✔
1750
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
273,976✔
1751
  }
1752
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
782,799✔
1753
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
3,913,530✔
1754
    if (writer->fd[i]) {
3,130,731✔
1755
      TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd[i], pEncryptData), &lino, _exit);
2,081,924✔
1756
      tsdbCloseFile(&writer->fd[i]);
2,081,924✔
1757
    }
1758
  }
1759

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

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

1772
  int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};
648,312✔
1773

1774
  for (int32_t i = 0; i < ARRAY_SIZE(ftypes); ++i) {
2,593,248✔
1775
    int32_t ftype = ftypes[i];
1,944,936✔
1776

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

1780
    if (writer->files[ftype].size == 0) {
1,944,936✔
1781
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
1,446,810✔
1782
    }
1783

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

1788
    if (writer->files[ftype].size == 0) {
1,944,471✔
1789
      uint8_t hdr[TSDB_FHDR_SIZE] = {0};
1,446,810✔
1790

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

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

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

1799
  if (writer->ctx->reader) {
648,312✔
1800
    TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray), &lino, _exit);
249,063✔
1801
  }
1802

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

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

1817
  writer[0]->config[0] = config[0];
4,325,288✔
1818
  return 0;
4,329,473✔
1819
}
1820

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

1824
  int32_t code = 0;
4,327,439✔
1825
  int32_t lino = 0;
4,327,439✔
1826

1827
  if (writer[0]->ctx->opened) {
4,329,473✔
1828
    if (abort) {
782,799✔
1829
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseAbort(writer[0]), &lino, _exit);
×
1830
    } else {
1831
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseCommit(writer[0], opArr), &lino, _exit);
782,799✔
1832
    }
1833
    tsdbDataFileWriterDoClose(writer[0]);
782,799✔
1834
  }
1835
  taosMemoryFree(writer[0]);
4,329,938✔
1836
  writer[0] = NULL;
4,327,613✔
1837

1838
_exit:
4,328,543✔
1839
  if (code) {
4,329,008✔
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,326,509✔
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);
79,961✔
1852
  }
1853

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

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

1878
  int32_t code = 0;
30,683,798✔
1879
  int32_t lino = 0;
30,683,798✔
1880

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

1885
  if (!writer->ctx->opened) {
30,684,399✔
1886
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
567,504✔
1887
  }
1888

1889
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
30,683,725✔
1890
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
568,351✔
1891
  }
1892

1893
  if (bData->uid != writer->ctx->tbid->uid) {
30,683,197✔
1894
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
13,524,933✔
1895
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData), &lino, _exit);
13,524,604✔
1896
  }
1897

1898
  if (writer->ctx->tbHasOldData) {
30,682,973✔
1899
    STsdbRowKey key;
2,118,755✔
1900

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

1905
  if (!writer->ctx->tbHasOldData       //
30,682,973✔
1906
      && writer->blockData->nRow == 0  //
28,785,490✔
1907
  ) {
1908
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, bData), &lino, _exit);
28,656,791✔
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,673,641✔
1918
  if (code) {
30,677,712✔
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,678,049✔
1923
}
1924

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

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

1933
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
805,293✔
1934
}
1935

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

1940
  char    fname[TSDB_FILENAME_LEN];
136,988✔
1941
  int32_t ftype = TSDB_FTYPE_TOMB;
136,988✔
1942

1943
  int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
136,988✔
1944

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

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

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

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

1956
  if (writer->ctx->reader) {
136,988✔
1957
    TAOS_CHECK_GOTO(tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray), &lino, _exit);
132,731✔
1958

1959
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
132,731✔
1960
      writer->ctx->hasOldTomb = true;
100,646✔
1961
    }
1962

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

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

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

1980
  if (!writer->ctx->opened) {
395,979✔
1981
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
135,334✔
1982
  }
1983

1984
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
395,979✔
1985
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenTombFD(writer), &lino, _exit);
136,988✔
1986
  }
1987

1988
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
395,979✔
1989

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